source: vendor/diffutils/2.8.1/lib/regex.c

Last change on this file was 2556, checked in by bird, 19 years ago

diffutils 2.8.1

File size: 254.8 KB
Line 
1/* Extended regular expression matching and search library,
2 version 0.12.
3 (Implements POSIX draft P1003.2/D11.2, except for some of the
4 internationalization features.)
5 Copyright (C) 1993-1999, 2000, 2001 Free Software Foundation, Inc.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software Foundation,
19 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20
21/* AIX requires this to be the first thing in the file. */
22#if defined _AIX && !defined REGEX_MALLOC
23 #pragma alloca
24#endif
25
26#undef _GNU_SOURCE
27#define _GNU_SOURCE
28
29#ifdef HAVE_CONFIG_H
30# include <config.h>
31#endif
32
33#ifndef PARAMS
34# if defined __GNUC__ || (defined __STDC__ && __STDC__)
35# define PARAMS(args) args
36# else
37# define PARAMS(args) ()
38# endif /* GCC. */
39#endif /* Not PARAMS. */
40
41#ifndef INSIDE_RECURSION
42
43# if defined STDC_HEADERS && !defined emacs
44# include <stddef.h>
45# else
46/* We need this for `regex.h', and perhaps for the Emacs include files. */
47# include <sys/types.h>
48# endif
49
50# define WIDE_CHAR_SUPPORT (HAVE_WCTYPE_H && HAVE_WCHAR_H && HAVE_BTOWC)
51
52/* For platform which support the ISO C amendement 1 functionality we
53 support user defined character classes. */
54# if defined _LIBC || WIDE_CHAR_SUPPORT
55/* Solaris 2.5 has a bug: <wchar.h> must be included before <wctype.h>. */
56# include <wchar.h>
57# include <wctype.h>
58# endif
59
60# ifdef _LIBC
61/* We have to keep the namespace clean. */
62# define regfree(preg) __regfree (preg)
63# define regexec(pr, st, nm, pm, ef) __regexec (pr, st, nm, pm, ef)
64# define regcomp(preg, pattern, cflags) __regcomp (preg, pattern, cflags)
65# define regerror(errcode, preg, errbuf, errbuf_size) \
66 __regerror(errcode, preg, errbuf, errbuf_size)
67# define re_set_registers(bu, re, nu, st, en) \
68 __re_set_registers (bu, re, nu, st, en)
69# define re_match_2(bufp, string1, size1, string2, size2, pos, regs, stop) \
70 __re_match_2 (bufp, string1, size1, string2, size2, pos, regs, stop)
71# define re_match(bufp, string, size, pos, regs) \
72 __re_match (bufp, string, size, pos, regs)
73# define re_search(bufp, string, size, startpos, range, regs) \
74 __re_search (bufp, string, size, startpos, range, regs)
75# define re_compile_pattern(pattern, length, bufp) \
76 __re_compile_pattern (pattern, length, bufp)
77# define re_set_syntax(syntax) __re_set_syntax (syntax)
78# define re_search_2(bufp, st1, s1, st2, s2, startpos, range, regs, stop) \
79 __re_search_2 (bufp, st1, s1, st2, s2, startpos, range, regs, stop)
80# define re_compile_fastmap(bufp) __re_compile_fastmap (bufp)
81
82# define btowc __btowc
83# define iswctype __iswctype
84# define mbrtowc __mbrtowc
85# define wcslen __wcslen
86# define wcscoll __wcscoll
87# define wcrtomb __wcrtomb
88
89/* We are also using some library internals. */
90# include <locale/localeinfo.h>
91# include <locale/elem-hash.h>
92# include <langinfo.h>
93# include <locale/coll-lookup.h>
94# endif
95
96/* This is for other GNU distributions with internationalized messages. */
97# if (HAVE_LIBINTL_H && ENABLE_NLS) || defined _LIBC
98# include <libintl.h>
99# ifdef _LIBC
100# undef gettext
101# define gettext(msgid) __dcgettext ("libc", msgid, LC_MESSAGES)
102# endif
103# else
104# define gettext(msgid) (msgid)
105# endif
106
107# ifndef gettext_noop
108/* This define is so xgettext can find the internationalizable
109 strings. */
110# define gettext_noop(String) String
111# endif
112
113/* Support for bounded pointers. */
114# if !defined _LIBC && !defined __BOUNDED_POINTERS__
115# define __bounded /* nothing */
116# define __unbounded /* nothing */
117# define __ptrvalue /* nothing */
118# endif
119
120/* The `emacs' switch turns on certain matching commands
121 that make sense only in Emacs. */
122# ifdef emacs
123
124# include "lisp.h"
125# include "buffer.h"
126# include "syntax.h"
127
128# else /* not emacs */
129
130/* If we are not linking with Emacs proper,
131 we can't use the relocating allocator
132 even if config.h says that we can. */
133# undef REL_ALLOC
134
135# if defined STDC_HEADERS || defined _LIBC
136# include <stdlib.h>
137# else
138char *malloc ();
139char *realloc ();
140# endif
141
142/* When used in Emacs's lib-src, we need to get bzero and bcopy somehow.
143 If nothing else has been done, use the method below. */
144# ifdef INHIBIT_STRING_HEADER
145# if !(defined HAVE_BZERO && defined HAVE_BCOPY)
146# if !defined bzero && !defined bcopy
147# undef INHIBIT_STRING_HEADER
148# endif
149# endif
150# endif
151
152/* This is the normal way of making sure we have a bcopy and a bzero.
153 This is used in most programs--a few other programs avoid this
154 by defining INHIBIT_STRING_HEADER. */
155# ifndef INHIBIT_STRING_HEADER
156# if defined HAVE_STRING_H || defined STDC_HEADERS || defined _LIBC
157# include <string.h>
158# ifndef bzero
159# ifndef _LIBC
160# define bzero(s, n) (memset (s, '\0', n), (s))
161# else
162# define bzero(s, n) __bzero (s, n)
163# endif
164# endif
165# else
166# include <strings.h>
167# ifndef memcmp
168# define memcmp(s1, s2, n) bcmp (s1, s2, n)
169# endif
170# ifndef memcpy
171# define memcpy(d, s, n) (bcopy (s, d, n), (d))
172# endif
173# endif
174# endif
175
176/* Define the syntax stuff for \<, \>, etc. */
177
178/* This must be nonzero for the wordchar and notwordchar pattern
179 commands in re_match_2. */
180# ifndef Sword
181# define Sword 1
182# endif
183
184# ifdef SWITCH_ENUM_BUG
185# define SWITCH_ENUM_CAST(x) ((int)(x))
186# else
187# define SWITCH_ENUM_CAST(x) (x)
188# endif
189
190# endif /* not emacs */
191
192# if defined _LIBC || HAVE_LIMITS_H
193# include <limits.h>
194# endif
195
196# ifndef MB_LEN_MAX
197# define MB_LEN_MAX 1
198# endif
199
200
201/* Get the interface, including the syntax bits. */
202# include <regex.h>
203
204/* isalpha etc. are used for the character classes. */
205# include <ctype.h>
206
207/* Jim Meyering writes:
208
209 "... Some ctype macros are valid only for character codes that
210 isascii says are ASCII (SGI's IRIX-4.0.5 is one such system --when
211 using /bin/cc or gcc but without giving an ansi option). So, all
212 ctype uses should be through macros like ISPRINT... If
213 STDC_HEADERS is defined, then autoconf has verified that the ctype
214 macros don't need to be guarded with references to isascii. ...
215 Defining isascii to 1 should let any compiler worth its salt
216 eliminate the && through constant folding."
217 Solaris defines some of these symbols so we must undefine them first. */
218
219# if defined STDC_HEADERS || (!defined isascii && !defined HAVE_ISASCII)
220# define IN_CTYPE_DOMAIN(c) 1
221# else
222# define IN_CTYPE_DOMAIN(c) isascii(c)
223# endif
224
225# ifdef isblank
226# define ISBLANK(c) (IN_CTYPE_DOMAIN (c) && isblank (c))
227# else
228# define ISBLANK(c) ((c) == ' ' || (c) == '\t')
229# endif
230# ifdef isgraph
231# define ISGRAPH(c) (IN_CTYPE_DOMAIN (c) && isgraph (c))
232# else
233# define ISGRAPH(c) (IN_CTYPE_DOMAIN (c) && isprint (c) && !isspace (c))
234# endif
235
236# undef ISPRINT
237# define ISPRINT(c) (IN_CTYPE_DOMAIN (c) && isprint (c))
238# define ISDIGIT(c) (IN_CTYPE_DOMAIN (c) && isdigit (c))
239# define ISALNUM(c) (IN_CTYPE_DOMAIN (c) && isalnum (c))
240# define ISALPHA(c) (IN_CTYPE_DOMAIN (c) && isalpha (c))
241# define ISCNTRL(c) (IN_CTYPE_DOMAIN (c) && iscntrl (c))
242# define ISLOWER(c) (IN_CTYPE_DOMAIN (c) && islower (c))
243# define ISPUNCT(c) (IN_CTYPE_DOMAIN (c) && ispunct (c))
244# define ISSPACE(c) (IN_CTYPE_DOMAIN (c) && isspace (c))
245# define ISUPPER(c) (IN_CTYPE_DOMAIN (c) && isupper (c))
246# define ISXDIGIT(c) (IN_CTYPE_DOMAIN (c) && isxdigit (c))
247
248# ifdef _tolower
249# define TOLOWER(c) _tolower(c)
250# else
251# define TOLOWER(c) tolower(c)
252# endif
253
254# ifndef NULL
255# define NULL (void *)0
256# endif
257
258/* We remove any previous definition of `SIGN_EXTEND_CHAR',
259 since ours (we hope) works properly with all combinations of
260 machines, compilers, `char' and `unsigned char' argument types.
261 (Per Bothner suggested the basic approach.) */
262# undef SIGN_EXTEND_CHAR
263# if __STDC__
264# define SIGN_EXTEND_CHAR(c) ((signed char) (c))
265# else /* not __STDC__ */
266/* As in Harbison and Steele. */
267# define SIGN_EXTEND_CHAR(c) ((((unsigned char) (c)) ^ 128) - 128)
268# endif
269
270
271# ifndef emacs
272/* How many characters in the character set. */
273# define CHAR_SET_SIZE 256
274
275# ifdef SYNTAX_TABLE
276
277extern char *re_syntax_table;
278
279# else /* not SYNTAX_TABLE */
280
281static char re_syntax_table[CHAR_SET_SIZE];
282
283static void init_syntax_once PARAMS ((void));
284
285static void
286init_syntax_once ()
287{
288 register int c;
289 static int done = 0;
290
291 if (done)
292 return;
293 bzero (re_syntax_table, sizeof re_syntax_table);
294
295 for (c = 0; c < CHAR_SET_SIZE; ++c)
296 if (ISALNUM (c))
297 re_syntax_table[c] = Sword;
298
299 re_syntax_table['_'] = Sword;
300
301 done = 1;
302}
303
304# endif /* not SYNTAX_TABLE */
305
306# define SYNTAX(c) re_syntax_table[(unsigned char) (c)]
307
308# endif /* emacs */
309
310
311/* Should we use malloc or alloca? If REGEX_MALLOC is not defined, we
312 use `alloca' instead of `malloc'. This is because using malloc in
313 re_search* or re_match* could cause memory leaks when C-g is used in
314 Emacs; also, malloc is slower and causes storage fragmentation. On
315 the other hand, malloc is more portable, and easier to debug.
316
317 Because we sometimes use alloca, some routines have to be macros,
318 not functions -- `alloca'-allocated space disappears at the end of the
319 function it is called in. */
320
321# ifdef REGEX_MALLOC
322
323# define REGEX_ALLOCATE malloc
324# define REGEX_REALLOCATE(source, osize, nsize) realloc (source, nsize)
325# define REGEX_FREE free
326
327# else /* not REGEX_MALLOC */
328
329/* Emacs already defines alloca, sometimes. */
330# ifndef alloca
331
332/* Make alloca work the best possible way. */
333# ifdef __GNUC__
334# define alloca __builtin_alloca
335# else /* not __GNUC__ */
336# if HAVE_ALLOCA_H
337# include <alloca.h>
338# endif /* HAVE_ALLOCA_H */
339# endif /* not __GNUC__ */
340
341# endif /* not alloca */
342
343# define REGEX_ALLOCATE alloca
344
345/* Assumes a `char *destination' variable. */
346# define REGEX_REALLOCATE(source, osize, nsize) \
347 (destination = (char *) alloca (nsize), \
348 memcpy (destination, source, osize))
349
350/* No need to do anything to free, after alloca. */
351# define REGEX_FREE(arg) ((void)0) /* Do nothing! But inhibit gcc warning. */
352
353# endif /* not REGEX_MALLOC */
354
355/* Define how to allocate the failure stack. */
356
357# if defined REL_ALLOC && defined REGEX_MALLOC
358
359# define REGEX_ALLOCATE_STACK(size) \
360 r_alloc (&failure_stack_ptr, (size))
361# define REGEX_REALLOCATE_STACK(source, osize, nsize) \
362 r_re_alloc (&failure_stack_ptr, (nsize))
363# define REGEX_FREE_STACK(ptr) \
364 r_alloc_free (&failure_stack_ptr)
365
366# else /* not using relocating allocator */
367
368# ifdef REGEX_MALLOC
369
370# define REGEX_ALLOCATE_STACK malloc
371# define REGEX_REALLOCATE_STACK(source, osize, nsize) realloc (source, nsize)
372# define REGEX_FREE_STACK free
373
374# else /* not REGEX_MALLOC */
375
376# define REGEX_ALLOCATE_STACK alloca
377
378# define REGEX_REALLOCATE_STACK(source, osize, nsize) \
379 REGEX_REALLOCATE (source, osize, nsize)
380/* No need to explicitly free anything. */
381# define REGEX_FREE_STACK(arg)
382
383# endif /* not REGEX_MALLOC */
384# endif /* not using relocating allocator */
385
386
387/* True if `size1' is non-NULL and PTR is pointing anywhere inside
388 `string1' or just past its end. This works if PTR is NULL, which is
389 a good thing. */
390# define FIRST_STRING_P(ptr) \
391 (size1 && string1 <= (ptr) && (ptr) <= string1 + size1)
392
393/* (Re)Allocate N items of type T using malloc, or fail. */
394# define TALLOC(n, t) ((t *) malloc ((n) * sizeof (t)))
395# define RETALLOC(addr, n, t) ((addr) = (t *) realloc (addr, (n) * sizeof (t)))
396# define RETALLOC_IF(addr, n, t) \
397 if (addr) RETALLOC((addr), (n), t); else (addr) = TALLOC ((n), t)
398# define REGEX_TALLOC(n, t) ((t *) REGEX_ALLOCATE ((n) * sizeof (t)))
399
400# define BYTEWIDTH 8 /* In bits. */
401
402# define STREQ(s1, s2) ((strcmp (s1, s2) == 0))
403
404# undef MAX
405# undef MIN
406# define MAX(a, b) ((a) > (b) ? (a) : (b))
407# define MIN(a, b) ((a) < (b) ? (a) : (b))
408
409typedef char boolean;
410# define false 0
411# define true 1
412
413static reg_errcode_t byte_regex_compile _RE_ARGS ((const char *pattern, size_t size,
414 reg_syntax_t syntax,
415 struct re_pattern_buffer *bufp));
416
417static int byte_re_match_2_internal PARAMS ((struct re_pattern_buffer *bufp,
418 const char *string1, int size1,
419 const char *string2, int size2,
420 int pos,
421 struct re_registers *regs,
422 int stop));
423static int byte_re_search_2 PARAMS ((struct re_pattern_buffer *bufp,
424 const char *string1, int size1,
425 const char *string2, int size2,
426 int startpos, int range,
427 struct re_registers *regs, int stop));
428static int byte_re_compile_fastmap PARAMS ((struct re_pattern_buffer *bufp));
429
430#ifdef MBS_SUPPORT
431static reg_errcode_t wcs_regex_compile _RE_ARGS ((const char *pattern, size_t size,
432 reg_syntax_t syntax,
433 struct re_pattern_buffer *bufp));
434
435
436static int wcs_re_match_2_internal PARAMS ((struct re_pattern_buffer *bufp,
437 const char *cstring1, int csize1,
438 const char *cstring2, int csize2,
439 int pos,
440 struct re_registers *regs,
441 int stop,
442 wchar_t *string1, int size1,
443 wchar_t *string2, int size2,
444 int *mbs_offset1, int *mbs_offset2));
445static int wcs_re_search_2 PARAMS ((struct re_pattern_buffer *bufp,
446 const char *string1, int size1,
447 const char *string2, int size2,
448 int startpos, int range,
449 struct re_registers *regs, int stop));
450static int wcs_re_compile_fastmap PARAMS ((struct re_pattern_buffer *bufp));
451#endif
452
453
454/* These are the command codes that appear in compiled regular
455 expressions. Some opcodes are followed by argument bytes. A
456 command code can specify any interpretation whatsoever for its
457 arguments. Zero bytes may appear in the compiled regular expression. */
458
459typedef enum
460{
461 no_op = 0,
462
463 /* Succeed right away--no more backtracking. */
464 succeed,
465
466 /* Followed by one byte giving n, then by n literal bytes. */
467 exactn,
468
469# ifdef MBS_SUPPORT
470 /* Same as exactn, but contains binary data. */
471 exactn_bin,
472# endif
473
474 /* Matches any (more or less) character. */
475 anychar,
476
477 /* Matches any one char belonging to specified set. First
478 following byte is number of bitmap bytes. Then come bytes
479 for a bitmap saying which chars are in. Bits in each byte
480 are ordered low-bit-first. A character is in the set if its
481 bit is 1. A character too large to have a bit in the map is
482 automatically not in the set. */
483 /* ifdef MBS_SUPPORT, following element is length of character
484 classes, length of collating symbols, length of equivalence
485 classes, length of character ranges, and length of characters.
486 Next, character class element, collating symbols elements,
487 equivalence class elements, range elements, and character
488 elements follow.
489 See regex_compile function. */
490 charset,
491
492 /* Same parameters as charset, but match any character that is
493 not one of those specified. */
494 charset_not,
495
496 /* Start remembering the text that is matched, for storing in a
497 register. Followed by one byte with the register number, in
498 the range 0 to one less than the pattern buffer's re_nsub
499 field. Then followed by one byte with the number of groups
500 inner to this one. (This last has to be part of the
501 start_memory only because we need it in the on_failure_jump
502 of re_match_2.) */
503 start_memory,
504
505 /* Stop remembering the text that is matched and store it in a
506 memory register. Followed by one byte with the register
507 number, in the range 0 to one less than `re_nsub' in the
508 pattern buffer, and one byte with the number of inner groups,
509 just like `start_memory'. (We need the number of inner
510 groups here because we don't have any easy way of finding the
511 corresponding start_memory when we're at a stop_memory.) */
512 stop_memory,
513
514 /* Match a duplicate of something remembered. Followed by one
515 byte containing the register number. */
516 duplicate,
517
518 /* Fail unless at beginning of line. */
519 begline,
520
521 /* Fail unless at end of line. */
522 endline,
523
524 /* Succeeds if at beginning of buffer (if emacs) or at beginning
525 of string to be matched (if not). */
526 begbuf,
527
528 /* Analogously, for end of buffer/string. */
529 endbuf,
530
531 /* Followed by two byte relative address to which to jump. */
532 jump,
533
534 /* Same as jump, but marks the end of an alternative. */
535 jump_past_alt,
536
537 /* Followed by two-byte relative address of place to resume at
538 in case of failure. */
539 /* ifdef MBS_SUPPORT, the size of address is 1. */
540 on_failure_jump,
541
542 /* Like on_failure_jump, but pushes a placeholder instead of the
543 current string position when executed. */
544 on_failure_keep_string_jump,
545
546 /* Throw away latest failure point and then jump to following
547 two-byte relative address. */
548 /* ifdef MBS_SUPPORT, the size of address is 1. */
549 pop_failure_jump,
550
551 /* Change to pop_failure_jump if know won't have to backtrack to
552 match; otherwise change to jump. This is used to jump
553 back to the beginning of a repeat. If what follows this jump
554 clearly won't match what the repeat does, such that we can be
555 sure that there is no use backtracking out of repetitions
556 already matched, then we change it to a pop_failure_jump.
557 Followed by two-byte address. */
558 /* ifdef MBS_SUPPORT, the size of address is 1. */
559 maybe_pop_jump,
560
561 /* Jump to following two-byte address, and push a dummy failure
562 point. This failure point will be thrown away if an attempt
563 is made to use it for a failure. A `+' construct makes this
564 before the first repeat. Also used as an intermediary kind
565 of jump when compiling an alternative. */
566 /* ifdef MBS_SUPPORT, the size of address is 1. */
567 dummy_failure_jump,
568
569 /* Push a dummy failure point and continue. Used at the end of
570 alternatives. */
571 push_dummy_failure,
572
573 /* Followed by two-byte relative address and two-byte number n.
574 After matching N times, jump to the address upon failure. */
575 /* ifdef MBS_SUPPORT, the size of address is 1. */
576 succeed_n,
577
578 /* Followed by two-byte relative address, and two-byte number n.
579 Jump to the address N times, then fail. */
580 /* ifdef MBS_SUPPORT, the size of address is 1. */
581 jump_n,
582
583 /* Set the following two-byte relative address to the
584 subsequent two-byte number. The address *includes* the two
585 bytes of number. */
586 /* ifdef MBS_SUPPORT, the size of address is 1. */
587 set_number_at,
588
589 wordchar, /* Matches any word-constituent character. */
590 notwordchar, /* Matches any char that is not a word-constituent. */
591
592 wordbeg, /* Succeeds if at word beginning. */
593 wordend, /* Succeeds if at word end. */
594
595 wordbound, /* Succeeds if at a word boundary. */
596 notwordbound /* Succeeds if not at a word boundary. */
597
598# ifdef emacs
599 ,before_dot, /* Succeeds if before point. */
600 at_dot, /* Succeeds if at point. */
601 after_dot, /* Succeeds if after point. */
602
603 /* Matches any character whose syntax is specified. Followed by
604 a byte which contains a syntax code, e.g., Sword. */
605 syntaxspec,
606
607 /* Matches any character whose syntax is not that specified. */
608 notsyntaxspec
609# endif /* emacs */
610} re_opcode_t;
611#endif /* not INSIDE_RECURSION */
612
613
614
615#ifdef BYTE
616# define CHAR_T char
617# define UCHAR_T unsigned char
618# define COMPILED_BUFFER_VAR bufp->buffer
619# define OFFSET_ADDRESS_SIZE 2
620# define PREFIX(name) byte_##name
621# define ARG_PREFIX(name) name
622# define PUT_CHAR(c) putchar (c)
623#else
624# ifdef WCHAR
625# define CHAR_T wchar_t
626# define UCHAR_T wchar_t
627# define COMPILED_BUFFER_VAR wc_buffer
628# define OFFSET_ADDRESS_SIZE 1 /* the size which STORE_NUMBER macro use */
629# define CHAR_CLASS_SIZE ((__alignof__(wctype_t)+sizeof(wctype_t))/sizeof(CHAR_T)+1)
630# define PREFIX(name) wcs_##name
631# define ARG_PREFIX(name) c##name
632/* Should we use wide stream?? */
633# define PUT_CHAR(c) printf ("%C", c);
634# define TRUE 1
635# define FALSE 0
636# else
637# ifdef MBS_SUPPORT
638# define WCHAR
639# define INSIDE_RECURSION
640# include "regex.c"
641# undef INSIDE_RECURSION
642# endif
643# define BYTE
644# define INSIDE_RECURSION
645# include "regex.c"
646# undef INSIDE_RECURSION
647# endif
648#endif
649#include "unlocked-io.h"
650
651#ifdef INSIDE_RECURSION
652/* Common operations on the compiled pattern. */
653
654/* Store NUMBER in two contiguous bytes starting at DESTINATION. */
655/* ifdef MBS_SUPPORT, we store NUMBER in 1 element. */
656
657# ifdef WCHAR
658# define STORE_NUMBER(destination, number) \
659 do { \
660 *(destination) = (UCHAR_T)(number); \
661 } while (0)
662# else /* BYTE */
663# define STORE_NUMBER(destination, number) \
664 do { \
665 (destination)[0] = (number) & 0377; \
666 (destination)[1] = (number) >> 8; \
667 } while (0)
668# endif /* WCHAR */
669
670/* Same as STORE_NUMBER, except increment DESTINATION to
671 the byte after where the number is stored. Therefore, DESTINATION
672 must be an lvalue. */
673/* ifdef MBS_SUPPORT, we store NUMBER in 1 element. */
674
675# define STORE_NUMBER_AND_INCR(destination, number) \
676 do { \
677 STORE_NUMBER (destination, number); \
678 (destination) += OFFSET_ADDRESS_SIZE; \
679 } while (0)
680
681/* Put into DESTINATION a number stored in two contiguous bytes starting
682 at SOURCE. */
683/* ifdef MBS_SUPPORT, we store NUMBER in 1 element. */
684
685# ifdef WCHAR
686# define EXTRACT_NUMBER(destination, source) \
687 do { \
688 (destination) = *(source); \
689 } while (0)
690# else /* BYTE */
691# define EXTRACT_NUMBER(destination, source) \
692 do { \
693 (destination) = *(source) & 0377; \
694 (destination) += SIGN_EXTEND_CHAR (*((source) + 1)) << 8; \
695 } while (0)
696# endif
697
698# ifdef DEBUG
699static void PREFIX(extract_number) _RE_ARGS ((int *dest, UCHAR_T *source));
700static void
701PREFIX(extract_number) (dest, source)
702 int *dest;
703 UCHAR_T *source;
704{
705# ifdef WCHAR
706 *dest = *source;
707# else /* BYTE */
708 int temp = SIGN_EXTEND_CHAR (*(source + 1));
709 *dest = *source & 0377;
710 *dest += temp << 8;
711# endif
712}
713
714# ifndef EXTRACT_MACROS /* To debug the macros. */
715# undef EXTRACT_NUMBER
716# define EXTRACT_NUMBER(dest, src) PREFIX(extract_number) (&dest, src)
717# endif /* not EXTRACT_MACROS */
718
719# endif /* DEBUG */
720
721/* Same as EXTRACT_NUMBER, except increment SOURCE to after the number.
722 SOURCE must be an lvalue. */
723
724# define EXTRACT_NUMBER_AND_INCR(destination, source) \
725 do { \
726 EXTRACT_NUMBER (destination, source); \
727 (source) += OFFSET_ADDRESS_SIZE; \
728 } while (0)
729
730# ifdef DEBUG
731static void PREFIX(extract_number_and_incr) _RE_ARGS ((int *destination,
732 UCHAR_T **source));
733static void
734PREFIX(extract_number_and_incr) (destination, source)
735 int *destination;
736 UCHAR_T **source;
737{
738 PREFIX(extract_number) (destination, *source);
739 *source += OFFSET_ADDRESS_SIZE;
740}
741
742# ifndef EXTRACT_MACROS
743# undef EXTRACT_NUMBER_AND_INCR
744# define EXTRACT_NUMBER_AND_INCR(dest, src) \
745 PREFIX(extract_number_and_incr) (&dest, &src)
746# endif /* not EXTRACT_MACROS */
747
748# endif /* DEBUG */
749
750
751
752
753/* If DEBUG is defined, Regex prints many voluminous messages about what
754 it is doing (if the variable `debug' is nonzero). If linked with the
755 main program in `iregex.c', you can enter patterns and strings
756 interactively. And if linked with the main program in `main.c' and
757 the other test files, you can run the already-written tests. */
758
759# ifdef DEBUG
760
761# ifndef DEFINED_ONCE
762
763/* We use standard I/O for debugging. */
764# include <stdio.h>
765
766/* It is useful to test things that ``must'' be true when debugging. */
767# include <assert.h>
768
769static int debug;
770
771# define DEBUG_STATEMENT(e) e
772# define DEBUG_PRINT1(x) if (debug) printf (x)
773# define DEBUG_PRINT2(x1, x2) if (debug) printf (x1, x2)
774# define DEBUG_PRINT3(x1, x2, x3) if (debug) printf (x1, x2, x3)
775# define DEBUG_PRINT4(x1, x2, x3, x4) if (debug) printf (x1, x2, x3, x4)
776# endif /* not DEFINED_ONCE */
777
778# define DEBUG_PRINT_COMPILED_PATTERN(p, s, e) \
779 if (debug) PREFIX(print_partial_compiled_pattern) (s, e)
780# define DEBUG_PRINT_DOUBLE_STRING(w, s1, sz1, s2, sz2) \
781 if (debug) PREFIX(print_double_string) (w, s1, sz1, s2, sz2)
782
783
784/* Print the fastmap in human-readable form. */
785
786# ifndef DEFINED_ONCE
787void
788print_fastmap (fastmap)
789 char *fastmap;
790{
791 unsigned was_a_range = 0;
792 unsigned i = 0;
793
794 while (i < (1 << BYTEWIDTH))
795 {
796 if (fastmap[i++])
797 {
798 was_a_range = 0;
799 putchar (i - 1);
800 while (i < (1 << BYTEWIDTH) && fastmap[i])
801 {
802 was_a_range = 1;
803 i++;
804 }
805 if (was_a_range)
806 {
807 printf ("-");
808 putchar (i - 1);
809 }
810 }
811 }
812 putchar ('\n');
813}
814# endif /* not DEFINED_ONCE */
815
816
817/* Print a compiled pattern string in human-readable form, starting at
818 the START pointer into it and ending just before the pointer END. */
819
820void
821PREFIX(print_partial_compiled_pattern) (start, end)
822 UCHAR_T *start;
823 UCHAR_T *end;
824{
825 int mcnt, mcnt2;
826 UCHAR_T *p1;
827 UCHAR_T *p = start;
828 UCHAR_T *pend = end;
829
830 if (start == NULL)
831 {
832 printf ("(null)\n");
833 return;
834 }
835
836 /* Loop over pattern commands. */
837 while (p < pend)
838 {
839# ifdef _LIBC
840 printf ("%td:\t", p - start);
841# else
842 printf ("%ld:\t", (long int) (p - start));
843# endif
844
845 switch ((re_opcode_t) *p++)
846 {
847 case no_op:
848 printf ("/no_op");
849 break;
850
851 case exactn:
852 mcnt = *p++;
853 printf ("/exactn/%d", mcnt);
854 do
855 {
856 putchar ('/');
857 PUT_CHAR (*p++);
858 }
859 while (--mcnt);
860 break;
861
862# ifdef MBS_SUPPORT
863 case exactn_bin:
864 mcnt = *p++;
865 printf ("/exactn_bin/%d", mcnt);
866 do
867 {
868 printf("/%lx", (long int) *p++);
869 }
870 while (--mcnt);
871 break;
872# endif /* MBS_SUPPORT */
873
874 case start_memory:
875 mcnt = *p++;
876 printf ("/start_memory/%d/%ld", mcnt, (long int) *p++);
877 break;
878
879 case stop_memory:
880 mcnt = *p++;
881 printf ("/stop_memory/%d/%ld", mcnt, (long int) *p++);
882 break;
883
884 case duplicate:
885 printf ("/duplicate/%ld", (long int) *p++);
886 break;
887
888 case anychar:
889 printf ("/anychar");
890 break;
891
892 case charset:
893 case charset_not:
894 {
895# ifdef WCHAR
896 int i, length;
897 wchar_t *workp = p;
898 printf ("/charset [%s",
899 (re_opcode_t) *(workp - 1) == charset_not ? "^" : "");
900 p += 5;
901 length = *workp++; /* the length of char_classes */
902 for (i=0 ; i<length ; i++)
903 printf("[:%lx:]", (long int) *p++);
904 length = *workp++; /* the length of collating_symbol */
905 for (i=0 ; i<length ;)
906 {
907 printf("[.");
908 while(*p != 0)
909 PUT_CHAR((i++,*p++));
910 i++,p++;
911 printf(".]");
912 }
913 length = *workp++; /* the length of equivalence_class */
914 for (i=0 ; i<length ;)
915 {
916 printf("[=");
917 while(*p != 0)
918 PUT_CHAR((i++,*p++));
919 i++,p++;
920 printf("=]");
921 }
922 length = *workp++; /* the length of char_range */
923 for (i=0 ; i<length ; i++)
924 {
925 wchar_t range_start = *p++;
926 wchar_t range_end = *p++;
927 printf("%C-%C", range_start, range_end);
928 }
929 length = *workp++; /* the length of char */
930 for (i=0 ; i<length ; i++)
931 printf("%C", *p++);
932 putchar (']');
933# else
934 register int c, last = -100;
935 register int in_range = 0;
936
937 printf ("/charset [%s",
938 (re_opcode_t) *(p - 1) == charset_not ? "^" : "");
939
940 assert (p + *p < pend);
941
942 for (c = 0; c < 256; c++)
943 if (c / 8 < *p
944 && (p[1 + (c/8)] & (1 << (c % 8))))
945 {
946 /* Are we starting a range? */
947 if (last + 1 == c && ! in_range)
948 {
949 putchar ('-');
950 in_range = 1;
951 }
952 /* Have we broken a range? */
953 else if (last + 1 != c && in_range)
954 {
955 putchar (last);
956 in_range = 0;
957 }
958
959 if (! in_range)
960 putchar (c);
961
962 last = c;
963 }
964
965 if (in_range)
966 putchar (last);
967
968 putchar (']');
969
970 p += 1 + *p;
971# endif /* WCHAR */
972 }
973 break;
974
975 case begline:
976 printf ("/begline");
977 break;
978
979 case endline:
980 printf ("/endline");
981 break;
982
983 case on_failure_jump:
984 PREFIX(extract_number_and_incr) (&mcnt, &p);
985# ifdef _LIBC
986 printf ("/on_failure_jump to %td", p + mcnt - start);
987# else
988 printf ("/on_failure_jump to %ld", (long int) (p + mcnt - start));
989# endif
990 break;
991
992 case on_failure_keep_string_jump:
993 PREFIX(extract_number_and_incr) (&mcnt, &p);
994# ifdef _LIBC
995 printf ("/on_failure_keep_string_jump to %td", p + mcnt - start);
996# else
997 printf ("/on_failure_keep_string_jump to %ld",
998 (long int) (p + mcnt - start));
999# endif
1000 break;
1001
1002 case dummy_failure_jump:
1003 PREFIX(extract_number_and_incr) (&mcnt, &p);
1004# ifdef _LIBC
1005 printf ("/dummy_failure_jump to %td", p + mcnt - start);
1006# else
1007 printf ("/dummy_failure_jump to %ld", (long int) (p + mcnt - start));
1008# endif
1009 break;
1010
1011 case push_dummy_failure:
1012 printf ("/push_dummy_failure");
1013 break;
1014
1015 case maybe_pop_jump:
1016 PREFIX(extract_number_and_incr) (&mcnt, &p);
1017# ifdef _LIBC
1018 printf ("/maybe_pop_jump to %td", p + mcnt - start);
1019# else
1020 printf ("/maybe_pop_jump to %ld", (long int) (p + mcnt - start));
1021# endif
1022 break;
1023
1024 case pop_failure_jump:
1025 PREFIX(extract_number_and_incr) (&mcnt, &p);
1026# ifdef _LIBC
1027 printf ("/pop_failure_jump to %td", p + mcnt - start);
1028# else
1029 printf ("/pop_failure_jump to %ld", (long int) (p + mcnt - start));
1030# endif
1031 break;
1032
1033 case jump_past_alt:
1034 PREFIX(extract_number_and_incr) (&mcnt, &p);
1035# ifdef _LIBC
1036 printf ("/jump_past_alt to %td", p + mcnt - start);
1037# else
1038 printf ("/jump_past_alt to %ld", (long int) (p + mcnt - start));
1039# endif
1040 break;
1041
1042 case jump:
1043 PREFIX(extract_number_and_incr) (&mcnt, &p);
1044# ifdef _LIBC
1045 printf ("/jump to %td", p + mcnt - start);
1046# else
1047 printf ("/jump to %ld", (long int) (p + mcnt - start));
1048# endif
1049 break;
1050
1051 case succeed_n:
1052 PREFIX(extract_number_and_incr) (&mcnt, &p);
1053 p1 = p + mcnt;
1054 PREFIX(extract_number_and_incr) (&mcnt2, &p);
1055# ifdef _LIBC
1056 printf ("/succeed_n to %td, %d times", p1 - start, mcnt2);
1057# else
1058 printf ("/succeed_n to %ld, %d times",
1059 (long int) (p1 - start), mcnt2);
1060# endif
1061 break;
1062
1063 case jump_n:
1064 PREFIX(extract_number_and_incr) (&mcnt, &p);
1065 p1 = p + mcnt;
1066 PREFIX(extract_number_and_incr) (&mcnt2, &p);
1067 printf ("/jump_n to %d, %d times", p1 - start, mcnt2);
1068 break;
1069
1070 case set_number_at:
1071 PREFIX(extract_number_and_incr) (&mcnt, &p);
1072 p1 = p + mcnt;
1073 PREFIX(extract_number_and_incr) (&mcnt2, &p);
1074# ifdef _LIBC
1075 printf ("/set_number_at location %td to %d", p1 - start, mcnt2);
1076# else
1077 printf ("/set_number_at location %ld to %d",
1078 (long int) (p1 - start), mcnt2);
1079# endif
1080 break;
1081
1082 case wordbound:
1083 printf ("/wordbound");
1084 break;
1085
1086 case notwordbound:
1087 printf ("/notwordbound");
1088 break;
1089
1090 case wordbeg:
1091 printf ("/wordbeg");
1092 break;
1093
1094 case wordend:
1095 printf ("/wordend");
1096 break;
1097
1098# ifdef emacs
1099 case before_dot:
1100 printf ("/before_dot");
1101 break;
1102
1103 case at_dot:
1104 printf ("/at_dot");
1105 break;
1106
1107 case after_dot:
1108 printf ("/after_dot");
1109 break;
1110
1111 case syntaxspec:
1112 printf ("/syntaxspec");
1113 mcnt = *p++;
1114 printf ("/%d", mcnt);
1115 break;
1116
1117 case notsyntaxspec:
1118 printf ("/notsyntaxspec");
1119 mcnt = *p++;
1120 printf ("/%d", mcnt);
1121 break;
1122# endif /* emacs */
1123
1124 case wordchar:
1125 printf ("/wordchar");
1126 break;
1127
1128 case notwordchar:
1129 printf ("/notwordchar");
1130 break;
1131
1132 case begbuf:
1133 printf ("/begbuf");
1134 break;
1135
1136 case endbuf:
1137 printf ("/endbuf");
1138 break;
1139
1140 default:
1141 printf ("?%ld", (long int) *(p-1));
1142 }
1143
1144 putchar ('\n');
1145 }
1146
1147# ifdef _LIBC
1148 printf ("%td:\tend of pattern.\n", p - start);
1149# else
1150 printf ("%ld:\tend of pattern.\n", (long int) (p - start));
1151# endif
1152}
1153
1154
1155void
1156PREFIX(print_compiled_pattern) (bufp)
1157 struct re_pattern_buffer *bufp;
1158{
1159 UCHAR_T *buffer = (UCHAR_T*) bufp->buffer;
1160
1161 PREFIX(print_partial_compiled_pattern) (buffer, buffer
1162 + bufp->used / sizeof(UCHAR_T));
1163 printf ("%ld bytes used/%ld bytes allocated.\n",
1164 bufp->used, bufp->allocated);
1165
1166 if (bufp->fastmap_accurate && bufp->fastmap)
1167 {
1168 printf ("fastmap: ");
1169 print_fastmap (bufp->fastmap);
1170 }
1171
1172# ifdef _LIBC
1173 printf ("re_nsub: %Zd\t", bufp->re_nsub);
1174# else
1175 printf ("re_nsub: %ld\t", (long int) bufp->re_nsub);
1176# endif
1177 printf ("regs_alloc: %d\t", bufp->regs_allocated);
1178 printf ("can_be_null: %d\t", bufp->can_be_null);
1179 printf ("newline_anchor: %d\n", bufp->newline_anchor);
1180 printf ("no_sub: %d\t", bufp->no_sub);
1181 printf ("not_bol: %d\t", bufp->not_bol);
1182 printf ("not_eol: %d\t", bufp->not_eol);
1183 printf ("syntax: %lx\n", bufp->syntax);
1184 /* Perhaps we should print the translate table? */
1185}
1186
1187
1188void
1189PREFIX(print_double_string) (where, string1, size1, string2, size2)
1190 const CHAR_T *where;
1191 const CHAR_T *string1;
1192 const CHAR_T *string2;
1193 int size1;
1194 int size2;
1195{
1196 int this_char;
1197
1198 if (where == NULL)
1199 printf ("(null)");
1200 else
1201 {
1202 int cnt;
1203
1204 if (FIRST_STRING_P (where))
1205 {
1206 for (this_char = where - string1; this_char < size1; this_char++)
1207 PUT_CHAR (string1[this_char]);
1208
1209 where = string2;
1210 }
1211
1212 cnt = 0;
1213 for (this_char = where - string2; this_char < size2; this_char++)
1214 {
1215 PUT_CHAR (string2[this_char]);
1216 if (++cnt > 100)
1217 {
1218 fputs ("...", stdout);
1219 break;
1220 }
1221 }
1222 }
1223}
1224
1225# ifndef DEFINED_ONCE
1226void
1227printchar (c)
1228 int c;
1229{
1230 putc (c, stderr);
1231}
1232# endif
1233
1234# else /* not DEBUG */
1235
1236# ifndef DEFINED_ONCE
1237# undef assert
1238# define assert(e)
1239
1240# define DEBUG_STATEMENT(e)
1241# define DEBUG_PRINT1(x)
1242# define DEBUG_PRINT2(x1, x2)
1243# define DEBUG_PRINT3(x1, x2, x3)
1244# define DEBUG_PRINT4(x1, x2, x3, x4)
1245# endif /* not DEFINED_ONCE */
1246# define DEBUG_PRINT_COMPILED_PATTERN(p, s, e)
1247# define DEBUG_PRINT_DOUBLE_STRING(w, s1, sz1, s2, sz2)
1248
1249# endif /* not DEBUG */
1250
1251
1252
1253
1254# ifdef WCHAR
1255/* This convert a multibyte string to a wide character string.
1256 And write their correspondances to offset_buffer(see below)
1257 and write whether each wchar_t is binary data to is_binary.
1258 This assume invalid multibyte sequences as binary data.
1259 We assume offset_buffer and is_binary is already allocated
1260 enough space. */
1261
1262static size_t convert_mbs_to_wcs (CHAR_T *dest, const unsigned char* src,
1263 size_t len, int *offset_buffer,
1264 char *is_binary);
1265static size_t
1266convert_mbs_to_wcs (dest, src, len, offset_buffer, is_binary)
1267 CHAR_T *dest;
1268 const unsigned char* src;
1269 size_t len; /* the length of multibyte string. */
1270
1271 /* It hold correspondances between src(char string) and
1272 dest(wchar_t string) for optimization.
1273 e.g. src = "xxxyzz"
1274 dest = {'X', 'Y', 'Z'}
1275 (each "xxx", "y" and "zz" represent one multibyte character
1276 corresponding to 'X', 'Y' and 'Z'.)
1277 offset_buffer = {0, 0+3("xxx"), 0+3+1("y"), 0+3+1+2("zz")}
1278 = {0, 3, 4, 6}
1279 */
1280 int *offset_buffer;
1281 char *is_binary;
1282{
1283 wchar_t *pdest = dest;
1284 const unsigned char *psrc = src;
1285 size_t wc_count = 0;
1286
1287 mbstate_t mbs;
1288 int i, consumed;
1289 size_t mb_remain = len;
1290 size_t mb_count = 0;
1291
1292 /* Initialize the conversion state. */
1293 memset (&mbs, 0, sizeof (mbstate_t));
1294
1295 offset_buffer[0] = 0;
1296 for( ; mb_remain > 0 ; ++wc_count, ++pdest, mb_remain -= consumed,
1297 psrc += consumed)
1298 {
1299 consumed = mbrtowc (pdest, psrc, mb_remain, &mbs);
1300
1301 if (consumed <= 0)
1302 /* failed to convert. maybe src contains binary data.
1303 So we consume 1 byte manualy. */
1304 {
1305 *pdest = *psrc;
1306 consumed = 1;
1307 is_binary[wc_count] = TRUE;
1308 }
1309 else
1310 is_binary[wc_count] = FALSE;
1311 /* In sjis encoding, we use yen sign as escape character in
1312 place of reverse solidus. So we convert 0x5c(yen sign in
1313 sjis) to not 0xa5(yen sign in UCS2) but 0x5c(reverse
1314 solidus in UCS2). */
1315 if (consumed == 1 && (int) *psrc == 0x5c && (int) *pdest == 0xa5)
1316 *pdest = (wchar_t) *psrc;
1317
1318 offset_buffer[wc_count + 1] = mb_count += consumed;
1319 }
1320
1321 /* Fill remain of the buffer with sentinel. */
1322 for (i = wc_count + 1 ; i <= len ; i++)
1323 offset_buffer[i] = mb_count + 1;
1324
1325 return wc_count;
1326}
1327
1328# endif /* WCHAR */
1329
1330#else /* not INSIDE_RECURSION */
1331
1332/* Set by `re_set_syntax' to the current regexp syntax to recognize. Can
1333 also be assigned to arbitrarily: each pattern buffer stores its own
1334 syntax, so it can be changed between regex compilations. */
1335/* This has no initializer because initialized variables in Emacs
1336 become read-only after dumping. */
1337reg_syntax_t re_syntax_options;
1338
1339
1340/* Specify the precise syntax of regexps for compilation. This provides
1341 for compatibility for various utilities which historically have
1342 different, incompatible syntaxes.
1343
1344 The argument SYNTAX is a bit mask comprised of the various bits
1345 defined in regex.h. We return the old syntax. */
1346
1347reg_syntax_t
1348re_set_syntax (syntax)
1349 reg_syntax_t syntax;
1350{
1351 reg_syntax_t ret = re_syntax_options;
1352
1353 re_syntax_options = syntax;
1354# ifdef DEBUG
1355 if (syntax & RE_DEBUG)
1356 debug = 1;
1357 else if (debug) /* was on but now is not */
1358 debug = 0;
1359# endif /* DEBUG */
1360 return ret;
1361}
1362# ifdef _LIBC
1363weak_alias (__re_set_syntax, re_set_syntax)
1364# endif
1365
1366
1367/* This table gives an error message for each of the error codes listed
1368 in regex.h. Obviously the order here has to be same as there.
1369 POSIX doesn't require that we do anything for REG_NOERROR,
1370 but why not be nice? */
1371
1372static const char re_error_msgid[] =
1373 {
1374# define REG_NOERROR_IDX 0
1375 gettext_noop ("Success") /* REG_NOERROR */
1376 "\0"
1377# define REG_NOMATCH_IDX (REG_NOERROR_IDX + sizeof "Success")
1378 gettext_noop ("No match") /* REG_NOMATCH */
1379 "\0"
1380# define REG_BADPAT_IDX (REG_NOMATCH_IDX + sizeof "No match")
1381 gettext_noop ("Invalid regular expression") /* REG_BADPAT */
1382 "\0"
1383# define REG_ECOLLATE_IDX (REG_BADPAT_IDX + sizeof "Invalid regular expression")
1384 gettext_noop ("Invalid collation character") /* REG_ECOLLATE */
1385 "\0"
1386# define REG_ECTYPE_IDX (REG_ECOLLATE_IDX + sizeof "Invalid collation character")
1387 gettext_noop ("Invalid character class name") /* REG_ECTYPE */
1388 "\0"
1389# define REG_EESCAPE_IDX (REG_ECTYPE_IDX + sizeof "Invalid character class name")
1390 gettext_noop ("Trailing backslash") /* REG_EESCAPE */
1391 "\0"
1392# define REG_ESUBREG_IDX (REG_EESCAPE_IDX + sizeof "Trailing backslash")
1393 gettext_noop ("Invalid back reference") /* REG_ESUBREG */
1394 "\0"
1395# define REG_EBRACK_IDX (REG_ESUBREG_IDX + sizeof "Invalid back reference")
1396 gettext_noop ("Unmatched [ or [^") /* REG_EBRACK */
1397 "\0"
1398# define REG_EPAREN_IDX (REG_EBRACK_IDX + sizeof "Unmatched [ or [^")
1399 gettext_noop ("Unmatched ( or \\(") /* REG_EPAREN */
1400 "\0"
1401# define REG_EBRACE_IDX (REG_EPAREN_IDX + sizeof "Unmatched ( or \\(")
1402 gettext_noop ("Unmatched \\{") /* REG_EBRACE */
1403 "\0"
1404# define REG_BADBR_IDX (REG_EBRACE_IDX + sizeof "Unmatched \\{")
1405 gettext_noop ("Invalid content of \\{\\}") /* REG_BADBR */
1406 "\0"
1407# define REG_ERANGE_IDX (REG_BADBR_IDX + sizeof "Invalid content of \\{\\}")
1408 gettext_noop ("Invalid range end") /* REG_ERANGE */
1409 "\0"
1410# define REG_ESPACE_IDX (REG_ERANGE_IDX + sizeof "Invalid range end")
1411 gettext_noop ("Memory exhausted") /* REG_ESPACE */
1412 "\0"
1413# define REG_BADRPT_IDX (REG_ESPACE_IDX + sizeof "Memory exhausted")
1414 gettext_noop ("Invalid preceding regular expression") /* REG_BADRPT */
1415 "\0"
1416# define REG_EEND_IDX (REG_BADRPT_IDX + sizeof "Invalid preceding regular expression")
1417 gettext_noop ("Premature end of regular expression") /* REG_EEND */
1418 "\0"
1419# define REG_ESIZE_IDX (REG_EEND_IDX + sizeof "Premature end of regular expression")
1420 gettext_noop ("Regular expression too big") /* REG_ESIZE */
1421 "\0"
1422# define REG_ERPAREN_IDX (REG_ESIZE_IDX + sizeof "Regular expression too big")
1423 gettext_noop ("Unmatched ) or \\)") /* REG_ERPAREN */
1424 };
1425
1426static const size_t re_error_msgid_idx[] =
1427 {
1428 REG_NOERROR_IDX,
1429 REG_NOMATCH_IDX,
1430 REG_BADPAT_IDX,
1431 REG_ECOLLATE_IDX,
1432 REG_ECTYPE_IDX,
1433 REG_EESCAPE_IDX,
1434 REG_ESUBREG_IDX,
1435 REG_EBRACK_IDX,
1436 REG_EPAREN_IDX,
1437 REG_EBRACE_IDX,
1438 REG_BADBR_IDX,
1439 REG_ERANGE_IDX,
1440 REG_ESPACE_IDX,
1441 REG_BADRPT_IDX,
1442 REG_EEND_IDX,
1443 REG_ESIZE_IDX,
1444 REG_ERPAREN_IDX
1445 };
1446
1447
1448#endif /* INSIDE_RECURSION */
1449
1450#ifndef DEFINED_ONCE
1451/* Avoiding alloca during matching, to placate r_alloc. */
1452
1453/* Define MATCH_MAY_ALLOCATE unless we need to make sure that the
1454 searching and matching functions should not call alloca. On some
1455 systems, alloca is implemented in terms of malloc, and if we're
1456 using the relocating allocator routines, then malloc could cause a
1457 relocation, which might (if the strings being searched are in the
1458 ralloc heap) shift the data out from underneath the regexp
1459 routines.
1460
1461 Here's another reason to avoid allocation: Emacs
1462 processes input from X in a signal handler; processing X input may
1463 call malloc; if input arrives while a matching routine is calling
1464 malloc, then we're scrod. But Emacs can't just block input while
1465 calling matching routines; then we don't notice interrupts when
1466 they come in. So, Emacs blocks input around all regexp calls
1467 except the matching calls, which it leaves unprotected, in the
1468 faith that they will not malloc. */
1469
1470/* Normally, this is fine. */
1471# define MATCH_MAY_ALLOCATE
1472
1473/* When using GNU C, we are not REALLY using the C alloca, no matter
1474 what config.h may say. So don't take precautions for it. */
1475# ifdef __GNUC__
1476# undef C_ALLOCA
1477# endif
1478
1479/* The match routines may not allocate if (1) they would do it with malloc
1480 and (2) it's not safe for them to use malloc.
1481 Note that if REL_ALLOC is defined, matching would not use malloc for the
1482 failure stack, but we would still use it for the register vectors;
1483 so REL_ALLOC should not affect this. */
1484# if (defined C_ALLOCA || defined REGEX_MALLOC) && defined emacs
1485# undef MATCH_MAY_ALLOCATE
1486# endif
1487#endif /* not DEFINED_ONCE */
1488
1489
1490#ifdef INSIDE_RECURSION
1491/* Failure stack declarations and macros; both re_compile_fastmap and
1492 re_match_2 use a failure stack. These have to be macros because of
1493 REGEX_ALLOCATE_STACK. */
1494
1495
1496/* Number of failure points for which to initially allocate space
1497 when matching. If this number is exceeded, we allocate more
1498 space, so it is not a hard limit. */
1499# ifndef INIT_FAILURE_ALLOC
1500# define INIT_FAILURE_ALLOC 5
1501# endif
1502
1503/* Roughly the maximum number of failure points on the stack. Would be
1504 exactly that if always used MAX_FAILURE_ITEMS items each time we failed.
1505 This is a variable only so users of regex can assign to it; we never
1506 change it ourselves. */
1507
1508# ifdef INT_IS_16BIT
1509
1510# ifndef DEFINED_ONCE
1511# if defined MATCH_MAY_ALLOCATE
1512/* 4400 was enough to cause a crash on Alpha OSF/1,
1513 whose default stack limit is 2mb. */
1514long int re_max_failures = 4000;
1515# else
1516long int re_max_failures = 2000;
1517# endif
1518# endif
1519
1520union PREFIX(fail_stack_elt)
1521{
1522 UCHAR_T *pointer;
1523 long int integer;
1524};
1525
1526typedef union PREFIX(fail_stack_elt) PREFIX(fail_stack_elt_t);
1527
1528typedef struct
1529{
1530 PREFIX(fail_stack_elt_t) *stack;
1531 unsigned long int size;
1532 unsigned long int avail; /* Offset of next open position. */
1533} PREFIX(fail_stack_type);
1534
1535# else /* not INT_IS_16BIT */
1536
1537# ifndef DEFINED_ONCE
1538# if defined MATCH_MAY_ALLOCATE
1539/* 4400 was enough to cause a crash on Alpha OSF/1,
1540 whose default stack limit is 2mb. */
1541int re_max_failures = 4000;
1542# else
1543int re_max_failures = 2000;
1544# endif
1545# endif
1546
1547union PREFIX(fail_stack_elt)
1548{
1549 UCHAR_T *pointer;
1550 int integer;
1551};
1552
1553typedef union PREFIX(fail_stack_elt) PREFIX(fail_stack_elt_t);
1554
1555typedef struct
1556{
1557 PREFIX(fail_stack_elt_t) *stack;
1558 unsigned size;
1559 unsigned avail; /* Offset of next open position. */
1560} PREFIX(fail_stack_type);
1561
1562# endif /* INT_IS_16BIT */
1563
1564# ifndef DEFINED_ONCE
1565# define FAIL_STACK_EMPTY() (fail_stack.avail == 0)
1566# define FAIL_STACK_PTR_EMPTY() (fail_stack_ptr->avail == 0)
1567# define FAIL_STACK_FULL() (fail_stack.avail == fail_stack.size)
1568# endif
1569
1570
1571/* Define macros to initialize and free the failure stack.
1572 Do `return -2' if the alloc fails. */
1573
1574# ifdef MATCH_MAY_ALLOCATE
1575# define INIT_FAIL_STACK() \
1576 do { \
1577 fail_stack.stack = (PREFIX(fail_stack_elt_t) *) \
1578 REGEX_ALLOCATE_STACK (INIT_FAILURE_ALLOC * sizeof (PREFIX(fail_stack_elt_t))); \
1579 \
1580 if (fail_stack.stack == NULL) \
1581 return -2; \
1582 \
1583 fail_stack.size = INIT_FAILURE_ALLOC; \
1584 fail_stack.avail = 0; \
1585 } while (0)
1586
1587# define RESET_FAIL_STACK() REGEX_FREE_STACK (fail_stack.stack)
1588# else
1589# define INIT_FAIL_STACK() \
1590 do { \
1591 fail_stack.avail = 0; \
1592 } while (0)
1593
1594# define RESET_FAIL_STACK()
1595# endif
1596
1597
1598/* Double the size of FAIL_STACK, up to approximately `re_max_failures' items.
1599
1600 Return 1 if succeeds, and 0 if either ran out of memory
1601 allocating space for it or it was already too large.
1602
1603 REGEX_REALLOCATE_STACK requires `destination' be declared. */
1604
1605# define DOUBLE_FAIL_STACK(fail_stack) \
1606 ((fail_stack).size > (unsigned) (re_max_failures * MAX_FAILURE_ITEMS) \
1607 ? 0 \
1608 : ((fail_stack).stack = (PREFIX(fail_stack_elt_t) *) \
1609 REGEX_REALLOCATE_STACK ((fail_stack).stack, \
1610 (fail_stack).size * sizeof (PREFIX(fail_stack_elt_t)), \
1611 ((fail_stack).size << 1) * sizeof (PREFIX(fail_stack_elt_t))),\
1612 \
1613 (fail_stack).stack == NULL \
1614 ? 0 \
1615 : ((fail_stack).size <<= 1, \
1616 1)))
1617
1618
1619/* Push pointer POINTER on FAIL_STACK.
1620 Return 1 if was able to do so and 0 if ran out of memory allocating
1621 space to do so. */
1622# define PUSH_PATTERN_OP(POINTER, FAIL_STACK) \
1623 ((FAIL_STACK_FULL () \
1624 && !DOUBLE_FAIL_STACK (FAIL_STACK)) \
1625 ? 0 \
1626 : ((FAIL_STACK).stack[(FAIL_STACK).avail++].pointer = POINTER, \
1627 1))
1628
1629/* Push a pointer value onto the failure stack.
1630 Assumes the variable `fail_stack'. Probably should only
1631 be called from within `PUSH_FAILURE_POINT'. */
1632# define PUSH_FAILURE_POINTER(item) \
1633 fail_stack.stack[fail_stack.avail++].pointer = (UCHAR_T *) (item)
1634
1635/* This pushes an integer-valued item onto the failure stack.
1636 Assumes the variable `fail_stack'. Probably should only
1637 be called from within `PUSH_FAILURE_POINT'. */
1638# define PUSH_FAILURE_INT(item) \
1639 fail_stack.stack[fail_stack.avail++].integer = (item)
1640
1641/* Push a fail_stack_elt_t value onto the failure stack.
1642 Assumes the variable `fail_stack'. Probably should only
1643 be called from within `PUSH_FAILURE_POINT'. */
1644# define PUSH_FAILURE_ELT(item) \
1645 fail_stack.stack[fail_stack.avail++] = (item)
1646
1647/* These three POP... operations complement the three PUSH... operations.
1648 All assume that `fail_stack' is nonempty. */
1649# define POP_FAILURE_POINTER() fail_stack.stack[--fail_stack.avail].pointer
1650# define POP_FAILURE_INT() fail_stack.stack[--fail_stack.avail].integer
1651# define POP_FAILURE_ELT() fail_stack.stack[--fail_stack.avail]
1652
1653/* Used to omit pushing failure point id's when we're not debugging. */
1654# ifdef DEBUG
1655# define DEBUG_PUSH PUSH_FAILURE_INT
1656# define DEBUG_POP(item_addr) *(item_addr) = POP_FAILURE_INT ()
1657# else
1658# define DEBUG_PUSH(item)
1659# define DEBUG_POP(item_addr)
1660# endif
1661
1662
1663/* Push the information about the state we will need
1664 if we ever fail back to it.
1665
1666 Requires variables fail_stack, regstart, regend, reg_info, and
1667 num_regs_pushed be declared. DOUBLE_FAIL_STACK requires `destination'
1668 be declared.
1669
1670 Does `return FAILURE_CODE' if runs out of memory. */
1671
1672# define PUSH_FAILURE_POINT(pattern_place, string_place, failure_code) \
1673 do { \
1674 char *destination; \
1675 /* Must be int, so when we don't save any registers, the arithmetic \
1676 of 0 + -1 isn't done as unsigned. */ \
1677 /* Can't be int, since there is not a shred of a guarantee that int \
1678 is wide enough to hold a value of something to which pointer can \
1679 be assigned */ \
1680 active_reg_t this_reg; \
1681 \
1682 DEBUG_STATEMENT (failure_id++); \
1683 DEBUG_STATEMENT (nfailure_points_pushed++); \
1684 DEBUG_PRINT2 ("\nPUSH_FAILURE_POINT #%u:\n", failure_id); \
1685 DEBUG_PRINT2 (" Before push, next avail: %d\n", (fail_stack).avail);\
1686 DEBUG_PRINT2 (" size: %d\n", (fail_stack).size);\
1687 \
1688 DEBUG_PRINT2 (" slots needed: %ld\n", NUM_FAILURE_ITEMS); \
1689 DEBUG_PRINT2 (" available: %d\n", REMAINING_AVAIL_SLOTS); \
1690 \
1691 /* Ensure we have enough space allocated for what we will push. */ \
1692 while (REMAINING_AVAIL_SLOTS < NUM_FAILURE_ITEMS) \
1693 { \
1694 if (!DOUBLE_FAIL_STACK (fail_stack)) \
1695 return failure_code; \
1696 \
1697 DEBUG_PRINT2 ("\n Doubled stack; size now: %d\n", \
1698 (fail_stack).size); \
1699 DEBUG_PRINT2 (" slots available: %d\n", REMAINING_AVAIL_SLOTS);\
1700 } \
1701 \
1702 /* Push the info, starting with the registers. */ \
1703 DEBUG_PRINT1 ("\n"); \
1704 \
1705 if (1) \
1706 for (this_reg = lowest_active_reg; this_reg <= highest_active_reg; \
1707 this_reg++) \
1708 { \
1709 DEBUG_PRINT2 (" Pushing reg: %lu\n", this_reg); \
1710 DEBUG_STATEMENT (num_regs_pushed++); \
1711 \
1712 DEBUG_PRINT2 (" start: %p\n", regstart[this_reg]); \
1713 PUSH_FAILURE_POINTER (regstart[this_reg]); \
1714 \
1715 DEBUG_PRINT2 (" end: %p\n", regend[this_reg]); \
1716 PUSH_FAILURE_POINTER (regend[this_reg]); \
1717 \
1718 DEBUG_PRINT2 (" info: %p\n ", \
1719 reg_info[this_reg].word.pointer); \
1720 DEBUG_PRINT2 (" match_null=%d", \
1721 REG_MATCH_NULL_STRING_P (reg_info[this_reg])); \
1722 DEBUG_PRINT2 (" active=%d", IS_ACTIVE (reg_info[this_reg])); \
1723 DEBUG_PRINT2 (" matched_something=%d", \
1724 MATCHED_SOMETHING (reg_info[this_reg])); \
1725 DEBUG_PRINT2 (" ever_matched=%d", \
1726 EVER_MATCHED_SOMETHING (reg_info[this_reg])); \
1727 DEBUG_PRINT1 ("\n"); \
1728 PUSH_FAILURE_ELT (reg_info[this_reg].word); \
1729 } \
1730 \
1731 DEBUG_PRINT2 (" Pushing low active reg: %ld\n", lowest_active_reg);\
1732 PUSH_FAILURE_INT (lowest_active_reg); \
1733 \
1734 DEBUG_PRINT2 (" Pushing high active reg: %ld\n", highest_active_reg);\
1735 PUSH_FAILURE_INT (highest_active_reg); \
1736 \
1737 DEBUG_PRINT2 (" Pushing pattern %p:\n", pattern_place); \
1738 DEBUG_PRINT_COMPILED_PATTERN (bufp, pattern_place, pend); \
1739 PUSH_FAILURE_POINTER (pattern_place); \
1740 \
1741 DEBUG_PRINT2 (" Pushing string %p: `", string_place); \
1742 DEBUG_PRINT_DOUBLE_STRING (string_place, string1, size1, string2, \
1743 size2); \
1744 DEBUG_PRINT1 ("'\n"); \
1745 PUSH_FAILURE_POINTER (string_place); \
1746 \
1747 DEBUG_PRINT2 (" Pushing failure id: %u\n", failure_id); \
1748 DEBUG_PUSH (failure_id); \
1749 } while (0)
1750
1751# ifndef DEFINED_ONCE
1752/* This is the number of items that are pushed and popped on the stack
1753 for each register. */
1754# define NUM_REG_ITEMS 3
1755
1756/* Individual items aside from the registers. */
1757# ifdef DEBUG
1758# define NUM_NONREG_ITEMS 5 /* Includes failure point id. */
1759# else
1760# define NUM_NONREG_ITEMS 4
1761# endif
1762
1763/* We push at most this many items on the stack. */
1764/* We used to use (num_regs - 1), which is the number of registers
1765 this regexp will save; but that was changed to 5
1766 to avoid stack overflow for a regexp with lots of parens. */
1767# define MAX_FAILURE_ITEMS (5 * NUM_REG_ITEMS + NUM_NONREG_ITEMS)
1768
1769/* We actually push this many items. */
1770# define NUM_FAILURE_ITEMS \
1771 (((0 \
1772 ? 0 : highest_active_reg - lowest_active_reg + 1) \
1773 * NUM_REG_ITEMS) \
1774 + NUM_NONREG_ITEMS)
1775
1776/* How many items can still be added to the stack without overflowing it. */
1777# define REMAINING_AVAIL_SLOTS ((fail_stack).size - (fail_stack).avail)
1778# endif /* not DEFINED_ONCE */
1779
1780
1781/* Pops what PUSH_FAIL_STACK pushes.
1782
1783 We restore into the parameters, all of which should be lvalues:
1784 STR -- the saved data position.
1785 PAT -- the saved pattern position.
1786 LOW_REG, HIGH_REG -- the highest and lowest active registers.
1787 REGSTART, REGEND -- arrays of string positions.
1788 REG_INFO -- array of information about each subexpression.
1789
1790 Also assumes the variables `fail_stack' and (if debugging), `bufp',
1791 `pend', `string1', `size1', `string2', and `size2'. */
1792# define POP_FAILURE_POINT(str, pat, low_reg, high_reg, regstart, regend, reg_info)\
1793{ \
1794 DEBUG_STATEMENT (unsigned failure_id;) \
1795 active_reg_t this_reg; \
1796 const UCHAR_T *string_temp; \
1797 \
1798 assert (!FAIL_STACK_EMPTY ()); \
1799 \
1800 /* Remove failure points and point to how many regs pushed. */ \
1801 DEBUG_PRINT1 ("POP_FAILURE_POINT:\n"); \
1802 DEBUG_PRINT2 (" Before pop, next avail: %d\n", fail_stack.avail); \
1803 DEBUG_PRINT2 (" size: %d\n", fail_stack.size); \
1804 \
1805 assert (fail_stack.avail >= NUM_NONREG_ITEMS); \
1806 \
1807 DEBUG_POP (&failure_id); \
1808 DEBUG_PRINT2 (" Popping failure id: %u\n", failure_id); \
1809 \
1810 /* If the saved string location is NULL, it came from an \
1811 on_failure_keep_string_jump opcode, and we want to throw away the \
1812 saved NULL, thus retaining our current position in the string. */ \
1813 string_temp = POP_FAILURE_POINTER (); \
1814 if (string_temp != NULL) \
1815 str = (const CHAR_T *) string_temp; \
1816 \
1817 DEBUG_PRINT2 (" Popping string %p: `", str); \
1818 DEBUG_PRINT_DOUBLE_STRING (str, string1, size1, string2, size2); \
1819 DEBUG_PRINT1 ("'\n"); \
1820 \
1821 pat = (UCHAR_T *) POP_FAILURE_POINTER (); \
1822 DEBUG_PRINT2 (" Popping pattern %p:\n", pat); \
1823 DEBUG_PRINT_COMPILED_PATTERN (bufp, pat, pend); \
1824 \
1825 /* Restore register info. */ \
1826 high_reg = (active_reg_t) POP_FAILURE_INT (); \
1827 DEBUG_PRINT2 (" Popping high active reg: %ld\n", high_reg); \
1828 \
1829 low_reg = (active_reg_t) POP_FAILURE_INT (); \
1830 DEBUG_PRINT2 (" Popping low active reg: %ld\n", low_reg); \
1831 \
1832 if (1) \
1833 for (this_reg = high_reg; this_reg >= low_reg; this_reg--) \
1834 { \
1835 DEBUG_PRINT2 (" Popping reg: %ld\n", this_reg); \
1836 \
1837 reg_info[this_reg].word = POP_FAILURE_ELT (); \
1838 DEBUG_PRINT2 (" info: %p\n", \
1839 reg_info[this_reg].word.pointer); \
1840 \
1841 regend[this_reg] = (const CHAR_T *) POP_FAILURE_POINTER (); \
1842 DEBUG_PRINT2 (" end: %p\n", regend[this_reg]); \
1843 \
1844 regstart[this_reg] = (const CHAR_T *) POP_FAILURE_POINTER (); \
1845 DEBUG_PRINT2 (" start: %p\n", regstart[this_reg]); \
1846 } \
1847 else \
1848 { \
1849 for (this_reg = highest_active_reg; this_reg > high_reg; this_reg--) \
1850 { \
1851 reg_info[this_reg].word.integer = 0; \
1852 regend[this_reg] = 0; \
1853 regstart[this_reg] = 0; \
1854 } \
1855 highest_active_reg = high_reg; \
1856 } \
1857 \
1858 set_regs_matched_done = 0; \
1859 DEBUG_STATEMENT (nfailure_points_popped++); \
1860} /* POP_FAILURE_POINT */
1861
1862
1863/* Structure for per-register (a.k.a. per-group) information.
1864 Other register information, such as the
1865 starting and ending positions (which are addresses), and the list of
1866 inner groups (which is a bits list) are maintained in separate
1867 variables.
1868
1869 We are making a (strictly speaking) nonportable assumption here: that
1870 the compiler will pack our bit fields into something that fits into
1871 the type of `word', i.e., is something that fits into one item on the
1872 failure stack. */
1873
1874
1875/* Declarations and macros for re_match_2. */
1876
1877typedef union
1878{
1879 PREFIX(fail_stack_elt_t) word;
1880 struct
1881 {
1882 /* This field is one if this group can match the empty string,
1883 zero if not. If not yet determined, `MATCH_NULL_UNSET_VALUE'. */
1884# define MATCH_NULL_UNSET_VALUE 3
1885 unsigned match_null_string_p : 2;
1886 unsigned is_active : 1;
1887 unsigned matched_something : 1;
1888 unsigned ever_matched_something : 1;
1889 } bits;
1890} PREFIX(register_info_type);
1891
1892# ifndef DEFINED_ONCE
1893# define REG_MATCH_NULL_STRING_P(R) ((R).bits.match_null_string_p)
1894# define IS_ACTIVE(R) ((R).bits.is_active)
1895# define MATCHED_SOMETHING(R) ((R).bits.matched_something)
1896# define EVER_MATCHED_SOMETHING(R) ((R).bits.ever_matched_something)
1897
1898
1899/* Call this when have matched a real character; it sets `matched' flags
1900 for the subexpressions which we are currently inside. Also records
1901 that those subexprs have matched. */
1902# define SET_REGS_MATCHED() \
1903 do \
1904 { \
1905 if (!set_regs_matched_done) \
1906 { \
1907 active_reg_t r; \
1908 set_regs_matched_done = 1; \
1909 for (r = lowest_active_reg; r <= highest_active_reg; r++) \
1910 { \
1911 MATCHED_SOMETHING (reg_info[r]) \
1912 = EVER_MATCHED_SOMETHING (reg_info[r]) \
1913 = 1; \
1914 } \
1915 } \
1916 } \
1917 while (0)
1918# endif /* not DEFINED_ONCE */
1919
1920/* Registers are set to a sentinel when they haven't yet matched. */
1921static CHAR_T PREFIX(reg_unset_dummy);
1922# define REG_UNSET_VALUE (&PREFIX(reg_unset_dummy))
1923# define REG_UNSET(e) ((e) == REG_UNSET_VALUE)
1924
1925/* Subroutine declarations and macros for regex_compile. */
1926static void PREFIX(store_op1) _RE_ARGS ((re_opcode_t op, UCHAR_T *loc, int arg));
1927static void PREFIX(store_op2) _RE_ARGS ((re_opcode_t op, UCHAR_T *loc,
1928 int arg1, int arg2));
1929static void PREFIX(insert_op1) _RE_ARGS ((re_opcode_t op, UCHAR_T *loc,
1930 int arg, UCHAR_T *end));
1931static void PREFIX(insert_op2) _RE_ARGS ((re_opcode_t op, UCHAR_T *loc,
1932 int arg1, int arg2, UCHAR_T *end));
1933static boolean PREFIX(at_begline_loc_p) _RE_ARGS ((const CHAR_T *pattern,
1934 const CHAR_T *p,
1935 reg_syntax_t syntax));
1936static boolean PREFIX(at_endline_loc_p) _RE_ARGS ((const CHAR_T *p,
1937 const CHAR_T *pend,
1938 reg_syntax_t syntax));
1939# ifdef WCHAR
1940static reg_errcode_t wcs_compile_range _RE_ARGS ((CHAR_T range_start,
1941 const CHAR_T **p_ptr,
1942 const CHAR_T *pend,
1943 char *translate,
1944 reg_syntax_t syntax,
1945 UCHAR_T *b,
1946 CHAR_T *char_set));
1947static void insert_space _RE_ARGS ((int num, CHAR_T *loc, CHAR_T *end));
1948# else /* BYTE */
1949static reg_errcode_t byte_compile_range _RE_ARGS ((unsigned int range_start,
1950 const char **p_ptr,
1951 const char *pend,
1952 char *translate,
1953 reg_syntax_t syntax,
1954 unsigned char *b));
1955# endif /* WCHAR */
1956
1957/* Fetch the next character in the uncompiled pattern---translating it
1958 if necessary. Also cast from a signed character in the constant
1959 string passed to us by the user to an unsigned char that we can use
1960 as an array index (in, e.g., `translate'). */
1961/* ifdef MBS_SUPPORT, we translate only if character <= 0xff,
1962 because it is impossible to allocate 4GB array for some encodings
1963 which have 4 byte character_set like UCS4. */
1964# ifndef PATFETCH
1965# ifdef WCHAR
1966# define PATFETCH(c) \
1967 do {if (p == pend) return REG_EEND; \
1968 c = (UCHAR_T) *p++; \
1969 if (translate && (c <= 0xff)) c = (UCHAR_T) translate[c]; \
1970 } while (0)
1971# else /* BYTE */
1972# define PATFETCH(c) \
1973 do {if (p == pend) return REG_EEND; \
1974 c = (unsigned char) *p++; \
1975 if (translate) c = (unsigned char) translate[c]; \
1976 } while (0)
1977# endif /* WCHAR */
1978# endif
1979
1980/* Fetch the next character in the uncompiled pattern, with no
1981 translation. */
1982# define PATFETCH_RAW(c) \
1983 do {if (p == pend) return REG_EEND; \
1984 c = (UCHAR_T) *p++; \
1985 } while (0)
1986
1987/* Go backwards one character in the pattern. */
1988# define PATUNFETCH p--
1989
1990
1991/* If `translate' is non-null, return translate[D], else just D. We
1992 cast the subscript to translate because some data is declared as
1993 `char *', to avoid warnings when a string constant is passed. But
1994 when we use a character as a subscript we must make it unsigned. */
1995/* ifdef MBS_SUPPORT, we translate only if character <= 0xff,
1996 because it is impossible to allocate 4GB array for some encodings
1997 which have 4 byte character_set like UCS4. */
1998
1999# ifndef TRANSLATE
2000# ifdef WCHAR
2001# define TRANSLATE(d) \
2002 ((translate && ((UCHAR_T) (d)) <= 0xff) \
2003 ? (char) translate[(unsigned char) (d)] : (d))
2004# else /* BYTE */
2005# define TRANSLATE(d) \
2006 (translate ? (char) translate[(unsigned char) (d)] : (d))
2007# endif /* WCHAR */
2008# endif
2009
2010
2011/* Macros for outputting the compiled pattern into `buffer'. */
2012
2013/* If the buffer isn't allocated when it comes in, use this. */
2014# define INIT_BUF_SIZE (32 * sizeof(UCHAR_T))
2015
2016/* Make sure we have at least N more bytes of space in buffer. */
2017# ifdef WCHAR
2018# define GET_BUFFER_SPACE(n) \
2019 while (((unsigned long)b - (unsigned long)COMPILED_BUFFER_VAR \
2020 + (n)*sizeof(CHAR_T)) > bufp->allocated) \
2021 EXTEND_BUFFER ()
2022# else /* BYTE */
2023# define GET_BUFFER_SPACE(n) \
2024 while ((unsigned long) (b - bufp->buffer + (n)) > bufp->allocated) \
2025 EXTEND_BUFFER ()
2026# endif /* WCHAR */
2027
2028/* Make sure we have one more byte of buffer space and then add C to it. */
2029# define BUF_PUSH(c) \
2030 do { \
2031 GET_BUFFER_SPACE (1); \
2032 *b++ = (UCHAR_T) (c); \
2033 } while (0)
2034
2035
2036/* Ensure we have two more bytes of buffer space and then append C1 and C2. */
2037# define BUF_PUSH_2(c1, c2) \
2038 do { \
2039 GET_BUFFER_SPACE (2); \
2040 *b++ = (UCHAR_T) (c1); \
2041 *b++ = (UCHAR_T) (c2); \
2042 } while (0)
2043
2044
2045/* As with BUF_PUSH_2, except for three bytes. */
2046# define BUF_PUSH_3(c1, c2, c3) \
2047 do { \
2048 GET_BUFFER_SPACE (3); \
2049 *b++ = (UCHAR_T) (c1); \
2050 *b++ = (UCHAR_T) (c2); \
2051 *b++ = (UCHAR_T) (c3); \
2052 } while (0)
2053
2054/* Store a jump with opcode OP at LOC to location TO. We store a
2055 relative address offset by the three bytes the jump itself occupies. */
2056# define STORE_JUMP(op, loc, to) \
2057 PREFIX(store_op1) (op, loc, (int) ((to) - (loc) - (1 + OFFSET_ADDRESS_SIZE)))
2058
2059/* Likewise, for a two-argument jump. */
2060# define STORE_JUMP2(op, loc, to, arg) \
2061 PREFIX(store_op2) (op, loc, (int) ((to) - (loc) - (1 + OFFSET_ADDRESS_SIZE)), arg)
2062
2063/* Like `STORE_JUMP', but for inserting. Assume `b' is the buffer end. */
2064# define INSERT_JUMP(op, loc, to) \
2065 PREFIX(insert_op1) (op, loc, (int) ((to) - (loc) - (1 + OFFSET_ADDRESS_SIZE)), b)
2066
2067/* Like `STORE_JUMP2', but for inserting. Assume `b' is the buffer end. */
2068# define INSERT_JUMP2(op, loc, to, arg) \
2069 PREFIX(insert_op2) (op, loc, (int) ((to) - (loc) - (1 + OFFSET_ADDRESS_SIZE)),\
2070 arg, b)
2071
2072/* This is not an arbitrary limit: the arguments which represent offsets
2073 into the pattern are two bytes long. So if 2^16 bytes turns out to
2074 be too small, many things would have to change. */
2075/* Any other compiler which, like MSC, has allocation limit below 2^16
2076 bytes will have to use approach similar to what was done below for
2077 MSC and drop MAX_BUF_SIZE a bit. Otherwise you may end up
2078 reallocating to 0 bytes. Such thing is not going to work too well.
2079 You have been warned!! */
2080# ifndef DEFINED_ONCE
2081# if defined _MSC_VER && !defined WIN32
2082/* Microsoft C 16-bit versions limit malloc to approx 65512 bytes.
2083 The REALLOC define eliminates a flurry of conversion warnings,
2084 but is not required. */
2085# define MAX_BUF_SIZE 65500L
2086# define REALLOC(p,s) realloc ((p), (size_t) (s))
2087# else
2088# define MAX_BUF_SIZE (1L << 16)
2089# define REALLOC(p,s) realloc ((p), (s))
2090# endif
2091
2092/* Extend the buffer by twice its current size via realloc and
2093 reset the pointers that pointed into the old block to point to the
2094 correct places in the new one. If extending the buffer results in it
2095 being larger than MAX_BUF_SIZE, then flag memory exhausted. */
2096# if __BOUNDED_POINTERS__
2097# define SET_HIGH_BOUND(P) (__ptrhigh (P) = __ptrlow (P) + bufp->allocated)
2098# define MOVE_BUFFER_POINTER(P) \
2099 (__ptrlow (P) += incr, SET_HIGH_BOUND (P), __ptrvalue (P) += incr)
2100# define ELSE_EXTEND_BUFFER_HIGH_BOUND \
2101 else \
2102 { \
2103 SET_HIGH_BOUND (b); \
2104 SET_HIGH_BOUND (begalt); \
2105 if (fixup_alt_jump) \
2106 SET_HIGH_BOUND (fixup_alt_jump); \
2107 if (laststart) \
2108 SET_HIGH_BOUND (laststart); \
2109 if (pending_exact) \
2110 SET_HIGH_BOUND (pending_exact); \
2111 }
2112# else
2113# define MOVE_BUFFER_POINTER(P) (P) += incr
2114# define ELSE_EXTEND_BUFFER_HIGH_BOUND
2115# endif
2116# endif /* not DEFINED_ONCE */
2117
2118# ifdef WCHAR
2119# define EXTEND_BUFFER() \
2120 do { \
2121 UCHAR_T *old_buffer = COMPILED_BUFFER_VAR; \
2122 int wchar_count; \
2123 if (bufp->allocated + sizeof(UCHAR_T) > MAX_BUF_SIZE) \
2124 return REG_ESIZE; \
2125 bufp->allocated <<= 1; \
2126 if (bufp->allocated > MAX_BUF_SIZE) \
2127 bufp->allocated = MAX_BUF_SIZE; \
2128 /* How many characters the new buffer can have? */ \
2129 wchar_count = bufp->allocated / sizeof(UCHAR_T); \
2130 if (wchar_count == 0) wchar_count = 1; \
2131 /* Truncate the buffer to CHAR_T align. */ \
2132 bufp->allocated = wchar_count * sizeof(UCHAR_T); \
2133 RETALLOC (COMPILED_BUFFER_VAR, wchar_count, UCHAR_T); \
2134 bufp->buffer = (char*)COMPILED_BUFFER_VAR; \
2135 if (COMPILED_BUFFER_VAR == NULL) \
2136 return REG_ESPACE; \
2137 /* If the buffer moved, move all the pointers into it. */ \
2138 if (old_buffer != COMPILED_BUFFER_VAR) \
2139 { \
2140 int incr = COMPILED_BUFFER_VAR - old_buffer; \
2141 MOVE_BUFFER_POINTER (b); \
2142 MOVE_BUFFER_POINTER (begalt); \
2143 if (fixup_alt_jump) \
2144 MOVE_BUFFER_POINTER (fixup_alt_jump); \
2145 if (laststart) \
2146 MOVE_BUFFER_POINTER (laststart); \
2147 if (pending_exact) \
2148 MOVE_BUFFER_POINTER (pending_exact); \
2149 } \
2150 ELSE_EXTEND_BUFFER_HIGH_BOUND \
2151 } while (0)
2152# else /* BYTE */
2153# define EXTEND_BUFFER() \
2154 do { \
2155 UCHAR_T *old_buffer = COMPILED_BUFFER_VAR; \
2156 if (bufp->allocated == MAX_BUF_SIZE) \
2157 return REG_ESIZE; \
2158 bufp->allocated <<= 1; \
2159 if (bufp->allocated > MAX_BUF_SIZE) \
2160 bufp->allocated = MAX_BUF_SIZE; \
2161 bufp->buffer = (UCHAR_T *) REALLOC (COMPILED_BUFFER_VAR, \
2162 bufp->allocated); \
2163 if (COMPILED_BUFFER_VAR == NULL) \
2164 return REG_ESPACE; \
2165 /* If the buffer moved, move all the pointers into it. */ \
2166 if (old_buffer != COMPILED_BUFFER_VAR) \
2167 { \
2168 int incr = COMPILED_BUFFER_VAR - old_buffer; \
2169 MOVE_BUFFER_POINTER (b); \
2170 MOVE_BUFFER_POINTER (begalt); \
2171 if (fixup_alt_jump) \
2172 MOVE_BUFFER_POINTER (fixup_alt_jump); \
2173 if (laststart) \
2174 MOVE_BUFFER_POINTER (laststart); \
2175 if (pending_exact) \
2176 MOVE_BUFFER_POINTER (pending_exact); \
2177 } \
2178 ELSE_EXTEND_BUFFER_HIGH_BOUND \
2179 } while (0)
2180# endif /* WCHAR */
2181
2182# ifndef DEFINED_ONCE
2183/* Since we have one byte reserved for the register number argument to
2184 {start,stop}_memory, the maximum number of groups we can report
2185 things about is what fits in that byte. */
2186# define MAX_REGNUM 255
2187
2188/* But patterns can have more than `MAX_REGNUM' registers. We just
2189 ignore the excess. */
2190typedef unsigned regnum_t;
2191
2192
2193/* Macros for the compile stack. */
2194
2195/* Since offsets can go either forwards or backwards, this type needs to
2196 be able to hold values from -(MAX_BUF_SIZE - 1) to MAX_BUF_SIZE - 1. */
2197/* int may be not enough when sizeof(int) == 2. */
2198typedef long pattern_offset_t;
2199
2200typedef struct
2201{
2202 pattern_offset_t begalt_offset;
2203 pattern_offset_t fixup_alt_jump;
2204 pattern_offset_t inner_group_offset;
2205 pattern_offset_t laststart_offset;
2206 regnum_t regnum;
2207} compile_stack_elt_t;
2208
2209
2210typedef struct
2211{
2212 compile_stack_elt_t *stack;
2213 unsigned size;
2214 unsigned avail; /* Offset of next open position. */
2215} compile_stack_type;
2216
2217
2218# define INIT_COMPILE_STACK_SIZE 32
2219
2220# define COMPILE_STACK_EMPTY (compile_stack.avail == 0)
2221# define COMPILE_STACK_FULL (compile_stack.avail == compile_stack.size)
2222
2223/* The next available element. */
2224# define COMPILE_STACK_TOP (compile_stack.stack[compile_stack.avail])
2225
2226# endif /* not DEFINED_ONCE */
2227
2228/* Set the bit for character C in a list. */
2229# ifndef DEFINED_ONCE
2230# define SET_LIST_BIT(c) \
2231 (b[((unsigned char) (c)) / BYTEWIDTH] \
2232 |= 1 << (((unsigned char) c) % BYTEWIDTH))
2233# endif /* DEFINED_ONCE */
2234
2235/* Get the next unsigned number in the uncompiled pattern. */
2236# define GET_UNSIGNED_NUMBER(num) \
2237 { \
2238 while (p != pend) \
2239 { \
2240 PATFETCH (c); \
2241 if (c < '0' || c > '9') \
2242 break; \
2243 if (num <= RE_DUP_MAX) \
2244 { \
2245 if (num < 0) \
2246 num = 0; \
2247 num = num * 10 + c - '0'; \
2248 } \
2249 } \
2250 }
2251
2252# ifndef DEFINED_ONCE
2253# if defined _LIBC || WIDE_CHAR_SUPPORT
2254/* The GNU C library provides support for user-defined character classes
2255 and the functions from ISO C amendement 1. */
2256# ifdef CHARCLASS_NAME_MAX
2257# define CHAR_CLASS_MAX_LENGTH CHARCLASS_NAME_MAX
2258# else
2259/* This shouldn't happen but some implementation might still have this
2260 problem. Use a reasonable default value. */
2261# define CHAR_CLASS_MAX_LENGTH 256
2262# endif
2263
2264# ifdef _LIBC
2265# define IS_CHAR_CLASS(string) __wctype (string)
2266# else
2267# define IS_CHAR_CLASS(string) wctype (string)
2268# endif
2269# else
2270# define CHAR_CLASS_MAX_LENGTH 6 /* Namely, `xdigit'. */
2271
2272# define IS_CHAR_CLASS(string) \
2273 (STREQ (string, "alpha") || STREQ (string, "upper") \
2274 || STREQ (string, "lower") || STREQ (string, "digit") \
2275 || STREQ (string, "alnum") || STREQ (string, "xdigit") \
2276 || STREQ (string, "space") || STREQ (string, "print") \
2277 || STREQ (string, "punct") || STREQ (string, "graph") \
2278 || STREQ (string, "cntrl") || STREQ (string, "blank"))
2279# endif
2280# endif /* DEFINED_ONCE */
2281
2282
2283# ifndef MATCH_MAY_ALLOCATE
2284
2285/* If we cannot allocate large objects within re_match_2_internal,
2286 we make the fail stack and register vectors global.
2287 The fail stack, we grow to the maximum size when a regexp
2288 is compiled.
2289 The register vectors, we adjust in size each time we
2290 compile a regexp, according to the number of registers it needs. */
2291
2292static PREFIX(fail_stack_type) fail_stack;
2293
2294/* Size with which the following vectors are currently allocated.
2295 That is so we can make them bigger as needed,
2296 but never make them smaller. */
2297# ifdef DEFINED_ONCE
2298static int regs_allocated_size;
2299
2300static const char ** regstart, ** regend;
2301static const char ** old_regstart, ** old_regend;
2302static const char **best_regstart, **best_regend;
2303static const char **reg_dummy;
2304# endif /* DEFINED_ONCE */
2305
2306static PREFIX(register_info_type) *PREFIX(reg_info);
2307static PREFIX(register_info_type) *PREFIX(reg_info_dummy);
2308
2309/* Make the register vectors big enough for NUM_REGS registers,
2310 but don't make them smaller. */
2311
2312static void
2313PREFIX(regex_grow_registers) (num_regs)
2314 int num_regs;
2315{
2316 if (num_regs > regs_allocated_size)
2317 {
2318 RETALLOC_IF (regstart, num_regs, const char *);
2319 RETALLOC_IF (regend, num_regs, const char *);
2320 RETALLOC_IF (old_regstart, num_regs, const char *);
2321 RETALLOC_IF (old_regend, num_regs, const char *);
2322 RETALLOC_IF (best_regstart, num_regs, const char *);
2323 RETALLOC_IF (best_regend, num_regs, const char *);
2324 RETALLOC_IF (PREFIX(reg_info), num_regs, PREFIX(register_info_type));
2325 RETALLOC_IF (reg_dummy, num_regs, const char *);
2326 RETALLOC_IF (PREFIX(reg_info_dummy), num_regs, PREFIX(register_info_type));
2327
2328 regs_allocated_size = num_regs;
2329 }
2330}
2331
2332# endif /* not MATCH_MAY_ALLOCATE */
2333
2334
2335# ifndef DEFINED_ONCE
2336static boolean group_in_compile_stack _RE_ARGS ((compile_stack_type
2337 compile_stack,
2338 regnum_t regnum));
2339# endif /* not DEFINED_ONCE */
2340
2341/* `regex_compile' compiles PATTERN (of length SIZE) according to SYNTAX.
2342 Returns one of error codes defined in `regex.h', or zero for success.
2343
2344 Assumes the `allocated' (and perhaps `buffer') and `translate'
2345 fields are set in BUFP on entry.
2346
2347 If it succeeds, results are put in BUFP (if it returns an error, the
2348 contents of BUFP are undefined):
2349 `buffer' is the compiled pattern;
2350 `syntax' is set to SYNTAX;
2351 `used' is set to the length of the compiled pattern;
2352 `fastmap_accurate' is zero;
2353 `re_nsub' is the number of subexpressions in PATTERN;
2354 `not_bol' and `not_eol' are zero;
2355
2356 The `fastmap' and `newline_anchor' fields are neither
2357 examined nor set. */
2358
2359/* Return, freeing storage we allocated. */
2360# ifdef WCHAR
2361# define FREE_STACK_RETURN(value) \
2362 return (free(pattern), free(mbs_offset), free(is_binary), free (compile_stack.stack), value)
2363# else
2364# define FREE_STACK_RETURN(value) \
2365 return (free (compile_stack.stack), value)
2366# endif /* WCHAR */
2367
2368static reg_errcode_t
2369PREFIX(regex_compile) (ARG_PREFIX(pattern), ARG_PREFIX(size), syntax, bufp)
2370 const char *ARG_PREFIX(pattern);
2371 size_t ARG_PREFIX(size);
2372 reg_syntax_t syntax;
2373 struct re_pattern_buffer *bufp;
2374{
2375 /* We fetch characters from PATTERN here. Even though PATTERN is
2376 `char *' (i.e., signed), we declare these variables as unsigned, so
2377 they can be reliably used as array indices. */
2378 register UCHAR_T c, c1;
2379
2380#ifdef WCHAR
2381 /* A temporary space to keep wchar_t pattern and compiled pattern. */
2382 CHAR_T *pattern, *COMPILED_BUFFER_VAR;
2383 size_t size;
2384 /* offset buffer for optimization. See convert_mbs_to_wc. */
2385 int *mbs_offset = NULL;
2386 /* It hold whether each wchar_t is binary data or not. */
2387 char *is_binary = NULL;
2388 /* A flag whether exactn is handling binary data or not. */
2389 char is_exactn_bin = FALSE;
2390#endif /* WCHAR */
2391
2392 /* A random temporary spot in PATTERN. */
2393 const CHAR_T *p1;
2394
2395 /* Points to the end of the buffer, where we should append. */
2396 register UCHAR_T *b;
2397
2398 /* Keeps track of unclosed groups. */
2399 compile_stack_type compile_stack;
2400
2401 /* Points to the current (ending) position in the pattern. */
2402#ifdef WCHAR
2403 const CHAR_T *p;
2404 const CHAR_T *pend;
2405#else /* BYTE */
2406 const CHAR_T *p = pattern;
2407 const CHAR_T *pend = pattern + size;
2408#endif /* WCHAR */
2409
2410 /* How to translate the characters in the pattern. */
2411 RE_TRANSLATE_TYPE translate = bufp->translate;
2412
2413 /* Address of the count-byte of the most recently inserted `exactn'
2414 command. This makes it possible to tell if a new exact-match
2415 character can be added to that command or if the character requires
2416 a new `exactn' command. */
2417 UCHAR_T *pending_exact = 0;
2418
2419 /* Address of start of the most recently finished expression.
2420 This tells, e.g., postfix * where to find the start of its
2421 operand. Reset at the beginning of groups and alternatives. */
2422 UCHAR_T *laststart = 0;
2423
2424 /* Address of beginning of regexp, or inside of last group. */
2425 UCHAR_T *begalt;
2426
2427 /* Address of the place where a forward jump should go to the end of
2428 the containing expression. Each alternative of an `or' -- except the
2429 last -- ends with a forward jump of this sort. */
2430 UCHAR_T *fixup_alt_jump = 0;
2431
2432 /* Counts open-groups as they are encountered. Remembered for the
2433 matching close-group on the compile stack, so the same register
2434 number is put in the stop_memory as the start_memory. */
2435 regnum_t regnum = 0;
2436
2437#ifdef WCHAR
2438 /* Initialize the wchar_t PATTERN and offset_buffer. */
2439 p = pend = pattern = TALLOC(csize + 1, CHAR_T);
2440 mbs_offset = TALLOC(csize + 1, int);
2441 is_binary = TALLOC(csize + 1, char);
2442 if (pattern == NULL || mbs_offset == NULL || is_binary == NULL)
2443 {
2444 free(pattern);
2445 free(mbs_offset);
2446 free(is_binary);
2447 return REG_ESPACE;
2448 }
2449 pattern[csize] = L'\0'; /* sentinel */
2450 size = convert_mbs_to_wcs(pattern, cpattern, csize, mbs_offset, is_binary);
2451 pend = p + size;
2452 if (size < 0)
2453 {
2454 free(pattern);
2455 free(mbs_offset);
2456 free(is_binary);
2457 return REG_BADPAT;
2458 }
2459#endif
2460
2461#ifdef DEBUG
2462 DEBUG_PRINT1 ("\nCompiling pattern: ");
2463 if (debug)
2464 {
2465 unsigned debug_count;
2466
2467 for (debug_count = 0; debug_count < size; debug_count++)
2468 PUT_CHAR (pattern[debug_count]);
2469 putchar ('\n');
2470 }
2471#endif /* DEBUG */
2472
2473 /* Initialize the compile stack. */
2474 compile_stack.stack = TALLOC (INIT_COMPILE_STACK_SIZE, compile_stack_elt_t);
2475 if (compile_stack.stack == NULL)
2476 {
2477#ifdef WCHAR
2478 free(pattern);
2479 free(mbs_offset);
2480 free(is_binary);
2481#endif
2482 return REG_ESPACE;
2483 }
2484
2485 compile_stack.size = INIT_COMPILE_STACK_SIZE;
2486 compile_stack.avail = 0;
2487
2488 /* Initialize the pattern buffer. */
2489 bufp->syntax = syntax;
2490 bufp->fastmap_accurate = 0;
2491 bufp->not_bol = bufp->not_eol = 0;
2492
2493 /* Set `used' to zero, so that if we return an error, the pattern
2494 printer (for debugging) will think there's no pattern. We reset it
2495 at the end. */
2496 bufp->used = 0;
2497
2498 /* Always count groups, whether or not bufp->no_sub is set. */
2499 bufp->re_nsub = 0;
2500
2501#if !defined emacs && !defined SYNTAX_TABLE
2502 /* Initialize the syntax table. */
2503 init_syntax_once ();
2504#endif
2505
2506 if (bufp->allocated == 0)
2507 {
2508 if (bufp->buffer)
2509 { /* If zero allocated, but buffer is non-null, try to realloc
2510 enough space. This loses if buffer's address is bogus, but
2511 that is the user's responsibility. */
2512#ifdef WCHAR
2513 /* Free bufp->buffer and allocate an array for wchar_t pattern
2514 buffer. */
2515 free(bufp->buffer);
2516 COMPILED_BUFFER_VAR = TALLOC (INIT_BUF_SIZE/sizeof(UCHAR_T),
2517 UCHAR_T);
2518#else
2519 RETALLOC (COMPILED_BUFFER_VAR, INIT_BUF_SIZE, UCHAR_T);
2520#endif /* WCHAR */
2521 }
2522 else
2523 { /* Caller did not allocate a buffer. Do it for them. */
2524 COMPILED_BUFFER_VAR = TALLOC (INIT_BUF_SIZE / sizeof(UCHAR_T),
2525 UCHAR_T);
2526 }
2527
2528 if (!COMPILED_BUFFER_VAR) FREE_STACK_RETURN (REG_ESPACE);
2529#ifdef WCHAR
2530 bufp->buffer = (char*)COMPILED_BUFFER_VAR;
2531#endif /* WCHAR */
2532 bufp->allocated = INIT_BUF_SIZE;
2533 }
2534#ifdef WCHAR
2535 else
2536 COMPILED_BUFFER_VAR = (UCHAR_T*) bufp->buffer;
2537#endif
2538
2539 begalt = b = COMPILED_BUFFER_VAR;
2540
2541 /* Loop through the uncompiled pattern until we're at the end. */
2542 while (p != pend)
2543 {
2544 PATFETCH (c);
2545
2546 switch (c)
2547 {
2548 case '^':
2549 {
2550 if ( /* If at start of pattern, it's an operator. */
2551 p == pattern + 1
2552 /* If context independent, it's an operator. */
2553 || syntax & RE_CONTEXT_INDEP_ANCHORS
2554 /* Otherwise, depends on what's come before. */
2555 || PREFIX(at_begline_loc_p) (pattern, p, syntax))
2556 BUF_PUSH (begline);
2557 else
2558 goto normal_char;
2559 }
2560 break;
2561
2562
2563 case '$':
2564 {
2565 if ( /* If at end of pattern, it's an operator. */
2566 p == pend
2567 /* If context independent, it's an operator. */
2568 || syntax & RE_CONTEXT_INDEP_ANCHORS
2569 /* Otherwise, depends on what's next. */
2570 || PREFIX(at_endline_loc_p) (p, pend, syntax))
2571 BUF_PUSH (endline);
2572 else
2573 goto normal_char;
2574 }
2575 break;
2576
2577
2578 case '+':
2579 case '?':
2580 if ((syntax & RE_BK_PLUS_QM)
2581 || (syntax & RE_LIMITED_OPS))
2582 goto normal_char;
2583 handle_plus:
2584 case '*':
2585 /* If there is no previous pattern... */
2586 if (!laststart)
2587 {
2588 if (syntax & RE_CONTEXT_INVALID_OPS)
2589 FREE_STACK_RETURN (REG_BADRPT);
2590 else if (!(syntax & RE_CONTEXT_INDEP_OPS))
2591 goto normal_char;
2592 }
2593
2594 {
2595 /* Are we optimizing this jump? */
2596 boolean keep_string_p = false;
2597
2598 /* 1 means zero (many) matches is allowed. */
2599 char zero_times_ok = 0, many_times_ok = 0;
2600
2601 /* If there is a sequence of repetition chars, collapse it
2602 down to just one (the right one). We can't combine
2603 interval operators with these because of, e.g., `a{2}*',
2604 which should only match an even number of `a's. */
2605
2606 for (;;)
2607 {
2608 zero_times_ok |= c != '+';
2609 many_times_ok |= c != '?';
2610
2611 if (p == pend)
2612 break;
2613
2614 PATFETCH (c);
2615
2616 if (c == '*'
2617 || (!(syntax & RE_BK_PLUS_QM) && (c == '+' || c == '?')))
2618 ;
2619
2620 else if (syntax & RE_BK_PLUS_QM && c == '\\')
2621 {
2622 if (p == pend) FREE_STACK_RETURN (REG_EESCAPE);
2623
2624 PATFETCH (c1);
2625 if (!(c1 == '+' || c1 == '?'))
2626 {
2627 PATUNFETCH;
2628 PATUNFETCH;
2629 break;
2630 }
2631
2632 c = c1;
2633 }
2634 else
2635 {
2636 PATUNFETCH;
2637 break;
2638 }
2639
2640 /* If we get here, we found another repeat character. */
2641 }
2642
2643 /* Star, etc. applied to an empty pattern is equivalent
2644 to an empty pattern. */
2645 if (!laststart)
2646 break;
2647
2648 /* Now we know whether or not zero matches is allowed
2649 and also whether or not two or more matches is allowed. */
2650 if (many_times_ok)
2651 { /* More than one repetition is allowed, so put in at the
2652 end a backward relative jump from `b' to before the next
2653 jump we're going to put in below (which jumps from
2654 laststart to after this jump).
2655
2656 But if we are at the `*' in the exact sequence `.*\n',
2657 insert an unconditional jump backwards to the .,
2658 instead of the beginning of the loop. This way we only
2659 push a failure point once, instead of every time
2660 through the loop. */
2661 assert (p - 1 > pattern);
2662
2663 /* Allocate the space for the jump. */
2664 GET_BUFFER_SPACE (1 + OFFSET_ADDRESS_SIZE);
2665
2666 /* We know we are not at the first character of the pattern,
2667 because laststart was nonzero. And we've already
2668 incremented `p', by the way, to be the character after
2669 the `*'. Do we have to do something analogous here
2670 for null bytes, because of RE_DOT_NOT_NULL? */
2671 if (TRANSLATE (*(p - 2)) == TRANSLATE ('.')
2672 && zero_times_ok
2673 && p < pend && TRANSLATE (*p) == TRANSLATE ('\n')
2674 && !(syntax & RE_DOT_NEWLINE))
2675 { /* We have .*\n. */
2676 STORE_JUMP (jump, b, laststart);
2677 keep_string_p = true;
2678 }
2679 else
2680 /* Anything else. */
2681 STORE_JUMP (maybe_pop_jump, b, laststart -
2682 (1 + OFFSET_ADDRESS_SIZE));
2683
2684 /* We've added more stuff to the buffer. */
2685 b += 1 + OFFSET_ADDRESS_SIZE;
2686 }
2687
2688 /* On failure, jump from laststart to b + 3, which will be the
2689 end of the buffer after this jump is inserted. */
2690 /* ifdef WCHAR, 'b + 1 + OFFSET_ADDRESS_SIZE' instead of
2691 'b + 3'. */
2692 GET_BUFFER_SPACE (1 + OFFSET_ADDRESS_SIZE);
2693 INSERT_JUMP (keep_string_p ? on_failure_keep_string_jump
2694 : on_failure_jump,
2695 laststart, b + 1 + OFFSET_ADDRESS_SIZE);
2696 pending_exact = 0;
2697 b += 1 + OFFSET_ADDRESS_SIZE;
2698
2699 if (!zero_times_ok)
2700 {
2701 /* At least one repetition is required, so insert a
2702 `dummy_failure_jump' before the initial
2703 `on_failure_jump' instruction of the loop. This
2704 effects a skip over that instruction the first time
2705 we hit that loop. */
2706 GET_BUFFER_SPACE (1 + OFFSET_ADDRESS_SIZE);
2707 INSERT_JUMP (dummy_failure_jump, laststart, laststart +
2708 2 + 2 * OFFSET_ADDRESS_SIZE);
2709 b += 1 + OFFSET_ADDRESS_SIZE;
2710 }
2711 }
2712 break;
2713
2714
2715 case '.':
2716 laststart = b;
2717 BUF_PUSH (anychar);
2718 break;
2719
2720
2721 case '[':
2722 {
2723 boolean had_char_class = false;
2724#ifdef WCHAR
2725 CHAR_T range_start = 0xffffffff;
2726#else
2727 unsigned int range_start = 0xffffffff;
2728#endif
2729 if (p == pend) FREE_STACK_RETURN (REG_EBRACK);
2730
2731#ifdef WCHAR
2732 /* We assume a charset(_not) structure as a wchar_t array.
2733 charset[0] = (re_opcode_t) charset(_not)
2734 charset[1] = l (= length of char_classes)
2735 charset[2] = m (= length of collating_symbols)
2736 charset[3] = n (= length of equivalence_classes)
2737 charset[4] = o (= length of char_ranges)
2738 charset[5] = p (= length of chars)
2739
2740 charset[6] = char_class (wctype_t)
2741 charset[6+CHAR_CLASS_SIZE] = char_class (wctype_t)
2742 ...
2743 charset[l+5] = char_class (wctype_t)
2744
2745 charset[l+6] = collating_symbol (wchar_t)
2746 ...
2747 charset[l+m+5] = collating_symbol (wchar_t)
2748 ifdef _LIBC we use the index if
2749 _NL_COLLATE_SYMB_EXTRAMB instead of
2750 wchar_t string.
2751
2752 charset[l+m+6] = equivalence_classes (wchar_t)
2753 ...
2754 charset[l+m+n+5] = equivalence_classes (wchar_t)
2755 ifdef _LIBC we use the index in
2756 _NL_COLLATE_WEIGHT instead of
2757 wchar_t string.
2758
2759 charset[l+m+n+6] = range_start
2760 charset[l+m+n+7] = range_end
2761 ...
2762 charset[l+m+n+2o+4] = range_start
2763 charset[l+m+n+2o+5] = range_end
2764 ifdef _LIBC we use the value looked up
2765 in _NL_COLLATE_COLLSEQ instead of
2766 wchar_t character.
2767
2768 charset[l+m+n+2o+6] = char
2769 ...
2770 charset[l+m+n+2o+p+5] = char
2771
2772 */
2773
2774 /* We need at least 6 spaces: the opcode, the length of
2775 char_classes, the length of collating_symbols, the length of
2776 equivalence_classes, the length of char_ranges, the length of
2777 chars. */
2778 GET_BUFFER_SPACE (6);
2779
2780 /* Save b as laststart. And We use laststart as the pointer
2781 to the first element of the charset here.
2782 In other words, laststart[i] indicates charset[i]. */
2783 laststart = b;
2784
2785 /* We test `*p == '^' twice, instead of using an if
2786 statement, so we only need one BUF_PUSH. */
2787 BUF_PUSH (*p == '^' ? charset_not : charset);
2788 if (*p == '^')
2789 p++;
2790
2791 /* Push the length of char_classes, the length of
2792 collating_symbols, the length of equivalence_classes, the
2793 length of char_ranges and the length of chars. */
2794 BUF_PUSH_3 (0, 0, 0);
2795 BUF_PUSH_2 (0, 0);
2796
2797 /* Remember the first position in the bracket expression. */
2798 p1 = p;
2799
2800 /* charset_not matches newline according to a syntax bit. */
2801 if ((re_opcode_t) b[-6] == charset_not
2802 && (syntax & RE_HAT_LISTS_NOT_NEWLINE))
2803 {
2804 BUF_PUSH('\n');
2805 laststart[5]++; /* Update the length of characters */
2806 }
2807
2808 /* Read in characters and ranges, setting map bits. */
2809 for (;;)
2810 {
2811 if (p == pend) FREE_STACK_RETURN (REG_EBRACK);
2812
2813 PATFETCH (c);
2814
2815 /* \ might escape characters inside [...] and [^...]. */
2816 if ((syntax & RE_BACKSLASH_ESCAPE_IN_LISTS) && c == '\\')
2817 {
2818 if (p == pend) FREE_STACK_RETURN (REG_EESCAPE);
2819
2820 PATFETCH (c1);
2821 BUF_PUSH(c1);
2822 laststart[5]++; /* Update the length of chars */
2823 range_start = c1;
2824 continue;
2825 }
2826
2827 /* Could be the end of the bracket expression. If it's
2828 not (i.e., when the bracket expression is `[]' so
2829 far), the ']' character bit gets set way below. */
2830 if (c == ']' && p != p1 + 1)
2831 break;
2832
2833 /* Look ahead to see if it's a range when the last thing
2834 was a character class. */
2835 if (had_char_class && c == '-' && *p != ']')
2836 FREE_STACK_RETURN (REG_ERANGE);
2837
2838 /* Look ahead to see if it's a range when the last thing
2839 was a character: if this is a hyphen not at the
2840 beginning or the end of a list, then it's the range
2841 operator. */
2842 if (c == '-'
2843 && !(p - 2 >= pattern && p[-2] == '[')
2844 && !(p - 3 >= pattern && p[-3] == '[' && p[-2] == '^')
2845 && *p != ']')
2846 {
2847 reg_errcode_t ret;
2848 /* Allocate the space for range_start and range_end. */
2849 GET_BUFFER_SPACE (2);
2850 /* Update the pointer to indicate end of buffer. */
2851 b += 2;
2852 ret = wcs_compile_range (range_start, &p, pend, translate,
2853 syntax, b, laststart);
2854 if (ret != REG_NOERROR) FREE_STACK_RETURN (ret);
2855 range_start = 0xffffffff;
2856 }
2857 else if (p[0] == '-' && p[1] != ']')
2858 { /* This handles ranges made up of characters only. */
2859 reg_errcode_t ret;
2860
2861 /* Move past the `-'. */
2862 PATFETCH (c1);
2863 /* Allocate the space for range_start and range_end. */
2864 GET_BUFFER_SPACE (2);
2865 /* Update the pointer to indicate end of buffer. */
2866 b += 2;
2867 ret = wcs_compile_range (c, &p, pend, translate, syntax, b,
2868 laststart);
2869 if (ret != REG_NOERROR) FREE_STACK_RETURN (ret);
2870 range_start = 0xffffffff;
2871 }
2872
2873 /* See if we're at the beginning of a possible character
2874 class. */
2875 else if (syntax & RE_CHAR_CLASSES && c == '[' && *p == ':')
2876 { /* Leave room for the null. */
2877 char str[CHAR_CLASS_MAX_LENGTH + 1];
2878
2879 PATFETCH (c);
2880 c1 = 0;
2881
2882 /* If pattern is `[[:'. */
2883 if (p == pend) FREE_STACK_RETURN (REG_EBRACK);
2884
2885 for (;;)
2886 {
2887 PATFETCH (c);
2888 if ((c == ':' && *p == ']') || p == pend)
2889 break;
2890 if (c1 < CHAR_CLASS_MAX_LENGTH)
2891 str[c1++] = c;
2892 else
2893 /* This is in any case an invalid class name. */
2894 str[0] = '\0';
2895 }
2896 str[c1] = '\0';
2897
2898 /* If isn't a word bracketed by `[:' and `:]':
2899 undo the ending character, the letters, and leave
2900 the leading `:' and `[' (but store them as character). */
2901 if (c == ':' && *p == ']')
2902 {
2903 wctype_t wt;
2904 uintptr_t alignedp;
2905
2906 /* Query the character class as wctype_t. */
2907 wt = IS_CHAR_CLASS (str);
2908 if (wt == 0)
2909 FREE_STACK_RETURN (REG_ECTYPE);
2910
2911 /* Throw away the ] at the end of the character
2912 class. */
2913 PATFETCH (c);
2914
2915 if (p == pend) FREE_STACK_RETURN (REG_EBRACK);
2916
2917 /* Allocate the space for character class. */
2918 GET_BUFFER_SPACE(CHAR_CLASS_SIZE);
2919 /* Update the pointer to indicate end of buffer. */
2920 b += CHAR_CLASS_SIZE;
2921 /* Move data which follow character classes
2922 not to violate the data. */
2923 insert_space(CHAR_CLASS_SIZE,
2924 laststart + 6 + laststart[1],
2925 b - 1);
2926 alignedp = ((uintptr_t)(laststart + 6 + laststart[1])
2927 + __alignof__(wctype_t) - 1)
2928 & ~(uintptr_t)(__alignof__(wctype_t) - 1);
2929 /* Store the character class. */
2930 *((wctype_t*)alignedp) = wt;
2931 /* Update length of char_classes */
2932 laststart[1] += CHAR_CLASS_SIZE;
2933
2934 had_char_class = true;
2935 }
2936 else
2937 {
2938 c1++;
2939 while (c1--)
2940 PATUNFETCH;
2941 BUF_PUSH ('[');
2942 BUF_PUSH (':');
2943 laststart[5] += 2; /* Update the length of characters */
2944 range_start = ':';
2945 had_char_class = false;
2946 }
2947 }
2948 else if (syntax & RE_CHAR_CLASSES && c == '[' && (*p == '='
2949 || *p == '.'))
2950 {
2951 CHAR_T str[128]; /* Should be large enough. */
2952 CHAR_T delim = *p; /* '=' or '.' */
2953# ifdef _LIBC
2954 uint32_t nrules =
2955 _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_NRULES);
2956# endif
2957 PATFETCH (c);
2958 c1 = 0;
2959
2960 /* If pattern is `[[=' or '[[.'. */
2961 if (p == pend) FREE_STACK_RETURN (REG_EBRACK);
2962
2963 for (;;)
2964 {
2965 PATFETCH (c);
2966 if ((c == delim && *p == ']') || p == pend)
2967 break;
2968 if (c1 < sizeof (str) - 1)
2969 str[c1++] = c;
2970 else
2971 /* This is in any case an invalid class name. */
2972 str[0] = '\0';
2973 }
2974 str[c1] = '\0';
2975
2976 if (c == delim && *p == ']' && str[0] != '\0')
2977 {
2978 unsigned int i, offset;
2979 /* If we have no collation data we use the default
2980 collation in which each character is in a class
2981 by itself. It also means that ASCII is the
2982 character set and therefore we cannot have character
2983 with more than one byte in the multibyte
2984 representation. */
2985
2986 /* If not defined _LIBC, we push the name and
2987 `\0' for the sake of matching performance. */
2988 int datasize = c1 + 1;
2989
2990# ifdef _LIBC
2991 int32_t idx = 0;
2992 if (nrules == 0)
2993# endif
2994 {
2995 if (c1 != 1)
2996 FREE_STACK_RETURN (REG_ECOLLATE);
2997 }
2998# ifdef _LIBC
2999 else
3000 {
3001 const int32_t *table;
3002 const int32_t *weights;
3003 const int32_t *extra;
3004 const int32_t *indirect;
3005 wint_t *cp;
3006
3007 /* This #include defines a local function! */
3008# include <locale/weightwc.h>
3009
3010 if(delim == '=')
3011 {
3012 /* We push the index for equivalence class. */
3013 cp = (wint_t*)str;
3014
3015 table = (const int32_t *)
3016 _NL_CURRENT (LC_COLLATE,
3017 _NL_COLLATE_TABLEWC);
3018 weights = (const int32_t *)
3019 _NL_CURRENT (LC_COLLATE,
3020 _NL_COLLATE_WEIGHTWC);
3021 extra = (const int32_t *)
3022 _NL_CURRENT (LC_COLLATE,
3023 _NL_COLLATE_EXTRAWC);
3024 indirect = (const int32_t *)
3025 _NL_CURRENT (LC_COLLATE,
3026 _NL_COLLATE_INDIRECTWC);
3027
3028 idx = findidx ((const wint_t**)&cp);
3029 if (idx == 0 || cp < (wint_t*) str + c1)
3030 /* This is no valid character. */
3031 FREE_STACK_RETURN (REG_ECOLLATE);
3032
3033 str[0] = (wchar_t)idx;
3034 }
3035 else /* delim == '.' */
3036 {
3037 /* We push collation sequence value
3038 for collating symbol. */
3039 int32_t table_size;
3040 const int32_t *symb_table;
3041 const unsigned char *extra;
3042 int32_t idx;
3043 int32_t elem;
3044 int32_t second;
3045 int32_t hash;
3046 char char_str[c1];
3047
3048 /* We have to convert the name to a single-byte
3049 string. This is possible since the names
3050 consist of ASCII characters and the internal
3051 representation is UCS4. */
3052 for (i = 0; i < c1; ++i)
3053 char_str[i] = str[i];
3054
3055 table_size =
3056 _NL_CURRENT_WORD (LC_COLLATE,
3057 _NL_COLLATE_SYMB_HASH_SIZEMB);
3058 symb_table = (const int32_t *)
3059 _NL_CURRENT (LC_COLLATE,
3060 _NL_COLLATE_SYMB_TABLEMB);
3061 extra = (const unsigned char *)
3062 _NL_CURRENT (LC_COLLATE,
3063 _NL_COLLATE_SYMB_EXTRAMB);
3064
3065 /* Locate the character in the hashing table. */
3066 hash = elem_hash (char_str, c1);
3067
3068 idx = 0;
3069 elem = hash % table_size;
3070 second = hash % (table_size - 2);
3071 while (symb_table[2 * elem] != 0)
3072 {
3073 /* First compare the hashing value. */
3074 if (symb_table[2 * elem] == hash
3075 && c1 == extra[symb_table[2 * elem + 1]]
3076 && memcmp (char_str,
3077 &extra[symb_table[2 * elem + 1]
3078 + 1], c1) == 0)
3079 {
3080 /* Yep, this is the entry. */
3081 idx = symb_table[2 * elem + 1];
3082 idx += 1 + extra[idx];
3083 break;
3084 }
3085
3086 /* Next entry. */
3087 elem += second;
3088 }
3089
3090 if (symb_table[2 * elem] != 0)
3091 {
3092 /* Compute the index of the byte sequence
3093 in the table. */
3094 idx += 1 + extra[idx];
3095 /* Adjust for the alignment. */
3096 idx = (idx + 3) & ~3;
3097
3098 str[0] = (wchar_t) idx + 4;
3099 }
3100 else if (symb_table[2 * elem] == 0 && c1 == 1)
3101 {
3102 /* No valid character. Match it as a
3103 single byte character. */
3104 had_char_class = false;
3105 BUF_PUSH(str[0]);
3106 /* Update the length of characters */
3107 laststart[5]++;
3108 range_start = str[0];
3109
3110 /* Throw away the ] at the end of the
3111 collating symbol. */
3112 PATFETCH (c);
3113 /* exit from the switch block. */
3114 continue;
3115 }
3116 else
3117 FREE_STACK_RETURN (REG_ECOLLATE);
3118 }
3119 datasize = 1;
3120 }
3121# endif
3122 /* Throw away the ] at the end of the equivalence
3123 class (or collating symbol). */
3124 PATFETCH (c);
3125
3126 /* Allocate the space for the equivalence class
3127 (or collating symbol) (and '\0' if needed). */
3128 GET_BUFFER_SPACE(datasize);
3129 /* Update the pointer to indicate end of buffer. */
3130 b += datasize;
3131
3132 if (delim == '=')
3133 { /* equivalence class */
3134 /* Calculate the offset of char_ranges,
3135 which is next to equivalence_classes. */
3136 offset = laststart[1] + laststart[2]
3137 + laststart[3] +6;
3138 /* Insert space. */
3139 insert_space(datasize, laststart + offset, b - 1);
3140
3141 /* Write the equivalence_class and \0. */
3142 for (i = 0 ; i < datasize ; i++)
3143 laststart[offset + i] = str[i];
3144
3145 /* Update the length of equivalence_classes. */
3146 laststart[3] += datasize;
3147 had_char_class = true;
3148 }
3149 else /* delim == '.' */
3150 { /* collating symbol */
3151 /* Calculate the offset of the equivalence_classes,
3152 which is next to collating_symbols. */
3153 offset = laststart[1] + laststart[2] + 6;
3154 /* Insert space and write the collationg_symbol
3155 and \0. */
3156 insert_space(datasize, laststart + offset, b-1);
3157 for (i = 0 ; i < datasize ; i++)
3158 laststart[offset + i] = str[i];
3159
3160 /* In re_match_2_internal if range_start < -1, we
3161 assume -range_start is the offset of the
3162 collating symbol which is specified as
3163 the character of the range start. So we assign
3164 -(laststart[1] + laststart[2] + 6) to
3165 range_start. */
3166 range_start = -(laststart[1] + laststart[2] + 6);
3167 /* Update the length of collating_symbol. */
3168 laststart[2] += datasize;
3169 had_char_class = false;
3170 }
3171 }
3172 else
3173 {
3174 c1++;
3175 while (c1--)
3176 PATUNFETCH;
3177 BUF_PUSH ('[');
3178 BUF_PUSH (delim);
3179 laststart[5] += 2; /* Update the length of characters */
3180 range_start = delim;
3181 had_char_class = false;
3182 }
3183 }
3184 else
3185 {
3186 had_char_class = false;
3187 BUF_PUSH(c);
3188 laststart[5]++; /* Update the length of characters */
3189 range_start = c;
3190 }
3191 }
3192
3193#else /* BYTE */
3194 /* Ensure that we have enough space to push a charset: the
3195 opcode, the length count, and the bitset; 34 bytes in all. */
3196 GET_BUFFER_SPACE (34);
3197
3198 laststart = b;
3199
3200 /* We test `*p == '^' twice, instead of using an if
3201 statement, so we only need one BUF_PUSH. */
3202 BUF_PUSH (*p == '^' ? charset_not : charset);
3203 if (*p == '^')
3204 p++;
3205
3206 /* Remember the first position in the bracket expression. */
3207 p1 = p;
3208
3209 /* Push the number of bytes in the bitmap. */
3210 BUF_PUSH ((1 << BYTEWIDTH) / BYTEWIDTH);
3211
3212 /* Clear the whole map. */
3213 bzero (b, (1 << BYTEWIDTH) / BYTEWIDTH);
3214
3215 /* charset_not matches newline according to a syntax bit. */
3216 if ((re_opcode_t) b[-2] == charset_not
3217 && (syntax & RE_HAT_LISTS_NOT_NEWLINE))
3218 SET_LIST_BIT ('\n');
3219
3220 /* Read in characters and ranges, setting map bits. */
3221 for (;;)
3222 {
3223 if (p == pend) FREE_STACK_RETURN (REG_EBRACK);
3224
3225 PATFETCH (c);
3226
3227 /* \ might escape characters inside [...] and [^...]. */
3228 if ((syntax & RE_BACKSLASH_ESCAPE_IN_LISTS) && c == '\\')
3229 {
3230 if (p == pend) FREE_STACK_RETURN (REG_EESCAPE);
3231
3232 PATFETCH (c1);
3233 SET_LIST_BIT (c1);
3234 range_start = c1;
3235 continue;
3236 }
3237
3238 /* Could be the end of the bracket expression. If it's
3239 not (i.e., when the bracket expression is `[]' so
3240 far), the ']' character bit gets set way below. */
3241 if (c == ']' && p != p1 + 1)
3242 break;
3243
3244 /* Look ahead to see if it's a range when the last thing
3245 was a character class. */
3246 if (had_char_class && c == '-' && *p != ']')
3247 FREE_STACK_RETURN (REG_ERANGE);
3248
3249 /* Look ahead to see if it's a range when the last thing
3250 was a character: if this is a hyphen not at the
3251 beginning or the end of a list, then it's the range
3252 operator. */
3253 if (c == '-'
3254 && !(p - 2 >= pattern && p[-2] == '[')
3255 && !(p - 3 >= pattern && p[-3] == '[' && p[-2] == '^')
3256 && *p != ']')
3257 {
3258 reg_errcode_t ret
3259 = byte_compile_range (range_start, &p, pend, translate,
3260 syntax, b);
3261 if (ret != REG_NOERROR) FREE_STACK_RETURN (ret);
3262 range_start = 0xffffffff;
3263 }
3264
3265 else if (p[0] == '-' && p[1] != ']')
3266 { /* This handles ranges made up of characters only. */
3267 reg_errcode_t ret;
3268
3269 /* Move past the `-'. */
3270 PATFETCH (c1);
3271
3272 ret = byte_compile_range (c, &p, pend, translate, syntax, b);
3273 if (ret != REG_NOERROR) FREE_STACK_RETURN (ret);
3274 range_start = 0xffffffff;
3275 }
3276
3277 /* See if we're at the beginning of a possible character
3278 class. */
3279
3280 else if (syntax & RE_CHAR_CLASSES && c == '[' && *p == ':')
3281 { /* Leave room for the null. */
3282 char str[CHAR_CLASS_MAX_LENGTH + 1];
3283
3284 PATFETCH (c);
3285 c1 = 0;
3286
3287 /* If pattern is `[[:'. */
3288 if (p == pend) FREE_STACK_RETURN (REG_EBRACK);
3289
3290 for (;;)
3291 {
3292 PATFETCH (c);
3293 if ((c == ':' && *p == ']') || p == pend)
3294 break;
3295 if (c1 < CHAR_CLASS_MAX_LENGTH)
3296 str[c1++] = c;
3297 else
3298 /* This is in any case an invalid class name. */
3299 str[0] = '\0';
3300 }
3301 str[c1] = '\0';
3302
3303 /* If isn't a word bracketed by `[:' and `:]':
3304 undo the ending character, the letters, and leave
3305 the leading `:' and `[' (but set bits for them). */
3306 if (c == ':' && *p == ']')
3307 {
3308# if defined _LIBC || WIDE_CHAR_SUPPORT
3309 boolean is_lower = STREQ (str, "lower");
3310 boolean is_upper = STREQ (str, "upper");
3311 wctype_t wt;
3312 int ch;
3313
3314 wt = IS_CHAR_CLASS (str);
3315 if (wt == 0)
3316 FREE_STACK_RETURN (REG_ECTYPE);
3317
3318 /* Throw away the ] at the end of the character
3319 class. */
3320 PATFETCH (c);
3321
3322 if (p == pend) FREE_STACK_RETURN (REG_EBRACK);
3323
3324 for (ch = 0; ch < 1 << BYTEWIDTH; ++ch)
3325 {
3326 if (iswctype (btowc (ch), wt))
3327 SET_LIST_BIT (ch);
3328
3329 if (translate && (is_upper || is_lower)
3330 && (ISUPPER (ch) || ISLOWER (ch)))
3331 SET_LIST_BIT (ch);
3332 }
3333
3334 had_char_class = true;
3335# else
3336 int ch;
3337 boolean is_alnum = STREQ (str, "alnum");
3338 boolean is_alpha = STREQ (str, "alpha");
3339 boolean is_blank = STREQ (str, "blank");
3340 boolean is_cntrl = STREQ (str, "cntrl");
3341 boolean is_digit = STREQ (str, "digit");
3342 boolean is_graph = STREQ (str, "graph");
3343 boolean is_lower = STREQ (str, "lower");
3344 boolean is_print = STREQ (str, "print");
3345 boolean is_punct = STREQ (str, "punct");
3346 boolean is_space = STREQ (str, "space");
3347 boolean is_upper = STREQ (str, "upper");
3348 boolean is_xdigit = STREQ (str, "xdigit");
3349
3350 if (!IS_CHAR_CLASS (str))
3351 FREE_STACK_RETURN (REG_ECTYPE);
3352
3353 /* Throw away the ] at the end of the character
3354 class. */
3355 PATFETCH (c);
3356
3357 if (p == pend) FREE_STACK_RETURN (REG_EBRACK);
3358
3359 for (ch = 0; ch < 1 << BYTEWIDTH; ch++)
3360 {
3361 /* This was split into 3 if's to
3362 avoid an arbitrary limit in some compiler. */
3363 if ( (is_alnum && ISALNUM (ch))
3364 || (is_alpha && ISALPHA (ch))
3365 || (is_blank && ISBLANK (ch))
3366 || (is_cntrl && ISCNTRL (ch)))
3367 SET_LIST_BIT (ch);
3368 if ( (is_digit && ISDIGIT (ch))
3369 || (is_graph && ISGRAPH (ch))
3370 || (is_lower && ISLOWER (ch))
3371 || (is_print && ISPRINT (ch)))
3372 SET_LIST_BIT (ch);
3373 if ( (is_punct && ISPUNCT (ch))
3374 || (is_space && ISSPACE (ch))
3375 || (is_upper && ISUPPER (ch))
3376 || (is_xdigit && ISXDIGIT (ch)))
3377 SET_LIST_BIT (ch);
3378 if ( translate && (is_upper || is_lower)
3379 && (ISUPPER (ch) || ISLOWER (ch)))
3380 SET_LIST_BIT (ch);
3381 }
3382 had_char_class = true;
3383# endif /* libc || wctype.h */
3384 }
3385 else
3386 {
3387 c1++;
3388 while (c1--)
3389 PATUNFETCH;
3390 SET_LIST_BIT ('[');
3391 SET_LIST_BIT (':');
3392 range_start = ':';
3393 had_char_class = false;
3394 }
3395 }
3396 else if (syntax & RE_CHAR_CLASSES && c == '[' && *p == '=')
3397 {
3398 unsigned char str[MB_LEN_MAX + 1];
3399# ifdef _LIBC
3400 uint32_t nrules =
3401 _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_NRULES);
3402# endif
3403
3404 PATFETCH (c);
3405 c1 = 0;
3406
3407 /* If pattern is `[[='. */
3408 if (p == pend) FREE_STACK_RETURN (REG_EBRACK);
3409
3410 for (;;)
3411 {
3412 PATFETCH (c);
3413 if ((c == '=' && *p == ']') || p == pend)
3414 break;
3415 if (c1 < MB_LEN_MAX)
3416 str[c1++] = c;
3417 else
3418 /* This is in any case an invalid class name. */
3419 str[0] = '\0';
3420 }
3421 str[c1] = '\0';
3422
3423 if (c == '=' && *p == ']' && str[0] != '\0')
3424 {
3425 /* If we have no collation data we use the default
3426 collation in which each character is in a class
3427 by itself. It also means that ASCII is the
3428 character set and therefore we cannot have character
3429 with more than one byte in the multibyte
3430 representation. */
3431# ifdef _LIBC
3432 if (nrules == 0)
3433# endif
3434 {
3435 if (c1 != 1)
3436 FREE_STACK_RETURN (REG_ECOLLATE);
3437
3438 /* Throw away the ] at the end of the equivalence
3439 class. */
3440 PATFETCH (c);
3441
3442 /* Set the bit for the character. */
3443 SET_LIST_BIT (str[0]);
3444 }
3445# ifdef _LIBC
3446 else
3447 {
3448 /* Try to match the byte sequence in `str' against
3449 those known to the collate implementation.
3450 First find out whether the bytes in `str' are
3451 actually from exactly one character. */
3452 const int32_t *table;
3453 const unsigned char *weights;
3454 const unsigned char *extra;
3455 const int32_t *indirect;
3456 int32_t idx;
3457 const unsigned char *cp = str;
3458 int ch;
3459
3460 /* This #include defines a local function! */
3461# include <locale/weight.h>
3462
3463 table = (const int32_t *)
3464 _NL_CURRENT (LC_COLLATE, _NL_COLLATE_TABLEMB);
3465 weights = (const unsigned char *)
3466 _NL_CURRENT (LC_COLLATE, _NL_COLLATE_WEIGHTMB);
3467 extra = (const unsigned char *)
3468 _NL_CURRENT (LC_COLLATE, _NL_COLLATE_EXTRAMB);
3469 indirect = (const int32_t *)
3470 _NL_CURRENT (LC_COLLATE, _NL_COLLATE_INDIRECTMB);
3471
3472 idx = findidx (&cp);
3473 if (idx == 0 || cp < str + c1)
3474 /* This is no valid character. */
3475 FREE_STACK_RETURN (REG_ECOLLATE);
3476
3477 /* Throw away the ] at the end of the equivalence
3478 class. */
3479 PATFETCH (c);
3480
3481 /* Now we have to go throught the whole table
3482 and find all characters which have the same
3483 first level weight.
3484
3485 XXX Note that this is not entirely correct.
3486 we would have to match multibyte sequences
3487 but this is not possible with the current
3488 implementation. */
3489 for (ch = 1; ch < 256; ++ch)
3490 /* XXX This test would have to be changed if we
3491 would allow matching multibyte sequences. */
3492 if (table[ch] > 0)
3493 {
3494 int32_t idx2 = table[ch];
3495 size_t len = weights[idx2];
3496
3497 /* Test whether the lenghts match. */
3498 if (weights[idx] == len)
3499 {
3500 /* They do. New compare the bytes of
3501 the weight. */
3502 size_t cnt = 0;
3503
3504 while (cnt < len
3505 && (weights[idx + 1 + cnt]
3506 == weights[idx2 + 1 + cnt]))
3507 ++cnt;
3508
3509 if (cnt == len)
3510 /* They match. Mark the character as
3511 acceptable. */
3512 SET_LIST_BIT (ch);
3513 }
3514 }
3515 }
3516# endif
3517 had_char_class = true;
3518 }
3519 else
3520 {
3521 c1++;
3522 while (c1--)
3523 PATUNFETCH;
3524 SET_LIST_BIT ('[');
3525 SET_LIST_BIT ('=');
3526 range_start = '=';
3527 had_char_class = false;
3528 }
3529 }
3530 else if (syntax & RE_CHAR_CLASSES && c == '[' && *p == '.')
3531 {
3532 unsigned char str[128]; /* Should be large enough. */
3533# ifdef _LIBC
3534 uint32_t nrules =
3535 _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_NRULES);
3536# endif
3537
3538 PATFETCH (c);
3539 c1 = 0;
3540
3541 /* If pattern is `[[.'. */
3542 if (p == pend) FREE_STACK_RETURN (REG_EBRACK);
3543
3544 for (;;)
3545 {
3546 PATFETCH (c);
3547 if ((c == '.' && *p == ']') || p == pend)
3548 break;
3549 if (c1 < sizeof (str))
3550 str[c1++] = c;
3551 else
3552 /* This is in any case an invalid class name. */
3553 str[0] = '\0';
3554 }
3555 str[c1] = '\0';
3556
3557 if (c == '.' && *p == ']' && str[0] != '\0')
3558 {
3559 /* If we have no collation data we use the default
3560 collation in which each character is the name
3561 for its own class which contains only the one
3562 character. It also means that ASCII is the
3563 character set and therefore we cannot have character
3564 with more than one byte in the multibyte
3565 representation. */
3566# ifdef _LIBC
3567 if (nrules == 0)
3568# endif
3569 {
3570 if (c1 != 1)
3571 FREE_STACK_RETURN (REG_ECOLLATE);
3572
3573 /* Throw away the ] at the end of the equivalence
3574 class. */
3575 PATFETCH (c);
3576
3577 /* Set the bit for the character. */
3578 SET_LIST_BIT (str[0]);
3579 range_start = ((const unsigned char *) str)[0];
3580 }
3581# ifdef _LIBC
3582 else
3583 {
3584 /* Try to match the byte sequence in `str' against
3585 those known to the collate implementation.
3586 First find out whether the bytes in `str' are
3587 actually from exactly one character. */
3588 int32_t table_size;
3589 const int32_t *symb_table;
3590 const unsigned char *extra;
3591 int32_t idx;
3592 int32_t elem;
3593 int32_t second;
3594 int32_t hash;
3595
3596 table_size =
3597 _NL_CURRENT_WORD (LC_COLLATE,
3598 _NL_COLLATE_SYMB_HASH_SIZEMB);
3599 symb_table = (const int32_t *)
3600 _NL_CURRENT (LC_COLLATE,
3601 _NL_COLLATE_SYMB_TABLEMB);
3602 extra = (const unsigned char *)
3603 _NL_CURRENT (LC_COLLATE,
3604 _NL_COLLATE_SYMB_EXTRAMB);
3605
3606 /* Locate the character in the hashing table. */
3607 hash = elem_hash (str, c1);
3608
3609 idx = 0;
3610 elem = hash % table_size;
3611 second = hash % (table_size - 2);
3612 while (symb_table[2 * elem] != 0)
3613 {
3614 /* First compare the hashing value. */
3615 if (symb_table[2 * elem] == hash
3616 && c1 == extra[symb_table[2 * elem + 1]]
3617 && memcmp (str,
3618 &extra[symb_table[2 * elem + 1]
3619 + 1],
3620 c1) == 0)
3621 {
3622 /* Yep, this is the entry. */
3623 idx = symb_table[2 * elem + 1];
3624 idx += 1 + extra[idx];
3625 break;
3626 }
3627
3628 /* Next entry. */
3629 elem += second;
3630 }
3631
3632 if (symb_table[2 * elem] == 0)
3633 /* This is no valid character. */
3634 FREE_STACK_RETURN (REG_ECOLLATE);
3635
3636 /* Throw away the ] at the end of the equivalence
3637 class. */
3638 PATFETCH (c);
3639
3640 /* Now add the multibyte character(s) we found
3641 to the accept list.
3642
3643 XXX Note that this is not entirely correct.
3644 we would have to match multibyte sequences
3645 but this is not possible with the current
3646 implementation. Also, we have to match
3647 collating symbols, which expand to more than
3648 one file, as a whole and not allow the
3649 individual bytes. */
3650 c1 = extra[idx++];
3651 if (c1 == 1)
3652 range_start = extra[idx];
3653 while (c1-- > 0)
3654 {
3655 SET_LIST_BIT (extra[idx]);
3656 ++idx;
3657 }
3658 }
3659# endif
3660 had_char_class = false;
3661 }
3662 else
3663 {
3664 c1++;
3665 while (c1--)
3666 PATUNFETCH;
3667 SET_LIST_BIT ('[');
3668 SET_LIST_BIT ('.');
3669 range_start = '.';
3670 had_char_class = false;
3671 }
3672 }
3673 else
3674 {
3675 had_char_class = false;
3676 SET_LIST_BIT (c);
3677 range_start = c;
3678 }
3679 }
3680
3681 /* Discard any (non)matching list bytes that are all 0 at the
3682 end of the map. Decrease the map-length byte too. */
3683 while ((int) b[-1] > 0 && b[b[-1] - 1] == 0)
3684 b[-1]--;
3685 b += b[-1];
3686#endif /* WCHAR */
3687 }
3688 break;
3689
3690
3691 case '(':
3692 if (syntax & RE_NO_BK_PARENS)
3693 goto handle_open;
3694 else
3695 goto normal_char;
3696
3697
3698 case ')':
3699 if (syntax & RE_NO_BK_PARENS)
3700 goto handle_close;
3701 else
3702 goto normal_char;
3703
3704
3705 case '\n':
3706 if (syntax & RE_NEWLINE_ALT)
3707 goto handle_alt;
3708 else
3709 goto normal_char;
3710
3711
3712 case '|':
3713 if (syntax & RE_NO_BK_VBAR)
3714 goto handle_alt;
3715 else
3716 goto normal_char;
3717
3718
3719 case '{':
3720 if (syntax & RE_INTERVALS && syntax & RE_NO_BK_BRACES)
3721 goto handle_interval;
3722 else
3723 goto normal_char;
3724
3725
3726 case '\\':
3727 if (p == pend) FREE_STACK_RETURN (REG_EESCAPE);
3728
3729 /* Do not translate the character after the \, so that we can
3730 distinguish, e.g., \B from \b, even if we normally would
3731 translate, e.g., B to b. */
3732 PATFETCH_RAW (c);
3733
3734 switch (c)
3735 {
3736 case '(':
3737 if (syntax & RE_NO_BK_PARENS)
3738 goto normal_backslash;
3739
3740 handle_open:
3741 bufp->re_nsub++;
3742 regnum++;
3743
3744 if (COMPILE_STACK_FULL)
3745 {
3746 RETALLOC (compile_stack.stack, compile_stack.size << 1,
3747 compile_stack_elt_t);
3748 if (compile_stack.stack == NULL) return REG_ESPACE;
3749
3750 compile_stack.size <<= 1;
3751 }
3752
3753 /* These are the values to restore when we hit end of this
3754 group. They are all relative offsets, so that if the
3755 whole pattern moves because of realloc, they will still
3756 be valid. */
3757 COMPILE_STACK_TOP.begalt_offset = begalt - COMPILED_BUFFER_VAR;
3758 COMPILE_STACK_TOP.fixup_alt_jump
3759 = fixup_alt_jump ? fixup_alt_jump - COMPILED_BUFFER_VAR + 1 : 0;
3760 COMPILE_STACK_TOP.laststart_offset = b - COMPILED_BUFFER_VAR;
3761 COMPILE_STACK_TOP.regnum = regnum;
3762
3763 /* We will eventually replace the 0 with the number of
3764 groups inner to this one. But do not push a
3765 start_memory for groups beyond the last one we can
3766 represent in the compiled pattern. */
3767 if (regnum <= MAX_REGNUM)
3768 {
3769 COMPILE_STACK_TOP.inner_group_offset = b
3770 - COMPILED_BUFFER_VAR + 2;
3771 BUF_PUSH_3 (start_memory, regnum, 0);
3772 }
3773
3774 compile_stack.avail++;
3775
3776 fixup_alt_jump = 0;
3777 laststart = 0;
3778 begalt = b;
3779 /* If we've reached MAX_REGNUM groups, then this open
3780 won't actually generate any code, so we'll have to
3781 clear pending_exact explicitly. */
3782 pending_exact = 0;
3783 break;
3784
3785
3786 case ')':
3787 if (syntax & RE_NO_BK_PARENS) goto normal_backslash;
3788
3789 if (COMPILE_STACK_EMPTY)
3790 {
3791 if (syntax & RE_UNMATCHED_RIGHT_PAREN_ORD)
3792 goto normal_backslash;
3793 else
3794 FREE_STACK_RETURN (REG_ERPAREN);
3795 }
3796
3797 handle_close:
3798 if (fixup_alt_jump)
3799 { /* Push a dummy failure point at the end of the
3800 alternative for a possible future
3801 `pop_failure_jump' to pop. See comments at
3802 `push_dummy_failure' in `re_match_2'. */
3803 BUF_PUSH (push_dummy_failure);
3804
3805 /* We allocated space for this jump when we assigned
3806 to `fixup_alt_jump', in the `handle_alt' case below. */
3807 STORE_JUMP (jump_past_alt, fixup_alt_jump, b - 1);
3808 }
3809
3810 /* See similar code for backslashed left paren above. */
3811 if (COMPILE_STACK_EMPTY)
3812 {
3813 if (syntax & RE_UNMATCHED_RIGHT_PAREN_ORD)
3814 goto normal_char;
3815 else
3816 FREE_STACK_RETURN (REG_ERPAREN);
3817 }
3818
3819 /* Since we just checked for an empty stack above, this
3820 ``can't happen''. */
3821 assert (compile_stack.avail != 0);
3822 {
3823 /* We don't just want to restore into `regnum', because
3824 later groups should continue to be numbered higher,
3825 as in `(ab)c(de)' -- the second group is #2. */
3826 regnum_t this_group_regnum;
3827
3828 compile_stack.avail--;
3829 begalt = COMPILED_BUFFER_VAR + COMPILE_STACK_TOP.begalt_offset;
3830 fixup_alt_jump
3831 = COMPILE_STACK_TOP.fixup_alt_jump
3832 ? COMPILED_BUFFER_VAR + COMPILE_STACK_TOP.fixup_alt_jump - 1
3833 : 0;
3834 laststart = COMPILED_BUFFER_VAR + COMPILE_STACK_TOP.laststart_offset;
3835 this_group_regnum = COMPILE_STACK_TOP.regnum;
3836 /* If we've reached MAX_REGNUM groups, then this open
3837 won't actually generate any code, so we'll have to
3838 clear pending_exact explicitly. */
3839 pending_exact = 0;
3840
3841 /* We're at the end of the group, so now we know how many
3842 groups were inside this one. */
3843 if (this_group_regnum <= MAX_REGNUM)
3844 {
3845 UCHAR_T *inner_group_loc
3846 = COMPILED_BUFFER_VAR + COMPILE_STACK_TOP.inner_group_offset;
3847
3848 *inner_group_loc = regnum - this_group_regnum;
3849 BUF_PUSH_3 (stop_memory, this_group_regnum,
3850 regnum - this_group_regnum);
3851 }
3852 }
3853 break;
3854
3855
3856 case '|': /* `\|'. */
3857 if (syntax & RE_LIMITED_OPS || syntax & RE_NO_BK_VBAR)
3858 goto normal_backslash;
3859 handle_alt:
3860 if (syntax & RE_LIMITED_OPS)
3861 goto normal_char;
3862
3863 /* Insert before the previous alternative a jump which
3864 jumps to this alternative if the former fails. */
3865 GET_BUFFER_SPACE (1 + OFFSET_ADDRESS_SIZE);
3866 INSERT_JUMP (on_failure_jump, begalt,
3867 b + 2 + 2 * OFFSET_ADDRESS_SIZE);
3868 pending_exact = 0;
3869 b += 1 + OFFSET_ADDRESS_SIZE;
3870
3871 /* The alternative before this one has a jump after it
3872 which gets executed if it gets matched. Adjust that
3873 jump so it will jump to this alternative's analogous
3874 jump (put in below, which in turn will jump to the next
3875 (if any) alternative's such jump, etc.). The last such
3876 jump jumps to the correct final destination. A picture:
3877 _____ _____
3878 | | | |
3879 | v | v
3880 a | b | c
3881
3882 If we are at `b', then fixup_alt_jump right now points to a
3883 three-byte space after `a'. We'll put in the jump, set
3884 fixup_alt_jump to right after `b', and leave behind three
3885 bytes which we'll fill in when we get to after `c'. */
3886
3887 if (fixup_alt_jump)
3888 STORE_JUMP (jump_past_alt, fixup_alt_jump, b);
3889
3890 /* Mark and leave space for a jump after this alternative,
3891 to be filled in later either by next alternative or
3892 when know we're at the end of a series of alternatives. */
3893 fixup_alt_jump = b;
3894 GET_BUFFER_SPACE (1 + OFFSET_ADDRESS_SIZE);
3895 b += 1 + OFFSET_ADDRESS_SIZE;
3896
3897 laststart = 0;
3898 begalt = b;
3899 break;
3900
3901
3902 case '{':
3903 /* If \{ is a literal. */
3904 if (!(syntax & RE_INTERVALS)
3905 /* If we're at `\{' and it's not the open-interval
3906 operator. */
3907 || (syntax & RE_NO_BK_BRACES))
3908 goto normal_backslash;
3909
3910 handle_interval:
3911 {
3912 /* If got here, then the syntax allows intervals. */
3913
3914 /* At least (most) this many matches must be made. */
3915 int lower_bound = -1, upper_bound = -1;
3916
3917 /* Place in the uncompiled pattern (i.e., just after
3918 the '{') to go back to if the interval is invalid. */
3919 const CHAR_T *beg_interval = p;
3920
3921 if (p == pend)
3922 goto invalid_interval;
3923
3924 GET_UNSIGNED_NUMBER (lower_bound);
3925
3926 if (c == ',')
3927 {
3928 GET_UNSIGNED_NUMBER (upper_bound);
3929 if (upper_bound < 0)
3930 upper_bound = RE_DUP_MAX;
3931 }
3932 else
3933 /* Interval such as `{1}' => match exactly once. */
3934 upper_bound = lower_bound;
3935
3936 if (! (0 <= lower_bound && lower_bound <= upper_bound))
3937 goto invalid_interval;
3938
3939 if (!(syntax & RE_NO_BK_BRACES))
3940 {
3941 if (c != '\\' || p == pend)
3942 goto invalid_interval;
3943 PATFETCH (c);
3944 }
3945
3946 if (c != '}')
3947 goto invalid_interval;
3948
3949 /* If it's invalid to have no preceding re. */
3950 if (!laststart)
3951 {
3952 if (syntax & RE_CONTEXT_INVALID_OPS
3953 && !(syntax & RE_INVALID_INTERVAL_ORD))
3954 FREE_STACK_RETURN (REG_BADRPT);
3955 else if (syntax & RE_CONTEXT_INDEP_OPS)
3956 laststart = b;
3957 else
3958 goto unfetch_interval;
3959 }
3960
3961 /* We just parsed a valid interval. */
3962
3963 if (RE_DUP_MAX < upper_bound)
3964 FREE_STACK_RETURN (REG_BADBR);
3965
3966 /* If the upper bound is zero, don't want to succeed at
3967 all; jump from `laststart' to `b + 3', which will be
3968 the end of the buffer after we insert the jump. */
3969 /* ifdef WCHAR, 'b + 1 + OFFSET_ADDRESS_SIZE'
3970 instead of 'b + 3'. */
3971 if (upper_bound == 0)
3972 {
3973 GET_BUFFER_SPACE (1 + OFFSET_ADDRESS_SIZE);
3974 INSERT_JUMP (jump, laststart, b + 1
3975 + OFFSET_ADDRESS_SIZE);
3976 b += 1 + OFFSET_ADDRESS_SIZE;
3977 }
3978
3979 /* Otherwise, we have a nontrivial interval. When
3980 we're all done, the pattern will look like:
3981 set_number_at <jump count> <upper bound>
3982 set_number_at <succeed_n count> <lower bound>
3983 succeed_n <after jump addr> <succeed_n count>
3984 <body of loop>
3985 jump_n <succeed_n addr> <jump count>
3986 (The upper bound and `jump_n' are omitted if
3987 `upper_bound' is 1, though.) */
3988 else
3989 { /* If the upper bound is > 1, we need to insert
3990 more at the end of the loop. */
3991 unsigned nbytes = 2 + 4 * OFFSET_ADDRESS_SIZE +
3992 (upper_bound > 1) * (2 + 4 * OFFSET_ADDRESS_SIZE);
3993
3994 GET_BUFFER_SPACE (nbytes);
3995
3996 /* Initialize lower bound of the `succeed_n', even
3997 though it will be set during matching by its
3998 attendant `set_number_at' (inserted next),
3999 because `re_compile_fastmap' needs to know.
4000 Jump to the `jump_n' we might insert below. */
4001 INSERT_JUMP2 (succeed_n, laststart,
4002 b + 1 + 2 * OFFSET_ADDRESS_SIZE
4003 + (upper_bound > 1) * (1 + 2 * OFFSET_ADDRESS_SIZE)
4004 , lower_bound);
4005 b += 1 + 2 * OFFSET_ADDRESS_SIZE;
4006
4007 /* Code to initialize the lower bound. Insert
4008 before the `succeed_n'. The `5' is the last two
4009 bytes of this `set_number_at', plus 3 bytes of
4010 the following `succeed_n'. */
4011 /* ifdef WCHAR, The '1+2*OFFSET_ADDRESS_SIZE'
4012 is the 'set_number_at', plus '1+OFFSET_ADDRESS_SIZE'
4013 of the following `succeed_n'. */
4014 PREFIX(insert_op2) (set_number_at, laststart, 1
4015 + 2 * OFFSET_ADDRESS_SIZE, lower_bound, b);
4016 b += 1 + 2 * OFFSET_ADDRESS_SIZE;
4017
4018 if (upper_bound > 1)
4019 { /* More than one repetition is allowed, so
4020 append a backward jump to the `succeed_n'
4021 that starts this interval.
4022
4023 When we've reached this during matching,
4024 we'll have matched the interval once, so
4025 jump back only `upper_bound - 1' times. */
4026 STORE_JUMP2 (jump_n, b, laststart
4027 + 2 * OFFSET_ADDRESS_SIZE + 1,
4028 upper_bound - 1);
4029 b += 1 + 2 * OFFSET_ADDRESS_SIZE;
4030
4031 /* The location we want to set is the second
4032 parameter of the `jump_n'; that is `b-2' as
4033 an absolute address. `laststart' will be
4034 the `set_number_at' we're about to insert;
4035 `laststart+3' the number to set, the source
4036 for the relative address. But we are
4037 inserting into the middle of the pattern --
4038 so everything is getting moved up by 5.
4039 Conclusion: (b - 2) - (laststart + 3) + 5,
4040 i.e., b - laststart.
4041
4042 We insert this at the beginning of the loop
4043 so that if we fail during matching, we'll
4044 reinitialize the bounds. */
4045 PREFIX(insert_op2) (set_number_at, laststart,
4046 b - laststart,
4047 upper_bound - 1, b);
4048 b += 1 + 2 * OFFSET_ADDRESS_SIZE;
4049 }
4050 }
4051 pending_exact = 0;
4052 break;
4053
4054 invalid_interval:
4055 if (!(syntax & RE_INVALID_INTERVAL_ORD))
4056 FREE_STACK_RETURN (p == pend ? REG_EBRACE : REG_BADBR);
4057 unfetch_interval:
4058 /* Match the characters as literals. */
4059 p = beg_interval;
4060 c = '{';
4061 if (syntax & RE_NO_BK_BRACES)
4062 goto normal_char;
4063 else
4064 goto normal_backslash;
4065 }
4066
4067#ifdef emacs
4068 /* There is no way to specify the before_dot and after_dot
4069 operators. rms says this is ok. --karl */
4070 case '=':
4071 BUF_PUSH (at_dot);
4072 break;
4073
4074 case 's':
4075 laststart = b;
4076 PATFETCH (c);
4077 BUF_PUSH_2 (syntaxspec, syntax_spec_code[c]);
4078 break;
4079
4080 case 'S':
4081 laststart = b;
4082 PATFETCH (c);
4083 BUF_PUSH_2 (notsyntaxspec, syntax_spec_code[c]);
4084 break;
4085#endif /* emacs */
4086
4087
4088 case 'w':
4089 if (syntax & RE_NO_GNU_OPS)
4090 goto normal_char;
4091 laststart = b;
4092 BUF_PUSH (wordchar);
4093 break;
4094
4095
4096 case 'W':
4097 if (syntax & RE_NO_GNU_OPS)
4098 goto normal_char;
4099 laststart = b;
4100 BUF_PUSH (notwordchar);
4101 break;
4102
4103
4104 case '<':
4105 if (syntax & RE_NO_GNU_OPS)
4106 goto normal_char;
4107 BUF_PUSH (wordbeg);
4108 break;
4109
4110 case '>':
4111 if (syntax & RE_NO_GNU_OPS)
4112 goto normal_char;
4113 BUF_PUSH (wordend);
4114 break;
4115
4116 case 'b':
4117 if (syntax & RE_NO_GNU_OPS)
4118 goto normal_char;
4119 BUF_PUSH (wordbound);
4120 break;
4121
4122 case 'B':
4123 if (syntax & RE_NO_GNU_OPS)
4124 goto normal_char;
4125 BUF_PUSH (notwordbound);
4126 break;
4127
4128 case '`':
4129 if (syntax & RE_NO_GNU_OPS)
4130 goto normal_char;
4131 BUF_PUSH (begbuf);
4132 break;
4133
4134 case '\'':
4135 if (syntax & RE_NO_GNU_OPS)
4136 goto normal_char;
4137 BUF_PUSH (endbuf);
4138 break;
4139
4140 case '1': case '2': case '3': case '4': case '5':
4141 case '6': case '7': case '8': case '9':
4142 if (syntax & RE_NO_BK_REFS)
4143 goto normal_char;
4144
4145 c1 = c - '0';
4146
4147 if (c1 > regnum)
4148 FREE_STACK_RETURN (REG_ESUBREG);
4149
4150 /* Can't back reference to a subexpression if inside of it. */
4151 if (group_in_compile_stack (compile_stack, (regnum_t) c1))
4152 goto normal_char;
4153
4154 laststart = b;
4155 BUF_PUSH_2 (duplicate, c1);
4156 break;
4157
4158
4159 case '+':
4160 case '?':
4161 if (syntax & RE_BK_PLUS_QM)
4162 goto handle_plus;
4163 else
4164 goto normal_backslash;
4165
4166 default:
4167 normal_backslash:
4168 /* You might think it would be useful for \ to mean
4169 not to translate; but if we don't translate it
4170 it will never match anything. */
4171 c = TRANSLATE (c);
4172 goto normal_char;
4173 }
4174 break;
4175
4176
4177 default:
4178 /* Expects the character in `c'. */
4179 normal_char:
4180 /* If no exactn currently being built. */
4181 if (!pending_exact
4182#ifdef WCHAR
4183 /* If last exactn handle binary(or character) and
4184 new exactn handle character(or binary). */
4185 || is_exactn_bin != is_binary[p - 1 - pattern]
4186#endif /* WCHAR */
4187
4188 /* If last exactn not at current position. */
4189 || pending_exact + *pending_exact + 1 != b
4190
4191 /* We have only one byte following the exactn for the count. */
4192 || *pending_exact == (1 << BYTEWIDTH) - 1
4193
4194 /* If followed by a repetition operator. */
4195 || *p == '*' || *p == '^'
4196 || ((syntax & RE_BK_PLUS_QM)
4197 ? *p == '\\' && (p[1] == '+' || p[1] == '?')
4198 : (*p == '+' || *p == '?'))
4199 || ((syntax & RE_INTERVALS)
4200 && ((syntax & RE_NO_BK_BRACES)
4201 ? *p == '{'
4202 : (p[0] == '\\' && p[1] == '{'))))
4203 {
4204 /* Start building a new exactn. */
4205
4206 laststart = b;
4207
4208#ifdef WCHAR
4209 /* Is this exactn binary data or character? */
4210 is_exactn_bin = is_binary[p - 1 - pattern];
4211 if (is_exactn_bin)
4212 BUF_PUSH_2 (exactn_bin, 0);
4213 else
4214 BUF_PUSH_2 (exactn, 0);
4215#else
4216 BUF_PUSH_2 (exactn, 0);
4217#endif /* WCHAR */
4218 pending_exact = b - 1;
4219 }
4220
4221 BUF_PUSH (c);
4222 (*pending_exact)++;
4223 break;
4224 } /* switch (c) */
4225 } /* while p != pend */
4226
4227
4228 /* Through the pattern now. */
4229
4230 if (fixup_alt_jump)
4231 STORE_JUMP (jump_past_alt, fixup_alt_jump, b);
4232
4233 if (!COMPILE_STACK_EMPTY)
4234 FREE_STACK_RETURN (REG_EPAREN);
4235
4236 /* If we don't want backtracking, force success
4237 the first time we reach the end of the compiled pattern. */
4238 if (syntax & RE_NO_POSIX_BACKTRACKING)
4239 BUF_PUSH (succeed);
4240
4241#ifdef WCHAR
4242 free (pattern);
4243 free (mbs_offset);
4244 free (is_binary);
4245#endif
4246 free (compile_stack.stack);
4247
4248 /* We have succeeded; set the length of the buffer. */
4249#ifdef WCHAR
4250 bufp->used = (uintptr_t) b - (uintptr_t) COMPILED_BUFFER_VAR;
4251#else
4252 bufp->used = b - bufp->buffer;
4253#endif
4254
4255#ifdef DEBUG
4256 if (debug)
4257 {
4258 DEBUG_PRINT1 ("\nCompiled pattern: \n");
4259 PREFIX(print_compiled_pattern) (bufp);
4260 }
4261#endif /* DEBUG */
4262
4263#ifndef MATCH_MAY_ALLOCATE
4264 /* Initialize the failure stack to the largest possible stack. This
4265 isn't necessary unless we're trying to avoid calling alloca in
4266 the search and match routines. */
4267 {
4268 int num_regs = bufp->re_nsub + 1;
4269
4270 /* Since DOUBLE_FAIL_STACK refuses to double only if the current size
4271 is strictly greater than re_max_failures, the largest possible stack
4272 is 2 * re_max_failures failure points. */
4273 if (fail_stack.size < (2 * re_max_failures * MAX_FAILURE_ITEMS))
4274 {
4275 fail_stack.size = (2 * re_max_failures * MAX_FAILURE_ITEMS);
4276
4277# ifdef emacs
4278 if (! fail_stack.stack)
4279 fail_stack.stack
4280 = (PREFIX(fail_stack_elt_t) *) xmalloc (fail_stack.size
4281 * sizeof (PREFIX(fail_stack_elt_t)));
4282 else
4283 fail_stack.stack
4284 = (PREFIX(fail_stack_elt_t) *) xrealloc (fail_stack.stack,
4285 (fail_stack.size
4286 * sizeof (PREFIX(fail_stack_elt_t))));
4287# else /* not emacs */
4288 if (! fail_stack.stack)
4289 fail_stack.stack
4290 = (PREFIX(fail_stack_elt_t) *) malloc (fail_stack.size
4291 * sizeof (PREFIX(fail_stack_elt_t)));
4292 else
4293 fail_stack.stack
4294 = (PREFIX(fail_stack_elt_t) *) realloc (fail_stack.stack,
4295 (fail_stack.size
4296 * sizeof (PREFIX(fail_stack_elt_t))));
4297# endif /* not emacs */
4298 }
4299
4300 PREFIX(regex_grow_registers) (num_regs);
4301 }
4302#endif /* not MATCH_MAY_ALLOCATE */
4303
4304 return REG_NOERROR;
4305} /* regex_compile */
4306
4307/* Subroutines for `regex_compile'. */
4308
4309/* Store OP at LOC followed by two-byte integer parameter ARG. */
4310/* ifdef WCHAR, integer parameter is 1 wchar_t. */
4311
4312static void
4313PREFIX(store_op1) (op, loc, arg)
4314 re_opcode_t op;
4315 UCHAR_T *loc;
4316 int arg;
4317{
4318 *loc = (UCHAR_T) op;
4319 STORE_NUMBER (loc + 1, arg);
4320}
4321
4322
4323/* Like `store_op1', but for two two-byte parameters ARG1 and ARG2. */
4324/* ifdef WCHAR, integer parameter is 1 wchar_t. */
4325
4326static void
4327PREFIX(store_op2) (op, loc, arg1, arg2)
4328 re_opcode_t op;
4329 UCHAR_T *loc;
4330 int arg1, arg2;
4331{
4332 *loc = (UCHAR_T) op;
4333 STORE_NUMBER (loc + 1, arg1);
4334 STORE_NUMBER (loc + 1 + OFFSET_ADDRESS_SIZE, arg2);
4335}
4336
4337
4338/* Copy the bytes from LOC to END to open up three bytes of space at LOC
4339 for OP followed by two-byte integer parameter ARG. */
4340/* ifdef WCHAR, integer parameter is 1 wchar_t. */
4341
4342static void
4343PREFIX(insert_op1) (op, loc, arg, end)
4344 re_opcode_t op;
4345 UCHAR_T *loc;
4346 int arg;
4347 UCHAR_T *end;
4348{
4349 register UCHAR_T *pfrom = end;
4350 register UCHAR_T *pto = end + 1 + OFFSET_ADDRESS_SIZE;
4351
4352 while (pfrom != loc)
4353 *--pto = *--pfrom;
4354
4355 PREFIX(store_op1) (op, loc, arg);
4356}
4357
4358
4359/* Like `insert_op1', but for two two-byte parameters ARG1 and ARG2. */
4360/* ifdef WCHAR, integer parameter is 1 wchar_t. */
4361
4362static void
4363PREFIX(insert_op2) (op, loc, arg1, arg2, end)
4364 re_opcode_t op;
4365 UCHAR_T *loc;
4366 int arg1, arg2;
4367 UCHAR_T *end;
4368{
4369 register UCHAR_T *pfrom = end;
4370 register UCHAR_T *pto = end + 1 + 2 * OFFSET_ADDRESS_SIZE;
4371
4372 while (pfrom != loc)
4373 *--pto = *--pfrom;
4374
4375 PREFIX(store_op2) (op, loc, arg1, arg2);
4376}
4377
4378
4379/* P points to just after a ^ in PATTERN. Return true if that ^ comes
4380 after an alternative or a begin-subexpression. We assume there is at
4381 least one character before the ^. */
4382
4383static boolean
4384PREFIX(at_begline_loc_p) (pattern, p, syntax)
4385 const CHAR_T *pattern, *p;
4386 reg_syntax_t syntax;
4387{
4388 const CHAR_T *prev = p - 2;
4389 boolean prev_prev_backslash = prev > pattern && prev[-1] == '\\';
4390
4391 return
4392 /* After a subexpression? */
4393 (*prev == '(' && (syntax & RE_NO_BK_PARENS || prev_prev_backslash))
4394 /* After an alternative? */
4395 || (*prev == '|' && (syntax & RE_NO_BK_VBAR || prev_prev_backslash));
4396}
4397
4398
4399/* The dual of at_begline_loc_p. This one is for $. We assume there is
4400 at least one character after the $, i.e., `P < PEND'. */
4401
4402static boolean
4403PREFIX(at_endline_loc_p) (p, pend, syntax)
4404 const CHAR_T *p, *pend;
4405 reg_syntax_t syntax;
4406{
4407 const CHAR_T *next = p;
4408 boolean next_backslash = *next == '\\';
4409 const CHAR_T *next_next = p + 1 < pend ? p + 1 : 0;
4410
4411 return
4412 /* Before a subexpression? */
4413 (syntax & RE_NO_BK_PARENS ? *next == ')'
4414 : next_backslash && next_next && *next_next == ')')
4415 /* Before an alternative? */
4416 || (syntax & RE_NO_BK_VBAR ? *next == '|'
4417 : next_backslash && next_next && *next_next == '|');
4418}
4419
4420#else /* not INSIDE_RECURSION */
4421
4422/* Returns true if REGNUM is in one of COMPILE_STACK's elements and
4423 false if it's not. */
4424
4425static boolean
4426group_in_compile_stack (compile_stack, regnum)
4427 compile_stack_type compile_stack;
4428 regnum_t regnum;
4429{
4430 int this_element;
4431
4432 for (this_element = compile_stack.avail - 1;
4433 this_element >= 0;
4434 this_element--)
4435 if (compile_stack.stack[this_element].regnum == regnum)
4436 return true;
4437
4438 return false;
4439}
4440#endif /* not INSIDE_RECURSION */
4441
4442#ifdef INSIDE_RECURSION
4443
4444#ifdef WCHAR
4445/* This insert space, which size is "num", into the pattern at "loc".
4446 "end" must point the end of the allocated buffer. */
4447static void
4448insert_space (num, loc, end)
4449 int num;
4450 CHAR_T *loc;
4451 CHAR_T *end;
4452{
4453 register CHAR_T *pto = end;
4454 register CHAR_T *pfrom = end - num;
4455
4456 while (pfrom >= loc)
4457 *pto-- = *pfrom--;
4458}
4459#endif /* WCHAR */
4460
4461#ifdef WCHAR
4462static reg_errcode_t
4463wcs_compile_range (range_start_char, p_ptr, pend, translate, syntax, b,
4464 char_set)
4465 CHAR_T range_start_char;
4466 const CHAR_T **p_ptr, *pend;
4467 CHAR_T *char_set, *b;
4468 RE_TRANSLATE_TYPE translate;
4469 reg_syntax_t syntax;
4470{
4471 const CHAR_T *p = *p_ptr;
4472 CHAR_T range_start, range_end;
4473 reg_errcode_t ret;
4474# ifdef _LIBC
4475 uint32_t nrules;
4476 uint32_t start_val, end_val;
4477# endif
4478 if (p == pend)
4479 return REG_ERANGE;
4480
4481# ifdef _LIBC
4482 nrules = _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_NRULES);
4483 if (nrules != 0)
4484 {
4485 const char *collseq = (const char *) _NL_CURRENT(LC_COLLATE,
4486 _NL_COLLATE_COLLSEQWC);
4487 const unsigned char *extra = (const unsigned char *)
4488 _NL_CURRENT (LC_COLLATE, _NL_COLLATE_SYMB_EXTRAMB);
4489
4490 if (range_start_char < -1)
4491 {
4492 /* range_start is a collating symbol. */
4493 int32_t *wextra;
4494 /* Retreive the index and get collation sequence value. */
4495 wextra = (int32_t*)(extra + char_set[-range_start_char]);
4496 start_val = wextra[1 + *wextra];
4497 }
4498 else
4499 start_val = collseq_table_lookup(collseq, TRANSLATE(range_start_char));
4500
4501 end_val = collseq_table_lookup (collseq, TRANSLATE (p[0]));
4502
4503 /* Report an error if the range is empty and the syntax prohibits
4504 this. */
4505 ret = ((syntax & RE_NO_EMPTY_RANGES)
4506 && (start_val > end_val))? REG_ERANGE : REG_NOERROR;
4507
4508 /* Insert space to the end of the char_ranges. */
4509 insert_space(2, b - char_set[5] - 2, b - 1);
4510 *(b - char_set[5] - 2) = (wchar_t)start_val;
4511 *(b - char_set[5] - 1) = (wchar_t)end_val;
4512 char_set[4]++; /* ranges_index */
4513 }
4514 else
4515# endif
4516 {
4517 range_start = (range_start_char >= 0)? TRANSLATE (range_start_char):
4518 range_start_char;
4519 range_end = TRANSLATE (p[0]);
4520 /* Report an error if the range is empty and the syntax prohibits
4521 this. */
4522 ret = ((syntax & RE_NO_EMPTY_RANGES)
4523 && (range_start > range_end))? REG_ERANGE : REG_NOERROR;
4524
4525 /* Insert space to the end of the char_ranges. */
4526 insert_space(2, b - char_set[5] - 2, b - 1);
4527 *(b - char_set[5] - 2) = range_start;
4528 *(b - char_set[5] - 1) = range_end;
4529 char_set[4]++; /* ranges_index */
4530 }
4531 /* Have to increment the pointer into the pattern string, so the
4532 caller isn't still at the ending character. */
4533 (*p_ptr)++;
4534
4535 return ret;
4536}
4537#else /* BYTE */
4538/* Read the ending character of a range (in a bracket expression) from the
4539 uncompiled pattern *P_PTR (which ends at PEND). We assume the
4540 starting character is in `P[-2]'. (`P[-1]' is the character `-'.)
4541 Then we set the translation of all bits between the starting and
4542 ending characters (inclusive) in the compiled pattern B.
4543
4544 Return an error code.
4545
4546 We use these short variable names so we can use the same macros as
4547 `regex_compile' itself. */
4548
4549static reg_errcode_t
4550byte_compile_range (range_start_char, p_ptr, pend, translate, syntax, b)
4551 unsigned int range_start_char;
4552 const char **p_ptr, *pend;
4553 RE_TRANSLATE_TYPE translate;
4554 reg_syntax_t syntax;
4555 unsigned char *b;
4556{
4557 unsigned this_char;
4558 const char *p = *p_ptr;
4559 reg_errcode_t ret;
4560# if _LIBC
4561 const unsigned char *collseq;
4562 unsigned int start_colseq;
4563 unsigned int end_colseq;
4564# else
4565 unsigned end_char;
4566# endif
4567
4568 if (p == pend)
4569 return REG_ERANGE;
4570
4571 /* Have to increment the pointer into the pattern string, so the
4572 caller isn't still at the ending character. */
4573 (*p_ptr)++;
4574
4575 /* Report an error if the range is empty and the syntax prohibits this. */
4576 ret = syntax & RE_NO_EMPTY_RANGES ? REG_ERANGE : REG_NOERROR;
4577
4578# if _LIBC
4579 collseq = (const unsigned char *) _NL_CURRENT (LC_COLLATE,
4580 _NL_COLLATE_COLLSEQMB);
4581
4582 start_colseq = collseq[(unsigned char) TRANSLATE (range_start_char)];
4583 end_colseq = collseq[(unsigned char) TRANSLATE (p[0])];
4584 for (this_char = 0; this_char <= (unsigned char) -1; ++this_char)
4585 {
4586 unsigned int this_colseq = collseq[(unsigned char) TRANSLATE (this_char)];
4587
4588 if (start_colseq <= this_colseq && this_colseq <= end_colseq)
4589 {
4590 SET_LIST_BIT (TRANSLATE (this_char));
4591 ret = REG_NOERROR;
4592 }
4593 }
4594# else
4595 /* Here we see why `this_char' has to be larger than an `unsigned
4596 char' -- we would otherwise go into an infinite loop, since all
4597 characters <= 0xff. */
4598 range_start_char = TRANSLATE (range_start_char);
4599 /* TRANSLATE(p[0]) is casted to char (not unsigned char) in TRANSLATE,
4600 and some compilers cast it to int implicitly, so following for_loop
4601 may fall to (almost) infinite loop.
4602 e.g. If translate[p[0]] = 0xff, end_char may equals to 0xffffffff.
4603 To avoid this, we cast p[0] to unsigned int and truncate it. */
4604 end_char = ((unsigned)TRANSLATE(p[0]) & ((1 << BYTEWIDTH) - 1));
4605
4606 for (this_char = range_start_char; this_char <= end_char; ++this_char)
4607 {
4608 SET_LIST_BIT (TRANSLATE (this_char));
4609 ret = REG_NOERROR;
4610 }
4611# endif
4612
4613 return ret;
4614}
4615#endif /* WCHAR */
4616
4617
4618/* re_compile_fastmap computes a ``fastmap'' for the compiled pattern in
4619 BUFP. A fastmap records which of the (1 << BYTEWIDTH) possible
4620 characters can start a string that matches the pattern. This fastmap
4621 is used by re_search to skip quickly over impossible starting points.
4622
4623 The caller must supply the address of a (1 << BYTEWIDTH)-byte data
4624 area as BUFP->fastmap.
4625
4626 We set the `fastmap', `fastmap_accurate', and `can_be_null' fields in
4627 the pattern buffer.
4628
4629 Returns 0 if we succeed, -2 if an internal error. */
4630
4631#ifdef WCHAR
4632/* local function for re_compile_fastmap.
4633 truncate wchar_t character to char. */
4634static unsigned char truncate_wchar (CHAR_T c);
4635
4636static unsigned char
4637truncate_wchar (c)
4638 CHAR_T c;
4639{
4640 unsigned char buf[MB_CUR_MAX];
4641 mbstate_t state;
4642 int retval;
4643 memset (&state, '\0', sizeof (state));
4644 retval = wcrtomb (buf, c, &state);
4645 return retval > 0 ? buf[0] : (unsigned char) c;
4646}
4647#endif /* WCHAR */
4648
4649static int
4650PREFIX(re_compile_fastmap) (bufp)
4651 struct re_pattern_buffer *bufp;
4652{
4653 int j, k;
4654#ifdef MATCH_MAY_ALLOCATE
4655 PREFIX(fail_stack_type) fail_stack;
4656#endif
4657#ifndef REGEX_MALLOC
4658 char *destination;
4659#endif
4660
4661 register char *fastmap = bufp->fastmap;
4662
4663#ifdef WCHAR
4664 /* We need to cast pattern to (wchar_t*), because we casted this compiled
4665 pattern to (char*) in regex_compile. */
4666 UCHAR_T *pattern = (UCHAR_T*)bufp->buffer;
4667 register UCHAR_T *pend = (UCHAR_T*) (bufp->buffer + bufp->used);
4668#else /* BYTE */
4669 UCHAR_T *pattern = bufp->buffer;
4670 register UCHAR_T *pend = pattern + bufp->used;
4671#endif /* WCHAR */
4672 UCHAR_T *p = pattern;
4673
4674#ifdef REL_ALLOC
4675 /* This holds the pointer to the failure stack, when
4676 it is allocated relocatably. */
4677 fail_stack_elt_t *failure_stack_ptr;
4678#endif
4679
4680 /* Assume that each path through the pattern can be null until
4681 proven otherwise. We set this false at the bottom of switch
4682 statement, to which we get only if a particular path doesn't
4683 match the empty string. */
4684 boolean path_can_be_null = true;
4685
4686 /* We aren't doing a `succeed_n' to begin with. */
4687 boolean succeed_n_p = false;
4688
4689 assert (fastmap != NULL && p != NULL);
4690
4691 INIT_FAIL_STACK ();
4692 bzero (fastmap, 1 << BYTEWIDTH); /* Assume nothing's valid. */
4693 bufp->fastmap_accurate = 1; /* It will be when we're done. */
4694 bufp->can_be_null = 0;
4695
4696 while (1)
4697 {
4698 if (p == pend || *p == succeed)
4699 {
4700 /* We have reached the (effective) end of pattern. */
4701 if (!FAIL_STACK_EMPTY ())
4702 {
4703 bufp->can_be_null |= path_can_be_null;
4704
4705 /* Reset for next path. */
4706 path_can_be_null = true;
4707
4708 p = fail_stack.stack[--fail_stack.avail].pointer;
4709
4710 continue;
4711 }
4712 else
4713 break;
4714 }
4715
4716 /* We should never be about to go beyond the end of the pattern. */
4717 assert (p < pend);
4718
4719 switch (SWITCH_ENUM_CAST ((re_opcode_t) *p++))
4720 {
4721
4722 /* I guess the idea here is to simply not bother with a fastmap
4723 if a backreference is used, since it's too hard to figure out
4724 the fastmap for the corresponding group. Setting
4725 `can_be_null' stops `re_search_2' from using the fastmap, so
4726 that is all we do. */
4727 case duplicate:
4728 bufp->can_be_null = 1;
4729 goto done;
4730
4731
4732 /* Following are the cases which match a character. These end
4733 with `break'. */
4734
4735#ifdef WCHAR
4736 case exactn:
4737 fastmap[truncate_wchar(p[1])] = 1;
4738 break;
4739#else /* BYTE */
4740 case exactn:
4741 fastmap[p[1]] = 1;
4742 break;
4743#endif /* WCHAR */
4744#ifdef MBS_SUPPORT
4745 case exactn_bin:
4746 fastmap[p[1]] = 1;
4747 break;
4748#endif
4749
4750#ifdef WCHAR
4751 /* It is hard to distinguish fastmap from (multi byte) characters
4752 which depends on current locale. */
4753 case charset:
4754 case charset_not:
4755 case wordchar:
4756 case notwordchar:
4757 bufp->can_be_null = 1;
4758 goto done;
4759#else /* BYTE */
4760 case charset:
4761 for (j = *p++ * BYTEWIDTH - 1; j >= 0; j--)
4762 if (p[j / BYTEWIDTH] & (1 << (j % BYTEWIDTH)))
4763 fastmap[j] = 1;
4764 break;
4765
4766
4767 case charset_not:
4768 /* Chars beyond end of map must be allowed. */
4769 for (j = *p * BYTEWIDTH; j < (1 << BYTEWIDTH); j++)
4770 fastmap[j] = 1;
4771
4772 for (j = *p++ * BYTEWIDTH - 1; j >= 0; j--)
4773 if (!(p[j / BYTEWIDTH] & (1 << (j % BYTEWIDTH))))
4774 fastmap[j] = 1;
4775 break;
4776
4777
4778 case wordchar:
4779 for (j = 0; j < (1 << BYTEWIDTH); j++)
4780 if (SYNTAX (j) == Sword)
4781 fastmap[j] = 1;
4782 break;
4783
4784
4785 case notwordchar:
4786 for (j = 0; j < (1 << BYTEWIDTH); j++)
4787 if (SYNTAX (j) != Sword)
4788 fastmap[j] = 1;
4789 break;
4790#endif /* WCHAR */
4791
4792 case anychar:
4793 {
4794 int fastmap_newline = fastmap['\n'];
4795
4796 /* `.' matches anything ... */
4797 for (j = 0; j < (1 << BYTEWIDTH); j++)
4798 fastmap[j] = 1;
4799
4800 /* ... except perhaps newline. */
4801 if (!(bufp->syntax & RE_DOT_NEWLINE))
4802 fastmap['\n'] = fastmap_newline;
4803
4804 /* Return if we have already set `can_be_null'; if we have,
4805 then the fastmap is irrelevant. Something's wrong here. */
4806 else if (bufp->can_be_null)
4807 goto done;
4808
4809 /* Otherwise, have to check alternative paths. */
4810 break;
4811 }
4812
4813#ifdef emacs
4814 case syntaxspec:
4815 k = *p++;
4816 for (j = 0; j < (1 << BYTEWIDTH); j++)
4817 if (SYNTAX (j) == (enum syntaxcode) k)
4818 fastmap[j] = 1;
4819 break;
4820
4821
4822 case notsyntaxspec:
4823 k = *p++;
4824 for (j = 0; j < (1 << BYTEWIDTH); j++)
4825 if (SYNTAX (j) != (enum syntaxcode) k)
4826 fastmap[j] = 1;
4827 break;
4828
4829
4830 /* All cases after this match the empty string. These end with
4831 `continue'. */
4832
4833
4834 case before_dot:
4835 case at_dot:
4836 case after_dot:
4837 continue;
4838#endif /* emacs */
4839
4840
4841 case no_op:
4842 case begline:
4843 case endline:
4844 case begbuf:
4845 case endbuf:
4846 case wordbound:
4847 case notwordbound:
4848 case wordbeg:
4849 case wordend:
4850 case push_dummy_failure:
4851 continue;
4852
4853
4854 case jump_n:
4855 case pop_failure_jump:
4856 case maybe_pop_jump:
4857 case jump:
4858 case jump_past_alt:
4859 case dummy_failure_jump:
4860 EXTRACT_NUMBER_AND_INCR (j, p);
4861 p += j;
4862 if (j > 0)
4863 continue;
4864
4865 /* Jump backward implies we just went through the body of a
4866 loop and matched nothing. Opcode jumped to should be
4867 `on_failure_jump' or `succeed_n'. Just treat it like an
4868 ordinary jump. For a * loop, it has pushed its failure
4869 point already; if so, discard that as redundant. */
4870 if ((re_opcode_t) *p != on_failure_jump
4871 && (re_opcode_t) *p != succeed_n)
4872 continue;
4873
4874 p++;
4875 EXTRACT_NUMBER_AND_INCR (j, p);
4876 p += j;
4877
4878 /* If what's on the stack is where we are now, pop it. */
4879 if (!FAIL_STACK_EMPTY ()
4880 && fail_stack.stack[fail_stack.avail - 1].pointer == p)
4881 fail_stack.avail--;
4882
4883 continue;
4884
4885
4886 case on_failure_jump:
4887 case on_failure_keep_string_jump:
4888 handle_on_failure_jump:
4889 EXTRACT_NUMBER_AND_INCR (j, p);
4890
4891 /* For some patterns, e.g., `(a?)?', `p+j' here points to the
4892 end of the pattern. We don't want to push such a point,
4893 since when we restore it above, entering the switch will
4894 increment `p' past the end of the pattern. We don't need
4895 to push such a point since we obviously won't find any more
4896 fastmap entries beyond `pend'. Such a pattern can match
4897 the null string, though. */
4898 if (p + j < pend)
4899 {
4900 if (!PUSH_PATTERN_OP (p + j, fail_stack))
4901 {
4902 RESET_FAIL_STACK ();
4903 return -2;
4904 }
4905 }
4906 else
4907 bufp->can_be_null = 1;
4908
4909 if (succeed_n_p)
4910 {
4911 EXTRACT_NUMBER_AND_INCR (k, p); /* Skip the n. */
4912 succeed_n_p = false;
4913 }
4914
4915 continue;
4916
4917
4918 case succeed_n:
4919 /* Get to the number of times to succeed. */
4920 p += OFFSET_ADDRESS_SIZE;
4921
4922 /* Increment p past the n for when k != 0. */
4923 EXTRACT_NUMBER_AND_INCR (k, p);
4924 if (k == 0)
4925 {
4926 p -= 2 * OFFSET_ADDRESS_SIZE;
4927 succeed_n_p = true; /* Spaghetti code alert. */
4928 goto handle_on_failure_jump;
4929 }
4930 continue;
4931
4932
4933 case set_number_at:
4934 p += 2 * OFFSET_ADDRESS_SIZE;
4935 continue;
4936
4937
4938 case start_memory:
4939 case stop_memory:
4940 p += 2;
4941 continue;
4942
4943
4944 default:
4945 abort (); /* We have listed all the cases. */
4946 } /* switch *p++ */
4947
4948 /* Getting here means we have found the possible starting
4949 characters for one path of the pattern -- and that the empty
4950 string does not match. We need not follow this path further.
4951 Instead, look at the next alternative (remembered on the
4952 stack), or quit if no more. The test at the top of the loop
4953 does these things. */
4954 path_can_be_null = false;
4955 p = pend;
4956 } /* while p */
4957
4958 /* Set `can_be_null' for the last path (also the first path, if the
4959 pattern is empty). */
4960 bufp->can_be_null |= path_can_be_null;
4961
4962 done:
4963 RESET_FAIL_STACK ();
4964 return 0;
4965}
4966
4967#else /* not INSIDE_RECURSION */
4968
4969int
4970re_compile_fastmap (bufp)
4971 struct re_pattern_buffer *bufp;
4972{
4973# ifdef MBS_SUPPORT
4974 if (MB_CUR_MAX != 1)
4975 return wcs_re_compile_fastmap(bufp);
4976 else
4977# endif
4978 return byte_re_compile_fastmap(bufp);
4979} /* re_compile_fastmap */
4980#ifdef _LIBC
4981weak_alias (__re_compile_fastmap, re_compile_fastmap)
4982#endif
4983
4984
4985
4986/* Set REGS to hold NUM_REGS registers, storing them in STARTS and
4987 ENDS. Subsequent matches using PATTERN_BUFFER and REGS will use
4988 this memory for recording register information. STARTS and ENDS
4989 must be allocated using the malloc library routine, and must each
4990 be at least NUM_REGS * sizeof (regoff_t) bytes long.
4991
4992 If NUM_REGS == 0, then subsequent matches should allocate their own
4993 register data.
4994
4995 Unless this function is called, the first search or match using
4996 PATTERN_BUFFER will allocate its own register data, without
4997 freeing the old data. */
4998
4999void
5000re_set_registers (bufp, regs, num_regs, starts, ends)
5001 struct re_pattern_buffer *bufp;
5002 struct re_registers *regs;
5003 unsigned num_regs;
5004 regoff_t *starts, *ends;
5005{
5006 if (num_regs)
5007 {
5008 bufp->regs_allocated = REGS_REALLOCATE;
5009 regs->num_regs = num_regs;
5010 regs->start = starts;
5011 regs->end = ends;
5012 }
5013 else
5014 {
5015 bufp->regs_allocated = REGS_UNALLOCATED;
5016 regs->num_regs = 0;
5017 regs->start = regs->end = (regoff_t *) 0;
5018 }
5019}
5020#ifdef _LIBC
5021weak_alias (__re_set_registers, re_set_registers)
5022#endif
5023
5024
5025/* Searching routines. */
5026
5027/* Like re_search_2, below, but only one string is specified, and
5028 doesn't let you say where to stop matching. */
5029
5030int
5031re_search (bufp, string, size, startpos, range, regs)
5032 struct re_pattern_buffer *bufp;
5033 const char *string;
5034 int size, startpos, range;
5035 struct re_registers *regs;
5036{
5037 return re_search_2 (bufp, NULL, 0, string, size, startpos, range,
5038 regs, size);
5039}
5040#ifdef _LIBC
5041weak_alias (__re_search, re_search)
5042#endif
5043
5044
5045/* Using the compiled pattern in BUFP->buffer, first tries to match the
5046 virtual concatenation of STRING1 and STRING2, starting first at index
5047 STARTPOS, then at STARTPOS + 1, and so on.
5048
5049 STRING1 and STRING2 have length SIZE1 and SIZE2, respectively.
5050
5051 RANGE is how far to scan while trying to match. RANGE = 0 means try
5052 only at STARTPOS; in general, the last start tried is STARTPOS +
5053 RANGE.
5054
5055 In REGS, return the indices of the virtual concatenation of STRING1
5056 and STRING2 that matched the entire BUFP->buffer and its contained
5057 subexpressions.
5058
5059 Do not consider matching one past the index STOP in the virtual
5060 concatenation of STRING1 and STRING2.
5061
5062 We return either the position in the strings at which the match was
5063 found, -1 if no match, or -2 if error (such as failure
5064 stack overflow). */
5065
5066int
5067re_search_2 (bufp, string1, size1, string2, size2, startpos, range, regs, stop)
5068 struct re_pattern_buffer *bufp;
5069 const char *string1, *string2;
5070 int size1, size2;
5071 int startpos;
5072 int range;
5073 struct re_registers *regs;
5074 int stop;
5075{
5076# ifdef MBS_SUPPORT
5077 if (MB_CUR_MAX != 1)
5078 return wcs_re_search_2 (bufp, string1, size1, string2, size2, startpos,
5079 range, regs, stop);
5080 else
5081# endif
5082 return byte_re_search_2 (bufp, string1, size1, string2, size2, startpos,
5083 range, regs, stop);
5084} /* re_search_2 */
5085#ifdef _LIBC
5086weak_alias (__re_search_2, re_search_2)
5087#endif
5088
5089#endif /* not INSIDE_RECURSION */
5090
5091#ifdef INSIDE_RECURSION
5092
5093#ifdef MATCH_MAY_ALLOCATE
5094# define FREE_VAR(var) if (var) REGEX_FREE (var); var = NULL
5095#else
5096# define FREE_VAR(var) if (var) free (var); var = NULL
5097#endif
5098
5099#ifdef WCHAR
5100# define MAX_ALLOCA_SIZE 2000
5101
5102# define FREE_WCS_BUFFERS() \
5103 do { \
5104 if (size1 > MAX_ALLOCA_SIZE) \
5105 { \
5106 free (wcs_string1); \
5107 free (mbs_offset1); \
5108 } \
5109 else \
5110 { \
5111 FREE_VAR (wcs_string1); \
5112 FREE_VAR (mbs_offset1); \
5113 } \
5114 if (size2 > MAX_ALLOCA_SIZE) \
5115 { \
5116 free (wcs_string2); \
5117 free (mbs_offset2); \
5118 } \
5119 else \
5120 { \
5121 FREE_VAR (wcs_string2); \
5122 FREE_VAR (mbs_offset2); \
5123 } \
5124 } while (0)
5125
5126#endif
5127
5128
5129static int
5130PREFIX(re_search_2) (bufp, string1, size1, string2, size2, startpos, range,
5131 regs, stop)
5132 struct re_pattern_buffer *bufp;
5133 const char *string1, *string2;
5134 int size1, size2;
5135 int startpos;
5136 int range;
5137 struct re_registers *regs;
5138 int stop;
5139{
5140 int val;
5141 register char *fastmap = bufp->fastmap;
5142 register RE_TRANSLATE_TYPE translate = bufp->translate;
5143 int total_size = size1 + size2;
5144 int endpos = startpos + range;
5145#ifdef WCHAR
5146 /* We need wchar_t* buffers correspond to cstring1, cstring2. */
5147 wchar_t *wcs_string1 = NULL, *wcs_string2 = NULL;
5148 /* We need the size of wchar_t buffers correspond to csize1, csize2. */
5149 int wcs_size1 = 0, wcs_size2 = 0;
5150 /* offset buffer for optimizatoin. See convert_mbs_to_wc. */
5151 int *mbs_offset1 = NULL, *mbs_offset2 = NULL;
5152 /* They hold whether each wchar_t is binary data or not. */
5153 char *is_binary = NULL;
5154#endif /* WCHAR */
5155
5156 /* Check for out-of-range STARTPOS. */
5157 if (startpos < 0 || startpos > total_size)
5158 return -1;
5159
5160 /* Fix up RANGE if it might eventually take us outside
5161 the virtual concatenation of STRING1 and STRING2.
5162 Make sure we won't move STARTPOS below 0 or above TOTAL_SIZE. */
5163 if (endpos < 0)
5164 range = 0 - startpos;
5165 else if (endpos > total_size)
5166 range = total_size - startpos;
5167
5168 /* If the search isn't to be a backwards one, don't waste time in a
5169 search for a pattern that must be anchored. */
5170 if (bufp->used > 0 && range > 0
5171 && ((re_opcode_t) bufp->buffer[0] == begbuf
5172 /* `begline' is like `begbuf' if it cannot match at newlines. */
5173 || ((re_opcode_t) bufp->buffer[0] == begline
5174 && !bufp->newline_anchor)))
5175 {
5176 if (startpos > 0)
5177 return -1;
5178 else
5179 range = 1;
5180 }
5181
5182#ifdef emacs
5183 /* In a forward search for something that starts with \=.
5184 don't keep searching past point. */
5185 if (bufp->used > 0 && (re_opcode_t) bufp->buffer[0] == at_dot && range > 0)
5186 {
5187 range = PT - startpos;
5188 if (range <= 0)
5189 return -1;
5190 }
5191#endif /* emacs */
5192
5193 /* Update the fastmap now if not correct already. */
5194 if (fastmap && !bufp->fastmap_accurate)
5195 if (re_compile_fastmap (bufp) == -2)
5196 return -2;
5197
5198#ifdef WCHAR
5199 /* Allocate wchar_t array for wcs_string1 and wcs_string2 and
5200 fill them with converted string. */
5201 if (size1 != 0)
5202 {
5203 if (size1 > MAX_ALLOCA_SIZE)
5204 {
5205 wcs_string1 = TALLOC (size1 + 1, CHAR_T);
5206 mbs_offset1 = TALLOC (size1 + 1, int);
5207 is_binary = TALLOC (size1 + 1, char);
5208 }
5209 else
5210 {
5211 wcs_string1 = REGEX_TALLOC (size1 + 1, CHAR_T);
5212 mbs_offset1 = REGEX_TALLOC (size1 + 1, int);
5213 is_binary = REGEX_TALLOC (size1 + 1, char);
5214 }
5215 if (!wcs_string1 || !mbs_offset1 || !is_binary)
5216 {
5217 if (size1 > MAX_ALLOCA_SIZE)
5218 {
5219 free (wcs_string1);
5220 free (mbs_offset1);
5221 free (is_binary);
5222 }
5223 else
5224 {
5225 FREE_VAR (wcs_string1);
5226 FREE_VAR (mbs_offset1);
5227 FREE_VAR (is_binary);
5228 }
5229 return -2;
5230 }
5231 wcs_size1 = convert_mbs_to_wcs(wcs_string1, string1, size1,
5232 mbs_offset1, is_binary);
5233 wcs_string1[wcs_size1] = L'\0'; /* for a sentinel */
5234 if (size1 > MAX_ALLOCA_SIZE)
5235 free (is_binary);
5236 else
5237 FREE_VAR (is_binary);
5238 }
5239 if (size2 != 0)
5240 {
5241 if (size2 > MAX_ALLOCA_SIZE)
5242 {
5243 wcs_string2 = TALLOC (size2 + 1, CHAR_T);
5244 mbs_offset2 = TALLOC (size2 + 1, int);
5245 is_binary = TALLOC (size2 + 1, char);
5246 }
5247 else
5248 {
5249 wcs_string2 = REGEX_TALLOC (size2 + 1, CHAR_T);
5250 mbs_offset2 = REGEX_TALLOC (size2 + 1, int);
5251 is_binary = REGEX_TALLOC (size2 + 1, char);
5252 }
5253 if (!wcs_string2 || !mbs_offset2 || !is_binary)
5254 {
5255 FREE_WCS_BUFFERS ();
5256 if (size2 > MAX_ALLOCA_SIZE)
5257 free (is_binary);
5258 else
5259 FREE_VAR (is_binary);
5260 return -2;
5261 }
5262 wcs_size2 = convert_mbs_to_wcs(wcs_string2, string2, size2,
5263 mbs_offset2, is_binary);
5264 wcs_string2[wcs_size2] = L'\0'; /* for a sentinel */
5265 if (size2 > MAX_ALLOCA_SIZE)
5266 free (is_binary);
5267 else
5268 FREE_VAR (is_binary);
5269 }
5270#endif /* WCHAR */
5271
5272
5273 /* Loop through the string, looking for a place to start matching. */
5274 for (;;)
5275 {
5276 /* If a fastmap is supplied, skip quickly over characters that
5277 cannot be the start of a match. If the pattern can match the
5278 null string, however, we don't need to skip characters; we want
5279 the first null string. */
5280 if (fastmap && startpos < total_size && !bufp->can_be_null)
5281 {
5282 if (range > 0) /* Searching forwards. */
5283 {
5284 register const char *d;
5285 register int lim = 0;
5286 int irange = range;
5287
5288 if (startpos < size1 && startpos + range >= size1)
5289 lim = range - (size1 - startpos);
5290
5291 d = (startpos >= size1 ? string2 - size1 : string1) + startpos;
5292
5293 /* Written out as an if-else to avoid testing `translate'
5294 inside the loop. */
5295 if (translate)
5296 while (range > lim
5297 && !fastmap[(unsigned char)
5298 translate[(unsigned char) *d++]])
5299 range--;
5300 else
5301 while (range > lim && !fastmap[(unsigned char) *d++])
5302 range--;
5303
5304 startpos += irange - range;
5305 }
5306 else /* Searching backwards. */
5307 {
5308 register CHAR_T c = (size1 == 0 || startpos >= size1
5309 ? string2[startpos - size1]
5310 : string1[startpos]);
5311
5312 if (!fastmap[(unsigned char) TRANSLATE (c)])
5313 goto advance;
5314 }
5315 }
5316
5317 /* If can't match the null string, and that's all we have left, fail. */
5318 if (range >= 0 && startpos == total_size && fastmap
5319 && !bufp->can_be_null)
5320 {
5321#ifdef WCHAR
5322 FREE_WCS_BUFFERS ();
5323#endif
5324 return -1;
5325 }
5326
5327#ifdef WCHAR
5328 val = wcs_re_match_2_internal (bufp, string1, size1, string2,
5329 size2, startpos, regs, stop,
5330 wcs_string1, wcs_size1,
5331 wcs_string2, wcs_size2,
5332 mbs_offset1, mbs_offset2);
5333#else /* BYTE */
5334 val = byte_re_match_2_internal (bufp, string1, size1, string2,
5335 size2, startpos, regs, stop);
5336#endif /* BYTE */
5337
5338#ifndef REGEX_MALLOC
5339# ifdef C_ALLOCA
5340 alloca (0);
5341# endif
5342#endif
5343
5344 if (val >= 0)
5345 {
5346#ifdef WCHAR
5347 FREE_WCS_BUFFERS ();
5348#endif
5349 return startpos;
5350 }
5351
5352 if (val == -2)
5353 {
5354#ifdef WCHAR
5355 FREE_WCS_BUFFERS ();
5356#endif
5357 return -2;
5358 }
5359
5360 advance:
5361 if (!range)
5362 break;
5363 else if (range > 0)
5364 {
5365 range--;
5366 startpos++;
5367 }
5368 else
5369 {
5370 range++;
5371 startpos--;
5372 }
5373 }
5374#ifdef WCHAR
5375 FREE_WCS_BUFFERS ();
5376#endif
5377 return -1;
5378}
5379
5380#ifdef WCHAR
5381/* This converts PTR, a pointer into one of the search wchar_t strings
5382 `string1' and `string2' into an multibyte string offset from the
5383 beginning of that string. We use mbs_offset to optimize.
5384 See convert_mbs_to_wcs. */
5385# define POINTER_TO_OFFSET(ptr) \
5386 (FIRST_STRING_P (ptr) \
5387 ? ((regoff_t)(mbs_offset1 != NULL? mbs_offset1[(ptr)-string1] : 0)) \
5388 : ((regoff_t)((mbs_offset2 != NULL? mbs_offset2[(ptr)-string2] : 0) \
5389 + csize1)))
5390#else /* BYTE */
5391/* This converts PTR, a pointer into one of the search strings `string1'
5392 and `string2' into an offset from the beginning of that string. */
5393# define POINTER_TO_OFFSET(ptr) \
5394 (FIRST_STRING_P (ptr) \
5395 ? ((regoff_t) ((ptr) - string1)) \
5396 : ((regoff_t) ((ptr) - string2 + size1)))
5397#endif /* WCHAR */
5398
5399/* Macros for dealing with the split strings in re_match_2. */
5400
5401#define MATCHING_IN_FIRST_STRING (dend == end_match_1)
5402
5403/* Call before fetching a character with *d. This switches over to
5404 string2 if necessary. */
5405#define PREFETCH() \
5406 while (d == dend) \
5407 { \
5408 /* End of string2 => fail. */ \
5409 if (dend == end_match_2) \
5410 goto fail; \
5411 /* End of string1 => advance to string2. */ \
5412 d = string2; \
5413 dend = end_match_2; \
5414 }
5415
5416/* Test if at very beginning or at very end of the virtual concatenation
5417 of `string1' and `string2'. If only one string, it's `string2'. */
5418#define AT_STRINGS_BEG(d) ((d) == (size1 ? string1 : string2) || !size2)
5419#define AT_STRINGS_END(d) ((d) == end2)
5420
5421
5422/* Test if D points to a character which is word-constituent. We have
5423 two special cases to check for: if past the end of string1, look at
5424 the first character in string2; and if before the beginning of
5425 string2, look at the last character in string1. */
5426#ifdef WCHAR
5427/* Use internationalized API instead of SYNTAX. */
5428# define WORDCHAR_P(d) \
5429 (iswalnum ((wint_t)((d) == end1 ? *string2 \
5430 : (d) == string2 - 1 ? *(end1 - 1) : *(d))) != 0 \
5431 || ((d) == end1 ? *string2 \
5432 : (d) == string2 - 1 ? *(end1 - 1) : *(d)) == L'_')
5433#else /* BYTE */
5434# define WORDCHAR_P(d) \
5435 (SYNTAX ((d) == end1 ? *string2 \
5436 : (d) == string2 - 1 ? *(end1 - 1) : *(d)) \
5437 == Sword)
5438#endif /* WCHAR */
5439
5440/* Disabled due to a compiler bug -- see comment at case wordbound */
5441#if 0
5442/* Test if the character before D and the one at D differ with respect
5443 to being word-constituent. */
5444#define AT_WORD_BOUNDARY(d) \
5445 (AT_STRINGS_BEG (d) || AT_STRINGS_END (d) \
5446 || WORDCHAR_P (d - 1) != WORDCHAR_P (d))
5447#endif
5448
5449/* Free everything we malloc. */
5450#ifdef MATCH_MAY_ALLOCATE
5451# ifdef WCHAR
5452# define FREE_VARIABLES() \
5453 do { \
5454 REGEX_FREE_STACK (fail_stack.stack); \
5455 FREE_VAR (regstart); \
5456 FREE_VAR (regend); \
5457 FREE_VAR (old_regstart); \
5458 FREE_VAR (old_regend); \
5459 FREE_VAR (best_regstart); \
5460 FREE_VAR (best_regend); \
5461 FREE_VAR (reg_info); \
5462 FREE_VAR (reg_dummy); \
5463 FREE_VAR (reg_info_dummy); \
5464 if (!cant_free_wcs_buf) \
5465 { \
5466 FREE_VAR (string1); \
5467 FREE_VAR (string2); \
5468 FREE_VAR (mbs_offset1); \
5469 FREE_VAR (mbs_offset2); \
5470 } \
5471 } while (0)
5472# else /* BYTE */
5473# define FREE_VARIABLES() \
5474 do { \
5475 REGEX_FREE_STACK (fail_stack.stack); \
5476 FREE_VAR (regstart); \
5477 FREE_VAR (regend); \
5478 FREE_VAR (old_regstart); \
5479 FREE_VAR (old_regend); \
5480 FREE_VAR (best_regstart); \
5481 FREE_VAR (best_regend); \
5482 FREE_VAR (reg_info); \
5483 FREE_VAR (reg_dummy); \
5484 FREE_VAR (reg_info_dummy); \
5485 } while (0)
5486# endif /* WCHAR */
5487#else
5488# ifdef WCHAR
5489# define FREE_VARIABLES() \
5490 do { \
5491 if (!cant_free_wcs_buf) \
5492 { \
5493 FREE_VAR (string1); \
5494 FREE_VAR (string2); \
5495 FREE_VAR (mbs_offset1); \
5496 FREE_VAR (mbs_offset2); \
5497 } \
5498 } while (0)
5499# else /* BYTE */
5500# define FREE_VARIABLES() ((void)0) /* Do nothing! But inhibit gcc warning. */
5501# endif /* WCHAR */
5502#endif /* not MATCH_MAY_ALLOCATE */
5503
5504/* These values must meet several constraints. They must not be valid
5505 register values; since we have a limit of 255 registers (because
5506 we use only one byte in the pattern for the register number), we can
5507 use numbers larger than 255. They must differ by 1, because of
5508 NUM_FAILURE_ITEMS above. And the value for the lowest register must
5509 be larger than the value for the highest register, so we do not try
5510 to actually save any registers when none are active. */
5511#define NO_HIGHEST_ACTIVE_REG (1 << BYTEWIDTH)
5512#define NO_LOWEST_ACTIVE_REG (NO_HIGHEST_ACTIVE_REG + 1)
5513
5514
5515#else /* not INSIDE_RECURSION */
5516/* Matching routines. */
5517
5518#ifndef emacs /* Emacs never uses this. */
5519/* re_match is like re_match_2 except it takes only a single string. */
5520
5521int
5522re_match (bufp, string, size, pos, regs)
5523 struct re_pattern_buffer *bufp;
5524 const char *string;
5525 int size, pos;
5526 struct re_registers *regs;
5527{
5528 int result;
5529# ifdef MBS_SUPPORT
5530 if (MB_CUR_MAX != 1)
5531 result = wcs_re_match_2_internal (bufp, NULL, 0, string, size,
5532 pos, regs, size,
5533 NULL, 0, NULL, 0, NULL, NULL);
5534 else
5535# endif
5536 result = byte_re_match_2_internal (bufp, NULL, 0, string, size,
5537 pos, regs, size);
5538# ifndef REGEX_MALLOC
5539# ifdef C_ALLOCA
5540 alloca (0);
5541# endif
5542# endif
5543 return result;
5544}
5545# ifdef _LIBC
5546weak_alias (__re_match, re_match)
5547# endif
5548#endif /* not emacs */
5549
5550#endif /* not INSIDE_RECURSION */
5551
5552#ifdef INSIDE_RECURSION
5553static boolean PREFIX(group_match_null_string_p) _RE_ARGS ((UCHAR_T **p,
5554 UCHAR_T *end,
5555 PREFIX(register_info_type) *reg_info));
5556static boolean PREFIX(alt_match_null_string_p) _RE_ARGS ((UCHAR_T *p,
5557 UCHAR_T *end,
5558 PREFIX(register_info_type) *reg_info));
5559static boolean PREFIX(common_op_match_null_string_p) _RE_ARGS ((UCHAR_T **p,
5560 UCHAR_T *end,
5561 PREFIX(register_info_type) *reg_info));
5562static int PREFIX(bcmp_translate) _RE_ARGS ((const CHAR_T *s1, const CHAR_T *s2,
5563 int len, char *translate));
5564#else /* not INSIDE_RECURSION */
5565
5566/* re_match_2 matches the compiled pattern in BUFP against the
5567 the (virtual) concatenation of STRING1 and STRING2 (of length SIZE1
5568 and SIZE2, respectively). We start matching at POS, and stop
5569 matching at STOP.
5570
5571 If REGS is non-null and the `no_sub' field of BUFP is nonzero, we
5572 store offsets for the substring each group matched in REGS. See the
5573 documentation for exactly how many groups we fill.
5574
5575 We return -1 if no match, -2 if an internal error (such as the
5576 failure stack overflowing). Otherwise, we return the length of the
5577 matched substring. */
5578
5579int
5580re_match_2 (bufp, string1, size1, string2, size2, pos, regs, stop)
5581 struct re_pattern_buffer *bufp;
5582 const char *string1, *string2;
5583 int size1, size2;
5584 int pos;
5585 struct re_registers *regs;
5586 int stop;
5587{
5588 int result;
5589# ifdef MBS_SUPPORT
5590 if (MB_CUR_MAX != 1)
5591 result = wcs_re_match_2_internal (bufp, string1, size1, string2, size2,
5592 pos, regs, stop,
5593 NULL, 0, NULL, 0, NULL, NULL);
5594 else
5595# endif
5596 result = byte_re_match_2_internal (bufp, string1, size1, string2, size2,
5597 pos, regs, stop);
5598
5599#ifndef REGEX_MALLOC
5600# ifdef C_ALLOCA
5601 alloca (0);
5602# endif
5603#endif
5604 return result;
5605}
5606#ifdef _LIBC
5607weak_alias (__re_match_2, re_match_2)
5608#endif
5609
5610#endif /* not INSIDE_RECURSION */
5611
5612#ifdef INSIDE_RECURSION
5613
5614#ifdef WCHAR
5615static int count_mbs_length PARAMS ((int *, int));
5616
5617/* This check the substring (from 0, to length) of the multibyte string,
5618 to which offset_buffer correspond. And count how many wchar_t_characters
5619 the substring occupy. We use offset_buffer to optimization.
5620 See convert_mbs_to_wcs. */
5621
5622static int
5623count_mbs_length(offset_buffer, length)
5624 int *offset_buffer;
5625 int length;
5626{
5627 int upper, lower;
5628
5629 /* Check whether the size is valid. */
5630 if (length < 0)
5631 return -1;
5632
5633 if (offset_buffer == NULL)
5634 return 0;
5635
5636 /* If there are no multibyte character, offset_buffer[i] == i.
5637 Optmize for this case. */
5638 if (offset_buffer[length] == length)
5639 return length;
5640
5641 /* Set up upper with length. (because for all i, offset_buffer[i] >= i) */
5642 upper = length;
5643 lower = 0;
5644
5645 while (true)
5646 {
5647 int middle = (lower + upper) / 2;
5648 if (middle == lower || middle == upper)
5649 break;
5650 if (offset_buffer[middle] > length)
5651 upper = middle;
5652 else if (offset_buffer[middle] < length)
5653 lower = middle;
5654 else
5655 return middle;
5656 }
5657
5658 return -1;
5659}
5660#endif /* WCHAR */
5661
5662/* This is a separate function so that we can force an alloca cleanup
5663 afterwards. */
5664#ifdef WCHAR
5665static int
5666wcs_re_match_2_internal (bufp, cstring1, csize1, cstring2, csize2, pos,
5667 regs, stop, string1, size1, string2, size2,
5668 mbs_offset1, mbs_offset2)
5669 struct re_pattern_buffer *bufp;
5670 const char *cstring1, *cstring2;
5671 int csize1, csize2;
5672 int pos;
5673 struct re_registers *regs;
5674 int stop;
5675 /* string1 == string2 == NULL means string1/2, size1/2 and
5676 mbs_offset1/2 need seting up in this function. */
5677 /* We need wchar_t* buffers correspond to cstring1, cstring2. */
5678 wchar_t *string1, *string2;
5679 /* We need the size of wchar_t buffers correspond to csize1, csize2. */
5680 int size1, size2;
5681 /* offset buffer for optimizatoin. See convert_mbs_to_wc. */
5682 int *mbs_offset1, *mbs_offset2;
5683#else /* BYTE */
5684static int
5685byte_re_match_2_internal (bufp, string1, size1,string2, size2, pos,
5686 regs, stop)
5687 struct re_pattern_buffer *bufp;
5688 const char *string1, *string2;
5689 int size1, size2;
5690 int pos;
5691 struct re_registers *regs;
5692 int stop;
5693#endif /* BYTE */
5694{
5695 /* General temporaries. */
5696 int mcnt;
5697 UCHAR_T *p1;
5698#ifdef WCHAR
5699 /* They hold whether each wchar_t is binary data or not. */
5700 char *is_binary = NULL;
5701 /* If true, we can't free string1/2, mbs_offset1/2. */
5702 int cant_free_wcs_buf = 1;
5703#endif /* WCHAR */
5704
5705 /* Just past the end of the corresponding string. */
5706 const CHAR_T *end1, *end2;
5707
5708 /* Pointers into string1 and string2, just past the last characters in
5709 each to consider matching. */
5710 const CHAR_T *end_match_1, *end_match_2;
5711
5712 /* Where we are in the data, and the end of the current string. */
5713 const CHAR_T *d, *dend;
5714
5715 /* Where we are in the pattern, and the end of the pattern. */
5716#ifdef WCHAR
5717 UCHAR_T *pattern, *p;
5718 register UCHAR_T *pend;
5719#else /* BYTE */
5720 UCHAR_T *p = bufp->buffer;
5721 register UCHAR_T *pend = p + bufp->used;
5722#endif /* WCHAR */
5723
5724 /* Mark the opcode just after a start_memory, so we can test for an
5725 empty subpattern when we get to the stop_memory. */
5726 UCHAR_T *just_past_start_mem = 0;
5727
5728 /* We use this to map every character in the string. */
5729 RE_TRANSLATE_TYPE translate = bufp->translate;
5730
5731 /* Failure point stack. Each place that can handle a failure further
5732 down the line pushes a failure point on this stack. It consists of
5733 restart, regend, and reg_info for all registers corresponding to
5734 the subexpressions we're currently inside, plus the number of such
5735 registers, and, finally, two char *'s. The first char * is where
5736 to resume scanning the pattern; the second one is where to resume
5737 scanning the strings. If the latter is zero, the failure point is
5738 a ``dummy''; if a failure happens and the failure point is a dummy,
5739 it gets discarded and the next next one is tried. */
5740#ifdef MATCH_MAY_ALLOCATE /* otherwise, this is global. */
5741 PREFIX(fail_stack_type) fail_stack;
5742#endif
5743#ifdef DEBUG
5744 static unsigned failure_id;
5745 unsigned nfailure_points_pushed = 0, nfailure_points_popped = 0;
5746#endif
5747
5748#ifdef REL_ALLOC
5749 /* This holds the pointer to the failure stack, when
5750 it is allocated relocatably. */
5751 fail_stack_elt_t *failure_stack_ptr;
5752#endif
5753
5754 /* We fill all the registers internally, independent of what we
5755 return, for use in backreferences. The number here includes
5756 an element for register zero. */
5757 size_t num_regs = bufp->re_nsub + 1;
5758
5759 /* The currently active registers. */
5760 active_reg_t lowest_active_reg = NO_LOWEST_ACTIVE_REG;
5761 active_reg_t highest_active_reg = NO_HIGHEST_ACTIVE_REG;
5762
5763 /* Information on the contents of registers. These are pointers into
5764 the input strings; they record just what was matched (on this
5765 attempt) by a subexpression part of the pattern, that is, the
5766 regnum-th regstart pointer points to where in the pattern we began
5767 matching and the regnum-th regend points to right after where we
5768 stopped matching the regnum-th subexpression. (The zeroth register
5769 keeps track of what the whole pattern matches.) */
5770#ifdef MATCH_MAY_ALLOCATE /* otherwise, these are global. */
5771 const CHAR_T **regstart, **regend;
5772#endif
5773
5774 /* If a group that's operated upon by a repetition operator fails to
5775 match anything, then the register for its start will need to be
5776 restored because it will have been set to wherever in the string we
5777 are when we last see its open-group operator. Similarly for a
5778 register's end. */
5779#ifdef MATCH_MAY_ALLOCATE /* otherwise, these are global. */
5780 const CHAR_T **old_regstart, **old_regend;
5781#endif
5782
5783 /* The is_active field of reg_info helps us keep track of which (possibly
5784 nested) subexpressions we are currently in. The matched_something
5785 field of reg_info[reg_num] helps us tell whether or not we have
5786 matched any of the pattern so far this time through the reg_num-th
5787 subexpression. These two fields get reset each time through any
5788 loop their register is in. */
5789#ifdef MATCH_MAY_ALLOCATE /* otherwise, this is global. */
5790 PREFIX(register_info_type) *reg_info;
5791#endif
5792
5793 /* The following record the register info as found in the above
5794 variables when we find a match better than any we've seen before.
5795 This happens as we backtrack through the failure points, which in
5796 turn happens only if we have not yet matched the entire string. */
5797 unsigned best_regs_set = false;
5798#ifdef MATCH_MAY_ALLOCATE /* otherwise, these are global. */
5799 const CHAR_T **best_regstart, **best_regend;
5800#endif
5801
5802 /* Logically, this is `best_regend[0]'. But we don't want to have to
5803 allocate space for that if we're not allocating space for anything
5804 else (see below). Also, we never need info about register 0 for
5805 any of the other register vectors, and it seems rather a kludge to
5806 treat `best_regend' differently than the rest. So we keep track of
5807 the end of the best match so far in a separate variable. We
5808 initialize this to NULL so that when we backtrack the first time
5809 and need to test it, it's not garbage. */
5810 const CHAR_T *match_end = NULL;
5811
5812 /* This helps SET_REGS_MATCHED avoid doing redundant work. */
5813 int set_regs_matched_done = 0;
5814
5815 /* Used when we pop values we don't care about. */
5816#ifdef MATCH_MAY_ALLOCATE /* otherwise, these are global. */
5817 const CHAR_T **reg_dummy;
5818 PREFIX(register_info_type) *reg_info_dummy;
5819#endif
5820
5821#ifdef DEBUG
5822 /* Counts the total number of registers pushed. */
5823 unsigned num_regs_pushed = 0;
5824#endif
5825
5826 /* Definitions for state transitions. More efficiently for gcc. */
5827#ifdef __GNUC__
5828# if defined HAVE_SUBTRACT_LOCAL_LABELS && defined SHARED
5829# define NEXT \
5830 do \
5831 { \
5832 int offset; \
5833 const void *__unbounded ptr; \
5834 offset = (p == pend \
5835 ? 0 : jmptable[SWITCH_ENUM_CAST ((re_opcode_t) *p++)]); \
5836 ptr = &&end_of_pattern + offset; \
5837 goto *ptr; \
5838 } \
5839 while (0)
5840# define REF(x) \
5841 &&label_##x - &&end_of_pattern
5842# define JUMP_TABLE_TYPE const int
5843# else
5844# define NEXT \
5845 do \
5846 { \
5847 const void *__unbounded ptr; \
5848 ptr = (p == pend ? &&end_of_pattern \
5849 : jmptable[SWITCH_ENUM_CAST ((re_opcode_t) *p++)]); \
5850 goto *ptr; \
5851 } \
5852 while (0)
5853# define REF(x) \
5854 &&label_##x
5855# define JUMP_TABLE_TYPE const void *const
5856# endif
5857# define CASE(x) label_##x
5858 static JUMP_TABLE_TYPE jmptable[] =
5859 {
5860 REF (no_op),
5861 REF (succeed),
5862 REF (exactn),
5863# ifdef MBS_SUPPORT
5864 REF (exactn_bin),
5865# endif
5866 REF (anychar),
5867 REF (charset),
5868 REF (charset_not),
5869 REF (start_memory),
5870 REF (stop_memory),
5871 REF (duplicate),
5872 REF (begline),
5873 REF (endline),
5874 REF (begbuf),
5875 REF (endbuf),
5876 REF (jump),
5877 REF (jump_past_alt),
5878 REF (on_failure_jump),
5879 REF (on_failure_keep_string_jump),
5880 REF (pop_failure_jump),
5881 REF (maybe_pop_jump),
5882 REF (dummy_failure_jump),
5883 REF (push_dummy_failure),
5884 REF (succeed_n),
5885 REF (jump_n),
5886 REF (set_number_at),
5887 REF (wordchar),
5888 REF (notwordchar),
5889 REF (wordbeg),
5890 REF (wordend),
5891 REF (wordbound),
5892 REF (notwordbound)
5893# ifdef emacs
5894 ,REF (before_dot),
5895 REF (at_dot),
5896 REF (after_dot),
5897 REF (syntaxspec),
5898 REF (notsyntaxspec)
5899# endif
5900 };
5901#else
5902# define NEXT \
5903 break
5904# define CASE(x) \
5905 case x
5906#endif
5907
5908 DEBUG_PRINT1 ("\n\nEntering re_match_2.\n");
5909
5910 INIT_FAIL_STACK ();
5911
5912#ifdef MATCH_MAY_ALLOCATE
5913 /* Do not bother to initialize all the register variables if there are
5914 no groups in the pattern, as it takes a fair amount of time. If
5915 there are groups, we include space for register 0 (the whole
5916 pattern), even though we never use it, since it simplifies the
5917 array indexing. We should fix this. */
5918 if (bufp->re_nsub)
5919 {
5920 regstart = REGEX_TALLOC (num_regs, const CHAR_T *);
5921 regend = REGEX_TALLOC (num_regs, const CHAR_T *);
5922 old_regstart = REGEX_TALLOC (num_regs, const CHAR_T *);
5923 old_regend = REGEX_TALLOC (num_regs, const CHAR_T *);
5924 best_regstart = REGEX_TALLOC (num_regs, const CHAR_T *);
5925 best_regend = REGEX_TALLOC (num_regs, const CHAR_T *);
5926 reg_info = REGEX_TALLOC (num_regs, PREFIX(register_info_type));
5927 reg_dummy = REGEX_TALLOC (num_regs, const CHAR_T *);
5928 reg_info_dummy = REGEX_TALLOC (num_regs, PREFIX(register_info_type));
5929
5930 if (!(regstart && regend && old_regstart && old_regend && reg_info
5931 && best_regstart && best_regend && reg_dummy && reg_info_dummy))
5932 {
5933 FREE_VARIABLES ();
5934 return -2;
5935 }
5936 }
5937 else
5938 {
5939 /* We must initialize all our variables to NULL, so that
5940 `FREE_VARIABLES' doesn't try to free them. */
5941 regstart = regend = old_regstart = old_regend = best_regstart
5942 = best_regend = reg_dummy = NULL;
5943 reg_info = reg_info_dummy = (PREFIX(register_info_type) *) NULL;
5944 }
5945#endif /* MATCH_MAY_ALLOCATE */
5946
5947 /* The starting position is bogus. */
5948#ifdef WCHAR
5949 if (pos < 0 || pos > csize1 + csize2)
5950#else /* BYTE */
5951 if (pos < 0 || pos > size1 + size2)
5952#endif
5953 {
5954 FREE_VARIABLES ();
5955 return -1;
5956 }
5957
5958#ifdef WCHAR
5959 /* Allocate wchar_t array for string1 and string2 and
5960 fill them with converted string. */
5961 if (string1 == NULL && string2 == NULL)
5962 {
5963 /* We need seting up buffers here. */
5964
5965 /* We must free wcs buffers in this function. */
5966 cant_free_wcs_buf = 0;
5967
5968 if (csize1 != 0)
5969 {
5970 string1 = REGEX_TALLOC (csize1 + 1, CHAR_T);
5971 mbs_offset1 = REGEX_TALLOC (csize1 + 1, int);
5972 is_binary = REGEX_TALLOC (csize1 + 1, char);
5973 if (!string1 || !mbs_offset1 || !is_binary)
5974 {
5975 FREE_VAR (string1);
5976 FREE_VAR (mbs_offset1);
5977 FREE_VAR (is_binary);
5978 return -2;
5979 }
5980 }
5981 if (csize2 != 0)
5982 {
5983 string2 = REGEX_TALLOC (csize2 + 1, CHAR_T);
5984 mbs_offset2 = REGEX_TALLOC (csize2 + 1, int);
5985 is_binary = REGEX_TALLOC (csize2 + 1, char);
5986 if (!string2 || !mbs_offset2 || !is_binary)
5987 {
5988 FREE_VAR (string1);
5989 FREE_VAR (mbs_offset1);
5990 FREE_VAR (string2);
5991 FREE_VAR (mbs_offset2);
5992 FREE_VAR (is_binary);
5993 return -2;
5994 }
5995 size2 = convert_mbs_to_wcs(string2, cstring2, csize2,
5996 mbs_offset2, is_binary);
5997 string2[size2] = L'\0'; /* for a sentinel */
5998 FREE_VAR (is_binary);
5999 }
6000 }
6001
6002 /* We need to cast pattern to (wchar_t*), because we casted this compiled
6003 pattern to (char*) in regex_compile. */
6004 p = pattern = (CHAR_T*)bufp->buffer;
6005 pend = (CHAR_T*)(bufp->buffer + bufp->used);
6006
6007#endif /* WCHAR */
6008
6009 /* Initialize subexpression text positions to -1 to mark ones that no
6010 start_memory/stop_memory has been seen for. Also initialize the
6011 register information struct. */
6012 for (mcnt = 1; (unsigned) mcnt < num_regs; mcnt++)
6013 {
6014 regstart[mcnt] = regend[mcnt]
6015 = old_regstart[mcnt] = old_regend[mcnt] = REG_UNSET_VALUE;
6016
6017 REG_MATCH_NULL_STRING_P (reg_info[mcnt]) = MATCH_NULL_UNSET_VALUE;
6018 IS_ACTIVE (reg_info[mcnt]) = 0;
6019 MATCHED_SOMETHING (reg_info[mcnt]) = 0;
6020 EVER_MATCHED_SOMETHING (reg_info[mcnt]) = 0;
6021 }
6022
6023 /* We move `string1' into `string2' if the latter's empty -- but not if
6024 `string1' is null. */
6025 if (size2 == 0 && string1 != NULL)
6026 {
6027 string2 = string1;
6028 size2 = size1;
6029 string1 = 0;
6030 size1 = 0;
6031#ifdef WCHAR
6032 mbs_offset2 = mbs_offset1;
6033 csize2 = csize1;
6034 mbs_offset1 = NULL;
6035 csize1 = 0;
6036#endif
6037 }
6038 end1 = string1 + size1;
6039 end2 = string2 + size2;
6040
6041 /* Compute where to stop matching, within the two strings. */
6042#ifdef WCHAR
6043 if (stop <= csize1)
6044 {
6045 mcnt = count_mbs_length(mbs_offset1, stop);
6046 end_match_1 = string1 + mcnt;
6047 end_match_2 = string2;
6048 }
6049 else
6050 {
6051 if (stop > csize1 + csize2)
6052 stop = csize1 + csize2;
6053 end_match_1 = end1;
6054 mcnt = count_mbs_length(mbs_offset2, stop-csize1);
6055 end_match_2 = string2 + mcnt;
6056 }
6057 if (mcnt < 0)
6058 { /* count_mbs_length return error. */
6059 FREE_VARIABLES ();
6060 return -1;
6061 }
6062#else
6063 if (stop <= size1)
6064 {
6065 end_match_1 = string1 + stop;
6066 end_match_2 = string2;
6067 }
6068 else
6069 {
6070 end_match_1 = end1;
6071 end_match_2 = string2 + stop - size1;
6072 }
6073#endif /* WCHAR */
6074
6075 /* `p' scans through the pattern as `d' scans through the data.
6076 `dend' is the end of the input string that `d' points within. `d'
6077 is advanced into the following input string whenever necessary, but
6078 this happens before fetching; therefore, at the beginning of the
6079 loop, `d' can be pointing at the end of a string, but it cannot
6080 equal `string2'. */
6081#ifdef WCHAR
6082 if (size1 > 0 && pos <= csize1)
6083 {
6084 mcnt = count_mbs_length(mbs_offset1, pos);
6085 d = string1 + mcnt;
6086 dend = end_match_1;
6087 }
6088 else
6089 {
6090 mcnt = count_mbs_length(mbs_offset2, pos-csize1);
6091 d = string2 + mcnt;
6092 dend = end_match_2;
6093 }
6094
6095 if (mcnt < 0)
6096 { /* count_mbs_length return error. */
6097 FREE_VARIABLES ();
6098 return -1;
6099 }
6100#else
6101 if (size1 > 0 && pos <= size1)
6102 {
6103 d = string1 + pos;
6104 dend = end_match_1;
6105 }
6106 else
6107 {
6108 d = string2 + pos - size1;
6109 dend = end_match_2;
6110 }
6111#endif /* WCHAR */
6112
6113 DEBUG_PRINT1 ("The compiled pattern is:\n");
6114 DEBUG_PRINT_COMPILED_PATTERN (bufp, p, pend);
6115 DEBUG_PRINT1 ("The string to match is: `");
6116 DEBUG_PRINT_DOUBLE_STRING (d, string1, size1, string2, size2);
6117 DEBUG_PRINT1 ("'\n");
6118
6119 /* This loops over pattern commands. It exits by returning from the
6120 function if the match is complete, or it drops through if the match
6121 fails at this starting point in the input data. */
6122 for (;;)
6123 {
6124#ifdef _LIBC
6125 DEBUG_PRINT2 ("\n%p: ", p);
6126#else
6127 DEBUG_PRINT2 ("\n0x%x: ", p);
6128#endif
6129
6130#ifdef __GNUC__
6131 NEXT;
6132#else
6133 if (p == pend)
6134#endif
6135 {
6136#ifdef __GNUC__
6137 end_of_pattern:
6138#endif
6139 /* End of pattern means we might have succeeded. */
6140 DEBUG_PRINT1 ("end of pattern ... ");
6141
6142 /* If we haven't matched the entire string, and we want the
6143 longest match, try backtracking. */
6144 if (d != end_match_2)
6145 {
6146 /* 1 if this match ends in the same string (string1 or string2)
6147 as the best previous match. */
6148 boolean same_str_p = (FIRST_STRING_P (match_end)
6149 == MATCHING_IN_FIRST_STRING);
6150 /* 1 if this match is the best seen so far. */
6151 boolean best_match_p;
6152
6153 /* AIX compiler got confused when this was combined
6154 with the previous declaration. */
6155 if (same_str_p)
6156 best_match_p = d > match_end;
6157 else
6158 best_match_p = !MATCHING_IN_FIRST_STRING;
6159
6160 DEBUG_PRINT1 ("backtracking.\n");
6161
6162 if (!FAIL_STACK_EMPTY ())
6163 { /* More failure points to try. */
6164
6165 /* If exceeds best match so far, save it. */
6166 if (!best_regs_set || best_match_p)
6167 {
6168 best_regs_set = true;
6169 match_end = d;
6170
6171 DEBUG_PRINT1 ("\nSAVING match as best so far.\n");
6172
6173 for (mcnt = 1; (unsigned) mcnt < num_regs; mcnt++)
6174 {
6175 best_regstart[mcnt] = regstart[mcnt];
6176 best_regend[mcnt] = regend[mcnt];
6177 }
6178 }
6179 goto fail;
6180 }
6181
6182 /* If no failure points, don't restore garbage. And if
6183 last match is real best match, don't restore second
6184 best one. */
6185 else if (best_regs_set && !best_match_p)
6186 {
6187 restore_best_regs:
6188 /* Restore best match. It may happen that `dend ==
6189 end_match_1' while the restored d is in string2.
6190 For example, the pattern `x.*y.*z' against the
6191 strings `x-' and `y-z-', if the two strings are
6192 not consecutive in memory. */
6193 DEBUG_PRINT1 ("Restoring best registers.\n");
6194
6195 d = match_end;
6196 dend = ((d >= string1 && d <= end1)
6197 ? end_match_1 : end_match_2);
6198
6199 for (mcnt = 1; (unsigned) mcnt < num_regs; mcnt++)
6200 {
6201 regstart[mcnt] = best_regstart[mcnt];
6202 regend[mcnt] = best_regend[mcnt];
6203 }
6204 }
6205 } /* d != end_match_2 */
6206
6207 succeed_label:
6208 DEBUG_PRINT1 ("Accepting match.\n");
6209 /* If caller wants register contents data back, do it. */
6210 if (regs && !bufp->no_sub)
6211 {
6212 /* Have the register data arrays been allocated? */
6213 if (bufp->regs_allocated == REGS_UNALLOCATED)
6214 { /* No. So allocate them with malloc. We need one
6215 extra element beyond `num_regs' for the `-1' marker
6216 GNU code uses. */
6217 regs->num_regs = MAX (RE_NREGS, num_regs + 1);
6218 regs->start = TALLOC (regs->num_regs, regoff_t);
6219 regs->end = TALLOC (regs->num_regs, regoff_t);
6220 if (regs->start == NULL || regs->end == NULL)
6221 {
6222 FREE_VARIABLES ();
6223 return -2;
6224 }
6225 bufp->regs_allocated = REGS_REALLOCATE;
6226 }
6227 else if (bufp->regs_allocated == REGS_REALLOCATE)
6228 { /* Yes. If we need more elements than were already
6229 allocated, reallocate them. If we need fewer, just
6230 leave it alone. */
6231 if (regs->num_regs < num_regs + 1)
6232 {
6233 regs->num_regs = num_regs + 1;
6234 RETALLOC (regs->start, regs->num_regs, regoff_t);
6235 RETALLOC (regs->end, regs->num_regs, regoff_t);
6236 if (regs->start == NULL || regs->end == NULL)
6237 {
6238 FREE_VARIABLES ();
6239 return -2;
6240 }
6241 }
6242 }
6243 else
6244 {
6245 /* These braces fend off a "empty body in an else-statement"
6246 warning under GCC when assert expands to nothing. */
6247 assert (bufp->regs_allocated == REGS_FIXED);
6248 }
6249
6250 /* Convert the pointer data in `regstart' and `regend' to
6251 indices. Register zero has to be set differently,
6252 since we haven't kept track of any info for it. */
6253 if (regs->num_regs > 0)
6254 {
6255 regs->start[0] = pos;
6256#ifdef WCHAR
6257 if (MATCHING_IN_FIRST_STRING)
6258 regs->end[0] = (mbs_offset1 != NULL ?
6259 mbs_offset1[d-string1] : 0);
6260 else
6261 regs->end[0] = csize1 + (mbs_offset2 != NULL
6262 ? mbs_offset2[d-string2] : 0);
6263#else
6264 regs->end[0] = (MATCHING_IN_FIRST_STRING
6265 ? ((regoff_t) (d - string1))
6266 : ((regoff_t) (d - string2 + size1)));
6267#endif /* WCHAR */
6268 }
6269
6270 /* Go through the first `min (num_regs, regs->num_regs)'
6271 registers, since that is all we initialized. */
6272 for (mcnt = 1; (unsigned) mcnt < MIN (num_regs, regs->num_regs);
6273 mcnt++)
6274 {
6275 if (REG_UNSET (regstart[mcnt]) || REG_UNSET (regend[mcnt]))
6276 regs->start[mcnt] = regs->end[mcnt] = -1;
6277 else
6278 {
6279 regs->start[mcnt]
6280 = (regoff_t) POINTER_TO_OFFSET (regstart[mcnt]);
6281 regs->end[mcnt]
6282 = (regoff_t) POINTER_TO_OFFSET (regend[mcnt]);
6283 }
6284 }
6285
6286 /* If the regs structure we return has more elements than
6287 were in the pattern, set the extra elements to -1. If
6288 we (re)allocated the registers, this is the case,
6289 because we always allocate enough to have at least one
6290 -1 at the end. */
6291 for (mcnt = num_regs; (unsigned) mcnt < regs->num_regs; mcnt++)
6292 regs->start[mcnt] = regs->end[mcnt] = -1;
6293 } /* regs && !bufp->no_sub */
6294
6295 DEBUG_PRINT4 ("%u failure points pushed, %u popped (%u remain).\n",
6296 nfailure_points_pushed, nfailure_points_popped,
6297 nfailure_points_pushed - nfailure_points_popped);
6298 DEBUG_PRINT2 ("%u registers pushed.\n", num_regs_pushed);
6299
6300#ifdef WCHAR
6301 if (MATCHING_IN_FIRST_STRING)
6302 mcnt = mbs_offset1 != NULL ? mbs_offset1[d-string1] : 0;
6303 else
6304 mcnt = (mbs_offset2 != NULL ? mbs_offset2[d-string2] : 0) +
6305 csize1;
6306 mcnt -= pos;
6307#else
6308 mcnt = d - pos - (MATCHING_IN_FIRST_STRING
6309 ? string1 : string2 - size1);
6310#endif /* WCHAR */
6311
6312 DEBUG_PRINT2 ("Returning %d from re_match_2.\n", mcnt);
6313
6314 FREE_VARIABLES ();
6315 return mcnt;
6316 }
6317
6318#ifndef __GNUC__
6319 /* Otherwise match next pattern command. */
6320 switch (SWITCH_ENUM_CAST ((re_opcode_t) *p++))
6321 {
6322#endif
6323 /* Ignore these. Used to ignore the n of succeed_n's which
6324 currently have n == 0. */
6325 CASE (no_op):
6326 DEBUG_PRINT1 ("EXECUTING no_op.\n");
6327 NEXT;
6328
6329 CASE (succeed):
6330 DEBUG_PRINT1 ("EXECUTING succeed.\n");
6331 goto succeed_label;
6332
6333 /* Match the next n pattern characters exactly. The following
6334 byte in the pattern defines n, and the n bytes after that
6335 are the characters to match. */
6336 CASE (exactn):
6337#ifdef MBS_SUPPORT
6338 CASE (exactn_bin):
6339#endif
6340 mcnt = *p++;
6341 DEBUG_PRINT2 ("EXECUTING exactn %d.\n", mcnt);
6342
6343 /* This is written out as an if-else so we don't waste time
6344 testing `translate' inside the loop. */
6345 if (translate)
6346 {
6347 do
6348 {
6349 PREFETCH ();
6350#ifdef WCHAR
6351 if (*d <= 0xff)
6352 {
6353 if ((UCHAR_T) translate[(unsigned char) *d++]
6354 != (UCHAR_T) *p++)
6355 goto fail;
6356 }
6357 else
6358 {
6359 if (*d++ != (CHAR_T) *p++)
6360 goto fail;
6361 }
6362#else
6363 if ((UCHAR_T) translate[(unsigned char) *d++]
6364 != (UCHAR_T) *p++)
6365 goto fail;
6366#endif /* WCHAR */
6367 }
6368 while (--mcnt);
6369 }
6370 else
6371 {
6372 do
6373 {
6374 PREFETCH ();
6375 if (*d++ != (CHAR_T) *p++) goto fail;
6376 }
6377 while (--mcnt);
6378 }
6379 SET_REGS_MATCHED ();
6380 NEXT;
6381
6382
6383 /* Match any character except possibly a newline or a null. */
6384 CASE (anychar):
6385 DEBUG_PRINT1 ("EXECUTING anychar.\n");
6386
6387 PREFETCH ();
6388
6389 if ((!(bufp->syntax & RE_DOT_NEWLINE) && TRANSLATE (*d) == '\n')
6390 || (bufp->syntax & RE_DOT_NOT_NULL && TRANSLATE (*d) == '\000'))
6391 goto fail;
6392
6393 SET_REGS_MATCHED ();
6394 DEBUG_PRINT2 (" Matched `%ld'.\n", (long int) *d);
6395 d++;
6396 NEXT;
6397
6398
6399 CASE (charset):
6400 CASE (charset_not):
6401 {
6402 register UCHAR_T c;
6403#ifdef WCHAR
6404 unsigned int i, char_class_length, coll_symbol_length,
6405 equiv_class_length, ranges_length, chars_length, length;
6406 CHAR_T *workp, *workp2, *charset_top;
6407#define WORK_BUFFER_SIZE 128
6408 CHAR_T str_buf[WORK_BUFFER_SIZE];
6409# ifdef _LIBC
6410 uint32_t nrules;
6411# endif /* _LIBC */
6412#endif /* WCHAR */
6413 boolean not = (re_opcode_t) *(p - 1) == charset_not;
6414
6415 DEBUG_PRINT2 ("EXECUTING charset%s.\n", not ? "_not" : "");
6416 PREFETCH ();
6417 c = TRANSLATE (*d); /* The character to match. */
6418#ifdef WCHAR
6419# ifdef _LIBC
6420 nrules = _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_NRULES);
6421# endif /* _LIBC */
6422 charset_top = p - 1;
6423 char_class_length = *p++;
6424 coll_symbol_length = *p++;
6425 equiv_class_length = *p++;
6426 ranges_length = *p++;
6427 chars_length = *p++;
6428 /* p points charset[6], so the address of the next instruction
6429 (charset[l+m+n+2o+k+p']) equals p[l+m+n+2*o+p'],
6430 where l=length of char_classes, m=length of collating_symbol,
6431 n=equivalence_class, o=length of char_range,
6432 p'=length of character. */
6433 workp = p;
6434 /* Update p to indicate the next instruction. */
6435 p += char_class_length + coll_symbol_length+ equiv_class_length +
6436 2*ranges_length + chars_length;
6437
6438 /* match with char_class? */
6439 for (i = 0; i < char_class_length ; i += CHAR_CLASS_SIZE)
6440 {
6441 wctype_t wctype;
6442 uintptr_t alignedp = ((uintptr_t)workp
6443 + __alignof__(wctype_t) - 1)
6444 & ~(uintptr_t)(__alignof__(wctype_t) - 1);
6445 wctype = *((wctype_t*)alignedp);
6446 workp += CHAR_CLASS_SIZE;
6447 if (iswctype((wint_t)c, wctype))
6448 goto char_set_matched;
6449 }
6450
6451 /* match with collating_symbol? */
6452# ifdef _LIBC
6453 if (nrules != 0)
6454 {
6455 const unsigned char *extra = (const unsigned char *)
6456 _NL_CURRENT (LC_COLLATE, _NL_COLLATE_SYMB_EXTRAMB);
6457
6458 for (workp2 = workp + coll_symbol_length ; workp < workp2 ;
6459 workp++)
6460 {
6461 int32_t *wextra;
6462 wextra = (int32_t*)(extra + *workp++);
6463 for (i = 0; i < *wextra; ++i)
6464 if (TRANSLATE(d[i]) != wextra[1 + i])
6465 break;
6466
6467 if (i == *wextra)
6468 {
6469 /* Update d, however d will be incremented at
6470 char_set_matched:, we decrement d here. */
6471 d += i - 1;
6472 goto char_set_matched;
6473 }
6474 }
6475 }
6476 else /* (nrules == 0) */
6477# endif
6478 /* If we can't look up collation data, we use wcscoll
6479 instead. */
6480 {
6481 for (workp2 = workp + coll_symbol_length ; workp < workp2 ;)
6482 {
6483 const CHAR_T *backup_d = d, *backup_dend = dend;
6484 length = wcslen (workp);
6485
6486 /* If wcscoll(the collating symbol, whole string) > 0,
6487 any substring of the string never match with the
6488 collating symbol. */
6489 if (wcscoll (workp, d) > 0)
6490 {
6491 workp += length + 1;
6492 continue;
6493 }
6494
6495 /* First, we compare the collating symbol with
6496 the first character of the string.
6497 If it don't match, we add the next character to
6498 the compare buffer in turn. */
6499 for (i = 0 ; i < WORK_BUFFER_SIZE-1 ; i++, d++)
6500 {
6501 int match;
6502 if (d == dend)
6503 {
6504 if (dend == end_match_2)
6505 break;
6506 d = string2;
6507 dend = end_match_2;
6508 }
6509
6510 /* add next character to the compare buffer. */
6511 str_buf[i] = TRANSLATE(*d);
6512 str_buf[i+1] = '\0';
6513
6514 match = wcscoll (workp, str_buf);
6515 if (match == 0)
6516 goto char_set_matched;
6517
6518 if (match < 0)
6519 /* (str_buf > workp) indicate (str_buf + X > workp),
6520 because for all X (str_buf + X > str_buf).
6521 So we don't need continue this loop. */
6522 break;
6523
6524 /* Otherwise(str_buf < workp),
6525 (str_buf+next_character) may equals (workp).
6526 So we continue this loop. */
6527 }
6528 /* not matched */
6529 d = backup_d;
6530 dend = backup_dend;
6531 workp += length + 1;
6532 }
6533 }
6534 /* match with equivalence_class? */
6535# ifdef _LIBC
6536 if (nrules != 0)
6537 {
6538 const CHAR_T *backup_d = d, *backup_dend = dend;
6539 /* Try to match the equivalence class against
6540 those known to the collate implementation. */
6541 const int32_t *table;
6542 const int32_t *weights;
6543 const int32_t *extra;
6544 const int32_t *indirect;
6545 int32_t idx, idx2;
6546 wint_t *cp;
6547 size_t len;
6548
6549 /* This #include defines a local function! */
6550# include <locale/weightwc.h>
6551
6552 table = (const int32_t *)
6553 _NL_CURRENT (LC_COLLATE, _NL_COLLATE_TABLEWC);
6554 weights = (const wint_t *)
6555 _NL_CURRENT (LC_COLLATE, _NL_COLLATE_WEIGHTWC);
6556 extra = (const wint_t *)
6557 _NL_CURRENT (LC_COLLATE, _NL_COLLATE_EXTRAWC);
6558 indirect = (const int32_t *)
6559 _NL_CURRENT (LC_COLLATE, _NL_COLLATE_INDIRECTWC);
6560
6561 /* Write 1 collating element to str_buf, and
6562 get its index. */
6563 idx2 = 0;
6564
6565 for (i = 0 ; idx2 == 0 && i < WORK_BUFFER_SIZE - 1; i++)
6566 {
6567 cp = (wint_t*)str_buf;
6568 if (d == dend)
6569 {
6570 if (dend == end_match_2)
6571 break;
6572 d = string2;
6573 dend = end_match_2;
6574 }
6575 str_buf[i] = TRANSLATE(*(d+i));
6576 str_buf[i+1] = '\0'; /* sentinel */
6577 idx2 = findidx ((const wint_t**)&cp);
6578 }
6579
6580 /* Update d, however d will be incremented at
6581 char_set_matched:, we decrement d here. */
6582 d = backup_d + ((wchar_t*)cp - (wchar_t*)str_buf - 1);
6583 if (d >= dend)
6584 {
6585 if (dend == end_match_2)
6586 d = dend;
6587 else
6588 {
6589 d = string2;
6590 dend = end_match_2;
6591 }
6592 }
6593
6594 len = weights[idx2];
6595
6596 for (workp2 = workp + equiv_class_length ; workp < workp2 ;
6597 workp++)
6598 {
6599 idx = (int32_t)*workp;
6600 /* We already checked idx != 0 in regex_compile. */
6601
6602 if (idx2 != 0 && len == weights[idx])
6603 {
6604 int cnt = 0;
6605 while (cnt < len && (weights[idx + 1 + cnt]
6606 == weights[idx2 + 1 + cnt]))
6607 ++cnt;
6608
6609 if (cnt == len)
6610 goto char_set_matched;
6611 }
6612 }
6613 /* not matched */
6614 d = backup_d;
6615 dend = backup_dend;
6616 }
6617 else /* (nrules == 0) */
6618# endif
6619 /* If we can't look up collation data, we use wcscoll
6620 instead. */
6621 {
6622 for (workp2 = workp + equiv_class_length ; workp < workp2 ;)
6623 {
6624 const CHAR_T *backup_d = d, *backup_dend = dend;
6625 length = wcslen (workp);
6626
6627 /* If wcscoll(the collating symbol, whole string) > 0,
6628 any substring of the string never match with the
6629 collating symbol. */
6630 if (wcscoll (workp, d) > 0)
6631 {
6632 workp += length + 1;
6633 break;
6634 }
6635
6636 /* First, we compare the equivalence class with
6637 the first character of the string.
6638 If it don't match, we add the next character to
6639 the compare buffer in turn. */
6640 for (i = 0 ; i < WORK_BUFFER_SIZE - 1 ; i++, d++)
6641 {
6642 int match;
6643 if (d == dend)
6644 {
6645 if (dend == end_match_2)
6646 break;
6647 d = string2;
6648 dend = end_match_2;
6649 }
6650
6651 /* add next character to the compare buffer. */
6652 str_buf[i] = TRANSLATE(*d);
6653 str_buf[i+1] = '\0';
6654
6655 match = wcscoll (workp, str_buf);
6656
6657 if (match == 0)
6658 goto char_set_matched;
6659
6660 if (match < 0)
6661 /* (str_buf > workp) indicate (str_buf + X > workp),
6662 because for all X (str_buf + X > str_buf).
6663 So we don't need continue this loop. */
6664 break;
6665
6666 /* Otherwise(str_buf < workp),
6667 (str_buf+next_character) may equals (workp).
6668 So we continue this loop. */
6669 }
6670 /* not matched */
6671 d = backup_d;
6672 dend = backup_dend;
6673 workp += length + 1;
6674 }
6675 }
6676
6677 /* match with char_range? */
6678# ifdef _LIBC
6679 if (nrules != 0)
6680 {
6681 uint32_t collseqval;
6682 const char *collseq = (const char *)
6683 _NL_CURRENT(LC_COLLATE, _NL_COLLATE_COLLSEQWC);
6684
6685 collseqval = collseq_table_lookup (collseq, c);
6686
6687 for (; workp < p - chars_length ;)
6688 {
6689 uint32_t start_val, end_val;
6690
6691 /* We already compute the collation sequence value
6692 of the characters (or collating symbols). */
6693 start_val = (uint32_t) *workp++; /* range_start */
6694 end_val = (uint32_t) *workp++; /* range_end */
6695
6696 if (start_val <= collseqval && collseqval <= end_val)
6697 goto char_set_matched;
6698 }
6699 }
6700 else
6701# endif
6702 {
6703 /* We set range_start_char at str_buf[0], range_end_char
6704 at str_buf[4], and compared char at str_buf[2]. */
6705 str_buf[1] = 0;
6706 str_buf[2] = c;
6707 str_buf[3] = 0;
6708 str_buf[5] = 0;
6709 for (; workp < p - chars_length ;)
6710 {
6711 wchar_t *range_start_char, *range_end_char;
6712
6713 /* match if (range_start_char <= c <= range_end_char). */
6714
6715 /* If range_start(or end) < 0, we assume -range_start(end)
6716 is the offset of the collating symbol which is specified
6717 as the character of the range start(end). */
6718
6719 /* range_start */
6720 if (*workp < 0)
6721 range_start_char = charset_top - (*workp++);
6722 else
6723 {
6724 str_buf[0] = *workp++;
6725 range_start_char = str_buf;
6726 }
6727
6728 /* range_end */
6729 if (*workp < 0)
6730 range_end_char = charset_top - (*workp++);
6731 else
6732 {
6733 str_buf[4] = *workp++;
6734 range_end_char = str_buf + 4;
6735 }
6736
6737 if (wcscoll (range_start_char, str_buf+2) <= 0
6738 && wcscoll (str_buf+2, range_end_char) <= 0)
6739 goto char_set_matched;
6740 }
6741 }
6742
6743 /* match with char? */
6744 for (; workp < p ; workp++)
6745 if (c == *workp)
6746 goto char_set_matched;
6747
6748 not = !not;
6749
6750 char_set_matched:
6751 if (not) goto fail;
6752#else
6753 /* Cast to `unsigned' instead of `unsigned char' in case the
6754 bit list is a full 32 bytes long. */
6755 if (c < (unsigned) (*p * BYTEWIDTH)
6756 && p[1 + c / BYTEWIDTH] & (1 << (c % BYTEWIDTH)))
6757 not = !not;
6758
6759 p += 1 + *p;
6760
6761 if (!not) goto fail;
6762#undef WORK_BUFFER_SIZE
6763#endif /* WCHAR */
6764 SET_REGS_MATCHED ();
6765 d++;
6766 NEXT;
6767 }
6768
6769
6770 /* The beginning of a group is represented by start_memory.
6771 The arguments are the register number in the next byte, and the
6772 number of groups inner to this one in the next. The text
6773 matched within the group is recorded (in the internal
6774 registers data structure) under the register number. */
6775 CASE (start_memory):
6776 DEBUG_PRINT3 ("EXECUTING start_memory %ld (%ld):\n",
6777 (long int) *p, (long int) p[1]);
6778
6779 /* Find out if this group can match the empty string. */
6780 p1 = p; /* To send to group_match_null_string_p. */
6781
6782 if (REG_MATCH_NULL_STRING_P (reg_info[*p]) == MATCH_NULL_UNSET_VALUE)
6783 REG_MATCH_NULL_STRING_P (reg_info[*p])
6784 = PREFIX(group_match_null_string_p) (&p1, pend, reg_info);
6785
6786 /* Save the position in the string where we were the last time
6787 we were at this open-group operator in case the group is
6788 operated upon by a repetition operator, e.g., with `(a*)*b'
6789 against `ab'; then we want to ignore where we are now in
6790 the string in case this attempt to match fails. */
6791 old_regstart[*p] = REG_MATCH_NULL_STRING_P (reg_info[*p])
6792 ? REG_UNSET (regstart[*p]) ? d : regstart[*p]
6793 : regstart[*p];
6794 DEBUG_PRINT2 (" old_regstart: %d\n",
6795 POINTER_TO_OFFSET (old_regstart[*p]));
6796
6797 regstart[*p] = d;
6798 DEBUG_PRINT2 (" regstart: %d\n", POINTER_TO_OFFSET (regstart[*p]));
6799
6800 IS_ACTIVE (reg_info[*p]) = 1;
6801 MATCHED_SOMETHING (reg_info[*p]) = 0;
6802
6803 /* Clear this whenever we change the register activity status. */
6804 set_regs_matched_done = 0;
6805
6806 /* This is the new highest active register. */
6807 highest_active_reg = *p;
6808
6809 /* If nothing was active before, this is the new lowest active
6810 register. */
6811 if (lowest_active_reg == NO_LOWEST_ACTIVE_REG)
6812 lowest_active_reg = *p;
6813
6814 /* Move past the register number and inner group count. */
6815 p += 2;
6816 just_past_start_mem = p;
6817
6818 NEXT;
6819
6820
6821 /* The stop_memory opcode represents the end of a group. Its
6822 arguments are the same as start_memory's: the register
6823 number, and the number of inner groups. */
6824 CASE (stop_memory):
6825 DEBUG_PRINT3 ("EXECUTING stop_memory %ld (%ld):\n",
6826 (long int) *p, (long int) p[1]);
6827
6828 /* We need to save the string position the last time we were at
6829 this close-group operator in case the group is operated
6830 upon by a repetition operator, e.g., with `((a*)*(b*)*)*'
6831 against `aba'; then we want to ignore where we are now in
6832 the string in case this attempt to match fails. */
6833 old_regend[*p] = REG_MATCH_NULL_STRING_P (reg_info[*p])
6834 ? REG_UNSET (regend[*p]) ? d : regend[*p]
6835 : regend[*p];
6836 DEBUG_PRINT2 (" old_regend: %d\n",
6837 POINTER_TO_OFFSET (old_regend[*p]));
6838
6839 regend[*p] = d;
6840 DEBUG_PRINT2 (" regend: %d\n", POINTER_TO_OFFSET (regend[*p]));
6841
6842 /* This register isn't active anymore. */
6843 IS_ACTIVE (reg_info[*p]) = 0;
6844
6845 /* Clear this whenever we change the register activity status. */
6846 set_regs_matched_done = 0;
6847
6848 /* If this was the only register active, nothing is active
6849 anymore. */
6850 if (lowest_active_reg == highest_active_reg)
6851 {
6852 lowest_active_reg = NO_LOWEST_ACTIVE_REG;
6853 highest_active_reg = NO_HIGHEST_ACTIVE_REG;
6854 }
6855 else
6856 { /* We must scan for the new highest active register, since
6857 it isn't necessarily one less than now: consider
6858 (a(b)c(d(e)f)g). When group 3 ends, after the f), the
6859 new highest active register is 1. */
6860 UCHAR_T r = *p - 1;
6861 while (r > 0 && !IS_ACTIVE (reg_info[r]))
6862 r--;
6863
6864 /* If we end up at register zero, that means that we saved
6865 the registers as the result of an `on_failure_jump', not
6866 a `start_memory', and we jumped to past the innermost
6867 `stop_memory'. For example, in ((.)*) we save
6868 registers 1 and 2 as a result of the *, but when we pop
6869 back to the second ), we are at the stop_memory 1.
6870 Thus, nothing is active. */
6871 if (r == 0)
6872 {
6873 lowest_active_reg = NO_LOWEST_ACTIVE_REG;
6874 highest_active_reg = NO_HIGHEST_ACTIVE_REG;
6875 }
6876 else
6877 highest_active_reg = r;
6878 }
6879
6880 /* If just failed to match something this time around with a
6881 group that's operated on by a repetition operator, try to
6882 force exit from the ``loop'', and restore the register
6883 information for this group that we had before trying this
6884 last match. */
6885 if ((!MATCHED_SOMETHING (reg_info[*p])
6886 || just_past_start_mem == p - 1)
6887 && (p + 2) < pend)
6888 {
6889 boolean is_a_jump_n = false;
6890
6891 p1 = p + 2;
6892 mcnt = 0;
6893 switch ((re_opcode_t) *p1++)
6894 {
6895 case jump_n:
6896 is_a_jump_n = true;
6897 case pop_failure_jump:
6898 case maybe_pop_jump:
6899 case jump:
6900 case dummy_failure_jump:
6901 EXTRACT_NUMBER_AND_INCR (mcnt, p1);
6902 if (is_a_jump_n)
6903 p1 += OFFSET_ADDRESS_SIZE;
6904 break;
6905
6906 default:
6907 /* do nothing */ ;
6908 }
6909 p1 += mcnt;
6910
6911 /* If the next operation is a jump backwards in the pattern
6912 to an on_failure_jump right before the start_memory
6913 corresponding to this stop_memory, exit from the loop
6914 by forcing a failure after pushing on the stack the
6915 on_failure_jump's jump in the pattern, and d. */
6916 if (mcnt < 0 && (re_opcode_t) *p1 == on_failure_jump
6917 && (re_opcode_t) p1[1+OFFSET_ADDRESS_SIZE] == start_memory
6918 && p1[2+OFFSET_ADDRESS_SIZE] == *p)
6919 {
6920 /* If this group ever matched anything, then restore
6921 what its registers were before trying this last
6922 failed match, e.g., with `(a*)*b' against `ab' for
6923 regstart[1], and, e.g., with `((a*)*(b*)*)*'
6924 against `aba' for regend[3].
6925
6926 Also restore the registers for inner groups for,
6927 e.g., `((a*)(b*))*' against `aba' (register 3 would
6928 otherwise get trashed). */
6929
6930 if (EVER_MATCHED_SOMETHING (reg_info[*p]))
6931 {
6932 unsigned r;
6933
6934 EVER_MATCHED_SOMETHING (reg_info[*p]) = 0;
6935
6936 /* Restore this and inner groups' (if any) registers. */
6937 for (r = *p; r < (unsigned) *p + (unsigned) *(p + 1);
6938 r++)
6939 {
6940 regstart[r] = old_regstart[r];
6941
6942 /* xx why this test? */
6943 if (old_regend[r] >= regstart[r])
6944 regend[r] = old_regend[r];
6945 }
6946 }
6947 p1++;
6948 EXTRACT_NUMBER_AND_INCR (mcnt, p1);
6949 PUSH_FAILURE_POINT (p1 + mcnt, d, -2);
6950
6951 goto fail;
6952 }
6953 }
6954
6955 /* Move past the register number and the inner group count. */
6956 p += 2;
6957 NEXT;
6958
6959
6960 /* \<digit> has been turned into a `duplicate' command which is
6961 followed by the numeric value of <digit> as the register number. */
6962 CASE (duplicate):
6963 {
6964 register const CHAR_T *d2, *dend2;
6965 int regno = *p++; /* Get which register to match against. */
6966 DEBUG_PRINT2 ("EXECUTING duplicate %d.\n", regno);
6967
6968 /* Can't back reference a group which we've never matched. */
6969 if (REG_UNSET (regstart[regno]) || REG_UNSET (regend[regno]))
6970 goto fail;
6971
6972 /* Where in input to try to start matching. */
6973 d2 = regstart[regno];
6974
6975 /* Where to stop matching; if both the place to start and
6976 the place to stop matching are in the same string, then
6977 set to the place to stop, otherwise, for now have to use
6978 the end of the first string. */
6979
6980 dend2 = ((FIRST_STRING_P (regstart[regno])
6981 == FIRST_STRING_P (regend[regno]))
6982 ? regend[regno] : end_match_1);
6983 for (;;)
6984 {
6985 /* If necessary, advance to next segment in register
6986 contents. */
6987 while (d2 == dend2)
6988 {
6989 if (dend2 == end_match_2) break;
6990 if (dend2 == regend[regno]) break;
6991
6992 /* End of string1 => advance to string2. */
6993 d2 = string2;
6994 dend2 = regend[regno];
6995 }
6996 /* At end of register contents => success */
6997 if (d2 == dend2) break;
6998
6999 /* If necessary, advance to next segment in data. */
7000 PREFETCH ();
7001
7002 /* How many characters left in this segment to match. */
7003 mcnt = dend - d;
7004
7005 /* Want how many consecutive characters we can match in
7006 one shot, so, if necessary, adjust the count. */
7007 if (mcnt > dend2 - d2)
7008 mcnt = dend2 - d2;
7009
7010 /* Compare that many; failure if mismatch, else move
7011 past them. */
7012 if (translate
7013 ? PREFIX(bcmp_translate) (d, d2, mcnt, translate)
7014 : memcmp (d, d2, mcnt*sizeof(UCHAR_T)))
7015 goto fail;
7016 d += mcnt, d2 += mcnt;
7017
7018 /* Do this because we've match some characters. */
7019 SET_REGS_MATCHED ();
7020 }
7021 }
7022 NEXT;
7023
7024
7025 /* begline matches the empty string at the beginning of the string
7026 (unless `not_bol' is set in `bufp'), and, if
7027 `newline_anchor' is set, after newlines. */
7028 CASE (begline):
7029 DEBUG_PRINT1 ("EXECUTING begline.\n");
7030
7031 if (AT_STRINGS_BEG (d))
7032 {
7033 if (!bufp->not_bol)
7034 {
7035 NEXT;
7036 }
7037 }
7038 else if (d[-1] == '\n' && bufp->newline_anchor)
7039 {
7040 NEXT;
7041 }
7042 /* In all other cases, we fail. */
7043 goto fail;
7044
7045
7046 /* endline is the dual of begline. */
7047 CASE (endline):
7048 DEBUG_PRINT1 ("EXECUTING endline.\n");
7049
7050 if (AT_STRINGS_END (d))
7051 {
7052 if (!bufp->not_eol)
7053 {
7054 NEXT;
7055 }
7056 }
7057
7058 /* We have to ``prefetch'' the next character. */
7059 else if ((d == end1 ? *string2 : *d) == '\n'
7060 && bufp->newline_anchor)
7061 {
7062 NEXT;
7063 }
7064 goto fail;
7065
7066
7067 /* Match at the very beginning of the data. */
7068 CASE (begbuf):
7069 DEBUG_PRINT1 ("EXECUTING begbuf.\n");
7070 if (AT_STRINGS_BEG (d))
7071 {
7072 NEXT;
7073 }
7074 goto fail;
7075
7076
7077 /* Match at the very end of the data. */
7078 CASE (endbuf):
7079 DEBUG_PRINT1 ("EXECUTING endbuf.\n");
7080 if (AT_STRINGS_END (d))
7081 {
7082 NEXT;
7083 }
7084 goto fail;
7085
7086
7087 /* on_failure_keep_string_jump is used to optimize `.*\n'. It
7088 pushes NULL as the value for the string on the stack. Then
7089 `pop_failure_point' will keep the current value for the
7090 string, instead of restoring it. To see why, consider
7091 matching `foo\nbar' against `.*\n'. The .* matches the foo;
7092 then the . fails against the \n. But the next thing we want
7093 to do is match the \n against the \n; if we restored the
7094 string value, we would be back at the foo.
7095
7096 Because this is used only in specific cases, we don't need to
7097 check all the things that `on_failure_jump' does, to make
7098 sure the right things get saved on the stack. Hence we don't
7099 share its code. The only reason to push anything on the
7100 stack at all is that otherwise we would have to change
7101 `anychar's code to do something besides goto fail in this
7102 case; that seems worse than this. */
7103 CASE (on_failure_keep_string_jump):
7104 DEBUG_PRINT1 ("EXECUTING on_failure_keep_string_jump");
7105
7106 EXTRACT_NUMBER_AND_INCR (mcnt, p);
7107#ifdef _LIBC
7108 DEBUG_PRINT3 (" %d (to %p):\n", mcnt, p + mcnt);
7109#else
7110 DEBUG_PRINT3 (" %d (to 0x%x):\n", mcnt, p + mcnt);
7111#endif
7112
7113 PUSH_FAILURE_POINT (p + mcnt, NULL, -2);
7114 NEXT;
7115
7116
7117 /* Uses of on_failure_jump:
7118
7119 Each alternative starts with an on_failure_jump that points
7120 to the beginning of the next alternative. Each alternative
7121 except the last ends with a jump that in effect jumps past
7122 the rest of the alternatives. (They really jump to the
7123 ending jump of the following alternative, because tensioning
7124 these jumps is a hassle.)
7125
7126 Repeats start with an on_failure_jump that points past both
7127 the repetition text and either the following jump or
7128 pop_failure_jump back to this on_failure_jump. */
7129 CASE (on_failure_jump):
7130 on_failure:
7131 DEBUG_PRINT1 ("EXECUTING on_failure_jump");
7132
7133 EXTRACT_NUMBER_AND_INCR (mcnt, p);
7134#ifdef _LIBC
7135 DEBUG_PRINT3 (" %d (to %p)", mcnt, p + mcnt);
7136#else
7137 DEBUG_PRINT3 (" %d (to 0x%x)", mcnt, p + mcnt);
7138#endif
7139
7140 /* If this on_failure_jump comes right before a group (i.e.,
7141 the original * applied to a group), save the information
7142 for that group and all inner ones, so that if we fail back
7143 to this point, the group's information will be correct.
7144 For example, in \(a*\)*\1, we need the preceding group,
7145 and in \(zz\(a*\)b*\)\2, we need the inner group. */
7146
7147 /* We can't use `p' to check ahead because we push
7148 a failure point to `p + mcnt' after we do this. */
7149 p1 = p;
7150
7151 /* We need to skip no_op's before we look for the
7152 start_memory in case this on_failure_jump is happening as
7153 the result of a completed succeed_n, as in \(a\)\{1,3\}b\1
7154 against aba. */
7155 while (p1 < pend && (re_opcode_t) *p1 == no_op)
7156 p1++;
7157
7158 if (p1 < pend && (re_opcode_t) *p1 == start_memory)
7159 {
7160 /* We have a new highest active register now. This will
7161 get reset at the start_memory we are about to get to,
7162 but we will have saved all the registers relevant to
7163 this repetition op, as described above. */
7164 highest_active_reg = *(p1 + 1) + *(p1 + 2);
7165 if (lowest_active_reg == NO_LOWEST_ACTIVE_REG)
7166 lowest_active_reg = *(p1 + 1);
7167 }
7168
7169 DEBUG_PRINT1 (":\n");
7170 PUSH_FAILURE_POINT (p + mcnt, d, -2);
7171 NEXT;
7172
7173
7174 /* A smart repeat ends with `maybe_pop_jump'.
7175 We change it to either `pop_failure_jump' or `jump'. */
7176 CASE (maybe_pop_jump):
7177 EXTRACT_NUMBER_AND_INCR (mcnt, p);
7178 DEBUG_PRINT2 ("EXECUTING maybe_pop_jump %d.\n", mcnt);
7179 {
7180 register UCHAR_T *p2 = p;
7181
7182 /* Compare the beginning of the repeat with what in the
7183 pattern follows its end. If we can establish that there
7184 is nothing that they would both match, i.e., that we
7185 would have to backtrack because of (as in, e.g., `a*a')
7186 then we can change to pop_failure_jump, because we'll
7187 never have to backtrack.
7188
7189 This is not true in the case of alternatives: in
7190 `(a|ab)*' we do need to backtrack to the `ab' alternative
7191 (e.g., if the string was `ab'). But instead of trying to
7192 detect that here, the alternative has put on a dummy
7193 failure point which is what we will end up popping. */
7194
7195 /* Skip over open/close-group commands.
7196 If what follows this loop is a ...+ construct,
7197 look at what begins its body, since we will have to
7198 match at least one of that. */
7199 while (1)
7200 {
7201 if (p2 + 2 < pend
7202 && ((re_opcode_t) *p2 == stop_memory
7203 || (re_opcode_t) *p2 == start_memory))
7204 p2 += 3;
7205 else if (p2 + 2 + 2 * OFFSET_ADDRESS_SIZE < pend
7206 && (re_opcode_t) *p2 == dummy_failure_jump)
7207 p2 += 2 + 2 * OFFSET_ADDRESS_SIZE;
7208 else
7209 break;
7210 }
7211
7212 p1 = p + mcnt;
7213 /* p1[0] ... p1[2] are the `on_failure_jump' corresponding
7214 to the `maybe_finalize_jump' of this case. Examine what
7215 follows. */
7216
7217 /* If we're at the end of the pattern, we can change. */
7218 if (p2 == pend)
7219 {
7220 /* Consider what happens when matching ":\(.*\)"
7221 against ":/". I don't really understand this code
7222 yet. */
7223 p[-(1+OFFSET_ADDRESS_SIZE)] = (UCHAR_T)
7224 pop_failure_jump;
7225 DEBUG_PRINT1
7226 (" End of pattern: change to `pop_failure_jump'.\n");
7227 }
7228
7229 else if ((re_opcode_t) *p2 == exactn
7230#ifdef MBS_SUPPORT
7231 || (re_opcode_t) *p2 == exactn_bin
7232#endif
7233 || (bufp->newline_anchor && (re_opcode_t) *p2 == endline))
7234 {
7235 register UCHAR_T c
7236 = *p2 == (UCHAR_T) endline ? '\n' : p2[2];
7237
7238 if (((re_opcode_t) p1[1+OFFSET_ADDRESS_SIZE] == exactn
7239#ifdef MBS_SUPPORT
7240 || (re_opcode_t) p1[1+OFFSET_ADDRESS_SIZE] == exactn_bin
7241#endif
7242 ) && p1[3+OFFSET_ADDRESS_SIZE] != c)
7243 {
7244 p[-(1+OFFSET_ADDRESS_SIZE)] = (UCHAR_T)
7245 pop_failure_jump;
7246#ifdef WCHAR
7247 DEBUG_PRINT3 (" %C != %C => pop_failure_jump.\n",
7248 (wint_t) c,
7249 (wint_t) p1[3+OFFSET_ADDRESS_SIZE]);
7250#else
7251 DEBUG_PRINT3 (" %c != %c => pop_failure_jump.\n",
7252 (char) c,
7253 (char) p1[3+OFFSET_ADDRESS_SIZE]);
7254#endif
7255 }
7256
7257#ifndef WCHAR
7258 else if ((re_opcode_t) p1[3] == charset
7259 || (re_opcode_t) p1[3] == charset_not)
7260 {
7261 int not = (re_opcode_t) p1[3] == charset_not;
7262
7263 if (c < (unsigned) (p1[4] * BYTEWIDTH)
7264 && p1[5 + c / BYTEWIDTH] & (1 << (c % BYTEWIDTH)))
7265 not = !not;
7266
7267 /* `not' is equal to 1 if c would match, which means
7268 that we can't change to pop_failure_jump. */
7269 if (!not)
7270 {
7271 p[-3] = (unsigned char) pop_failure_jump;
7272 DEBUG_PRINT1 (" No match => pop_failure_jump.\n");
7273 }
7274 }
7275#endif /* not WCHAR */
7276 }
7277#ifndef WCHAR
7278 else if ((re_opcode_t) *p2 == charset)
7279 {
7280 /* We win if the first character of the loop is not part
7281 of the charset. */
7282 if ((re_opcode_t) p1[3] == exactn
7283 && ! ((int) p2[1] * BYTEWIDTH > (int) p1[5]
7284 && (p2[2 + p1[5] / BYTEWIDTH]
7285 & (1 << (p1[5] % BYTEWIDTH)))))
7286 {
7287 p[-3] = (unsigned char) pop_failure_jump;
7288 DEBUG_PRINT1 (" No match => pop_failure_jump.\n");
7289 }
7290
7291 else if ((re_opcode_t) p1[3] == charset_not)
7292 {
7293 int idx;
7294 /* We win if the charset_not inside the loop
7295 lists every character listed in the charset after. */
7296 for (idx = 0; idx < (int) p2[1]; idx++)
7297 if (! (p2[2 + idx] == 0
7298 || (idx < (int) p1[4]
7299 && ((p2[2 + idx] & ~ p1[5 + idx]) == 0))))
7300 break;
7301
7302 if (idx == p2[1])
7303 {
7304 p[-3] = (unsigned char) pop_failure_jump;
7305 DEBUG_PRINT1 (" No match => pop_failure_jump.\n");
7306 }
7307 }
7308 else if ((re_opcode_t) p1[3] == charset)
7309 {
7310 int idx;
7311 /* We win if the charset inside the loop
7312 has no overlap with the one after the loop. */
7313 for (idx = 0;
7314 idx < (int) p2[1] && idx < (int) p1[4];
7315 idx++)
7316 if ((p2[2 + idx] & p1[5 + idx]) != 0)
7317 break;
7318
7319 if (idx == p2[1] || idx == p1[4])
7320 {
7321 p[-3] = (unsigned char) pop_failure_jump;
7322 DEBUG_PRINT1 (" No match => pop_failure_jump.\n");
7323 }
7324 }
7325 }
7326#endif /* not WCHAR */
7327 }
7328 p -= OFFSET_ADDRESS_SIZE; /* Point at relative address again. */
7329 if ((re_opcode_t) p[-1] != pop_failure_jump)
7330 {
7331 p[-1] = (UCHAR_T) jump;
7332 DEBUG_PRINT1 (" Match => jump.\n");
7333 goto unconditional_jump;
7334 }
7335 /* Note fall through. */
7336
7337
7338 /* The end of a simple repeat has a pop_failure_jump back to
7339 its matching on_failure_jump, where the latter will push a
7340 failure point. The pop_failure_jump takes off failure
7341 points put on by this pop_failure_jump's matching
7342 on_failure_jump; we got through the pattern to here from the
7343 matching on_failure_jump, so didn't fail. */
7344 CASE (pop_failure_jump):
7345 {
7346 /* We need to pass separate storage for the lowest and
7347 highest registers, even though we don't care about the
7348 actual values. Otherwise, we will restore only one
7349 register from the stack, since lowest will == highest in
7350 `pop_failure_point'. */
7351 active_reg_t dummy_low_reg, dummy_high_reg;
7352 UCHAR_T *pdummy = NULL;
7353 const CHAR_T *sdummy = NULL;
7354
7355 DEBUG_PRINT1 ("EXECUTING pop_failure_jump.\n");
7356 POP_FAILURE_POINT (sdummy, pdummy,
7357 dummy_low_reg, dummy_high_reg,
7358 reg_dummy, reg_dummy, reg_info_dummy);
7359 }
7360 /* Note fall through. */
7361
7362 unconditional_jump:
7363#ifdef _LIBC
7364 DEBUG_PRINT2 ("\n%p: ", p);
7365#else
7366 DEBUG_PRINT2 ("\n0x%x: ", p);
7367#endif
7368 /* Note fall through. */
7369
7370 /* Unconditionally jump (without popping any failure points). */
7371 CASE (jump):
7372 EXTRACT_NUMBER_AND_INCR (mcnt, p); /* Get the amount to jump. */
7373 DEBUG_PRINT2 ("EXECUTING jump %d ", mcnt);
7374 p += mcnt; /* Do the jump. */
7375#ifdef _LIBC
7376 DEBUG_PRINT2 ("(to %p).\n", p);
7377#else
7378 DEBUG_PRINT2 ("(to 0x%x).\n", p);
7379#endif
7380 NEXT;
7381
7382
7383 /* We need this opcode so we can detect where alternatives end
7384 in `group_match_null_string_p' et al. */
7385 CASE (jump_past_alt):
7386 DEBUG_PRINT1 ("EXECUTING jump_past_alt.\n");
7387 goto unconditional_jump;
7388
7389
7390 /* Normally, the on_failure_jump pushes a failure point, which
7391 then gets popped at pop_failure_jump. We will end up at
7392 pop_failure_jump, also, and with a pattern of, say, `a+', we
7393 are skipping over the on_failure_jump, so we have to push
7394 something meaningless for pop_failure_jump to pop. */
7395 CASE (dummy_failure_jump):
7396 DEBUG_PRINT1 ("EXECUTING dummy_failure_jump.\n");
7397 /* It doesn't matter what we push for the string here. What
7398 the code at `fail' tests is the value for the pattern. */
7399 PUSH_FAILURE_POINT (NULL, NULL, -2);
7400 goto unconditional_jump;
7401
7402
7403 /* At the end of an alternative, we need to push a dummy failure
7404 point in case we are followed by a `pop_failure_jump', because
7405 we don't want the failure point for the alternative to be
7406 popped. For example, matching `(a|ab)*' against `aab'
7407 requires that we match the `ab' alternative. */
7408 CASE (push_dummy_failure):
7409 DEBUG_PRINT1 ("EXECUTING push_dummy_failure.\n");
7410 /* See comments just above at `dummy_failure_jump' about the
7411 two zeroes. */
7412 PUSH_FAILURE_POINT (NULL, NULL, -2);
7413 NEXT;
7414
7415 /* Have to succeed matching what follows at least n times.
7416 After that, handle like `on_failure_jump'. */
7417 CASE (succeed_n):
7418 EXTRACT_NUMBER (mcnt, p + OFFSET_ADDRESS_SIZE);
7419 DEBUG_PRINT2 ("EXECUTING succeed_n %d.\n", mcnt);
7420
7421 assert (mcnt >= 0);
7422 /* Originally, this is how many times we HAVE to succeed. */
7423 if (mcnt > 0)
7424 {
7425 mcnt--;
7426 p += OFFSET_ADDRESS_SIZE;
7427 STORE_NUMBER_AND_INCR (p, mcnt);
7428#ifdef _LIBC
7429 DEBUG_PRINT3 (" Setting %p to %d.\n", p - OFFSET_ADDRESS_SIZE
7430 , mcnt);
7431#else
7432 DEBUG_PRINT3 (" Setting 0x%x to %d.\n", p - OFFSET_ADDRESS_SIZE
7433 , mcnt);
7434#endif
7435 }
7436 else if (mcnt == 0)
7437 {
7438#ifdef _LIBC
7439 DEBUG_PRINT2 (" Setting two bytes from %p to no_op.\n",
7440 p + OFFSET_ADDRESS_SIZE);
7441#else
7442 DEBUG_PRINT2 (" Setting two bytes from 0x%x to no_op.\n",
7443 p + OFFSET_ADDRESS_SIZE);
7444#endif /* _LIBC */
7445
7446#ifdef WCHAR
7447 p[1] = (UCHAR_T) no_op;
7448#else
7449 p[2] = (UCHAR_T) no_op;
7450 p[3] = (UCHAR_T) no_op;
7451#endif /* WCHAR */
7452 goto on_failure;
7453 }
7454 NEXT;
7455
7456 CASE (jump_n):
7457 EXTRACT_NUMBER (mcnt, p + OFFSET_ADDRESS_SIZE);
7458 DEBUG_PRINT2 ("EXECUTING jump_n %d.\n", mcnt);
7459
7460 /* Originally, this is how many times we CAN jump. */
7461 if (mcnt)
7462 {
7463 mcnt--;
7464 STORE_NUMBER (p + OFFSET_ADDRESS_SIZE, mcnt);
7465
7466#ifdef _LIBC
7467 DEBUG_PRINT3 (" Setting %p to %d.\n", p + OFFSET_ADDRESS_SIZE,
7468 mcnt);
7469#else
7470 DEBUG_PRINT3 (" Setting 0x%x to %d.\n", p + OFFSET_ADDRESS_SIZE,
7471 mcnt);
7472#endif /* _LIBC */
7473 goto unconditional_jump;
7474 }
7475 /* If don't have to jump any more, skip over the rest of command. */
7476 else
7477 p += 2 * OFFSET_ADDRESS_SIZE;
7478 NEXT;
7479
7480 CASE (set_number_at):
7481 {
7482 DEBUG_PRINT1 ("EXECUTING set_number_at.\n");
7483
7484 EXTRACT_NUMBER_AND_INCR (mcnt, p);
7485 p1 = p + mcnt;
7486 EXTRACT_NUMBER_AND_INCR (mcnt, p);
7487#ifdef _LIBC
7488 DEBUG_PRINT3 (" Setting %p to %d.\n", p1, mcnt);
7489#else
7490 DEBUG_PRINT3 (" Setting 0x%x to %d.\n", p1, mcnt);
7491#endif
7492 STORE_NUMBER (p1, mcnt);
7493 NEXT;
7494 }
7495
7496#if 0
7497 /* The DEC Alpha C compiler 3.x generates incorrect code for the
7498 test WORDCHAR_P (d - 1) != WORDCHAR_P (d) in the expansion of
7499 AT_WORD_BOUNDARY, so this code is disabled. Expanding the
7500 macro and introducing temporary variables works around the bug. */
7501
7502 CASE (wordbound):
7503 DEBUG_PRINT1 ("EXECUTING wordbound.\n");
7504 if (AT_WORD_BOUNDARY (d))
7505 {
7506 NEXT;
7507 }
7508 goto fail;
7509
7510 CASE (notwordbound):
7511 DEBUG_PRINT1 ("EXECUTING notwordbound.\n");
7512 if (AT_WORD_BOUNDARY (d))
7513 goto fail;
7514 NEXT;
7515#else
7516 CASE (wordbound):
7517 {
7518 boolean prevchar, thischar;
7519
7520 DEBUG_PRINT1 ("EXECUTING wordbound.\n");
7521 if (AT_STRINGS_BEG (d) || AT_STRINGS_END (d))
7522 {
7523 NEXT;
7524 }
7525
7526 prevchar = WORDCHAR_P (d - 1);
7527 thischar = WORDCHAR_P (d);
7528 if (prevchar != thischar)
7529 {
7530 NEXT;
7531 }
7532 goto fail;
7533 }
7534
7535 CASE (notwordbound):
7536 {
7537 boolean prevchar, thischar;
7538
7539 DEBUG_PRINT1 ("EXECUTING notwordbound.\n");
7540 if (AT_STRINGS_BEG (d) || AT_STRINGS_END (d))
7541 goto fail;
7542
7543 prevchar = WORDCHAR_P (d - 1);
7544 thischar = WORDCHAR_P (d);
7545 if (prevchar != thischar)
7546 goto fail;
7547 NEXT;
7548 }
7549#endif
7550
7551 CASE (wordbeg):
7552 DEBUG_PRINT1 ("EXECUTING wordbeg.\n");
7553 if (!AT_STRINGS_END (d) && WORDCHAR_P (d)
7554 && (AT_STRINGS_BEG (d) || !WORDCHAR_P (d - 1)))
7555 {
7556 NEXT;
7557 }
7558 goto fail;
7559
7560 CASE (wordend):
7561 DEBUG_PRINT1 ("EXECUTING wordend.\n");
7562 if (!AT_STRINGS_BEG (d) && WORDCHAR_P (d - 1)
7563 && (AT_STRINGS_END (d) || !WORDCHAR_P (d)))
7564 {
7565 NEXT;
7566 }
7567 goto fail;
7568
7569#ifdef emacs
7570 CASE (before_dot):
7571 DEBUG_PRINT1 ("EXECUTING before_dot.\n");
7572 if (PTR_CHAR_POS ((unsigned char *) d) >= point)
7573 goto fail;
7574 NEXT;
7575
7576 CASE (at_dot):
7577 DEBUG_PRINT1 ("EXECUTING at_dot.\n");
7578 if (PTR_CHAR_POS ((unsigned char *) d) != point)
7579 goto fail;
7580 NEXT;
7581
7582 CASE (after_dot):
7583 DEBUG_PRINT1 ("EXECUTING after_dot.\n");
7584 if (PTR_CHAR_POS ((unsigned char *) d) <= point)
7585 goto fail;
7586 NEXT;
7587
7588 CASE (syntaxspec):
7589 DEBUG_PRINT2 ("EXECUTING syntaxspec %d.\n", mcnt);
7590 mcnt = *p++;
7591 goto matchsyntax;
7592
7593 CASE (wordchar):
7594 DEBUG_PRINT1 ("EXECUTING Emacs wordchar.\n");
7595 mcnt = (int) Sword;
7596 matchsyntax:
7597 PREFETCH ();
7598 /* Can't use *d++ here; SYNTAX may be an unsafe macro. */
7599 d++;
7600 if (SYNTAX (d[-1]) != (enum syntaxcode) mcnt)
7601 goto fail;
7602 SET_REGS_MATCHED ();
7603 NEXT;
7604
7605 CASE (notsyntaxspec):
7606 DEBUG_PRINT2 ("EXECUTING notsyntaxspec %d.\n", mcnt);
7607 mcnt = *p++;
7608 goto matchnotsyntax;
7609
7610 CASE (notwordchar):
7611 DEBUG_PRINT1 ("EXECUTING Emacs notwordchar.\n");
7612 mcnt = (int) Sword;
7613 matchnotsyntax:
7614 PREFETCH ();
7615 /* Can't use *d++ here; SYNTAX may be an unsafe macro. */
7616 d++;
7617 if (SYNTAX (d[-1]) == (enum syntaxcode) mcnt)
7618 goto fail;
7619 SET_REGS_MATCHED ();
7620 NEXT;
7621
7622#else /* not emacs */
7623 CASE (wordchar):
7624 DEBUG_PRINT1 ("EXECUTING non-Emacs wordchar.\n");
7625 PREFETCH ();
7626 if (!WORDCHAR_P (d))
7627 goto fail;
7628 SET_REGS_MATCHED ();
7629 d++;
7630 NEXT;
7631
7632 CASE (notwordchar):
7633 DEBUG_PRINT1 ("EXECUTING non-Emacs notwordchar.\n");
7634 PREFETCH ();
7635 if (WORDCHAR_P (d))
7636 goto fail;
7637 SET_REGS_MATCHED ();
7638 d++;
7639 NEXT;
7640#endif /* not emacs */
7641
7642#ifndef __GNUC__
7643 default:
7644 abort ();
7645 }
7646 continue; /* Successfully executed one pattern command; keep going. */
7647#endif
7648
7649
7650 /* We goto here if a matching operation fails. */
7651 fail:
7652 if (!FAIL_STACK_EMPTY ())
7653 { /* A restart point is known. Restore to that state. */
7654 DEBUG_PRINT1 ("\nFAIL:\n");
7655 POP_FAILURE_POINT (d, p,
7656 lowest_active_reg, highest_active_reg,
7657 regstart, regend, reg_info);
7658
7659 /* If this failure point is a dummy, try the next one. */
7660 if (!p)
7661 goto fail;
7662
7663 /* If we failed to the end of the pattern, don't examine *p. */
7664 assert (p <= pend);
7665 if (p < pend)
7666 {
7667 boolean is_a_jump_n = false;
7668
7669 /* If failed to a backwards jump that's part of a repetition
7670 loop, need to pop this failure point and use the next one. */
7671 switch ((re_opcode_t) *p)
7672 {
7673 case jump_n:
7674 is_a_jump_n = true;
7675 case maybe_pop_jump:
7676 case pop_failure_jump:
7677 case jump:
7678 p1 = p + 1;
7679 EXTRACT_NUMBER_AND_INCR (mcnt, p1);
7680 p1 += mcnt;
7681
7682 if ((is_a_jump_n && (re_opcode_t) *p1 == succeed_n)
7683 || (!is_a_jump_n
7684 && (re_opcode_t) *p1 == on_failure_jump))
7685 goto fail;
7686 break;
7687 default:
7688 /* do nothing */ ;
7689 }
7690 }
7691
7692 if (d >= string1 && d <= end1)
7693 dend = end_match_1;
7694 }
7695 else
7696 break; /* Matching at this starting point really fails. */
7697 } /* for (;;) */
7698
7699 if (best_regs_set)
7700 goto restore_best_regs;
7701
7702 FREE_VARIABLES ();
7703
7704 return -1; /* Failure to match. */
7705} /* re_match_2 */
7706
7707
7708/* Subroutine definitions for re_match_2. */
7709
7710
7711/* We are passed P pointing to a register number after a start_memory.
7712
7713 Return true if the pattern up to the corresponding stop_memory can
7714 match the empty string, and false otherwise.
7715
7716 If we find the matching stop_memory, sets P to point to one past its number.
7717 Otherwise, sets P to an undefined byte less than or equal to END.
7718
7719 We don't handle duplicates properly (yet). */
7720
7721static boolean
7722PREFIX(group_match_null_string_p) (p, end, reg_info)
7723 UCHAR_T **p, *end;
7724 PREFIX(register_info_type) *reg_info;
7725{
7726 int mcnt;
7727 /* Point to after the args to the start_memory. */
7728 UCHAR_T *p1 = *p + 2;
7729
7730 while (p1 < end)
7731 {
7732 /* Skip over opcodes that can match nothing, and return true or
7733 false, as appropriate, when we get to one that can't, or to the
7734 matching stop_memory. */
7735
7736 switch ((re_opcode_t) *p1)
7737 {
7738 /* Could be either a loop or a series of alternatives. */
7739 case on_failure_jump:
7740 p1++;
7741 EXTRACT_NUMBER_AND_INCR (mcnt, p1);
7742
7743 /* If the next operation is not a jump backwards in the
7744 pattern. */
7745
7746 if (mcnt >= 0)
7747 {
7748 /* Go through the on_failure_jumps of the alternatives,
7749 seeing if any of the alternatives cannot match nothing.
7750 The last alternative starts with only a jump,
7751 whereas the rest start with on_failure_jump and end
7752 with a jump, e.g., here is the pattern for `a|b|c':
7753
7754 /on_failure_jump/0/6/exactn/1/a/jump_past_alt/0/6
7755 /on_failure_jump/0/6/exactn/1/b/jump_past_alt/0/3
7756 /exactn/1/c
7757
7758 So, we have to first go through the first (n-1)
7759 alternatives and then deal with the last one separately. */
7760
7761
7762 /* Deal with the first (n-1) alternatives, which start
7763 with an on_failure_jump (see above) that jumps to right
7764 past a jump_past_alt. */
7765
7766 while ((re_opcode_t) p1[mcnt-(1+OFFSET_ADDRESS_SIZE)] ==
7767 jump_past_alt)
7768 {
7769 /* `mcnt' holds how many bytes long the alternative
7770 is, including the ending `jump_past_alt' and
7771 its number. */
7772
7773 if (!PREFIX(alt_match_null_string_p) (p1, p1 + mcnt -
7774 (1 + OFFSET_ADDRESS_SIZE),
7775 reg_info))
7776 return false;
7777
7778 /* Move to right after this alternative, including the
7779 jump_past_alt. */
7780 p1 += mcnt;
7781
7782 /* Break if it's the beginning of an n-th alternative
7783 that doesn't begin with an on_failure_jump. */
7784 if ((re_opcode_t) *p1 != on_failure_jump)
7785 break;
7786
7787 /* Still have to check that it's not an n-th
7788 alternative that starts with an on_failure_jump. */
7789 p1++;
7790 EXTRACT_NUMBER_AND_INCR (mcnt, p1);
7791 if ((re_opcode_t) p1[mcnt-(1+OFFSET_ADDRESS_SIZE)] !=
7792 jump_past_alt)
7793 {
7794 /* Get to the beginning of the n-th alternative. */
7795 p1 -= 1 + OFFSET_ADDRESS_SIZE;
7796 break;
7797 }
7798 }
7799
7800 /* Deal with the last alternative: go back and get number
7801 of the `jump_past_alt' just before it. `mcnt' contains
7802 the length of the alternative. */
7803 EXTRACT_NUMBER (mcnt, p1 - OFFSET_ADDRESS_SIZE);
7804
7805 if (!PREFIX(alt_match_null_string_p) (p1, p1 + mcnt, reg_info))
7806 return false;
7807
7808 p1 += mcnt; /* Get past the n-th alternative. */
7809 } /* if mcnt > 0 */
7810 break;
7811
7812
7813 case stop_memory:
7814 assert (p1[1] == **p);
7815 *p = p1 + 2;
7816 return true;
7817
7818
7819 default:
7820 if (!PREFIX(common_op_match_null_string_p) (&p1, end, reg_info))
7821 return false;
7822 }
7823 } /* while p1 < end */
7824
7825 return false;
7826} /* group_match_null_string_p */
7827
7828
7829/* Similar to group_match_null_string_p, but doesn't deal with alternatives:
7830 It expects P to be the first byte of a single alternative and END one
7831 byte past the last. The alternative can contain groups. */
7832
7833static boolean
7834PREFIX(alt_match_null_string_p) (p, end, reg_info)
7835 UCHAR_T *p, *end;
7836 PREFIX(register_info_type) *reg_info;
7837{
7838 int mcnt;
7839 UCHAR_T *p1 = p;
7840
7841 while (p1 < end)
7842 {
7843 /* Skip over opcodes that can match nothing, and break when we get
7844 to one that can't. */
7845
7846 switch ((re_opcode_t) *p1)
7847 {
7848 /* It's a loop. */
7849 case on_failure_jump:
7850 p1++;
7851 EXTRACT_NUMBER_AND_INCR (mcnt, p1);
7852 p1 += mcnt;
7853 break;
7854
7855 default:
7856 if (!PREFIX(common_op_match_null_string_p) (&p1, end, reg_info))
7857 return false;
7858 }
7859 } /* while p1 < end */
7860
7861 return true;
7862} /* alt_match_null_string_p */
7863
7864
7865/* Deals with the ops common to group_match_null_string_p and
7866 alt_match_null_string_p.
7867
7868 Sets P to one after the op and its arguments, if any. */
7869
7870static boolean
7871PREFIX(common_op_match_null_string_p) (p, end, reg_info)
7872 UCHAR_T **p, *end;
7873 PREFIX(register_info_type) *reg_info;
7874{
7875 int mcnt;
7876 boolean ret;
7877 int reg_no;
7878 UCHAR_T *p1 = *p;
7879
7880 switch ((re_opcode_t) *p1++)
7881 {
7882 case no_op:
7883 case begline:
7884 case endline:
7885 case begbuf:
7886 case endbuf:
7887 case wordbeg:
7888 case wordend:
7889 case wordbound:
7890 case notwordbound:
7891#ifdef emacs
7892 case before_dot:
7893 case at_dot:
7894 case after_dot:
7895#endif
7896 break;
7897
7898 case start_memory:
7899 reg_no = *p1;
7900 assert (reg_no > 0 && reg_no <= MAX_REGNUM);
7901 ret = PREFIX(group_match_null_string_p) (&p1, end, reg_info);
7902
7903 /* Have to set this here in case we're checking a group which
7904 contains a group and a back reference to it. */
7905
7906 if (REG_MATCH_NULL_STRING_P (reg_info[reg_no]) == MATCH_NULL_UNSET_VALUE)
7907 REG_MATCH_NULL_STRING_P (reg_info[reg_no]) = ret;
7908
7909 if (!ret)
7910 return false;
7911 break;
7912
7913 /* If this is an optimized succeed_n for zero times, make the jump. */
7914 case jump:
7915 EXTRACT_NUMBER_AND_INCR (mcnt, p1);
7916 if (mcnt >= 0)
7917 p1 += mcnt;
7918 else
7919 return false;
7920 break;
7921
7922 case succeed_n:
7923 /* Get to the number of times to succeed. */
7924 p1 += OFFSET_ADDRESS_SIZE;
7925 EXTRACT_NUMBER_AND_INCR (mcnt, p1);
7926
7927 if (mcnt == 0)
7928 {
7929 p1 -= 2 * OFFSET_ADDRESS_SIZE;
7930 EXTRACT_NUMBER_AND_INCR (mcnt, p1);
7931 p1 += mcnt;
7932 }
7933 else
7934 return false;
7935 break;
7936
7937 case duplicate:
7938 if (!REG_MATCH_NULL_STRING_P (reg_info[*p1]))
7939 return false;
7940 break;
7941
7942 case set_number_at:
7943 p1 += 2 * OFFSET_ADDRESS_SIZE;
7944
7945 default:
7946 /* All other opcodes mean we cannot match the empty string. */
7947 return false;
7948 }
7949
7950 *p = p1;
7951 return true;
7952} /* common_op_match_null_string_p */
7953
7954
7955/* Return zero if TRANSLATE[S1] and TRANSLATE[S2] are identical for LEN
7956 bytes; nonzero otherwise. */
7957
7958static int
7959PREFIX(bcmp_translate) (s1, s2, len, translate)
7960 const CHAR_T *s1, *s2;
7961 register int len;
7962 RE_TRANSLATE_TYPE translate;
7963{
7964 register const UCHAR_T *p1 = (const UCHAR_T *) s1;
7965 register const UCHAR_T *p2 = (const UCHAR_T *) s2;
7966 while (len)
7967 {
7968#ifdef WCHAR
7969 if (((*p1<=0xff)?translate[*p1++]:*p1++)
7970 != ((*p2<=0xff)?translate[*p2++]:*p2++))
7971 return 1;
7972#else /* BYTE */
7973 if (translate[*p1++] != translate[*p2++]) return 1;
7974#endif /* WCHAR */
7975 len--;
7976 }
7977 return 0;
7978}
7979
7980
7981
7982#else /* not INSIDE_RECURSION */
7983
7984/* Entry points for GNU code. */
7985
7986/* re_compile_pattern is the GNU regular expression compiler: it
7987 compiles PATTERN (of length SIZE) and puts the result in BUFP.
7988 Returns 0 if the pattern was valid, otherwise an error string.
7989
7990 Assumes the `allocated' (and perhaps `buffer') and `translate' fields
7991 are set in BUFP on entry.
7992
7993 We call regex_compile to do the actual compilation. */
7994
7995const char *
7996re_compile_pattern (pattern, length, bufp)
7997 const char *pattern;
7998 size_t length;
7999 struct re_pattern_buffer *bufp;
8000{
8001 reg_errcode_t ret;
8002
8003 /* GNU code is written to assume at least RE_NREGS registers will be set
8004 (and at least one extra will be -1). */
8005 bufp->regs_allocated = REGS_UNALLOCATED;
8006
8007 /* And GNU code determines whether or not to get register information
8008 by passing null for the REGS argument to re_match, etc., not by
8009 setting no_sub. */
8010 bufp->no_sub = 0;
8011
8012 /* Match anchors at newline. */
8013 bufp->newline_anchor = 1;
8014
8015# ifdef MBS_SUPPORT
8016 if (MB_CUR_MAX != 1)
8017 ret = wcs_regex_compile (pattern, length, re_syntax_options, bufp);
8018 else
8019# endif
8020 ret = byte_regex_compile (pattern, length, re_syntax_options, bufp);
8021
8022 if (!ret)
8023 return NULL;
8024 return gettext (re_error_msgid + re_error_msgid_idx[(int) ret]);
8025}
8026#ifdef _LIBC
8027weak_alias (__re_compile_pattern, re_compile_pattern)
8028#endif
8029
8030
8031/* Entry points compatible with 4.2 BSD regex library. We don't define
8032 them unless specifically requested. */
8033
8034#if defined _REGEX_RE_COMP || defined _LIBC
8035
8036/* BSD has one and only one pattern buffer. */
8037static struct re_pattern_buffer re_comp_buf;
8038
8039char *
8040#ifdef _LIBC
8041/* Make these definitions weak in libc, so POSIX programs can redefine
8042 these names if they don't use our functions, and still use
8043 regcomp/regexec below without link errors. */
8044weak_function
8045#endif
8046re_comp (s)
8047 const char *s;
8048{
8049 reg_errcode_t ret;
8050
8051 if (!s)
8052 {
8053 if (!re_comp_buf.buffer)
8054 return gettext ("No previous regular expression");
8055 return 0;
8056 }
8057
8058 if (!re_comp_buf.buffer)
8059 {
8060 re_comp_buf.buffer = (unsigned char *) malloc (200);
8061 if (re_comp_buf.buffer == NULL)
8062 return (char *) gettext (re_error_msgid
8063 + re_error_msgid_idx[(int) REG_ESPACE]);
8064 re_comp_buf.allocated = 200;
8065
8066 re_comp_buf.fastmap = (char *) malloc (1 << BYTEWIDTH);
8067 if (re_comp_buf.fastmap == NULL)
8068 return (char *) gettext (re_error_msgid
8069 + re_error_msgid_idx[(int) REG_ESPACE]);
8070 }
8071
8072 /* Since `re_exec' always passes NULL for the `regs' argument, we
8073 don't need to initialize the pattern buffer fields which affect it. */
8074
8075 /* Match anchors at newlines. */
8076 re_comp_buf.newline_anchor = 1;
8077
8078# ifdef MBS_SUPPORT
8079 if (MB_CUR_MAX != 1)
8080 ret = wcs_regex_compile (s, strlen (s), re_syntax_options, &re_comp_buf);
8081 else
8082# endif
8083 ret = byte_regex_compile (s, strlen (s), re_syntax_options, &re_comp_buf);
8084
8085 if (!ret)
8086 return NULL;
8087
8088 /* Yes, we're discarding `const' here if !HAVE_LIBINTL. */
8089 return (char *) gettext (re_error_msgid + re_error_msgid_idx[(int) ret]);
8090}
8091
8092
8093int
8094#ifdef _LIBC
8095weak_function
8096#endif
8097re_exec (s)
8098 const char *s;
8099{
8100 const int len = strlen (s);
8101 return
8102 0 <= re_search (&re_comp_buf, s, len, 0, len, (struct re_registers *) 0);
8103}
8104
8105#endif /* _REGEX_RE_COMP */
8106
8107
8108/* POSIX.2 functions. Don't define these for Emacs. */
8109
8110#ifndef emacs
8111
8112/* regcomp takes a regular expression as a string and compiles it.
8113
8114 PREG is a regex_t *. We do not expect any fields to be initialized,
8115 since POSIX says we shouldn't. Thus, we set
8116
8117 `buffer' to the compiled pattern;
8118 `used' to the length of the compiled pattern;
8119 `syntax' to RE_SYNTAX_POSIX_EXTENDED if the
8120 REG_EXTENDED bit in CFLAGS is set; otherwise, to
8121 RE_SYNTAX_POSIX_BASIC;
8122 `newline_anchor' to REG_NEWLINE being set in CFLAGS;
8123 `fastmap' to an allocated space for the fastmap;
8124 `fastmap_accurate' to zero;
8125 `re_nsub' to the number of subexpressions in PATTERN.
8126
8127 PATTERN is the address of the pattern string.
8128
8129 CFLAGS is a series of bits which affect compilation.
8130
8131 If REG_EXTENDED is set, we use POSIX extended syntax; otherwise, we
8132 use POSIX basic syntax.
8133
8134 If REG_NEWLINE is set, then . and [^...] don't match newline.
8135 Also, regexec will try a match beginning after every newline.
8136
8137 If REG_ICASE is set, then we considers upper- and lowercase
8138 versions of letters to be equivalent when matching.
8139
8140 If REG_NOSUB is set, then when PREG is passed to regexec, that
8141 routine will report only success or failure, and nothing about the
8142 registers.
8143
8144 It returns 0 if it succeeds, nonzero if it doesn't. (See regex.h for
8145 the return codes and their meanings.) */
8146
8147int
8148regcomp (preg, pattern, cflags)
8149 regex_t *preg;
8150 const char *pattern;
8151 int cflags;
8152{
8153 reg_errcode_t ret;
8154 reg_syntax_t syntax
8155 = (cflags & REG_EXTENDED) ?
8156 RE_SYNTAX_POSIX_EXTENDED : RE_SYNTAX_POSIX_BASIC;
8157
8158 /* regex_compile will allocate the space for the compiled pattern. */
8159 preg->buffer = 0;
8160 preg->allocated = 0;
8161 preg->used = 0;
8162
8163 /* Try to allocate space for the fastmap. */
8164 preg->fastmap = (char *) malloc (1 << BYTEWIDTH);
8165
8166 if (cflags & REG_ICASE)
8167 {
8168 unsigned i;
8169
8170 preg->translate
8171 = (RE_TRANSLATE_TYPE) malloc (CHAR_SET_SIZE
8172 * sizeof (*(RE_TRANSLATE_TYPE)0));
8173 if (preg->translate == NULL)
8174 return (int) REG_ESPACE;
8175
8176 /* Map uppercase characters to corresponding lowercase ones. */
8177 for (i = 0; i < CHAR_SET_SIZE; i++)
8178 preg->translate[i] = ISUPPER (i) ? TOLOWER (i) : i;
8179 }
8180 else
8181 preg->translate = NULL;
8182
8183 /* If REG_NEWLINE is set, newlines are treated differently. */
8184 if (cflags & REG_NEWLINE)
8185 { /* REG_NEWLINE implies neither . nor [^...] match newline. */
8186 syntax &= ~RE_DOT_NEWLINE;
8187 syntax |= RE_HAT_LISTS_NOT_NEWLINE;
8188 /* It also changes the matching behavior. */
8189 preg->newline_anchor = 1;
8190 }
8191 else
8192 preg->newline_anchor = 0;
8193
8194 preg->no_sub = !!(cflags & REG_NOSUB);
8195
8196 /* POSIX says a null character in the pattern terminates it, so we
8197 can use strlen here in compiling the pattern. */
8198# ifdef MBS_SUPPORT
8199 if (MB_CUR_MAX != 1)
8200 ret = wcs_regex_compile (pattern, strlen (pattern), syntax, preg);
8201 else
8202# endif
8203 ret = byte_regex_compile (pattern, strlen (pattern), syntax, preg);
8204
8205 /* POSIX doesn't distinguish between an unmatched open-group and an
8206 unmatched close-group: both are REG_EPAREN. */
8207 if (ret == REG_ERPAREN) ret = REG_EPAREN;
8208
8209 if (ret == REG_NOERROR && preg->fastmap)
8210 {
8211 /* Compute the fastmap now, since regexec cannot modify the pattern
8212 buffer. */
8213 if (re_compile_fastmap (preg) == -2)
8214 {
8215 /* Some error occurred while computing the fastmap, just forget
8216 about it. */
8217 free (preg->fastmap);
8218 preg->fastmap = NULL;
8219 }
8220 }
8221
8222 return (int) ret;
8223}
8224#ifdef _LIBC
8225weak_alias (__regcomp, regcomp)
8226#endif
8227
8228
8229/* regexec searches for a given pattern, specified by PREG, in the
8230 string STRING.
8231
8232 If NMATCH is zero or REG_NOSUB was set in the cflags argument to
8233 `regcomp', we ignore PMATCH. Otherwise, we assume PMATCH has at
8234 least NMATCH elements, and we set them to the offsets of the
8235 corresponding matched substrings.
8236
8237 EFLAGS specifies `execution flags' which affect matching: if
8238 REG_NOTBOL is set, then ^ does not match at the beginning of the
8239 string; if REG_NOTEOL is set, then $ does not match at the end.
8240
8241 We return 0 if we find a match and REG_NOMATCH if not. */
8242
8243int
8244regexec (preg, string, nmatch, pmatch, eflags)
8245 const regex_t *preg;
8246 const char *string;
8247 size_t nmatch;
8248 regmatch_t pmatch[];
8249 int eflags;
8250{
8251 int ret;
8252 struct re_registers regs;
8253 regex_t private_preg;
8254 int len = strlen (string);
8255 boolean want_reg_info = !preg->no_sub && nmatch > 0;
8256
8257 private_preg = *preg;
8258
8259 private_preg.not_bol = !!(eflags & REG_NOTBOL);
8260 private_preg.not_eol = !!(eflags & REG_NOTEOL);
8261
8262 /* The user has told us exactly how many registers to return
8263 information about, via `nmatch'. We have to pass that on to the
8264 matching routines. */
8265 private_preg.regs_allocated = REGS_FIXED;
8266
8267 if (want_reg_info)
8268 {
8269 regs.num_regs = nmatch;
8270 regs.start = TALLOC (nmatch * 2, regoff_t);
8271 if (regs.start == NULL)
8272 return (int) REG_NOMATCH;
8273 regs.end = regs.start + nmatch;
8274 }
8275
8276 /* Perform the searching operation. */
8277 ret = re_search (&private_preg, string, len,
8278 /* start: */ 0, /* range: */ len,
8279 want_reg_info ? &regs : (struct re_registers *) 0);
8280
8281 /* Copy the register information to the POSIX structure. */
8282 if (want_reg_info)
8283 {
8284 if (ret >= 0)
8285 {
8286 unsigned r;
8287
8288 for (r = 0; r < nmatch; r++)
8289 {
8290 pmatch[r].rm_so = regs.start[r];
8291 pmatch[r].rm_eo = regs.end[r];
8292 }
8293 }
8294
8295 /* If we needed the temporary register info, free the space now. */
8296 free (regs.start);
8297 }
8298
8299 /* We want zero return to mean success, unlike `re_search'. */
8300 return ret >= 0 ? (int) REG_NOERROR : (int) REG_NOMATCH;
8301}
8302#ifdef _LIBC
8303weak_alias (__regexec, regexec)
8304#endif
8305
8306
8307/* Returns a message corresponding to an error code, ERRCODE, returned
8308 from either regcomp or regexec. We don't use PREG here. */
8309
8310size_t
8311regerror (errcode, preg, errbuf, errbuf_size)
8312 int errcode;
8313 const regex_t *preg;
8314 char *errbuf;
8315 size_t errbuf_size;
8316{
8317 const char *msg;
8318 size_t msg_size;
8319
8320 if (errcode < 0
8321 || errcode >= (int) (sizeof (re_error_msgid_idx)
8322 / sizeof (re_error_msgid_idx[0])))
8323 /* Only error codes returned by the rest of the code should be passed
8324 to this routine. If we are given anything else, or if other regex
8325 code generates an invalid error code, then the program has a bug.
8326 Dump core so we can fix it. */
8327 abort ();
8328
8329 msg = gettext (re_error_msgid + re_error_msgid_idx[errcode]);
8330
8331 msg_size = strlen (msg) + 1; /* Includes the null. */
8332
8333 if (errbuf_size != 0)
8334 {
8335 if (msg_size > errbuf_size)
8336 {
8337#if defined HAVE_MEMPCPY || defined _LIBC
8338 *((char *) __mempcpy (errbuf, msg, errbuf_size - 1)) = '\0';
8339#else
8340 memcpy (errbuf, msg, errbuf_size - 1);
8341 errbuf[errbuf_size - 1] = 0;
8342#endif
8343 }
8344 else
8345 memcpy (errbuf, msg, msg_size);
8346 }
8347
8348 return msg_size;
8349}
8350#ifdef _LIBC
8351weak_alias (__regerror, regerror)
8352#endif
8353
8354
8355/* Free dynamically allocated space used by PREG. */
8356
8357void
8358regfree (preg)
8359 regex_t *preg;
8360{
8361 if (preg->buffer != NULL)
8362 free (preg->buffer);
8363 preg->buffer = NULL;
8364
8365 preg->allocated = 0;
8366 preg->used = 0;
8367
8368 if (preg->fastmap != NULL)
8369 free (preg->fastmap);
8370 preg->fastmap = NULL;
8371 preg->fastmap_accurate = 0;
8372
8373 if (preg->translate != NULL)
8374 free (preg->translate);
8375 preg->translate = NULL;
8376}
8377#ifdef _LIBC
8378weak_alias (__regfree, regfree)
8379#endif
8380
8381#endif /* not emacs */
8382
8383#endif /* not INSIDE_RECURSION */
8384
8385
8386
8387#undef STORE_NUMBER
8388#undef STORE_NUMBER_AND_INCR
8389#undef EXTRACT_NUMBER
8390#undef EXTRACT_NUMBER_AND_INCR
8391
8392#undef DEBUG_PRINT_COMPILED_PATTERN
8393#undef DEBUG_PRINT_DOUBLE_STRING
8394
8395#undef INIT_FAIL_STACK
8396#undef RESET_FAIL_STACK
8397#undef DOUBLE_FAIL_STACK
8398#undef PUSH_PATTERN_OP
8399#undef PUSH_FAILURE_POINTER
8400#undef PUSH_FAILURE_INT
8401#undef PUSH_FAILURE_ELT
8402#undef POP_FAILURE_POINTER
8403#undef POP_FAILURE_INT
8404#undef POP_FAILURE_ELT
8405#undef DEBUG_PUSH
8406#undef DEBUG_POP
8407#undef PUSH_FAILURE_POINT
8408#undef POP_FAILURE_POINT
8409
8410#undef REG_UNSET_VALUE
8411#undef REG_UNSET
8412
8413#undef PATFETCH
8414#undef PATFETCH_RAW
8415#undef PATUNFETCH
8416#undef TRANSLATE
8417
8418#undef INIT_BUF_SIZE
8419#undef GET_BUFFER_SPACE
8420#undef BUF_PUSH
8421#undef BUF_PUSH_2
8422#undef BUF_PUSH_3
8423#undef STORE_JUMP
8424#undef STORE_JUMP2
8425#undef INSERT_JUMP
8426#undef INSERT_JUMP2
8427#undef EXTEND_BUFFER
8428#undef GET_UNSIGNED_NUMBER
8429#undef FREE_STACK_RETURN
8430
8431# undef POINTER_TO_OFFSET
8432# undef MATCHING_IN_FRST_STRING
8433# undef PREFETCH
8434# undef AT_STRINGS_BEG
8435# undef AT_STRINGS_END
8436# undef WORDCHAR_P
8437# undef FREE_VAR
8438# undef FREE_VARIABLES
8439# undef NO_HIGHEST_ACTIVE_REG
8440# undef NO_LOWEST_ACTIVE_REG
8441
8442# undef CHAR_T
8443# undef UCHAR_T
8444# undef COMPILED_BUFFER_VAR
8445# undef OFFSET_ADDRESS_SIZE
8446# undef CHAR_CLASS_SIZE
8447# undef PREFIX
8448# undef ARG_PREFIX
8449# undef PUT_CHAR
8450# undef BYTE
8451# undef WCHAR
8452
8453# define DEFINED_ONCE
Note: See TracBrowser for help on using the repository browser.