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