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