The SWORD Project  1.9.0.svnversion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
regex.c
Go to the documentation of this file.
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  This file is part of the GNU C Library.
7 
8  The GNU C Library is free software; you can redistribute it and/or
9  modify it under the terms of the GNU Lesser General Public
10  License as published by the Free Software Foundation; either
11  version 2.1 of the License, or (at your option) any later version.
12 
13  The GNU C Library is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  Lesser General Public License for more details.
17 
18  You should have received a copy of the GNU Lesser General Public
19  License along with the GNU C Library; if not, write to the Free
20  Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21  02111-1307 USA. */
22 
23 /* AIX requires this to be the first thing in the file. */
24 #if defined _AIX && !defined REGEX_MALLOC
25  #pragma alloca
26 #endif
27 
28 #undef _GNU_SOURCE
29 #define _GNU_SOURCE
30 
31 #ifdef HAVE_CONFIG_H
32 # include <config.h>
33 #endif
34 
35 #ifndef PARAMS
36 # if defined __GNUC__ || (defined __STDC__ && __STDC__)
37 # define PARAMS(args) args
38 # else
39 # define PARAMS(args) ()
40 # endif /* GCC. */
41 #endif /* Not PARAMS. */
42 
43 #ifndef INSIDE_RECURSION
44 
45 # if defined STDC_HEADERS && !defined emacs
46 # include <stddef.h>
47 # else
48 /* We need this for `regex.h', and perhaps for the Emacs include files. */
49 # include <sys/types.h>
50 # endif
51 
52 # define WIDE_CHAR_SUPPORT (HAVE_WCTYPE_H && HAVE_WCHAR_H && HAVE_BTOWC)
53 
54 /* For platform which support the ISO C amendement 1 functionality we
55  support user defined character classes. */
56 # if defined _LIBC || WIDE_CHAR_SUPPORT
57 /* Solaris 2.5 has a bug: <wchar.h> must be included before <wctype.h>. */
58 # include <wchar.h>
59 # include <wctype.h>
60 # endif
61 
62 # ifdef _LIBC
63 /* We have to keep the namespace clean. */
64 # define regfree(preg) __regfree (preg)
65 # define regexec(pr, st, nm, pm, ef) __regexec (pr, st, nm, pm, ef)
66 # define regcomp(preg, pattern, cflags) __regcomp (preg, pattern, cflags)
67 # define regerror(errcode, preg, errbuf, errbuf_size) \
68  __regerror(errcode, preg, errbuf, errbuf_size)
69 # define re_set_registers(bu, re, nu, st, en) \
70  __re_set_registers (bu, re, nu, st, en)
71 # define re_match_2(bufp, string1, size1, string2, size2, pos, regs, stop) \
72  __re_match_2 (bufp, string1, size1, string2, size2, pos, regs, stop)
73 # define re_match(bufp, string, size, pos, regs) \
74  __re_match (bufp, string, size, pos, regs)
75 # define re_search(bufp, string, size, startpos, range, regs) \
76  __re_search (bufp, string, size, startpos, range, regs)
77 # define re_compile_pattern(pattern, length, bufp) \
78  __re_compile_pattern (pattern, length, bufp)
79 # define re_set_syntax(syntax) __re_set_syntax (syntax)
80 # define re_search_2(bufp, st1, s1, st2, s2, startpos, range, regs, stop) \
81  __re_search_2 (bufp, st1, s1, st2, s2, startpos, range, regs, stop)
82 # define re_compile_fastmap(bufp) __re_compile_fastmap (bufp)
83 
84 # define btowc __btowc
85 
86 /* We are also using some library internals. */
87 # include <locale/localeinfo.h>
88 # include <locale/elem-hash.h>
89 # include <langinfo.h>
90 # include <locale/coll-lookup.h>
91 # endif
92 
93 /* This is for other GNU distributions with internationalized messages. */
94 # if (HAVE_LIBINTL_H && ENABLE_NLS) || defined _LIBC
95 # include <libintl.h>
96 # ifdef _LIBC
97 # undef gettext
98 # define gettext(msgid) __dcgettext ("libc", msgid, LC_MESSAGES)
99 # endif
100 # else
101 # define gettext(msgid) (msgid)
102 # endif
103 
104 # ifndef gettext_noop
105 /* This define is so xgettext can find the internationalizable
106  strings. */
107 # define gettext_noop(String) String
108 # endif
109 
110 /* The `emacs' switch turns on certain matching commands
111  that make sense only in Emacs. */
112 # ifdef emacs
113 
114 # include "lisp.h"
115 # include "buffer.h"
116 # include "syntax.h"
117 
118 # else /* not emacs */
119 
120 /* If we are not linking with Emacs proper,
121  we can't use the relocating allocator
122  even if config.h says that we can. */
123 # undef REL_ALLOC
124 
125 # if defined STDC_HEADERS || defined _LIBC
126 # include <stdlib.h>
127 # else
128 char *malloc ();
129 char *realloc ();
130 # endif
131 
132 /* When used in Emacs's lib-src, we need to get bzero and bcopy somehow.
133  If nothing else has been done, use the method below. */
134 # ifdef INHIBIT_STRING_HEADER
135 # if !(defined HAVE_BZERO && defined HAVE_BCOPY)
136 # if !defined bzero && !defined bcopy
137 # undef INHIBIT_STRING_HEADER
138 # endif
139 # endif
140 # endif
141 
142 /* This is the normal way of making sure we have a bcopy and a bzero.
143  This is used in most programs--a few other programs avoid this
144  by defining INHIBIT_STRING_HEADER. */
145 #define HAVE_STRING_H // for SWORD Project
146 # ifndef INHIBIT_STRING_HEADER
147 # if defined HAVE_STRING_H || defined STDC_HEADERS || defined _LIBC
148 # include <string.h>
149 # ifndef bzero
150 # ifndef _LIBC
151 # define bzero(s, n) (memset (s, '\0', n), (s))
152 # else
153 # define bzero(s, n) __bzero (s, n)
154 # endif
155 # endif
156 # else
157 # include <strings.h>
158 # ifndef memcmp
159 # define memcmp(s1, s2, n) bcmp (s1, s2, n)
160 # endif
161 # ifndef memcpy
162 # define memcpy(d, s, n) (bcopy (s, d, n), (d))
163 # endif
164 # endif
165 # endif
166 
167 /* Define the syntax stuff for <, >, etc. */
168 
169 /* This must be nonzero for the wordchar and notwordchar pattern
170  commands in re_match_2. */
171 # ifndef Sword
172 # define Sword 1
173 # endif
174 
175 # ifdef SWITCH_ENUM_BUG
176 # define SWITCH_ENUM_CAST(x) ((int)(x))
177 # else
178 # define SWITCH_ENUM_CAST(x) (x)
179 # endif
180 
181 # endif /* not emacs */
182 
183 # if defined _LIBC || HAVE_LIMITS_H
184 # include <limits.h>
185 # endif
186 
187 # ifndef MB_LEN_MAX
188 # define MB_LEN_MAX 1
189 # endif
190 
191 
192 /* Get the interface, including the syntax bits. */
193 # include <regex.h>
194 
195 /* isalpha etc. are used for the character classes. */
196 # include <ctype.h>
197 
198 /* Jim Meyering writes:
199 
200  "... Some ctype macros are valid only for character codes that
201  isascii says are ASCII (SGI's IRIX-4.0.5 is one such system --when
202  using /bin/cc or gcc but without giving an ansi option). So, all
203  ctype uses should be through macros like ISPRINT... If
204  STDC_HEADERS is defined, then autoconf has verified that the ctype
205  macros don't need to be guarded with references to isascii. ...
206  Defining isascii to 1 should let any compiler worth its salt
207  eliminate the && through constant folding."
208  Solaris defines some of these symbols so we must undefine them first. */
209 
210 # undef ISASCII
211 # if defined STDC_HEADERS || (!defined isascii && !defined HAVE_ISASCII)
212 # define ISASCII(c) 1
213 # else
214 # define ISASCII(c) isascii(c)
215 # endif
216 
217 # ifdef isblank
218 # define ISBLANK(c) (ISASCII (c) && isblank (c))
219 # else
220 # define ISBLANK(c) ((c) == ' ' || (c) == '\t')
221 # endif
222 # ifdef isgraph
223 # define ISGRAPH(c) (ISASCII (c) && isgraph (c))
224 # else
225 # define ISGRAPH(c) (ISASCII (c) && isprint (c) && !isspace (c))
226 # endif
227 
228 # undef ISPRINT
229 # define ISPRINT(c) (ISASCII (c) && isprint (c))
230 # define ISDIGIT(c) (ISASCII (c) && isdigit (c))
231 # define ISALNUM(c) (ISASCII (c) && isalnum (c))
232 # define ISALPHA(c) (ISASCII (c) && isalpha (c))
233 # define ISCNTRL(c) (ISASCII (c) && iscntrl (c))
234 # define ISLOWER(c) (ISASCII (c) && islower (c))
235 # define ISPUNCT(c) (ISASCII (c) && ispunct (c))
236 # define ISSPACE(c) (ISASCII (c) && isspace (c))
237 # define ISUPPER(c) (ISASCII (c) && isupper (c))
238 # define ISXDIGIT(c) (ISASCII (c) && isxdigit (c))
239 
240 # ifdef _tolower
241 # define TOLOWER(c) _tolower(c)
242 # else
243 # define TOLOWER(c) tolower(c)
244 # endif
245 
246 # ifndef NULL
247 # define NULL (void *)0
248 # endif
249 
250 /* We remove any previous definition of `SIGN_EXTEND_CHAR',
251  since ours (we hope) works properly with all combinations of
252  machines, compilers, `char' and `unsigned char' argument types.
253  (Per Bothner suggested the basic approach.) */
254 # undef SIGN_EXTEND_CHAR
255 # if __STDC__
256 # define SIGN_EXTEND_CHAR(c) ((signed char) (c))
257 # else /* not __STDC__ */
258 /* As in Harbison and Steele. */
259 # define SIGN_EXTEND_CHAR(c) ((((unsigned char) (c)) ^ 128) - 128)
260 # endif
261 
262 
263 # ifndef emacs
264 /* How many characters in the character set. */
265 # define CHAR_SET_SIZE 256
266 
267 # ifdef SYNTAX_TABLE
268 
269 extern char *re_syntax_table;
270 
271 # else /* not SYNTAX_TABLE */
272 
274 
275 static void init_syntax_once PARAMS ((void));
276 
277 static void
279 {
280  register int c;
281  static int done = 0;
282 
283  if (done)
284  return;
286 
287  for (c = 0; c < CHAR_SET_SIZE; ++c)
288  if (ISALNUM (c))
289  re_syntax_table[c] = Sword;
290 
291  re_syntax_table['_'] = Sword;
292 
293  done = 1;
294 }
295 
296 # endif /* not SYNTAX_TABLE */
297 
298 # define SYNTAX(c) re_syntax_table[(unsigned char) (c)]
299 
300 # endif /* emacs */
301 
302 #define HAVE_UINTPTR_T
303 /* Integer type for pointers. */
304 # if !defined _LIBC && !defined HAVE_UINTPTR_T
305 typedef unsigned long int uintptr_t;
306 # endif
307 
308 /* Should we use malloc or alloca? If REGEX_MALLOC is not defined, we
309  use `alloca' instead of `malloc'. This is because using malloc in
310  re_search* or re_match* could cause memory leaks when C-g is used in
311  Emacs; also, malloc is slower and causes storage fragmentation. On
312  the other hand, malloc is more portable, and easier to debug.
313 
314  Because we sometimes use alloca, some routines have to be macros,
315  not functions -- `alloca'-allocated space disappears at the end of the
316  function it is called in. */
317 
318 # ifdef REGEX_MALLOC
319 
320 # define REGEX_ALLOCATE malloc
321 # define REGEX_REALLOCATE(source, osize, nsize) realloc (source, nsize)
322 # define REGEX_FREE free
323 
324 # else /* not REGEX_MALLOC */
325 
326 /* Emacs already defines alloca, sometimes. */
327 # ifndef alloca
328 
329 /* Make alloca work the best possible way. */
330 # ifdef __GNUC__
331 # define alloca __builtin_alloca
332 # else /* not __GNUC__ */
333 # if HAVE_ALLOCA_H
334 # include <alloca.h>
335 # endif /* HAVE_ALLOCA_H */
336 # endif /* not __GNUC__ */
337 
338 # endif /* not alloca */
339 
340 # define REGEX_ALLOCATE alloca
341 
342 /* Assumes a `char *destination' variable. */
343 # define REGEX_REALLOCATE(source, osize, nsize) \
344  (destination = (char *) alloca (nsize), \
345  memcpy (destination, source, osize))
346 
347 /* No need to do anything to free, after alloca. */
348 # define REGEX_FREE(arg) ((void)0) /* Do nothing! But inhibit gcc warning. */
349 
350 # endif /* not REGEX_MALLOC */
351 
352 /* Define how to allocate the failure stack. */
353 
354 # if defined REL_ALLOC && defined REGEX_MALLOC
355 
356 # define REGEX_ALLOCATE_STACK(size) \
357  r_alloc (&failure_stack_ptr, (size))
358 # define REGEX_REALLOCATE_STACK(source, osize, nsize) \
359  r_re_alloc (&failure_stack_ptr, (nsize))
360 # define REGEX_FREE_STACK(ptr) \
361  r_alloc_free (&failure_stack_ptr)
362 
363 # else /* not using relocating allocator */
364 
365 # ifdef REGEX_MALLOC
366 
367 # define REGEX_ALLOCATE_STACK malloc
368 # define REGEX_REALLOCATE_STACK(source, osize, nsize) realloc (source, nsize)
369 # define REGEX_FREE_STACK free
370 
371 # else /* not REGEX_MALLOC */
372 
373 # define REGEX_ALLOCATE_STACK alloca
374 
375 # define REGEX_REALLOCATE_STACK(source, osize, nsize) \
376  REGEX_REALLOCATE (source, osize, nsize)
377 /* No need to explicitly free anything. */
378 # define REGEX_FREE_STACK(arg)
379 
380 # endif /* not REGEX_MALLOC */
381 # endif /* not using relocating allocator */
382 
383 
384 /* True if `size1' is non-NULL and PTR is pointing anywhere inside
385  `string1' or just past its end. This works if PTR is NULL, which is
386  a good thing. */
387 # define FIRST_STRING_P(ptr) \
388  (size1 && string1 <= (ptr) && (ptr) <= string1 + size1)
389 
390 /* (Re)Allocate N items of type T using malloc, or fail. */
391 # define TALLOC(n, t) ((t *) malloc ((n) * sizeof (t)))
392 # define RETALLOC(addr, n, t) ((addr) = (t *) realloc (addr, (n) * sizeof (t)))
393 # define RETALLOC_IF(addr, n, t) \
394  if (addr) RETALLOC((addr), (n), t); else (addr) = TALLOC ((n), t)
395 # define REGEX_TALLOC(n, t) ((t *) REGEX_ALLOCATE ((n) * sizeof (t)))
396 
397 # define BYTEWIDTH 8 /* In bits. */
398 
399 # define STREQ(s1, s2) ((strcmp (s1, s2) == 0))
400 
401 # undef MAX
402 # undef MIN
403 # define MAX(a, b) ((a) > (b) ? (a) : (b))
404 # define MIN(a, b) ((a) < (b) ? (a) : (b))
405 
406 typedef char boolean;
407 # define false 0
408 # define true 1
409 
410 static reg_errcode_t byte_regex_compile _RE_ARGS ((const char *pattern, size_t size,
412  struct re_pattern_buffer *bufp));
413 
414 static int byte_re_match_2_internal PARAMS ((struct re_pattern_buffer *bufp,
415  const char *string1, int size1,
416  const char *string2, int size2,
417  int pos,
418  struct re_registers *regs,
419  int stop));
420 static int byte_re_search_2 PARAMS ((struct re_pattern_buffer *bufp,
421  const char *string1, int size1,
422  const char *string2, int size2,
423  int startpos, int range,
424  struct re_registers *regs, int stop));
425 static int byte_re_compile_fastmap PARAMS ((struct re_pattern_buffer *bufp));
426 
427 #ifdef MBS_SUPPORT
428 static reg_errcode_t wcs_regex_compile _RE_ARGS ((const char *pattern, size_t size,
430  struct re_pattern_buffer *bufp));
431 
432 
433 static int wcs_re_match_2_internal PARAMS ((struct re_pattern_buffer *bufp,
434  const char *cstring1, int csize1,
435  const char *cstring2, int csize2,
436  int pos,
437  struct re_registers *regs,
438  int stop,
439  wchar_t *string1, int size1,
440  wchar_t *string2, int size2,
441  int *mbs_offset1, int *mbs_offset2));
442 static int wcs_re_search_2 PARAMS ((struct re_pattern_buffer *bufp,
443  const char *string1, int size1,
444  const char *string2, int size2,
445  int startpos, int range,
446  struct re_registers *regs, int stop));
447 static int wcs_re_compile_fastmap PARAMS ((struct re_pattern_buffer *bufp));
448 #endif
449 
450 
451 /* These are the command codes that appear in compiled regular
452  expressions. Some opcodes are followed by argument bytes. A
453  command code can specify any interpretation whatsoever for its
454  arguments. Zero bytes may appear in the compiled regular expression. */
455 
456 typedef enum
457 {
458  no_op = 0,
459 
460  /* Succeed right away--no more backtracking. */
462 
463  /* Followed by one byte giving n, then by n literal bytes. */
465 
466 # ifdef MBS_SUPPORT
467  /* Same as exactn, but contains binary data. */
468  exactn_bin,
469 # endif
470 
471  /* Matches any (more or less) character. */
473 
474  /* Matches any one char belonging to specified set. First
475  following byte is number of bitmap bytes. Then come bytes
476  for a bitmap saying which chars are in. Bits in each byte
477  are ordered low-bit-first. A character is in the set if its
478  bit is 1. A character too large to have a bit in the map is
479  automatically not in the set. */
480  /* ifdef MBS_SUPPORT, following element is length of character
481  classes, length of collating symbols, length of equivalence
482  classes, length of character ranges, and length of characters.
483  Next, character class element, collating symbols elements,
484  equivalence class elements, range elements, and character
485  elements follow.
486  See regex_compile function. */
488 
489  /* Same parameters as charset, but match any character that is
490  not one of those specified. */
492 
493  /* Start remembering the text that is matched, for storing in a
494  register. Followed by one byte with the register number, in
495  the range 0 to one less than the pattern buffer's re_nsub
496  field. Then followed by one byte with the number of groups
497  inner to this one. (This last has to be part of the
498  start_memory only because we need it in the on_failure_jump
499  of re_match_2.) */
501 
502  /* Stop remembering the text that is matched and store it in a
503  memory register. Followed by one byte with the register
504  number, in the range 0 to one less than `re_nsub' in the
505  pattern buffer, and one byte with the number of inner groups,
506  just like `start_memory'. (We need the number of inner
507  groups here because we don't have any easy way of finding the
508  corresponding start_memory when we're at a stop_memory.) */
510 
511  /* Match a duplicate of something remembered. Followed by one
512  byte containing the register number. */
514 
515  /* Fail unless at beginning of line. */
517 
518  /* Fail unless at end of line. */
520 
521  /* Succeeds if at beginning of buffer (if emacs) or at beginning
522  of string to be matched (if not). */
524 
525  /* Analogously, for end of buffer/string. */
527 
528  /* Followed by two byte relative address to which to jump. */
530 
531  /* Same as jump, but marks the end of an alternative. */
533 
534  /* Followed by two-byte relative address of place to resume at
535  in case of failure. */
536  /* ifdef MBS_SUPPORT, the size of address is 1. */
538 
539  /* Like on_failure_jump, but pushes a placeholder instead of the
540  current string position when executed. */
542 
543  /* Throw away latest failure point and then jump to following
544  two-byte relative address. */
545  /* ifdef MBS_SUPPORT, the size of address is 1. */
547 
548  /* Change to pop_failure_jump if know won't have to backtrack to
549  match; otherwise change to jump. This is used to jump
550  back to the beginning of a repeat. If what follows this jump
551  clearly won't match what the repeat does, such that we can be
552  sure that there is no use backtracking out of repetitions
553  already matched, then we change it to a pop_failure_jump.
554  Followed by two-byte address. */
555  /* ifdef MBS_SUPPORT, the size of address is 1. */
557 
558  /* Jump to following two-byte address, and push a dummy failure
559  point. This failure point will be thrown away if an attempt
560  is made to use it for a failure. A `+' construct makes this
561  before the first repeat. Also used as an intermediary kind
562  of jump when compiling an alternative. */
563  /* ifdef MBS_SUPPORT, the size of address is 1. */
565 
566  /* Push a dummy failure point and continue. Used at the end of
567  alternatives. */
569 
570  /* Followed by two-byte relative address and two-byte number n.
571  After matching N times, jump to the address upon failure. */
572  /* ifdef MBS_SUPPORT, the size of address is 1. */
574 
575  /* Followed by two-byte relative address, and two-byte number n.
576  Jump to the address N times, then fail. */
577  /* ifdef MBS_SUPPORT, the size of address is 1. */
579 
580  /* Set the following two-byte relative address to the
581  subsequent two-byte number. The address *includes* the two
582  bytes of number. */
583  /* ifdef MBS_SUPPORT, the size of address is 1. */
585 
586  wordchar, /* Matches any word-constituent character. */
587  notwordchar, /* Matches any char that is not a word-constituent. */
588 
589  wordbeg, /* Succeeds if at word beginning. */
590  wordend, /* Succeeds if at word end. */
591 
592  wordbound, /* Succeeds if at a word boundary. */
593  notwordbound /* Succeeds if not at a word boundary. */
594 
595 # ifdef emacs
596  ,before_dot, /* Succeeds if before point. */
597  at_dot, /* Succeeds if at point. */
598  after_dot, /* Succeeds if after point. */
599 
600  /* Matches any character whose syntax is specified. Followed by
601  a byte which contains a syntax code, e.g., Sword. */
602  syntaxspec,
603 
604  /* Matches any character whose syntax is not that specified. */
605  notsyntaxspec
606 # endif /* emacs */
607 } re_opcode_t;
608 #endif /* not INSIDE_RECURSION */
609 
610 
611 
612 #ifdef BYTE
613 # define CHAR_T char
614 # define UCHAR_T unsigned char
615 # define COMPILED_BUFFER_VAR bufp->buffer
616 # define OFFSET_ADDRESS_SIZE 2
617 # define PREFIX(name) byte_##name
618 # define ARG_PREFIX(name) name
619 # define PUT_CHAR(c) putchar (c)
620 #else
621 # ifdef WCHAR
622 # define CHAR_T wchar_t
623 # define UCHAR_T wchar_t
624 # define COMPILED_BUFFER_VAR wc_buffer
625 # define OFFSET_ADDRESS_SIZE 1 /* the size which STORE_NUMBER macro use */
626 # define CHAR_CLASS_SIZE ((__alignof__(wctype_t)+sizeof(wctype_t))/sizeof(CHAR_T)+1)
627 # define PREFIX(name) wcs_##name
628 # define ARG_PREFIX(name) c##name
629 /* Should we use wide stream?? */
630 # define PUT_CHAR(c) printf ("%C", c);
631 # define TRUE 1
632 # define FALSE 0
633 # else
634 # ifdef MBS_SUPPORT
635 # define WCHAR
636 # define INSIDE_RECURSION
637 # include "regex.c"
638 # undef INSIDE_RECURSION
639 # endif
640 # define BYTE
641 # define INSIDE_RECURSION
642 # include "regex.c"
643 # undef INSIDE_RECURSION
644 # endif
645 #endif
646 
647 #ifdef INSIDE_RECURSION
648 /* Common operations on the compiled pattern. */
649 
650 /* Store NUMBER in two contiguous bytes starting at DESTINATION. */
651 /* ifdef MBS_SUPPORT, we store NUMBER in 1 element. */
652 
653 # ifdef WCHAR
654 # define STORE_NUMBER(destination, number) \
655  do { \
656  *(destination) = (UCHAR_T)(number); \
657  } while (0)
658 # else /* BYTE */
659 # define STORE_NUMBER(destination, number) \
660  do { \
661  (destination)[0] = (number) & 0377; \
662  (destination)[1] = (number) >> 8; \
663  } while (0)
664 # endif /* WCHAR */
665 
666 /* Same as STORE_NUMBER, except increment DESTINATION to
667  the byte after where the number is stored. Therefore, DESTINATION
668  must be an lvalue. */
669 /* ifdef MBS_SUPPORT, we store NUMBER in 1 element. */
670 
671 # define STORE_NUMBER_AND_INCR(destination, number) \
672  do { \
673  STORE_NUMBER (destination, number); \
674  (destination) += OFFSET_ADDRESS_SIZE; \
675  } while (0)
676 
677 /* Put into DESTINATION a number stored in two contiguous bytes starting
678  at SOURCE. */
679 /* ifdef MBS_SUPPORT, we store NUMBER in 1 element. */
680 
681 # ifdef WCHAR
682 # define EXTRACT_NUMBER(destination, source) \
683  do { \
684  (destination) = *(source); \
685  } while (0)
686 # else /* BYTE */
687 # define EXTRACT_NUMBER(destination, source) \
688  do { \
689  (destination) = *(source) & 0377; \
690  (destination) += SIGN_EXTEND_CHAR (*((source) + 1)) << 8; \
691  } while (0)
692 # endif
693 
694 # ifdef DEBUG
695 static void PREFIX(extract_number) _RE_ARGS ((int *dest, UCHAR_T *source));
696 static void
697 PREFIX(extract_number) (dest, source)
698  int *dest;
699  UCHAR_T *source;
700 {
701 # ifdef WCHAR
702  *dest = *source;
703 # else /* BYTE */
704  int temp = SIGN_EXTEND_CHAR (*(source + 1));
705  *dest = *source & 0377;
706  *dest += temp << 8;
707 # endif
708 }
709 
710 # ifndef EXTRACT_MACROS /* To debug the macros. */
711 # undef EXTRACT_NUMBER
712 # define EXTRACT_NUMBER(dest, src) PREFIX(extract_number) (&dest, src)
713 # endif /* not EXTRACT_MACROS */
714 
715 # endif /* DEBUG */
716 
717 /* Same as EXTRACT_NUMBER, except increment SOURCE to after the number.
718  SOURCE must be an lvalue. */
719 
720 # define EXTRACT_NUMBER_AND_INCR(destination, source) \
721  do { \
722  EXTRACT_NUMBER (destination, source); \
723  (source) += OFFSET_ADDRESS_SIZE; \
724  } while (0)
725 
726 # ifdef DEBUG
727 static void PREFIX(extract_number_and_incr) _RE_ARGS ((int *destination,
728  UCHAR_T **source));
729 static void
730 PREFIX(extract_number_and_incr) (destination, source)
731  int *destination;
732  UCHAR_T **source;
733 {
734  PREFIX(extract_number) (destination, *source);
735  *source += OFFSET_ADDRESS_SIZE;
736 }
737 
738 # ifndef EXTRACT_MACROS
739 # undef EXTRACT_NUMBER_AND_INCR
740 # define EXTRACT_NUMBER_AND_INCR(dest, src) \
741  PREFIX(extract_number_and_incr) (&dest, &src)
742 # endif /* not EXTRACT_MACROS */
743 
744 # endif /* DEBUG */
745 
746 
747 
748 
749 /* If DEBUG is defined, Regex prints many voluminous messages about what
750  it is doing (if the variable `debug' is nonzero). If linked with the
751  main program in `iregex.c', you can enter patterns and strings
752  interactively. And if linked with the main program in `main.c' and
753  the other test files, you can run the already-written tests. */
754 
755 # ifdef DEBUG
756 
757 # ifndef DEFINED_ONCE
758 
759 /* We use standard I/O for debugging. */
760 # include <stdio.h>
761 
762 /* It is useful to test things that ``must'' be true when debugging. */
763 # include <assert.h>
764 
765 static int debug;
766 
767 # define DEBUG_STATEMENT(e) e
768 # define DEBUG_PRINT1(x) if (debug) printf (x)
769 # define DEBUG_PRINT2(x1, x2) if (debug) printf (x1, x2)
770 # define DEBUG_PRINT3(x1, x2, x3) if (debug) printf (x1, x2, x3)
771 # define DEBUG_PRINT4(x1, x2, x3, x4) if (debug) printf (x1, x2, x3, x4)
772 # endif /* not DEFINED_ONCE */
773 
774 # define DEBUG_PRINT_COMPILED_PATTERN(p, s, e) \
775  if (debug) PREFIX(print_partial_compiled_pattern) (s, e)
776 # define DEBUG_PRINT_DOUBLE_STRING(w, s1, sz1, s2, sz2) \
777  if (debug) PREFIX(print_double_string) (w, s1, sz1, s2, sz2)
778 
779 
780 /* Print the fastmap in human-readable form. */
781 
782 # ifndef DEFINED_ONCE
783 void
784 print_fastmap (fastmap)
785  char *fastmap;
786 {
787  unsigned was_a_range = 0;
788  unsigned i = 0;
789 
790  while (i < (1 << BYTEWIDTH))
791  {
792  if (fastmap[i++])
793  {
794  was_a_range = 0;
795  putchar (i - 1);
796  while (i < (1 << BYTEWIDTH) && fastmap[i])
797  {
798  was_a_range = 1;
799  i++;
800  }
801  if (was_a_range)
802  {
803  printf ("-");
804  putchar (i - 1);
805  }
806  }
807  }
808  putchar ('\n');
809 }
810 # endif /* not DEFINED_ONCE */
811 
812 
813 /* Print a compiled pattern string in human-readable form, starting at
814  the START pointer into it and ending just before the pointer END. */
815 
816 void
817 PREFIX(print_partial_compiled_pattern) (start, end)
818  UCHAR_T *start;
819  UCHAR_T *end;
820 {
821  int mcnt, mcnt2;
822  UCHAR_T *p1;
823  UCHAR_T *p = start;
824  UCHAR_T *pend = end;
825 
826  if (start == NULL)
827  {
828  printf ("(null)\n");
829  return;
830  }
831 
832  /* Loop over pattern commands. */
833  while (p < pend)
834  {
835 # ifdef _LIBC
836  printf ("%td:\t", p - start);
837 # else
838  printf ("%ld:\t", (long int) (p - start));
839 # endif
840 
841  switch ((re_opcode_t) *p++)
842  {
843  case no_op:
844  printf ("/no_op");
845  break;
846 
847  case exactn:
848  mcnt = *p++;
849  printf ("/exactn/%d", mcnt);
850  do
851  {
852  putchar ('/');
853  PUT_CHAR (*p++);
854  }
855  while (--mcnt);
856  break;
857 
858 # ifdef MBS_SUPPORT
859  case exactn_bin:
860  mcnt = *p++;
861  printf ("/exactn_bin/%d", mcnt);
862  do
863  {
864  printf("/%lx", (long int) *p++);
865  }
866  while (--mcnt);
867  break;
868 # endif /* MBS_SUPPORT */
869 
870  case start_memory:
871  mcnt = *p++;
872  printf ("/start_memory/%d/%ld", mcnt, (long int) *p++);
873  break;
874 
875  case stop_memory:
876  mcnt = *p++;
877  printf ("/stop_memory/%d/%ld", mcnt, (long int) *p++);
878  break;
879 
880  case duplicate:
881  printf ("/duplicate/%ld", (long int) *p++);
882  break;
883 
884  case anychar:
885  printf ("/anychar");
886  break;
887 
888  case charset:
889  case charset_not:
890  {
891 # ifdef WCHAR
892  int i, length;
893  wchar_t *workp = p;
894  printf ("/charset [%s",
895  (re_opcode_t) *(workp - 1) == charset_not ? "^" : "");
896  p += 5;
897  length = *workp++; /* the length of char_classes */
898  for (i=0 ; i<length ; i++)
899  printf("[:%lx:]", (long int) *p++);
900  length = *workp++; /* the length of collating_symbol */
901  for (i=0 ; i<length ;)
902  {
903  printf("[.");
904  while(*p != 0)
905  PUT_CHAR((i++,*p++));
906  i++,p++;
907  printf(".]");
908  }
909  length = *workp++; /* the length of equivalence_class */
910  for (i=0 ; i<length ;)
911  {
912  printf("[=");
913  while(*p != 0)
914  PUT_CHAR((i++,*p++));
915  i++,p++;
916  printf("=]");
917  }
918  length = *workp++; /* the length of char_range */
919  for (i=0 ; i<length ; i++)
920  {
921  wchar_t range_start = *p++;
922  wchar_t range_end = *p++;
923  printf("%C-%C", range_start, range_end);
924  }
925  length = *workp++; /* the length of char */
926  for (i=0 ; i<length ; i++)
927  printf("%C", *p++);
928  putchar (']');
929 # else
930  register int c, last = -100;
931  register int in_range = 0;
932 
933  printf ("/charset [%s",
934  (re_opcode_t) *(p - 1) == charset_not ? "^" : "");
935 
936  assert (p + *p < pend);
937 
938  for (c = 0; c < 256; c++)
939  if (c / 8 < *p
940  && (p[1 + (c/8)] & (1 << (c % 8))))
941  {
942  /* Are we starting a range? */
943  if (last + 1 == c && ! in_range)
944  {
945  putchar ('-');
946  in_range = 1;
947  }
948  /* Have we broken a range? */
949  else if (last + 1 != c && in_range)
950  {
951  putchar (last);
952  in_range = 0;
953  }
954 
955  if (! in_range)
956  putchar (c);
957 
958  last = c;
959  }
960 
961  if (in_range)
962  putchar (last);
963 
964  putchar (']');
965 
966  p += 1 + *p;
967 # endif /* WCHAR */
968  }
969  break;
970 
971  case begline:
972  printf ("/begline");
973  break;
974 
975  case endline:
976  printf ("/endline");
977  break;
978 
979  case on_failure_jump:
980  PREFIX(extract_number_and_incr) (&mcnt, &p);
981 # ifdef _LIBC
982  printf ("/on_failure_jump to %td", p + mcnt - start);
983 # else
984  printf ("/on_failure_jump to %ld", (long int) (p + mcnt - start));
985 # endif
986  break;
987 
989  PREFIX(extract_number_and_incr) (&mcnt, &p);
990 # ifdef _LIBC
991  printf ("/on_failure_keep_string_jump to %td", p + mcnt - start);
992 # else
993  printf ("/on_failure_keep_string_jump to %ld",
994  (long int) (p + mcnt - start));
995 # endif
996  break;
997 
998  case dummy_failure_jump:
999  PREFIX(extract_number_and_incr) (&mcnt, &p);
1000 # ifdef _LIBC
1001  printf ("/dummy_failure_jump to %td", p + mcnt - start);
1002 # else
1003  printf ("/dummy_failure_jump to %ld", (long int) (p + mcnt - start));
1004 # endif
1005  break;
1006 
1007  case push_dummy_failure:
1008  printf ("/push_dummy_failure");
1009  break;
1010 
1011  case maybe_pop_jump:
1012  PREFIX(extract_number_and_incr) (&mcnt, &p);
1013 # ifdef _LIBC
1014  printf ("/maybe_pop_jump to %td", p + mcnt - start);
1015 # else
1016  printf ("/maybe_pop_jump to %ld", (long int) (p + mcnt - start));
1017 # endif
1018  break;
1019 
1020  case pop_failure_jump:
1021  PREFIX(extract_number_and_incr) (&mcnt, &p);
1022 # ifdef _LIBC
1023  printf ("/pop_failure_jump to %td", p + mcnt - start);
1024 # else
1025  printf ("/pop_failure_jump to %ld", (long int) (p + mcnt - start));
1026 # endif
1027  break;
1028 
1029  case jump_past_alt:
1030  PREFIX(extract_number_and_incr) (&mcnt, &p);
1031 # ifdef _LIBC
1032  printf ("/jump_past_alt to %td", p + mcnt - start);
1033 # else
1034  printf ("/jump_past_alt to %ld", (long int) (p + mcnt - start));
1035 # endif
1036  break;
1037 
1038  case jump:
1039  PREFIX(extract_number_and_incr) (&mcnt, &p);
1040 # ifdef _LIBC
1041  printf ("/jump to %td", p + mcnt - start);
1042 # else
1043  printf ("/jump to %ld", (long int) (p + mcnt - start));
1044 # endif
1045  break;
1046 
1047  case succeed_n:
1048  PREFIX(extract_number_and_incr) (&mcnt, &p);
1049  p1 = p + mcnt;
1050  PREFIX(extract_number_and_incr) (&mcnt2, &p);
1051 # ifdef _LIBC
1052  printf ("/succeed_n to %td, %d times", p1 - start, mcnt2);
1053 # else
1054  printf ("/succeed_n to %ld, %d times",
1055  (long int) (p1 - start), mcnt2);
1056 # endif
1057  break;
1058 
1059  case jump_n:
1060  PREFIX(extract_number_and_incr) (&mcnt, &p);
1061  p1 = p + mcnt;
1062  PREFIX(extract_number_and_incr) (&mcnt2, &p);
1063  printf ("/jump_n to %d, %d times", p1 - start, mcnt2);
1064  break;
1065 
1066  case set_number_at:
1067  PREFIX(extract_number_and_incr) (&mcnt, &p);
1068  p1 = p + mcnt;
1069  PREFIX(extract_number_and_incr) (&mcnt2, &p);
1070 # ifdef _LIBC
1071  printf ("/set_number_at location %td to %d", p1 - start, mcnt2);
1072 # else
1073  printf ("/set_number_at location %ld to %d",
1074  (long int) (p1 - start), mcnt2);
1075 # endif
1076  break;
1077 
1078  case wordbound:
1079  printf ("/wordbound");
1080  break;
1081 
1082  case notwordbound:
1083  printf ("/notwordbound");
1084  break;
1085 
1086  case wordbeg:
1087  printf ("/wordbeg");
1088  break;
1089 
1090  case wordend:
1091  printf ("/wordend");
1092  break;
1093 
1094 # ifdef emacs
1095  case before_dot:
1096  printf ("/before_dot");
1097  break;
1098 
1099  case at_dot:
1100  printf ("/at_dot");
1101  break;
1102 
1103  case after_dot:
1104  printf ("/after_dot");
1105  break;
1106 
1107  case syntaxspec:
1108  printf ("/syntaxspec");
1109  mcnt = *p++;
1110  printf ("/%d", mcnt);
1111  break;
1112 
1113  case notsyntaxspec:
1114  printf ("/notsyntaxspec");
1115  mcnt = *p++;
1116  printf ("/%d", mcnt);
1117  break;
1118 # endif /* emacs */
1119 
1120  case wordchar:
1121  printf ("/wordchar");
1122  break;
1123 
1124  case notwordchar:
1125  printf ("/notwordchar");
1126  break;
1127 
1128  case begbuf:
1129  printf ("/begbuf");
1130  break;
1131 
1132  case endbuf:
1133  printf ("/endbuf");
1134  break;
1135 
1136  default:
1137  printf ("?%ld", (long int) *(p-1));
1138  }
1139 
1140  putchar ('\n');
1141  }
1142 
1143 # ifdef _LIBC
1144  printf ("%td:\tend of pattern.\n", p - start);
1145 # else
1146  printf ("%ld:\tend of pattern.\n", (long int) (p - start));
1147 # endif
1148 }
1149 
1150 
1151 void
1152 PREFIX(print_compiled_pattern) (bufp)
1153  struct re_pattern_buffer *bufp;
1154 {
1155  UCHAR_T *buffer = (UCHAR_T*) bufp->buffer;
1156 
1157  PREFIX(print_partial_compiled_pattern) (buffer, buffer
1158  + bufp->used / sizeof(UCHAR_T));
1159  printf ("%ld bytes used/%ld bytes allocated.\n",
1160  bufp->used, bufp->allocated);
1161 
1162  if (bufp->fastmap_accurate && bufp->fastmap)
1163  {
1164  printf ("fastmap: ");
1165  print_fastmap (bufp->fastmap);
1166  }
1167 
1168 # ifdef _LIBC
1169  printf ("re_nsub: %Zd\t", bufp->re_nsub);
1170 # else
1171  printf ("re_nsub: %ld\t", (long int) bufp->re_nsub);
1172 # endif
1173  printf ("regs_alloc: %d\t", bufp->regs_allocated);
1174  printf ("can_be_null: %d\t", bufp->can_be_null);
1175  printf ("newline_anchor: %d\n", bufp->newline_anchor);
1176  printf ("no_sub: %d\t", bufp->no_sub);
1177  printf ("not_bol: %d\t", bufp->not_bol);
1178  printf ("not_eol: %d\t", bufp->not_eol);
1179  printf ("syntax: %lx\n", bufp->syntax);
1180  /* Perhaps we should print the translate table? */
1181 }
1182 
1183 
1184 void
1185 PREFIX(print_double_string) (where, string1, size1, string2, size2)
1186  const CHAR_T *where;
1187  const CHAR_T *string1;
1188  const CHAR_T *string2;
1189  int size1;
1190  int size2;
1191 {
1192  int this_char;
1193 
1194  if (where == NULL)
1195  printf ("(null)");
1196  else
1197  {
1198  int cnt;
1199 
1200  if (FIRST_STRING_P (where))
1201  {
1202  for (this_char = where - string1; this_char < size1; this_char++)
1203  PUT_CHAR (string1[this_char]);
1204 
1205  where = string2;
1206  }
1207 
1208  cnt = 0;
1209  for (this_char = where - string2; this_char < size2; this_char++)
1210  {
1211  PUT_CHAR (string2[this_char]);
1212  if (++cnt > 100)
1213  {
1214  fputs ("...", stdout);
1215  break;
1216  }
1217  }
1218  }
1219 }
1220 
1221 # ifndef DEFINED_ONCE
1222 void
1223 printchar (c)
1224  int c;
1225 {
1226  putc (c, stderr);
1227 }
1228 # endif
1229 
1230 # else /* not DEBUG */
1231 
1232 # ifndef DEFINED_ONCE
1233 # undef assert
1234 # define assert(e)
1235 
1236 # define DEBUG_STATEMENT(e)
1237 # define DEBUG_PRINT1(x)
1238 # define DEBUG_PRINT2(x1, x2)
1239 # define DEBUG_PRINT3(x1, x2, x3)
1240 # define DEBUG_PRINT4(x1, x2, x3, x4)
1241 # endif /* not DEFINED_ONCE */
1242 # define DEBUG_PRINT_COMPILED_PATTERN(p, s, e)
1243 # define DEBUG_PRINT_DOUBLE_STRING(w, s1, sz1, s2, sz2)
1244 
1245 # endif /* not DEBUG */
1246 
1247 
1248 
1249 
1250 # ifdef WCHAR
1251 /* This convert a multibyte string to a wide character string.
1252  And write their correspondances to offset_buffer(see below)
1253  and write whether each wchar_t is binary data to is_binary.
1254  This assume invalid multibyte sequences as binary data.
1255  We assume offset_buffer and is_binary is already allocated
1256  enough space. */
1257 
1258 static size_t convert_mbs_to_wcs (CHAR_T *dest, const unsigned char* src,
1259  size_t len, int *offset_buffer,
1260  char *is_binary);
1261 static size_t
1262 convert_mbs_to_wcs (dest, src, len, offset_buffer, is_binary)
1263  CHAR_T *dest;
1264  const unsigned char* src;
1265  size_t len; /* the length of multibyte string. */
1266 
1267  /* It hold correspondances between src(char string) and
1268  dest(wchar_t string) for optimization.
1269  e.g. src = "xxxyzz"
1270  dest = {'X', 'Y', 'Z'}
1271  (each "xxx", "y" and "zz" represent one multibyte character
1272  corresponding to 'X', 'Y' and 'Z'.)
1273  offset_buffer = {0, 0+3("xxx"), 0+3+1("y"), 0+3+1+2("zz")}
1274  = {0, 3, 4, 6}
1275  */
1276  int *offset_buffer;
1277  char *is_binary;
1278 {
1279  wchar_t *pdest = dest;
1280  const unsigned char *psrc = src;
1281  size_t wc_count = 0;
1282 
1283  mbstate_t mbs;
1284  int i, consumed;
1285  size_t mb_remain = len;
1286  size_t mb_count = 0;
1287 
1288  /* Initialize the conversion state. */
1289  memset (&mbs, 0, sizeof (mbstate_t));
1290 
1291  offset_buffer[0] = 0;
1292  for( ; mb_remain > 0 ; ++wc_count, ++pdest, mb_remain -= consumed,
1293  psrc += consumed)
1294  {
1295 #ifdef _LIBC
1296  consumed = __mbrtowc (pdest, psrc, mb_remain, &mbs);
1297 #else
1298  consumed = mbrtowc (pdest, psrc, mb_remain, &mbs);
1299 #endif
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. */
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 
1348 re_set_syntax (syntax)
1350 {
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
1363 weak_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 
1372 static 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 
1426 static const size_t re_error_msgid_idx[] =
1427  {
1438  REG_BADBR_IDX,
1442  REG_EEND_IDX,
1443  REG_ESIZE_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. */
1514 long int re_max_failures = 4000;
1515 # else
1516 long int re_max_failures = 2000;
1517 # endif
1518 # endif
1519 
1520 union PREFIX(fail_stack_elt)
1521 {
1522  UCHAR_T *pointer;
1523  long int integer;
1524 };
1525 
1526 typedef union PREFIX(fail_stack_elt) PREFIX(fail_stack_elt_t);
1527 
1528 typedef 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. */
1541 int re_max_failures = 4000;
1542 # else
1543 int re_max_failures = 2000;
1544 # endif
1545 # endif
1546 
1547 union PREFIX(fail_stack_elt)
1548 {
1549  UCHAR_T *pointer;
1550  int integer;
1551 };
1552 
1553 typedef union PREFIX(fail_stack_elt) PREFIX(fail_stack_elt_t);
1554 
1555 typedef 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 
1877 typedef 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. */
1921 static 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. */
1926 static void PREFIX(store_op1) _RE_ARGS ((re_opcode_t op, UCHAR_T *loc, int arg));
1927 static void PREFIX(store_op2) _RE_ARGS ((re_opcode_t op, UCHAR_T *loc,
1928  int arg1, int arg2));
1929 static void PREFIX(insert_op1) _RE_ARGS ((re_opcode_t op, UCHAR_T *loc,
1930  int arg, UCHAR_T *end));
1931 static void PREFIX(insert_op2) _RE_ARGS ((re_opcode_t op, UCHAR_T *loc,
1932  int arg1, int arg2, UCHAR_T *end));
1933 static boolean PREFIX(at_begline_loc_p) _RE_ARGS ((const CHAR_T *pattern,
1934  const CHAR_T *p,
1935  reg_syntax_t syntax));
1936 static 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
1940 static 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));
1947 static void insert_space _RE_ARGS ((int num, CHAR_T *loc, CHAR_T *end));
1948 # else /* BYTE */
1949 static 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. */
2190 typedef 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. */
2198 typedef long pattern_offset_t;
2199 
2200 typedef 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 
2210 typedef 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 
2292 static 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
2298 static int regs_allocated_size;
2299 
2300 static const char ** regstart, ** regend;
2301 static const char ** old_regstart, ** old_regend;
2302 static const char **best_regstart, **best_regend;
2303 static const char **reg_dummy;
2304 # endif /* DEFINED_ONCE */
2305 
2306 static PREFIX(register_info_type) *PREFIX(reg_info);
2307 static 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 
2312 static void
2313 PREFIX(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
2336 static 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 
2368 static reg_errcode_t
2369 PREFIX(regex_compile) (ARG_PREFIX(pattern), ARG_PREFIX(size), syntax, bufp)
2370  const char *ARG_PREFIX(pattern);
2371  size_t ARG_PREFIX(size);
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. */
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  {
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. */
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. */
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 # ifdef _LIBC
3327  if (__iswctype (__btowc (ch), wt))
3328  SET_LIST_BIT (ch);
3329 # else
3330  if (iswctype (btowc (ch), wt))
3331  SET_LIST_BIT (ch);
3332 # endif
3333 
3334  if (translate && (is_upper || is_lower)
3335  && (ISUPPER (ch) || ISLOWER (ch)))
3336  SET_LIST_BIT (ch);
3337  }
3338 
3339  had_char_class = true;
3340 # else
3341  int ch;
3342  boolean is_alnum = STREQ (str, "alnum");
3343  boolean is_alpha = STREQ (str, "alpha");
3344  boolean is_blank = STREQ (str, "blank");
3345  boolean is_cntrl = STREQ (str, "cntrl");
3346  boolean is_digit = STREQ (str, "digit");
3347  boolean is_graph = STREQ (str, "graph");
3348  boolean is_lower = STREQ (str, "lower");
3349  boolean is_print = STREQ (str, "print");
3350  boolean is_punct = STREQ (str, "punct");
3351  boolean is_space = STREQ (str, "space");
3352  boolean is_upper = STREQ (str, "upper");
3353  boolean is_xdigit = STREQ (str, "xdigit");
3354 
3355  if (!IS_CHAR_CLASS (str))
3356  FREE_STACK_RETURN (REG_ECTYPE);
3357 
3358  /* Throw away the ] at the end of the character
3359  class. */
3360  PATFETCH (c);
3361 
3362  if (p == pend) FREE_STACK_RETURN (REG_EBRACK);
3363 
3364  for (ch = 0; ch < 1 << BYTEWIDTH; ch++)
3365  {
3366  /* This was split into 3 if's to
3367  avoid an arbitrary limit in some compiler. */
3368  if ( (is_alnum && ISALNUM (ch))
3369  || (is_alpha && ISALPHA (ch))
3370  || (is_blank && ISBLANK (ch))
3371  || (is_cntrl && ISCNTRL (ch)))
3372  SET_LIST_BIT (ch);
3373  if ( (is_digit && ISDIGIT (ch))
3374  || (is_graph && ISGRAPH (ch))
3375  || (is_lower && ISLOWER (ch))
3376  || (is_print && ISPRINT (ch)))
3377  SET_LIST_BIT (ch);
3378  if ( (is_punct && ISPUNCT (ch))
3379  || (is_space && ISSPACE (ch))
3380  || (is_upper && ISUPPER (ch))
3381  || (is_xdigit && ISXDIGIT (ch)))
3382  SET_LIST_BIT (ch);
3383  if ( translate && (is_upper || is_lower)
3384  && (ISUPPER (ch) || ISLOWER (ch)))
3385  SET_LIST_BIT (ch);
3386  }
3387  had_char_class = true;
3388 # endif /* libc || wctype.h */
3389  }
3390  else
3391  {
3392  c1++;
3393  while (c1--)
3394  PATUNFETCH;
3395  SET_LIST_BIT ('[');
3396  SET_LIST_BIT (':');
3397  range_start = ':';
3398  had_char_class = false;
3399  }
3400  }
3401  else if (syntax & RE_CHAR_CLASSES && c == '[' && *p == '=')
3402  {
3403  unsigned char str[MB_LEN_MAX + 1];
3404 # ifdef _LIBC
3405  uint32_t nrules =
3406  _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_NRULES);
3407 # endif
3408 
3409  PATFETCH (c);
3410  c1 = 0;
3411 
3412  /* If pattern is `[[='. */
3413  if (p == pend) FREE_STACK_RETURN (REG_EBRACK);
3414 
3415  for (;;)
3416  {
3417  PATFETCH (c);
3418  if ((c == '=' && *p == ']') || p == pend)
3419  break;
3420  if (c1 < MB_LEN_MAX)
3421  str[c1++] = c;
3422  else
3423  /* This is in any case an invalid class name. */
3424  str[0] = '\0';
3425  }
3426  str[c1] = '\0';
3427 
3428  if (c == '=' && *p == ']' && str[0] != '\0')
3429  {
3430  /* If we have no collation data we use the default
3431  collation in which each character is in a class
3432  by itself. It also means that ASCII is the
3433  character set and therefore we cannot have character
3434  with more than one byte in the multibyte
3435  representation. */
3436 # ifdef _LIBC
3437  if (nrules == 0)
3438 # endif
3439  {
3440  if (c1 != 1)
3441  FREE_STACK_RETURN (REG_ECOLLATE);
3442 
3443  /* Throw away the ] at the end of the equivalence
3444  class. */
3445  PATFETCH (c);
3446 
3447  /* Set the bit for the character. */
3448  SET_LIST_BIT (str[0]);
3449  }
3450 # ifdef _LIBC
3451  else
3452  {
3453  /* Try to match the byte sequence in `str' against
3454  those known to the collate implementation.
3455  First find out whether the bytes in `str' are
3456  actually from exactly one character. */
3457  const int32_t *table;
3458  const unsigned char *weights;
3459  const unsigned char *extra;
3460  const int32_t *indirect;
3461  int32_t idx;
3462  const unsigned char *cp = str;
3463  int ch;
3464 
3465  /* This #include defines a local function! */
3466 # include <locale/weight.h>
3467 
3468  table = (const int32_t *)
3469  _NL_CURRENT (LC_COLLATE, _NL_COLLATE_TABLEMB);
3470  weights = (const unsigned char *)
3471  _NL_CURRENT (LC_COLLATE, _NL_COLLATE_WEIGHTMB);
3472  extra = (const unsigned char *)
3473  _NL_CURRENT (LC_COLLATE, _NL_COLLATE_EXTRAMB);
3474  indirect = (const int32_t *)
3475  _NL_CURRENT (LC_COLLATE, _NL_COLLATE_INDIRECTMB);
3476 
3477  idx = findidx (&cp);
3478  if (idx == 0 || cp < str + c1)
3479  /* This is no valid character. */
3480  FREE_STACK_RETURN (REG_ECOLLATE);
3481 
3482  /* Throw away the ] at the end of the equivalence
3483  class. */
3484  PATFETCH (c);
3485 
3486  /* Now we have to go throught the whole table
3487  and find all characters which have the same
3488  first level weight.
3489 
3490  XXX Note that this is not entirely correct.
3491  we would have to match multibyte sequences
3492  but this is not possible with the current
3493  implementation. */
3494  for (ch = 1; ch < 256; ++ch)
3495  /* XXX This test would have to be changed if we
3496  would allow matching multibyte sequences. */
3497  if (table[ch] > 0)
3498  {
3499  int32_t idx2 = table[ch];
3500  size_t len = weights[idx2];
3501 
3502  /* Test whether the lenghts match. */
3503  if (weights[idx] == len)
3504  {
3505  /* They do. New compare the bytes of
3506  the weight. */
3507  size_t cnt = 0;
3508 
3509  while (cnt < len
3510  && (weights[idx + 1 + cnt]
3511  == weights[idx2 + 1 + cnt]))
3512  ++cnt;
3513 
3514  if (cnt == len)
3515  /* They match. Mark the character as
3516  acceptable. */
3517  SET_LIST_BIT (ch);
3518  }
3519  }
3520  }
3521 # endif
3522  had_char_class = true;
3523  }
3524  else
3525  {
3526  c1++;
3527  while (c1--)
3528  PATUNFETCH;
3529  SET_LIST_BIT ('[');
3530  SET_LIST_BIT ('=');
3531  range_start = '=';
3532  had_char_class = false;
3533  }
3534  }
3535  else if (syntax & RE_CHAR_CLASSES && c == '[' && *p == '.')
3536  {
3537  unsigned char str[128]; /* Should be large enough. */
3538 # ifdef _LIBC
3539  uint32_t nrules =
3540  _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_NRULES);
3541 # endif
3542 
3543  PATFETCH (c);
3544  c1 = 0;
3545 
3546  /* If pattern is `[[.'. */
3547  if (p == pend) FREE_STACK_RETURN (REG_EBRACK);
3548 
3549  for (;;)
3550  {
3551  PATFETCH (c);
3552  if ((c == '.' && *p == ']') || p == pend)
3553  break;
3554  if (c1 < sizeof (str))
3555  str[c1++] = c;
3556  else
3557  /* This is in any case an invalid class name. */
3558  str[0] = '\0';
3559  }
3560  str[c1] = '\0';
3561 
3562  if (c == '.' && *p == ']' && str[0] != '\0')
3563  {
3564  /* If we have no collation data we use the default
3565  collation in which each character is the name
3566  for its own class which contains only the one
3567  character. It also means that ASCII is the
3568  character set and therefore we cannot have character
3569  with more than one byte in the multibyte
3570  representation. */
3571 # ifdef _LIBC
3572  if (nrules == 0)
3573 # endif
3574  {
3575  if (c1 != 1)
3576  FREE_STACK_RETURN (REG_ECOLLATE);
3577 
3578  /* Throw away the ] at the end of the equivalence
3579  class. */
3580  PATFETCH (c);
3581 
3582  /* Set the bit for the character. */
3583  SET_LIST_BIT (str[0]);
3584  range_start = ((const unsigned char *) str)[0];
3585  }
3586 # ifdef _LIBC
3587  else
3588  {
3589  /* Try to match the byte sequence in `str' against
3590  those known to the collate implementation.
3591  First find out whether the bytes in `str' are
3592  actually from exactly one character. */
3593  int32_t table_size;
3594  const int32_t *symb_table;
3595  const unsigned char *extra;
3596  int32_t idx;
3597  int32_t elem;
3598  int32_t second;
3599  int32_t hash;
3600 
3601  table_size =
3602  _NL_CURRENT_WORD (LC_COLLATE,
3603  _NL_COLLATE_SYMB_HASH_SIZEMB);
3604  symb_table = (const int32_t *)
3605  _NL_CURRENT (LC_COLLATE,
3606  _NL_COLLATE_SYMB_TABLEMB);
3607  extra = (const unsigned char *)
3608  _NL_CURRENT (LC_COLLATE,
3609  _NL_COLLATE_SYMB_EXTRAMB);
3610 
3611  /* Locate the character in the hashing table. */
3612  hash = elem_hash (str, c1);
3613 
3614  idx = 0;
3615  elem = hash % table_size;
3616  second = hash % (table_size - 2);
3617  while (symb_table[2 * elem] != 0)
3618  {
3619  /* First compare the hashing value. */
3620  if (symb_table[2 * elem] == hash
3621  && c1 == extra[symb_table[2 * elem + 1]]
3622  && memcmp (str,
3623  &extra[symb_table[2 * elem + 1]
3624  + 1],
3625  c1) == 0)
3626  {
3627  /* Yep, this is the entry. */
3628  idx = symb_table[2 * elem + 1];
3629  idx += 1 + extra[idx];
3630  break;
3631  }
3632 
3633  /* Next entry. */
3634  elem += second;
3635  }
3636 
3637  if (symb_table[2 * elem] == 0)
3638  /* This is no valid character. */
3639  FREE_STACK_RETURN (REG_ECOLLATE);
3640 
3641  /* Throw away the ] at the end of the equivalence
3642  class. */
3643  PATFETCH (c);
3644 
3645  /* Now add the multibyte character(s) we found
3646  to the accept list.
3647 
3648  XXX Note that this is not entirely correct.
3649  we would have to match multibyte sequences
3650  but this is not possible with the current
3651  implementation. Also, we have to match
3652  collating symbols, which expand to more than
3653  one file, as a whole and not allow the
3654  individual bytes. */
3655  c1 = extra[idx++];
3656  if (c1 == 1)
3657  range_start = extra[idx];
3658  while (c1-- > 0)
3659  {
3660  SET_LIST_BIT (extra[idx]);
3661  ++idx;
3662  }
3663  }
3664 # endif
3665  had_char_class = false;
3666  }
3667  else
3668  {
3669  c1++;
3670  while (c1--)
3671  PATUNFETCH;
3672  SET_LIST_BIT ('[');
3673  SET_LIST_BIT ('.');
3674  range_start = '.';
3675  had_char_class = false;
3676  }
3677  }
3678  else
3679  {
3680  had_char_class = false;
3681  SET_LIST_BIT (c);
3682  range_start = c;
3683  }
3684  }
3685 
3686  /* Discard any (non)matching list bytes that are all 0 at the
3687  end of the map. Decrease the map-length byte too. */
3688  while ((int) b[-1] > 0 && b[b[-1] - 1] == 0)
3689  b[-1]--;
3690  b += b[-1];
3691 #endif /* WCHAR */
3692  }
3693  break;
3694 
3695 
3696  case '(':
3697  if (syntax & RE_NO_BK_PARENS)
3698  goto handle_open;
3699  else
3700  goto normal_char;
3701 
3702 
3703  case ')':
3704  if (syntax & RE_NO_BK_PARENS)
3705  goto handle_close;
3706  else
3707  goto normal_char;
3708 
3709 
3710  case '\n':
3711  if (syntax & RE_NEWLINE_ALT)
3712  goto handle_alt;
3713  else
3714  goto normal_char;
3715 
3716 
3717  case '|':
3718  if (syntax & RE_NO_BK_VBAR)
3719  goto handle_alt;
3720  else
3721  goto normal_char;
3722 
3723 
3724  case '{':
3725  if (syntax & RE_INTERVALS && syntax & RE_NO_BK_BRACES)
3726  goto handle_interval;
3727  else
3728  goto normal_char;
3729 
3730 
3731  case '\\':
3732  if (p == pend) FREE_STACK_RETURN (REG_EESCAPE);
3733 
3734  /* Do not translate the character after the \, so that we can
3735  distinguish, e.g., \B from \b, even if we normally would
3736  translate, e.g., B to b. */
3737  PATFETCH_RAW (c);
3738 
3739  switch (c)
3740  {
3741  case '(':
3742  if (syntax & RE_NO_BK_PARENS)
3743  goto normal_backslash;
3744 
3745  handle_open:
3746  bufp->re_nsub++;
3747  regnum++;
3748 
3749  if (COMPILE_STACK_FULL)
3750  {
3751  RETALLOC (compile_stack.stack, compile_stack.size << 1,
3752  compile_stack_elt_t);
3753  if (compile_stack.stack == NULL) return REG_ESPACE;
3754 
3755  compile_stack.size <<= 1;
3756  }
3757 
3758  /* These are the values to restore when we hit end of this
3759  group. They are all relative offsets, so that if the
3760  whole pattern moves because of realloc, they will still
3761  be valid. */
3762  COMPILE_STACK_TOP.begalt_offset = begalt - COMPILED_BUFFER_VAR;
3763  COMPILE_STACK_TOP.fixup_alt_jump
3764  = fixup_alt_jump ? fixup_alt_jump - COMPILED_BUFFER_VAR + 1 : 0;
3765  COMPILE_STACK_TOP.laststart_offset = b - COMPILED_BUFFER_VAR;
3766  COMPILE_STACK_TOP.regnum = regnum;
3767 
3768  /* We will eventually replace the 0 with the number of
3769  groups inner to this one. But do not push a
3770  start_memory for groups beyond the last one we can
3771  represent in the compiled pattern. */
3772  if (regnum <= MAX_REGNUM)
3773  {
3774  COMPILE_STACK_TOP.inner_group_offset = b
3775  - COMPILED_BUFFER_VAR + 2;
3776  BUF_PUSH_3 (start_memory, regnum, 0);
3777  }
3778 
3779  compile_stack.avail++;
3780 
3781  fixup_alt_jump = 0;
3782  laststart = 0;
3783  begalt = b;
3784  /* If we've reached MAX_REGNUM groups, then this open
3785  won't actually generate any code, so we'll have to
3786  clear pending_exact explicitly. */
3787  pending_exact = 0;
3788  break;
3789 
3790 
3791  case ')':
3792  if (syntax & RE_NO_BK_PARENS) goto normal_backslash;
3793 
3794  if (COMPILE_STACK_EMPTY)
3795  {
3796  if (syntax & RE_UNMATCHED_RIGHT_PAREN_ORD)
3797  goto normal_backslash;
3798  else
3799  FREE_STACK_RETURN (REG_ERPAREN);
3800  }
3801 
3802  handle_close:
3803  if (fixup_alt_jump)
3804  { /* Push a dummy failure point at the end of the
3805  alternative for a possible future
3806  `pop_failure_jump' to pop. See comments at
3807  `push_dummy_failure' in `re_match_2'. */
3808  BUF_PUSH (push_dummy_failure);
3809 
3810  /* We allocated space for this jump when we assigned
3811  to `fixup_alt_jump', in the `handle_alt' case below. */
3812  STORE_JUMP (jump_past_alt, fixup_alt_jump, b - 1);
3813  }
3814 
3815  /* See similar code for backslashed left paren above. */
3816  if (COMPILE_STACK_EMPTY)
3817  {
3818  if (syntax & RE_UNMATCHED_RIGHT_PAREN_ORD)
3819  goto normal_char;
3820  else
3821  FREE_STACK_RETURN (REG_ERPAREN);
3822  }
3823 
3824  /* Since we just checked for an empty stack above, this
3825  ``can't happen''. */
3826  assert (compile_stack.avail != 0);
3827  {
3828  /* We don't just want to restore into `regnum', because
3829  later groups should continue to be numbered higher,
3830  as in `(ab)c(de)' -- the second group is #2. */
3831  regnum_t this_group_regnum;
3832 
3833  compile_stack.avail--;
3834  begalt = COMPILED_BUFFER_VAR + COMPILE_STACK_TOP.begalt_offset;
3835  fixup_alt_jump
3836  = COMPILE_STACK_TOP.fixup_alt_jump
3837  ? COMPILED_BUFFER_VAR + COMPILE_STACK_TOP.fixup_alt_jump - 1
3838  : 0;
3839  laststart = COMPILED_BUFFER_VAR + COMPILE_STACK_TOP.laststart_offset;
3840  this_group_regnum = COMPILE_STACK_TOP.regnum;
3841  /* If we've reached MAX_REGNUM groups, then this open
3842  won't actually generate any code, so we'll have to
3843  clear pending_exact explicitly. */
3844  pending_exact = 0;
3845 
3846  /* We're at the end of the group, so now we know how many
3847  groups were inside this one. */
3848  if (this_group_regnum <= MAX_REGNUM)
3849  {
3850  UCHAR_T *inner_group_loc
3851  = COMPILED_BUFFER_VAR + COMPILE_STACK_TOP.inner_group_offset;
3852 
3853  *inner_group_loc = regnum - this_group_regnum;
3854  BUF_PUSH_3 (stop_memory, this_group_regnum,
3855  regnum - this_group_regnum);
3856  }
3857  }
3858  break;
3859 
3860 
3861  case '|': /* `\|'. */
3862  if (syntax & RE_LIMITED_OPS || syntax & RE_NO_BK_VBAR)
3863  goto normal_backslash;
3864  handle_alt:
3865  if (syntax & RE_LIMITED_OPS)
3866  goto normal_char;
3867 
3868  /* Insert before the previous alternative a jump which
3869  jumps to this alternative if the former fails. */
3870  GET_BUFFER_SPACE (1 + OFFSET_ADDRESS_SIZE);
3871  INSERT_JUMP (on_failure_jump, begalt,
3872  b + 2 + 2 * OFFSET_ADDRESS_SIZE);
3873  pending_exact = 0;
3874  b += 1 + OFFSET_ADDRESS_SIZE;
3875 
3876  /* The alternative before this one has a jump after it
3877  which gets executed if it gets matched. Adjust that
3878  jump so it will jump to this alternative's analogous
3879  jump (put in below, which in turn will jump to the next
3880  (if any) alternative's such jump, etc.). The last such
3881  jump jumps to the correct final destination. A picture:
3882  _____ _____
3883  | | | |
3884  | v | v
3885  a | b | c
3886 
3887  If we are at `b', then fixup_alt_jump right now points to a
3888  three-byte space after `a'. We'll put in the jump, set
3889  fixup_alt_jump to right after `b', and leave behind three
3890  bytes which we'll fill in when we get to after `c'. */
3891 
3892  if (fixup_alt_jump)
3893  STORE_JUMP (jump_past_alt, fixup_alt_jump, b);
3894 
3895  /* Mark and leave space for a jump after this alternative,
3896  to be filled in later either by next alternative or
3897  when know we're at the end of a series of alternatives. */
3898  fixup_alt_jump = b;
3899  GET_BUFFER_SPACE (1 + OFFSET_ADDRESS_SIZE);
3900  b += 1 + OFFSET_ADDRESS_SIZE;
3901 
3902  laststart = 0;
3903  begalt = b;
3904  break;
3905 
3906 
3907  case '{':
3908  /* If \{ is a literal. */
3909  if (!(syntax & RE_INTERVALS)
3910  /* If we're at `\{' and it's not the open-interval
3911  operator. */
3912  || (syntax & RE_NO_BK_BRACES))
3913  goto normal_backslash;
3914 
3915  handle_interval:
3916  {
3917  /* If got here, then the syntax allows intervals. */
3918 
3919  /* At least (most) this many matches must be made. */
3920  int lower_bound = -1, upper_bound = -1;
3921 
3922  /* Place in the uncompiled pattern (i.e., just after
3923  the '{') to go back to if the interval is invalid. */
3924  const CHAR_T *beg_interval = p;
3925 
3926  if (p == pend)
3927  goto invalid_interval;
3928 
3929  GET_UNSIGNED_NUMBER (lower_bound);
3930 
3931  if (c == ',')
3932  {
3933  GET_UNSIGNED_NUMBER (upper_bound);
3934  if (upper_bound < 0)
3935  upper_bound = RE_DUP_MAX;
3936  }
3937  else
3938  /* Interval such as `{1}' => match exactly once. */
3939  upper_bound = lower_bound;
3940 
3941  if (! (0 <= lower_bound && lower_bound <= upper_bound))
3942  goto invalid_interval;
3943 
3944  if (!(syntax & RE_NO_BK_BRACES))
3945  {
3946  if (c != '\\' || p == pend)
3947  goto invalid_interval;
3948  PATFETCH (c);
3949  }
3950 
3951  if (c != '}')
3952  goto invalid_interval;
3953 
3954  /* If it's invalid to have no preceding re. */
3955  if (!laststart)
3956  {
3957  if (syntax & RE_CONTEXT_INVALID_OPS
3958  && !(syntax & RE_INVALID_INTERVAL_ORD))
3959  FREE_STACK_RETURN (REG_BADRPT);
3960  else if (syntax & RE_CONTEXT_INDEP_OPS)
3961  laststart = b;
3962  else
3963  goto unfetch_interval;
3964  }
3965 
3966  /* We just parsed a valid interval. */
3967 
3968  if (RE_DUP_MAX < upper_bound)
3969  FREE_STACK_RETURN (REG_BADBR);
3970 
3971  /* If the upper bound is zero, don't want to succeed at
3972  all; jump from `laststart' to `b + 3', which will be
3973  the end of the buffer after we insert the jump. */
3974  /* ifdef WCHAR, 'b + 1 + OFFSET_ADDRESS_SIZE'
3975  instead of 'b + 3'. */
3976  if (upper_bound == 0)
3977  {
3978  GET_BUFFER_SPACE (1 + OFFSET_ADDRESS_SIZE);
3979  INSERT_JUMP (jump, laststart, b + 1
3980  + OFFSET_ADDRESS_SIZE);
3981  b += 1 + OFFSET_ADDRESS_SIZE;
3982  }
3983 
3984  /* Otherwise, we have a nontrivial interval. When
3985  we're all done, the pattern will look like:
3986  set_number_at <jump count> <upper bound>
3987  set_number_at <succeed_n count> <lower bound>
3988  succeed_n <after jump addr> <succeed_n count>
3989  <body of loop>
3990  jump_n <succeed_n addr> <jump count>
3991  (The upper bound and `jump_n' are omitted if
3992  `upper_bound' is 1, though.) */
3993  else
3994  { /* If the upper bound is > 1, we need to insert
3995  more at the end of the loop. */
3996  unsigned nbytes = 2 + 4 * OFFSET_ADDRESS_SIZE +
3997  (upper_bound > 1) * (2 + 4 * OFFSET_ADDRESS_SIZE);
3998 
3999  GET_BUFFER_SPACE (nbytes);
4000 
4001  /* Initialize lower bound of the `succeed_n', even
4002  though it will be set during matching by its
4003  attendant `set_number_at' (inserted next),
4004  because `re_compile_fastmap' needs to know.
4005  Jump to the `jump_n' we might insert below. */
4006  INSERT_JUMP2 (succeed_n, laststart,
4007  b + 1 + 2 * OFFSET_ADDRESS_SIZE
4008  + (upper_bound > 1) * (1 + 2 * OFFSET_ADDRESS_SIZE)
4009  , lower_bound);
4010  b += 1 + 2 * OFFSET_ADDRESS_SIZE;
4011 
4012  /* Code to initialize the lower bound. Insert
4013  before the `succeed_n'. The `5' is the last two
4014  bytes of this `set_number_at', plus 3 bytes of
4015  the following `succeed_n'. */
4016  /* ifdef WCHAR, The '1+2*OFFSET_ADDRESS_SIZE'
4017  is the 'set_number_at', plus '1+OFFSET_ADDRESS_SIZE'
4018  of the following `succeed_n'. */
4019  PREFIX(insert_op2) (set_number_at, laststart, 1
4020  + 2 * OFFSET_ADDRESS_SIZE, lower_bound, b);
4021  b += 1 + 2 * OFFSET_ADDRESS_SIZE;
4022 
4023  if (upper_bound > 1)
4024  { /* More than one repetition is allowed, so
4025  append a backward jump to the `succeed_n'
4026  that starts this interval.
4027 
4028  When we've reached this during matching,
4029  we'll have matched the interval once, so
4030  jump back only `upper_bound - 1' times. */
4031  STORE_JUMP2 (jump_n, b, laststart
4032  + 2 * OFFSET_ADDRESS_SIZE + 1,
4033  upper_bound - 1);
4034  b += 1 + 2 * OFFSET_ADDRESS_SIZE;
4035 
4036  /* The location we want to set is the second
4037  parameter of the `jump_n'; that is `b-2' as
4038  an absolute address. `laststart' will be
4039  the `set_number_at' we're about to insert;
4040  `laststart+3' the number to set, the source
4041  for the relative address. But we are
4042  inserting into the middle of the pattern --
4043  so everything is getting moved up by 5.
4044  Conclusion: (b - 2) - (laststart + 3) + 5,
4045  i.e., b - laststart.
4046 
4047  We insert this at the beginning of the loop
4048  so that if we fail during matching, we'll
4049  reinitialize the bounds. */
4050  PREFIX(insert_op2) (set_number_at, laststart,
4051  b - laststart,
4052  upper_bound - 1, b);
4053  b += 1 + 2 * OFFSET_ADDRESS_SIZE;
4054  }
4055  }
4056  pending_exact = 0;
4057  break;
4058 
4059  invalid_interval:
4060  if (!(syntax & RE_INVALID_INTERVAL_ORD))
4061  FREE_STACK_RETURN (p == pend ? REG_EBRACE : REG_BADBR);
4062  unfetch_interval:
4063  /* Match the characters as literals. */
4064  p = beg_interval;
4065  c = '{';
4066  if (syntax & RE_NO_BK_BRACES)
4067  goto normal_char;
4068  else
4069  goto normal_backslash;
4070  }
4071 
4072 #ifdef emacs
4073  /* There is no way to specify the before_dot and after_dot
4074  operators. rms says this is ok. --karl */
4075  case '=':
4076  BUF_PUSH (at_dot);
4077  break;
4078 
4079  case 's':
4080  laststart = b;
4081  PATFETCH (c);
4082  BUF_PUSH_2 (syntaxspec, syntax_spec_code[c]);
4083  break;
4084 
4085  case 'S':
4086  laststart = b;
4087  PATFETCH (c);
4088  BUF_PUSH_2 (notsyntaxspec, syntax_spec_code[c]);
4089  break;
4090 #endif /* emacs */
4091 
4092 
4093  case 'w':
4094  if (syntax & RE_NO_GNU_OPS)
4095  goto normal_char;
4096  laststart = b;
4097  BUF_PUSH (wordchar);
4098  break;
4099 
4100 
4101  case 'W':
4102  if (syntax & RE_NO_GNU_OPS)
4103  goto normal_char;
4104  laststart = b;
4105  BUF_PUSH (notwordchar);
4106  break;
4107 
4108 
4109  case '<':
4110  if (syntax & RE_NO_GNU_OPS)
4111  goto normal_char;
4112  BUF_PUSH (wordbeg);
4113  break;
4114 
4115  case '>':
4116  if (syntax & RE_NO_GNU_OPS)
4117  goto normal_char;
4118  BUF_PUSH (wordend);
4119  break;
4120 
4121  case 'b':
4122  if (syntax & RE_NO_GNU_OPS)
4123  goto normal_char;
4124  BUF_PUSH (wordbound);
4125  break;
4126 
4127  case 'B':
4128  if (syntax & RE_NO_GNU_OPS)
4129  goto normal_char;
4130  BUF_PUSH (notwordbound);
4131  break;
4132 
4133  case '`':
4134  if (syntax & RE_NO_GNU_OPS)
4135  goto normal_char;
4136  BUF_PUSH (begbuf);
4137  break;
4138 
4139  case '\'':
4140  if (syntax & RE_NO_GNU_OPS)
4141  goto normal_char;
4142  BUF_PUSH (endbuf);
4143  break;
4144 
4145  case '1': case '2': case '3': case '4': case '5':
4146  case '6': case '7': case '8': case '9':
4147  if (syntax & RE_NO_BK_REFS)
4148  goto normal_char;
4149 
4150  c1 = c - '0';
4151 
4152  if (c1 > regnum)
4153  FREE_STACK_RETURN (REG_ESUBREG);
4154 
4155  /* Can't back reference to a subexpression if inside of it. */
4156  if (group_in_compile_stack (compile_stack, (regnum_t) c1))
4157  goto normal_char;
4158 
4159  laststart = b;
4160  BUF_PUSH_2 (duplicate, c1);
4161  break;
4162 
4163 
4164  case '+':
4165  case '?':
4166  if (syntax & RE_BK_PLUS_QM)
4167  goto handle_plus;
4168  else
4169  goto normal_backslash;
4170 
4171  default:
4172  normal_backslash:
4173  /* You might think it would be useful for \ to mean
4174  not to translate; but if we don't translate it
4175  it will never match anything. */
4176  c = TRANSLATE (c);
4177  goto normal_char;
4178  }
4179  break;
4180 
4181 
4182  default:
4183  /* Expects the character in `c'. */
4184  normal_char:
4185  /* If no exactn currently being built. */
4186  if (!pending_exact
4187 #ifdef WCHAR
4188  /* If last exactn handle binary(or character) and
4189  new exactn handle character(or binary). */
4190  || is_exactn_bin != is_binary[p - 1 - pattern]
4191 #endif /* WCHAR */
4192 
4193  /* If last exactn not at current position. */
4194  || pending_exact + *pending_exact + 1 != b
4195 
4196  /* We have only one byte following the exactn for the count. */
4197  || *pending_exact == (1 << BYTEWIDTH) - 1
4198 
4199  /* If followed by a repetition operator. */
4200  || *p == '*' || *p == '^'
4201  || ((syntax & RE_BK_PLUS_QM)
4202  ? *p == '\\' && (p[1] == '+' || p[1] == '?')
4203  : (*p == '+' || *p == '?'))
4204  || ((syntax & RE_INTERVALS)
4205  && ((syntax & RE_NO_BK_BRACES)
4206  ? *p == '{'
4207  : (p[0] == '\\' && p[1] == '{'))))
4208  {
4209  /* Start building a new exactn. */
4210 
4211  laststart = b;
4212 
4213 #ifdef WCHAR
4214  /* Is this exactn binary data or character? */
4215  is_exactn_bin = is_binary[p - 1 - pattern];
4216  if (is_exactn_bin)
4217  BUF_PUSH_2 (exactn_bin, 0);
4218  else
4219  BUF_PUSH_2 (exactn, 0);
4220 #else
4221  BUF_PUSH_2 (exactn, 0);
4222 #endif /* WCHAR */
4223  pending_exact = b - 1;
4224  }
4225 
4226  BUF_PUSH (c);
4227  (*pending_exact)++;
4228  break;
4229  } /* switch (c) */
4230  } /* while p != pend */
4231 
4232 
4233  /* Through the pattern now. */
4234 
4235  if (fixup_alt_jump)
4236  STORE_JUMP (jump_past_alt, fixup_alt_jump, b);
4237 
4238  if (!COMPILE_STACK_EMPTY)
4239  FREE_STACK_RETURN (REG_EPAREN);
4240 
4241  /* If we don't want backtracking, force success
4242  the first time we reach the end of the compiled pattern. */
4243  if (syntax & RE_NO_POSIX_BACKTRACKING)
4244  BUF_PUSH (succeed);
4245 
4246 #ifdef WCHAR
4247  free (pattern);
4248  free (mbs_offset);
4249  free (is_binary);
4250 #endif
4251  free (compile_stack.stack);
4252 
4253  /* We have succeeded; set the length of the buffer. */
4254 #ifdef WCHAR
4255  bufp->used = (uintptr_t) b - (uintptr_t) COMPILED_BUFFER_VAR;
4256 #else
4257  bufp->used = b - bufp->buffer;
4258 #endif
4259 
4260 #ifdef DEBUG
4261  if (debug)
4262  {
4263  DEBUG_PRINT1 ("\nCompiled pattern: \n");
4264  PREFIX(print_compiled_pattern) (bufp);
4265  }
4266 #endif /* DEBUG */
4267 
4268 #ifndef MATCH_MAY_ALLOCATE
4269  /* Initialize the failure stack to the largest possible stack. This
4270  isn't necessary unless we're trying to avoid calling alloca in
4271  the search and match routines. */
4272  {
4273  int num_regs = bufp->re_nsub + 1;
4274 
4275  /* Since DOUBLE_FAIL_STACK refuses to double only if the current size
4276  is strictly greater than re_max_failures, the largest possible stack
4277  is 2 * re_max_failures failure points. */
4278  if (fail_stack.size < (2 * re_max_failures * MAX_FAILURE_ITEMS))
4279  {
4280  fail_stack.size = (2 * re_max_failures * MAX_FAILURE_ITEMS);
4281 
4282 # ifdef emacs
4283  if (! fail_stack.stack)
4284  fail_stack.stack
4285  = (PREFIX(fail_stack_elt_t) *) xmalloc (fail_stack.size
4286  * sizeof (PREFIX(fail_stack_elt_t)));
4287  else
4288  fail_stack.stack
4289  = (PREFIX(fail_stack_elt_t) *) xrealloc (fail_stack.stack,
4290  (fail_stack.size
4291  * sizeof (PREFIX(fail_stack_elt_t))));
4292 # else /* not emacs */
4293  if (! fail_stack.stack)
4294  fail_stack.stack
4295  = (PREFIX(fail_stack_elt_t) *) malloc (fail_stack.size
4296  * sizeof (PREFIX(fail_stack_elt_t)));
4297  else
4298  fail_stack.stack
4299  = (PREFIX(fail_stack_elt_t) *) realloc (fail_stack.stack,
4300  (fail_stack.size
4301  * sizeof (PREFIX(fail_stack_elt_t))));
4302 # endif /* not emacs */
4303  }
4304 
4305  PREFIX(regex_grow_registers) (num_regs);
4306  }
4307 #endif /* not MATCH_MAY_ALLOCATE */
4308 
4309  return REG_NOERROR;
4310 } /* regex_compile */
4311 
4312 /* Subroutines for `regex_compile'. */
4313 
4314 /* Store OP at LOC followed by two-byte integer parameter ARG. */
4315 /* ifdef WCHAR, integer parameter is 1 wchar_t. */
4316 
4317 static void
4318 PREFIX(store_op1) (op, loc, arg)
4319  re_opcode_t op;
4320  UCHAR_T *loc;
4321  int arg;
4322 {
4323  *loc = (UCHAR_T) op;
4324  STORE_NUMBER (loc + 1, arg);
4325 }
4326 
4327 
4328 /* Like `store_op1', but for two two-byte parameters ARG1 and ARG2. */
4329 /* ifdef WCHAR, integer parameter is 1 wchar_t. */
4330 
4331 static void
4332 PREFIX(store_op2) (op, loc, arg1, arg2)
4333  re_opcode_t op;
4334  UCHAR_T *loc;
4335  int arg1, arg2;
4336 {
4337  *loc = (UCHAR_T) op;
4338  STORE_NUMBER (loc + 1, arg1);
4339  STORE_NUMBER (loc + 1 + OFFSET_ADDRESS_SIZE, arg2);
4340 }
4341 
4342 
4343 /* Copy the bytes from LOC to END to open up three bytes of space at LOC
4344  for OP followed by two-byte integer parameter ARG. */
4345 /* ifdef WCHAR, integer parameter is 1 wchar_t. */
4346 
4347 static void
4348 PREFIX(insert_op1) (op, loc, arg, end)
4349  re_opcode_t op;
4350  UCHAR_T *loc;
4351  int arg;
4352  UCHAR_T *end;
4353 {
4354  register UCHAR_T *pfrom = end;
4355  register UCHAR_T *pto = end + 1 + OFFSET_ADDRESS_SIZE;
4356 
4357  while (pfrom != loc)
4358  *--pto = *--pfrom;
4359 
4360  PREFIX(store_op1) (op, loc, arg);
4361 }
4362 
4363 
4364 /* Like `insert_op1', but for two two-byte parameters ARG1 and ARG2. */
4365 /* ifdef WCHAR, integer parameter is 1 wchar_t. */
4366 
4367 static void
4368 PREFIX(insert_op2) (op, loc, arg1, arg2, end)
4369  re_opcode_t op;
4370  UCHAR_T *loc;
4371  int arg1, arg2;
4372  UCHAR_T *end;
4373 {
4374  register UCHAR_T *pfrom = end;
4375  register UCHAR_T *pto = end + 1 + 2 * OFFSET_ADDRESS_SIZE;
4376 
4377  while (pfrom != loc)
4378  *--pto = *--pfrom;
4379 
4380  PREFIX(store_op2) (op, loc, arg1, arg2);
4381 }
4382 
4383 
4384 /* P points to just after a ^ in PATTERN. Return true if that ^ comes
4385  after an alternative or a begin-subexpression. We assume there is at
4386  least one character before the ^. */
4387 
4388 static boolean
4389 PREFIX(at_begline_loc_p) (pattern, p, syntax)
4390  const CHAR_T *pattern, *p;
4392 {
4393  const CHAR_T *prev = p - 2;
4394  boolean prev_prev_backslash = prev > pattern && prev[-1] == '\\';
4395 
4396  return
4397  /* After a subexpression? */
4398  (*prev == '(' && (syntax & RE_NO_BK_PARENS || prev_prev_backslash))
4399  /* After an alternative? */
4400  || (*prev == '|' && (syntax & RE_NO_BK_VBAR || prev_prev_backslash));
4401 }
4402 
4403 
4404 /* The dual of at_begline_loc_p. This one is for $. We assume there is
4405  at least one character after the $, i.e., `P < PEND'. */
4406 
4407 static boolean
4408 PREFIX(at_endline_loc_p) (p, pend, syntax)
4409  const CHAR_T *p, *pend;
4411 {
4412  const CHAR_T *next = p;
4413  boolean next_backslash = *next == '\\';
4414  const CHAR_T *next_next = p + 1 < pend ? p + 1 : 0;
4415 
4416  return
4417  /* Before a subexpression? */
4418  (syntax & RE_NO_BK_PARENS ? *next == ')'
4419  : next_backslash && next_next && *next_next == ')')
4420  /* Before an alternative? */
4421  || (syntax & RE_NO_BK_VBAR ? *next == '|'
4422  : next_backslash && next_next && *next_next == '|');
4423 }
4424 
4425 #else /* not INSIDE_RECURSION */
4426 
4427 /* Returns true if REGNUM is in one of COMPILE_STACK's elements and
4428  false if it's not. */
4429 
4430 static boolean
4431 group_in_compile_stack (compile_stack, regnum)
4432  compile_stack_type compile_stack;
4433  regnum_t regnum;
4434 {
4435  int this_element;
4436 
4437  for (this_element = compile_stack.avail - 1;
4438  this_element >= 0;
4439  this_element--)
4440  if (compile_stack.stack[this_element].regnum == regnum)
4441  return true;
4442 
4443  return false;
4444 }
4445 #endif /* not INSIDE_RECURSION */
4446 
4447 #ifdef INSIDE_RECURSION
4448 
4449 #ifdef WCHAR
4450 /* This insert space, which size is "num", into the pattern at "loc".
4451  "end" must point the end of the allocated buffer. */
4452 static void
4453 insert_space (num, loc, end)
4454  int num;
4455  CHAR_T *loc;
4456  CHAR_T *end;
4457 {
4458  register CHAR_T *pto = end;
4459  register CHAR_T *pfrom = end - num;
4460 
4461  while (pfrom >= loc)
4462  *pto-- = *pfrom--;
4463 }
4464 #endif /* WCHAR */
4465 
4466 #ifdef WCHAR
4467 static reg_errcode_t
4468 wcs_compile_range (range_start_char, p_ptr, pend, translate, syntax, b,
4469  char_set)
4470  CHAR_T range_start_char;
4471  const CHAR_T **p_ptr, *pend;
4472  CHAR_T *char_set, *b;
4473  RE_TRANSLATE_TYPE translate;
4474  reg_syntax_t syntax;
4475 {
4476  const CHAR_T *p = *p_ptr;
4477  CHAR_T range_start, range_end;
4479 # ifdef _LIBC
4480  uint32_t nrules;
4481  uint32_t start_val, end_val;
4482 # endif
4483  if (p == pend)
4484  return REG_ERANGE;
4485 
4486 # ifdef _LIBC
4487  nrules = _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_NRULES);
4488  if (nrules != 0)
4489  {
4490  const char *collseq = (const char *) _NL_CURRENT(LC_COLLATE,
4491  _NL_COLLATE_COLLSEQWC);
4492  const unsigned char *extra = (const unsigned char *)
4493  _NL_CURRENT (LC_COLLATE, _NL_COLLATE_SYMB_EXTRAMB);
4494 
4495  if (range_start_char < -1)
4496  {
4497  /* range_start is a collating symbol. */
4498  int32_t *wextra;
4499  /* Retreive the index and get collation sequence value. */
4500  wextra = (int32_t*)(extra + char_set[-range_start_char]);
4501  start_val = wextra[1 + *wextra];
4502  }
4503  else
4504  start_val = collseq_table_lookup(collseq, TRANSLATE(range_start_char));
4505 
4506  end_val = collseq_table_lookup (collseq, TRANSLATE (p[0]));
4507 
4508  /* Report an error if the range is empty and the syntax prohibits
4509  this. */
4510  ret = ((syntax & RE_NO_EMPTY_RANGES)
4511  && (start_val > end_val))? REG_ERANGE : REG_NOERROR;
4512 
4513  /* Insert space to the end of the char_ranges. */
4514  insert_space(2, b - char_set[5] - 2, b - 1);
4515  *(b - char_set[5] - 2) = (wchar_t)start_val;
4516  *(b - char_set[5] - 1) = (wchar_t)end_val;
4517  char_set[4]++; /* ranges_index */
4518  }
4519  else
4520 # endif
4521  {
4522  range_start = (range_start_char >= 0)? TRANSLATE (range_start_char):
4523  range_start_char;
4524  range_end = TRANSLATE (p[0]);
4525  /* Report an error if the range is empty and the syntax prohibits
4526  this. */
4527  ret = ((syntax & RE_NO_EMPTY_RANGES)
4528  && (range_start > range_end))? REG_ERANGE : REG_NOERROR;
4529 
4530  /* Insert space to the end of the char_ranges. */
4531  insert_space(2, b - char_set[5] - 2, b - 1);
4532  *(b - char_set[5] - 2) = range_start;
4533  *(b - char_set[5] - 1) = range_end;
4534  char_set[4]++; /* ranges_index */
4535  }
4536  /* Have to increment the pointer into the pattern string, so the
4537  caller isn't still at the ending character. */
4538  (*p_ptr)++;
4539 
4540  return ret;
4541 }
4542 #else /* BYTE */
4543 /* Read the ending character of a range (in a bracket expression) from the
4544  uncompiled pattern *P_PTR (which ends at PEND). We assume the
4545  starting character is in `P[-2]'. (`P[-1]' is the character `-'.)
4546  Then we set the translation of all bits between the starting and
4547  ending characters (inclusive) in the compiled pattern B.
4548 
4549  Return an error code.
4550 
4551  We use these short variable names so we can use the same macros as
4552  `regex_compile' itself. */
4553 
4554 static reg_errcode_t
4555 byte_compile_range (range_start_char, p_ptr, pend, translate, syntax, b)
4556  unsigned int range_start_char;
4557  const char **p_ptr, *pend;
4558  RE_TRANSLATE_TYPE translate;
4559  reg_syntax_t syntax;
4560  unsigned char *b;
4561 {
4562  unsigned this_char;
4563  const char *p = *p_ptr;
4565 # if _LIBC
4566  const unsigned char *collseq;
4567  unsigned int start_colseq;
4568  unsigned int end_colseq;
4569 # else
4570  unsigned end_char;
4571 # endif
4572 
4573  if (p == pend)
4574  return REG_ERANGE;
4575 
4576  /* Have to increment the pointer into the pattern string, so the
4577  caller isn't still at the ending character. */
4578  (*p_ptr)++;
4579 
4580  /* Report an error if the range is empty and the syntax prohibits this. */
4581  ret = syntax & RE_NO_EMPTY_RANGES ? REG_ERANGE : REG_NOERROR;
4582 
4583 # if _LIBC
4584  collseq = (const unsigned char *) _NL_CURRENT (LC_COLLATE,
4585  _NL_COLLATE_COLLSEQMB);
4586 
4587  start_colseq = collseq[(unsigned char) TRANSLATE (range_start_char)];
4588  end_colseq = collseq[(unsigned char) TRANSLATE (p[0])];
4589  for (this_char = 0; this_char <= (unsigned char) -1; ++this_char)
4590  {
4591  unsigned int this_colseq = collseq[(unsigned char) TRANSLATE (this_char)];
4592 
4593  if (start_colseq <= this_colseq && this_colseq <= end_colseq)
4594  {
4595  SET_LIST_BIT (TRANSLATE (this_char));
4596  ret = REG_NOERROR;
4597  }
4598  }
4599 # else
4600  /* Here we see why `this_char' has to be larger than an `unsigned
4601  char' -- we would otherwise go into an infinite loop, since all
4602  characters <= 0xff. */
4603  range_start_char = TRANSLATE (range_start_char);
4604  /* TRANSLATE(p[0]) is casted to char (not unsigned char) in TRANSLATE,
4605  and some compilers cast it to int implicitly, so following for_loop
4606  may fall to (almost) infinite loop.
4607  e.g. If translate[p[0]] = 0xff, end_char may equals to 0xffffffff.
4608  To avoid this, we cast p[0] to unsigned int and truncate it. */
4609  end_char = ((unsigned)TRANSLATE(p[0]) & ((1 << BYTEWIDTH) - 1));
4610 
4611  for (this_char = range_start_char; this_char <= end_char; ++this_char)
4612  {
4613  SET_LIST_BIT (TRANSLATE (this_char));
4614  ret = REG_NOERROR;
4615  }
4616 # endif
4617 
4618  return ret;
4619 }
4620 #endif /* WCHAR */
4621 
4622 
4623 /* re_compile_fastmap computes a ``fastmap'' for the compiled pattern in
4624  BUFP. A fastmap records which of the (1 << BYTEWIDTH) possible
4625  characters can start a string that matches the pattern. This fastmap
4626  is used by re_search to skip quickly over impossible starting points.
4627 
4628  The caller must supply the address of a (1 << BYTEWIDTH)-byte data
4629  area as BUFP->fastmap.
4630 
4631  We set the `fastmap', `fastmap_accurate', and `can_be_null' fields in
4632  the pattern buffer.
4633 
4634  Returns 0 if we succeed, -2 if an internal error. */
4635 
4636 #ifdef WCHAR
4637 /* local function for re_compile_fastmap.
4638  truncate wchar_t character to char. */
4639 static unsigned char truncate_wchar (CHAR_T c);
4640 
4641 static unsigned char
4642 truncate_wchar (c)
4643  CHAR_T c;
4644 {
4645  unsigned char buf[MB_CUR_MAX];
4646  mbstate_t state;
4647  int retval;
4648  memset (&state, '\0', sizeof (state));
4649 # ifdef _LIBC
4650  retval = __wcrtomb (buf, c, &state);
4651 # else
4652  retval = wcrtomb (buf, c, &state);
4653 # endif
4654  return retval > 0 ? buf[0] : (unsigned char) c;
4655 }
4656 #endif /* WCHAR */
4657 
4658 static int
4659 PREFIX(re_compile_fastmap) (bufp)
4660  struct re_pattern_buffer *bufp;
4661 {
4662  int j, k;
4663 #ifdef MATCH_MAY_ALLOCATE
4664  PREFIX(fail_stack_type) fail_stack;
4665 #endif
4666 #ifndef REGEX_MALLOC
4667  char *destination;
4668 #endif
4669 
4670  register char *fastmap = bufp->fastmap;
4671 
4672 #ifdef WCHAR
4673  /* We need to cast pattern to (wchar_t*), because we casted this compiled
4674  pattern to (char*) in regex_compile. */
4675  UCHAR_T *pattern = (UCHAR_T*)bufp->buffer;
4676  register UCHAR_T *pend = (UCHAR_T*) (bufp->buffer + bufp->used);
4677 #else /* BYTE */
4678  UCHAR_T *pattern = bufp->buffer;
4679  register UCHAR_T *pend = pattern + bufp->used;
4680 #endif /* WCHAR */
4681  UCHAR_T *p = pattern;
4682 
4683 #ifdef REL_ALLOC
4684  /* This holds the pointer to the failure stack, when
4685  it is allocated relocatably. */
4686  fail_stack_elt_t *failure_stack_ptr;
4687 #endif
4688 
4689  /* Assume that each path through the pattern can be null until
4690  proven otherwise. We set this false at the bottom of switch
4691  statement, to which we get only if a particular path doesn't
4692  match the empty string. */
4693  boolean path_can_be_null = true;
4694 
4695  /* We aren't doing a `succeed_n' to begin with. */
4696  boolean succeed_n_p = false;
4697 
4698  assert (fastmap != NULL && p != NULL);
4699 
4700  INIT_FAIL_STACK ();
4701  bzero (fastmap, 1 << BYTEWIDTH); /* Assume nothing's valid. */
4702  bufp->fastmap_accurate = 1; /* It will be when we're done. */
4703  bufp->can_be_null = 0;
4704 
4705  while (1)
4706  {
4707  if (p == pend || *p == succeed)
4708  {
4709  /* We have reached the (effective) end of pattern. */
4710  if (!FAIL_STACK_EMPTY ())
4711  {
4712  bufp->can_be_null |= path_can_be_null;
4713 
4714  /* Reset for next path. */
4715  path_can_be_null = true;
4716 
4717  p = fail_stack.stack[--fail_stack.avail].pointer;
4718 
4719  continue;
4720  }
4721  else
4722  break;
4723  }
4724 
4725  /* We should never be about to go beyond the end of the pattern. */
4726  assert (p < pend);
4727 
4728  switch (SWITCH_ENUM_CAST ((re_opcode_t) *p++))
4729  {
4730 
4731  /* I guess the idea here is to simply not bother with a fastmap
4732  if a backreference is used, since it's too hard to figure out
4733  the fastmap for the corresponding group. Setting
4734  `can_be_null' stops `re_search_2' from using the fastmap, so
4735  that is all we do. */
4736  case duplicate:
4737  bufp->can_be_null = 1;
4738  goto done;
4739 
4740 
4741  /* Following are the cases which match a character. These end
4742  with `break'. */
4743 
4744 #ifdef WCHAR
4745  case exactn:
4746  fastmap[truncate_wchar(p[1])] = 1;
4747  break;
4748 #else /* BYTE */
4749  case exactn:
4750  fastmap[p[1]] = 1;
4751  break;
4752 #endif /* WCHAR */
4753 #ifdef MBS_SUPPORT
4754  case exactn_bin:
4755  fastmap[p[1]] = 1;
4756  break;
4757 #endif
4758 
4759 #ifdef WCHAR
4760  /* It is hard to distinguish fastmap from (multi byte) characters
4761  which depends on current locale. */
4762  case charset:
4763  case charset_not:
4764  case wordchar:
4765  case notwordchar:
4766  bufp->can_be_null = 1;
4767  goto done;
4768 #else /* BYTE */
4769  case charset:
4770  for (j = *p++ * BYTEWIDTH - 1; j >= 0; j--)
4771  if (p[j / BYTEWIDTH] & (1 << (j % BYTEWIDTH)))
4772  fastmap[j] = 1;
4773  break;
4774 
4775 
4776  case charset_not:
4777  /* Chars beyond end of map must be allowed. */
4778  for (j = *p * BYTEWIDTH; j < (1 << BYTEWIDTH); j++)
4779  fastmap[j] = 1;
4780 
4781  for (j = *p++ * BYTEWIDTH - 1; j >= 0; j--)
4782  if (!(p[j / BYTEWIDTH] & (1 << (j % BYTEWIDTH))))
4783  fastmap[j] = 1;
4784  break;
4785 
4786 
4787  case wordchar:
4788  for (j = 0; j < (1 << BYTEWIDTH); j++)
4789  if (SYNTAX (j) == Sword)
4790  fastmap[j] = 1;
4791  break;
4792 
4793 
4794  case notwordchar:
4795  for (j = 0; j < (1 << BYTEWIDTH); j++)
4796  if (SYNTAX (j) != Sword)
4797  fastmap[j] = 1;
4798  break;
4799 #endif /* WCHAR */
4800 
4801  case anychar:
4802  {
4803  int fastmap_newline = fastmap['\n'];
4804 
4805  /* `.' matches anything ... */
4806  for (j = 0; j < (1 << BYTEWIDTH); j++)
4807  fastmap[j] = 1;
4808 
4809  /* ... except perhaps newline. */
4810  if (!(bufp->syntax & RE_DOT_NEWLINE))
4811  fastmap['\n'] = fastmap_newline;
4812 
4813  /* Return if we have already set `can_be_null'; if we have,
4814  then the fastmap is irrelevant. Something's wrong here. */
4815  else if (bufp->can_be_null)
4816  goto done;
4817 
4818  /* Otherwise, have to check alternative paths. */
4819  break;
4820  }
4821 
4822 #ifdef emacs
4823  case syntaxspec:
4824  k = *p++;
4825  for (j = 0; j < (1 << BYTEWIDTH); j++)
4826  if (SYNTAX (j) == (enum syntaxcode) k)
4827  fastmap[j] = 1;
4828  break;
4829 
4830 
4831  case notsyntaxspec:
4832  k = *p++;
4833  for (j = 0; j < (1 << BYTEWIDTH); j++)
4834  if (SYNTAX (j) != (enum syntaxcode) k)
4835  fastmap[j] = 1;
4836  break;
4837 
4838 
4839  /* All cases after this match the empty string. These end with
4840  `continue'. */
4841 
4842 
4843  case before_dot:
4844  case at_dot:
4845  case after_dot:
4846  continue;
4847 #endif /* emacs */
4848 
4849 
4850  case no_op:
4851  case begline:
4852  case endline:
4853  case begbuf:
4854  case endbuf:
4855  case wordbound:
4856  case notwordbound:
4857  case wordbeg:
4858  case wordend:
4859  case push_dummy_failure:
4860  continue;
4861 
4862 
4863  case jump_n:
4864  case pop_failure_jump:
4865  case maybe_pop_jump:
4866  case jump:
4867  case jump_past_alt:
4868  case dummy_failure_jump:
4869  EXTRACT_NUMBER_AND_INCR (j, p);
4870  p += j;
4871  if (j > 0)
4872  continue;
4873 
4874  /* Jump backward implies we just went through the body of a
4875  loop and matched nothing. Opcode jumped to should be
4876  `on_failure_jump' or `succeed_n'. Just treat it like an
4877  ordinary jump. For a * loop, it has pushed its failure
4878  point already; if so, discard that as redundant. */
4879  if ((re_opcode_t) *p != on_failure_jump
4880  && (re_opcode_t) *p != succeed_n)
4881  continue;
4882 
4883  p++;
4884  EXTRACT_NUMBER_AND_INCR (j, p);
4885  p += j;
4886 
4887  /* If what's on the stack is where we are now, pop it. */
4888  if (!FAIL_STACK_EMPTY ()
4889  && fail_stack.stack[fail_stack.avail - 1].pointer == p)
4890  fail_stack.avail--;
4891 
4892  continue;
4893 
4894 
4895  case on_failure_jump:
4897  handle_on_failure_jump:
4898  EXTRACT_NUMBER_AND_INCR (j, p);
4899 
4900  /* For some patterns, e.g., `(a?)?', `p+j' here points to the
4901  end of the pattern. We don't want to push such a point,
4902  since when we restore it above, entering the switch will
4903  increment `p' past the end of the pattern. We don't need
4904  to push such a point since we obviously won't find any more
4905  fastmap entries beyond `pend'. Such a pattern can match
4906  the null string, though. */
4907  if (p + j < pend)
4908  {
4909  if (!PUSH_PATTERN_OP (p + j, fail_stack))
4910  {
4911  RESET_FAIL_STACK ();
4912  return -2;
4913  }
4914  }
4915  else
4916  bufp->can_be_null = 1;
4917 
4918  if (succeed_n_p)
4919  {
4920  EXTRACT_NUMBER_AND_INCR (k, p); /* Skip the n. */
4921  succeed_n_p = false;
4922  }
4923 
4924  continue;
4925 
4926 
4927  case succeed_n:
4928  /* Get to the number of times to succeed. */
4929  p += OFFSET_ADDRESS_SIZE;
4930 
4931  /* Increment p past the n for when k != 0. */
4932  EXTRACT_NUMBER_AND_INCR (k, p);
4933  if (k == 0)
4934  {
4935  p -= 2 * OFFSET_ADDRESS_SIZE;
4936  succeed_n_p = true; /* Spaghetti code alert. */
4937  goto handle_on_failure_jump;
4938  }
4939  continue;
4940 
4941 
4942  case set_number_at:
4943  p += 2 * OFFSET_ADDRESS_SIZE;
4944  continue;
4945 
4946 
4947  case start_memory:
4948  case stop_memory:
4949  p += 2;
4950  continue;
4951 
4952 
4953  default:
4954  abort (); /* We have listed all the cases. */
4955  } /* switch *p++ */
4956 
4957  /* Getting here means we have found the possible starting
4958  characters for one path of the pattern -- and that the empty
4959  string does not match. We need not follow this path further.
4960  Instead, look at the next alternative (remembered on the
4961  stack), or quit if no more. The test at the top of the loop
4962  does these things. */
4963  path_can_be_null = false;
4964  p = pend;
4965  } /* while p */
4966 
4967  /* Set `can_be_null' for the last path (also the first path, if the
4968  pattern is empty). */
4969  bufp->can_be_null |= path_can_be_null;
4970 
4971  done:
4972  RESET_FAIL_STACK ();
4973  return 0;
4974 }
4975 
4976 #else /* not INSIDE_RECURSION */
4977 
4978 int
4979 re_compile_fastmap (bufp)
4980  struct re_pattern_buffer *bufp;
4981 {
4982 # ifdef MBS_SUPPORT
4983  if (MB_CUR_MAX != 1)
4984  return wcs_re_compile_fastmap(bufp);
4985  else
4986 # endif
4987  return byte_re_compile_fastmap(bufp);
4988 } /* re_compile_fastmap */
4989 #ifdef _LIBC
4990 weak_alias (__re_compile_fastmap, re_compile_fastmap)
4991 #endif
4992 
4993 
4994 
4995 /* Set REGS to hold NUM_REGS registers, storing them in STARTS and
4996  ENDS. Subsequent matches using PATTERN_BUFFER and REGS will use
4997  this memory for recording register information. STARTS and ENDS
4998  must be allocated using the malloc library routine, and must each
4999  be at least NUM_REGS * sizeof (regoff_t) bytes long.
5000 
5001  If NUM_REGS == 0, then subsequent matches should allocate their own
5002  register data.
5003 
5004  Unless this function is called, the first search or match using
5005  PATTERN_BUFFER will allocate its own register data, without
5006  freeing the old data. */
5007 
5008 void
5009 re_set_registers (bufp, regs, num_regs, starts, ends)
5010  struct re_pattern_buffer *bufp;
5012  unsigned num_regs;
5015  if (num_regs)
5016  {
5018  regs->num_regs = num_regs;
5019  regs->start = starts;
5020  regs->end = ends;
5021  }
5022  else
5023  {
5025  regs->num_regs = 0;
5026  regs->start = regs->end = (regoff_t *) 0;
5027  }
5028 }
5029 #ifdef _LIBC
5030 weak_alias (__re_set_registers, re_set_registers)
5031 #endif
5032 
5033 
5034 /* Searching routines. */
5035 
5036 /* Like re_search_2, below, but only one string is specified, and
5037  doesn't let you say where to stop matching. */
5038 
5039 int
5040 re_search (bufp, string, size, startpos, range, regs)
5041  struct re_pattern_buffer *bufp;
5042  const char *string;
5043  int size, startpos, range;
5044  struct re_registers *regs;
5045 {
5046  return re_search_2 (bufp, NULL, 0, string, size, startpos, range,
5047  regs, size);
5048 }
5049 #ifdef _LIBC
5050 weak_alias (__re_search, re_search)
5051 #endif
5052 
5053 
5054 /* Using the compiled pattern in BUFP->buffer, first tries to match the
5055  virtual concatenation of STRING1 and STRING2, starting first at index
5056  STARTPOS, then at STARTPOS + 1, and so on.
5057 
5058  STRING1 and STRING2 have length SIZE1 and SIZE2, respectively.
5059 
5060  RANGE is how far to scan while trying to match. RANGE = 0 means try
5061  only at STARTPOS; in general, the last start tried is STARTPOS +
5062  RANGE.
5063 
5064  In REGS, return the indices of the virtual concatenation of STRING1
5065  and STRING2 that matched the entire BUFP->buffer and its contained
5066  subexpressions.
5067 
5068  Do not consider matching one past the index STOP in the virtual
5069  concatenation of STRING1 and STRING2.
5070 
5071  We return either the position in the strings at which the match was
5072  found, -1 if no match, or -2 if error (such as failure
5073  stack overflow). */
5074 
5075 int
5076 re_search_2 (bufp, string1, size1, string2, size2, startpos, range, regs, stop)
5077  struct re_pattern_buffer *bufp;
5078  const char *string1, *string2;
5079  int size1, size2;
5080  int startpos;
5081  int range;
5082  struct re_registers *regs;
5083  int stop;
5084 {
5085 # ifdef MBS_SUPPORT
5086  if (MB_CUR_MAX != 1)
5087  return wcs_re_search_2 (bufp, string1, size1, string2, size2, startpos,
5088  range, regs, stop);
5089  else
5090 # endif
5091  return byte_re_search_2 (bufp, string1, size1, string2, size2, startpos,
5092  range, regs, stop);
5093 } /* re_search_2 */
5094 #ifdef _LIBC
5095 weak_alias (__re_search_2, re_search_2)
5096 #endif
5097 
5098 #endif /* not INSIDE_RECURSION */
5099 
5100 #ifdef INSIDE_RECURSION
5101 
5102 #ifdef MATCH_MAY_ALLOCATE
5103 # define FREE_VAR(var) if (var) REGEX_FREE (var); var = NULL
5104 #else
5105 # define FREE_VAR(var) if (var) free (var); var = NULL
5106 #endif
5107 
5108 #ifdef WCHAR
5109 # define MAX_ALLOCA_SIZE 2000
5110 
5111 # define FREE_WCS_BUFFERS() \
5112  do { \
5113  if (size1 > MAX_ALLOCA_SIZE) \
5114  { \
5115  free (wcs_string1); \
5116  free (mbs_offset1); \
5117  } \
5118  else \
5119  { \
5120  FREE_VAR (wcs_string1); \
5121  FREE_VAR (mbs_offset1); \
5122  } \
5123  if (size2 > MAX_ALLOCA_SIZE) \
5124  { \
5125  free (wcs_string2); \
5126  free (mbs_offset2); \
5127  } \
5128  else \
5129  { \
5130  FREE_VAR (wcs_string2); \
5131  FREE_VAR (mbs_offset2); \
5132  } \
5133  } while (0)
5134 
5135 #endif
5136 
5137 
5138 static int
5139 PREFIX(re_search_2) (bufp, string1, size1, string2, size2, startpos, range,
5140  regs, stop)
5141  struct re_pattern_buffer *bufp;
5142  const char *string1, *string2;
5143  int size1, size2;
5144  int startpos;
5145  int range;
5146  struct re_registers *regs;
5147  int stop;
5148 {
5149  int val;
5150  register char *fastmap = bufp->fastmap;
5151  register RE_TRANSLATE_TYPE translate = bufp->translate;
5152  int total_size = size1 + size2;
5153  int endpos = startpos + range;
5154 #ifdef WCHAR
5155  /* We need wchar_t* buffers correspond to cstring1, cstring2. */
5156  wchar_t *wcs_string1 = NULL, *wcs_string2 = NULL;
5157  /* We need the size of wchar_t buffers correspond to csize1, csize2. */
5158  int wcs_size1 = 0, wcs_size2 = 0;
5159  /* offset buffer for optimizatoin. See convert_mbs_to_wc. */
5160  int *mbs_offset1 = NULL, *mbs_offset2 = NULL;
5161  /* They hold whether each wchar_t is binary data or not. */
5162  char *is_binary = NULL;
5163 #endif /* WCHAR */
5164 
5165  /* Check for out-of-range STARTPOS. */
5166  if (startpos < 0 || startpos > total_size)
5167  return -1;
5168 
5169  /* Fix up RANGE if it might eventually take us outside
5170  the virtual concatenation of STRING1 and STRING2.
5171  Make sure we won't move STARTPOS below 0 or above TOTAL_SIZE. */
5172  if (endpos < 0)
5173  range = 0 - startpos;
5174  else if (endpos > total_size)
5175  range = total_size - startpos;
5176 
5177  /* If the search isn't to be a backwards one, don't waste time in a
5178  search for a pattern that must be anchored. */
5179  if (bufp->used > 0 && range > 0
5180  && ((re_opcode_t) bufp->buffer[0] == begbuf
5181  /* `begline' is like `begbuf' if it cannot match at newlines. */
5182  || ((re_opcode_t) bufp->buffer[0] == begline
5183  && !bufp->newline_anchor)))
5184  {
5185  if (startpos > 0)
5186  return -1;
5187  else
5188  range = 1;
5189  }
5190 
5191 #ifdef emacs
5192  /* In a forward search for something that starts with \=.
5193  don't keep searching past point. */
5194  if (bufp->used > 0 && (re_opcode_t) bufp->buffer[0] == at_dot && range > 0)
5195  {
5196  range = PT - startpos;
5197  if (range <= 0)
5198  return -1;
5199  }
5200 #endif /* emacs */
5201 
5202  /* Update the fastmap now if not correct already. */
5203  if (fastmap && !bufp->fastmap_accurate)
5204  if (re_compile_fastmap (bufp) == -2)
5205  return -2;
5206 
5207 #ifdef WCHAR
5208  /* Allocate wchar_t array for wcs_string1 and wcs_string2 and
5209  fill them with converted string. */
5210  if (size1 != 0)
5211  {
5212  if (size1 > MAX_ALLOCA_SIZE)
5213  {
5214  wcs_string1 = TALLOC (size1 + 1, CHAR_T);
5215  mbs_offset1 = TALLOC (size1 + 1, int);
5216  is_binary = TALLOC (size1 + 1, char);
5217  }
5218  else
5219  {
5220  wcs_string1 = REGEX_TALLOC (size1 + 1, CHAR_T);
5221  mbs_offset1 = REGEX_TALLOC (size1 + 1, int);
5222  is_binary = REGEX_TALLOC (size1 + 1, char);
5223  }
5224  if (!wcs_string1 || !mbs_offset1 || !is_binary)
5225  {
5226  if (size1 > MAX_ALLOCA_SIZE)
5227  {
5228  free (wcs_string1);
5229  free (mbs_offset1);
5230  free (is_binary);
5231  }
5232  else
5233  {
5234  FREE_VAR (wcs_string1);
5235  FREE_VAR (mbs_offset1);
5236  FREE_VAR (is_binary);
5237  }
5238  return -2;
5239  }
5240  wcs_size1 = convert_mbs_to_wcs(wcs_string1, string1, size1,
5241  mbs_offset1, is_binary);
5242  wcs_string1[wcs_size1] = L'\0'; /* for a sentinel */
5243  if (size1 > MAX_ALLOCA_SIZE)
5244  free (is_binary);
5245  else
5246  FREE_VAR (is_binary);
5247  }
5248  if (size2 != 0)
5249  {
5250  if (size2 > MAX_ALLOCA_SIZE)
5251  {
5252  wcs_string2 = TALLOC (size2 + 1, CHAR_T);
5253  mbs_offset2 = TALLOC (size2 + 1, int);
5254  is_binary = TALLOC (size2 + 1, char);
5255  }
5256  else
5257  {
5258  wcs_string2 = REGEX_TALLOC (size2 + 1, CHAR_T);
5259  mbs_offset2 = REGEX_TALLOC (size2 + 1, int);
5260  is_binary = REGEX_TALLOC (size2 + 1, char);
5261  }
5262  if (!wcs_string2 || !mbs_offset2 || !is_binary)
5263  {
5264  FREE_WCS_BUFFERS ();
5265  if (size2 > MAX_ALLOCA_SIZE)
5266  free (is_binary);
5267  else
5268  FREE_VAR (is_binary);
5269  return -2;
5270  }
5271  wcs_size2 = convert_mbs_to_wcs(wcs_string2, string2, size2,
5272  mbs_offset2, is_binary);
5273  wcs_string2[wcs_size2] = L'\0'; /* for a sentinel */
5274  if (size2 > MAX_ALLOCA_SIZE)
5275  free (is_binary);
5276  else
5277  FREE_VAR (is_binary);
5278  }
5279 #endif /* WCHAR */
5280 
5281 
5282  /* Loop through the string, looking for a place to start matching. */
5283  for (;;)
5284  {
5285  /* If a fastmap is supplied, skip quickly over characters that
5286  cannot be the start of a match. If the pattern can match the
5287  null string, however, we don't need to skip characters; we want
5288  the first null string. */
5289  if (fastmap && startpos < total_size && !bufp->can_be_null)
5290  {
5291  if (range > 0) /* Searching forwards. */
5292  {
5293  register const char *d;
5294  register int lim = 0;
5295  int irange = range;
5296 
5297  if (startpos < size1 && startpos + range >= size1)
5298  lim = range - (size1 - startpos);
5299 
5300  d = (startpos >= size1 ? string2 - size1 : string1) + startpos;
5301 
5302  /* Written out as an if-else to avoid testing `translate'
5303  inside the loop. */
5304  if (translate)
5305  while (range > lim
5306  && !fastmap[(unsigned char)
5307  translate[(unsigned char) *d++]])
5308  range--;
5309  else
5310  while (range > lim && !fastmap[(unsigned char) *d++])
5311  range--;
5312 
5313  startpos += irange - range;
5314  }
5315  else /* Searching backwards. */
5316  {
5317  register CHAR_T c = (size1 == 0 || startpos >= size1
5318  ? string2[startpos - size1]
5319  : string1[startpos]);
5320 
5321  if (!fastmap[(unsigned char) TRANSLATE (c)])
5322  goto advance;
5323  }
5324  }
5325 
5326  /* If can't match the null string, and that's all we have left, fail. */
5327  if (range >= 0 && startpos == total_size && fastmap
5328  && !bufp->can_be_null)
5329  {
5330 #ifdef WCHAR
5331  FREE_WCS_BUFFERS ();
5332 #endif
5333  return -1;
5334  }
5335 
5336 #ifdef WCHAR
5337  val = wcs_re_match_2_internal (bufp, string1, size1, string2,
5338  size2, startpos, regs, stop,
5339  wcs_string1, wcs_size1,
5340  wcs_string2, wcs_size2,
5341  mbs_offset1, mbs_offset2);
5342 #else /* BYTE */
5343  val = byte_re_match_2_internal (bufp, string1, size1, string2,
5344  size2, startpos, regs, stop);
5345 #endif /* BYTE */
5346 
5347 #ifndef REGEX_MALLOC
5348 # ifdef C_ALLOCA
5349  alloca (0);
5350 # endif
5351 #endif
5352 
5353  if (val >= 0)
5354  {
5355 #ifdef WCHAR
5356  FREE_WCS_BUFFERS ();
5357 #endif
5358  return startpos;
5359  }
5360 
5361  if (val == -2)
5362  {
5363 #ifdef WCHAR
5364  FREE_WCS_BUFFERS ();
5365 #endif
5366  return -2;
5367  }
5368 
5369  advance:
5370  if (!range)
5371  break;
5372  else if (range > 0)
5373  {
5374  range--;
5375  startpos++;
5376  }
5377  else
5378  {
5379  range++;
5380  startpos--;
5381  }
5382  }
5383 #ifdef WCHAR
5384  FREE_WCS_BUFFERS ();
5385 #endif
5386  return -1;
5387 }
5388 
5389 #ifdef WCHAR
5390 /* This converts PTR, a pointer into one of the search wchar_t strings
5391  `string1' and `string2' into an multibyte string offset from the
5392  beginning of that string. We use mbs_offset to optimize.
5393  See convert_mbs_to_wcs. */
5394 # define POINTER_TO_OFFSET(ptr) \
5395  (FIRST_STRING_P (ptr) \
5396  ? ((regoff_t)(mbs_offset1 != NULL? mbs_offset1[(ptr)-string1] : 0)) \
5397  : ((regoff_t)((mbs_offset2 != NULL? mbs_offset2[(ptr)-string2] : 0) \
5398  + csize1)))
5399 #else /* BYTE */
5400 /* This converts PTR, a pointer into one of the search strings `string1'
5401  and `string2' into an offset from the beginning of that string. */
5402 # define POINTER_TO_OFFSET(ptr) \
5403  (FIRST_STRING_P (ptr) \
5404  ? ((regoff_t) ((ptr) - string1)) \
5405  : ((regoff_t) ((ptr) - string2 + size1)))
5406 #endif /* WCHAR */
5407 
5408 /* Macros for dealing with the split strings in re_match_2. */
5409 
5410 #define MATCHING_IN_FIRST_STRING (dend == end_match_1)
5411 
5412 /* Call before fetching a character with *d. This switches over to
5413  string2 if necessary. */
5414 #define PREFETCH() \
5415  while (d == dend) \
5416  { \
5417  /* End of string2 => fail. */ \
5418  if (dend == end_match_2) \
5419  goto fail; \
5420  /* End of string1 => advance to string2. */ \
5421  d = string2; \
5422  dend = end_match_2; \
5423  }
5424 
5425 /* Test if at very beginning or at very end of the virtual concatenation
5426  of `string1' and `string2'. If only one string, it's `string2'. */
5427 #define AT_STRINGS_BEG(d) ((d) == (size1 ? string1 : string2) || !size2)
5428 #define AT_STRINGS_END(d) ((d) == end2)
5429 
5430 
5431 /* Test if D points to a character which is word-constituent. We have
5432  two special cases to check for: if past the end of string1, look at
5433  the first character in string2; and if before the beginning of
5434  string2, look at the last character in string1. */
5435 #ifdef WCHAR
5436 /* Use internationalized API instead of SYNTAX. */
5437 # define WORDCHAR_P(d) \
5438  (iswalnum ((wint_t)((d) == end1 ? *string2 \
5439  : (d) == string2 - 1 ? *(end1 - 1) : *(d))) != 0 \
5440  || ((d) == end1 ? *string2 \
5441  : (d) == string2 - 1 ? *(end1 - 1) : *(d)) == L'_')
5442 #else /* BYTE */
5443 # define WORDCHAR_P(d) \
5444  (SYNTAX ((d) == end1 ? *string2 \
5445  : (d) == string2 - 1 ? *(end1 - 1) : *(d)) \
5446  == Sword)
5447 #endif /* WCHAR */
5448 
5449 /* Disabled due to a compiler bug -- see comment at case wordbound */
5450 #if 0
5451 /* Test if the character before D and the one at D differ with respect
5452  to being word-constituent. */
5453 #define AT_WORD_BOUNDARY(d) \
5454  (AT_STRINGS_BEG (d) || AT_STRINGS_END (d) \
5455  || WORDCHAR_P (d - 1) != WORDCHAR_P (d))
5456 #endif
5457 
5458 /* Free everything we malloc. */
5459 #ifdef MATCH_MAY_ALLOCATE
5460 # ifdef WCHAR
5461 # define FREE_VARIABLES() \
5462  do { \
5463  REGEX_FREE_STACK (fail_stack.stack); \
5464  FREE_VAR (regstart); \
5465  FREE_VAR (regend); \
5466  FREE_VAR (old_regstart); \
5467  FREE_VAR (old_regend); \
5468  FREE_VAR (best_regstart); \
5469  FREE_VAR (best_regend); \
5470  FREE_VAR (reg_info); \
5471  FREE_VAR (reg_dummy); \
5472  FREE_VAR (reg_info_dummy); \
5473  if (!cant_free_wcs_buf) \
5474  { \
5475  FREE_VAR (string1); \
5476  FREE_VAR (string2); \
5477  FREE_VAR (mbs_offset1); \
5478  FREE_VAR (mbs_offset2); \
5479  } \
5480  } while (0)
5481 # else /* BYTE */
5482 # define FREE_VARIABLES() \
5483  do { \
5484  REGEX_FREE_STACK (fail_stack.stack); \
5485  FREE_VAR (regstart); \
5486  FREE_VAR (regend); \
5487  FREE_VAR (old_regstart); \
5488  FREE_VAR (old_regend); \
5489  FREE_VAR (best_regstart); \
5490  FREE_VAR (best_regend); \
5491  FREE_VAR (reg_info); \
5492  FREE_VAR (reg_dummy); \
5493  FREE_VAR (reg_info_dummy); \
5494  } while (0)
5495 # endif /* WCHAR */
5496 #else
5497 # ifdef WCHAR
5498 # define FREE_VARIABLES() \
5499  do { \
5500  if (!cant_free_wcs_buf) \
5501  { \
5502  FREE_VAR (string1); \
5503  FREE_VAR (string2); \
5504  FREE_VAR (mbs_offset1); \
5505  FREE_VAR (mbs_offset2); \
5506  } \
5507  } while (0)
5508 # else /* BYTE */
5509 # define FREE_VARIABLES() ((void)0) /* Do nothing! But inhibit gcc warning. */
5510 # endif /* WCHAR */
5511 #endif /* not MATCH_MAY_ALLOCATE */
5512 
5513 /* These values must meet several constraints. They must not be valid
5514  register values; since we have a limit of 255 registers (because
5515  we use only one byte in the pattern for the register number), we can
5516  use numbers larger than 255. They must differ by 1, because of
5517  NUM_FAILURE_ITEMS above. And the value for the lowest register must
5518  be larger than the value for the highest register, so we do not try
5519  to actually save any registers when none are active. */
5520 #define NO_HIGHEST_ACTIVE_REG (1 << BYTEWIDTH)
5521 #define NO_LOWEST_ACTIVE_REG (NO_HIGHEST_ACTIVE_REG + 1)
5522 
5523 
5524 #else /* not INSIDE_RECURSION */
5525 /* Matching routines. */
5526 
5527 #ifndef emacs /* Emacs never uses this. */
5528 /* re_match is like re_match_2 except it takes only a single string. */
5529 
5530 int
5531 re_match (bufp, string, size, pos, regs)
5532  struct re_pattern_buffer *bufp;
5533  const char *string;
5534  int size, pos;
5535  struct re_registers *regs;
5536 {
5537  int result;
5538 # ifdef MBS_SUPPORT
5539  if (MB_CUR_MAX != 1)
5540  result = wcs_re_match_2_internal (bufp, NULL, 0, string, size,
5541  pos, regs, size,
5542  NULL, 0, NULL, 0, NULL, NULL);
5543  else
5544 # endif
5545  result = byte_re_match_2_internal (bufp, NULL, 0, string, size,
5546  pos, regs, size);
5547 # ifndef REGEX_MALLOC
5548 # ifdef C_ALLOCA
5549  alloca (0);
5550 # endif
5551 # endif
5552  return result;
5553 }
5554 # ifdef _LIBC
5555 weak_alias (__re_match, re_match)
5556 # endif
5557 #endif /* not emacs */
5558 
5559 #endif /* not INSIDE_RECURSION */
5560 
5561 #ifdef INSIDE_RECURSION
5562 static boolean PREFIX(group_match_null_string_p) _RE_ARGS ((UCHAR_T **p,
5563  UCHAR_T *end,
5564  PREFIX(register_info_type) *reg_info));
5565 static boolean PREFIX(alt_match_null_string_p) _RE_ARGS ((UCHAR_T *p,
5566  UCHAR_T *end,
5567  PREFIX(register_info_type) *reg_info));
5568 static boolean PREFIX(common_op_match_null_string_p) _RE_ARGS ((UCHAR_T **p,
5569  UCHAR_T *end,
5570  PREFIX(register_info_type) *reg_info));
5571 static int PREFIX(bcmp_translate) _RE_ARGS ((const CHAR_T *s1, const CHAR_T *s2,
5572  int len, char *translate));
5573 #else /* not INSIDE_RECURSION */
5574 
5575 /* re_match_2 matches the compiled pattern in BUFP against the
5576  the (virtual) concatenation of STRING1 and STRING2 (of length SIZE1
5577  and SIZE2, respectively). We start matching at POS, and stop
5578  matching at STOP.
5579 
5580  If REGS is non-null and the `no_sub' field of BUFP is nonzero, we
5581  store offsets for the substring each group matched in REGS. See the
5582  documentation for exactly how many groups we fill.
5583 
5584  We return -1 if no match, -2 if an internal error (such as the
5585  failure stack overflowing). Otherwise, we return the length of the
5586  matched substring. */
5587 
5588 int
5589 re_match_2 (bufp, string1, size1, string2, size2, pos, regs, stop)
5590  struct re_pattern_buffer *bufp;
5591  const char *string1, *string2;
5592  int size1, size2;
5593  int pos;
5594  struct re_registers *regs;
5595  int stop;
5596 {
5597  int result;
5598 # ifdef MBS_SUPPORT
5599  if (MB_CUR_MAX != 1)
5600  result = wcs_re_match_2_internal (bufp, string1, size1, string2, size2,
5601  pos, regs, stop,
5602  NULL, 0, NULL, 0, NULL, NULL);
5603  else
5604 # endif
5605  result = byte_re_match_2_internal (bufp, string1, size1, string2, size2,
5606  pos, regs, stop);
5607 
5608 #ifndef REGEX_MALLOC
5609 # ifdef C_ALLOCA
5610  alloca (0);
5611 # endif
5612 #endif
5613  return result;
5614 }
5615 #ifdef _LIBC
5616 weak_alias (__re_match_2, re_match_2)
5617 #endif
5618 
5619 #endif /* not INSIDE_RECURSION */
5620 
5621 #ifdef INSIDE_RECURSION
5622 
5623 #ifdef WCHAR
5624 static int count_mbs_length PARAMS ((int *, int));
5625 
5626 /* This check the substring (from 0, to length) of the multibyte string,
5627  to which offset_buffer correspond. And count how many wchar_t_characters
5628  the substring occupy. We use offset_buffer to optimization.
5629  See convert_mbs_to_wcs. */
5630 
5631 static int
5632 count_mbs_length(offset_buffer, length)
5633  int *offset_buffer;
5634  int length;
5635 {
5636  int upper, lower;
5637 
5638  /* Check whether the size is valid. */
5639  if (length < 0)
5640  return -1;
5641 
5642  if (offset_buffer == NULL)
5643  return 0;
5644 
5645  /* If there are no multibyte character, offset_buffer[i] == i.
5646  Optmize for this case. */
5647  if (offset_buffer[length] == length)
5648  return length;
5649 
5650  /* Set up upper with length. (because for all i, offset_buffer[i] >= i) */
5651  upper = length;
5652  lower = 0;
5653 
5654  while (true)
5655  {
5656  int middle = (lower + upper) / 2;
5657  if (middle == lower || middle == upper)
5658  break;
5659  if (offset_buffer[middle] > length)
5660  upper = middle;
5661  else if (offset_buffer[middle] < length)
5662  lower = middle;
5663  else
5664  return middle;
5665  }
5666 
5667  return -1;
5668 }
5669 #endif /* WCHAR */
5670 
5671 /* This is a separate function so that we can force an alloca cleanup
5672  afterwards. */
5673 #ifdef WCHAR
5674 static int
5675 wcs_re_match_2_internal (bufp, cstring1, csize1, cstring2, csize2, pos,
5676  regs, stop, string1, size1, string2, size2,
5677  mbs_offset1, mbs_offset2)
5678  struct re_pattern_buffer *bufp;
5679  const char *cstring1, *cstring2;
5680  int csize1, csize2;
5681  int pos;
5682  struct re_registers *regs;
5683  int stop;
5684  /* string1 == string2 == NULL means string1/2, size1/2 and
5685  mbs_offset1/2 need seting up in this function. */
5686  /* We need wchar_t* buffers correspond to cstring1, cstring2. */
5687  wchar_t *string1, *string2;
5688  /* We need the size of wchar_t buffers correspond to csize1, csize2. */
5689  int size1, size2;
5690  /* offset buffer for optimizatoin. See convert_mbs_to_wc. */
5691  int *mbs_offset1, *mbs_offset2;
5692 #else /* BYTE */
5693 static int
5694 byte_re_match_2_internal (bufp, string1, size1,string2, size2, pos,
5695  regs, stop)
5696  struct re_pattern_buffer *bufp;
5697  const char *string1, *string2;
5698  int size1, size2;
5699  int pos;
5700  struct re_registers *regs;
5701  int stop;
5702 #endif /* BYTE */
5703 {
5704  /* General temporaries. */
5705  int mcnt;
5706  UCHAR_T *p1;
5707 #ifdef WCHAR
5708  /* They hold whether each wchar_t is binary data or not. */
5709  char *is_binary = NULL;
5710  /* If true, we can't free string1/2, mbs_offset1/2. */
5711  int cant_free_wcs_buf = 1;
5712 #endif /* WCHAR */
5713 
5714  /* Just past the end of the corresponding string. */
5715  const CHAR_T *end1, *end2;
5716 
5717  /* Pointers into string1 and string2, just past the last characters in
5718  each to consider matching. */
5719  const CHAR_T *end_match_1, *end_match_2;
5720 
5721  /* Where we are in the data, and the end of the current string. */
5722  const CHAR_T *d, *dend;
5723 
5724  /* Where we are in the pattern, and the end of the pattern. */
5725 #ifdef WCHAR
5726  UCHAR_T *pattern, *p;
5727  register UCHAR_T *pend;
5728 #else /* BYTE */
5729  UCHAR_T *p = bufp->buffer;
5730  register UCHAR_T *pend = p + bufp->used;
5731 #endif /* WCHAR */
5732 
5733  /* Mark the opcode just after a start_memory, so we can test for an
5734  empty subpattern when we get to the stop_memory. */
5735  UCHAR_T *just_past_start_mem = 0;
5736 
5737  /* We use this to map every character in the string. */
5738  RE_TRANSLATE_TYPE translate = bufp->translate;
5739 
5740  /* Failure point stack. Each place that can handle a failure further
5741  down the line pushes a failure point on this stack. It consists of
5742  restart, regend, and reg_info for all registers corresponding to
5743  the subexpressions we're currently inside, plus the number of such
5744  registers, and, finally, two char *'s. The first char * is where
5745  to resume scanning the pattern; the second one is where to resume
5746  scanning the strings. If the latter is zero, the failure point is
5747  a ``dummy''; if a failure happens and the failure point is a dummy,
5748  it gets discarded and the next next one is tried. */
5749 #ifdef MATCH_MAY_ALLOCATE /* otherwise, this is global. */
5750  PREFIX(fail_stack_type) fail_stack;
5751 #endif
5752 #ifdef DEBUG
5753  static unsigned failure_id;
5754  unsigned nfailure_points_pushed = 0, nfailure_points_popped = 0;
5755 #endif
5756 
5757 #ifdef REL_ALLOC
5758  /* This holds the pointer to the failure stack, when
5759  it is allocated relocatably. */
5760  fail_stack_elt_t *failure_stack_ptr;
5761 #endif
5762 
5763  /* We fill all the registers internally, independent of what we
5764  return, for use in backreferences. The number here includes
5765  an element for register zero. */
5766  size_t num_regs = bufp->re_nsub + 1;
5767 
5768  /* The currently active registers. */
5769  active_reg_t lowest_active_reg = NO_LOWEST_ACTIVE_REG;
5770  active_reg_t highest_active_reg = NO_HIGHEST_ACTIVE_REG;
5771 
5772  /* Information on the contents of registers. These are pointers into
5773  the input strings; they record just what was matched (on this
5774  attempt) by a subexpression part of the pattern, that is, the
5775  regnum-th regstart pointer points to where in the pattern we began
5776  matching and the regnum-th regend points to right after where we
5777  stopped matching the regnum-th subexpression. (The zeroth register
5778  keeps track of what the whole pattern matches.) */
5779 #ifdef MATCH_MAY_ALLOCATE /* otherwise, these are global. */
5780  const CHAR_T **regstart, **regend;
5781 #endif
5782 
5783  /* If a group that's operated upon by a repetition operator fails to
5784  match anything, then the register for its start will need to be
5785  restored because it will have been set to wherever in the string we
5786  are when we last see its open-group operator. Similarly for a
5787  register's end. */
5788 #ifdef MATCH_MAY_ALLOCATE /* otherwise, these are global. */
5789  const CHAR_T **old_regstart, **old_regend;
5790 #endif
5791 
5792  /* The is_active field of reg_info helps us keep track of which (possibly
5793  nested) subexpressions we are currently in. The matched_something
5794  field of reg_info[reg_num] helps us tell whether or not we have
5795  matched any of the pattern so far this time through the reg_num-th
5796  subexpression. These two fields get reset each time through any
5797  loop their register is in. */
5798 #ifdef MATCH_MAY_ALLOCATE /* otherwise, this is global. */
5799  PREFIX(register_info_type) *reg_info;
5800 #endif
5801 
5802  /* The following record the register info as found in the above
5803  variables when we find a match better than any we've seen before.
5804  This happens as we backtrack through the failure points, which in
5805  turn happens only if we have not yet matched the entire string. */
5806  unsigned best_regs_set = false;
5807 #ifdef MATCH_MAY_ALLOCATE /* otherwise, these are global. */
5808  const CHAR_T **best_regstart, **best_regend;
5809 #endif
5810 
5811  /* Logically, this is `best_regend[0]'. But we don't want to have to
5812  allocate space for that if we're not allocating space for anything
5813  else (see below). Also, we never need info about register 0 for
5814  any of the other register vectors, and it seems rather a kludge to
5815  treat `best_regend' differently than the rest. So we keep track of
5816  the end of the best match so far in a separate variable. We
5817  initialize this to NULL so that when we backtrack the first time
5818  and need to test it, it's not garbage. */
5819  const CHAR_T *match_end = NULL;
5820 
5821  /* This helps SET_REGS_MATCHED avoid doing redundant work. */
5822  int set_regs_matched_done = 0;
5823 
5824  /* Used when we pop values we don't care about. */
5825 #ifdef MATCH_MAY_ALLOCATE /* otherwise, these are global. */
5826  const CHAR_T **reg_dummy;
5827  PREFIX(register_info_type) *reg_info_dummy;
5828 #endif
5829 
5830 #ifdef DEBUG
5831  /* Counts the total number of registers pushed. */
5832  unsigned num_regs_pushed = 0;
5833 #endif
5834 
5835  DEBUG_PRINT1 ("\n\nEntering re_match_2.\n");
5836 
5837  INIT_FAIL_STACK ();
5838 
5839 #ifdef MATCH_MAY_ALLOCATE
5840  /* Do not bother to initialize all the register variables if there are
5841  no groups in the pattern, as it takes a fair amount of time. If
5842  there are groups, we include space for register 0 (the whole
5843  pattern), even though we never use it, since it simplifies the
5844  array indexing. We should fix this. */
5845  if (bufp->re_nsub)
5846  {
5847  regstart = REGEX_TALLOC (num_regs, const CHAR_T *);
5848  regend = REGEX_TALLOC (num_regs, const CHAR_T *);
5849  old_regstart = REGEX_TALLOC (num_regs, const CHAR_T *);
5850  old_regend = REGEX_TALLOC (num_regs, const CHAR_T *);
5851  best_regstart = REGEX_TALLOC (num_regs, const CHAR_T *);
5852  best_regend = REGEX_TALLOC (num_regs, const CHAR_T *);
5853  reg_info = REGEX_TALLOC (num_regs, PREFIX(register_info_type));
5854  reg_dummy = REGEX_TALLOC (num_regs, const CHAR_T *);
5855  reg_info_dummy = REGEX_TALLOC (num_regs, PREFIX(register_info_type));
5856 
5857  if (!(regstart && regend && old_regstart && old_regend && reg_info
5858  && best_regstart && best_regend && reg_dummy && reg_info_dummy))
5859  {
5860  FREE_VARIABLES ();
5861  return -2;
5862  }
5863  }
5864  else
5865  {
5866  /* We must initialize all our variables to NULL, so that
5867  `FREE_VARIABLES' doesn't try to free them. */
5868  regstart = regend = old_regstart = old_regend = best_regstart
5869  = best_regend = reg_dummy = NULL;
5870  reg_info = reg_info_dummy = (PREFIX(register_info_type) *) NULL;
5871  }
5872 #endif /* MATCH_MAY_ALLOCATE */
5873 
5874  /* The starting position is bogus. */
5875 #ifdef WCHAR
5876  if (pos < 0 || pos > csize1 + csize2)
5877 #else /* BYTE */
5878  if (pos < 0 || pos > size1 + size2)
5879 #endif
5880  {
5881  FREE_VARIABLES ();
5882  return -1;
5883  }
5884 
5885 #ifdef WCHAR
5886  /* Allocate wchar_t array for string1 and string2 and
5887  fill them with converted string. */
5888  if (string1 == NULL && string2 == NULL)
5889  {
5890  /* We need seting up buffers here. */
5891 
5892  /* We must free wcs buffers in this function. */
5893  cant_free_wcs_buf = 0;
5894 
5895  if (csize1 != 0)
5896  {
5897  string1 = REGEX_TALLOC (csize1 + 1, CHAR_T);
5898  mbs_offset1 = REGEX_TALLOC (csize1 + 1, int);
5899  is_binary = REGEX_TALLOC (csize1 + 1, char);
5900  if (!string1 || !mbs_offset1 || !is_binary)
5901  {
5902  FREE_VAR (string1);
5903  FREE_VAR (mbs_offset1);
5904  FREE_VAR (is_binary);
5905  return -2;
5906  }
5907  }
5908  if (csize2 != 0)
5909  {
5910  string2 = REGEX_TALLOC (csize2 + 1, CHAR_T);
5911  mbs_offset2 = REGEX_TALLOC (csize2 + 1, int);
5912  is_binary = REGEX_TALLOC (csize2 + 1, char);
5913  if (!string2 || !mbs_offset2 || !is_binary)
5914  {
5915  FREE_VAR (string1);
5916  FREE_VAR (mbs_offset1);
5917  FREE_VAR (string2);
5918  FREE_VAR (mbs_offset2);
5919  FREE_VAR (is_binary);
5920  return -2;
5921  }
5922  size2 = convert_mbs_to_wcs(string2, cstring2, csize2,
5923  mbs_offset2, is_binary);
5924  string2[size2] = L'\0'; /* for a sentinel */
5925  FREE_VAR (is_binary);
5926  }
5927  }
5928 
5929  /* We need to cast pattern to (wchar_t*), because we casted this compiled
5930  pattern to (char*) in regex_compile. */
5931  p = pattern = (CHAR_T*)bufp->buffer;
5932  pend = (CHAR_T*)(bufp->buffer + bufp->used);
5933 
5934 #endif /* WCHAR */
5935 
5936  /* Initialize subexpression text positions to -1 to mark ones that no
5937  start_memory/stop_memory has been seen for. Also initialize the
5938  register information struct. */
5939  for (mcnt = 1; (unsigned) mcnt < num_regs; mcnt++)
5940  {
5941  regstart[mcnt] = regend[mcnt]
5942  = old_regstart[mcnt] = old_regend[mcnt] = REG_UNSET_VALUE;
5943 
5944  REG_MATCH_NULL_STRING_P (reg_info[mcnt]) = MATCH_NULL_UNSET_VALUE;
5945  IS_ACTIVE (reg_info[mcnt]) = 0;
5946  MATCHED_SOMETHING (reg_info[mcnt]) = 0;
5947  EVER_MATCHED_SOMETHING (reg_info[mcnt]) = 0;
5948  }
5949 
5950  /* We move `string1' into `string2' if the latter's empty -- but not if
5951  `string1' is null. */
5952  if (size2 == 0 && string1 != NULL)
5953  {
5954  string2 = string1;
5955  size2 = size1;
5956  string1 = 0;
5957  size1 = 0;
5958 #ifdef WCHAR
5959  mbs_offset2 = mbs_offset1;
5960  csize2 = csize1;
5961  mbs_offset1 = NULL;
5962  csize1 = 0;
5963 #endif
5964  }
5965  end1 = string1 + size1;
5966  end2 = string2 + size2;
5967 
5968  /* Compute where to stop matching, within the two strings. */
5969 #ifdef WCHAR
5970  if (stop <= csize1)
5971  {
5972  mcnt = count_mbs_length(mbs_offset1, stop);
5973  end_match_1 = string1 + mcnt;
5974  end_match_2 = string2;
5975  }
5976  else
5977  {
5978  if (stop > csize1 + csize2)
5979  stop = csize1 + csize2;
5980  end_match_1 = end1;
5981  mcnt = count_mbs_length(mbs_offset2, stop-csize1);
5982  end_match_2 = string2 + mcnt;
5983  }
5984  if (mcnt < 0)
5985  { /* count_mbs_length return error. */
5986  FREE_VARIABLES ();
5987  return -1;
5988  }
5989 #else
5990  if (stop <= size1)
5991  {
5992  end_match_1 = string1 + stop;
5993  end_match_2 = string2;
5994  }
5995  else
5996  {
5997  end_match_1 = end1;
5998  end_match_2 = string2 + stop - size1;
5999  }
6000 #endif /* WCHAR */
6001 
6002  /* `p' scans through the pattern as `d' scans through the data.
6003  `dend' is the end of the input string that `d' points within. `d'
6004  is advanced into the following input string whenever necessary, but
6005  this happens before fetching; therefore, at the beginning of the
6006  loop, `d' can be pointing at the end of a string, but it cannot
6007  equal `string2'. */
6008 #ifdef WCHAR
6009  if (size1 > 0 && pos <= csize1)
6010  {
6011  mcnt = count_mbs_length(mbs_offset1, pos);
6012  d = string1 + mcnt;
6013  dend = end_match_1;
6014  }
6015  else
6016  {
6017  mcnt = count_mbs_length(mbs_offset2, pos-csize1);
6018  d = string2 + mcnt;
6019  dend = end_match_2;
6020  }
6021 
6022  if (mcnt < 0)
6023  { /* count_mbs_length return error. */
6024  FREE_VARIABLES ();
6025  return -1;
6026  }
6027 #else
6028  if (size1 > 0 && pos <= size1)
6029  {
6030  d = string1 + pos;
6031  dend = end_match_1;
6032  }
6033  else
6034  {
6035  d = string2 + pos - size1;
6036  dend = end_match_2;
6037  }
6038 #endif /* WCHAR */
6039 
6040  DEBUG_PRINT1 ("The compiled pattern is:\n");
6041  DEBUG_PRINT_COMPILED_PATTERN (bufp, p, pend);
6042  DEBUG_PRINT1 ("The string to match is: `");
6043  DEBUG_PRINT_DOUBLE_STRING (d, string1, size1, string2, size2);
6044  DEBUG_PRINT1 ("'\n");
6045 
6046  /* This loops over pattern commands. It exits by returning from the
6047  function if the match is complete, or it drops through if the match
6048  fails at this starting point in the input data. */
6049  for (;;)
6050  {
6051 #ifdef _LIBC
6052  DEBUG_PRINT2 ("\n%p: ", p);
6053 #else
6054  DEBUG_PRINT2 ("\n0x%x: ", p);
6055 #endif
6056 
6057  if (p == pend)
6058  { /* End of pattern means we might have succeeded. */
6059  DEBUG_PRINT1 ("end of pattern ... ");
6060 
6061  /* If we haven't matched the entire string, and we want the
6062  longest match, try backtracking. */
6063  if (d != end_match_2)
6064  {
6065  /* 1 if this match ends in the same string (string1 or string2)
6066  as the best previous match. */
6067  boolean same_str_p = (FIRST_STRING_P (match_end)
6068  == MATCHING_IN_FIRST_STRING);
6069  /* 1 if this match is the best seen so far. */
6070  boolean best_match_p;
6071 
6072  /* AIX compiler got confused when this was combined
6073  with the previous declaration. */
6074  if (same_str_p)
6075  best_match_p = d > match_end;
6076  else
6077  best_match_p = !MATCHING_IN_FIRST_STRING;
6078 
6079  DEBUG_PRINT1 ("backtracking.\n");
6080 
6081  if (!FAIL_STACK_EMPTY ())
6082  { /* More failure points to try. */
6083 
6084  /* If exceeds best match so far, save it. */
6085  if (!best_regs_set || best_match_p)
6086  {
6087  best_regs_set = true;
6088  match_end = d;
6089 
6090  DEBUG_PRINT1 ("\nSAVING match as best so far.\n");
6091 
6092  for (mcnt = 1; (unsigned) mcnt < num_regs; mcnt++)
6093  {
6094  best_regstart[mcnt] = regstart[mcnt];
6095  best_regend[mcnt] = regend[mcnt];
6096  }
6097  }
6098  goto fail;
6099  }
6100 
6101  /* If no failure points, don't restore garbage. And if
6102  last match is real best match, don't restore second
6103  best one. */
6104  else if (best_regs_set && !best_match_p)
6105  {
6106  restore_best_regs:
6107  /* Restore best match. It may happen that `dend ==
6108  end_match_1' while the restored d is in string2.
6109  For example, the pattern `x.*y.*z' against the
6110  strings `x-' and `y-z-', if the two strings are
6111  not consecutive in memory. */
6112  DEBUG_PRINT1 ("Restoring best registers.\n");
6113 
6114  d = match_end;
6115  dend = ((d >= string1 && d <= end1)
6116  ? end_match_1 : end_match_2);
6117 
6118  for (mcnt = 1; (unsigned) mcnt < num_regs; mcnt++)
6119  {
6120  regstart[mcnt] = best_regstart[mcnt];
6121  regend[mcnt] = best_regend[mcnt];
6122  }
6123  }
6124  } /* d != end_match_2 */
6125 
6126  succeed_label:
6127  DEBUG_PRINT1 ("Accepting match.\n");
6128  /* If caller wants register contents data back, do it. */
6129  if (regs && !bufp->no_sub)
6130  {
6131  /* Have the register data arrays been allocated? */
6132  if (bufp->regs_allocated == REGS_UNALLOCATED)
6133  { /* No. So allocate them with malloc. We need one
6134  extra element beyond `num_regs' for the `-1' marker
6135  GNU code uses. */
6136  regs->num_regs = MAX (RE_NREGS, num_regs + 1);
6137  regs->start = TALLOC (regs->num_regs, regoff_t);
6138  regs->end = TALLOC (regs->num_regs, regoff_t);
6139  if (regs->start == NULL || regs->end == NULL)
6140  {
6141  FREE_VARIABLES ();
6142  return -2;
6143  }
6144  bufp->regs_allocated = REGS_REALLOCATE;
6145  }
6146  else if (bufp->regs_allocated == REGS_REALLOCATE)
6147  { /* Yes. If we need more elements than were already
6148  allocated, reallocate them. If we need fewer, just
6149  leave it alone. */
6150  if (regs->num_regs < num_regs + 1)
6151  {
6152  regs->num_regs = num_regs + 1;
6153  RETALLOC (regs->start, regs->num_regs, regoff_t);
6154  RETALLOC (regs->end, regs->num_regs, regoff_t);
6155  if (regs->start == NULL || regs->end == NULL)
6156  {
6157  FREE_VARIABLES ();
6158  return -2;
6159  }
6160  }
6161  }
6162  else
6163  {
6164  /* These braces fend off a "empty body in an else-statement"
6165  warning under GCC when assert expands to nothing. */
6166  assert (bufp->regs_allocated == REGS_FIXED);
6167  }
6168 
6169  /* Convert the pointer data in `regstart' and `regend' to
6170  indices. Register zero has to be set differently,
6171  since we haven't kept track of any info for it. */
6172  if (regs->num_regs > 0)
6173  {
6174  regs->start[0] = pos;
6175 #ifdef WCHAR
6176  if (MATCHING_IN_FIRST_STRING)
6177  regs->end[0] = mbs_offset1 != NULL ?
6178  mbs_offset1[d-string1] : 0;
6179  else
6180  regs->end[0] = csize1 + (mbs_offset2 != NULL ?
6181  mbs_offset2[d-string2] : 0);
6182 #else
6183  regs->end[0] = (MATCHING_IN_FIRST_STRING
6184  ? ((regoff_t) (d - string1))
6185  : ((regoff_t) (d - string2 + size1)));
6186 #endif /* WCHAR */
6187  }
6188 
6189  /* Go through the first `min (num_regs, regs->num_regs)'
6190  registers, since that is all we initialized. */
6191  for (mcnt = 1; (unsigned) mcnt < MIN (num_regs, regs->num_regs);
6192  mcnt++)
6193  {
6194  if (REG_UNSET (regstart[mcnt]) || REG_UNSET (regend[mcnt]))
6195  regs->start[mcnt] = regs->end[mcnt] = -1;
6196  else
6197  {
6198  regs->start[mcnt]
6199  = (regoff_t) POINTER_TO_OFFSET (regstart[mcnt]);
6200  regs->end[mcnt]
6201  = (regoff_t) POINTER_TO_OFFSET (regend[mcnt]);
6202  }
6203  }
6204 
6205  /* If the regs structure we return has more elements than
6206  were in the pattern, set the extra elements to -1. If
6207  we (re)allocated the registers, this is the case,
6208  because we always allocate enough to have at least one
6209  -1 at the end. */
6210  for (mcnt = num_regs; (unsigned) mcnt < regs->num_regs; mcnt++)
6211  regs->start[mcnt] = regs->end[mcnt] = -1;
6212  } /* regs && !bufp->no_sub */
6213 
6214  DEBUG_PRINT4 ("%u failure points pushed, %u popped (%u remain).\n",
6215  nfailure_points_pushed, nfailure_points_popped,
6216  nfailure_points_pushed - nfailure_points_popped);
6217  DEBUG_PRINT2 ("%u registers pushed.\n", num_regs_pushed);
6218 
6219 #ifdef WCHAR
6220  if (MATCHING_IN_FIRST_STRING)
6221  mcnt = mbs_offset1 != NULL ? mbs_offset1[d-string1] : 0;
6222  else
6223  mcnt = (mbs_offset2 != NULL ? mbs_offset2[d-string2] : 0) +
6224  csize1;
6225  mcnt -= pos;
6226 #else
6227  mcnt = d - pos - (MATCHING_IN_FIRST_STRING
6228  ? string1
6229  : string2 - size1);
6230 #endif /* WCHAR */
6231 
6232  DEBUG_PRINT2 ("Returning %d from re_match_2.\n", mcnt);
6233 
6234  FREE_VARIABLES ();
6235  return mcnt;
6236  }
6237 
6238  /* Otherwise match next pattern command. */
6239  switch (SWITCH_ENUM_CAST ((re_opcode_t) *p++))
6240  {
6241  /* Ignore these. Used to ignore the n of succeed_n's which
6242  currently have n == 0. */
6243  case no_op:
6244  DEBUG_PRINT1 ("EXECUTING no_op.\n");
6245  break;
6246 
6247  case succeed:
6248  DEBUG_PRINT1 ("EXECUTING succeed.\n");
6249  goto succeed_label;
6250 
6251  /* Match the next n pattern characters exactly. The following
6252  byte in the pattern defines n, and the n bytes after that
6253  are the characters to match. */
6254  case exactn:
6255 #ifdef MBS_SUPPORT
6256  case exactn_bin:
6257 #endif
6258  mcnt = *p++;
6259  DEBUG_PRINT2 ("EXECUTING exactn %d.\n", mcnt);
6260 
6261  /* This is written out as an if-else so we don't waste time
6262  testing `translate' inside the loop. */
6263  if (translate)
6264  {
6265  do
6266  {
6267  PREFETCH ();
6268 #ifdef WCHAR
6269  if (*d <= 0xff)
6270  {
6271  if ((UCHAR_T) translate[(unsigned char) *d++]
6272  != (UCHAR_T) *p++)
6273  goto fail;
6274  }
6275  else
6276  {
6277  if (*d++ != (CHAR_T) *p++)
6278  goto fail;
6279  }
6280 #else
6281  if ((UCHAR_T) translate[(unsigned char) *d++]
6282  != (UCHAR_T) *p++)
6283  goto fail;
6284 #endif /* WCHAR */
6285  }
6286  while (--mcnt);
6287  }
6288  else
6289  {
6290  do
6291  {
6292  PREFETCH ();
6293  if (*d++ != (CHAR_T) *p++) goto fail;
6294  }
6295  while (--mcnt);
6296  }
6297  SET_REGS_MATCHED ();
6298  break;
6299 
6300 
6301  /* Match any character except possibly a newline or a null. */
6302  case anychar:
6303  DEBUG_PRINT1 ("EXECUTING anychar.\n");
6304 
6305  PREFETCH ();
6306 
6307  if ((!(bufp->syntax & RE_DOT_NEWLINE) && TRANSLATE (*d) == '\n')
6308  || (bufp->syntax & RE_DOT_NOT_NULL && TRANSLATE (*d) == '\000'))
6309  goto fail;
6310 
6311  SET_REGS_MATCHED ();
6312  DEBUG_PRINT2 (" Matched `%ld'.\n", (long int) *d);
6313  d++;
6314  break;
6315 
6316 
6317  case charset:
6318  case charset_not:
6319  {
6320  register UCHAR_T c;
6321 #ifdef WCHAR
6322  unsigned int i, char_class_length, coll_symbol_length,
6323  equiv_class_length, ranges_length, chars_length, length;
6324  CHAR_T *workp, *workp2, *charset_top;
6325 #define WORK_BUFFER_SIZE 128
6326  CHAR_T str_buf[WORK_BUFFER_SIZE];
6327 # ifdef _LIBC
6328  uint32_t nrules;
6329 # endif /* _LIBC */
6330 #endif /* WCHAR */
6331  boolean not = (re_opcode_t) *(p - 1) == charset_not;
6332 
6333  DEBUG_PRINT2 ("EXECUTING charset%s.\n", not ? "_not" : "");
6334  PREFETCH ();
6335  c = TRANSLATE (*d); /* The character to match. */
6336 #ifdef WCHAR
6337 # ifdef _LIBC
6338  nrules = _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_NRULES);
6339 # endif /* _LIBC */
6340  charset_top = p - 1;
6341  char_class_length = *p++;
6342  coll_symbol_length = *p++;
6343  equiv_class_length = *p++;
6344  ranges_length = *p++;
6345  chars_length = *p++;
6346  /* p points charset[6], so the address of the next instruction
6347  (charset[l+m+n+2o+k+p']) equals p[l+m+n+2*o+p'],
6348  where l=length of char_classes, m=length of collating_symbol,
6349  n=equivalence_class, o=length of char_range,
6350  p'=length of character. */
6351  workp = p;
6352  /* Update p to indicate the next instruction. */
6353  p += char_class_length + coll_symbol_length+ equiv_class_length +
6354  2*ranges_length + chars_length;
6355 
6356  /* match with char_class? */
6357  for (i = 0; i < char_class_length ; i += CHAR_CLASS_SIZE)
6358  {
6359  wctype_t wctype;
6360  uintptr_t alignedp = ((uintptr_t)workp
6361  + __alignof__(wctype_t) - 1)
6362  & ~(uintptr_t)(__alignof__(wctype_t) - 1);
6363  wctype = *((wctype_t*)alignedp);
6364  workp += CHAR_CLASS_SIZE;
6365 # ifdef _LIBC
6366  if (__iswctype((wint_t)c, wctype))
6367  goto char_set_matched;
6368 # else
6369  if (iswctype((wint_t)c, wctype))
6370  goto char_set_matched;
6371 # endif
6372  }
6373 
6374  /* match with collating_symbol? */
6375 # ifdef _LIBC
6376  if (nrules != 0)
6377  {
6378  const unsigned char *extra = (const unsigned char *)
6379  _NL_CURRENT (LC_COLLATE, _NL_COLLATE_SYMB_EXTRAMB);
6380 
6381  for (workp2 = workp + coll_symbol_length ; workp < workp2 ;
6382  workp++)
6383  {
6384  int32_t *wextra;
6385  wextra = (int32_t*)(extra + *workp++);
6386  for (i = 0; i < *wextra; ++i)
6387  if (TRANSLATE(d[i]) != wextra[1 + i])
6388  break;
6389 
6390  if (i == *wextra)
6391  {
6392  /* Update d, however d will be incremented at
6393  char_set_matched:, we decrement d here. */
6394  d += i - 1;
6395  goto char_set_matched;
6396  }
6397  }
6398  }
6399  else /* (nrules == 0) */
6400 # endif
6401  /* If we can't look up collation data, we use wcscoll
6402  instead. */
6403  {
6404  for (workp2 = workp + coll_symbol_length ; workp < workp2 ;)
6405  {
6406  const CHAR_T *backup_d = d, *backup_dend = dend;
6407 # ifdef _LIBC
6408  length = __wcslen (workp);
6409 # else
6410  length = wcslen (workp);
6411 # endif
6412 
6413  /* If wcscoll(the collating symbol, whole string) > 0,
6414  any substring of the string never match with the
6415  collating symbol. */
6416 # ifdef _LIBC
6417  if (__wcscoll (workp, d) > 0)
6418 # else
6419  if (wcscoll (workp, d) > 0)
6420 # endif
6421  {
6422  workp += length + 1;
6423  continue;
6424  }
6425 
6426  /* First, we compare the collating symbol with
6427  the first character of the string.
6428  If it don't match, we add the next character to
6429  the compare buffer in turn. */
6430  for (i = 0 ; i < WORK_BUFFER_SIZE-1 ; i++, d++)
6431  {
6432  int match;
6433  if (d == dend)
6434  {
6435  if (dend == end_match_2)
6436  break;
6437  d = string2;
6438  dend = end_match_2;
6439  }
6440 
6441  /* add next character to the compare buffer. */
6442  str_buf[i] = TRANSLATE(*d);
6443  str_buf[i+1] = '\0';
6444 
6445 # ifdef _LIBC
6446  match = __wcscoll (workp, str_buf);
6447 # else
6448  match = wcscoll (workp, str_buf);
6449 # endif
6450  if (match == 0)
6451  goto char_set_matched;
6452 
6453  if (match < 0)
6454  /* (str_buf > workp) indicate (str_buf + X > workp),
6455  because for all X (str_buf + X > str_buf).
6456  So we don't need continue this loop. */
6457  break;
6458 
6459  /* Otherwise(str_buf < workp),
6460  (str_buf+next_character) may equals (workp).
6461  So we continue this loop. */
6462  }
6463  /* not matched */
6464  d = backup_d;
6465  dend = backup_dend;
6466  workp += length + 1;
6467  }
6468  }
6469  /* match with equivalence_class? */
6470 # ifdef _LIBC
6471  if (nrules != 0)
6472  {
6473  const CHAR_T *backup_d = d, *backup_dend = dend;
6474  /* Try to match the equivalence class against
6475  those known to the collate implementation. */
6476  const int32_t *table;
6477  const int32_t *weights;
6478  const int32_t *extra;
6479  const int32_t *indirect;
6480  int32_t idx, idx2;
6481  wint_t *cp;
6482  size_t len;
6483 
6484  /* This #include defines a local function! */
6485 # include <locale/weightwc.h>
6486 
6487  table = (const int32_t *)
6488  _NL_CURRENT (LC_COLLATE, _NL_COLLATE_TABLEWC);
6489  weights = (const wint_t *)
6490  _NL_CURRENT (LC_COLLATE, _NL_COLLATE_WEIGHTWC);
6491  extra = (const wint_t *)
6492  _NL_CURRENT (LC_COLLATE, _NL_COLLATE_EXTRAWC);
6493  indirect = (const int32_t *)
6494  _NL_CURRENT (LC_COLLATE, _NL_COLLATE_INDIRECTWC);
6495 
6496  /* Write 1 collating element to str_buf, and
6497  get its index. */
6498  idx2 = 0;
6499 
6500  for (i = 0 ; idx2 == 0 && i < WORK_BUFFER_SIZE - 1; i++)
6501  {
6502  cp = (wint_t*)str_buf;
6503  if (d == dend)
6504  {
6505  if (dend == end_match_2)
6506  break;
6507  d = string2;
6508  dend = end_match_2;
6509  }
6510  str_buf[i] = TRANSLATE(*(d+i));
6511  str_buf[i+1] = '\0'; /* sentinel */
6512  idx2 = findidx ((const wint_t**)&cp);
6513  }
6514 
6515  /* Update d, however d will be incremented at
6516  char_set_matched:, we decrement d here. */
6517  d = backup_d + ((wchar_t*)cp - (wchar_t*)str_buf - 1);
6518  if (d >= dend)
6519  {
6520  if (dend == end_match_2)
6521  d = dend;
6522  else
6523  {
6524  d = string2;
6525  dend = end_match_2;
6526  }
6527  }
6528 
6529  len = weights[idx2];
6530 
6531  for (workp2 = workp + equiv_class_length ; workp < workp2 ;
6532  workp++)
6533  {
6534  idx = (int32_t)*workp;
6535  /* We already checked idx != 0 in regex_compile. */
6536 
6537  if (idx2 != 0 && len == weights[idx])
6538  {
6539  int cnt = 0;
6540  while (cnt < len && (weights[idx + 1 + cnt]
6541  == weights[idx2 + 1 + cnt]))
6542  ++cnt;
6543 
6544  if (cnt == len)
6545  goto char_set_matched;
6546  }
6547  }
6548  /* not matched */
6549  d = backup_d;
6550  dend = backup_dend;
6551  }
6552  else /* (nrules == 0) */
6553 # endif
6554  /* If we can't look up collation data, we use wcscoll
6555  instead. */
6556  {
6557  for (workp2 = workp + equiv_class_length ; workp < workp2 ;)
6558  {
6559  const CHAR_T *backup_d = d, *backup_dend = dend;
6560 # ifdef _LIBC
6561  length = __wcslen (workp);
6562 # else
6563  length = wcslen (workp);
6564 # endif
6565 
6566  /* If wcscoll(the collating symbol, whole string) > 0,
6567  any substring of the string never match with the
6568  collating symbol. */
6569 # ifdef _LIBC
6570  if (__wcscoll (workp, d) > 0)
6571 # else
6572  if (wcscoll (workp, d) > 0)
6573 # endif
6574  {
6575  workp += length + 1;
6576  break;
6577  }
6578 
6579  /* First, we compare the equivalence class with
6580  the first character of the string.
6581  If it don't match, we add the next character to
6582  the compare buffer in turn. */
6583  for (i = 0 ; i < WORK_BUFFER_SIZE - 1 ; i++, d++)
6584  {
6585  int match;
6586  if (d == dend)
6587  {
6588  if (dend == end_match_2)
6589  break;
6590  d = string2;
6591  dend = end_match_2;
6592  }
6593 
6594  /* add next character to the compare buffer. */
6595  str_buf[i] = TRANSLATE(*d);
6596  str_buf[i+1] = '\0';
6597 
6598 # ifdef _LIBC
6599  match = __wcscoll (workp, str_buf);
6600 # else
6601  match = wcscoll (workp, str_buf);
6602 # endif
6603 
6604  if (match == 0)
6605  goto char_set_matched;
6606 
6607  if (match < 0)
6608  /* (str_buf > workp) indicate (str_buf + X > workp),
6609  because for all X (str_buf + X > str_buf).
6610  So we don't need continue this loop. */
6611  break;
6612 
6613  /* Otherwise(str_buf < workp),
6614  (str_buf+next_character) may equals (workp).
6615  So we continue this loop. */
6616  }
6617  /* not matched */
6618  d = backup_d;
6619  dend = backup_dend;
6620  workp += length + 1;
6621  }
6622  }
6623 
6624  /* match with char_range? */
6625 # ifdef _LIBC
6626  if (nrules != 0)
6627  {
6628  uint32_t collseqval;
6629  const char *collseq = (const char *)
6630  _NL_CURRENT(LC_COLLATE, _NL_COLLATE_COLLSEQWC);
6631 
6632  collseqval = collseq_table_lookup (collseq, c);
6633 
6634  for (; workp < p - chars_length ;)
6635  {
6636  uint32_t start_val, end_val;
6637 
6638  /* We already compute the collation sequence value
6639  of the characters (or collating symbols). */
6640  start_val = (uint32_t) *workp++; /* range_start */
6641  end_val = (uint32_t) *workp++; /* range_end */
6642 
6643  if (start_val <= collseqval && collseqval <= end_val)
6644  goto char_set_matched;
6645  }
6646  }
6647  else
6648 # endif
6649  {
6650  /* We set range_start_char at str_buf[0], range_end_char
6651  at str_buf[4], and compared char at str_buf[2]. */
6652  str_buf[1] = 0;
6653  str_buf[2] = c;
6654  str_buf[3] = 0;
6655  str_buf[5] = 0;
6656  for (; workp < p - chars_length ;)
6657  {
6658  wchar_t *range_start_char, *range_end_char;
6659 
6660  /* match if (range_start_char <= c <= range_end_char). */
6661 
6662  /* If range_start(or end) < 0, we assume -range_start(end)
6663  is the offset of the collating symbol which is specified
6664  as the character of the range start(end). */
6665 
6666  /* range_start */
6667  if (*workp < 0)
6668  range_start_char = charset_top - (*workp++);
6669  else
6670  {
6671  str_buf[0] = *workp++;
6672  range_start_char = str_buf;
6673  }
6674 
6675  /* range_end */
6676  if (*workp < 0)
6677  range_end_char = charset_top - (*workp++);
6678  else
6679  {
6680  str_buf[4] = *workp++;
6681  range_end_char = str_buf + 4;
6682  }
6683 
6684 # ifdef _LIBC
6685  if (__wcscoll (range_start_char, str_buf+2) <= 0
6686  && __wcscoll (str_buf+2, range_end_char) <= 0)
6687 # else
6688  if (wcscoll (range_start_char, str_buf+2) <= 0
6689  && wcscoll (str_buf+2, range_end_char) <= 0)
6690 # endif
6691  goto char_set_matched;
6692  }
6693  }
6694 
6695  /* match with char? */
6696  for (; workp < p ; workp++)
6697  if (c == *workp)
6698  goto char_set_matched;
6699 
6700  not = !not;
6701 
6702  char_set_matched:
6703  if (not) goto fail;
6704 #else
6705  /* Cast to `unsigned' instead of `unsigned char' in case the
6706  bit list is a full 32 bytes long. */
6707  if (c < (unsigned) (*p * BYTEWIDTH)
6708  && p[1 + c / BYTEWIDTH] & (1 << (c % BYTEWIDTH)))
6709  not = !not;
6710 
6711  p += 1 + *p;
6712 
6713  if (!not) goto fail;
6714 #undef WORK_BUFFER_SIZE
6715 #endif /* WCHAR */
6716  SET_REGS_MATCHED ();
6717  d++;
6718  break;
6719  }
6720 
6721 
6722  /* The beginning of a group is represented by start_memory.
6723  The arguments are the register number in the next byte, and the
6724  number of groups inner to this one in the next. The text
6725  matched within the group is recorded (in the internal
6726  registers data structure) under the register number. */
6727  case start_memory:
6728  DEBUG_PRINT3 ("EXECUTING start_memory %ld (%ld):\n",
6729  (long int) *p, (long int) p[1]);
6730 
6731  /* Find out if this group can match the empty string. */
6732  p1 = p; /* To send to group_match_null_string_p. */
6733 
6734  if (REG_MATCH_NULL_STRING_P (reg_info[*p]) == MATCH_NULL_UNSET_VALUE)
6735  REG_MATCH_NULL_STRING_P (reg_info[*p])
6736  = PREFIX(group_match_null_string_p) (&p1, pend, reg_info);
6737 
6738  /* Save the position in the string where we were the last time
6739  we were at this open-group operator in case the group is
6740  operated upon by a repetition operator, e.g., with `(a*)*b'
6741  against `ab'; then we want to ignore where we are now in
6742  the string in case this attempt to match fails. */
6743  old_regstart[*p] = REG_MATCH_NULL_STRING_P (reg_info[*p])
6744  ? REG_UNSET (regstart[*p]) ? d : regstart[*p]
6745  : regstart[*p];
6746  DEBUG_PRINT2 (" old_regstart: %d\n",
6747  POINTER_TO_OFFSET (old_regstart[*p]));
6748 
6749  regstart[*p] = d;
6750  DEBUG_PRINT2 (" regstart: %d\n", POINTER_TO_OFFSET (regstart[*p]));
6751 
6752  IS_ACTIVE (reg_info[*p]) = 1;
6753  MATCHED_SOMETHING (reg_info[*p]) = 0;
6754 
6755  /* Clear this whenever we change the register activity status. */
6756  set_regs_matched_done = 0;
6757 
6758  /* This is the new highest active register. */
6759  highest_active_reg = *p;
6760 
6761  /* If nothing was active before, this is the new lowest active
6762  register. */
6763  if (lowest_active_reg == NO_LOWEST_ACTIVE_REG)
6764  lowest_active_reg = *p;
6765 
6766  /* Move past the register number and inner group count. */
6767  p += 2;
6768  just_past_start_mem = p;
6769 
6770  break;
6771 
6772 
6773  /* The stop_memory opcode represents the end of a group. Its
6774  arguments are the same as start_memory's: the register
6775  number, and the number of inner groups. */
6776  case stop_memory:
6777  DEBUG_PRINT3 ("EXECUTING stop_memory %ld (%ld):\n",
6778  (long int) *p, (long int) p[1]);
6779 
6780  /* We need to save the string position the last time we were at
6781  this close-group operator in case the group is operated
6782  upon by a repetition operator, e.g., with `((a*)*(b*)*)*'
6783  against `aba'; then we want to ignore where we are now in
6784  the string in case this attempt to match fails. */
6785  old_regend[*p] = REG_MATCH_NULL_STRING_P (reg_info[*p])
6786  ? REG_UNSET (regend[*p]) ? d : regend[*p]
6787  : regend[*p];
6788  DEBUG_PRINT2 (" old_regend: %d\n",
6789  POINTER_TO_OFFSET (old_regend[*p]));
6790 
6791  regend[*p] = d;
6792  DEBUG_PRINT2 (" regend: %d\n", POINTER_TO_OFFSET (regend[*p]));
6793 
6794  /* This register isn't active anymore. */
6795  IS_ACTIVE (reg_info[*p]) = 0;
6796 
6797  /* Clear this whenever we change the register activity status. */
6798  set_regs_matched_done = 0;
6799 
6800  /* If this was the only register active, nothing is active
6801  anymore. */
6802  if (lowest_active_reg == highest_active_reg)
6803  {
6804  lowest_active_reg = NO_LOWEST_ACTIVE_REG;
6805  highest_active_reg = NO_HIGHEST_ACTIVE_REG;
6806  }
6807  else
6808  { /* We must scan for the new highest active register, since
6809  it isn't necessarily one less than now: consider
6810  (a(b)c(d(e)f)g). When group 3 ends, after the f), the
6811  new highest active register is 1. */
6812  UCHAR_T r = *p - 1;
6813  while (r > 0 && !IS_ACTIVE (reg_info[r]))
6814  r--;
6815 
6816  /* If we end up at register zero, that means that we saved
6817  the registers as the result of an `on_failure_jump', not
6818  a `start_memory', and we jumped to past the innermost
6819  `stop_memory'. For example, in ((.)*) we save
6820  registers 1 and 2 as a result of the *, but when we pop
6821  back to the second ), we are at the stop_memory 1.
6822  Thus, nothing is active. */
6823  if (r == 0)
6824  {
6825  lowest_active_reg = NO_LOWEST_ACTIVE_REG;
6826  highest_active_reg = NO_HIGHEST_ACTIVE_REG;
6827  }
6828  else
6829  highest_active_reg = r;
6830  }
6831 
6832  /* If just failed to match something this time around with a
6833  group that's operated on by a repetition operator, try to
6834  force exit from the ``loop'', and restore the register
6835  information for this group that we had before trying this
6836  last match. */
6837  if ((!MATCHED_SOMETHING (reg_info[*p])
6838  || just_past_start_mem == p - 1)
6839  && (p + 2) < pend)
6840  {
6841  boolean is_a_jump_n = false;
6842 
6843  p1 = p + 2;
6844  mcnt = 0;
6845  switch ((re_opcode_t) *p1++)
6846  {
6847  case jump_n:
6848  is_a_jump_n = true;
6849  case pop_failure_jump:
6850  case maybe_pop_jump:
6851  case jump:
6852  case dummy_failure_jump:
6853  EXTRACT_NUMBER_AND_INCR (mcnt, p1);
6854  if (is_a_jump_n)
6855  p1 += OFFSET_ADDRESS_SIZE;
6856  break;
6857 
6858  default:
6859  /* do nothing */ ;
6860  }
6861  p1 += mcnt;
6862 
6863  /* If the next operation is a jump backwards in the pattern
6864  to an on_failure_jump right before the start_memory
6865  corresponding to this stop_memory, exit from the loop
6866  by forcing a failure after pushing on the stack the
6867  on_failure_jump's jump in the pattern, and d. */
6868  if (mcnt < 0 && (re_opcode_t) *p1 == on_failure_jump
6869  && (re_opcode_t) p1[1+OFFSET_ADDRESS_SIZE] == start_memory
6870  && p1[2+OFFSET_ADDRESS_SIZE] == *p)
6871  {
6872  /* If this group ever matched anything, then restore
6873  what its registers were before trying this last
6874  failed match, e.g., with `(a*)*b' against `ab' for
6875  regstart[1], and, e.g., with `((a*)*(b*)*)*'
6876  against `aba' for regend[3].
6877 
6878  Also restore the registers for inner groups for,
6879  e.g., `((a*)(b*))*' against `aba' (register 3 would
6880  otherwise get trashed). */
6881 
6882  if (EVER_MATCHED_SOMETHING (reg_info[*p]))
6883  {
6884  unsigned r;
6885 
6886  EVER_MATCHED_SOMETHING (reg_info[*p]) = 0;
6887 
6888  /* Restore this and inner groups' (if any) registers. */
6889  for (r = *p; r < (unsigned) *p + (unsigned) *(p + 1);
6890  r++)
6891  {
6892  regstart[r] = old_regstart[r];
6893 
6894  /* xx why this test? */
6895  if (old_regend[r] >= regstart[r])
6896  regend[r] = old_regend[r];
6897  }
6898  }
6899  p1++;
6900  EXTRACT_NUMBER_AND_INCR (mcnt, p1);
6901  PUSH_FAILURE_POINT (p1 + mcnt, d, -2);
6902 
6903  goto fail;
6904  }
6905  }
6906 
6907  /* Move past the register number and the inner group count. */
6908  p += 2;
6909  break;
6910 
6911 
6912  /* <digit> has been turned into a `duplicate' command which is
6913  followed by the numeric value of <digit> as the register number. */
6914  case duplicate:
6915  {
6916  register const CHAR_T *d2, *dend2;
6917  int regno = *p++; /* Get which register to match against. */
6918  DEBUG_PRINT2 ("EXECUTING duplicate %d.\n", regno);
6919 
6920  /* Can't back reference a group which we've never matched. */
6921  if (REG_UNSET (regstart[regno]) || REG_UNSET (regend[regno]))
6922  goto fail;
6923 
6924  /* Where in input to try to start matching. */
6925  d2 = regstart[regno];
6926 
6927  /* Where to stop matching; if both the place to start and
6928  the place to stop matching are in the same string, then
6929  set to the place to stop, otherwise, for now have to use
6930  the end of the first string. */
6931 
6932  dend2 = ((FIRST_STRING_P (regstart[regno])
6933  == FIRST_STRING_P (regend[regno]))
6934  ? regend[regno] : end_match_1);
6935  for (;;)
6936  {
6937  /* If necessary, advance to next segment in register
6938  contents. */
6939  while (d2 == dend2)
6940  {
6941  if (dend2 == end_match_2) break;
6942  if (dend2 == regend[regno]) break;
6943 
6944  /* End of string1 => advance to string2. */
6945  d2 = string2;
6946  dend2 = regend[regno];
6947  }
6948  /* At end of register contents => success */
6949  if (d2 == dend2) break;
6950 
6951  /* If necessary, advance to next segment in data. */
6952  PREFETCH ();
6953 
6954  /* How many characters left in this segment to match. */
6955  mcnt = dend - d;
6956 
6957  /* Want how many consecutive characters we can match in
6958  one shot, so, if necessary, adjust the count. */
6959  if (mcnt > dend2 - d2)
6960  mcnt = dend2 - d2;
6961 
6962  /* Compare that many; failure if mismatch, else move
6963  past them. */
6964  if (translate
6965  ? PREFIX(bcmp_translate) (d, d2, mcnt, translate)
6966  : memcmp (d, d2, mcnt*sizeof(UCHAR_T)))
6967  goto fail;
6968  d += mcnt, d2 += mcnt;
6969 
6970  /* Do this because we've match some characters. */
6971  SET_REGS_MATCHED ();
6972  }
6973  }
6974  break;
6975 
6976 
6977  /* begline matches the empty string at the beginning of the string
6978  (unless `not_bol' is set in `bufp'), and, if
6979  `newline_anchor' is set, after newlines. */
6980  case begline:
6981  DEBUG_PRINT1 ("EXECUTING begline.\n");
6982 
6983  if (AT_STRINGS_BEG (d))
6984  {
6985  if (!bufp->not_bol) break;
6986  }
6987  else if (d[-1] == '\n' && bufp->newline_anchor)
6988  {
6989  break;
6990  }
6991  /* In all other cases, we fail. */
6992  goto fail;
6993 
6994 
6995  /* endline is the dual of begline. */
6996  case endline:
6997  DEBUG_PRINT1 ("EXECUTING endline.\n");
6998 
6999  if (AT_STRINGS_END (d))
7000  {
7001  if (!bufp->not_eol) break;
7002  }
7003 
7004  /* We have to ``prefetch'' the next character. */
7005  else if ((d == end1 ? *string2 : *d) == '\n'
7006  && bufp->newline_anchor)
7007  {
7008  break;
7009  }
7010  goto fail;
7011 
7012 
7013  /* Match at the very beginning of the data. */
7014  case begbuf:
7015  DEBUG_PRINT1 ("EXECUTING begbuf.\n");
7016  if (AT_STRINGS_BEG (d))
7017  break;
7018  goto fail;
7019 
7020 
7021  /* Match at the very end of the data. */
7022  case endbuf:
7023  DEBUG_PRINT1 ("EXECUTING endbuf.\n");
7024  if (AT_STRINGS_END (d))
7025  break;
7026  goto fail;
7027 
7028 
7029  /* on_failure_keep_string_jump is used to optimize `.*\n'. It
7030  pushes NULL as the value for the string on the stack. Then
7031  `pop_failure_point' will keep the current value for the
7032  string, instead of restoring it. To see why, consider
7033  matching `foo\nbar' against `.*\n'. The .* matches the foo;
7034  then the . fails against the \n. But the next thing we want
7035  to do is match the \n against the \n; if we restored the
7036  string value, we would be back at the foo.
7037 
7038  Because this is used only in specific cases, we don't need to
7039  check all the things that `on_failure_jump' does, to make
7040  sure the right things get saved on the stack. Hence we don't
7041  share its code. The only reason to push anything on the
7042  stack at all is that otherwise we would have to change
7043  `anychar's code to do something besides goto fail in this
7044  case; that seems worse than this. */
7046  DEBUG_PRINT1 ("EXECUTING on_failure_keep_string_jump");
7047 
7048  EXTRACT_NUMBER_AND_INCR (mcnt, p);
7049 #ifdef _LIBC
7050  DEBUG_PRINT3 (" %d (to %p):\n", mcnt, p + mcnt);
7051 #else
7052  DEBUG_PRINT3 (" %d (to 0x%x):\n", mcnt, p + mcnt);
7053 #endif
7054 
7055  PUSH_FAILURE_POINT (p + mcnt, NULL, -2);
7056  break;
7057 
7058 
7059  /* Uses of on_failure_jump:
7060 
7061  Each alternative starts with an on_failure_jump that points
7062  to the beginning of the next alternative. Each alternative
7063  except the last ends with a jump that in effect jumps past
7064  the rest of the alternatives. (They really jump to the
7065  ending jump of the following alternative, because tensioning
7066  these jumps is a hassle.)
7067 
7068  Repeats start with an on_failure_jump that points past both
7069  the repetition text and either the following jump or
7070  pop_failure_jump back to this on_failure_jump. */
7071  case on_failure_jump:
7072  on_failure:
7073  DEBUG_PRINT1 ("EXECUTING on_failure_jump");
7074 
7075  EXTRACT_NUMBER_AND_INCR (mcnt, p);
7076 #ifdef _LIBC
7077  DEBUG_PRINT3 (" %d (to %p)", mcnt, p + mcnt);
7078 #else
7079  DEBUG_PRINT3 (" %d (to 0x%x)", mcnt, p + mcnt);
7080 #endif
7081 
7082  /* If this on_failure_jump comes right before a group (i.e.,
7083  the original * applied to a group), save the information
7084  for that group and all inner ones, so that if we fail back
7085  to this point, the group's information will be correct.
7086  For example, in \(a*\)*\1, we need the preceding group,
7087  and in \(zz\(a*\)b*\)\2, we need the inner group. */
7088 
7089  /* We can't use `p' to check ahead because we push
7090  a failure point to `p + mcnt' after we do this. */
7091  p1 = p;
7092 
7093  /* We need to skip no_op's before we look for the
7094  start_memory in case this on_failure_jump is happening as
7095  the result of a completed succeed_n, as in \(a\)\{1,3\}b\1
7096  against aba. */
7097  while (p1 < pend && (re_opcode_t) *p1 == no_op)
7098  p1++;
7099 
7100  if (p1 < pend && (re_opcode_t) *p1 == start_memory)
7101  {
7102  /* We have a new highest active register now. This will
7103  get reset at the start_memory we are about to get to,
7104  but we will have saved all the registers relevant to
7105  this repetition op, as described above. */
7106  highest_active_reg = *(p1 + 1) + *(p1 + 2);
7107  if (lowest_active_reg == NO_LOWEST_ACTIVE_REG)
7108  lowest_active_reg = *(p1 + 1);
7109  }
7110 
7111  DEBUG_PRINT1 (":\n");
7112  PUSH_FAILURE_POINT (p + mcnt, d, -2);
7113  break;
7114 
7115 
7116  /* A smart repeat ends with `maybe_pop_jump'.
7117  We change it to either `pop_failure_jump' or `jump'. */
7118  case maybe_pop_jump:
7119  EXTRACT_NUMBER_AND_INCR (mcnt, p);
7120  DEBUG_PRINT2 ("EXECUTING maybe_pop_jump %d.\n", mcnt);
7121  {
7122  register UCHAR_T *p2 = p;
7123 
7124  /* Compare the beginning of the repeat with what in the
7125  pattern follows its end. If we can establish that there
7126  is nothing that they would both match, i.e., that we
7127  would have to backtrack because of (as in, e.g., `a*a')
7128  then we can change to pop_failure_jump, because we'll
7129  never have to backtrack.
7130 
7131  This is not true in the case of alternatives: in
7132  `(a|ab)*' we do need to backtrack to the `ab' alternative
7133  (e.g., if the string was `ab'). But instead of trying to
7134  detect that here, the alternative has put on a dummy
7135  failure point which is what we will end up popping. */
7136 
7137  /* Skip over open/close-group commands.
7138  If what follows this loop is a ...+ construct,
7139  look at what begins its body, since we will have to
7140  match at least one of that. */
7141  while (1)
7142  {
7143  if (p2 + 2 < pend
7144  && ((re_opcode_t) *p2 == stop_memory
7145  || (re_opcode_t) *p2 == start_memory))
7146  p2 += 3;
7147  else if (p2 + 2 + 2 * OFFSET_ADDRESS_SIZE < pend
7148  && (re_opcode_t) *p2 == dummy_failure_jump)
7149  p2 += 2 + 2 * OFFSET_ADDRESS_SIZE;
7150  else
7151  break;
7152  }
7153 
7154  p1 = p + mcnt;
7155  /* p1[0] ... p1[2] are the `on_failure_jump' corresponding
7156  to the `maybe_finalize_jump' of this case. Examine what
7157  follows. */
7158 
7159  /* If we're at the end of the pattern, we can change. */
7160  if (p2 == pend)
7161  {
7162  /* Consider what happens when matching ":\(.*\)"
7163  against ":/". I don't really understand this code
7164  yet. */
7165  p[-(1+OFFSET_ADDRESS_SIZE)] = (UCHAR_T)
7167  DEBUG_PRINT1
7168  (" End of pattern: change to `pop_failure_jump'.\n");
7169  }
7170 
7171  else if ((re_opcode_t) *p2 == exactn
7172 #ifdef MBS_SUPPORT
7173  || (re_opcode_t) *p2 == exactn_bin
7174 #endif
7175  || (bufp->newline_anchor && (re_opcode_t) *p2 == endline))
7176  {
7177  register UCHAR_T c
7178  = *p2 == (UCHAR_T) endline ? '\n' : p2[2];
7179 
7180  if (((re_opcode_t) p1[1+OFFSET_ADDRESS_SIZE] == exactn
7181 #ifdef MBS_SUPPORT
7182  || (re_opcode_t) p1[1+OFFSET_ADDRESS_SIZE] == exactn_bin
7183 #endif
7184  ) && p1[3+OFFSET_ADDRESS_SIZE] != c)
7185  {
7186  p[-(1+OFFSET_ADDRESS_SIZE)] = (UCHAR_T)
7187  pop_failure_jump;
7188 #ifdef WCHAR
7189  DEBUG_PRINT3 (" %C != %C => pop_failure_jump.\n",
7190  (wint_t) c,
7191  (wint_t) p1[3+OFFSET_ADDRESS_SIZE]);
7192 #else
7193  DEBUG_PRINT3 (" %c != %c => pop_failure_jump.\n",
7194  (char) c,
7195  (char) p1[3+OFFSET_ADDRESS_SIZE]);
7196 #endif
7197  }
7198 
7199 #ifndef WCHAR
7200  else if ((re_opcode_t) p1[3] == charset
7201  || (re_opcode_t) p1[3] == charset_not)
7202  {
7203  int not = (re_opcode_t) p1[3] == charset_not;
7204 
7205  if (c < (unsigned) (p1[4] * BYTEWIDTH)
7206  && p1[5 + c / BYTEWIDTH] & (1 << (c % BYTEWIDTH)))
7207  not = !not;
7208 
7209  /* `not' is equal to 1 if c would match, which means
7210  that we can't change to pop_failure_jump. */
7211  if (!not)
7212  {
7213  p[-3] = (unsigned char) pop_failure_jump;
7214  DEBUG_PRINT1 (" No match => pop_failure_jump.\n");
7215  }
7216  }
7217 #endif /* not WCHAR */
7218  }
7219 #ifndef WCHAR
7220  else if ((re_opcode_t) *p2 == charset)
7221  {
7222  /* We win if the first character of the loop is not part
7223  of the charset. */
7224  if ((re_opcode_t) p1[3] == exactn
7225  && ! ((int) p2[1] * BYTEWIDTH > (int) p1[5]
7226  && (p2[2 + p1[5] / BYTEWIDTH]
7227  & (1 << (p1[5] % BYTEWIDTH)))))
7228  {
7229  p[-3] = (unsigned char) pop_failure_jump;
7230  DEBUG_PRINT1 (" No match => pop_failure_jump.\n");
7231  }
7232 
7233  else if ((re_opcode_t) p1[3] == charset_not)
7234  {
7235  int idx;
7236  /* We win if the charset_not inside the loop
7237  lists every character listed in the charset after. */
7238  for (idx = 0; idx < (int) p2[1]; idx++)
7239  if (! (p2[2 + idx] == 0
7240  || (idx < (int) p1[4]
7241  && ((p2[2 + idx] & ~ p1[5 + idx]) == 0))))
7242  break;
7243 
7244  if (idx == p2[1])
7245  {
7246  p[-3] = (unsigned char) pop_failure_jump;
7247  DEBUG_PRINT1 (" No match => pop_failure_jump.\n");
7248  }
7249  }
7250  else if ((re_opcode_t) p1[3] == charset)
7251  {
7252  int idx;
7253  /* We win if the charset inside the loop
7254  has no overlap with the one after the loop. */
7255  for (idx = 0;
7256  idx < (int) p2[1] && idx < (int) p1[4];
7257  idx++)
7258  if ((p2[2 + idx] & p1[5 + idx]) != 0)
7259  break;
7260 
7261  if (idx == p2[1] || idx == p1[4])
7262  {
7263  p[-3] = (unsigned char) pop_failure_jump;
7264  DEBUG_PRINT1 (" No match => pop_failure_jump.\n");
7265  }
7266  }
7267  }
7268 #endif /* not WCHAR */
7269  }
7270  p -= OFFSET_ADDRESS_SIZE; /* Point at relative address again. */
7271  if ((re_opcode_t) p[-1] != pop_failure_jump)
7272  {
7273  p[-1] = (UCHAR_T) jump;
7274  DEBUG_PRINT1 (" Match => jump.\n");
7275  goto unconditional_jump;
7276  }
7277  /* Note fall through. */
7278 
7279 
7280  /* The end of a simple repeat has a pop_failure_jump back to
7281  its matching on_failure_jump, where the latter will push a
7282  failure point. The pop_failure_jump takes off failure
7283  points put on by this pop_failure_jump's matching
7284  on_failure_jump; we got through the pattern to here from the
7285  matching on_failure_jump, so didn't fail. */
7286  case pop_failure_jump:
7287  {
7288  /* We need to pass separate storage for the lowest and
7289  highest registers, even though we don't care about the
7290  actual values. Otherwise, we will restore only one
7291  register from the stack, since lowest will == highest in
7292  `pop_failure_point'. */
7293  active_reg_t dummy_low_reg, dummy_high_reg;
7294  UCHAR_T *pdummy = NULL;
7295  const CHAR_T *sdummy = NULL;
7296 
7297  DEBUG_PRINT1 ("EXECUTING pop_failure_jump.\n");
7298  POP_FAILURE_POINT (sdummy, pdummy,
7299  dummy_low_reg, dummy_high_reg,
7300  reg_dummy, reg_dummy, reg_info_dummy);
7301  }
7302  /* Note fall through. */
7303 
7304  unconditional_jump:
7305 #ifdef _LIBC
7306  DEBUG_PRINT2 ("\n%p: ", p);
7307 #else
7308  DEBUG_PRINT2 ("\n0x%x: ", p);
7309 #endif
7310  /* Note fall through. */
7311 
7312  /* Unconditionally jump (without popping any failure points). */
7313  case jump:
7314  EXTRACT_NUMBER_AND_INCR (mcnt, p); /* Get the amount to jump. */
7315  DEBUG_PRINT2 ("EXECUTING jump %d ", mcnt);
7316  p += mcnt; /* Do the jump. */
7317 #ifdef _LIBC
7318  DEBUG_PRINT2 ("(to %p).\n", p);
7319 #else
7320  DEBUG_PRINT2 ("(to 0x%x).\n", p);
7321 #endif
7322  break;
7323 
7324 
7325  /* We need this opcode so we can detect where alternatives end
7326  in `group_match_null_string_p' et al. */
7327  case jump_past_alt:
7328  DEBUG_PRINT1 ("EXECUTING jump_past_alt.\n");
7329  goto unconditional_jump;
7330 
7331 
7332  /* Normally, the on_failure_jump pushes a failure point, which
7333  then gets popped at pop_failure_jump. We will end up at
7334  pop_failure_jump, also, and with a pattern of, say, `a+', we
7335  are skipping over the on_failure_jump, so we have to push
7336  something meaningless for pop_failure_jump to pop. */
7337  case dummy_failure_jump:
7338  DEBUG_PRINT1 ("EXECUTING dummy_failure_jump.\n");
7339  /* It doesn't matter what we push for the string here. What
7340  the code at `fail' tests is the value for the pattern. */
7341  PUSH_FAILURE_POINT (NULL, NULL, -2);
7342  goto unconditional_jump;
7343 
7344 
7345  /* At the end of an alternative, we need to push a dummy failure
7346  point in case we are followed by a `pop_failure_jump', because
7347  we don't want the failure point for the alternative to be
7348  popped. For example, matching `(a|ab)*' against `aab'
7349  requires that we match the `ab' alternative. */
7350  case push_dummy_failure:
7351  DEBUG_PRINT1 ("EXECUTING push_dummy_failure.\n");
7352  /* See comments just above at `dummy_failure_jump' about the
7353  two zeroes. */
7354  PUSH_FAILURE_POINT (NULL, NULL, -2);
7355  break;
7356 
7357  /* Have to succeed matching what follows at least n times.
7358  After that, handle like `on_failure_jump'. */
7359  case succeed_n:
7360  EXTRACT_NUMBER (mcnt, p + OFFSET_ADDRESS_SIZE);
7361  DEBUG_PRINT2 ("EXECUTING succeed_n %d.\n", mcnt);
7362 
7363  assert (mcnt >= 0);
7364  /* Originally, this is how many times we HAVE to succeed. */
7365  if (mcnt > 0)
7366  {
7367  mcnt--;
7368  p += OFFSET_ADDRESS_SIZE;
7369  STORE_NUMBER_AND_INCR (p, mcnt);
7370 #ifdef _LIBC
7371  DEBUG_PRINT3 (" Setting %p to %d.\n", p - OFFSET_ADDRESS_SIZE
7372  , mcnt);
7373 #else
7374  DEBUG_PRINT3 (" Setting 0x%x to %d.\n", p - OFFSET_ADDRESS_SIZE
7375  , mcnt);
7376 #endif
7377  }
7378  else if (mcnt == 0)
7379  {
7380 #ifdef _LIBC
7381  DEBUG_PRINT2 (" Setting two bytes from %p to no_op.\n",
7382  p + OFFSET_ADDRESS_SIZE);
7383 #else
7384  DEBUG_PRINT2 (" Setting two bytes from 0x%x to no_op.\n",
7385  p + OFFSET_ADDRESS_SIZE);
7386 #endif /* _LIBC */
7387 
7388 #ifdef WCHAR
7389  p[1] = (UCHAR_T) no_op;
7390 #else
7391  p[2] = (UCHAR_T) no_op;
7392  p[3] = (UCHAR_T) no_op;
7393 #endif /* WCHAR */
7394  goto on_failure;
7395  }
7396  break;
7397 
7398  case jump_n:
7399  EXTRACT_NUMBER (mcnt, p + OFFSET_ADDRESS_SIZE);
7400  DEBUG_PRINT2 ("EXECUTING jump_n %d.\n", mcnt);
7401 
7402  /* Originally, this is how many times we CAN jump. */
7403  if (mcnt)
7404  {
7405  mcnt--;
7406  STORE_NUMBER (p + OFFSET_ADDRESS_SIZE, mcnt);
7407 
7408 #ifdef _LIBC
7409  DEBUG_PRINT3 (" Setting %p to %d.\n", p + OFFSET_ADDRESS_SIZE,
7410  mcnt);
7411 #else
7412  DEBUG_PRINT3 (" Setting 0x%x to %d.\n", p + OFFSET_ADDRESS_SIZE,
7413  mcnt);
7414 #endif /* _LIBC */
7415  goto unconditional_jump;
7416  }
7417  /* If don't have to jump any more, skip over the rest of command. */
7418  else
7419  p += 2 * OFFSET_ADDRESS_SIZE;
7420  break;
7421 
7422  case set_number_at:
7423  {
7424  DEBUG_PRINT1 ("EXECUTING set_number_at.\n");
7425 
7426  EXTRACT_NUMBER_AND_INCR (mcnt, p);
7427  p1 = p + mcnt;
7428  EXTRACT_NUMBER_AND_INCR (mcnt, p);
7429 #ifdef _LIBC
7430  DEBUG_PRINT3 (" Setting %p to %d.\n", p1, mcnt);
7431 #else
7432  DEBUG_PRINT3 (" Setting 0x%x to %d.\n", p1, mcnt);
7433 #endif
7434  STORE_NUMBER (p1, mcnt);
7435  break;
7436  }
7437 
7438 #if 0
7439  /* The DEC Alpha C compiler 3.x generates incorrect code for the
7440  test WORDCHAR_P (d - 1) != WORDCHAR_P (d) in the expansion of
7441  AT_WORD_BOUNDARY, so this code is disabled. Expanding the
7442  macro and introducing temporary variables works around the bug. */
7443 
7444  case wordbound:
7445  DEBUG_PRINT1 ("EXECUTING wordbound.\n");
7446  if (AT_WORD_BOUNDARY (d))
7447  break;
7448  goto fail;
7449 
7450  case notwordbound:
7451  DEBUG_PRINT1 ("EXECUTING notwordbound.\n");
7452  if (AT_WORD_BOUNDARY (d))
7453  goto fail;
7454  break;
7455 #else
7456  case wordbound:
7457  {
7458  boolean prevchar, thischar;
7459 
7460  DEBUG_PRINT1 ("EXECUTING wordbound.\n");
7461  if (AT_STRINGS_BEG (d) || AT_STRINGS_END (d))
7462  break;
7463 
7464  prevchar = WORDCHAR_P (d - 1);
7465  thischar = WORDCHAR_P (d);
7466  if (prevchar != thischar)
7467  break;
7468  goto fail;
7469  }
7470 
7471  case notwordbound:
7472  {
7473  boolean prevchar, thischar;
7474 
7475  DEBUG_PRINT1 ("EXECUTING notwordbound.\n");
7476  if (AT_STRINGS_BEG (d) || AT_STRINGS_END (d))
7477  goto fail;
7478 
7479  prevchar = WORDCHAR_P (d - 1);
7480  thischar = WORDCHAR_P (d);
7481  if (prevchar != thischar)
7482  goto fail;
7483  break;
7484  }
7485 #endif
7486 
7487  case wordbeg:
7488  DEBUG_PRINT1 ("EXECUTING wordbeg.\n");
7489  if (!AT_STRINGS_END (d) && WORDCHAR_P (d)
7490  && (AT_STRINGS_BEG (d) || !WORDCHAR_P (d - 1)))
7491  break;
7492  goto fail;
7493 
7494  case wordend:
7495  DEBUG_PRINT1 ("EXECUTING wordend.\n");
7496  if (!AT_STRINGS_BEG (d) && WORDCHAR_P (d - 1)
7497  && (AT_STRINGS_END (d) || !WORDCHAR_P (d)))
7498  break;
7499  goto fail;
7500 
7501 #ifdef emacs
7502  case before_dot:
7503  DEBUG_PRINT1 ("EXECUTING before_dot.\n");
7504  if (PTR_CHAR_POS ((unsigned char *) d) >= point)
7505  goto fail;
7506  break;
7507 
7508  case at_dot:
7509  DEBUG_PRINT1 ("EXECUTING at_dot.\n");
7510  if (PTR_CHAR_POS ((unsigned char *) d) != point)
7511  goto fail;
7512  break;
7513 
7514  case after_dot:
7515  DEBUG_PRINT1 ("EXECUTING after_dot.\n");
7516  if (PTR_CHAR_POS ((unsigned char *) d) <= point)
7517  goto fail;
7518  break;
7519 
7520  case syntaxspec:
7521  DEBUG_PRINT2 ("EXECUTING syntaxspec %d.\n", mcnt);
7522  mcnt = *p++;
7523  goto matchsyntax;
7524 
7525  case wordchar:
7526  DEBUG_PRINT1 ("EXECUTING Emacs wordchar.\n");
7527  mcnt = (int) Sword;
7528  matchsyntax:
7529  PREFETCH ();
7530  /* Can't use *d++ here; SYNTAX may be an unsafe macro. */
7531  d++;
7532  if (SYNTAX (d[-1]) != (enum syntaxcode) mcnt)
7533  goto fail;
7534  SET_REGS_MATCHED ();
7535  break;
7536 
7537  case notsyntaxspec:
7538  DEBUG_PRINT2 ("EXECUTING notsyntaxspec %d.\n", mcnt);
7539  mcnt = *p++;
7540  goto matchnotsyntax;
7541 
7542  case notwordchar:
7543  DEBUG_PRINT1 ("EXECUTING Emacs notwordchar.\n");
7544  mcnt = (int) Sword;
7545  matchnotsyntax:
7546  PREFETCH ();
7547  /* Can't use *d++ here; SYNTAX may be an unsafe macro. */
7548  d++;
7549  if (SYNTAX (d[-1]) == (enum syntaxcode) mcnt)
7550  goto fail;
7551  SET_REGS_MATCHED ();
7552  break;
7553 
7554 #else /* not emacs */
7555  case wordchar:
7556  DEBUG_PRINT1 ("EXECUTING non-Emacs wordchar.\n");
7557  PREFETCH ();
7558  if (!WORDCHAR_P (d))
7559  goto fail;
7560  SET_REGS_MATCHED ();
7561  d++;
7562  break;
7563 
7564  case notwordchar:
7565  DEBUG_PRINT1 ("EXECUTING non-Emacs notwordchar.\n");
7566  PREFETCH ();
7567  if (WORDCHAR_P (d))
7568  goto fail;
7569  SET_REGS_MATCHED ();
7570  d++;
7571  break;
7572 #endif /* not emacs */
7573 
7574  default:
7575  abort ();
7576  }
7577  continue; /* Successfully executed one pattern command; keep going. */
7578 
7579 
7580  /* We goto here if a matching operation fails. */
7581  fail:
7582  if (!FAIL_STACK_EMPTY ())
7583  { /* A restart point is known. Restore to that state. */
7584  DEBUG_PRINT1 ("\nFAIL:\n");
7585  POP_FAILURE_POINT (d, p,
7586  lowest_active_reg, highest_active_reg,
7587  regstart, regend, reg_info);
7588 
7589  /* If this failure point is a dummy, try the next one. */
7590  if (!p)
7591  goto fail;
7592 
7593  /* If we failed to the end of the pattern, don't examine *p. */
7594  assert (p <= pend);
7595  if (p < pend)
7596  {
7597  boolean is_a_jump_n = false;
7598 
7599  /* If failed to a backwards jump that's part of a repetition
7600  loop, need to pop this failure point and use the next one. */
7601  switch ((re_opcode_t) *p)
7602  {
7603  case jump_n:
7604  is_a_jump_n = true;
7605  case maybe_pop_jump:
7606  case pop_failure_jump:
7607  case jump:
7608  p1 = p + 1;
7609  EXTRACT_NUMBER_AND_INCR (mcnt, p1);
7610  p1 += mcnt;
7611 
7612  if ((is_a_jump_n && (re_opcode_t) *p1 == succeed_n)
7613  || (!is_a_jump_n
7614  && (re_opcode_t) *p1 == on_failure_jump))
7615  goto fail;
7616  break;
7617  default:
7618  /* do nothing */ ;
7619  }
7620  }
7621 
7622  if (d >= string1 && d <= end1)
7623  dend = end_match_1;
7624  }
7625  else
7626  break; /* Matching at this starting point really fails. */
7627  } /* for (;;) */
7628 
7629  if (best_regs_set)
7630  goto restore_best_regs;
7631 
7632  FREE_VARIABLES ();
7633 
7634  return -1; /* Failure to match. */
7635 } /* re_match_2 */
7636 
7637 
7638 /* Subroutine definitions for re_match_2. */
7639 
7640 
7641 /* We are passed P pointing to a register number after a start_memory.
7642 
7643  Return true if the pattern up to the corresponding stop_memory can
7644  match the empty string, and false otherwise.
7645 
7646  If we find the matching stop_memory, sets P to point to one past its number.
7647  Otherwise, sets P to an undefined byte less than or equal to END.
7648 
7649  We don't handle duplicates properly (yet). */
7650 
7651 static boolean
7652 PREFIX(group_match_null_string_p) (p, end, reg_info)
7653  UCHAR_T **p, *end;
7654  PREFIX(register_info_type) *reg_info;
7655 {
7656  int mcnt;
7657  /* Point to after the args to the start_memory. */
7658  UCHAR_T *p1 = *p + 2;
7659 
7660  while (p1 < end)
7661  {
7662  /* Skip over opcodes that can match nothing, and return true or
7663  false, as appropriate, when we get to one that can't, or to the
7664  matching stop_memory. */
7665 
7666  switch ((re_opcode_t) *p1)
7667  {
7668  /* Could be either a loop or a series of alternatives. */
7669  case on_failure_jump:
7670  p1++;
7671  EXTRACT_NUMBER_AND_INCR (mcnt, p1);
7672 
7673  /* If the next operation is not a jump backwards in the
7674  pattern. */
7675 
7676  if (mcnt >= 0)
7677  {
7678  /* Go through the on_failure_jumps of the alternatives,
7679  seeing if any of the alternatives cannot match nothing.
7680  The last alternative starts with only a jump,
7681  whereas the rest start with on_failure_jump and end
7682  with a jump, e.g., here is the pattern for `a|b|c':
7683 
7684  /on_failure_jump/0/6/exactn/1/a/jump_past_alt/0/6
7685  /on_failure_jump/0/6/exactn/1/b/jump_past_alt/0/3
7686  /exactn/1/c
7687 
7688  So, we have to first go through the first (n-1)
7689  alternatives and then deal with the last one separately. */
7690 
7691 
7692  /* Deal with the first (n-1) alternatives, which start
7693  with an on_failure_jump (see above) that jumps to right
7694  past a jump_past_alt. */
7695 
7696  while ((re_opcode_t) p1[mcnt-(1+OFFSET_ADDRESS_SIZE)] ==
7697  jump_past_alt)
7698  {
7699  /* `mcnt' holds how many bytes long the alternative
7700  is, including the ending `jump_past_alt' and
7701  its number. */
7702 
7703  if (!PREFIX(alt_match_null_string_p) (p1, p1 + mcnt -
7704  (1 + OFFSET_ADDRESS_SIZE),
7705  reg_info))
7706  return false;
7707 
7708  /* Move to right after this alternative, including the
7709  jump_past_alt. */
7710  p1 += mcnt;
7711 
7712  /* Break if it's the beginning of an n-th alternative
7713  that doesn't begin with an on_failure_jump. */
7714  if ((re_opcode_t) *p1 != on_failure_jump)
7715  break;
7716 
7717  /* Still have to check that it's not an n-th
7718  alternative that starts with an on_failure_jump. */
7719  p1++;
7720  EXTRACT_NUMBER_AND_INCR (mcnt, p1);
7721  if ((re_opcode_t) p1[mcnt-(1+OFFSET_ADDRESS_SIZE)] !=
7722  jump_past_alt)
7723  {
7724  /* Get to the beginning of the n-th alternative. */
7725  p1 -= 1 + OFFSET_ADDRESS_SIZE;
7726  break;
7727  }
7728  }
7729 
7730  /* Deal with the last alternative: go back and get number
7731  of the `jump_past_alt' just before it. `mcnt' contains
7732  the length of the alternative. */
7733  EXTRACT_NUMBER (mcnt, p1 - OFFSET_ADDRESS_SIZE);
7734 
7735  if (!PREFIX(alt_match_null_string_p) (p1, p1 + mcnt, reg_info))
7736  return false;
7737 
7738  p1 += mcnt; /* Get past the n-th alternative. */
7739  } /* if mcnt > 0 */
7740  break;
7741 
7742 
7743  case stop_memory:
7744  assert (p1[1] == **p);
7745  *p = p1 + 2;
7746  return true;
7747 
7748 
7749  default:
7750  if (!PREFIX(common_op_match_null_string_p) (&p1, end, reg_info))
7751  return false;
7752  }
7753  } /* while p1 < end */
7754 
7755  return false;
7756 } /* group_match_null_string_p */
7757 
7758 
7759 /* Similar to group_match_null_string_p, but doesn't deal with alternatives:
7760  It expects P to be the first byte of a single alternative and END one
7761  byte past the last. The alternative can contain groups. */
7762 
7763 static boolean
7764 PREFIX(alt_match_null_string_p) (p, end, reg_info)
7765  UCHAR_T *p, *end;
7766  PREFIX(register_info_type) *reg_info;
7767 {
7768  int mcnt;
7769  UCHAR_T *p1 = p;
7770 
7771  while (p1 < end)
7772  {
7773  /* Skip over opcodes that can match nothing, and break when we get
7774  to one that can't. */
7775 
7776  switch ((re_opcode_t) *p1)
7777  {
7778  /* It's a loop. */
7779  case on_failure_jump:
7780  p1++;
7781  EXTRACT_NUMBER_AND_INCR (mcnt, p1);
7782  p1 += mcnt;
7783  break;
7784 
7785  default:
7786  if (!PREFIX(common_op_match_null_string_p) (&p1, end, reg_info))
7787  return false;
7788  }
7789  } /* while p1 < end */
7790 
7791  return true;
7792 } /* alt_match_null_string_p */
7793 
7794 
7795 /* Deals with the ops common to group_match_null_string_p and
7796  alt_match_null_string_p.
7797 
7798  Sets P to one after the op and its arguments, if any. */
7799 
7800 static boolean
7801 PREFIX(common_op_match_null_string_p) (p, end, reg_info)
7802  UCHAR_T **p, *end;
7803  PREFIX(register_info_type) *reg_info;
7804 {
7805  int mcnt;
7806  boolean ret;
7807  int reg_no;
7808  UCHAR_T *p1 = *p;
7809 
7810  switch ((re_opcode_t) *p1++)
7811  {
7812  case no_op:
7813  case begline:
7814  case endline:
7815  case begbuf:
7816  case endbuf:
7817  case wordbeg:
7818  case wordend:
7819  case wordbound:
7820  case notwordbound:
7821 #ifdef emacs
7822  case before_dot:
7823  case at_dot:
7824  case after_dot:
7825 #endif
7826  break;
7827 
7828  case start_memory:
7829  reg_no = *p1;
7830  assert (reg_no > 0 && reg_no <= MAX_REGNUM);
7831  ret = PREFIX(group_match_null_string_p) (&p1, end, reg_info);
7832 
7833  /* Have to set this here in case we're checking a group which
7834  contains a group and a back reference to it. */
7835 
7836  if (REG_MATCH_NULL_STRING_P (reg_info[reg_no]) == MATCH_NULL_UNSET_VALUE)
7837  REG_MATCH_NULL_STRING_P (reg_info[reg_no]) = ret;
7838 
7839  if (!ret)
7840  return false;
7841  break;
7842 
7843  /* If this is an optimized succeed_n for zero times, make the jump. */
7844  case jump:
7845  EXTRACT_NUMBER_AND_INCR (mcnt, p1);
7846  if (mcnt >= 0)
7847  p1 += mcnt;
7848  else
7849  return false;
7850  break;
7851 
7852  case succeed_n:
7853  /* Get to the number of times to succeed. */
7854  p1 += OFFSET_ADDRESS_SIZE;
7855  EXTRACT_NUMBER_AND_INCR (mcnt, p1);
7856 
7857  if (mcnt == 0)
7858  {
7859  p1 -= 2 * OFFSET_ADDRESS_SIZE;
7860  EXTRACT_NUMBER_AND_INCR (mcnt, p1);
7861  p1 += mcnt;
7862  }
7863  else
7864  return false;
7865  break;
7866 
7867  case duplicate:
7868  if (!REG_MATCH_NULL_STRING_P (reg_info[*p1]))
7869  return false;
7870  break;
7871 
7872  case set_number_at:
7873  p1 += 2 * OFFSET_ADDRESS_SIZE;
7874 
7875  default:
7876  /* All other opcodes mean we cannot match the empty string. */
7877  return false;
7878  }
7879 
7880  *p = p1;
7881  return true;
7882 } /* common_op_match_null_string_p */
7883 
7884 
7885 /* Return zero if TRANSLATE[S1] and TRANSLATE[S2] are identical for LEN
7886  bytes; nonzero otherwise. */
7887 
7888 static int
7889 PREFIX(bcmp_translate) (s1, s2, len, translate)
7890  const CHAR_T *s1, *s2;
7891  register int len;
7893 {
7894  register const UCHAR_T *p1 = (const UCHAR_T *) s1;
7895  register const UCHAR_T *p2 = (const UCHAR_T *) s2;
7896  while (len)
7897  {
7898 #ifdef WCHAR
7899  if (((*p1<=0xff)?translate[*p1++]:*p1++)
7900  != ((*p2<=0xff)?translate[*p2++]:*p2++))
7901  return 1;
7902 #else /* BYTE */
7903  if (translate[*p1++] != translate[*p2++]) return 1;
7904 #endif /* WCHAR */
7905  len--;
7906  }
7907  return 0;
7908 }
7909 
7910 
7911 
7912 #else /* not INSIDE_RECURSION */
7913 
7914 /* Entry points for GNU code. */
7915 
7916 /* re_compile_pattern is the GNU regular expression compiler: it
7917  compiles PATTERN (of length SIZE) and puts the result in BUFP.
7918  Returns 0 if the pattern was valid, otherwise an error string.
7919 
7920  Assumes the `allocated' (and perhaps `buffer') and `translate' fields
7921  are set in BUFP on entry.
7922 
7923  We call regex_compile to do the actual compilation. */
7924 
7925 const char *
7926 re_compile_pattern (pattern, length, bufp)
7927  const char *pattern;
7928  size_t length;
7929  struct re_pattern_buffer *bufp;
7930 {
7932 
7933  /* GNU code is written to assume at least RE_NREGS registers will be set
7934  (and at least one extra will be -1). */
7936 
7937  /* And GNU code determines whether or not to get register information
7938  by passing null for the REGS argument to re_match, etc., not by
7939  setting no_sub. */
7940  bufp->no_sub = 0;
7941 
7942  /* Match anchors at newline. */
7943  bufp->newline_anchor = 1;
7944 
7945 # ifdef MBS_SUPPORT
7946  if (MB_CUR_MAX != 1)
7947  ret = wcs_regex_compile (pattern, length, re_syntax_options, bufp);
7948  else
7949 # endif
7950  ret = byte_regex_compile (pattern, length, re_syntax_options, bufp);
7951 
7952  if (!ret)
7953  return NULL;
7954  return gettext (re_error_msgid + re_error_msgid_idx[(int) ret]);
7955 }
7956 #ifdef _LIBC
7957 weak_alias (__re_compile_pattern, re_compile_pattern)
7958 #endif
7959 
7960 
7961 /* Entry points compatible with 4.2 BSD regex library. We don't define
7962  them unless specifically requested. */
7963 
7964 #if defined _REGEX_RE_COMP || defined _LIBC
7965 
7966 /* BSD has one and only one pattern buffer. */
7967 static struct re_pattern_buffer re_comp_buf;
7968 
7969 char *
7970 #ifdef _LIBC
7971 /* Make these definitions weak in libc, so POSIX programs can redefine
7972  these names if they don't use our functions, and still use
7973  regcomp/regexec below without link errors. */
7974 weak_function
7975 #endif
7976 re_comp (s)
7977  const char *s;
7978 {
7980 
7981  if (!s)
7982  {
7983  if (!re_comp_buf.buffer)
7984  return gettext ("No previous regular expression");
7985  return 0;
7986  }
7987 
7988  if (!re_comp_buf.buffer)
7989  {
7990  re_comp_buf.buffer = (unsigned char *) malloc (200);
7991  if (re_comp_buf.buffer == NULL)
7992  return (char *) gettext (re_error_msgid
7993  + re_error_msgid_idx[(int) REG_ESPACE]);
7994  re_comp_buf.allocated = 200;
7995 
7996  re_comp_buf.fastmap = (char *) malloc (1 << BYTEWIDTH);
7997  if (re_comp_buf.fastmap == NULL)
7998  return (char *) gettext (re_error_msgid
7999  + re_error_msgid_idx[(int) REG_ESPACE]);
8000  }
8001 
8002  /* Since `re_exec' always passes NULL for the `regs' argument, we
8003  don't need to initialize the pattern buffer fields which affect it. */
8004 
8005  /* Match anchors at newlines. */
8006  re_comp_buf.newline_anchor = 1;
8007 
8008 # ifdef MBS_SUPPORT
8009  if (MB_CUR_MAX != 1)
8010  ret = wcs_regex_compile (s, strlen (s), re_syntax_options, &re_comp_buf);
8011  else
8012 # endif
8013  ret = byte_regex_compile (s, strlen (s), re_syntax_options, &re_comp_buf);
8014 
8015  if (!ret)
8016  return NULL;
8017 
8018  /* Yes, we're discarding `const' here if !HAVE_LIBINTL. */
8019  return (char *) gettext (re_error_msgid + re_error_msgid_idx[(int) ret]);
8020 }
8021 
8022 
8023 int
8024 #ifdef _LIBC
8025 weak_function
8026 #endif
8027 re_exec (s)
8028  const char *s;
8029 {
8030  const int len = strlen (s);
8031  return
8032  0 <= re_search (&re_comp_buf, s, len, 0, len, (struct re_registers *) 0);
8033 }
8034 
8035 #endif /* _REGEX_RE_COMP */
8036 
8037 
8038 /* POSIX.2 functions. Don't define these for Emacs. */
8039 
8040 #ifndef emacs
8041 
8042 /* regcomp takes a regular expression as a string and compiles it.
8043 
8044  PREG is a regex_t *. We do not expect any fields to be initialized,
8045  since POSIX says we shouldn't. Thus, we set
8046 
8047  `buffer' to the compiled pattern;
8048  `used' to the length of the compiled pattern;
8049  `syntax' to RE_SYNTAX_POSIX_EXTENDED if the
8050  REG_EXTENDED bit in CFLAGS is set; otherwise, to
8051  RE_SYNTAX_POSIX_BASIC;
8052  `newline_anchor' to REG_NEWLINE being set in CFLAGS;
8053  `fastmap' to an allocated space for the fastmap;
8054  `fastmap_accurate' to zero;
8055  `re_nsub' to the number of subexpressions in PATTERN.
8056 
8057  PATTERN is the address of the pattern string.
8058 
8059  CFLAGS is a series of bits which affect compilation.
8060 
8061  If REG_EXTENDED is set, we use POSIX extended syntax; otherwise, we
8062  use POSIX basic syntax.
8063 
8064  If REG_NEWLINE is set, then . and [^...] don't match newline.
8065  Also, regexec will try a match beginning after every newline.
8066 
8067  If REG_ICASE is set, then we considers upper- and lowercase
8068  versions of letters to be equivalent when matching.
8069 
8070  If REG_NOSUB is set, then when PREG is passed to regexec, that
8071  routine will report only success or failure, and nothing about the
8072  registers.
8073 
8074  It returns 0 if it succeeds, nonzero if it doesn't. (See regex.h for
8075  the return codes and their meanings.) */
8076 
8077 int
8078 regcomp (preg, pattern, cflags)
8080  const char *pattern;
8081  int cflags;
8082 {
8084  reg_syntax_t syntax
8085  = (cflags & REG_EXTENDED) ?
8087 
8088  /* regex_compile will allocate the space for the compiled pattern. */
8089  preg->buffer = 0;
8090  preg->allocated = 0;
8091  preg->used = 0;
8092 
8093  /* Try to allocate space for the fastmap. */
8094  preg->fastmap = (char *) malloc (1 << BYTEWIDTH);
8095 
8096  if (cflags & REG_ICASE)
8097  {
8098  unsigned i;
8099 
8100  preg->translate
8102  * sizeof (*(RE_TRANSLATE_TYPE)0));
8103  if (preg->translate == NULL)
8104  return (int) REG_ESPACE;
8105 
8106  /* Map uppercase characters to corresponding lowercase ones. */
8107  for (i = 0; i < CHAR_SET_SIZE; i++)
8108  preg->translate[i] = ISUPPER (i) ? TOLOWER (i) : i;
8109  }
8110  else
8111  preg->translate = NULL;
8112 
8113  /* If REG_NEWLINE is set, newlines are treated differently. */
8114  if (cflags & REG_NEWLINE)
8115  { /* REG_NEWLINE implies neither . nor [^...] match newline. */
8116  syntax &= ~RE_DOT_NEWLINE;
8117  syntax |= RE_HAT_LISTS_NOT_NEWLINE;
8118  /* It also changes the matching behavior. */
8119  preg->newline_anchor = 1;
8120  }
8121  else
8122  preg->newline_anchor = 0;
8123 
8124  preg->no_sub = !!(cflags & REG_NOSUB);
8125 
8126  /* POSIX says a null character in the pattern terminates it, so we
8127  can use strlen here in compiling the pattern. */
8128 # ifdef MBS_SUPPORT
8129  if (MB_CUR_MAX != 1)
8130  ret = wcs_regex_compile (pattern, strlen (pattern), syntax, preg);
8131  else
8132 # endif
8133  ret = byte_regex_compile (pattern, strlen (pattern), syntax, preg);
8134 
8135  /* POSIX doesn't distinguish between an unmatched open-group and an
8136  unmatched close-group: both are REG_EPAREN. */
8137  if (ret == REG_ERPAREN) ret = REG_EPAREN;
8138 
8139  if (ret == REG_NOERROR && preg->fastmap)
8140  {
8141  /* Compute the fastmap now, since regexec cannot modify the pattern
8142  buffer. */
8143  if (re_compile_fastmap (preg) == -2)
8144  {
8145  /* Some error occurred while computing the fastmap, just forget
8146  about it. */
8147  free (preg->fastmap);
8148  preg->fastmap = NULL;
8149  }
8150  }
8151 
8152  return (int) ret;
8153 }
8154 #ifdef _LIBC
8155 weak_alias (__regcomp, regcomp)
8156 #endif
8157 
8158 
8159 /* regexec searches for a given pattern, specified by PREG, in the
8160  string STRING.
8161 
8162  If NMATCH is zero or REG_NOSUB was set in the cflags argument to
8163  `regcomp', we ignore PMATCH. Otherwise, we assume PMATCH has at
8164  least NMATCH elements, and we set them to the offsets of the
8165  corresponding matched substrings.
8166 
8167  EFLAGS specifies `execution flags' which affect matching: if
8168  REG_NOTBOL is set, then ^ does not match at the beginning of the
8169  string; if REG_NOTEOL is set, then $ does not match at the end.
8170 
8171  We return 0 if we find a match and REG_NOMATCH if not. */
8172 
8173 int
8174 regexec (preg, string, nmatch, pmatch, eflags)
8175  const regex_t *preg;
8176  const char *string;
8177  size_t nmatch;
8178  regmatch_t pmatch[];
8179  int eflags;
8180 {
8181  int ret;
8182  struct re_registers regs;
8183  regex_t private_preg;
8184  int len = strlen (string);
8185  boolean want_reg_info = !preg->no_sub && nmatch > 0;
8186 
8187  private_preg = *preg;
8188 
8189  private_preg.not_bol = !!(eflags & REG_NOTBOL);
8190  private_preg.not_eol = !!(eflags & REG_NOTEOL);
8191 
8192  /* The user has told us exactly how many registers to return
8193  information about, via `nmatch'. We have to pass that on to the
8194  matching routines. */
8195  private_preg.regs_allocated = REGS_FIXED;
8196 
8197  if (want_reg_info)
8198  {
8199  regs.num_regs = nmatch;
8200  regs.start = TALLOC (nmatch * 2, regoff_t);
8201  if (regs.start == NULL)
8202  return (int) REG_NOMATCH;
8203  regs.end = regs.start + nmatch;
8204  }
8205 
8206  /* Perform the searching operation. */
8207  ret = re_search (&private_preg, string, len,
8208  /* start: */ 0, /* range: */ len,
8209  want_reg_info ? &regs : (struct re_registers *) 0);
8210 
8211  /* Copy the register information to the POSIX structure. */
8212  if (want_reg_info)
8213  {
8214  if (ret >= 0)
8215  {
8216  unsigned r;
8217 
8218  for (r = 0; r < nmatch; r++)
8219  {
8220  pmatch[r].rm_so = regs.start[r];
8221  pmatch[r].rm_eo = regs.end[r];
8222  }
8223  }
8224 
8225  /* If we needed the temporary register info, free the space now. */
8226  free (regs.start);
8227  }
8228 
8229  /* We want zero return to mean success, unlike `re_search'. */
8230  return ret >= 0 ? (int) REG_NOERROR : (int) REG_NOMATCH;
8231 }
8232 #ifdef _LIBC
8233 weak_alias (__regexec, regexec)
8234 #endif
8235 
8236 
8237 /* Returns a message corresponding to an error code, ERRCODE, returned
8238  from either regcomp or regexec. We don't use PREG here. */
8239 
8240 size_t
8241 regerror (errcode, preg, errbuf, errbuf_size)
8242  int errcode;
8243  const regex_t *preg;
8244  char *errbuf;
8245  size_t errbuf_size;
8247  const char *msg;
8248  size_t msg_size;
8249 
8250  if (errcode < 0
8251  || errcode >= (int) (sizeof (re_error_msgid_idx)
8252  / sizeof (re_error_msgid_idx[0])))
8253  /* Only error codes returned by the rest of the code should be passed
8254  to this routine. If we are given anything else, or if other regex
8255  code generates an invalid error code, then the program has a bug.
8256  Dump core so we can fix it. */
8257  abort ();
8258 
8259  msg = gettext (re_error_msgid + re_error_msgid_idx[errcode]);
8260 
8261  msg_size = strlen (msg) + 1; /* Includes the null. */
8262 
8263  if (errbuf_size != 0)
8264  {
8265  if (msg_size > errbuf_size)
8266  {
8267 #if defined HAVE_MEMPCPY || defined _LIBC
8268  *((char *) __mempcpy (errbuf, msg, errbuf_size - 1)) = '\0';
8269 #else
8270  memcpy (errbuf, msg, errbuf_size - 1);
8271  errbuf[errbuf_size - 1] = 0;
8272 #endif
8273  }
8274  else
8275  memcpy (errbuf, msg, msg_size);
8276  }
8277 
8278  return msg_size;
8279 }
8280 #ifdef _LIBC
8281 weak_alias (__regerror, regerror)
8282 #endif
8283 
8284 
8285 /* Free dynamically allocated space used by PREG. */
8286 
8287 void
8288 regfree (preg)
8289  regex_t *preg;
8290 {
8291  if (preg->buffer != NULL)
8292  free (preg->buffer);
8293  preg->buffer = NULL;
8294 
8295  preg->allocated = 0;
8296  preg->used = 0;
8297 
8298  if (preg->fastmap != NULL)
8299  free (preg->fastmap);
8300  preg->fastmap = NULL;
8301  preg->fastmap_accurate = 0;
8302 
8303  if (preg->translate != NULL)
8304  free (preg->translate);
8305  preg->translate = NULL;
8306 }
8307 #ifdef _LIBC
8308 weak_alias (__regfree, regfree)
8309 #endif
8310 
8311 #endif /* not emacs */
8312 
8313 #endif /* not INSIDE_RECURSION */
8314 
8315 
8316 
8317 #undef STORE_NUMBER
8318 #undef STORE_NUMBER_AND_INCR
8319 #undef EXTRACT_NUMBER
8320 #undef EXTRACT_NUMBER_AND_INCR
8321 
8322 #undef DEBUG_PRINT_COMPILED_PATTERN
8323 #undef DEBUG_PRINT_DOUBLE_STRING
8324 
8325 #undef INIT_FAIL_STACK
8326 #undef RESET_FAIL_STACK
8327 #undef DOUBLE_FAIL_STACK
8328 #undef PUSH_PATTERN_OP
8329 #undef PUSH_FAILURE_POINTER
8330 #undef PUSH_FAILURE_INT
8331 #undef PUSH_FAILURE_ELT
8332 #undef POP_FAILURE_POINTER
8333 #undef POP_FAILURE_INT
8334 #undef POP_FAILURE_ELT
8335 #undef DEBUG_PUSH
8336 #undef DEBUG_POP
8337 #undef PUSH_FAILURE_POINT
8338 #undef POP_FAILURE_POINT
8339 
8340 #undef REG_UNSET_VALUE
8341 #undef REG_UNSET
8342 
8343 #undef PATFETCH
8344 #undef PATFETCH_RAW
8345 #undef PATUNFETCH
8346 #undef TRANSLATE
8347 
8348 #undef INIT_BUF_SIZE
8349 #undef GET_BUFFER_SPACE
8350 #undef BUF_PUSH
8351 #undef BUF_PUSH_2
8352 #undef BUF_PUSH_3
8353 #undef STORE_JUMP
8354 #undef STORE_JUMP2
8355 #undef INSERT_JUMP
8356 #undef INSERT_JUMP2
8357 #undef EXTEND_BUFFER
8358 #undef GET_UNSIGNED_NUMBER
8359 #undef FREE_STACK_RETURN
8360 
8361 # undef POINTER_TO_OFFSET
8362 # undef MATCHING_IN_FRST_STRING
8363 # undef PREFETCH
8364 # undef AT_STRINGS_BEG
8365 # undef AT_STRINGS_END
8366 # undef WORDCHAR_P
8367 # undef FREE_VAR
8368 # undef FREE_VARIABLES
8369 # undef NO_HIGHEST_ACTIVE_REG
8370 # undef NO_LOWEST_ACTIVE_REG
8371 
8372 # undef CHAR_T
8373 # undef UCHAR_T
8374 # undef COMPILED_BUFFER_VAR
8375 # undef OFFSET_ADDRESS_SIZE
8376 # undef CHAR_CLASS_SIZE
8377 # undef PREFIX
8378 # undef ARG_PREFIX
8379 # undef PUT_CHAR
8380 # undef BYTE
8381 # undef WCHAR
8382 
8383 # define DEFINED_ONCE
regoff_t * end
Definition: regex.h:408
unsigned long int active_reg_t
Definition: regex.h:44
regnum_t regnum
Definition: regex.c:4433
struct re_registers * regs
Definition: regex.c:5011
#define SWITCH_ENUM_CAST(x)
Definition: regex.c:178
Definition: regex.c:458
#define REGS_FIXED
Definition: regex.h:372
#define BYTEWIDTH
Definition: regex.c:397
#define REG_NOTBOL
Definition: regex.h:280
#define ISPRINT(c)
Definition: regex.c:229
#define RE_SYNTAX_POSIX_BASIC
Definition: regex.h:219
#define RE_NO_BK_BRACES
Definition: regex.h:122
Definition: regex.c:464
#define REG_BADBR_IDX
int pos
Definition: regex.c:5534
void struct re_pattern_buffer * bufp
Definition: regex.c:5010
Definition: regex.c:523
Definition: regex.c:516
#define _RE_ARGS(args)
Definition: regex.h:443
#define RE_NO_BK_REFS
Definition: regex.h:130
static char re_syntax_table[CHAR_SET_SIZE]
Definition: regex.c:273
regoff_t rm_so
Definition: regex.h:425
#define REG_ESIZE_IDX
#define RE_CONTEXT_INDEP_ANCHORS
Definition: regex.h:80
#define RE_DUP_MAX
Definition: regex.h:250
static const char re_error_msgid[]
Definition: regex.c:1372
const char * string1
Definition: regex.c:5078
size_t length
Definition: regex.c:7928
char * fastmap
Definition: regex.h:348
#define RE_INVALID_INTERVAL_ORD
Definition: regex.h:165
preg buffer
Definition: regex.c:8089
#define REG_ESUBREG_IDX
#define STREQ(s1, s2)
Definition: regex.c:399
Definition: regex.c:519
Definition: regex.c:487
#define REG_ICASE
Definition: regex.h:261
#define RE_NO_POSIX_BACKTRACKING
Definition: regex.h:148
Definition: regex.c:472
size_t msg_size
Definition: regex.c:8246
#define SIGN_EXTEND_CHAR(c)
Definition: regex.c:259
reg_syntax_t syntax
Definition: regex.h:343
re_opcode_t
Definition: regex.c:456
regoff_t rm_eo
Definition: regex.h:426
int debug
Definition: osis2mod.cpp:76
#define ISCNTRL(c)
Definition: regex.c:233
int size2
Definition: regex.c:5079
else preg translate
Definition: regex.c:8111
Definition: regex.c:461
#define ISGRAPH(c)
Definition: regex.c:225
#define bzero(s, n)
Definition: regex.c:151
Definition: regex.c:526
int range
Definition: regex.c:5043
#define NULL
Definition: regex.c:247
char * malloc()
Definition: regex.c:589
#define TALLOC(n, t)
Definition: regex.c:391
#define RE_NO_BK_VBAR
Definition: regex.h:134
#define RETALLOC(addr, n, t)
Definition: regex.c:392
int cflags
Definition: regex.c:8081
unsigned long int reg_syntax_t
Definition: regex.h:51
free(preg->fastmap)
Definition: regex.c:529
reg_errcode_t
Definition: regex.h:288
#define RE_BACKSLASH_ESCAPE_IN_LISTS
Definition: regex.h:55
reg_syntax_t reg_syntax_t syntax
Definition: regex.c:1349
#define REG_BADRPT_IDX
#define RE_CHAR_CLASSES
Definition: regex.h:66
char * realloc()
#define ISSPACE(c)
Definition: regex.c:236
#define ISLOWER(c)
Definition: regex.c:234
#define RE_NREGS
Definition: regex.h:416
#define REG_ECOLLATE_IDX
if(cflags &REG_ICASE)
Definition: regex.c:8096
size_t int errcode
Definition: regex.c:8242
#define RE_TRANSLATE_TYPE
Definition: regex.h:325
unsigned num_regs
Definition: regex.h:406
#define RE_UNMATCHED_RIGHT_PAREN_ORD
Definition: regex.h:144
#define REG_EPAREN_IDX
#define ISPUNCT(c)
Definition: regex.c:235
#define REG_NEWLINE
Definition: regex.h:266
unsigned fastmap_accurate
Definition: regex.h:377
#define ISBLANK(c)
Definition: regex.c:220
#define RE_DOT_NOT_NULL
Definition: regex.h:100
size_t re_nsub
Definition: regex.h:357
#define PARAMS(args)
Definition: regex.c:39
#define RE_NO_EMPTY_RANGES
Definition: regex.h:140
#define REGS_UNALLOCATED
Definition: regex.h:370
char boolean
Definition: regex.c:406
unsigned long int used
Definition: regex.h:340
#define RE_LIMITED_OPS
Definition: regex.h:113
#define RE_NEWLINE_ALT
Definition: regex.h:117
#define REG_EEND_IDX
preg fastmap
Definition: regex.c:8094
#define REG_ERPAREN_IDX
#define REG_EXTENDED
Definition: regex.h:257
unsigned not_eol
Definition: regex.h:388
regoff_t * start
Definition: regex.h:407
unsigned newline_anchor
Definition: regex.h:391
static const size_t re_error_msgid_idx[]
Definition: regex.c:1426
#define gettext(msgid)
Definition: regex.c:101
#define RE_CONTEXT_INDEP_OPS
Definition: regex.h:88
#define REG_NOMATCH_IDX
const char *const char * pattern
Definition: regex.c:7927
unsigned can_be_null
Definition: regex.h:364
#define REG_ESPACE_IDX
unsigned char * buffer
Definition: regex.h:334
#define FIRST_STRING_P(ptr)
Definition: regex.c:387
regoff_t * starts
Definition: regex.c:5013
int size
Definition: regex.c:5043
#define RETALLOC_IF(addr, n, t)
Definition: regex.c:393
unsigned not_bol
Definition: regex.h:385
#define RE_DOT_NEWLINE
Definition: regex.h:96
Definition: regex.c:578
#define ISALNUM(c)
Definition: regex.c:231
unsigned long int allocated
Definition: regex.h:337
#define RE_INTERVALS
Definition: regex.h:109
#define RE_DEBUG
Definition: regex.h:160
int regex_t * preg
Definition: regex.c:8079
#define RE_NO_GNU_OPS
Definition: regex.h:152
#define REGS_REALLOCATE
Definition: regex.h:371
#define REG_ECTYPE_IDX
#define CHAR_SET_SIZE
Definition: regex.c:265
#define REG_BADPAT_IDX
#define REG_NOERROR_IDX
reg_syntax_t ret
Definition: regex.c:1351
#define REG_NOSUB
Definition: regex.h:270
int stop
Definition: regex.c:5083
reg_syntax_t re_syntax_options
Definition: regex.c:1337
int regoff_t
Definition: regex.h:399
unsigned num_regs
Definition: regex.c:5012
#define RE_BK_PLUS_QM
Definition: regex.h:60
#define RE_NO_BK_PARENS
Definition: regex.h:126
#define SYNTAX(c)
Definition: regex.c:298
#define MIN(a, b)
Definition: regex.c:404
char * errbuf
Definition: regex.c:8244
size_t errbuf_size
Definition: regex.c:8245
result
Definition: regex.c:5545
#define RE_CONTEXT_INVALID_OPS
Definition: regex.h:92
#define ISXDIGIT(c)
Definition: regex.c:238
#define ISALPHA(c)
Definition: regex.c:232
#define gettext_noop(String)
Definition: regex.c:107
Definition: regex.c:590
unsigned no_sub
Definition: regex.h:381
#define const
Definition: zconf.h:85
#define REG_EESCAPE_IDX
#define RE_HAT_LISTS_NOT_NEWLINE
Definition: regex.h:104
int startpos
Definition: regex.c:5043
#define REGEX_TALLOC(n, t)
Definition: regex.c:395
static boolean compile_stack_type compile_stack
Definition: regex.c:4432
#define REG_ERANGE_IDX
#define MAX(a, b)
Definition: regex.c:403
#define MB_LEN_MAX
Definition: regex.c:188
unsigned regs_allocated
Definition: regex.h:373
const char * string2
Definition: regex.c:5078
#define REG_EBRACK_IDX
#define ISUPPER(c)
Definition: regex.c:237
#define RE_SYNTAX_POSIX_EXTENDED
Definition: regex.h:228
int size1
Definition: regex.c:5079
#define REG_NOTEOL
Definition: regex.h:283
#define Sword
Definition: regex.c:172
#define REG_EBRACE_IDX
regoff_t * ends
Definition: regex.c:5013
RE_TRANSLATE_TYPE translate
Definition: regex.h:354
#define TOLOWER(c)
Definition: regex.c:243
#define ISDIGIT(c)
Definition: regex.c:230
static void init_syntax_once()
Definition: regex.c:278