Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

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