]> code.delx.au - gnu-emacs/blob - src/regex.c
(PUSH_FAILURE_COUNT): New macro.
[gnu-emacs] / src / regex.c
1 /* Extended regular expression matching and search library, version
2 0.12. (Implements POSIX draft P10003.2/D11.2, except for
3 internationalization features.)
4
5 Copyright (C) 1993,94,95,96,97,98,2000 Free Software Foundation, Inc.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20 USA. */
21
22 /* TODO:
23 - structure the opcode space into opcode+flag.
24 - merge with glibc's regex.[ch].
25 - replace succeed_n + jump_n with a combined operation so that the counter
26 can simply be decremented when popping the failure_point without having
27 to stack up failure_count entries.
28 */
29
30 /* AIX requires this to be the first thing in the file. */
31 #if defined (_AIX) && !defined (REGEX_MALLOC)
32 #pragma alloca
33 #endif
34
35 #undef _GNU_SOURCE
36 #define _GNU_SOURCE
37
38 #ifdef emacs
39 /* Converts the pointer to the char to BEG-based offset from the start. */
40 #define PTR_TO_OFFSET(d) POS_AS_IN_BUFFER (POINTER_TO_OFFSET (d))
41 #define POS_AS_IN_BUFFER(p) ((p) + (NILP (re_match_object) || BUFFERP (re_match_object)))
42 #endif
43
44 #ifdef HAVE_CONFIG_H
45 #include <config.h>
46 #endif
47
48 /* We need this for `regex.h', and perhaps for the Emacs include files. */
49 #include <sys/types.h>
50
51 /* This is for other GNU distributions with internationalized messages. */
52 #if HAVE_LIBINTL_H || defined (_LIBC)
53 # include <libintl.h>
54 #else
55 # define gettext(msgid) (msgid)
56 #endif
57
58 #ifndef gettext_noop
59 /* This define is so xgettext can find the internationalizable
60 strings. */
61 #define gettext_noop(String) String
62 #endif
63
64 /* The `emacs' switch turns on certain matching commands
65 that make sense only in Emacs. */
66 #ifdef emacs
67
68 #include "lisp.h"
69 #include "buffer.h"
70
71 /* Make syntax table lookup grant data in gl_state. */
72 #define SYNTAX_ENTRY_VIA_PROPERTY
73
74 #include "syntax.h"
75 #include "charset.h"
76 #include "category.h"
77
78 #define malloc xmalloc
79 #define realloc xrealloc
80 #define free xfree
81
82 #define RE_MULTIBYTE_P(bufp) ((bufp)->multibyte)
83 #define RE_STRING_CHAR(p, s) \
84 (multibyte ? (STRING_CHAR (p, s)) : (*(p)))
85 #define RE_STRING_CHAR_AND_LENGTH(p, s, len) \
86 (multibyte ? (STRING_CHAR_AND_LENGTH (p, s, len)) : ((len) = 1, *(p)))
87
88 /* Set C a (possibly multibyte) character before P. P points into a
89 string which is the virtual concatenation of STR1 (which ends at
90 END1) or STR2 (which ends at END2). */
91 #define GET_CHAR_BEFORE_2(c, p, str1, end1, str2, end2) \
92 do { \
93 if (multibyte) \
94 { \
95 re_char *dtemp = (p) == (str2) ? (end1) : (p); \
96 re_char *dlimit = ((p) > (str2) && (p) <= (end2)) ? (str2) : (str1); \
97 while (dtemp-- > dlimit && !CHAR_HEAD_P (*dtemp)); \
98 c = STRING_CHAR (dtemp, (p) - dtemp); \
99 } \
100 else \
101 (c = ((p) == (str2) ? (end1) : (p))[-1]); \
102 } while (0)
103
104
105 #else /* not emacs */
106
107 /* If we are not linking with Emacs proper,
108 we can't use the relocating allocator
109 even if config.h says that we can. */
110 #undef REL_ALLOC
111
112 #if defined (STDC_HEADERS) || defined (_LIBC)
113 #include <stdlib.h>
114 #else
115 char *malloc ();
116 char *realloc ();
117 #endif
118
119 /* When used in Emacs's lib-src, we need to get bzero and bcopy somehow.
120 If nothing else has been done, use the method below. */
121 #ifdef INHIBIT_STRING_HEADER
122 #if !(defined (HAVE_BZERO) && defined (HAVE_BCOPY))
123 #if !defined (bzero) && !defined (bcopy)
124 #undef INHIBIT_STRING_HEADER
125 #endif
126 #endif
127 #endif
128
129 /* This is the normal way of making sure we have a bcopy and a bzero.
130 This is used in most programs--a few other programs avoid this
131 by defining INHIBIT_STRING_HEADER. */
132 #ifndef INHIBIT_STRING_HEADER
133 #if defined (HAVE_STRING_H) || defined (STDC_HEADERS) || defined (_LIBC)
134 #include <string.h>
135 #ifndef bcmp
136 #define bcmp(s1, s2, n) memcmp ((s1), (s2), (n))
137 #endif
138 #ifndef bcopy
139 #define bcopy(s, d, n) memcpy ((d), (s), (n))
140 #endif
141 #ifndef bzero
142 #define bzero(s, n) memset ((s), 0, (n))
143 #endif
144 #else
145 #include <strings.h>
146 #endif
147 #endif
148
149 /* Define the syntax stuff for \<, \>, etc. */
150
151 /* Sword must be nonzero for the wordchar pattern commands in re_match_2. */
152 enum syntaxcode { Swhitespace = 0, Sword = 1 };
153
154 #ifdef SWITCH_ENUM_BUG
155 #define SWITCH_ENUM_CAST(x) ((int)(x))
156 #else
157 #define SWITCH_ENUM_CAST(x) (x)
158 #endif
159
160 #ifdef SYNTAX_TABLE
161
162 extern char *re_syntax_table;
163
164 #else /* not SYNTAX_TABLE */
165
166 /* How many characters in the character set. */
167 #define CHAR_SET_SIZE 256
168
169 static char re_syntax_table[CHAR_SET_SIZE];
170
171 static void
172 init_syntax_once ()
173 {
174 register int c;
175 static int done = 0;
176
177 if (done)
178 return;
179
180 bzero (re_syntax_table, sizeof re_syntax_table);
181
182 for (c = 'a'; c <= 'z'; c++)
183 re_syntax_table[c] = Sword;
184
185 for (c = 'A'; c <= 'Z'; c++)
186 re_syntax_table[c] = Sword;
187
188 for (c = '0'; c <= '9'; c++)
189 re_syntax_table[c] = Sword;
190
191 re_syntax_table['_'] = Sword;
192
193 done = 1;
194 }
195
196 #endif /* not SYNTAX_TABLE */
197
198 #define SYNTAX(c) re_syntax_table[c]
199
200 /* Dummy macros for non-Emacs environments. */
201 #define BASE_LEADING_CODE_P(c) (0)
202 #define CHAR_CHARSET(c) 0
203 #define CHARSET_LEADING_CODE_BASE(c) 0
204 #define MAX_MULTIBYTE_LENGTH 1
205 #define RE_MULTIBYTE_P(x) 0
206 #define WORD_BOUNDARY_P(c1, c2) (0)
207 #define CHAR_HEAD_P(p) (1)
208 #define SINGLE_BYTE_CHAR_P(c) (1)
209 #define SAME_CHARSET_P(c1, c2) (1)
210 #define MULTIBYTE_FORM_LENGTH(p, s) (1)
211 #define STRING_CHAR(p, s) (*(p))
212 #define RE_STRING_CHAR STRING_CHAR
213 #define CHAR_STRING(c, s) (*(s) = (c), 1)
214 #define STRING_CHAR_AND_LENGTH(p, s, actual_len) ((actual_len) = 1, *(p))
215 #define RE_STRING_CHAR_AND_LENGTH STRING_CHAR_AND_LENGTH
216 #define GET_CHAR_BEFORE_2(c, p, str1, end1, str2, end2) \
217 (c = ((p) == (str2) ? *((end1) - 1) : *((p) - 1)))
218 #define MAKE_CHAR(charset, c1, c2) (c1)
219 #endif /* not emacs */
220
221 #ifndef RE_TRANSLATE
222 #define RE_TRANSLATE(TBL, C) ((unsigned char)(TBL)[C])
223 #define RE_TRANSLATE_P(TBL) (TBL)
224 #endif
225 \f
226 /* Get the interface, including the syntax bits. */
227 #include "regex.h"
228
229 /* isalpha etc. are used for the character classes. */
230 #include <ctype.h>
231
232 #ifdef emacs
233
234 /* 1 if C is an ASCII character. */
235 #define IS_REAL_ASCII(c) ((c) < 0200)
236
237 /* 1 if C is a unibyte character. */
238 #define ISUNIBYTE(c) (SINGLE_BYTE_CHAR_P ((c)))
239
240 /* The Emacs definitions should not be directly affected by locales. */
241
242 /* In Emacs, these are only used for single-byte characters. */
243 #define ISDIGIT(c) ((c) >= '0' && (c) <= '9')
244 #define ISCNTRL(c) ((c) < ' ')
245 #define ISXDIGIT(c) (((c) >= '0' && (c) <= '9') \
246 || ((c) >= 'a' && (c) <= 'f') \
247 || ((c) >= 'A' && (c) <= 'F'))
248
249 /* This is only used for single-byte characters. */
250 #define ISBLANK(c) ((c) == ' ' || (c) == '\t')
251
252 /* The rest must handle multibyte characters. */
253
254 #define ISGRAPH(c) (SINGLE_BYTE_CHAR_P (c) \
255 ? (c) > ' ' && !((c) >= 0177 && (c) <= 0237) \
256 : 1)
257
258 #define ISPRINT(c) (SINGLE_BYTE_CHAR_P (c) \
259 ? (c) >= ' ' && !((c) >= 0177 && (c) <= 0237) \
260 : 1)
261
262 #define ISALNUM(c) (IS_REAL_ASCII (c) \
263 ? (((c) >= 'a' && (c) <= 'z') \
264 || ((c) >= 'A' && (c) <= 'Z') \
265 || ((c) >= '0' && (c) <= '9')) \
266 : SYNTAX (c) == Sword)
267
268 #define ISALPHA(c) (IS_REAL_ASCII (c) \
269 ? (((c) >= 'a' && (c) <= 'z') \
270 || ((c) >= 'A' && (c) <= 'Z')) \
271 : SYNTAX (c) == Sword)
272
273 #define ISLOWER(c) (LOWERCASEP (c))
274
275 #define ISPUNCT(c) (IS_REAL_ASCII (c) \
276 ? ((c) > ' ' && (c) < 0177 \
277 && !(((c) >= 'a' && (c) <= 'z') \
278 || ((c) >= 'A' && (c) <= 'Z') \
279 || ((c) >= '0' && (c) <= '9'))) \
280 : SYNTAX (c) != Sword)
281
282 #define ISSPACE(c) (SYNTAX (c) == Swhitespace)
283
284 #define ISUPPER(c) (UPPERCASEP (c))
285
286 #define ISWORD(c) (SYNTAX (c) == Sword)
287
288 #else /* not emacs */
289
290 /* Jim Meyering writes:
291
292 "... Some ctype macros are valid only for character codes that
293 isascii says are ASCII (SGI's IRIX-4.0.5 is one such system --when
294 using /bin/cc or gcc but without giving an ansi option). So, all
295 ctype uses should be through macros like ISPRINT... If
296 STDC_HEADERS is defined, then autoconf has verified that the ctype
297 macros don't need to be guarded with references to isascii. ...
298 Defining isascii to 1 should let any compiler worth its salt
299 eliminate the && through constant folding." */
300
301 #if defined (STDC_HEADERS) || (!defined (isascii) && !defined (HAVE_ISASCII))
302 #define ISASCII(c) 1
303 #else
304 #define ISASCII(c) isascii(c)
305 #endif
306
307 /* 1 if C is an ASCII character. */
308 #define IS_REAL_ASCII(c) ((c) < 0200)
309
310 /* This distinction is not meaningful, except in Emacs. */
311 #define ISUNIBYTE(c) 1
312
313 #define ISDIGIT(c) (ISASCII (c) && isdigit (c))
314 #define ISCNTRL(c) (ISASCII (c) && iscntrl (c))
315 #define ISXDIGIT(c) (ISASCII (c) && isxdigit (c))
316
317 #ifdef isblank
318 #define ISBLANK(c) (ISASCII (c) && isblank (c))
319 #else
320 #define ISBLANK(c) ((c) == ' ' || (c) == '\t')
321 #endif
322 #ifdef isgraph
323 #define ISGRAPH(c) (ISASCII (c) && isgraph (c))
324 #else
325 #define ISGRAPH(c) (ISASCII (c) && isprint (c) && !isspace (c))
326 #endif
327
328 #define ISPRINT(c) (ISASCII (c) && isprint (c))
329 #define ISDIGIT(c) (ISASCII (c) && isdigit (c))
330 #define ISALNUM(c) (ISASCII (c) && isalnum (c))
331 #define ISALPHA(c) (ISASCII (c) && isalpha (c))
332 #define ISCNTRL(c) (ISASCII (c) && iscntrl (c))
333 #define ISLOWER(c) (ISASCII (c) && islower (c))
334 #define ISPUNCT(c) (ISASCII (c) && ispunct (c))
335 #define ISSPACE(c) (ISASCII (c) && isspace (c))
336 #define ISUPPER(c) (ISASCII (c) && isupper (c))
337 #define ISXDIGIT(c) (ISASCII (c) && isxdigit (c))
338
339 #define ISWORD(c) ISALPHA(c)
340
341 #endif /* not emacs */
342 \f
343 #ifndef NULL
344 #define NULL (void *)0
345 #endif
346
347 /* We remove any previous definition of `SIGN_EXTEND_CHAR',
348 since ours (we hope) works properly with all combinations of
349 machines, compilers, `char' and `unsigned char' argument types.
350 (Per Bothner suggested the basic approach.) */
351 #undef SIGN_EXTEND_CHAR
352 #if __STDC__
353 #define SIGN_EXTEND_CHAR(c) ((signed char) (c))
354 #else /* not __STDC__ */
355 /* As in Harbison and Steele. */
356 #define SIGN_EXTEND_CHAR(c) ((((unsigned char) (c)) ^ 128) - 128)
357 #endif
358 \f
359 /* Should we use malloc or alloca? If REGEX_MALLOC is not defined, we
360 use `alloca' instead of `malloc'. This is because using malloc in
361 re_search* or re_match* could cause memory leaks when C-g is used in
362 Emacs; also, malloc is slower and causes storage fragmentation. On
363 the other hand, malloc is more portable, and easier to debug.
364
365 Because we sometimes use alloca, some routines have to be macros,
366 not functions -- `alloca'-allocated space disappears at the end of the
367 function it is called in. */
368
369 #ifdef REGEX_MALLOC
370
371 #define REGEX_ALLOCATE malloc
372 #define REGEX_REALLOCATE(source, osize, nsize) realloc (source, nsize)
373 #define REGEX_FREE free
374
375 #else /* not REGEX_MALLOC */
376
377 /* Emacs already defines alloca, sometimes. */
378 #ifndef alloca
379
380 /* Make alloca work the best possible way. */
381 #ifdef __GNUC__
382 #define alloca __builtin_alloca
383 #else /* not __GNUC__ */
384 #if HAVE_ALLOCA_H
385 #include <alloca.h>
386 #else /* not __GNUC__ or HAVE_ALLOCA_H */
387 #if 0 /* It is a bad idea to declare alloca. We always cast the result. */
388 #ifndef _AIX /* Already did AIX, up at the top. */
389 char *alloca ();
390 #endif /* not _AIX */
391 #endif
392 #endif /* not HAVE_ALLOCA_H */
393 #endif /* not __GNUC__ */
394
395 #endif /* not alloca */
396
397 #define REGEX_ALLOCATE alloca
398
399 /* Assumes a `char *destination' variable. */
400 #define REGEX_REALLOCATE(source, osize, nsize) \
401 (destination = (char *) alloca (nsize), \
402 bcopy (source, destination, osize), \
403 destination)
404
405 /* No need to do anything to free, after alloca. */
406 #define REGEX_FREE(arg) ((void)0) /* Do nothing! But inhibit gcc warning. */
407
408 #endif /* not REGEX_MALLOC */
409
410 /* Define how to allocate the failure stack. */
411
412 #if defined (REL_ALLOC) && defined (REGEX_MALLOC)
413
414 #define REGEX_ALLOCATE_STACK(size) \
415 r_alloc (&failure_stack_ptr, (size))
416 #define REGEX_REALLOCATE_STACK(source, osize, nsize) \
417 r_re_alloc (&failure_stack_ptr, (nsize))
418 #define REGEX_FREE_STACK(ptr) \
419 r_alloc_free (&failure_stack_ptr)
420
421 #else /* not using relocating allocator */
422
423 #ifdef REGEX_MALLOC
424
425 #define REGEX_ALLOCATE_STACK malloc
426 #define REGEX_REALLOCATE_STACK(source, osize, nsize) realloc (source, nsize)
427 #define REGEX_FREE_STACK free
428
429 #else /* not REGEX_MALLOC */
430
431 #define REGEX_ALLOCATE_STACK alloca
432
433 #define REGEX_REALLOCATE_STACK(source, osize, nsize) \
434 REGEX_REALLOCATE (source, osize, nsize)
435 /* No need to explicitly free anything. */
436 #define REGEX_FREE_STACK(arg) ((void)0)
437
438 #endif /* not REGEX_MALLOC */
439 #endif /* not using relocating allocator */
440
441
442 /* True if `size1' is non-NULL and PTR is pointing anywhere inside
443 `string1' or just past its end. This works if PTR is NULL, which is
444 a good thing. */
445 #define FIRST_STRING_P(ptr) \
446 (size1 && string1 <= (ptr) && (ptr) <= string1 + size1)
447
448 /* (Re)Allocate N items of type T using malloc, or fail. */
449 #define TALLOC(n, t) ((t *) malloc ((n) * sizeof (t)))
450 #define RETALLOC(addr, n, t) ((addr) = (t *) realloc (addr, (n) * sizeof (t)))
451 #define RETALLOC_IF(addr, n, t) \
452 if (addr) RETALLOC((addr), (n), t); else (addr) = TALLOC ((n), t)
453 #define REGEX_TALLOC(n, t) ((t *) REGEX_ALLOCATE ((n) * sizeof (t)))
454
455 #define BYTEWIDTH 8 /* In bits. */
456
457 #define STREQ(s1, s2) ((strcmp (s1, s2) == 0))
458
459 #undef MAX
460 #undef MIN
461 #define MAX(a, b) ((a) > (b) ? (a) : (b))
462 #define MIN(a, b) ((a) < (b) ? (a) : (b))
463
464 /* Type of source-pattern and string chars. */
465 typedef const unsigned char re_char;
466
467 typedef char boolean;
468 #define false 0
469 #define true 1
470
471 static int re_match_2_internal ();
472 \f
473 /* These are the command codes that appear in compiled regular
474 expressions. Some opcodes are followed by argument bytes. A
475 command code can specify any interpretation whatsoever for its
476 arguments. Zero bytes may appear in the compiled regular expression. */
477
478 typedef enum
479 {
480 no_op = 0,
481
482 /* Succeed right away--no more backtracking. */
483 succeed,
484
485 /* Followed by one byte giving n, then by n literal bytes. */
486 exactn,
487
488 /* Matches any (more or less) character. */
489 anychar,
490
491 /* Matches any one char belonging to specified set. First
492 following byte is number of bitmap bytes. Then come bytes
493 for a bitmap saying which chars are in. Bits in each byte
494 are ordered low-bit-first. A character is in the set if its
495 bit is 1. A character too large to have a bit in the map is
496 automatically not in the set.
497
498 If the length byte has the 0x80 bit set, then that stuff
499 is followed by a range table:
500 2 bytes of flags for character sets (low 8 bits, high 8 bits)
501 See RANGE_TABLE_WORK_BITS below.
502 2 bytes, the number of pairs that follow
503 pairs, each 2 multibyte characters,
504 each multibyte character represented as 3 bytes. */
505 charset,
506
507 /* Same parameters as charset, but match any character that is
508 not one of those specified. */
509 charset_not,
510
511 /* Start remembering the text that is matched, for storing in a
512 register. Followed by one byte with the register number, in
513 the range 0 to one less than the pattern buffer's re_nsub
514 field. */
515 start_memory,
516
517 /* Stop remembering the text that is matched and store it in a
518 memory register. Followed by one byte with the register
519 number, in the range 0 to one less than `re_nsub' in the
520 pattern buffer. */
521 stop_memory,
522
523 /* Match a duplicate of something remembered. Followed by one
524 byte containing the register number. */
525 duplicate,
526
527 /* Fail unless at beginning of line. */
528 begline,
529
530 /* Fail unless at end of line. */
531 endline,
532
533 /* Succeeds if at beginning of buffer (if emacs) or at beginning
534 of string to be matched (if not). */
535 begbuf,
536
537 /* Analogously, for end of buffer/string. */
538 endbuf,
539
540 /* Followed by two byte relative address to which to jump. */
541 jump,
542
543 /* Followed by two-byte relative address of place to resume at
544 in case of failure. */
545 on_failure_jump,
546
547 /* Like on_failure_jump, but pushes a placeholder instead of the
548 current string position when executed. */
549 on_failure_keep_string_jump,
550
551 /* Just like `on_failure_jump', except that it checks that we
552 don't get stuck in an infinite loop (matching an empty string
553 indefinitely). */
554 on_failure_jump_loop,
555
556 /* Just like `on_failure_jump_loop', except that it checks for
557 a different kind of loop (the kind that shows up with non-greedy
558 operators). This operation has to be immediately preceded
559 by a `no_op'. */
560 on_failure_jump_nastyloop,
561
562 /* A smart `on_failure_jump' used for greedy * and + operators.
563 It analyses the loop before which it is put and if the
564 loop does not require backtracking, it changes itself to
565 `on_failure_keep_string_jump' and short-circuits the loop,
566 else it just defaults to changing itself into `on_failure_jump'.
567 It assumes that it is pointing to just past a `jump'. */
568 on_failure_jump_smart,
569
570 /* Followed by two-byte relative address and two-byte number n.
571 After matching N times, jump to the address upon failure.
572 Does not work if N starts at 0: use on_failure_jump_loop
573 instead. */
574 succeed_n,
575
576 /* Followed by two-byte relative address, and two-byte number n.
577 Jump to the address N times, then fail. */
578 jump_n,
579
580 /* Set the following two-byte relative address to the
581 subsequent two-byte number. The address *includes* the two
582 bytes of number. */
583 set_number_at,
584
585 wordbeg, /* Succeeds if at word beginning. */
586 wordend, /* Succeeds if at word end. */
587
588 wordbound, /* Succeeds if at a word boundary. */
589 notwordbound, /* Succeeds if not at a word boundary. */
590
591 /* Matches any character whose syntax is specified. Followed by
592 a byte which contains a syntax code, e.g., Sword. */
593 syntaxspec,
594
595 /* Matches any character whose syntax is not that specified. */
596 notsyntaxspec
597
598 #ifdef emacs
599 ,before_dot, /* Succeeds if before point. */
600 at_dot, /* Succeeds if at point. */
601 after_dot, /* Succeeds if after point. */
602
603 /* Matches any character whose category-set contains the specified
604 category. The operator is followed by a byte which contains a
605 category code (mnemonic ASCII character). */
606 categoryspec,
607
608 /* Matches any character whose category-set does not contain the
609 specified category. The operator is followed by a byte which
610 contains the category code (mnemonic ASCII character). */
611 notcategoryspec
612 #endif /* emacs */
613 } re_opcode_t;
614 \f
615 /* Common operations on the compiled pattern. */
616
617 /* Store NUMBER in two contiguous bytes starting at DESTINATION. */
618
619 #define STORE_NUMBER(destination, number) \
620 do { \
621 (destination)[0] = (number) & 0377; \
622 (destination)[1] = (number) >> 8; \
623 } while (0)
624
625 /* Same as STORE_NUMBER, except increment DESTINATION to
626 the byte after where the number is stored. Therefore, DESTINATION
627 must be an lvalue. */
628
629 #define STORE_NUMBER_AND_INCR(destination, number) \
630 do { \
631 STORE_NUMBER (destination, number); \
632 (destination) += 2; \
633 } while (0)
634
635 /* Put into DESTINATION a number stored in two contiguous bytes starting
636 at SOURCE. */
637
638 #define EXTRACT_NUMBER(destination, source) \
639 do { \
640 (destination) = *(source) & 0377; \
641 (destination) += SIGN_EXTEND_CHAR (*((source) + 1)) << 8; \
642 } while (0)
643
644 #ifdef DEBUG
645 static void
646 extract_number (dest, source)
647 int *dest;
648 unsigned char *source;
649 {
650 int temp = SIGN_EXTEND_CHAR (*(source + 1));
651 *dest = *source & 0377;
652 *dest += temp << 8;
653 }
654
655 #ifndef EXTRACT_MACROS /* To debug the macros. */
656 #undef EXTRACT_NUMBER
657 #define EXTRACT_NUMBER(dest, src) extract_number (&dest, src)
658 #endif /* not EXTRACT_MACROS */
659
660 #endif /* DEBUG */
661
662 /* Same as EXTRACT_NUMBER, except increment SOURCE to after the number.
663 SOURCE must be an lvalue. */
664
665 #define EXTRACT_NUMBER_AND_INCR(destination, source) \
666 do { \
667 EXTRACT_NUMBER (destination, source); \
668 (source) += 2; \
669 } while (0)
670
671 #ifdef DEBUG
672 static void
673 extract_number_and_incr (destination, source)
674 int *destination;
675 unsigned char **source;
676 {
677 extract_number (destination, *source);
678 *source += 2;
679 }
680
681 #ifndef EXTRACT_MACROS
682 #undef EXTRACT_NUMBER_AND_INCR
683 #define EXTRACT_NUMBER_AND_INCR(dest, src) \
684 extract_number_and_incr (&dest, &src)
685 #endif /* not EXTRACT_MACROS */
686
687 #endif /* DEBUG */
688 \f
689 /* Store a multibyte character in three contiguous bytes starting
690 DESTINATION, and increment DESTINATION to the byte after where the
691 character is stored. Therefore, DESTINATION must be an lvalue. */
692
693 #define STORE_CHARACTER_AND_INCR(destination, character) \
694 do { \
695 (destination)[0] = (character) & 0377; \
696 (destination)[1] = ((character) >> 8) & 0377; \
697 (destination)[2] = (character) >> 16; \
698 (destination) += 3; \
699 } while (0)
700
701 /* Put into DESTINATION a character stored in three contiguous bytes
702 starting at SOURCE. */
703
704 #define EXTRACT_CHARACTER(destination, source) \
705 do { \
706 (destination) = ((source)[0] \
707 | ((source)[1] << 8) \
708 | ((source)[2] << 16)); \
709 } while (0)
710
711
712 /* Macros for charset. */
713
714 /* Size of bitmap of charset P in bytes. P is a start of charset,
715 i.e. *P is (re_opcode_t) charset or (re_opcode_t) charset_not. */
716 #define CHARSET_BITMAP_SIZE(p) ((p)[1] & 0x7F)
717
718 /* Nonzero if charset P has range table. */
719 #define CHARSET_RANGE_TABLE_EXISTS_P(p) ((p)[1] & 0x80)
720
721 /* Return the address of range table of charset P. But not the start
722 of table itself, but the before where the number of ranges is
723 stored. `2 +' means to skip re_opcode_t and size of bitmap,
724 and the 2 bytes of flags at the start of the range table. */
725 #define CHARSET_RANGE_TABLE(p) (&(p)[4 + CHARSET_BITMAP_SIZE (p)])
726
727 /* Extract the bit flags that start a range table. */
728 #define CHARSET_RANGE_TABLE_BITS(p) \
729 ((p)[2 + CHARSET_BITMAP_SIZE (p)] \
730 + (p)[3 + CHARSET_BITMAP_SIZE (p)] * 0x100)
731
732 /* Test if C is listed in the bitmap of charset P. */
733 #define CHARSET_LOOKUP_BITMAP(p, c) \
734 ((c) < CHARSET_BITMAP_SIZE (p) * BYTEWIDTH \
735 && (p)[2 + (c) / BYTEWIDTH] & (1 << ((c) % BYTEWIDTH)))
736
737 /* Return the address of end of RANGE_TABLE. COUNT is number of
738 ranges (which is a pair of (start, end)) in the RANGE_TABLE. `* 2'
739 is start of range and end of range. `* 3' is size of each start
740 and end. */
741 #define CHARSET_RANGE_TABLE_END(range_table, count) \
742 ((range_table) + (count) * 2 * 3)
743
744 /* Test if C is in RANGE_TABLE. A flag NOT is negated if C is in.
745 COUNT is number of ranges in RANGE_TABLE. */
746 #define CHARSET_LOOKUP_RANGE_TABLE_RAW(not, c, range_table, count) \
747 do \
748 { \
749 int range_start, range_end; \
750 unsigned char *p; \
751 unsigned char *range_table_end \
752 = CHARSET_RANGE_TABLE_END ((range_table), (count)); \
753 \
754 for (p = (range_table); p < range_table_end; p += 2 * 3) \
755 { \
756 EXTRACT_CHARACTER (range_start, p); \
757 EXTRACT_CHARACTER (range_end, p + 3); \
758 \
759 if (range_start <= (c) && (c) <= range_end) \
760 { \
761 (not) = !(not); \
762 break; \
763 } \
764 } \
765 } \
766 while (0)
767
768 /* Test if C is in range table of CHARSET. The flag NOT is negated if
769 C is listed in it. */
770 #define CHARSET_LOOKUP_RANGE_TABLE(not, c, charset) \
771 do \
772 { \
773 /* Number of ranges in range table. */ \
774 int count; \
775 unsigned char *range_table = CHARSET_RANGE_TABLE (charset); \
776 \
777 EXTRACT_NUMBER_AND_INCR (count, range_table); \
778 CHARSET_LOOKUP_RANGE_TABLE_RAW ((not), (c), range_table, count); \
779 } \
780 while (0)
781 \f
782 /* If DEBUG is defined, Regex prints many voluminous messages about what
783 it is doing (if the variable `debug' is nonzero). If linked with the
784 main program in `iregex.c', you can enter patterns and strings
785 interactively. And if linked with the main program in `main.c' and
786 the other test files, you can run the already-written tests. */
787
788 #ifdef DEBUG
789
790 /* We use standard I/O for debugging. */
791 #include <stdio.h>
792
793 /* It is useful to test things that ``must'' be true when debugging. */
794 #include <assert.h>
795
796 static int debug = -100000;
797
798 #define DEBUG_STATEMENT(e) e
799 #define DEBUG_PRINT1(x) if (debug > 0) printf (x)
800 #define DEBUG_PRINT2(x1, x2) if (debug > 0) printf (x1, x2)
801 #define DEBUG_PRINT3(x1, x2, x3) if (debug > 0) printf (x1, x2, x3)
802 #define DEBUG_PRINT4(x1, x2, x3, x4) if (debug > 0) printf (x1, x2, x3, x4)
803 #define DEBUG_PRINT_COMPILED_PATTERN(p, s, e) \
804 if (debug > 0) print_partial_compiled_pattern (s, e)
805 #define DEBUG_PRINT_DOUBLE_STRING(w, s1, sz1, s2, sz2) \
806 if (debug > 0) print_double_string (w, s1, sz1, s2, sz2)
807
808
809 /* Print the fastmap in human-readable form. */
810
811 void
812 print_fastmap (fastmap)
813 char *fastmap;
814 {
815 unsigned was_a_range = 0;
816 unsigned i = 0;
817
818 while (i < (1 << BYTEWIDTH))
819 {
820 if (fastmap[i++])
821 {
822 was_a_range = 0;
823 putchar (i - 1);
824 while (i < (1 << BYTEWIDTH) && fastmap[i])
825 {
826 was_a_range = 1;
827 i++;
828 }
829 if (was_a_range)
830 {
831 printf ("-");
832 putchar (i - 1);
833 }
834 }
835 }
836 putchar ('\n');
837 }
838
839
840 /* Print a compiled pattern string in human-readable form, starting at
841 the START pointer into it and ending just before the pointer END. */
842
843 void
844 print_partial_compiled_pattern (start, end)
845 unsigned char *start;
846 unsigned char *end;
847 {
848 int mcnt, mcnt2;
849 unsigned char *p = start;
850 unsigned char *pend = end;
851
852 if (start == NULL)
853 {
854 printf ("(null)\n");
855 return;
856 }
857
858 /* Loop over pattern commands. */
859 while (p < pend)
860 {
861 printf ("%d:\t", p - start);
862
863 switch ((re_opcode_t) *p++)
864 {
865 case no_op:
866 printf ("/no_op");
867 break;
868
869 case succeed:
870 printf ("/succeed");
871 break;
872
873 case exactn:
874 mcnt = *p++;
875 printf ("/exactn/%d", mcnt);
876 do
877 {
878 putchar ('/');
879 putchar (*p++);
880 }
881 while (--mcnt);
882 break;
883
884 case start_memory:
885 printf ("/start_memory/%d", *p++);
886 break;
887
888 case stop_memory:
889 printf ("/stop_memory/%d", *p++);
890 break;
891
892 case duplicate:
893 printf ("/duplicate/%d", *p++);
894 break;
895
896 case anychar:
897 printf ("/anychar");
898 break;
899
900 case charset:
901 case charset_not:
902 {
903 register int c, last = -100;
904 register int in_range = 0;
905 int length = CHARSET_BITMAP_SIZE (p - 1);
906 int has_range_table = CHARSET_RANGE_TABLE_EXISTS_P (p - 1);
907
908 printf ("/charset [%s",
909 (re_opcode_t) *(p - 1) == charset_not ? "^" : "");
910
911 assert (p + *p < pend);
912
913 for (c = 0; c < 256; c++)
914 if (c / 8 < length
915 && (p[1 + (c/8)] & (1 << (c % 8))))
916 {
917 /* Are we starting a range? */
918 if (last + 1 == c && ! in_range)
919 {
920 putchar ('-');
921 in_range = 1;
922 }
923 /* Have we broken a range? */
924 else if (last + 1 != c && in_range)
925 {
926 putchar (last);
927 in_range = 0;
928 }
929
930 if (! in_range)
931 putchar (c);
932
933 last = c;
934 }
935
936 if (in_range)
937 putchar (last);
938
939 putchar (']');
940
941 p += 1 + length;
942
943 if (has_range_table)
944 {
945 int count;
946 printf ("has-range-table");
947
948 /* ??? Should print the range table; for now, just skip it. */
949 p += 2; /* skip range table bits */
950 EXTRACT_NUMBER_AND_INCR (count, p);
951 p = CHARSET_RANGE_TABLE_END (p, count);
952 }
953 }
954 break;
955
956 case begline:
957 printf ("/begline");
958 break;
959
960 case endline:
961 printf ("/endline");
962 break;
963
964 case on_failure_jump:
965 extract_number_and_incr (&mcnt, &p);
966 printf ("/on_failure_jump to %d", p + mcnt - start);
967 break;
968
969 case on_failure_keep_string_jump:
970 extract_number_and_incr (&mcnt, &p);
971 printf ("/on_failure_keep_string_jump to %d", p + mcnt - start);
972 break;
973
974 case on_failure_jump_nastyloop:
975 extract_number_and_incr (&mcnt, &p);
976 printf ("/on_failure_jump_nastyloop to %d", p + mcnt - start);
977 break;
978
979 case on_failure_jump_loop:
980 extract_number_and_incr (&mcnt, &p);
981 printf ("/on_failure_jump_loop to %d", p + mcnt - start);
982 break;
983
984 case on_failure_jump_smart:
985 extract_number_and_incr (&mcnt, &p);
986 printf ("/on_failure_jump_smart to %d", p + mcnt - start);
987 break;
988
989 case jump:
990 extract_number_and_incr (&mcnt, &p);
991 printf ("/jump to %d", p + mcnt - start);
992 break;
993
994 case succeed_n:
995 extract_number_and_incr (&mcnt, &p);
996 extract_number_and_incr (&mcnt2, &p);
997 printf ("/succeed_n to %d, %d times", p - 2 + mcnt - start, mcnt2);
998 break;
999
1000 case jump_n:
1001 extract_number_and_incr (&mcnt, &p);
1002 extract_number_and_incr (&mcnt2, &p);
1003 printf ("/jump_n to %d, %d times", p - 2 + mcnt - start, mcnt2);
1004 break;
1005
1006 case set_number_at:
1007 extract_number_and_incr (&mcnt, &p);
1008 extract_number_and_incr (&mcnt2, &p);
1009 printf ("/set_number_at location %d to %d", p - 2 + mcnt - start, mcnt2);
1010 break;
1011
1012 case wordbound:
1013 printf ("/wordbound");
1014 break;
1015
1016 case notwordbound:
1017 printf ("/notwordbound");
1018 break;
1019
1020 case wordbeg:
1021 printf ("/wordbeg");
1022 break;
1023
1024 case wordend:
1025 printf ("/wordend");
1026
1027 case syntaxspec:
1028 printf ("/syntaxspec");
1029 mcnt = *p++;
1030 printf ("/%d", mcnt);
1031 break;
1032
1033 case notsyntaxspec:
1034 printf ("/notsyntaxspec");
1035 mcnt = *p++;
1036 printf ("/%d", mcnt);
1037 break;
1038
1039 #ifdef emacs
1040 case before_dot:
1041 printf ("/before_dot");
1042 break;
1043
1044 case at_dot:
1045 printf ("/at_dot");
1046 break;
1047
1048 case after_dot:
1049 printf ("/after_dot");
1050 break;
1051
1052 case categoryspec:
1053 printf ("/categoryspec");
1054 mcnt = *p++;
1055 printf ("/%d", mcnt);
1056 break;
1057
1058 case notcategoryspec:
1059 printf ("/notcategoryspec");
1060 mcnt = *p++;
1061 printf ("/%d", mcnt);
1062 break;
1063 #endif /* emacs */
1064
1065 case begbuf:
1066 printf ("/begbuf");
1067 break;
1068
1069 case endbuf:
1070 printf ("/endbuf");
1071 break;
1072
1073 default:
1074 printf ("?%d", *(p-1));
1075 }
1076
1077 putchar ('\n');
1078 }
1079
1080 printf ("%d:\tend of pattern.\n", p - start);
1081 }
1082
1083
1084 void
1085 print_compiled_pattern (bufp)
1086 struct re_pattern_buffer *bufp;
1087 {
1088 unsigned char *buffer = bufp->buffer;
1089
1090 print_partial_compiled_pattern (buffer, buffer + bufp->used);
1091 printf ("%ld bytes used/%ld bytes allocated.\n", bufp->used, bufp->allocated);
1092
1093 if (bufp->fastmap_accurate && bufp->fastmap)
1094 {
1095 printf ("fastmap: ");
1096 print_fastmap (bufp->fastmap);
1097 }
1098
1099 printf ("re_nsub: %d\t", bufp->re_nsub);
1100 printf ("regs_alloc: %d\t", bufp->regs_allocated);
1101 printf ("can_be_null: %d\t", bufp->can_be_null);
1102 printf ("newline_anchor: %d\n", bufp->newline_anchor);
1103 printf ("no_sub: %d\t", bufp->no_sub);
1104 printf ("not_bol: %d\t", bufp->not_bol);
1105 printf ("not_eol: %d\t", bufp->not_eol);
1106 printf ("syntax: %d\n", bufp->syntax);
1107 fflush (stdout);
1108 /* Perhaps we should print the translate table? */
1109 }
1110
1111
1112 void
1113 print_double_string (where, string1, size1, string2, size2)
1114 re_char *where;
1115 re_char *string1;
1116 re_char *string2;
1117 int size1;
1118 int size2;
1119 {
1120 unsigned this_char;
1121
1122 if (where == NULL)
1123 printf ("(null)");
1124 else
1125 {
1126 if (FIRST_STRING_P (where))
1127 {
1128 for (this_char = where - string1; this_char < size1; this_char++)
1129 putchar (string1[this_char]);
1130
1131 where = string2;
1132 }
1133
1134 for (this_char = where - string2; this_char < size2; this_char++)
1135 putchar (string2[this_char]);
1136 }
1137 }
1138
1139 #else /* not DEBUG */
1140
1141 #undef assert
1142 #define assert(e)
1143
1144 #define DEBUG_STATEMENT(e)
1145 #define DEBUG_PRINT1(x)
1146 #define DEBUG_PRINT2(x1, x2)
1147 #define DEBUG_PRINT3(x1, x2, x3)
1148 #define DEBUG_PRINT4(x1, x2, x3, x4)
1149 #define DEBUG_PRINT_COMPILED_PATTERN(p, s, e)
1150 #define DEBUG_PRINT_DOUBLE_STRING(w, s1, sz1, s2, sz2)
1151
1152 #endif /* not DEBUG */
1153 \f
1154 /* Set by `re_set_syntax' to the current regexp syntax to recognize. Can
1155 also be assigned to arbitrarily: each pattern buffer stores its own
1156 syntax, so it can be changed between regex compilations. */
1157 /* This has no initializer because initialized variables in Emacs
1158 become read-only after dumping. */
1159 reg_syntax_t re_syntax_options;
1160
1161
1162 /* Specify the precise syntax of regexps for compilation. This provides
1163 for compatibility for various utilities which historically have
1164 different, incompatible syntaxes.
1165
1166 The argument SYNTAX is a bit mask comprised of the various bits
1167 defined in regex.h. We return the old syntax. */
1168
1169 reg_syntax_t
1170 re_set_syntax (syntax)
1171 reg_syntax_t syntax;
1172 {
1173 reg_syntax_t ret = re_syntax_options;
1174
1175 re_syntax_options = syntax;
1176 return ret;
1177 }
1178 \f
1179 /* This table gives an error message for each of the error codes listed
1180 in regex.h. Obviously the order here has to be same as there.
1181 POSIX doesn't require that we do anything for REG_NOERROR,
1182 but why not be nice? */
1183
1184 static const char *re_error_msgid[] =
1185 {
1186 gettext_noop ("Success"), /* REG_NOERROR */
1187 gettext_noop ("No match"), /* REG_NOMATCH */
1188 gettext_noop ("Invalid regular expression"), /* REG_BADPAT */
1189 gettext_noop ("Invalid collation character"), /* REG_ECOLLATE */
1190 gettext_noop ("Invalid character class name"), /* REG_ECTYPE */
1191 gettext_noop ("Trailing backslash"), /* REG_EESCAPE */
1192 gettext_noop ("Invalid back reference"), /* REG_ESUBREG */
1193 gettext_noop ("Unmatched [ or [^"), /* REG_EBRACK */
1194 gettext_noop ("Unmatched ( or \\("), /* REG_EPAREN */
1195 gettext_noop ("Unmatched \\{"), /* REG_EBRACE */
1196 gettext_noop ("Invalid content of \\{\\}"), /* REG_BADBR */
1197 gettext_noop ("Invalid range end"), /* REG_ERANGE */
1198 gettext_noop ("Memory exhausted"), /* REG_ESPACE */
1199 gettext_noop ("Invalid preceding regular expression"), /* REG_BADRPT */
1200 gettext_noop ("Premature end of regular expression"), /* REG_EEND */
1201 gettext_noop ("Regular expression too big"), /* REG_ESIZE */
1202 gettext_noop ("Unmatched ) or \\)"), /* REG_ERPAREN */
1203 };
1204 \f
1205 /* Avoiding alloca during matching, to placate r_alloc. */
1206
1207 /* Define MATCH_MAY_ALLOCATE unless we need to make sure that the
1208 searching and matching functions should not call alloca. On some
1209 systems, alloca is implemented in terms of malloc, and if we're
1210 using the relocating allocator routines, then malloc could cause a
1211 relocation, which might (if the strings being searched are in the
1212 ralloc heap) shift the data out from underneath the regexp
1213 routines.
1214
1215 Here's another reason to avoid allocation: Emacs
1216 processes input from X in a signal handler; processing X input may
1217 call malloc; if input arrives while a matching routine is calling
1218 malloc, then we're scrod. But Emacs can't just block input while
1219 calling matching routines; then we don't notice interrupts when
1220 they come in. So, Emacs blocks input around all regexp calls
1221 except the matching calls, which it leaves unprotected, in the
1222 faith that they will not malloc. */
1223
1224 /* Normally, this is fine. */
1225 #define MATCH_MAY_ALLOCATE
1226
1227 /* When using GNU C, we are not REALLY using the C alloca, no matter
1228 what config.h may say. So don't take precautions for it. */
1229 #ifdef __GNUC__
1230 #undef C_ALLOCA
1231 #endif
1232
1233 /* The match routines may not allocate if (1) they would do it with malloc
1234 and (2) it's not safe for them to use malloc.
1235 Note that if REL_ALLOC is defined, matching would not use malloc for the
1236 failure stack, but we would still use it for the register vectors;
1237 so REL_ALLOC should not affect this. */
1238 #if (defined (C_ALLOCA) || defined (REGEX_MALLOC)) && defined (emacs)
1239 #undef MATCH_MAY_ALLOCATE
1240 #endif
1241
1242 \f
1243 /* Failure stack declarations and macros; both re_compile_fastmap and
1244 re_match_2 use a failure stack. These have to be macros because of
1245 REGEX_ALLOCATE_STACK. */
1246
1247
1248 /* Approximate number of failure points for which to initially allocate space
1249 when matching. If this number is exceeded, we allocate more
1250 space, so it is not a hard limit. */
1251 #ifndef INIT_FAILURE_ALLOC
1252 #define INIT_FAILURE_ALLOC 20
1253 #endif
1254
1255 /* Roughly the maximum number of failure points on the stack. Would be
1256 exactly that if always used TYPICAL_FAILURE_SIZE items each time we failed.
1257 This is a variable only so users of regex can assign to it; we never
1258 change it ourselves. */
1259 #if defined (MATCH_MAY_ALLOCATE)
1260 /* Note that 4400 is enough to cause a crash on Alpha OSF/1,
1261 whose default stack limit is 2mb. In order for a larger
1262 value to work reliably, you have to try to make it accord
1263 with the process stack limit. */
1264 int re_max_failures = 40000;
1265 #else
1266 int re_max_failures = 4000;
1267 #endif
1268
1269 union fail_stack_elt
1270 {
1271 const unsigned char *pointer;
1272 unsigned int integer;
1273 };
1274
1275 typedef union fail_stack_elt fail_stack_elt_t;
1276
1277 typedef struct
1278 {
1279 fail_stack_elt_t *stack;
1280 unsigned size;
1281 unsigned avail; /* Offset of next open position. */
1282 unsigned frame; /* Offset of the cur constructed frame. */
1283 } fail_stack_type;
1284
1285 #define PATTERN_STACK_EMPTY() (fail_stack.avail == 0)
1286 #define FAIL_STACK_EMPTY() (fail_stack.frame == 0)
1287 #define FAIL_STACK_FULL() (fail_stack.avail == fail_stack.size)
1288
1289
1290 /* Define macros to initialize and free the failure stack.
1291 Do `return -2' if the alloc fails. */
1292
1293 #ifdef MATCH_MAY_ALLOCATE
1294 #define INIT_FAIL_STACK() \
1295 do { \
1296 fail_stack.stack = (fail_stack_elt_t *) \
1297 REGEX_ALLOCATE_STACK (INIT_FAILURE_ALLOC * TYPICAL_FAILURE_SIZE \
1298 * sizeof (fail_stack_elt_t)); \
1299 \
1300 if (fail_stack.stack == NULL) \
1301 return -2; \
1302 \
1303 fail_stack.size = INIT_FAILURE_ALLOC; \
1304 fail_stack.avail = 0; \
1305 fail_stack.frame = 0; \
1306 } while (0)
1307
1308 #define RESET_FAIL_STACK() REGEX_FREE_STACK (fail_stack.stack)
1309 #else
1310 #define INIT_FAIL_STACK() \
1311 do { \
1312 fail_stack.avail = 0; \
1313 fail_stack.frame = 0; \
1314 } while (0)
1315
1316 #define RESET_FAIL_STACK() ((void)0)
1317 #endif
1318
1319
1320 /* Double the size of FAIL_STACK, up to a limit
1321 which allows approximately `re_max_failures' items.
1322
1323 Return 1 if succeeds, and 0 if either ran out of memory
1324 allocating space for it or it was already too large.
1325
1326 REGEX_REALLOCATE_STACK requires `destination' be declared. */
1327
1328 /* Factor to increase the failure stack size by
1329 when we increase it.
1330 This used to be 2, but 2 was too wasteful
1331 because the old discarded stacks added up to as much space
1332 were as ultimate, maximum-size stack. */
1333 #define FAIL_STACK_GROWTH_FACTOR 4
1334
1335 #define GROW_FAIL_STACK(fail_stack) \
1336 (((fail_stack).size * sizeof (fail_stack_elt_t) \
1337 >= re_max_failures * TYPICAL_FAILURE_SIZE) \
1338 ? 0 \
1339 : ((fail_stack).stack \
1340 = (fail_stack_elt_t *) \
1341 REGEX_REALLOCATE_STACK ((fail_stack).stack, \
1342 (fail_stack).size * sizeof (fail_stack_elt_t), \
1343 MIN (re_max_failures * TYPICAL_FAILURE_SIZE, \
1344 ((fail_stack).size * sizeof (fail_stack_elt_t) \
1345 * FAIL_STACK_GROWTH_FACTOR))), \
1346 \
1347 (fail_stack).stack == NULL \
1348 ? 0 \
1349 : ((fail_stack).size \
1350 = (MIN (re_max_failures * TYPICAL_FAILURE_SIZE, \
1351 ((fail_stack).size * sizeof (fail_stack_elt_t) \
1352 * FAIL_STACK_GROWTH_FACTOR)) \
1353 / sizeof (fail_stack_elt_t)), \
1354 1)))
1355
1356
1357 /* Push pointer POINTER on FAIL_STACK.
1358 Return 1 if was able to do so and 0 if ran out of memory allocating
1359 space to do so. */
1360 #define PUSH_PATTERN_OP(POINTER, FAIL_STACK) \
1361 ((FAIL_STACK_FULL () \
1362 && !GROW_FAIL_STACK (FAIL_STACK)) \
1363 ? 0 \
1364 : ((FAIL_STACK).stack[(FAIL_STACK).avail++].pointer = POINTER, \
1365 1))
1366 #define POP_PATTERN_OP() POP_FAILURE_POINTER ()
1367
1368 /* Push a pointer value onto the failure stack.
1369 Assumes the variable `fail_stack'. Probably should only
1370 be called from within `PUSH_FAILURE_POINT'. */
1371 #define PUSH_FAILURE_POINTER(item) \
1372 fail_stack.stack[fail_stack.avail++].pointer = (unsigned char *) (item)
1373
1374 /* This pushes an integer-valued item onto the failure stack.
1375 Assumes the variable `fail_stack'. Probably should only
1376 be called from within `PUSH_FAILURE_POINT'. */
1377 #define PUSH_FAILURE_INT(item) \
1378 fail_stack.stack[fail_stack.avail++].integer = (item)
1379
1380 /* Push a fail_stack_elt_t value onto the failure stack.
1381 Assumes the variable `fail_stack'. Probably should only
1382 be called from within `PUSH_FAILURE_POINT'. */
1383 #define PUSH_FAILURE_ELT(item) \
1384 fail_stack.stack[fail_stack.avail++] = (item)
1385
1386 /* These three POP... operations complement the three PUSH... operations.
1387 All assume that `fail_stack' is nonempty. */
1388 #define POP_FAILURE_POINTER() fail_stack.stack[--fail_stack.avail].pointer
1389 #define POP_FAILURE_INT() fail_stack.stack[--fail_stack.avail].integer
1390 #define POP_FAILURE_ELT() fail_stack.stack[--fail_stack.avail]
1391
1392 /* Individual items aside from the registers. */
1393 #define NUM_NONREG_ITEMS 3
1394
1395 /* Used to examine the stack (to detect infinite loops). */
1396 #define FAILURE_PAT(h) fail_stack.stack[(h) - 1].pointer
1397 #define FAILURE_STR(h) (fail_stack.stack[(h) - 2].pointer)
1398 #define NEXT_FAILURE_HANDLE(h) fail_stack.stack[(h) - 3].integer
1399 #define TOP_FAILURE_HANDLE() fail_stack.frame
1400
1401
1402 #define ENSURE_FAIL_STACK(space) \
1403 while (REMAINING_AVAIL_SLOTS <= space) { \
1404 if (!GROW_FAIL_STACK (fail_stack)) \
1405 return -2; \
1406 DEBUG_PRINT2 ("\n Doubled stack; size now: %d\n", (fail_stack).size);\
1407 DEBUG_PRINT2 (" slots available: %d\n", REMAINING_AVAIL_SLOTS);\
1408 }
1409
1410 /* Push register NUM onto the stack. */
1411 #define PUSH_FAILURE_REG(num) \
1412 do { \
1413 char *destination; \
1414 ENSURE_FAIL_STACK(3); \
1415 DEBUG_PRINT4 (" Push reg %d (spanning %p -> %p)\n", \
1416 num, regstart[num], regend[num]); \
1417 PUSH_FAILURE_POINTER (regstart[num]); \
1418 PUSH_FAILURE_POINTER (regend[num]); \
1419 PUSH_FAILURE_INT (num); \
1420 } while (0)
1421
1422 #define PUSH_FAILURE_COUNT(ptr) \
1423 do { \
1424 char *destination; \
1425 int c; \
1426 ENSURE_FAIL_STACK(3); \
1427 EXTRACT_NUMBER (c, ptr); \
1428 DEBUG_PRINT3 (" Push counter %p = %d\n", ptr, c); \
1429 PUSH_FAILURE_INT (c); \
1430 PUSH_FAILURE_POINTER (ptr); \
1431 PUSH_FAILURE_INT (-1); \
1432 } while (0)
1433
1434 /* Pop a saved register off the stack. */
1435 #define POP_FAILURE_REG_OR_COUNT() \
1436 do { \
1437 int reg = POP_FAILURE_INT (); \
1438 if (reg == -1) \
1439 { \
1440 /* It's a counter. */ \
1441 unsigned char *ptr = (unsigned char*) POP_FAILURE_POINTER (); \
1442 reg = POP_FAILURE_INT (); \
1443 STORE_NUMBER (ptr, reg); \
1444 DEBUG_PRINT3 (" Pop counter %p = %d\n", ptr, reg); \
1445 } \
1446 else \
1447 { \
1448 regend[reg] = POP_FAILURE_POINTER (); \
1449 regstart[reg] = POP_FAILURE_POINTER (); \
1450 DEBUG_PRINT4 (" Pop reg %d (spanning %p -> %p)\n", \
1451 reg, regstart[reg], regend[reg]); \
1452 } \
1453 } while (0)
1454
1455 /* Check that we are not stuck in an infinite loop. */
1456 #define CHECK_INFINITE_LOOP(pat_cur, string_place) \
1457 do { \
1458 int failure = TOP_FAILURE_HANDLE(); \
1459 /* Check for infinite matching loops */ \
1460 while (failure > 0 && \
1461 (FAILURE_STR (failure) == string_place \
1462 || FAILURE_STR (failure) == NULL)) \
1463 { \
1464 assert (FAILURE_PAT (failure) >= bufp->buffer \
1465 && FAILURE_PAT (failure) <= bufp->buffer + bufp->used); \
1466 if (FAILURE_PAT (failure) == pat_cur) \
1467 goto fail; \
1468 DEBUG_PRINT2 (" Other pattern: %p\n", FAILURE_PAT (failure)); \
1469 failure = NEXT_FAILURE_HANDLE(failure); \
1470 } \
1471 DEBUG_PRINT2 (" Other string: %p\n", FAILURE_STR (failure)); \
1472 } while (0)
1473
1474 /* Push the information about the state we will need
1475 if we ever fail back to it.
1476
1477 Requires variables fail_stack, regstart, regend and
1478 num_regs be declared. GROW_FAIL_STACK requires `destination' be
1479 declared.
1480
1481 Does `return FAILURE_CODE' if runs out of memory. */
1482
1483 #define PUSH_FAILURE_POINT(pattern, string_place) \
1484 do { \
1485 char *destination; \
1486 /* Must be int, so when we don't save any registers, the arithmetic \
1487 of 0 + -1 isn't done as unsigned. */ \
1488 \
1489 DEBUG_STATEMENT (failure_id++); \
1490 DEBUG_STATEMENT (nfailure_points_pushed++); \
1491 DEBUG_PRINT2 ("\nPUSH_FAILURE_POINT #%u:\n", failure_id); \
1492 DEBUG_PRINT2 (" Before push, next avail: %d\n", (fail_stack).avail); \
1493 DEBUG_PRINT2 (" size: %d\n", (fail_stack).size);\
1494 \
1495 ENSURE_FAIL_STACK (NUM_NONREG_ITEMS); \
1496 \
1497 DEBUG_PRINT1 ("\n"); \
1498 \
1499 DEBUG_PRINT2 (" Push frame index: %d\n", fail_stack.frame); \
1500 PUSH_FAILURE_INT (fail_stack.frame); \
1501 \
1502 DEBUG_PRINT2 (" Push string %p: `", string_place); \
1503 DEBUG_PRINT_DOUBLE_STRING (string_place, string1, size1, string2, size2);\
1504 DEBUG_PRINT1 ("'\n"); \
1505 PUSH_FAILURE_POINTER (string_place); \
1506 \
1507 DEBUG_PRINT2 (" Push pattern %p: ", pattern); \
1508 DEBUG_PRINT_COMPILED_PATTERN (bufp, pattern, pend); \
1509 PUSH_FAILURE_POINTER (pattern); \
1510 \
1511 /* Close the frame by moving the frame pointer past it. */ \
1512 fail_stack.frame = fail_stack.avail; \
1513 } while (0)
1514
1515 /* Estimate the size of data pushed by a typical failure stack entry.
1516 An estimate is all we need, because all we use this for
1517 is to choose a limit for how big to make the failure stack. */
1518
1519 #define TYPICAL_FAILURE_SIZE 20
1520
1521 /* How many items can still be added to the stack without overflowing it. */
1522 #define REMAINING_AVAIL_SLOTS ((fail_stack).size - (fail_stack).avail)
1523
1524
1525 /* Pops what PUSH_FAIL_STACK pushes.
1526
1527 We restore into the parameters, all of which should be lvalues:
1528 STR -- the saved data position.
1529 PAT -- the saved pattern position.
1530 REGSTART, REGEND -- arrays of string positions.
1531
1532 Also assumes the variables `fail_stack' and (if debugging), `bufp',
1533 `pend', `string1', `size1', `string2', and `size2'. */
1534
1535 #define POP_FAILURE_POINT(str, pat) \
1536 do { \
1537 assert (!FAIL_STACK_EMPTY ()); \
1538 \
1539 /* Remove failure points and point to how many regs pushed. */ \
1540 DEBUG_PRINT1 ("POP_FAILURE_POINT:\n"); \
1541 DEBUG_PRINT2 (" Before pop, next avail: %d\n", fail_stack.avail); \
1542 DEBUG_PRINT2 (" size: %d\n", fail_stack.size); \
1543 \
1544 /* Pop the saved registers. */ \
1545 while (fail_stack.frame < fail_stack.avail) \
1546 POP_FAILURE_REG_OR_COUNT (); \
1547 \
1548 pat = (unsigned char *) POP_FAILURE_POINTER (); \
1549 DEBUG_PRINT2 (" Popping pattern %p: ", pat); \
1550 DEBUG_PRINT_COMPILED_PATTERN (bufp, pat, pend); \
1551 \
1552 /* If the saved string location is NULL, it came from an \
1553 on_failure_keep_string_jump opcode, and we want to throw away the \
1554 saved NULL, thus retaining our current position in the string. */ \
1555 str = (re_char *) POP_FAILURE_POINTER (); \
1556 DEBUG_PRINT2 (" Popping string %p: `", str); \
1557 DEBUG_PRINT_DOUBLE_STRING (str, string1, size1, string2, size2); \
1558 DEBUG_PRINT1 ("'\n"); \
1559 \
1560 fail_stack.frame = POP_FAILURE_INT (); \
1561 DEBUG_PRINT2 (" Popping frame index: %d\n", fail_stack.frame); \
1562 \
1563 assert (fail_stack.avail >= 0); \
1564 assert (fail_stack.frame <= fail_stack.avail); \
1565 \
1566 DEBUG_STATEMENT (nfailure_points_popped++); \
1567 } while (0) /* POP_FAILURE_POINT */
1568
1569
1570 \f
1571 /* Registers are set to a sentinel when they haven't yet matched. */
1572 #define REG_UNSET_VALUE NULL
1573 #define REG_UNSET(e) ((e) == REG_UNSET_VALUE)
1574 \f
1575 /* Subroutine declarations and macros for regex_compile. */
1576
1577 static void store_op1 _RE_ARGS((re_opcode_t op, unsigned char *loc, int arg));
1578 static void store_op2 _RE_ARGS((re_opcode_t op, unsigned char *loc,
1579 int arg1, int arg2));
1580 static void insert_op1 _RE_ARGS((re_opcode_t op, unsigned char *loc,
1581 int arg, unsigned char *end));
1582 static void insert_op2 _RE_ARGS((re_opcode_t op, unsigned char *loc,
1583 int arg1, int arg2, unsigned char *end));
1584 static boolean at_begline_loc_p _RE_ARGS((const unsigned char *pattern,
1585 const unsigned char *p,
1586 reg_syntax_t syntax));
1587 static boolean at_endline_loc_p _RE_ARGS((const unsigned char *p,
1588 const unsigned char *pend,
1589 reg_syntax_t syntax));
1590 static unsigned char *skip_one_char _RE_ARGS((unsigned char *p));
1591 static int analyse_first _RE_ARGS((unsigned char *p, unsigned char *pend,
1592 char *fastmap, const int multibyte));
1593
1594 /* Fetch the next character in the uncompiled pattern---translating it
1595 if necessary. Also cast from a signed character in the constant
1596 string passed to us by the user to an unsigned char that we can use
1597 as an array index (in, e.g., `translate'). */
1598 #define PATFETCH(c) \
1599 do { \
1600 PATFETCH_RAW (c); \
1601 c = TRANSLATE (c); \
1602 } while (0)
1603
1604 /* Fetch the next character in the uncompiled pattern, with no
1605 translation. */
1606 #define PATFETCH_RAW(c) \
1607 do { \
1608 int len; \
1609 if (p == pend) return REG_EEND; \
1610 c = RE_STRING_CHAR_AND_LENGTH (p, pend - p, len); \
1611 p += len; \
1612 } while (0)
1613
1614
1615 /* If `translate' is non-null, return translate[D], else just D. We
1616 cast the subscript to translate because some data is declared as
1617 `char *', to avoid warnings when a string constant is passed. But
1618 when we use a character as a subscript we must make it unsigned. */
1619 #ifndef TRANSLATE
1620 #define TRANSLATE(d) \
1621 (RE_TRANSLATE_P (translate) ? RE_TRANSLATE (translate, (d)) : (d))
1622 #endif
1623
1624
1625 /* Macros for outputting the compiled pattern into `buffer'. */
1626
1627 /* If the buffer isn't allocated when it comes in, use this. */
1628 #define INIT_BUF_SIZE 32
1629
1630 /* Make sure we have at least N more bytes of space in buffer. */
1631 #define GET_BUFFER_SPACE(n) \
1632 while (b - bufp->buffer + (n) > bufp->allocated) \
1633 EXTEND_BUFFER ()
1634
1635 /* Make sure we have one more byte of buffer space and then add C to it. */
1636 #define BUF_PUSH(c) \
1637 do { \
1638 GET_BUFFER_SPACE (1); \
1639 *b++ = (unsigned char) (c); \
1640 } while (0)
1641
1642
1643 /* Ensure we have two more bytes of buffer space and then append C1 and C2. */
1644 #define BUF_PUSH_2(c1, c2) \
1645 do { \
1646 GET_BUFFER_SPACE (2); \
1647 *b++ = (unsigned char) (c1); \
1648 *b++ = (unsigned char) (c2); \
1649 } while (0)
1650
1651
1652 /* As with BUF_PUSH_2, except for three bytes. */
1653 #define BUF_PUSH_3(c1, c2, c3) \
1654 do { \
1655 GET_BUFFER_SPACE (3); \
1656 *b++ = (unsigned char) (c1); \
1657 *b++ = (unsigned char) (c2); \
1658 *b++ = (unsigned char) (c3); \
1659 } while (0)
1660
1661
1662 /* Store a jump with opcode OP at LOC to location TO. We store a
1663 relative address offset by the three bytes the jump itself occupies. */
1664 #define STORE_JUMP(op, loc, to) \
1665 store_op1 (op, loc, (to) - (loc) - 3)
1666
1667 /* Likewise, for a two-argument jump. */
1668 #define STORE_JUMP2(op, loc, to, arg) \
1669 store_op2 (op, loc, (to) - (loc) - 3, arg)
1670
1671 /* Like `STORE_JUMP', but for inserting. Assume `b' is the buffer end. */
1672 #define INSERT_JUMP(op, loc, to) \
1673 insert_op1 (op, loc, (to) - (loc) - 3, b)
1674
1675 /* Like `STORE_JUMP2', but for inserting. Assume `b' is the buffer end. */
1676 #define INSERT_JUMP2(op, loc, to, arg) \
1677 insert_op2 (op, loc, (to) - (loc) - 3, arg, b)
1678
1679
1680 /* This is not an arbitrary limit: the arguments which represent offsets
1681 into the pattern are two bytes long. So if 2^16 bytes turns out to
1682 be too small, many things would have to change. */
1683 #define MAX_BUF_SIZE (1L << 16)
1684
1685
1686 /* Extend the buffer by twice its current size via realloc and
1687 reset the pointers that pointed into the old block to point to the
1688 correct places in the new one. If extending the buffer results in it
1689 being larger than MAX_BUF_SIZE, then flag memory exhausted. */
1690 #define EXTEND_BUFFER() \
1691 do { \
1692 unsigned char *old_buffer = bufp->buffer; \
1693 if (bufp->allocated == MAX_BUF_SIZE) \
1694 return REG_ESIZE; \
1695 bufp->allocated <<= 1; \
1696 if (bufp->allocated > MAX_BUF_SIZE) \
1697 bufp->allocated = MAX_BUF_SIZE; \
1698 bufp->buffer = (unsigned char *) realloc (bufp->buffer, bufp->allocated);\
1699 if (bufp->buffer == NULL) \
1700 return REG_ESPACE; \
1701 /* If the buffer moved, move all the pointers into it. */ \
1702 if (old_buffer != bufp->buffer) \
1703 { \
1704 b = (b - old_buffer) + bufp->buffer; \
1705 begalt = (begalt - old_buffer) + bufp->buffer; \
1706 if (fixup_alt_jump) \
1707 fixup_alt_jump = (fixup_alt_jump - old_buffer) + bufp->buffer;\
1708 if (laststart) \
1709 laststart = (laststart - old_buffer) + bufp->buffer; \
1710 if (pending_exact) \
1711 pending_exact = (pending_exact - old_buffer) + bufp->buffer; \
1712 } \
1713 } while (0)
1714
1715
1716 /* Since we have one byte reserved for the register number argument to
1717 {start,stop}_memory, the maximum number of groups we can report
1718 things about is what fits in that byte. */
1719 #define MAX_REGNUM 255
1720
1721 /* But patterns can have more than `MAX_REGNUM' registers. We just
1722 ignore the excess. */
1723 typedef unsigned regnum_t;
1724
1725
1726 /* Macros for the compile stack. */
1727
1728 /* Since offsets can go either forwards or backwards, this type needs to
1729 be able to hold values from -(MAX_BUF_SIZE - 1) to MAX_BUF_SIZE - 1. */
1730 typedef int pattern_offset_t;
1731
1732 typedef struct
1733 {
1734 pattern_offset_t begalt_offset;
1735 pattern_offset_t fixup_alt_jump;
1736 pattern_offset_t laststart_offset;
1737 regnum_t regnum;
1738 } compile_stack_elt_t;
1739
1740
1741 typedef struct
1742 {
1743 compile_stack_elt_t *stack;
1744 unsigned size;
1745 unsigned avail; /* Offset of next open position. */
1746 } compile_stack_type;
1747
1748
1749 #define INIT_COMPILE_STACK_SIZE 32
1750
1751 #define COMPILE_STACK_EMPTY (compile_stack.avail == 0)
1752 #define COMPILE_STACK_FULL (compile_stack.avail == compile_stack.size)
1753
1754 /* The next available element. */
1755 #define COMPILE_STACK_TOP (compile_stack.stack[compile_stack.avail])
1756
1757
1758 /* Structure to manage work area for range table. */
1759 struct range_table_work_area
1760 {
1761 int *table; /* actual work area. */
1762 int allocated; /* allocated size for work area in bytes. */
1763 int used; /* actually used size in words. */
1764 int bits; /* flag to record character classes */
1765 };
1766
1767 /* Make sure that WORK_AREA can hold more N multibyte characters. */
1768 #define EXTEND_RANGE_TABLE_WORK_AREA(work_area, n) \
1769 do { \
1770 if (((work_area).used + (n)) * sizeof (int) > (work_area).allocated) \
1771 { \
1772 (work_area).allocated += 16 * sizeof (int); \
1773 if ((work_area).table) \
1774 (work_area).table \
1775 = (int *) realloc ((work_area).table, (work_area).allocated); \
1776 else \
1777 (work_area).table \
1778 = (int *) malloc ((work_area).allocated); \
1779 if ((work_area).table == 0) \
1780 FREE_STACK_RETURN (REG_ESPACE); \
1781 } \
1782 } while (0)
1783
1784 #define SET_RANGE_TABLE_WORK_AREA_BIT(work_area, bit) \
1785 (work_area).bits |= (bit)
1786
1787 /* These bits represent the various character classes such as [:alnum:]
1788 in a charset's range table. */
1789 #define BIT_ALNUM 0x1
1790 #define BIT_ALPHA 0x2
1791 #define BIT_WORD 0x4
1792 #define BIT_ASCII 0x8
1793 #define BIT_NONASCII 0x10
1794 #define BIT_GRAPH 0x20
1795 #define BIT_LOWER 0x40
1796 #define BIT_PRINT 0x80
1797 #define BIT_PUNCT 0x100
1798 #define BIT_SPACE 0x200
1799 #define BIT_UPPER 0x400
1800 #define BIT_UNIBYTE 0x800
1801 #define BIT_MULTIBYTE 0x1000
1802
1803 /* Set a range (RANGE_START, RANGE_END) to WORK_AREA. */
1804 #define SET_RANGE_TABLE_WORK_AREA(work_area, range_start, range_end) \
1805 do { \
1806 EXTEND_RANGE_TABLE_WORK_AREA ((work_area), 2); \
1807 (work_area).table[(work_area).used++] = (range_start); \
1808 (work_area).table[(work_area).used++] = (range_end); \
1809 } while (0)
1810
1811 /* Free allocated memory for WORK_AREA. */
1812 #define FREE_RANGE_TABLE_WORK_AREA(work_area) \
1813 do { \
1814 if ((work_area).table) \
1815 free ((work_area).table); \
1816 } while (0)
1817
1818 #define CLEAR_RANGE_TABLE_WORK_USED(work_area) ((work_area).used = 0, (work_area).bits = 0)
1819 #define RANGE_TABLE_WORK_USED(work_area) ((work_area).used)
1820 #define RANGE_TABLE_WORK_BITS(work_area) ((work_area).bits)
1821 #define RANGE_TABLE_WORK_ELT(work_area, i) ((work_area).table[i])
1822
1823
1824 /* Set the bit for character C in a list. */
1825 #define SET_LIST_BIT(c) \
1826 (b[((unsigned char) (c)) / BYTEWIDTH] \
1827 |= 1 << (((unsigned char) c) % BYTEWIDTH))
1828
1829
1830 /* Get the next unsigned number in the uncompiled pattern. */
1831 #define GET_UNSIGNED_NUMBER(num) \
1832 do { if (p != pend) \
1833 { \
1834 PATFETCH (c); \
1835 while (ISDIGIT (c)) \
1836 { \
1837 if (num < 0) \
1838 num = 0; \
1839 num = num * 10 + c - '0'; \
1840 if (p == pend) \
1841 break; \
1842 PATFETCH (c); \
1843 } \
1844 } \
1845 } while (0)
1846
1847 #define CHAR_CLASS_MAX_LENGTH 6 /* Namely, `xdigit'. */
1848
1849 #define IS_CHAR_CLASS(string) \
1850 (STREQ (string, "alpha") || STREQ (string, "upper") \
1851 || STREQ (string, "lower") || STREQ (string, "digit") \
1852 || STREQ (string, "alnum") || STREQ (string, "xdigit") \
1853 || STREQ (string, "space") || STREQ (string, "print") \
1854 || STREQ (string, "punct") || STREQ (string, "graph") \
1855 || STREQ (string, "cntrl") || STREQ (string, "blank") \
1856 || STREQ (string, "word") \
1857 || STREQ (string, "ascii") || STREQ (string, "nonascii") \
1858 || STREQ (string, "unibyte") || STREQ (string, "multibyte"))
1859
1860 /* QUIT is only used on NTemacs. */
1861 #if !defined (WINDOWSNT) || !defined (emacs)
1862 #undef QUIT
1863 #define QUIT
1864 #endif
1865 \f
1866 #ifndef MATCH_MAY_ALLOCATE
1867
1868 /* If we cannot allocate large objects within re_match_2_internal,
1869 we make the fail stack and register vectors global.
1870 The fail stack, we grow to the maximum size when a regexp
1871 is compiled.
1872 The register vectors, we adjust in size each time we
1873 compile a regexp, according to the number of registers it needs. */
1874
1875 static fail_stack_type fail_stack;
1876
1877 /* Size with which the following vectors are currently allocated.
1878 That is so we can make them bigger as needed,
1879 but never make them smaller. */
1880 static int regs_allocated_size;
1881
1882 static re_char ** regstart, ** regend;
1883 static re_char **best_regstart, **best_regend;
1884
1885 /* Make the register vectors big enough for NUM_REGS registers,
1886 but don't make them smaller. */
1887
1888 static
1889 regex_grow_registers (num_regs)
1890 int num_regs;
1891 {
1892 if (num_regs > regs_allocated_size)
1893 {
1894 RETALLOC_IF (regstart, num_regs, re_char *);
1895 RETALLOC_IF (regend, num_regs, re_char *);
1896 RETALLOC_IF (best_regstart, num_regs, re_char *);
1897 RETALLOC_IF (best_regend, num_regs, re_char *);
1898
1899 regs_allocated_size = num_regs;
1900 }
1901 }
1902
1903 #endif /* not MATCH_MAY_ALLOCATE */
1904 \f
1905 static boolean group_in_compile_stack _RE_ARGS ((compile_stack_type
1906 compile_stack,
1907 regnum_t regnum));
1908
1909 /* `regex_compile' compiles PATTERN (of length SIZE) according to SYNTAX.
1910 Returns one of error codes defined in `regex.h', or zero for success.
1911
1912 Assumes the `allocated' (and perhaps `buffer') and `translate'
1913 fields are set in BUFP on entry.
1914
1915 If it succeeds, results are put in BUFP (if it returns an error, the
1916 contents of BUFP are undefined):
1917 `buffer' is the compiled pattern;
1918 `syntax' is set to SYNTAX;
1919 `used' is set to the length of the compiled pattern;
1920 `fastmap_accurate' is zero;
1921 `re_nsub' is the number of subexpressions in PATTERN;
1922 `not_bol' and `not_eol' are zero;
1923
1924 The `fastmap' and `newline_anchor' fields are neither
1925 examined nor set. */
1926
1927 /* Insert the `jump' from the end of last alternative to "here".
1928 The space for the jump has already been allocated. */
1929 #define FIXUP_ALT_JUMP() \
1930 do { \
1931 if (fixup_alt_jump) \
1932 STORE_JUMP (jump, fixup_alt_jump, b); \
1933 } while (0)
1934
1935
1936 /* Return, freeing storage we allocated. */
1937 #define FREE_STACK_RETURN(value) \
1938 do { \
1939 FREE_RANGE_TABLE_WORK_AREA (range_table_work); \
1940 free (compile_stack.stack); \
1941 return value; \
1942 } while (0)
1943
1944 static reg_errcode_t
1945 regex_compile (pattern, size, syntax, bufp)
1946 re_char *pattern;
1947 int size;
1948 reg_syntax_t syntax;
1949 struct re_pattern_buffer *bufp;
1950 {
1951 /* We fetch characters from PATTERN here. Even though PATTERN is
1952 `char *' (i.e., signed), we declare these variables as unsigned, so
1953 they can be reliably used as array indices. */
1954 register unsigned int c, c1;
1955
1956 /* A random temporary spot in PATTERN. */
1957 re_char *p1;
1958
1959 /* Points to the end of the buffer, where we should append. */
1960 register unsigned char *b;
1961
1962 /* Keeps track of unclosed groups. */
1963 compile_stack_type compile_stack;
1964
1965 /* Points to the current (ending) position in the pattern. */
1966 #ifdef AIX
1967 /* `const' makes AIX compiler fail. */
1968 unsigned char *p = pattern;
1969 #else
1970 re_char *p = pattern;
1971 #endif
1972 re_char *pend = pattern + size;
1973
1974 /* How to translate the characters in the pattern. */
1975 RE_TRANSLATE_TYPE translate = bufp->translate;
1976
1977 /* Address of the count-byte of the most recently inserted `exactn'
1978 command. This makes it possible to tell if a new exact-match
1979 character can be added to that command or if the character requires
1980 a new `exactn' command. */
1981 unsigned char *pending_exact = 0;
1982
1983 /* Address of start of the most recently finished expression.
1984 This tells, e.g., postfix * where to find the start of its
1985 operand. Reset at the beginning of groups and alternatives. */
1986 unsigned char *laststart = 0;
1987
1988 /* Address of beginning of regexp, or inside of last group. */
1989 unsigned char *begalt;
1990
1991 /* Place in the uncompiled pattern (i.e., the {) to
1992 which to go back if the interval is invalid. */
1993 re_char *beg_interval;
1994
1995 /* Address of the place where a forward jump should go to the end of
1996 the containing expression. Each alternative of an `or' -- except the
1997 last -- ends with a forward jump of this sort. */
1998 unsigned char *fixup_alt_jump = 0;
1999
2000 /* Counts open-groups as they are encountered. Remembered for the
2001 matching close-group on the compile stack, so the same register
2002 number is put in the stop_memory as the start_memory. */
2003 regnum_t regnum = 0;
2004
2005 /* Work area for range table of charset. */
2006 struct range_table_work_area range_table_work;
2007
2008 /* If the object matched can contain multibyte characters. */
2009 const boolean multibyte = RE_MULTIBYTE_P (bufp);
2010
2011 #ifdef DEBUG
2012 debug++;
2013 DEBUG_PRINT1 ("\nCompiling pattern: ");
2014 if (debug > 0)
2015 {
2016 unsigned debug_count;
2017
2018 for (debug_count = 0; debug_count < size; debug_count++)
2019 putchar (pattern[debug_count]);
2020 putchar ('\n');
2021 }
2022 #endif /* DEBUG */
2023
2024 /* Initialize the compile stack. */
2025 compile_stack.stack = TALLOC (INIT_COMPILE_STACK_SIZE, compile_stack_elt_t);
2026 if (compile_stack.stack == NULL)
2027 return REG_ESPACE;
2028
2029 compile_stack.size = INIT_COMPILE_STACK_SIZE;
2030 compile_stack.avail = 0;
2031
2032 range_table_work.table = 0;
2033 range_table_work.allocated = 0;
2034
2035 /* Initialize the pattern buffer. */
2036 bufp->syntax = syntax;
2037 bufp->fastmap_accurate = 0;
2038 bufp->not_bol = bufp->not_eol = 0;
2039
2040 /* Set `used' to zero, so that if we return an error, the pattern
2041 printer (for debugging) will think there's no pattern. We reset it
2042 at the end. */
2043 bufp->used = 0;
2044
2045 /* Always count groups, whether or not bufp->no_sub is set. */
2046 bufp->re_nsub = 0;
2047
2048 #if !defined (emacs) && !defined (SYNTAX_TABLE)
2049 /* Initialize the syntax table. */
2050 init_syntax_once ();
2051 #endif
2052
2053 if (bufp->allocated == 0)
2054 {
2055 if (bufp->buffer)
2056 { /* If zero allocated, but buffer is non-null, try to realloc
2057 enough space. This loses if buffer's address is bogus, but
2058 that is the user's responsibility. */
2059 RETALLOC (bufp->buffer, INIT_BUF_SIZE, unsigned char);
2060 }
2061 else
2062 { /* Caller did not allocate a buffer. Do it for them. */
2063 bufp->buffer = TALLOC (INIT_BUF_SIZE, unsigned char);
2064 }
2065 if (!bufp->buffer) FREE_STACK_RETURN (REG_ESPACE);
2066
2067 bufp->allocated = INIT_BUF_SIZE;
2068 }
2069
2070 begalt = b = bufp->buffer;
2071
2072 /* Loop through the uncompiled pattern until we're at the end. */
2073 while (p != pend)
2074 {
2075 PATFETCH (c);
2076
2077 switch (c)
2078 {
2079 case '^':
2080 {
2081 if ( /* If at start of pattern, it's an operator. */
2082 p == pattern + 1
2083 /* If context independent, it's an operator. */
2084 || syntax & RE_CONTEXT_INDEP_ANCHORS
2085 /* Otherwise, depends on what's come before. */
2086 || at_begline_loc_p (pattern, p, syntax))
2087 BUF_PUSH (begline);
2088 else
2089 goto normal_char;
2090 }
2091 break;
2092
2093
2094 case '$':
2095 {
2096 if ( /* If at end of pattern, it's an operator. */
2097 p == pend
2098 /* If context independent, it's an operator. */
2099 || syntax & RE_CONTEXT_INDEP_ANCHORS
2100 /* Otherwise, depends on what's next. */
2101 || at_endline_loc_p (p, pend, syntax))
2102 BUF_PUSH (endline);
2103 else
2104 goto normal_char;
2105 }
2106 break;
2107
2108
2109 case '+':
2110 case '?':
2111 if ((syntax & RE_BK_PLUS_QM)
2112 || (syntax & RE_LIMITED_OPS))
2113 goto normal_char;
2114 handle_plus:
2115 case '*':
2116 /* If there is no previous pattern... */
2117 if (!laststart)
2118 {
2119 if (syntax & RE_CONTEXT_INVALID_OPS)
2120 FREE_STACK_RETURN (REG_BADRPT);
2121 else if (!(syntax & RE_CONTEXT_INDEP_OPS))
2122 goto normal_char;
2123 }
2124
2125 {
2126 /* 1 means zero (many) matches is allowed. */
2127 boolean zero_times_ok = 0, many_times_ok = 0;
2128 boolean greedy = 1;
2129
2130 /* If there is a sequence of repetition chars, collapse it
2131 down to just one (the right one). We can't combine
2132 interval operators with these because of, e.g., `a{2}*',
2133 which should only match an even number of `a's. */
2134
2135 for (;;)
2136 {
2137 if (!(syntax & RE_ALL_GREEDY)
2138 && c == '?' && (zero_times_ok || many_times_ok))
2139 greedy = 0;
2140 else
2141 {
2142 zero_times_ok |= c != '+';
2143 many_times_ok |= c != '?';
2144 }
2145
2146 if (p == pend)
2147 break;
2148 else if (*p == '*'
2149 || (!(syntax & RE_BK_PLUS_QM)
2150 && (*p == '+' || *p == '?')))
2151 ;
2152 else if (syntax & RE_BK_PLUS_QM && *p == '\\')
2153 {
2154 if (p+1 == pend)
2155 FREE_STACK_RETURN (REG_EESCAPE);
2156 if (p[1] == '+' || p[1] == '?')
2157 PATFETCH (c); /* Gobble up the backslash. */
2158 else
2159 break;
2160 }
2161 else
2162 break;
2163 /* If we get here, we found another repeat character. */
2164 PATFETCH (c);
2165 }
2166
2167 /* Star, etc. applied to an empty pattern is equivalent
2168 to an empty pattern. */
2169 if (!laststart || laststart == b)
2170 break;
2171
2172 /* Now we know whether or not zero matches is allowed
2173 and also whether or not two or more matches is allowed. */
2174 if (greedy)
2175 {
2176 if (many_times_ok)
2177 {
2178 boolean simple = skip_one_char (laststart) == b;
2179 unsigned int startoffset = 0;
2180 re_opcode_t ofj =
2181 (simple || !analyse_first (laststart, b, NULL, 0)) ?
2182 on_failure_jump : on_failure_jump_loop;
2183 assert (skip_one_char (laststart) <= b);
2184
2185 if (!zero_times_ok && simple)
2186 { /* Since simple * loops can be made faster by using
2187 on_failure_keep_string_jump, we turn simple P+
2188 into PP* if P is simple. */
2189 unsigned char *p1, *p2;
2190 startoffset = b - laststart;
2191 GET_BUFFER_SPACE (startoffset);
2192 p1 = b; p2 = laststart;
2193 while (p2 < p1)
2194 *b++ = *p2++;
2195 zero_times_ok = 1;
2196 }
2197
2198 GET_BUFFER_SPACE (6);
2199 if (!zero_times_ok)
2200 /* A + loop. */
2201 STORE_JUMP (ofj, b, b + 6);
2202 else
2203 /* Simple * loops can use on_failure_keep_string_jump
2204 depending on what follows. But since we don't know
2205 that yet, we leave the decision up to
2206 on_failure_jump_smart. */
2207 INSERT_JUMP (simple ? on_failure_jump_smart : ofj,
2208 laststart + startoffset, b + 6);
2209 b += 3;
2210 STORE_JUMP (jump, b, laststart + startoffset);
2211 b += 3;
2212 }
2213 else
2214 {
2215 /* A simple ? pattern. */
2216 assert (zero_times_ok);
2217 GET_BUFFER_SPACE (3);
2218 INSERT_JUMP (on_failure_jump, laststart, b + 3);
2219 b += 3;
2220 }
2221 }
2222 else /* not greedy */
2223 { /* I wish the greedy and non-greedy cases could be merged. */
2224
2225 GET_BUFFER_SPACE (7); /* We might use less. */
2226 if (many_times_ok)
2227 {
2228 boolean emptyp = analyse_first (laststart, b, NULL, 0);
2229
2230 /* The non-greedy multiple match looks like a repeat..until:
2231 we only need a conditional jump at the end of the loop */
2232 if (emptyp) BUF_PUSH (no_op);
2233 STORE_JUMP (emptyp ? on_failure_jump_nastyloop
2234 : on_failure_jump, b, laststart);
2235 b += 3;
2236 if (zero_times_ok)
2237 {
2238 /* The repeat...until naturally matches one or more.
2239 To also match zero times, we need to first jump to
2240 the end of the loop (its conditional jump). */
2241 INSERT_JUMP (jump, laststart, b);
2242 b += 3;
2243 }
2244 }
2245 else
2246 {
2247 /* non-greedy a?? */
2248 INSERT_JUMP (jump, laststart, b + 3);
2249 b += 3;
2250 INSERT_JUMP (on_failure_jump, laststart, laststart + 6);
2251 b += 3;
2252 }
2253 }
2254 }
2255 pending_exact = 0;
2256 break;
2257
2258
2259 case '.':
2260 laststart = b;
2261 BUF_PUSH (anychar);
2262 break;
2263
2264
2265 case '[':
2266 {
2267 CLEAR_RANGE_TABLE_WORK_USED (range_table_work);
2268
2269 if (p == pend) FREE_STACK_RETURN (REG_EBRACK);
2270
2271 /* Ensure that we have enough space to push a charset: the
2272 opcode, the length count, and the bitset; 34 bytes in all. */
2273 GET_BUFFER_SPACE (34);
2274
2275 laststart = b;
2276
2277 /* We test `*p == '^' twice, instead of using an if
2278 statement, so we only need one BUF_PUSH. */
2279 BUF_PUSH (*p == '^' ? charset_not : charset);
2280 if (*p == '^')
2281 p++;
2282
2283 /* Remember the first position in the bracket expression. */
2284 p1 = p;
2285
2286 /* Push the number of bytes in the bitmap. */
2287 BUF_PUSH ((1 << BYTEWIDTH) / BYTEWIDTH);
2288
2289 /* Clear the whole map. */
2290 bzero (b, (1 << BYTEWIDTH) / BYTEWIDTH);
2291
2292 /* charset_not matches newline according to a syntax bit. */
2293 if ((re_opcode_t) b[-2] == charset_not
2294 && (syntax & RE_HAT_LISTS_NOT_NEWLINE))
2295 SET_LIST_BIT ('\n');
2296
2297 /* Read in characters and ranges, setting map bits. */
2298 for (;;)
2299 {
2300 boolean escaped_char = false;
2301 const unsigned char *p2 = p;
2302
2303 if (p == pend) FREE_STACK_RETURN (REG_EBRACK);
2304
2305 PATFETCH (c);
2306
2307 /* \ might escape characters inside [...] and [^...]. */
2308 if ((syntax & RE_BACKSLASH_ESCAPE_IN_LISTS) && c == '\\')
2309 {
2310 if (p == pend) FREE_STACK_RETURN (REG_EESCAPE);
2311
2312 PATFETCH (c);
2313 escaped_char = true;
2314 }
2315 else
2316 {
2317 /* Could be the end of the bracket expression. If it's
2318 not (i.e., when the bracket expression is `[]' so
2319 far), the ']' character bit gets set way below. */
2320 if (c == ']' && p2 != p1)
2321 break;
2322 }
2323
2324 /* What should we do for the character which is
2325 greater than 0x7F, but not BASE_LEADING_CODE_P?
2326 XXX */
2327
2328 /* See if we're at the beginning of a possible character
2329 class. */
2330
2331 if (!escaped_char &&
2332 syntax & RE_CHAR_CLASSES && c == '[' && *p == ':')
2333 {
2334 /* Leave room for the null. */
2335 char str[CHAR_CLASS_MAX_LENGTH + 1];
2336 const unsigned char *class_beg;
2337
2338 PATFETCH (c);
2339 c1 = 0;
2340 class_beg = p;
2341
2342 /* If pattern is `[[:'. */
2343 if (p == pend) FREE_STACK_RETURN (REG_EBRACK);
2344
2345 for (;;)
2346 {
2347 PATFETCH (c);
2348 if (c == ':' || c == ']' || p == pend
2349 || c1 == CHAR_CLASS_MAX_LENGTH)
2350 break;
2351 str[c1++] = c;
2352 }
2353 str[c1] = '\0';
2354
2355 /* If isn't a word bracketed by `[:' and `:]':
2356 undo the ending character, the letters, and
2357 leave the leading `:' and `[' (but set bits for
2358 them). */
2359 if (c == ':' && *p == ']')
2360 {
2361 int ch;
2362 boolean is_alnum = STREQ (str, "alnum");
2363 boolean is_alpha = STREQ (str, "alpha");
2364 boolean is_ascii = STREQ (str, "ascii");
2365 boolean is_blank = STREQ (str, "blank");
2366 boolean is_cntrl = STREQ (str, "cntrl");
2367 boolean is_digit = STREQ (str, "digit");
2368 boolean is_graph = STREQ (str, "graph");
2369 boolean is_lower = STREQ (str, "lower");
2370 boolean is_multibyte = STREQ (str, "multibyte");
2371 boolean is_nonascii = STREQ (str, "nonascii");
2372 boolean is_print = STREQ (str, "print");
2373 boolean is_punct = STREQ (str, "punct");
2374 boolean is_space = STREQ (str, "space");
2375 boolean is_unibyte = STREQ (str, "unibyte");
2376 boolean is_upper = STREQ (str, "upper");
2377 boolean is_word = STREQ (str, "word");
2378 boolean is_xdigit = STREQ (str, "xdigit");
2379
2380 if (!IS_CHAR_CLASS (str))
2381 FREE_STACK_RETURN (REG_ECTYPE);
2382
2383 /* Throw away the ] at the end of the character
2384 class. */
2385 PATFETCH (c);
2386
2387 if (p == pend) FREE_STACK_RETURN (REG_EBRACK);
2388
2389 /* Most character classes in a multibyte match
2390 just set a flag. Exceptions are is_blank,
2391 is_digit, is_cntrl, and is_xdigit, since
2392 they can only match ASCII characters. We
2393 don't need to handle them for multibyte. */
2394
2395 if (multibyte)
2396 {
2397 int bit = 0;
2398
2399 if (is_alnum) bit = BIT_ALNUM;
2400 if (is_alpha) bit = BIT_ALPHA;
2401 if (is_ascii) bit = BIT_ASCII;
2402 if (is_graph) bit = BIT_GRAPH;
2403 if (is_lower) bit = BIT_LOWER;
2404 if (is_multibyte) bit = BIT_MULTIBYTE;
2405 if (is_nonascii) bit = BIT_NONASCII;
2406 if (is_print) bit = BIT_PRINT;
2407 if (is_punct) bit = BIT_PUNCT;
2408 if (is_space) bit = BIT_SPACE;
2409 if (is_unibyte) bit = BIT_UNIBYTE;
2410 if (is_upper) bit = BIT_UPPER;
2411 if (is_word) bit = BIT_WORD;
2412 if (bit)
2413 SET_RANGE_TABLE_WORK_AREA_BIT (range_table_work,
2414 bit);
2415 }
2416
2417 /* Handle character classes for ASCII characters. */
2418 for (ch = 0; ch < 1 << BYTEWIDTH; ch++)
2419 {
2420 int translated = TRANSLATE (ch);
2421 /* This was split into 3 if's to
2422 avoid an arbitrary limit in some compiler. */
2423 if ( (is_alnum && ISALNUM (ch))
2424 || (is_alpha && ISALPHA (ch))
2425 || (is_blank && ISBLANK (ch))
2426 || (is_cntrl && ISCNTRL (ch)))
2427 SET_LIST_BIT (translated);
2428 if ( (is_digit && ISDIGIT (ch))
2429 || (is_graph && ISGRAPH (ch))
2430 || (is_lower && ISLOWER (ch))
2431 || (is_print && ISPRINT (ch)))
2432 SET_LIST_BIT (translated);
2433 if ( (is_punct && ISPUNCT (ch))
2434 || (is_space && ISSPACE (ch))
2435 || (is_upper && ISUPPER (ch))
2436 || (is_xdigit && ISXDIGIT (ch)))
2437 SET_LIST_BIT (translated);
2438 if ( (is_ascii && IS_REAL_ASCII (ch))
2439 || (is_nonascii && !IS_REAL_ASCII (ch))
2440 || (is_unibyte && ISUNIBYTE (ch))
2441 || (is_multibyte && !ISUNIBYTE (ch)))
2442 SET_LIST_BIT (translated);
2443
2444 if ( (is_word && ISWORD (ch)))
2445 SET_LIST_BIT (translated);
2446 }
2447
2448 /* Repeat the loop. */
2449 continue;
2450 }
2451 else
2452 {
2453 /* Go back to right after the "[:". */
2454 p = class_beg;
2455 SET_LIST_BIT ('[');
2456
2457 /* Because the `:' may starts the range, we
2458 can't simply set bit and repeat the loop.
2459 Instead, just set it to C and handle below. */
2460 c = ':';
2461 }
2462 }
2463
2464 if (p < pend && p[0] == '-' && p[1] != ']')
2465 {
2466
2467 /* Discard the `-'. */
2468 PATFETCH (c1);
2469
2470 /* Fetch the character which ends the range. */
2471 PATFETCH (c1);
2472
2473 if (SINGLE_BYTE_CHAR_P (c))
2474 {
2475 if (! SINGLE_BYTE_CHAR_P (c1))
2476 {
2477 /* Handle a range such as \177-\377 in
2478 multibyte mode. Split that into two
2479 ranges, the low one ending at 0237, and
2480 the high one starting at the smallest
2481 character in the charset of C1 and
2482 ending at C1. */
2483 int charset = CHAR_CHARSET (c1);
2484 int c2 = MAKE_CHAR (charset, 0, 0);
2485
2486 SET_RANGE_TABLE_WORK_AREA (range_table_work,
2487 c2, c1);
2488 c1 = 0237;
2489 }
2490 }
2491 else if (!SAME_CHARSET_P (c, c1))
2492 FREE_STACK_RETURN (REG_ERANGE);
2493 }
2494 else
2495 /* Range from C to C. */
2496 c1 = c;
2497
2498 /* Set the range ... */
2499 if (SINGLE_BYTE_CHAR_P (c))
2500 /* ... into bitmap. */
2501 {
2502 unsigned this_char;
2503 int range_start = c, range_end = c1;
2504
2505 /* If the start is after the end, the range is empty. */
2506 if (range_start > range_end)
2507 {
2508 if (syntax & RE_NO_EMPTY_RANGES)
2509 FREE_STACK_RETURN (REG_ERANGE);
2510 /* Else, repeat the loop. */
2511 }
2512 else
2513 {
2514 for (this_char = range_start; this_char <= range_end;
2515 this_char++)
2516 SET_LIST_BIT (TRANSLATE (this_char));
2517 }
2518 }
2519 else
2520 /* ... into range table. */
2521 SET_RANGE_TABLE_WORK_AREA (range_table_work, c, c1);
2522 }
2523
2524 /* Discard any (non)matching list bytes that are all 0 at the
2525 end of the map. Decrease the map-length byte too. */
2526 while ((int) b[-1] > 0 && b[b[-1] - 1] == 0)
2527 b[-1]--;
2528 b += b[-1];
2529
2530 /* Build real range table from work area. */
2531 if (RANGE_TABLE_WORK_USED (range_table_work)
2532 || RANGE_TABLE_WORK_BITS (range_table_work))
2533 {
2534 int i;
2535 int used = RANGE_TABLE_WORK_USED (range_table_work);
2536
2537 /* Allocate space for COUNT + RANGE_TABLE. Needs two
2538 bytes for flags, two for COUNT, and three bytes for
2539 each character. */
2540 GET_BUFFER_SPACE (4 + used * 3);
2541
2542 /* Indicate the existence of range table. */
2543 laststart[1] |= 0x80;
2544
2545 /* Store the character class flag bits into the range table.
2546 If not in emacs, these flag bits are always 0. */
2547 *b++ = RANGE_TABLE_WORK_BITS (range_table_work) & 0xff;
2548 *b++ = RANGE_TABLE_WORK_BITS (range_table_work) >> 8;
2549
2550 STORE_NUMBER_AND_INCR (b, used / 2);
2551 for (i = 0; i < used; i++)
2552 STORE_CHARACTER_AND_INCR
2553 (b, RANGE_TABLE_WORK_ELT (range_table_work, i));
2554 }
2555 }
2556 break;
2557
2558
2559 case '(':
2560 if (syntax & RE_NO_BK_PARENS)
2561 goto handle_open;
2562 else
2563 goto normal_char;
2564
2565
2566 case ')':
2567 if (syntax & RE_NO_BK_PARENS)
2568 goto handle_close;
2569 else
2570 goto normal_char;
2571
2572
2573 case '\n':
2574 if (syntax & RE_NEWLINE_ALT)
2575 goto handle_alt;
2576 else
2577 goto normal_char;
2578
2579
2580 case '|':
2581 if (syntax & RE_NO_BK_VBAR)
2582 goto handle_alt;
2583 else
2584 goto normal_char;
2585
2586
2587 case '{':
2588 if (syntax & RE_INTERVALS && syntax & RE_NO_BK_BRACES)
2589 goto handle_interval;
2590 else
2591 goto normal_char;
2592
2593
2594 case '\\':
2595 if (p == pend) FREE_STACK_RETURN (REG_EESCAPE);
2596
2597 /* Do not translate the character after the \, so that we can
2598 distinguish, e.g., \B from \b, even if we normally would
2599 translate, e.g., B to b. */
2600 PATFETCH_RAW (c);
2601
2602 switch (c)
2603 {
2604 case '(':
2605 if (syntax & RE_NO_BK_PARENS)
2606 goto normal_backslash;
2607
2608 handle_open:
2609 {
2610 int shy = 0;
2611 if (p+1 < pend)
2612 {
2613 /* Look for a special (?...) construct */
2614 if ((syntax & RE_SHY_GROUPS) && *p == '?')
2615 {
2616 PATFETCH (c); /* Gobble up the '?'. */
2617 PATFETCH (c);
2618 switch (c)
2619 {
2620 case ':': shy = 1; break;
2621 default:
2622 /* Only (?:...) is supported right now. */
2623 FREE_STACK_RETURN (REG_BADPAT);
2624 }
2625 }
2626 }
2627
2628 if (!shy)
2629 {
2630 bufp->re_nsub++;
2631 regnum++;
2632 }
2633
2634 if (COMPILE_STACK_FULL)
2635 {
2636 RETALLOC (compile_stack.stack, compile_stack.size << 1,
2637 compile_stack_elt_t);
2638 if (compile_stack.stack == NULL) return REG_ESPACE;
2639
2640 compile_stack.size <<= 1;
2641 }
2642
2643 /* These are the values to restore when we hit end of this
2644 group. They are all relative offsets, so that if the
2645 whole pattern moves because of realloc, they will still
2646 be valid. */
2647 COMPILE_STACK_TOP.begalt_offset = begalt - bufp->buffer;
2648 COMPILE_STACK_TOP.fixup_alt_jump
2649 = fixup_alt_jump ? fixup_alt_jump - bufp->buffer + 1 : 0;
2650 COMPILE_STACK_TOP.laststart_offset = b - bufp->buffer;
2651 COMPILE_STACK_TOP.regnum = shy ? -regnum : regnum;
2652
2653 /* Do not push a
2654 start_memory for groups beyond the last one we can
2655 represent in the compiled pattern. */
2656 if (regnum <= MAX_REGNUM && !shy)
2657 BUF_PUSH_2 (start_memory, regnum);
2658
2659 compile_stack.avail++;
2660
2661 fixup_alt_jump = 0;
2662 laststart = 0;
2663 begalt = b;
2664 /* If we've reached MAX_REGNUM groups, then this open
2665 won't actually generate any code, so we'll have to
2666 clear pending_exact explicitly. */
2667 pending_exact = 0;
2668 break;
2669 }
2670
2671 case ')':
2672 if (syntax & RE_NO_BK_PARENS) goto normal_backslash;
2673
2674 if (COMPILE_STACK_EMPTY)
2675 {
2676 if (syntax & RE_UNMATCHED_RIGHT_PAREN_ORD)
2677 goto normal_backslash;
2678 else
2679 FREE_STACK_RETURN (REG_ERPAREN);
2680 }
2681
2682 handle_close:
2683 FIXUP_ALT_JUMP ();
2684
2685 /* See similar code for backslashed left paren above. */
2686 if (COMPILE_STACK_EMPTY)
2687 {
2688 if (syntax & RE_UNMATCHED_RIGHT_PAREN_ORD)
2689 goto normal_char;
2690 else
2691 FREE_STACK_RETURN (REG_ERPAREN);
2692 }
2693
2694 /* Since we just checked for an empty stack above, this
2695 ``can't happen''. */
2696 assert (compile_stack.avail != 0);
2697 {
2698 /* We don't just want to restore into `regnum', because
2699 later groups should continue to be numbered higher,
2700 as in `(ab)c(de)' -- the second group is #2. */
2701 regnum_t this_group_regnum;
2702
2703 compile_stack.avail--;
2704 begalt = bufp->buffer + COMPILE_STACK_TOP.begalt_offset;
2705 fixup_alt_jump
2706 = COMPILE_STACK_TOP.fixup_alt_jump
2707 ? bufp->buffer + COMPILE_STACK_TOP.fixup_alt_jump - 1
2708 : 0;
2709 laststart = bufp->buffer + COMPILE_STACK_TOP.laststart_offset;
2710 this_group_regnum = COMPILE_STACK_TOP.regnum;
2711 /* If we've reached MAX_REGNUM groups, then this open
2712 won't actually generate any code, so we'll have to
2713 clear pending_exact explicitly. */
2714 pending_exact = 0;
2715
2716 /* We're at the end of the group, so now we know how many
2717 groups were inside this one. */
2718 if (this_group_regnum <= MAX_REGNUM && this_group_regnum > 0)
2719 BUF_PUSH_2 (stop_memory, this_group_regnum);
2720 }
2721 break;
2722
2723
2724 case '|': /* `\|'. */
2725 if (syntax & RE_LIMITED_OPS || syntax & RE_NO_BK_VBAR)
2726 goto normal_backslash;
2727 handle_alt:
2728 if (syntax & RE_LIMITED_OPS)
2729 goto normal_char;
2730
2731 /* Insert before the previous alternative a jump which
2732 jumps to this alternative if the former fails. */
2733 GET_BUFFER_SPACE (3);
2734 INSERT_JUMP (on_failure_jump, begalt, b + 6);
2735 pending_exact = 0;
2736 b += 3;
2737
2738 /* The alternative before this one has a jump after it
2739 which gets executed if it gets matched. Adjust that
2740 jump so it will jump to this alternative's analogous
2741 jump (put in below, which in turn will jump to the next
2742 (if any) alternative's such jump, etc.). The last such
2743 jump jumps to the correct final destination. A picture:
2744 _____ _____
2745 | | | |
2746 | v | v
2747 a | b | c
2748
2749 If we are at `b', then fixup_alt_jump right now points to a
2750 three-byte space after `a'. We'll put in the jump, set
2751 fixup_alt_jump to right after `b', and leave behind three
2752 bytes which we'll fill in when we get to after `c'. */
2753
2754 FIXUP_ALT_JUMP ();
2755
2756 /* Mark and leave space for a jump after this alternative,
2757 to be filled in later either by next alternative or
2758 when know we're at the end of a series of alternatives. */
2759 fixup_alt_jump = b;
2760 GET_BUFFER_SPACE (3);
2761 b += 3;
2762
2763 laststart = 0;
2764 begalt = b;
2765 break;
2766
2767
2768 case '{':
2769 /* If \{ is a literal. */
2770 if (!(syntax & RE_INTERVALS)
2771 /* If we're at `\{' and it's not the open-interval
2772 operator. */
2773 || (syntax & RE_NO_BK_BRACES)
2774 /* What is that? -sm */
2775 /* || (p - 2 == pattern && p == pend) */)
2776 goto normal_backslash;
2777
2778 handle_interval:
2779 {
2780 /* If got here, then the syntax allows intervals. */
2781
2782 /* At least (most) this many matches must be made. */
2783 int lower_bound = 0, upper_bound = -1;
2784
2785 beg_interval = p;
2786
2787 if (p == pend)
2788 {
2789 if (syntax & RE_NO_BK_BRACES)
2790 goto unfetch_interval;
2791 else
2792 FREE_STACK_RETURN (REG_EBRACE);
2793 }
2794
2795 GET_UNSIGNED_NUMBER (lower_bound);
2796
2797 if (c == ',')
2798 GET_UNSIGNED_NUMBER (upper_bound);
2799 else
2800 /* Interval such as `{1}' => match exactly once. */
2801 upper_bound = lower_bound;
2802
2803 if (lower_bound < 0 || upper_bound > RE_DUP_MAX
2804 || (upper_bound >= 0 && lower_bound > upper_bound))
2805 {
2806 if (syntax & RE_NO_BK_BRACES)
2807 goto unfetch_interval;
2808 else
2809 FREE_STACK_RETURN (REG_BADBR);
2810 }
2811
2812 if (!(syntax & RE_NO_BK_BRACES))
2813 {
2814 if (c != '\\') FREE_STACK_RETURN (REG_EBRACE);
2815
2816 PATFETCH (c);
2817 }
2818
2819 if (c != '}')
2820 {
2821 if (syntax & RE_NO_BK_BRACES)
2822 goto unfetch_interval;
2823 else
2824 FREE_STACK_RETURN (REG_BADBR);
2825 }
2826
2827 /* We just parsed a valid interval. */
2828
2829 /* If it's invalid to have no preceding re. */
2830 if (!laststart)
2831 {
2832 if (syntax & RE_CONTEXT_INVALID_OPS)
2833 FREE_STACK_RETURN (REG_BADRPT);
2834 else if (syntax & RE_CONTEXT_INDEP_OPS)
2835 laststart = b;
2836 else
2837 goto unfetch_interval;
2838 }
2839
2840 if (upper_bound == 0)
2841 /* If the upper bound is zero, just drop the sub pattern
2842 altogether. */
2843 b = laststart;
2844 else if (lower_bound == 1 && upper_bound == 1)
2845 /* Just match it once: nothing to do here. */
2846 ;
2847
2848 /* Otherwise, we have a nontrivial interval. When
2849 we're all done, the pattern will look like:
2850 set_number_at <jump count> <upper bound>
2851 set_number_at <succeed_n count> <lower bound>
2852 succeed_n <after jump addr> <succeed_n count>
2853 <body of loop>
2854 jump_n <succeed_n addr> <jump count>
2855 (The upper bound and `jump_n' are omitted if
2856 `upper_bound' is 1, though.) */
2857 else
2858 { /* If the upper bound is > 1, we need to insert
2859 more at the end of the loop. */
2860 unsigned int nbytes = (upper_bound < 0 ? 3
2861 : upper_bound > 1 ? 5 : 0);
2862 unsigned int startoffset = 0;
2863
2864 GET_BUFFER_SPACE (20); /* We might use less. */
2865
2866 if (lower_bound == 0)
2867 {
2868 /* A succeed_n that starts with 0 is really a
2869 a simple on_failure_jump_loop. */
2870 INSERT_JUMP (on_failure_jump_loop, laststart,
2871 b + 3 + nbytes);
2872 b += 3;
2873 }
2874 else
2875 {
2876 /* Initialize lower bound of the `succeed_n', even
2877 though it will be set during matching by its
2878 attendant `set_number_at' (inserted next),
2879 because `re_compile_fastmap' needs to know.
2880 Jump to the `jump_n' we might insert below. */
2881 INSERT_JUMP2 (succeed_n, laststart,
2882 b + 5 + nbytes,
2883 lower_bound);
2884 b += 5;
2885
2886 /* Code to initialize the lower bound. Insert
2887 before the `succeed_n'. The `5' is the last two
2888 bytes of this `set_number_at', plus 3 bytes of
2889 the following `succeed_n'. */
2890 insert_op2 (set_number_at, laststart, 5, lower_bound, b);
2891 b += 5;
2892 startoffset += 5;
2893 }
2894
2895 if (upper_bound < 0)
2896 {
2897 /* A negative upper bound stands for infinity,
2898 in which case it degenerates to a plain jump. */
2899 STORE_JUMP (jump, b, laststart + startoffset);
2900 b += 3;
2901 }
2902 else if (upper_bound > 1)
2903 { /* More than one repetition is allowed, so
2904 append a backward jump to the `succeed_n'
2905 that starts this interval.
2906
2907 When we've reached this during matching,
2908 we'll have matched the interval once, so
2909 jump back only `upper_bound - 1' times. */
2910 STORE_JUMP2 (jump_n, b, laststart + startoffset,
2911 upper_bound - 1);
2912 b += 5;
2913
2914 /* The location we want to set is the second
2915 parameter of the `jump_n'; that is `b-2' as
2916 an absolute address. `laststart' will be
2917 the `set_number_at' we're about to insert;
2918 `laststart+3' the number to set, the source
2919 for the relative address. But we are
2920 inserting into the middle of the pattern --
2921 so everything is getting moved up by 5.
2922 Conclusion: (b - 2) - (laststart + 3) + 5,
2923 i.e., b - laststart.
2924
2925 We insert this at the beginning of the loop
2926 so that if we fail during matching, we'll
2927 reinitialize the bounds. */
2928 insert_op2 (set_number_at, laststart, b - laststart,
2929 upper_bound - 1, b);
2930 b += 5;
2931 }
2932 }
2933 pending_exact = 0;
2934 beg_interval = NULL;
2935 }
2936 break;
2937
2938 unfetch_interval:
2939 /* If an invalid interval, match the characters as literals. */
2940 assert (beg_interval);
2941 p = beg_interval;
2942 beg_interval = NULL;
2943
2944 /* normal_char and normal_backslash need `c'. */
2945 c = '{';
2946
2947 if (!(syntax & RE_NO_BK_BRACES))
2948 {
2949 assert (p > pattern && p[-1] == '\\');
2950 goto normal_backslash;
2951 }
2952 else
2953 goto normal_char;
2954
2955 #ifdef emacs
2956 /* There is no way to specify the before_dot and after_dot
2957 operators. rms says this is ok. --karl */
2958 case '=':
2959 BUF_PUSH (at_dot);
2960 break;
2961
2962 case 's':
2963 laststart = b;
2964 PATFETCH (c);
2965 BUF_PUSH_2 (syntaxspec, syntax_spec_code[c]);
2966 break;
2967
2968 case 'S':
2969 laststart = b;
2970 PATFETCH (c);
2971 BUF_PUSH_2 (notsyntaxspec, syntax_spec_code[c]);
2972 break;
2973
2974 case 'c':
2975 laststart = b;
2976 PATFETCH_RAW (c);
2977 BUF_PUSH_2 (categoryspec, c);
2978 break;
2979
2980 case 'C':
2981 laststart = b;
2982 PATFETCH_RAW (c);
2983 BUF_PUSH_2 (notcategoryspec, c);
2984 break;
2985 #endif /* emacs */
2986
2987
2988 case 'w':
2989 laststart = b;
2990 BUF_PUSH_2 (syntaxspec, Sword);
2991 break;
2992
2993
2994 case 'W':
2995 laststart = b;
2996 BUF_PUSH_2 (notsyntaxspec, Sword);
2997 break;
2998
2999
3000 case '<':
3001 BUF_PUSH (wordbeg);
3002 break;
3003
3004 case '>':
3005 BUF_PUSH (wordend);
3006 break;
3007
3008 case 'b':
3009 BUF_PUSH (wordbound);
3010 break;
3011
3012 case 'B':
3013 BUF_PUSH (notwordbound);
3014 break;
3015
3016 case '`':
3017 BUF_PUSH (begbuf);
3018 break;
3019
3020 case '\'':
3021 BUF_PUSH (endbuf);
3022 break;
3023
3024 case '1': case '2': case '3': case '4': case '5':
3025 case '6': case '7': case '8': case '9':
3026 if (syntax & RE_NO_BK_REFS)
3027 goto normal_char;
3028
3029 c1 = c - '0';
3030
3031 if (c1 > regnum)
3032 FREE_STACK_RETURN (REG_ESUBREG);
3033
3034 /* Can't back reference to a subexpression if inside of it. */
3035 if (group_in_compile_stack (compile_stack, c1))
3036 goto normal_char;
3037
3038 laststart = b;
3039 BUF_PUSH_2 (duplicate, c1);
3040 break;
3041
3042
3043 case '+':
3044 case '?':
3045 if (syntax & RE_BK_PLUS_QM)
3046 goto handle_plus;
3047 else
3048 goto normal_backslash;
3049
3050 default:
3051 normal_backslash:
3052 /* You might think it would be useful for \ to mean
3053 not to translate; but if we don't translate it
3054 it will never match anything. */
3055 c = TRANSLATE (c);
3056 goto normal_char;
3057 }
3058 break;
3059
3060
3061 default:
3062 /* Expects the character in `c'. */
3063 normal_char:
3064 /* If no exactn currently being built. */
3065 if (!pending_exact
3066
3067 /* If last exactn not at current position. */
3068 || pending_exact + *pending_exact + 1 != b
3069
3070 /* We have only one byte following the exactn for the count. */
3071 || *pending_exact >= (1 << BYTEWIDTH) - MAX_MULTIBYTE_LENGTH
3072
3073 /* If followed by a repetition operator. */
3074 || (p != pend && (*p == '*' || *p == '^'))
3075 || ((syntax & RE_BK_PLUS_QM)
3076 ? p + 1 < pend && *p == '\\' && (p[1] == '+' || p[1] == '?')
3077 : p != pend && (*p == '+' || *p == '?'))
3078 || ((syntax & RE_INTERVALS)
3079 && ((syntax & RE_NO_BK_BRACES)
3080 ? p != pend && *p == '{'
3081 : p + 1 < pend && p[0] == '\\' && p[1] == '{')))
3082 {
3083 /* Start building a new exactn. */
3084
3085 laststart = b;
3086
3087 BUF_PUSH_2 (exactn, 0);
3088 pending_exact = b - 1;
3089 }
3090
3091 GET_BUFFER_SPACE (MAX_MULTIBYTE_LENGTH);
3092 {
3093 int len;
3094
3095 if (multibyte)
3096 len = CHAR_STRING (c, b);
3097 else
3098 *b = c, len = 1;
3099 b += len;
3100 (*pending_exact) += len;
3101 }
3102
3103 break;
3104 } /* switch (c) */
3105 } /* while p != pend */
3106
3107
3108 /* Through the pattern now. */
3109
3110 FIXUP_ALT_JUMP ();
3111
3112 if (!COMPILE_STACK_EMPTY)
3113 FREE_STACK_RETURN (REG_EPAREN);
3114
3115 /* If we don't want backtracking, force success
3116 the first time we reach the end of the compiled pattern. */
3117 if (syntax & RE_NO_POSIX_BACKTRACKING)
3118 BUF_PUSH (succeed);
3119
3120 free (compile_stack.stack);
3121
3122 /* We have succeeded; set the length of the buffer. */
3123 bufp->used = b - bufp->buffer;
3124
3125 #ifdef DEBUG
3126 if (debug > 0)
3127 {
3128 re_compile_fastmap (bufp);
3129 DEBUG_PRINT1 ("\nCompiled pattern: \n");
3130 print_compiled_pattern (bufp);
3131 }
3132 debug--;
3133 #endif /* DEBUG */
3134
3135 #ifndef MATCH_MAY_ALLOCATE
3136 /* Initialize the failure stack to the largest possible stack. This
3137 isn't necessary unless we're trying to avoid calling alloca in
3138 the search and match routines. */
3139 {
3140 int num_regs = bufp->re_nsub + 1;
3141
3142 if (fail_stack.size < re_max_failures * TYPICAL_FAILURE_SIZE)
3143 {
3144 fail_stack.size = re_max_failures * TYPICAL_FAILURE_SIZE;
3145
3146 if (! fail_stack.stack)
3147 fail_stack.stack
3148 = (fail_stack_elt_t *) malloc (fail_stack.size
3149 * sizeof (fail_stack_elt_t));
3150 else
3151 fail_stack.stack
3152 = (fail_stack_elt_t *) realloc (fail_stack.stack,
3153 (fail_stack.size
3154 * sizeof (fail_stack_elt_t)));
3155 }
3156
3157 regex_grow_registers (num_regs);
3158 }
3159 #endif /* not MATCH_MAY_ALLOCATE */
3160
3161 return REG_NOERROR;
3162 } /* regex_compile */
3163 \f
3164 /* Subroutines for `regex_compile'. */
3165
3166 /* Store OP at LOC followed by two-byte integer parameter ARG. */
3167
3168 static void
3169 store_op1 (op, loc, arg)
3170 re_opcode_t op;
3171 unsigned char *loc;
3172 int arg;
3173 {
3174 *loc = (unsigned char) op;
3175 STORE_NUMBER (loc + 1, arg);
3176 }
3177
3178
3179 /* Like `store_op1', but for two two-byte parameters ARG1 and ARG2. */
3180
3181 static void
3182 store_op2 (op, loc, arg1, arg2)
3183 re_opcode_t op;
3184 unsigned char *loc;
3185 int arg1, arg2;
3186 {
3187 *loc = (unsigned char) op;
3188 STORE_NUMBER (loc + 1, arg1);
3189 STORE_NUMBER (loc + 3, arg2);
3190 }
3191
3192
3193 /* Copy the bytes from LOC to END to open up three bytes of space at LOC
3194 for OP followed by two-byte integer parameter ARG. */
3195
3196 static void
3197 insert_op1 (op, loc, arg, end)
3198 re_opcode_t op;
3199 unsigned char *loc;
3200 int arg;
3201 unsigned char *end;
3202 {
3203 register unsigned char *pfrom = end;
3204 register unsigned char *pto = end + 3;
3205
3206 while (pfrom != loc)
3207 *--pto = *--pfrom;
3208
3209 store_op1 (op, loc, arg);
3210 }
3211
3212
3213 /* Like `insert_op1', but for two two-byte parameters ARG1 and ARG2. */
3214
3215 static void
3216 insert_op2 (op, loc, arg1, arg2, end)
3217 re_opcode_t op;
3218 unsigned char *loc;
3219 int arg1, arg2;
3220 unsigned char *end;
3221 {
3222 register unsigned char *pfrom = end;
3223 register unsigned char *pto = end + 5;
3224
3225 while (pfrom != loc)
3226 *--pto = *--pfrom;
3227
3228 store_op2 (op, loc, arg1, arg2);
3229 }
3230
3231
3232 /* P points to just after a ^ in PATTERN. Return true if that ^ comes
3233 after an alternative or a begin-subexpression. We assume there is at
3234 least one character before the ^. */
3235
3236 static boolean
3237 at_begline_loc_p (pattern, p, syntax)
3238 const unsigned char *pattern, *p;
3239 reg_syntax_t syntax;
3240 {
3241 const unsigned char *prev = p - 2;
3242 boolean prev_prev_backslash = prev > pattern && prev[-1] == '\\';
3243
3244 return
3245 /* After a subexpression? */
3246 (*prev == '(' && (syntax & RE_NO_BK_PARENS || prev_prev_backslash))
3247 /* After an alternative? */
3248 || (*prev == '|' && (syntax & RE_NO_BK_VBAR || prev_prev_backslash))
3249 /* After a shy subexpression? */
3250 || ((syntax & RE_SHY_GROUPS) && prev - 2 >= pattern
3251 && prev[-1] == '?' && prev[-2] == '('
3252 && (syntax & RE_NO_BK_PARENS
3253 || (prev - 3 >= pattern && prev[-3] == '\\')));
3254 }
3255
3256
3257 /* The dual of at_begline_loc_p. This one is for $. We assume there is
3258 at least one character after the $, i.e., `P < PEND'. */
3259
3260 static boolean
3261 at_endline_loc_p (p, pend, syntax)
3262 const unsigned char *p, *pend;
3263 reg_syntax_t syntax;
3264 {
3265 const unsigned char *next = p;
3266 boolean next_backslash = *next == '\\';
3267 const unsigned char *next_next = p + 1 < pend ? p + 1 : 0;
3268
3269 return
3270 /* Before a subexpression? */
3271 (syntax & RE_NO_BK_PARENS ? *next == ')'
3272 : next_backslash && next_next && *next_next == ')')
3273 /* Before an alternative? */
3274 || (syntax & RE_NO_BK_VBAR ? *next == '|'
3275 : next_backslash && next_next && *next_next == '|');
3276 }
3277
3278
3279 /* Returns true if REGNUM is in one of COMPILE_STACK's elements and
3280 false if it's not. */
3281
3282 static boolean
3283 group_in_compile_stack (compile_stack, regnum)
3284 compile_stack_type compile_stack;
3285 regnum_t regnum;
3286 {
3287 int this_element;
3288
3289 for (this_element = compile_stack.avail - 1;
3290 this_element >= 0;
3291 this_element--)
3292 if (compile_stack.stack[this_element].regnum == regnum)
3293 return true;
3294
3295 return false;
3296 }
3297 \f
3298 /* analyse_first.
3299 If fastmap is non-NULL, go through the pattern and fill fastmap
3300 with all the possible leading chars. If fastmap is NULL, don't
3301 bother filling it up (obviously) and only return whether the
3302 pattern could potentially match the empty string.
3303
3304 Return 1 if p..pend might match the empty string.
3305 Return 0 if p..pend matches at least one char.
3306 Return -1 if p..pend matches at least one char, but fastmap was not
3307 updated accurately.
3308 Return -2 if an error occurred. */
3309
3310 static int
3311 analyse_first (p, pend, fastmap, multibyte)
3312 unsigned char *p, *pend;
3313 char *fastmap;
3314 const int multibyte;
3315 {
3316 int j, k;
3317 boolean not;
3318 #ifdef MATCH_MAY_ALLOCATE
3319 fail_stack_type fail_stack;
3320 #endif
3321 #ifndef REGEX_MALLOC
3322 char *destination;
3323 #endif
3324
3325 #if defined (REL_ALLOC) && defined (REGEX_MALLOC)
3326 /* This holds the pointer to the failure stack, when
3327 it is allocated relocatably. */
3328 fail_stack_elt_t *failure_stack_ptr;
3329 #endif
3330
3331 /* Assume that each path through the pattern can be null until
3332 proven otherwise. We set this false at the bottom of switch
3333 statement, to which we get only if a particular path doesn't
3334 match the empty string. */
3335 boolean path_can_be_null = true;
3336
3337 /* If all elements for base leading-codes in fastmap is set, this
3338 flag is set true. */
3339 boolean match_any_multibyte_characters = false;
3340
3341 assert (p);
3342
3343 INIT_FAIL_STACK ();
3344
3345 /* The loop below works as follows:
3346 - It has a working-list kept in the PATTERN_STACK and which basically
3347 starts by only containing a pointer to the first operation.
3348 - If the opcode we're looking at is a match against some set of
3349 chars, then we add those chars to the fastmap and go on to the
3350 next work element from the worklist (done via `break').
3351 - If the opcode is a control operator on the other hand, we either
3352 ignore it (if it's meaningless at this point, such as `start_memory')
3353 or execute it (if it's a jump). If the jump has several destinations
3354 (i.e. `on_failure_jump'), then we push the other destination onto the
3355 worklist.
3356 We guarantee termination by ignoring backward jumps (more or less),
3357 so that `p' is monotonically increasing. More to the point, we
3358 never set `p' (or push) anything `<= p1'. */
3359
3360 /* If can_be_null is set, then the fastmap will not be used anyway. */
3361 while (1)
3362 {
3363 /* `p1' is used as a marker of how far back a `on_failure_jump'
3364 can go without being ignored. It is normally equal to `p'
3365 (which prevents any backward `on_failure_jump') except right
3366 after a plain `jump', to allow patterns such as:
3367 0: jump 10
3368 3..9: <body>
3369 10: on_failure_jump 3
3370 as used for the *? operator. */
3371 unsigned char *p1 = p;
3372
3373 if (p >= pend)
3374 {
3375 if (path_can_be_null)
3376 return (RESET_FAIL_STACK (), 1);
3377
3378 /* We have reached the (effective) end of pattern. */
3379 if (PATTERN_STACK_EMPTY ())
3380 return (RESET_FAIL_STACK (), 0);
3381
3382 p = (unsigned char*) POP_PATTERN_OP ();
3383 path_can_be_null = true;
3384 continue;
3385 }
3386
3387 /* We should never be about to go beyond the end of the pattern. */
3388 assert (p < pend);
3389
3390 switch (SWITCH_ENUM_CAST ((re_opcode_t) *p++))
3391 {
3392 case succeed:
3393 p = pend;
3394 continue;
3395
3396 case duplicate:
3397 /* If the first character has to match a backreference, that means
3398 that the group was empty (since it already matched). Since this
3399 is the only case that interests us here, we can assume that the
3400 backreference must match the empty string. */
3401 p++;
3402 continue;
3403
3404
3405 /* Following are the cases which match a character. These end
3406 with `break'. */
3407
3408 case exactn:
3409 if (fastmap)
3410 {
3411 int c = RE_STRING_CHAR (p + 1, pend - p);
3412
3413 if (SINGLE_BYTE_CHAR_P (c))
3414 fastmap[c] = 1;
3415 else
3416 fastmap[p[1]] = 1;
3417 }
3418 break;
3419
3420
3421 case anychar:
3422 /* We could put all the chars except for \n (and maybe \0)
3423 but we don't bother since it is generally not worth it. */
3424 if (!fastmap) break;
3425 return (RESET_FAIL_STACK (), -1);
3426
3427
3428 case charset_not:
3429 /* Chars beyond end of bitmap are possible matches.
3430 All the single-byte codes can occur in multibyte buffers.
3431 So any that are not listed in the charset
3432 are possible matches, even in multibyte buffers. */
3433 if (!fastmap) break;
3434 for (j = CHARSET_BITMAP_SIZE (&p[-1]) * BYTEWIDTH;
3435 j < (1 << BYTEWIDTH); j++)
3436 fastmap[j] = 1;
3437 /* Fallthrough */
3438 case charset:
3439 if (!fastmap) break;
3440 not = (re_opcode_t) *(p - 1) == charset_not;
3441 for (j = CHARSET_BITMAP_SIZE (&p[-1]) * BYTEWIDTH - 1, p++;
3442 j >= 0; j--)
3443 if (!!(p[j / BYTEWIDTH] & (1 << (j % BYTEWIDTH))) ^ not)
3444 fastmap[j] = 1;
3445
3446 if ((not && multibyte)
3447 /* Any character set can possibly contain a character
3448 which doesn't match the specified set of characters. */
3449 || (CHARSET_RANGE_TABLE_EXISTS_P (&p[-2])
3450 && CHARSET_RANGE_TABLE_BITS (&p[-2]) != 0))
3451 /* If we can match a character class, we can match
3452 any character set. */
3453 {
3454 set_fastmap_for_multibyte_characters:
3455 if (match_any_multibyte_characters == false)
3456 {
3457 for (j = 0x80; j < 0xA0; j++) /* XXX */
3458 if (BASE_LEADING_CODE_P (j))
3459 fastmap[j] = 1;
3460 match_any_multibyte_characters = true;
3461 }
3462 }
3463
3464 else if (!not && CHARSET_RANGE_TABLE_EXISTS_P (&p[-2])
3465 && match_any_multibyte_characters == false)
3466 {
3467 /* Set fastmap[I] 1 where I is a base leading code of each
3468 multibyte character in the range table. */
3469 int c, count;
3470
3471 /* Make P points the range table. `+ 2' is to skip flag
3472 bits for a character class. */
3473 p += CHARSET_BITMAP_SIZE (&p[-2]) + 2;
3474
3475 /* Extract the number of ranges in range table into COUNT. */
3476 EXTRACT_NUMBER_AND_INCR (count, p);
3477 for (; count > 0; count--, p += 2 * 3) /* XXX */
3478 {
3479 /* Extract the start of each range. */
3480 EXTRACT_CHARACTER (c, p);
3481 j = CHAR_CHARSET (c);
3482 fastmap[CHARSET_LEADING_CODE_BASE (j)] = 1;
3483 }
3484 }
3485 break;
3486
3487 case syntaxspec:
3488 case notsyntaxspec:
3489 if (!fastmap) break;
3490 #ifndef emacs
3491 not = (re_opcode_t)p[-1] == notsyntaxspec;
3492 k = *p++;
3493 for (j = 0; j < (1 << BYTEWIDTH); j++)
3494 if ((SYNTAX (j) == (enum syntaxcode) k) ^ not)
3495 fastmap[j] = 1;
3496 break;
3497 #else /* emacs */
3498 /* This match depends on text properties. These end with
3499 aborting optimizations. */
3500 return (RESET_FAIL_STACK (), -1);
3501
3502 case categoryspec:
3503 case notcategoryspec:
3504 if (!fastmap) break;
3505 not = (re_opcode_t)p[-1] == notcategoryspec;
3506 k = *p++;
3507 for (j = 0; j < (1 << BYTEWIDTH); j++)
3508 if ((CHAR_HAS_CATEGORY (j, k)) ^ not)
3509 fastmap[j] = 1;
3510
3511 if (multibyte)
3512 /* Any character set can possibly contain a character
3513 whose category is K (or not). */
3514 goto set_fastmap_for_multibyte_characters;
3515 break;
3516
3517 /* All cases after this match the empty string. These end with
3518 `continue'. */
3519
3520 case before_dot:
3521 case at_dot:
3522 case after_dot:
3523 #endif /* !emacs */
3524 case no_op:
3525 case begline:
3526 case endline:
3527 case begbuf:
3528 case endbuf:
3529 case wordbound:
3530 case notwordbound:
3531 case wordbeg:
3532 case wordend:
3533 continue;
3534
3535
3536 case jump:
3537 EXTRACT_NUMBER_AND_INCR (j, p);
3538 if (j < 0)
3539 /* Backward jumps can only go back to code that we've already
3540 visited. `re_compile' should make sure this is true. */
3541 break;
3542 p += j;
3543 switch (SWITCH_ENUM_CAST ((re_opcode_t) *p))
3544 {
3545 case on_failure_jump:
3546 case on_failure_keep_string_jump:
3547 case on_failure_jump_loop:
3548 case on_failure_jump_nastyloop:
3549 case on_failure_jump_smart:
3550 p++;
3551 break;
3552 default:
3553 continue;
3554 };
3555 /* Keep `p1' to allow the `on_failure_jump' we are jumping to
3556 to jump back to "just after here". */
3557 /* Fallthrough */
3558
3559 case on_failure_jump:
3560 case on_failure_keep_string_jump:
3561 case on_failure_jump_nastyloop:
3562 case on_failure_jump_loop:
3563 case on_failure_jump_smart:
3564 EXTRACT_NUMBER_AND_INCR (j, p);
3565 if (p + j <= p1)
3566 ; /* Backward jump to be ignored. */
3567 else if (!PUSH_PATTERN_OP (p + j, fail_stack))
3568 return (RESET_FAIL_STACK (), -2);
3569 continue;
3570
3571
3572 case jump_n:
3573 /* This code simply does not properly handle forward jump_n. */
3574 DEBUG_STATEMENT (EXTRACT_NUMBER (j, p); assert (j < 0));
3575 p += 4;
3576 /* jump_n can either jump or fall through. The (backward) jump
3577 case has already been handled, so we only need to look at the
3578 fallthrough case. */
3579 continue;
3580
3581 case succeed_n:
3582 /* If N == 0, it should be an on_failure_jump_loop instead. */
3583 DEBUG_STATEMENT (EXTRACT_NUMBER (j, p + 2); assert (j > 0));
3584 p += 4;
3585 /* We only care about one iteration of the loop, so we don't
3586 need to consider the case where this behaves like an
3587 on_failure_jump. */
3588 continue;
3589
3590
3591 case set_number_at:
3592 p += 4;
3593 continue;
3594
3595
3596 case start_memory:
3597 case stop_memory:
3598 p += 1;
3599 continue;
3600
3601
3602 default:
3603 abort (); /* We have listed all the cases. */
3604 } /* switch *p++ */
3605
3606 /* Getting here means we have found the possible starting
3607 characters for one path of the pattern -- and that the empty
3608 string does not match. We need not follow this path further.
3609 Instead, look at the next alternative (remembered on the
3610 stack), or quit if no more. The test at the top of the loop
3611 does these things. */
3612 path_can_be_null = false;
3613 p = pend;
3614 } /* while p */
3615
3616 return (RESET_FAIL_STACK (), 0);
3617 } /* analyse_first */
3618 \f
3619 /* re_compile_fastmap computes a ``fastmap'' for the compiled pattern in
3620 BUFP. A fastmap records which of the (1 << BYTEWIDTH) possible
3621 characters can start a string that matches the pattern. This fastmap
3622 is used by re_search to skip quickly over impossible starting points.
3623
3624 Character codes above (1 << BYTEWIDTH) are not represented in the
3625 fastmap, but the leading codes are represented. Thus, the fastmap
3626 indicates which character sets could start a match.
3627
3628 The caller must supply the address of a (1 << BYTEWIDTH)-byte data
3629 area as BUFP->fastmap.
3630
3631 We set the `fastmap', `fastmap_accurate', and `can_be_null' fields in
3632 the pattern buffer.
3633
3634 Returns 0 if we succeed, -2 if an internal error. */
3635
3636 int
3637 re_compile_fastmap (bufp)
3638 struct re_pattern_buffer *bufp;
3639 {
3640 char *fastmap = bufp->fastmap;
3641 int analysis;
3642
3643 assert (fastmap && bufp->buffer);
3644
3645 bzero (fastmap, 1 << BYTEWIDTH); /* Assume nothing's valid. */
3646 bufp->fastmap_accurate = 1; /* It will be when we're done. */
3647
3648 analysis = analyse_first (bufp->buffer, bufp->buffer + bufp->used,
3649 fastmap, RE_MULTIBYTE_P (bufp));
3650 if (analysis < -1)
3651 return analysis;
3652 bufp->can_be_null = (analysis != 0);
3653 return 0;
3654 } /* re_compile_fastmap */
3655 \f
3656 /* Set REGS to hold NUM_REGS registers, storing them in STARTS and
3657 ENDS. Subsequent matches using PATTERN_BUFFER and REGS will use
3658 this memory for recording register information. STARTS and ENDS
3659 must be allocated using the malloc library routine, and must each
3660 be at least NUM_REGS * sizeof (regoff_t) bytes long.
3661
3662 If NUM_REGS == 0, then subsequent matches should allocate their own
3663 register data.
3664
3665 Unless this function is called, the first search or match using
3666 PATTERN_BUFFER will allocate its own register data, without
3667 freeing the old data. */
3668
3669 void
3670 re_set_registers (bufp, regs, num_regs, starts, ends)
3671 struct re_pattern_buffer *bufp;
3672 struct re_registers *regs;
3673 unsigned num_regs;
3674 regoff_t *starts, *ends;
3675 {
3676 if (num_regs)
3677 {
3678 bufp->regs_allocated = REGS_REALLOCATE;
3679 regs->num_regs = num_regs;
3680 regs->start = starts;
3681 regs->end = ends;
3682 }
3683 else
3684 {
3685 bufp->regs_allocated = REGS_UNALLOCATED;
3686 regs->num_regs = 0;
3687 regs->start = regs->end = (regoff_t *) 0;
3688 }
3689 }
3690 \f
3691 /* Searching routines. */
3692
3693 /* Like re_search_2, below, but only one string is specified, and
3694 doesn't let you say where to stop matching. */
3695
3696 int
3697 re_search (bufp, string, size, startpos, range, regs)
3698 struct re_pattern_buffer *bufp;
3699 const char *string;
3700 int size, startpos, range;
3701 struct re_registers *regs;
3702 {
3703 return re_search_2 (bufp, NULL, 0, string, size, startpos, range,
3704 regs, size);
3705 }
3706
3707 /* End address of virtual concatenation of string. */
3708 #define STOP_ADDR_VSTRING(P) \
3709 (((P) >= size1 ? string2 + size2 : string1 + size1))
3710
3711 /* Address of POS in the concatenation of virtual string. */
3712 #define POS_ADDR_VSTRING(POS) \
3713 (((POS) >= size1 ? string2 - size1 : string1) + (POS))
3714
3715 /* Using the compiled pattern in BUFP->buffer, first tries to match the
3716 virtual concatenation of STRING1 and STRING2, starting first at index
3717 STARTPOS, then at STARTPOS + 1, and so on.
3718
3719 STRING1 and STRING2 have length SIZE1 and SIZE2, respectively.
3720
3721 RANGE is how far to scan while trying to match. RANGE = 0 means try
3722 only at STARTPOS; in general, the last start tried is STARTPOS +
3723 RANGE.
3724
3725 In REGS, return the indices of the virtual concatenation of STRING1
3726 and STRING2 that matched the entire BUFP->buffer and its contained
3727 subexpressions.
3728
3729 Do not consider matching one past the index STOP in the virtual
3730 concatenation of STRING1 and STRING2.
3731
3732 We return either the position in the strings at which the match was
3733 found, -1 if no match, or -2 if error (such as failure
3734 stack overflow). */
3735
3736 int
3737 re_search_2 (bufp, str1, size1, str2, size2, startpos, range, regs, stop)
3738 struct re_pattern_buffer *bufp;
3739 const char *str1, *str2;
3740 int size1, size2;
3741 int startpos;
3742 int range;
3743 struct re_registers *regs;
3744 int stop;
3745 {
3746 int val;
3747 re_char *string1 = (re_char*) str1;
3748 re_char *string2 = (re_char*) str2;
3749 register char *fastmap = bufp->fastmap;
3750 register RE_TRANSLATE_TYPE translate = bufp->translate;
3751 int total_size = size1 + size2;
3752 int endpos = startpos + range;
3753 int anchored_start = 0;
3754
3755 /* Nonzero if we have to concern multibyte character. */
3756 const boolean multibyte = RE_MULTIBYTE_P (bufp);
3757
3758 /* Check for out-of-range STARTPOS. */
3759 if (startpos < 0 || startpos > total_size)
3760 return -1;
3761
3762 /* Fix up RANGE if it might eventually take us outside
3763 the virtual concatenation of STRING1 and STRING2.
3764 Make sure we won't move STARTPOS below 0 or above TOTAL_SIZE. */
3765 if (endpos < 0)
3766 range = 0 - startpos;
3767 else if (endpos > total_size)
3768 range = total_size - startpos;
3769
3770 /* If the search isn't to be a backwards one, don't waste time in a
3771 search for a pattern anchored at beginning of buffer. */
3772 if (bufp->used > 0 && (re_opcode_t) bufp->buffer[0] == begbuf && range > 0)
3773 {
3774 if (startpos > 0)
3775 return -1;
3776 else
3777 range = 0;
3778 }
3779
3780 #ifdef emacs
3781 /* In a forward search for something that starts with \=.
3782 don't keep searching past point. */
3783 if (bufp->used > 0 && (re_opcode_t) bufp->buffer[0] == at_dot && range > 0)
3784 {
3785 range = PT_BYTE - BEGV_BYTE - startpos;
3786 if (range < 0)
3787 return -1;
3788 }
3789 #endif /* emacs */
3790
3791 /* Update the fastmap now if not correct already. */
3792 if (fastmap && !bufp->fastmap_accurate)
3793 if (re_compile_fastmap (bufp) == -2)
3794 return -2;
3795
3796 /* See whether the pattern is anchored. */
3797 if (bufp->buffer[0] == begline)
3798 anchored_start = 1;
3799
3800 #ifdef emacs
3801 gl_state.object = re_match_object;
3802 {
3803 int charpos = SYNTAX_TABLE_BYTE_TO_CHAR (POS_AS_IN_BUFFER (startpos));
3804
3805 SETUP_SYNTAX_TABLE_FOR_OBJECT (re_match_object, charpos, 1);
3806 }
3807 #endif
3808
3809 /* Loop through the string, looking for a place to start matching. */
3810 for (;;)
3811 {
3812 /* If the pattern is anchored,
3813 skip quickly past places we cannot match.
3814 We don't bother to treat startpos == 0 specially
3815 because that case doesn't repeat. */
3816 if (anchored_start && startpos > 0)
3817 {
3818 if (! (bufp->newline_anchor
3819 && ((startpos <= size1 ? string1[startpos - 1]
3820 : string2[startpos - size1 - 1])
3821 == '\n')))
3822 goto advance;
3823 }
3824
3825 /* If a fastmap is supplied, skip quickly over characters that
3826 cannot be the start of a match. If the pattern can match the
3827 null string, however, we don't need to skip characters; we want
3828 the first null string. */
3829 if (fastmap && startpos < total_size && !bufp->can_be_null)
3830 {
3831 register re_char *d;
3832 register unsigned int buf_ch;
3833
3834 d = POS_ADDR_VSTRING (startpos);
3835
3836 if (range > 0) /* Searching forwards. */
3837 {
3838 register int lim = 0;
3839 int irange = range;
3840
3841 if (startpos < size1 && startpos + range >= size1)
3842 lim = range - (size1 - startpos);
3843
3844 /* Written out as an if-else to avoid testing `translate'
3845 inside the loop. */
3846 if (RE_TRANSLATE_P (translate))
3847 {
3848 if (multibyte)
3849 while (range > lim)
3850 {
3851 int buf_charlen;
3852
3853 buf_ch = STRING_CHAR_AND_LENGTH (d, range - lim,
3854 buf_charlen);
3855
3856 buf_ch = RE_TRANSLATE (translate, buf_ch);
3857 if (buf_ch >= 0400
3858 || fastmap[buf_ch])
3859 break;
3860
3861 range -= buf_charlen;
3862 d += buf_charlen;
3863 }
3864 else
3865 while (range > lim
3866 && !fastmap[RE_TRANSLATE (translate, *d)])
3867 {
3868 d++;
3869 range--;
3870 }
3871 }
3872 else
3873 while (range > lim && !fastmap[*d])
3874 {
3875 d++;
3876 range--;
3877 }
3878
3879 startpos += irange - range;
3880 }
3881 else /* Searching backwards. */
3882 {
3883 int room = (startpos >= size1
3884 ? size2 + size1 - startpos
3885 : size1 - startpos);
3886 buf_ch = RE_STRING_CHAR (d, room);
3887 buf_ch = TRANSLATE (buf_ch);
3888
3889 if (! (buf_ch >= 0400
3890 || fastmap[buf_ch]))
3891 goto advance;
3892 }
3893 }
3894
3895 /* If can't match the null string, and that's all we have left, fail. */
3896 if (range >= 0 && startpos == total_size && fastmap
3897 && !bufp->can_be_null)
3898 return -1;
3899
3900 val = re_match_2_internal (bufp, string1, size1, string2, size2,
3901 startpos, regs, stop);
3902 #ifndef REGEX_MALLOC
3903 #ifdef C_ALLOCA
3904 alloca (0);
3905 #endif
3906 #endif
3907
3908 if (val >= 0)
3909 return startpos;
3910
3911 if (val == -2)
3912 return -2;
3913
3914 advance:
3915 if (!range)
3916 break;
3917 else if (range > 0)
3918 {
3919 /* Update STARTPOS to the next character boundary. */
3920 if (multibyte)
3921 {
3922 re_char *p = POS_ADDR_VSTRING (startpos);
3923 re_char *pend = STOP_ADDR_VSTRING (startpos);
3924 int len = MULTIBYTE_FORM_LENGTH (p, pend - p);
3925
3926 range -= len;
3927 if (range < 0)
3928 break;
3929 startpos += len;
3930 }
3931 else
3932 {
3933 range--;
3934 startpos++;
3935 }
3936 }
3937 else
3938 {
3939 range++;
3940 startpos--;
3941
3942 /* Update STARTPOS to the previous character boundary. */
3943 if (multibyte)
3944 {
3945 re_char *p = POS_ADDR_VSTRING (startpos);
3946 int len = 0;
3947
3948 /* Find the head of multibyte form. */
3949 while (!CHAR_HEAD_P (*p))
3950 p--, len++;
3951
3952 /* Adjust it. */
3953 #if 0 /* XXX */
3954 if (MULTIBYTE_FORM_LENGTH (p, len + 1) != (len + 1))
3955 ;
3956 else
3957 #endif
3958 {
3959 range += len;
3960 if (range > 0)
3961 break;
3962
3963 startpos -= len;
3964 }
3965 }
3966 }
3967 }
3968 return -1;
3969 } /* re_search_2 */
3970 \f
3971 /* Declarations and macros for re_match_2. */
3972
3973 static int bcmp_translate _RE_ARGS((re_char *s1, re_char *s2,
3974 register int len,
3975 RE_TRANSLATE_TYPE translate,
3976 const int multibyte));
3977
3978 /* This converts PTR, a pointer into one of the search strings `string1'
3979 and `string2' into an offset from the beginning of that string. */
3980 #define POINTER_TO_OFFSET(ptr) \
3981 (FIRST_STRING_P (ptr) \
3982 ? ((regoff_t) ((ptr) - string1)) \
3983 : ((regoff_t) ((ptr) - string2 + size1)))
3984
3985 /* Call before fetching a character with *d. This switches over to
3986 string2 if necessary.
3987 Check re_match_2_internal for a discussion of why end_match_2 might
3988 not be within string2 (but be equal to end_match_1 instead). */
3989 #define PREFETCH() \
3990 while (d == dend) \
3991 { \
3992 /* End of string2 => fail. */ \
3993 if (dend == end_match_2) \
3994 goto fail; \
3995 /* End of string1 => advance to string2. */ \
3996 d = string2; \
3997 dend = end_match_2; \
3998 }
3999
4000 /* Call before fetching a char with *d if you already checked other limits.
4001 This is meant for use in lookahead operations like wordend, etc..
4002 where we might need to look at parts of the string that might be
4003 outside of the LIMITs (i.e past `stop'). */
4004 #define PREFETCH_NOLIMIT() \
4005 if (d == end1) \
4006 { \
4007 d = string2; \
4008 dend = end_match_2; \
4009 } \
4010
4011 /* Test if at very beginning or at very end of the virtual concatenation
4012 of `string1' and `string2'. If only one string, it's `string2'. */
4013 #define AT_STRINGS_BEG(d) ((d) == (size1 ? string1 : string2) || !size2)
4014 #define AT_STRINGS_END(d) ((d) == end2)
4015
4016
4017 /* Test if D points to a character which is word-constituent. We have
4018 two special cases to check for: if past the end of string1, look at
4019 the first character in string2; and if before the beginning of
4020 string2, look at the last character in string1. */
4021 #define WORDCHAR_P(d) \
4022 (SYNTAX ((d) == end1 ? *string2 \
4023 : (d) == string2 - 1 ? *(end1 - 1) : *(d)) \
4024 == Sword)
4025
4026 /* Disabled due to a compiler bug -- see comment at case wordbound */
4027
4028 /* The comment at case wordbound is following one, but we don't use
4029 AT_WORD_BOUNDARY anymore to support multibyte form.
4030
4031 The DEC Alpha C compiler 3.x generates incorrect code for the
4032 test WORDCHAR_P (d - 1) != WORDCHAR_P (d) in the expansion of
4033 AT_WORD_BOUNDARY, so this code is disabled. Expanding the
4034 macro and introducing temporary variables works around the bug. */
4035
4036 #if 0
4037 /* Test if the character before D and the one at D differ with respect
4038 to being word-constituent. */
4039 #define AT_WORD_BOUNDARY(d) \
4040 (AT_STRINGS_BEG (d) || AT_STRINGS_END (d) \
4041 || WORDCHAR_P (d - 1) != WORDCHAR_P (d))
4042 #endif
4043
4044 /* Free everything we malloc. */
4045 #ifdef MATCH_MAY_ALLOCATE
4046 #define FREE_VAR(var) if (var) { REGEX_FREE (var); var = NULL; } else
4047 #define FREE_VARIABLES() \
4048 do { \
4049 REGEX_FREE_STACK (fail_stack.stack); \
4050 FREE_VAR (regstart); \
4051 FREE_VAR (regend); \
4052 FREE_VAR (best_regstart); \
4053 FREE_VAR (best_regend); \
4054 } while (0)
4055 #else
4056 #define FREE_VARIABLES() ((void)0) /* Do nothing! But inhibit gcc warning. */
4057 #endif /* not MATCH_MAY_ALLOCATE */
4058
4059 \f
4060 /* Optimization routines. */
4061
4062 /* If the operation is a match against one or more chars,
4063 return a pointer to the next operation, else return NULL. */
4064 static unsigned char *
4065 skip_one_char (p)
4066 unsigned char *p;
4067 {
4068 switch (SWITCH_ENUM_CAST (*p++))
4069 {
4070 case anychar:
4071 break;
4072
4073 case exactn:
4074 p += *p + 1;
4075 break;
4076
4077 case charset_not:
4078 case charset:
4079 if (CHARSET_RANGE_TABLE_EXISTS_P (p - 1))
4080 {
4081 int mcnt;
4082 p = CHARSET_RANGE_TABLE (p - 1);
4083 EXTRACT_NUMBER_AND_INCR (mcnt, p);
4084 p = CHARSET_RANGE_TABLE_END (p, mcnt);
4085 }
4086 else
4087 p += 1 + CHARSET_BITMAP_SIZE (p - 1);
4088 break;
4089
4090 case syntaxspec:
4091 case notsyntaxspec:
4092 #ifdef emacs
4093 case categoryspec:
4094 case notcategoryspec:
4095 #endif /* emacs */
4096 p++;
4097 break;
4098
4099 default:
4100 p = NULL;
4101 }
4102 return p;
4103 }
4104
4105
4106 /* Jump over non-matching operations. */
4107 static unsigned char *
4108 skip_noops (p, pend)
4109 unsigned char *p, *pend;
4110 {
4111 int mcnt;
4112 while (p < pend)
4113 {
4114 switch (SWITCH_ENUM_CAST ((re_opcode_t) *p))
4115 {
4116 case start_memory:
4117 case stop_memory:
4118 p += 2; break;
4119 case no_op:
4120 p += 1; break;
4121 case jump:
4122 p += 1;
4123 EXTRACT_NUMBER_AND_INCR (mcnt, p);
4124 p += mcnt;
4125 break;
4126 default:
4127 return p;
4128 }
4129 }
4130 assert (p == pend);
4131 return p;
4132 }
4133
4134 /* Non-zero if "p1 matches something" implies "p2 fails". */
4135 static int
4136 mutually_exclusive_p (bufp, p1, p2)
4137 struct re_pattern_buffer *bufp;
4138 unsigned char *p1, *p2;
4139 {
4140 re_opcode_t op2;
4141 const boolean multibyte = RE_MULTIBYTE_P (bufp);
4142 unsigned char *pend = bufp->buffer + bufp->used;
4143
4144 assert (p1 >= bufp->buffer && p1 < pend
4145 && p2 >= bufp->buffer && p2 <= pend);
4146
4147 /* Skip over open/close-group commands.
4148 If what follows this loop is a ...+ construct,
4149 look at what begins its body, since we will have to
4150 match at least one of that. */
4151 p2 = skip_noops (p2, pend);
4152 /* The same skip can be done for p1, except that this function
4153 is only used in the case where p1 is a simple match operator. */
4154 /* p1 = skip_noops (p1, pend); */
4155
4156 assert (p1 >= bufp->buffer && p1 < pend
4157 && p2 >= bufp->buffer && p2 <= pend);
4158
4159 op2 = p2 == pend ? succeed : *p2;
4160
4161 switch (SWITCH_ENUM_CAST (op2))
4162 {
4163 case succeed:
4164 case endbuf:
4165 /* If we're at the end of the pattern, we can change. */
4166 if (skip_one_char (p1))
4167 {
4168 DEBUG_PRINT1 (" End of pattern: fast loop.\n");
4169 return 1;
4170 }
4171 break;
4172
4173 case endline:
4174 if (!bufp->newline_anchor)
4175 break;
4176 /* Fallthrough */
4177 case exactn:
4178 {
4179 register unsigned int c
4180 = (re_opcode_t) *p2 == endline ? '\n'
4181 : RE_STRING_CHAR(p2 + 2, pend - p2 - 2);
4182
4183 if ((re_opcode_t) *p1 == exactn)
4184 {
4185 if (c != RE_STRING_CHAR (p1 + 2, pend - p1 - 2))
4186 {
4187 DEBUG_PRINT3 (" '%c' != '%c' => fast loop.\n", c, p1[2]);
4188 return 1;
4189 }
4190 }
4191
4192 else if ((re_opcode_t) *p1 == charset
4193 || (re_opcode_t) *p1 == charset_not)
4194 {
4195 int not = (re_opcode_t) *p1 == charset_not;
4196
4197 /* Test if C is listed in charset (or charset_not)
4198 at `p1'. */
4199 if (SINGLE_BYTE_CHAR_P (c))
4200 {
4201 if (c < CHARSET_BITMAP_SIZE (p1) * BYTEWIDTH
4202 && p1[2 + c / BYTEWIDTH] & (1 << (c % BYTEWIDTH)))
4203 not = !not;
4204 }
4205 else if (CHARSET_RANGE_TABLE_EXISTS_P (p1))
4206 CHARSET_LOOKUP_RANGE_TABLE (not, c, p1);
4207
4208 /* `not' is equal to 1 if c would match, which means
4209 that we can't change to pop_failure_jump. */
4210 if (!not)
4211 {
4212 DEBUG_PRINT1 (" No match => fast loop.\n");
4213 return 1;
4214 }
4215 }
4216 else if ((re_opcode_t) *p1 == anychar
4217 && c == '\n')
4218 {
4219 DEBUG_PRINT1 (" . != \\n => fast loop.\n");
4220 return 1;
4221 }
4222 }
4223 break;
4224
4225 case charset:
4226 case charset_not:
4227 {
4228 if ((re_opcode_t) *p1 == exactn)
4229 /* Reuse the code above. */
4230 return mutually_exclusive_p (bufp, p2, p1);
4231
4232
4233 /* It is hard to list up all the character in charset
4234 P2 if it includes multibyte character. Give up in
4235 such case. */
4236 else if (!multibyte || !CHARSET_RANGE_TABLE_EXISTS_P (p2))
4237 {
4238 /* Now, we are sure that P2 has no range table.
4239 So, for the size of bitmap in P2, `p2[1]' is
4240 enough. But P1 may have range table, so the
4241 size of bitmap table of P1 is extracted by
4242 using macro `CHARSET_BITMAP_SIZE'.
4243
4244 Since we know that all the character listed in
4245 P2 is ASCII, it is enough to test only bitmap
4246 table of P1. */
4247
4248 if (*p1 == *p2)
4249 {
4250 int idx;
4251 /* We win if the charset inside the loop
4252 has no overlap with the one after the loop. */
4253 for (idx = 0;
4254 (idx < (int) p2[1]
4255 && idx < CHARSET_BITMAP_SIZE (p1));
4256 idx++)
4257 if ((p2[2 + idx] & p1[2 + idx]) != 0)
4258 break;
4259
4260 if (idx == p2[1]
4261 || idx == CHARSET_BITMAP_SIZE (p1))
4262 {
4263 DEBUG_PRINT1 (" No match => fast loop.\n");
4264 return 1;
4265 }
4266 }
4267 else if ((re_opcode_t) *p1 == charset
4268 || (re_opcode_t) *p1 == charset_not)
4269 {
4270 int idx;
4271 /* We win if the charset_not inside the loop lists
4272 every character listed in the charset after. */
4273 for (idx = 0; idx < (int) p2[1]; idx++)
4274 if (! (p2[2 + idx] == 0
4275 || (idx < CHARSET_BITMAP_SIZE (p1)
4276 && ((p2[2 + idx] & ~ p1[2 + idx]) == 0))))
4277 break;
4278
4279 if (idx == p2[1])
4280 {
4281 DEBUG_PRINT1 (" No match => fast loop.\n");
4282 return 1;
4283 }
4284 }
4285 }
4286 }
4287
4288 case wordend:
4289 case notsyntaxspec:
4290 return ((re_opcode_t) *p1 == syntaxspec
4291 && p1[1] == (op2 == wordend ? Sword : p2[1]));
4292
4293 case wordbeg:
4294 case syntaxspec:
4295 return ((re_opcode_t) *p1 == notsyntaxspec
4296 && p1[1] == (op2 == wordend ? Sword : p2[1]));
4297
4298 case wordbound:
4299 return (((re_opcode_t) *p1 == notsyntaxspec
4300 || (re_opcode_t) *p1 == syntaxspec)
4301 && p1[1] == Sword);
4302
4303 #ifdef emacs
4304 case categoryspec:
4305 return ((re_opcode_t) *p1 == notcategoryspec && p1[1] == p2[1]);
4306 case notcategoryspec:
4307 return ((re_opcode_t) *p1 == categoryspec && p1[1] == p2[1]);
4308 #endif /* emacs */
4309
4310 default:
4311 ;
4312 }
4313
4314 /* Safe default. */
4315 return 0;
4316 }
4317
4318 \f
4319 /* Matching routines. */
4320
4321 #ifndef emacs /* Emacs never uses this. */
4322 /* re_match is like re_match_2 except it takes only a single string. */
4323
4324 int
4325 re_match (bufp, string, size, pos, regs)
4326 struct re_pattern_buffer *bufp;
4327 const char *string;
4328 int size, pos;
4329 struct re_registers *regs;
4330 {
4331 int result = re_match_2_internal (bufp, NULL, 0, string, size,
4332 pos, regs, size);
4333 #if defined (C_ALLOCA) && !defined (REGEX_MALLOC)
4334 alloca (0);
4335 #endif
4336 return result;
4337 }
4338 #endif /* not emacs */
4339
4340 #ifdef emacs
4341 /* In Emacs, this is the string or buffer in which we
4342 are matching. It is used for looking up syntax properties. */
4343 Lisp_Object re_match_object;
4344 #endif
4345
4346 /* re_match_2 matches the compiled pattern in BUFP against the
4347 the (virtual) concatenation of STRING1 and STRING2 (of length SIZE1
4348 and SIZE2, respectively). We start matching at POS, and stop
4349 matching at STOP.
4350
4351 If REGS is non-null and the `no_sub' field of BUFP is nonzero, we
4352 store offsets for the substring each group matched in REGS. See the
4353 documentation for exactly how many groups we fill.
4354
4355 We return -1 if no match, -2 if an internal error (such as the
4356 failure stack overflowing). Otherwise, we return the length of the
4357 matched substring. */
4358
4359 int
4360 re_match_2 (bufp, string1, size1, string2, size2, pos, regs, stop)
4361 struct re_pattern_buffer *bufp;
4362 const char *string1, *string2;
4363 int size1, size2;
4364 int pos;
4365 struct re_registers *regs;
4366 int stop;
4367 {
4368 int result;
4369
4370 #ifdef emacs
4371 int charpos;
4372 gl_state.object = re_match_object;
4373 charpos = SYNTAX_TABLE_BYTE_TO_CHAR (POS_AS_IN_BUFFER (pos));
4374 SETUP_SYNTAX_TABLE_FOR_OBJECT (re_match_object, charpos, 1);
4375 #endif
4376
4377 result = re_match_2_internal (bufp, string1, size1, string2, size2,
4378 pos, regs, stop);
4379 #if defined (C_ALLOCA) && !defined (REGEX_MALLOC)
4380 alloca (0);
4381 #endif
4382 return result;
4383 }
4384
4385 /* This is a separate function so that we can force an alloca cleanup
4386 afterwards. */
4387 static int
4388 re_match_2_internal (bufp, string1, size1, string2, size2, pos, regs, stop)
4389 struct re_pattern_buffer *bufp;
4390 re_char *string1, *string2;
4391 int size1, size2;
4392 int pos;
4393 struct re_registers *regs;
4394 int stop;
4395 {
4396 /* General temporaries. */
4397 int mcnt;
4398 boolean not;
4399 unsigned char *p1;
4400
4401 /* Just past the end of the corresponding string. */
4402 re_char *end1, *end2;
4403
4404 /* Pointers into string1 and string2, just past the last characters in
4405 each to consider matching. */
4406 re_char *end_match_1, *end_match_2;
4407
4408 /* Where we are in the data, and the end of the current string. */
4409 re_char *d, *dend;
4410
4411 /* Used sometimes to remember where we were before starting matching
4412 an operator so that we can go back in case of failure. This "atomic"
4413 behavior of matching opcodes is indispensable to the correctness
4414 of the on_failure_keep_string_jump optimization. */
4415 re_char *dfail;
4416
4417 /* Where we are in the pattern, and the end of the pattern. */
4418 unsigned char *p = bufp->buffer;
4419 register unsigned char *pend = p + bufp->used;
4420
4421 /* We use this to map every character in the string. */
4422 RE_TRANSLATE_TYPE translate = bufp->translate;
4423
4424 /* Nonzero if we have to concern multibyte character. */
4425 const boolean multibyte = RE_MULTIBYTE_P (bufp);
4426
4427 /* Failure point stack. Each place that can handle a failure further
4428 down the line pushes a failure point on this stack. It consists of
4429 regstart, and regend for all registers corresponding to
4430 the subexpressions we're currently inside, plus the number of such
4431 registers, and, finally, two char *'s. The first char * is where
4432 to resume scanning the pattern; the second one is where to resume
4433 scanning the strings. */
4434 #ifdef MATCH_MAY_ALLOCATE /* otherwise, this is global. */
4435 fail_stack_type fail_stack;
4436 #endif
4437 #ifdef DEBUG
4438 static unsigned failure_id = 0;
4439 unsigned nfailure_points_pushed = 0, nfailure_points_popped = 0;
4440 #endif
4441
4442 #if defined (REL_ALLOC) && defined (REGEX_MALLOC)
4443 /* This holds the pointer to the failure stack, when
4444 it is allocated relocatably. */
4445 fail_stack_elt_t *failure_stack_ptr;
4446 #endif
4447
4448 /* We fill all the registers internally, independent of what we
4449 return, for use in backreferences. The number here includes
4450 an element for register zero. */
4451 unsigned num_regs = bufp->re_nsub + 1;
4452
4453 /* Information on the contents of registers. These are pointers into
4454 the input strings; they record just what was matched (on this
4455 attempt) by a subexpression part of the pattern, that is, the
4456 regnum-th regstart pointer points to where in the pattern we began
4457 matching and the regnum-th regend points to right after where we
4458 stopped matching the regnum-th subexpression. (The zeroth register
4459 keeps track of what the whole pattern matches.) */
4460 #ifdef MATCH_MAY_ALLOCATE /* otherwise, these are global. */
4461 re_char **regstart, **regend;
4462 #endif
4463
4464 /* The following record the register info as found in the above
4465 variables when we find a match better than any we've seen before.
4466 This happens as we backtrack through the failure points, which in
4467 turn happens only if we have not yet matched the entire string. */
4468 unsigned best_regs_set = false;
4469 #ifdef MATCH_MAY_ALLOCATE /* otherwise, these are global. */
4470 re_char **best_regstart, **best_regend;
4471 #endif
4472
4473 /* Logically, this is `best_regend[0]'. But we don't want to have to
4474 allocate space for that if we're not allocating space for anything
4475 else (see below). Also, we never need info about register 0 for
4476 any of the other register vectors, and it seems rather a kludge to
4477 treat `best_regend' differently than the rest. So we keep track of
4478 the end of the best match so far in a separate variable. We
4479 initialize this to NULL so that when we backtrack the first time
4480 and need to test it, it's not garbage. */
4481 re_char *match_end = NULL;
4482
4483 #ifdef DEBUG
4484 /* Counts the total number of registers pushed. */
4485 unsigned num_regs_pushed = 0;
4486 #endif
4487
4488 DEBUG_PRINT1 ("\n\nEntering re_match_2.\n");
4489
4490 INIT_FAIL_STACK ();
4491
4492 #ifdef MATCH_MAY_ALLOCATE
4493 /* Do not bother to initialize all the register variables if there are
4494 no groups in the pattern, as it takes a fair amount of time. If
4495 there are groups, we include space for register 0 (the whole
4496 pattern), even though we never use it, since it simplifies the
4497 array indexing. We should fix this. */
4498 if (bufp->re_nsub)
4499 {
4500 regstart = REGEX_TALLOC (num_regs, re_char *);
4501 regend = REGEX_TALLOC (num_regs, re_char *);
4502 best_regstart = REGEX_TALLOC (num_regs, re_char *);
4503 best_regend = REGEX_TALLOC (num_regs, re_char *);
4504
4505 if (!(regstart && regend && best_regstart && best_regend))
4506 {
4507 FREE_VARIABLES ();
4508 return -2;
4509 }
4510 }
4511 else
4512 {
4513 /* We must initialize all our variables to NULL, so that
4514 `FREE_VARIABLES' doesn't try to free them. */
4515 regstart = regend = best_regstart = best_regend = NULL;
4516 }
4517 #endif /* MATCH_MAY_ALLOCATE */
4518
4519 /* The starting position is bogus. */
4520 if (pos < 0 || pos > size1 + size2)
4521 {
4522 FREE_VARIABLES ();
4523 return -1;
4524 }
4525
4526 /* Initialize subexpression text positions to -1 to mark ones that no
4527 start_memory/stop_memory has been seen for. Also initialize the
4528 register information struct. */
4529 for (mcnt = 1; mcnt < num_regs; mcnt++)
4530 regstart[mcnt] = regend[mcnt] = REG_UNSET_VALUE;
4531
4532 /* We move `string1' into `string2' if the latter's empty -- but not if
4533 `string1' is null. */
4534 if (size2 == 0 && string1 != NULL)
4535 {
4536 string2 = string1;
4537 size2 = size1;
4538 string1 = 0;
4539 size1 = 0;
4540 }
4541 end1 = string1 + size1;
4542 end2 = string2 + size2;
4543
4544 /* `p' scans through the pattern as `d' scans through the data.
4545 `dend' is the end of the input string that `d' points within. `d'
4546 is advanced into the following input string whenever necessary, but
4547 this happens before fetching; therefore, at the beginning of the
4548 loop, `d' can be pointing at the end of a string, but it cannot
4549 equal `string2'. */
4550 if (pos >= size1)
4551 {
4552 /* Only match within string2. */
4553 d = string2 + pos - size1;
4554 dend = end_match_2 = string2 + stop - size1;
4555 end_match_1 = end1; /* Just to give it a value. */
4556 }
4557 else
4558 {
4559 if (stop < size1)
4560 {
4561 /* Only match within string1. */
4562 end_match_1 = string1 + stop;
4563 /* BEWARE!
4564 When we reach end_match_1, PREFETCH normally switches to string2.
4565 But in the present case, this means that just doing a PREFETCH
4566 makes us jump from `stop' to `gap' within the string.
4567 What we really want here is for the search to stop as
4568 soon as we hit end_match_1. That's why we set end_match_2
4569 to end_match_1 (since PREFETCH fails as soon as we hit
4570 end_match_2). */
4571 end_match_2 = end_match_1;
4572 }
4573 else
4574 { /* It's important to use this code when stop == size so that
4575 moving `d' from end1 to string2 will not prevent the d == dend
4576 check from catching the end of string. */
4577 end_match_1 = end1;
4578 end_match_2 = string2 + stop - size1;
4579 }
4580 d = string1 + pos;
4581 dend = end_match_1;
4582 }
4583
4584 DEBUG_PRINT1 ("The compiled pattern is: ");
4585 DEBUG_PRINT_COMPILED_PATTERN (bufp, p, pend);
4586 DEBUG_PRINT1 ("The string to match is: `");
4587 DEBUG_PRINT_DOUBLE_STRING (d, string1, size1, string2, size2);
4588 DEBUG_PRINT1 ("'\n");
4589
4590 /* This loops over pattern commands. It exits by returning from the
4591 function if the match is complete, or it drops through if the match
4592 fails at this starting point in the input data. */
4593 for (;;)
4594 {
4595 DEBUG_PRINT2 ("\n%p: ", p);
4596
4597 if (p == pend)
4598 { /* End of pattern means we might have succeeded. */
4599 DEBUG_PRINT1 ("end of pattern ... ");
4600
4601 /* If we haven't matched the entire string, and we want the
4602 longest match, try backtracking. */
4603 if (d != end_match_2)
4604 {
4605 /* 1 if this match ends in the same string (string1 or string2)
4606 as the best previous match. */
4607 boolean same_str_p = (FIRST_STRING_P (match_end)
4608 == FIRST_STRING_P (d));
4609 /* 1 if this match is the best seen so far. */
4610 boolean best_match_p;
4611
4612 /* AIX compiler got confused when this was combined
4613 with the previous declaration. */
4614 if (same_str_p)
4615 best_match_p = d > match_end;
4616 else
4617 best_match_p = !FIRST_STRING_P (d);
4618
4619 DEBUG_PRINT1 ("backtracking.\n");
4620
4621 if (!FAIL_STACK_EMPTY ())
4622 { /* More failure points to try. */
4623
4624 /* If exceeds best match so far, save it. */
4625 if (!best_regs_set || best_match_p)
4626 {
4627 best_regs_set = true;
4628 match_end = d;
4629
4630 DEBUG_PRINT1 ("\nSAVING match as best so far.\n");
4631
4632 for (mcnt = 1; mcnt < num_regs; mcnt++)
4633 {
4634 best_regstart[mcnt] = regstart[mcnt];
4635 best_regend[mcnt] = regend[mcnt];
4636 }
4637 }
4638 goto fail;
4639 }
4640
4641 /* If no failure points, don't restore garbage. And if
4642 last match is real best match, don't restore second
4643 best one. */
4644 else if (best_regs_set && !best_match_p)
4645 {
4646 restore_best_regs:
4647 /* Restore best match. It may happen that `dend ==
4648 end_match_1' while the restored d is in string2.
4649 For example, the pattern `x.*y.*z' against the
4650 strings `x-' and `y-z-', if the two strings are
4651 not consecutive in memory. */
4652 DEBUG_PRINT1 ("Restoring best registers.\n");
4653
4654 d = match_end;
4655 dend = ((d >= string1 && d <= end1)
4656 ? end_match_1 : end_match_2);
4657
4658 for (mcnt = 1; mcnt < num_regs; mcnt++)
4659 {
4660 regstart[mcnt] = best_regstart[mcnt];
4661 regend[mcnt] = best_regend[mcnt];
4662 }
4663 }
4664 } /* d != end_match_2 */
4665
4666 succeed_label:
4667 DEBUG_PRINT1 ("Accepting match.\n");
4668
4669 /* If caller wants register contents data back, do it. */
4670 if (regs && !bufp->no_sub)
4671 {
4672 /* Have the register data arrays been allocated? */
4673 if (bufp->regs_allocated == REGS_UNALLOCATED)
4674 { /* No. So allocate them with malloc. We need one
4675 extra element beyond `num_regs' for the `-1' marker
4676 GNU code uses. */
4677 regs->num_regs = MAX (RE_NREGS, num_regs + 1);
4678 regs->start = TALLOC (regs->num_regs, regoff_t);
4679 regs->end = TALLOC (regs->num_regs, regoff_t);
4680 if (regs->start == NULL || regs->end == NULL)
4681 {
4682 FREE_VARIABLES ();
4683 return -2;
4684 }
4685 bufp->regs_allocated = REGS_REALLOCATE;
4686 }
4687 else if (bufp->regs_allocated == REGS_REALLOCATE)
4688 { /* Yes. If we need more elements than were already
4689 allocated, reallocate them. If we need fewer, just
4690 leave it alone. */
4691 if (regs->num_regs < num_regs + 1)
4692 {
4693 regs->num_regs = num_regs + 1;
4694 RETALLOC (regs->start, regs->num_regs, regoff_t);
4695 RETALLOC (regs->end, regs->num_regs, regoff_t);
4696 if (regs->start == NULL || regs->end == NULL)
4697 {
4698 FREE_VARIABLES ();
4699 return -2;
4700 }
4701 }
4702 }
4703 else
4704 {
4705 /* These braces fend off a "empty body in an else-statement"
4706 warning under GCC when assert expands to nothing. */
4707 assert (bufp->regs_allocated == REGS_FIXED);
4708 }
4709
4710 /* Convert the pointer data in `regstart' and `regend' to
4711 indices. Register zero has to be set differently,
4712 since we haven't kept track of any info for it. */
4713 if (regs->num_regs > 0)
4714 {
4715 regs->start[0] = pos;
4716 regs->end[0] = POINTER_TO_OFFSET (d);
4717 }
4718
4719 /* Go through the first `min (num_regs, regs->num_regs)'
4720 registers, since that is all we initialized. */
4721 for (mcnt = 1; mcnt < MIN (num_regs, regs->num_regs); mcnt++)
4722 {
4723 if (REG_UNSET (regstart[mcnt]) || REG_UNSET (regend[mcnt]))
4724 regs->start[mcnt] = regs->end[mcnt] = -1;
4725 else
4726 {
4727 regs->start[mcnt]
4728 = (regoff_t) POINTER_TO_OFFSET (regstart[mcnt]);
4729 regs->end[mcnt]
4730 = (regoff_t) POINTER_TO_OFFSET (regend[mcnt]);
4731 }
4732 }
4733
4734 /* If the regs structure we return has more elements than
4735 were in the pattern, set the extra elements to -1. If
4736 we (re)allocated the registers, this is the case,
4737 because we always allocate enough to have at least one
4738 -1 at the end. */
4739 for (mcnt = num_regs; mcnt < regs->num_regs; mcnt++)
4740 regs->start[mcnt] = regs->end[mcnt] = -1;
4741 } /* regs && !bufp->no_sub */
4742
4743 DEBUG_PRINT4 ("%u failure points pushed, %u popped (%u remain).\n",
4744 nfailure_points_pushed, nfailure_points_popped,
4745 nfailure_points_pushed - nfailure_points_popped);
4746 DEBUG_PRINT2 ("%u registers pushed.\n", num_regs_pushed);
4747
4748 mcnt = POINTER_TO_OFFSET (d) - pos;
4749
4750 DEBUG_PRINT2 ("Returning %d from re_match_2.\n", mcnt);
4751
4752 FREE_VARIABLES ();
4753 return mcnt;
4754 }
4755
4756 /* Otherwise match next pattern command. */
4757 switch (SWITCH_ENUM_CAST ((re_opcode_t) *p++))
4758 {
4759 /* Ignore these. Used to ignore the n of succeed_n's which
4760 currently have n == 0. */
4761 case no_op:
4762 DEBUG_PRINT1 ("EXECUTING no_op.\n");
4763 break;
4764
4765 case succeed:
4766 DEBUG_PRINT1 ("EXECUTING succeed.\n");
4767 goto succeed_label;
4768
4769 /* Match the next n pattern characters exactly. The following
4770 byte in the pattern defines n, and the n bytes after that
4771 are the characters to match. */
4772 case exactn:
4773 mcnt = *p++;
4774 DEBUG_PRINT2 ("EXECUTING exactn %d.\n", mcnt);
4775
4776 /* Remember the start point to rollback upon failure. */
4777 dfail = d;
4778
4779 /* This is written out as an if-else so we don't waste time
4780 testing `translate' inside the loop. */
4781 if (RE_TRANSLATE_P (translate))
4782 {
4783 if (multibyte)
4784 do
4785 {
4786 int pat_charlen, buf_charlen;
4787 unsigned int pat_ch, buf_ch;
4788
4789 PREFETCH ();
4790 pat_ch = STRING_CHAR_AND_LENGTH (p, pend - p, pat_charlen);
4791 buf_ch = STRING_CHAR_AND_LENGTH (d, dend - d, buf_charlen);
4792
4793 if (RE_TRANSLATE (translate, buf_ch)
4794 != pat_ch)
4795 {
4796 d = dfail;
4797 goto fail;
4798 }
4799
4800 p += pat_charlen;
4801 d += buf_charlen;
4802 mcnt -= pat_charlen;
4803 }
4804 while (mcnt > 0);
4805 else
4806 do
4807 {
4808 PREFETCH ();
4809 if (RE_TRANSLATE (translate, *d) != *p++)
4810 {
4811 d = dfail;
4812 goto fail;
4813 }
4814 d++;
4815 }
4816 while (--mcnt);
4817 }
4818 else
4819 {
4820 do
4821 {
4822 PREFETCH ();
4823 if (*d++ != *p++)
4824 {
4825 d = dfail;
4826 goto fail;
4827 }
4828 }
4829 while (--mcnt);
4830 }
4831 break;
4832
4833
4834 /* Match any character except possibly a newline or a null. */
4835 case anychar:
4836 {
4837 int buf_charlen;
4838 unsigned int buf_ch;
4839
4840 DEBUG_PRINT1 ("EXECUTING anychar.\n");
4841
4842 PREFETCH ();
4843 buf_ch = RE_STRING_CHAR_AND_LENGTH (d, dend - d, buf_charlen);
4844 buf_ch = TRANSLATE (buf_ch);
4845
4846 if ((!(bufp->syntax & RE_DOT_NEWLINE)
4847 && buf_ch == '\n')
4848 || ((bufp->syntax & RE_DOT_NOT_NULL)
4849 && buf_ch == '\000'))
4850 goto fail;
4851
4852 DEBUG_PRINT2 (" Matched `%d'.\n", *d);
4853 d += buf_charlen;
4854 }
4855 break;
4856
4857
4858 case charset:
4859 case charset_not:
4860 {
4861 register unsigned int c;
4862 boolean not = (re_opcode_t) *(p - 1) == charset_not;
4863 int len;
4864
4865 /* Start of actual range_table, or end of bitmap if there is no
4866 range table. */
4867 unsigned char *range_table;
4868
4869 /* Nonzero if there is a range table. */
4870 int range_table_exists;
4871
4872 /* Number of ranges of range table. This is not included
4873 in the initial byte-length of the command. */
4874 int count = 0;
4875
4876 DEBUG_PRINT2 ("EXECUTING charset%s.\n", not ? "_not" : "");
4877
4878 range_table_exists = CHARSET_RANGE_TABLE_EXISTS_P (&p[-1]);
4879
4880 if (range_table_exists)
4881 {
4882 range_table = CHARSET_RANGE_TABLE (&p[-1]); /* Past the bitmap. */
4883 EXTRACT_NUMBER_AND_INCR (count, range_table);
4884 }
4885
4886 PREFETCH ();
4887 c = RE_STRING_CHAR_AND_LENGTH (d, dend - d, len);
4888 c = TRANSLATE (c); /* The character to match. */
4889
4890 if (SINGLE_BYTE_CHAR_P (c))
4891 { /* Lookup bitmap. */
4892 /* Cast to `unsigned' instead of `unsigned char' in
4893 case the bit list is a full 32 bytes long. */
4894 if (c < (unsigned) (CHARSET_BITMAP_SIZE (&p[-1]) * BYTEWIDTH)
4895 && p[1 + c / BYTEWIDTH] & (1 << (c % BYTEWIDTH)))
4896 not = !not;
4897 }
4898 #ifdef emacs
4899 else if (range_table_exists)
4900 {
4901 int class_bits = CHARSET_RANGE_TABLE_BITS (&p[-1]);
4902
4903 if ( (class_bits & BIT_ALNUM && ISALNUM (c))
4904 | (class_bits & BIT_ALPHA && ISALPHA (c))
4905 | (class_bits & BIT_ASCII && IS_REAL_ASCII (c))
4906 | (class_bits & BIT_GRAPH && ISGRAPH (c))
4907 | (class_bits & BIT_LOWER && ISLOWER (c))
4908 | (class_bits & BIT_MULTIBYTE && !ISUNIBYTE (c))
4909 | (class_bits & BIT_NONASCII && !IS_REAL_ASCII (c))
4910 | (class_bits & BIT_PRINT && ISPRINT (c))
4911 | (class_bits & BIT_PUNCT && ISPUNCT (c))
4912 | (class_bits & BIT_SPACE && ISSPACE (c))
4913 | (class_bits & BIT_UNIBYTE && ISUNIBYTE (c))
4914 | (class_bits & BIT_UPPER && ISUPPER (c))
4915 | (class_bits & BIT_WORD && ISWORD (c)))
4916 not = !not;
4917 else
4918 CHARSET_LOOKUP_RANGE_TABLE_RAW (not, c, range_table, count);
4919 }
4920 #endif /* emacs */
4921
4922 if (range_table_exists)
4923 p = CHARSET_RANGE_TABLE_END (range_table, count);
4924 else
4925 p += CHARSET_BITMAP_SIZE (&p[-1]) + 1;
4926
4927 if (!not) goto fail;
4928
4929 d += len;
4930 break;
4931 }
4932
4933
4934 /* The beginning of a group is represented by start_memory.
4935 The argument is the register number. The text
4936 matched within the group is recorded (in the internal
4937 registers data structure) under the register number. */
4938 case start_memory:
4939 DEBUG_PRINT2 ("EXECUTING start_memory %d:\n", *p);
4940
4941 /* In case we need to undo this operation (via backtracking). */
4942 PUSH_FAILURE_REG ((unsigned int)*p);
4943
4944 regstart[*p] = d;
4945 regend[*p] = REG_UNSET_VALUE; /* probably unnecessary. -sm */
4946 DEBUG_PRINT2 (" regstart: %d\n", POINTER_TO_OFFSET (regstart[*p]));
4947
4948 /* Move past the register number and inner group count. */
4949 p += 1;
4950 break;
4951
4952
4953 /* The stop_memory opcode represents the end of a group. Its
4954 argument is the same as start_memory's: the register number. */
4955 case stop_memory:
4956 DEBUG_PRINT2 ("EXECUTING stop_memory %d:\n", *p);
4957
4958 assert (!REG_UNSET (regstart[*p]));
4959 /* Strictly speaking, there should be code such as:
4960
4961 assert (REG_UNSET (regend[*p]));
4962 PUSH_FAILURE_REGSTOP ((unsigned int)*p);
4963
4964 But the only info to be pushed is regend[*p] and it is known to
4965 be UNSET, so there really isn't anything to push.
4966 Not pushing anything, on the other hand deprives us from the
4967 guarantee that regend[*p] is UNSET since undoing this operation
4968 will not reset its value properly. This is not important since
4969 the value will only be read on the next start_memory or at
4970 the very end and both events can only happen if this stop_memory
4971 is *not* undone. */
4972
4973 regend[*p] = d;
4974 DEBUG_PRINT2 (" regend: %d\n", POINTER_TO_OFFSET (regend[*p]));
4975
4976 /* Move past the register number and the inner group count. */
4977 p += 1;
4978 break;
4979
4980
4981 /* \<digit> has been turned into a `duplicate' command which is
4982 followed by the numeric value of <digit> as the register number. */
4983 case duplicate:
4984 {
4985 register re_char *d2, *dend2;
4986 int regno = *p++; /* Get which register to match against. */
4987 DEBUG_PRINT2 ("EXECUTING duplicate %d.\n", regno);
4988
4989 /* Can't back reference a group which we've never matched. */
4990 if (REG_UNSET (regstart[regno]) || REG_UNSET (regend[regno]))
4991 goto fail;
4992
4993 /* Where in input to try to start matching. */
4994 d2 = regstart[regno];
4995
4996 /* Remember the start point to rollback upon failure. */
4997 dfail = d;
4998
4999 /* Where to stop matching; if both the place to start and
5000 the place to stop matching are in the same string, then
5001 set to the place to stop, otherwise, for now have to use
5002 the end of the first string. */
5003
5004 dend2 = ((FIRST_STRING_P (regstart[regno])
5005 == FIRST_STRING_P (regend[regno]))
5006 ? regend[regno] : end_match_1);
5007 for (;;)
5008 {
5009 /* If necessary, advance to next segment in register
5010 contents. */
5011 while (d2 == dend2)
5012 {
5013 if (dend2 == end_match_2) break;
5014 if (dend2 == regend[regno]) break;
5015
5016 /* End of string1 => advance to string2. */
5017 d2 = string2;
5018 dend2 = regend[regno];
5019 }
5020 /* At end of register contents => success */
5021 if (d2 == dend2) break;
5022
5023 /* If necessary, advance to next segment in data. */
5024 PREFETCH ();
5025
5026 /* How many characters left in this segment to match. */
5027 mcnt = dend - d;
5028
5029 /* Want how many consecutive characters we can match in
5030 one shot, so, if necessary, adjust the count. */
5031 if (mcnt > dend2 - d2)
5032 mcnt = dend2 - d2;
5033
5034 /* Compare that many; failure if mismatch, else move
5035 past them. */
5036 if (RE_TRANSLATE_P (translate)
5037 ? bcmp_translate (d, d2, mcnt, translate, multibyte)
5038 : bcmp (d, d2, mcnt))
5039 {
5040 d = dfail;
5041 goto fail;
5042 }
5043 d += mcnt, d2 += mcnt;
5044 }
5045 }
5046 break;
5047
5048
5049 /* begline matches the empty string at the beginning of the string
5050 (unless `not_bol' is set in `bufp'), and, if
5051 `newline_anchor' is set, after newlines. */
5052 case begline:
5053 DEBUG_PRINT1 ("EXECUTING begline.\n");
5054
5055 if (AT_STRINGS_BEG (d))
5056 {
5057 if (!bufp->not_bol) break;
5058 }
5059 else
5060 {
5061 unsigned char c;
5062 GET_CHAR_BEFORE_2 (c, d, string1, end1, string2, end2);
5063 if (c == '\n' && bufp->newline_anchor)
5064 break;
5065 }
5066 /* In all other cases, we fail. */
5067 goto fail;
5068
5069
5070 /* endline is the dual of begline. */
5071 case endline:
5072 DEBUG_PRINT1 ("EXECUTING endline.\n");
5073
5074 if (AT_STRINGS_END (d))
5075 {
5076 if (!bufp->not_eol) break;
5077 }
5078 else
5079 {
5080 PREFETCH_NOLIMIT ();
5081 if (*d == '\n' && bufp->newline_anchor)
5082 break;
5083 }
5084 goto fail;
5085
5086
5087 /* Match at the very beginning of the data. */
5088 case begbuf:
5089 DEBUG_PRINT1 ("EXECUTING begbuf.\n");
5090 if (AT_STRINGS_BEG (d))
5091 break;
5092 goto fail;
5093
5094
5095 /* Match at the very end of the data. */
5096 case endbuf:
5097 DEBUG_PRINT1 ("EXECUTING endbuf.\n");
5098 if (AT_STRINGS_END (d))
5099 break;
5100 goto fail;
5101
5102
5103 /* on_failure_keep_string_jump is used to optimize `.*\n'. It
5104 pushes NULL as the value for the string on the stack. Then
5105 `POP_FAILURE_POINT' will keep the current value for the
5106 string, instead of restoring it. To see why, consider
5107 matching `foo\nbar' against `.*\n'. The .* matches the foo;
5108 then the . fails against the \n. But the next thing we want
5109 to do is match the \n against the \n; if we restored the
5110 string value, we would be back at the foo.
5111
5112 Because this is used only in specific cases, we don't need to
5113 check all the things that `on_failure_jump' does, to make
5114 sure the right things get saved on the stack. Hence we don't
5115 share its code. The only reason to push anything on the
5116 stack at all is that otherwise we would have to change
5117 `anychar's code to do something besides goto fail in this
5118 case; that seems worse than this. */
5119 case on_failure_keep_string_jump:
5120 EXTRACT_NUMBER_AND_INCR (mcnt, p);
5121 DEBUG_PRINT3 ("EXECUTING on_failure_keep_string_jump %d (to %p):\n",
5122 mcnt, p + mcnt);
5123
5124 PUSH_FAILURE_POINT (p - 3, NULL);
5125 break;
5126
5127 /* A nasty loop is introduced by the non-greedy *? and +?.
5128 With such loops, the stack only ever contains one failure point
5129 at a time, so that a plain on_failure_jump_loop kind of
5130 cycle detection cannot work. Worse yet, such a detection
5131 can not only fail to detect a cycle, but it can also wrongly
5132 detect a cycle (between different instantiations of the same
5133 loop.
5134 So the method used for those nasty loops is a little different:
5135 We use a special cycle-detection-stack-frame which is pushed
5136 when the on_failure_jump_nastyloop failure-point is *popped*.
5137 This special frame thus marks the beginning of one iteration
5138 through the loop and we can hence easily check right here
5139 whether something matched between the beginning and the end of
5140 the loop. */
5141 case on_failure_jump_nastyloop:
5142 EXTRACT_NUMBER_AND_INCR (mcnt, p);
5143 DEBUG_PRINT3 ("EXECUTING on_failure_jump_nastyloop %d (to %p):\n",
5144 mcnt, p + mcnt);
5145
5146 assert ((re_opcode_t)p[-4] == no_op);
5147 CHECK_INFINITE_LOOP (p - 4, d);
5148 PUSH_FAILURE_POINT (p - 3, d);
5149 break;
5150
5151
5152 /* Simple loop detecting on_failure_jump: just check on the
5153 failure stack if the same spot was already hit earlier. */
5154 case on_failure_jump_loop:
5155 on_failure:
5156 EXTRACT_NUMBER_AND_INCR (mcnt, p);
5157 DEBUG_PRINT3 ("EXECUTING on_failure_jump_loop %d (to %p):\n",
5158 mcnt, p + mcnt);
5159
5160 CHECK_INFINITE_LOOP (p - 3, d);
5161 PUSH_FAILURE_POINT (p - 3, d);
5162 break;
5163
5164
5165 /* Uses of on_failure_jump:
5166
5167 Each alternative starts with an on_failure_jump that points
5168 to the beginning of the next alternative. Each alternative
5169 except the last ends with a jump that in effect jumps past
5170 the rest of the alternatives. (They really jump to the
5171 ending jump of the following alternative, because tensioning
5172 these jumps is a hassle.)
5173
5174 Repeats start with an on_failure_jump that points past both
5175 the repetition text and either the following jump or
5176 pop_failure_jump back to this on_failure_jump. */
5177 case on_failure_jump:
5178 QUIT;
5179 EXTRACT_NUMBER_AND_INCR (mcnt, p);
5180 DEBUG_PRINT3 ("EXECUTING on_failure_jump %d (to %p):\n",
5181 mcnt, p + mcnt);
5182
5183 PUSH_FAILURE_POINT (p -3, d);
5184 break;
5185
5186 /* This operation is used for greedy *.
5187 Compare the beginning of the repeat with what in the
5188 pattern follows its end. If we can establish that there
5189 is nothing that they would both match, i.e., that we
5190 would have to backtrack because of (as in, e.g., `a*a')
5191 then we can use a non-backtracking loop based on
5192 on_failure_keep_string_jump instead of on_failure_jump. */
5193 case on_failure_jump_smart:
5194 QUIT;
5195 EXTRACT_NUMBER_AND_INCR (mcnt, p);
5196 DEBUG_PRINT3 ("EXECUTING on_failure_jump_smart %d (to %p).\n",
5197 mcnt, p + mcnt);
5198 {
5199 unsigned char *p1 = p; /* Next operation. */
5200 unsigned char *p2 = p + mcnt; /* Destination of the jump. */
5201
5202 p -= 3; /* Reset so that we will re-execute the
5203 instruction once it's been changed. */
5204
5205 EXTRACT_NUMBER (mcnt, p2 - 2);
5206
5207 /* Ensure this is a indeed the trivial kind of loop
5208 we are expecting. */
5209 assert (skip_one_char (p1) == p2 - 3);
5210 assert ((re_opcode_t) p2[-3] == jump && p2 + mcnt == p);
5211 DEBUG_STATEMENT (debug += 2);
5212 if (mutually_exclusive_p (bufp, p1, p2))
5213 {
5214 /* Use a fast `on_failure_keep_string_jump' loop. */
5215 DEBUG_PRINT1 (" smart exclusive => fast loop.\n");
5216 *p = (unsigned char) on_failure_keep_string_jump;
5217 STORE_NUMBER (p2 - 2, mcnt + 3);
5218 }
5219 else
5220 {
5221 /* Default to a safe `on_failure_jump' loop. */
5222 DEBUG_PRINT1 (" smart default => slow loop.\n");
5223 *p = (unsigned char) on_failure_jump;
5224 }
5225 DEBUG_STATEMENT (debug -= 2);
5226 }
5227 break;
5228
5229 /* Unconditionally jump (without popping any failure points). */
5230 case jump:
5231 unconditional_jump:
5232 QUIT;
5233 EXTRACT_NUMBER_AND_INCR (mcnt, p); /* Get the amount to jump. */
5234 DEBUG_PRINT2 ("EXECUTING jump %d ", mcnt);
5235 p += mcnt; /* Do the jump. */
5236 DEBUG_PRINT2 ("(to %p).\n", p);
5237 break;
5238
5239
5240 /* Have to succeed matching what follows at least n times.
5241 After that, handle like `on_failure_jump'. */
5242 case succeed_n:
5243 EXTRACT_NUMBER (mcnt, p + 2);
5244 DEBUG_PRINT2 ("EXECUTING succeed_n %d.\n", mcnt);
5245
5246 /* Originally, mcnt is how many times we HAVE to succeed. */
5247 if (mcnt != 0)
5248 {
5249 mcnt--;
5250 p += 2;
5251 PUSH_FAILURE_COUNT (p);
5252 STORE_NUMBER_AND_INCR (p, mcnt);
5253 DEBUG_PRINT3 (" Setting %p to %d.\n", p, mcnt);
5254 }
5255 else
5256 /* The two bytes encoding mcnt == 0 are two no_op opcodes. */
5257 goto on_failure;
5258 break;
5259
5260 case jump_n:
5261 EXTRACT_NUMBER (mcnt, p + 2);
5262 DEBUG_PRINT2 ("EXECUTING jump_n %d.\n", mcnt);
5263
5264 /* Originally, this is how many times we CAN jump. */
5265 if (mcnt != 0)
5266 {
5267 mcnt--;
5268 PUSH_FAILURE_COUNT (p + 2);
5269 STORE_NUMBER (p + 2, mcnt);
5270 goto unconditional_jump;
5271 }
5272 /* If don't have to jump any more, skip over the rest of command. */
5273 else
5274 p += 4;
5275 break;
5276
5277 case set_number_at:
5278 {
5279 DEBUG_PRINT1 ("EXECUTING set_number_at.\n");
5280
5281 EXTRACT_NUMBER_AND_INCR (mcnt, p);
5282 p1 = p + mcnt;
5283 EXTRACT_NUMBER_AND_INCR (mcnt, p);
5284 DEBUG_PRINT3 (" Setting %p to %d.\n", p1, mcnt);
5285 PUSH_FAILURE_COUNT (p1);
5286 STORE_NUMBER (p1, mcnt);
5287 break;
5288 }
5289
5290 case wordbound:
5291 case notwordbound:
5292 not = (re_opcode_t) *(p - 1) == notwordbound;
5293 DEBUG_PRINT2 ("EXECUTING %swordbound.\n", not?"not":"");
5294
5295 /* We SUCCEED (or FAIL) in one of the following cases: */
5296
5297 /* Case 1: D is at the beginning or the end of string. */
5298 if (AT_STRINGS_BEG (d) || AT_STRINGS_END (d))
5299 not = !not;
5300 else
5301 {
5302 /* C1 is the character before D, S1 is the syntax of C1, C2
5303 is the character at D, and S2 is the syntax of C2. */
5304 int c1, c2, s1, s2;
5305 #ifdef emacs
5306 int offset = PTR_TO_OFFSET (d - 1);
5307 int charpos = SYNTAX_TABLE_BYTE_TO_CHAR (offset);
5308 UPDATE_SYNTAX_TABLE (charpos);
5309 #endif
5310 GET_CHAR_BEFORE_2 (c1, d, string1, end1, string2, end2);
5311 s1 = SYNTAX (c1);
5312 #ifdef emacs
5313 UPDATE_SYNTAX_TABLE_FORWARD (charpos + 1);
5314 #endif
5315 PREFETCH_NOLIMIT ();
5316 c2 = RE_STRING_CHAR (d, dend - d);
5317 s2 = SYNTAX (c2);
5318
5319 if (/* Case 2: Only one of S1 and S2 is Sword. */
5320 ((s1 == Sword) != (s2 == Sword))
5321 /* Case 3: Both of S1 and S2 are Sword, and macro
5322 WORD_BOUNDARY_P (C1, C2) returns nonzero. */
5323 || ((s1 == Sword) && WORD_BOUNDARY_P (c1, c2)))
5324 not = !not;
5325 }
5326 if (not)
5327 break;
5328 else
5329 goto fail;
5330
5331 case wordbeg:
5332 DEBUG_PRINT1 ("EXECUTING wordbeg.\n");
5333
5334 /* We FAIL in one of the following cases: */
5335
5336 /* Case 1: D is at the end of string. */
5337 if (AT_STRINGS_END (d))
5338 goto fail;
5339 else
5340 {
5341 /* C1 is the character before D, S1 is the syntax of C1, C2
5342 is the character at D, and S2 is the syntax of C2. */
5343 int c1, c2, s1, s2;
5344 #ifdef emacs
5345 int offset = PTR_TO_OFFSET (d);
5346 int charpos = SYNTAX_TABLE_BYTE_TO_CHAR (offset);
5347 UPDATE_SYNTAX_TABLE (charpos);
5348 #endif
5349 PREFETCH ();
5350 c2 = RE_STRING_CHAR (d, dend - d);
5351 s2 = SYNTAX (c2);
5352
5353 /* Case 2: S2 is not Sword. */
5354 if (s2 != Sword)
5355 goto fail;
5356
5357 /* Case 3: D is not at the beginning of string ... */
5358 if (!AT_STRINGS_BEG (d))
5359 {
5360 GET_CHAR_BEFORE_2 (c1, d, string1, end1, string2, end2);
5361 #ifdef emacs
5362 UPDATE_SYNTAX_TABLE_BACKWARD (charpos - 1);
5363 #endif
5364 s1 = SYNTAX (c1);
5365
5366 /* ... and S1 is Sword, and WORD_BOUNDARY_P (C1, C2)
5367 returns 0. */
5368 if ((s1 == Sword) && !WORD_BOUNDARY_P (c1, c2))
5369 goto fail;
5370 }
5371 }
5372 break;
5373
5374 case wordend:
5375 DEBUG_PRINT1 ("EXECUTING wordend.\n");
5376
5377 /* We FAIL in one of the following cases: */
5378
5379 /* Case 1: D is at the beginning of string. */
5380 if (AT_STRINGS_BEG (d))
5381 goto fail;
5382 else
5383 {
5384 /* C1 is the character before D, S1 is the syntax of C1, C2
5385 is the character at D, and S2 is the syntax of C2. */
5386 int c1, c2, s1, s2;
5387 #ifdef emacs
5388 int offset = PTR_TO_OFFSET (d) - 1;
5389 int charpos = SYNTAX_TABLE_BYTE_TO_CHAR (offset);
5390 UPDATE_SYNTAX_TABLE (charpos);
5391 #endif
5392 GET_CHAR_BEFORE_2 (c1, d, string1, end1, string2, end2);
5393 s1 = SYNTAX (c1);
5394
5395 /* Case 2: S1 is not Sword. */
5396 if (s1 != Sword)
5397 goto fail;
5398
5399 /* Case 3: D is not at the end of string ... */
5400 if (!AT_STRINGS_END (d))
5401 {
5402 PREFETCH_NOLIMIT ();
5403 c2 = RE_STRING_CHAR (d, dend - d);
5404 #ifdef emacs
5405 UPDATE_SYNTAX_TABLE_FORWARD (charpos);
5406 #endif
5407 s2 = SYNTAX (c2);
5408
5409 /* ... and S2 is Sword, and WORD_BOUNDARY_P (C1, C2)
5410 returns 0. */
5411 if ((s2 == Sword) && !WORD_BOUNDARY_P (c1, c2))
5412 goto fail;
5413 }
5414 }
5415 break;
5416
5417 case syntaxspec:
5418 case notsyntaxspec:
5419 not = (re_opcode_t) *(p - 1) == notsyntaxspec;
5420 mcnt = *p++;
5421 DEBUG_PRINT3 ("EXECUTING %ssyntaxspec %d.\n", not?"not":"", mcnt);
5422 PREFETCH ();
5423 #ifdef emacs
5424 {
5425 int offset = PTR_TO_OFFSET (d);
5426 int pos1 = SYNTAX_TABLE_BYTE_TO_CHAR (offset);
5427 UPDATE_SYNTAX_TABLE (pos1);
5428 }
5429 #endif
5430 {
5431 int c, len;
5432
5433 c = RE_STRING_CHAR_AND_LENGTH (d, dend - d, len);
5434
5435 if ((SYNTAX (c) != (enum syntaxcode) mcnt) ^ not)
5436 goto fail;
5437 d += len;
5438 }
5439 break;
5440
5441 #ifdef emacs
5442 case before_dot:
5443 DEBUG_PRINT1 ("EXECUTING before_dot.\n");
5444 if (PTR_BYTE_POS (d) >= PT_BYTE)
5445 goto fail;
5446 break;
5447
5448 case at_dot:
5449 DEBUG_PRINT1 ("EXECUTING at_dot.\n");
5450 if (PTR_BYTE_POS (d) != PT_BYTE)
5451 goto fail;
5452 break;
5453
5454 case after_dot:
5455 DEBUG_PRINT1 ("EXECUTING after_dot.\n");
5456 if (PTR_BYTE_POS (d) <= PT_BYTE)
5457 goto fail;
5458 break;
5459
5460 case categoryspec:
5461 case notcategoryspec:
5462 not = (re_opcode_t) *(p - 1) == notcategoryspec;
5463 mcnt = *p++;
5464 DEBUG_PRINT3 ("EXECUTING %scategoryspec %d.\n", not?"not":"", mcnt);
5465 PREFETCH ();
5466 {
5467 int c, len;
5468 c = RE_STRING_CHAR_AND_LENGTH (d, dend - d, len);
5469
5470 if ((!CHAR_HAS_CATEGORY (c, mcnt)) ^ not)
5471 goto fail;
5472 d += len;
5473 }
5474 break;
5475
5476 #endif /* emacs */
5477
5478 default:
5479 abort ();
5480 }
5481 continue; /* Successfully executed one pattern command; keep going. */
5482
5483
5484 /* We goto here if a matching operation fails. */
5485 fail:
5486 QUIT;
5487 if (!FAIL_STACK_EMPTY ())
5488 {
5489 re_char *str;
5490 unsigned char *pat;
5491 /* A restart point is known. Restore to that state. */
5492 DEBUG_PRINT1 ("\nFAIL:\n");
5493 POP_FAILURE_POINT (str, pat);
5494 switch (SWITCH_ENUM_CAST ((re_opcode_t) *pat++))
5495 {
5496 case on_failure_keep_string_jump:
5497 assert (str == NULL);
5498 goto continue_failure_jump;
5499
5500 case on_failure_jump_nastyloop:
5501 assert ((re_opcode_t)pat[-2] == no_op);
5502 PUSH_FAILURE_POINT (pat - 2, str);
5503 /* Fallthrough */
5504
5505 case on_failure_jump_loop:
5506 case on_failure_jump:
5507 case succeed_n:
5508 d = str;
5509 continue_failure_jump:
5510 EXTRACT_NUMBER_AND_INCR (mcnt, pat);
5511 p = pat + mcnt;
5512 break;
5513
5514 case no_op:
5515 /* A special frame used for nastyloops. */
5516 goto fail;
5517
5518 default:
5519 abort();
5520 }
5521
5522 assert (p >= bufp->buffer && p <= pend);
5523
5524 if (d >= string1 && d <= end1)
5525 dend = end_match_1;
5526 }
5527 else
5528 break; /* Matching at this starting point really fails. */
5529 } /* for (;;) */
5530
5531 if (best_regs_set)
5532 goto restore_best_regs;
5533
5534 FREE_VARIABLES ();
5535
5536 return -1; /* Failure to match. */
5537 } /* re_match_2 */
5538 \f
5539 /* Subroutine definitions for re_match_2. */
5540
5541 /* Return zero if TRANSLATE[S1] and TRANSLATE[S2] are identical for LEN
5542 bytes; nonzero otherwise. */
5543
5544 static int
5545 bcmp_translate (s1, s2, len, translate, multibyte)
5546 re_char *s1, *s2;
5547 register int len;
5548 RE_TRANSLATE_TYPE translate;
5549 const int multibyte;
5550 {
5551 register re_char *p1 = s1, *p2 = s2;
5552 re_char *p1_end = s1 + len;
5553 re_char *p2_end = s2 + len;
5554
5555 while (p1 != p1_end && p2 != p2_end)
5556 {
5557 int p1_charlen, p2_charlen;
5558 int p1_ch, p2_ch;
5559
5560 p1_ch = RE_STRING_CHAR_AND_LENGTH (p1, p1_end - p1, p1_charlen);
5561 p2_ch = RE_STRING_CHAR_AND_LENGTH (p2, p2_end - p2, p2_charlen);
5562
5563 if (RE_TRANSLATE (translate, p1_ch)
5564 != RE_TRANSLATE (translate, p2_ch))
5565 return 1;
5566
5567 p1 += p1_charlen, p2 += p2_charlen;
5568 }
5569
5570 if (p1 != p1_end || p2 != p2_end)
5571 return 1;
5572
5573 return 0;
5574 }
5575 \f
5576 /* Entry points for GNU code. */
5577
5578 /* re_compile_pattern is the GNU regular expression compiler: it
5579 compiles PATTERN (of length SIZE) and puts the result in BUFP.
5580 Returns 0 if the pattern was valid, otherwise an error string.
5581
5582 Assumes the `allocated' (and perhaps `buffer') and `translate' fields
5583 are set in BUFP on entry.
5584
5585 We call regex_compile to do the actual compilation. */
5586
5587 const char *
5588 re_compile_pattern (pattern, length, bufp)
5589 const char *pattern;
5590 int length;
5591 struct re_pattern_buffer *bufp;
5592 {
5593 reg_errcode_t ret;
5594
5595 /* GNU code is written to assume at least RE_NREGS registers will be set
5596 (and at least one extra will be -1). */
5597 bufp->regs_allocated = REGS_UNALLOCATED;
5598
5599 /* And GNU code determines whether or not to get register information
5600 by passing null for the REGS argument to re_match, etc., not by
5601 setting no_sub. */
5602 bufp->no_sub = 0;
5603
5604 /* Match anchors at newline. */
5605 bufp->newline_anchor = 1;
5606
5607 ret = regex_compile (pattern, length, re_syntax_options, bufp);
5608
5609 if (!ret)
5610 return NULL;
5611 return gettext (re_error_msgid[(int) ret]);
5612 }
5613 \f
5614 /* Entry points compatible with 4.2 BSD regex library. We don't define
5615 them unless specifically requested. */
5616
5617 #if defined (_REGEX_RE_COMP) || defined (_LIBC)
5618
5619 /* BSD has one and only one pattern buffer. */
5620 static struct re_pattern_buffer re_comp_buf;
5621
5622 char *
5623 #ifdef _LIBC
5624 /* Make these definitions weak in libc, so POSIX programs can redefine
5625 these names if they don't use our functions, and still use
5626 regcomp/regexec below without link errors. */
5627 weak_function
5628 #endif
5629 re_comp (s)
5630 const char *s;
5631 {
5632 reg_errcode_t ret;
5633
5634 if (!s)
5635 {
5636 if (!re_comp_buf.buffer)
5637 /* Yes, we're discarding `const' here if !HAVE_LIBINTL. */
5638 return (char *) gettext ("No previous regular expression");
5639 return 0;
5640 }
5641
5642 if (!re_comp_buf.buffer)
5643 {
5644 re_comp_buf.buffer = (unsigned char *) malloc (200);
5645 if (re_comp_buf.buffer == NULL)
5646 /* Yes, we're discarding `const' here if !HAVE_LIBINTL. */
5647 return (char *) gettext (re_error_msgid[(int) REG_ESPACE]);
5648 re_comp_buf.allocated = 200;
5649
5650 re_comp_buf.fastmap = (char *) malloc (1 << BYTEWIDTH);
5651 if (re_comp_buf.fastmap == NULL)
5652 /* Yes, we're discarding `const' here if !HAVE_LIBINTL. */
5653 return (char *) gettext (re_error_msgid[(int) REG_ESPACE]);
5654 }
5655
5656 /* Since `re_exec' always passes NULL for the `regs' argument, we
5657 don't need to initialize the pattern buffer fields which affect it. */
5658
5659 /* Match anchors at newlines. */
5660 re_comp_buf.newline_anchor = 1;
5661
5662 ret = regex_compile (s, strlen (s), re_syntax_options, &re_comp_buf);
5663
5664 if (!ret)
5665 return NULL;
5666
5667 /* Yes, we're discarding `const' here if !HAVE_LIBINTL. */
5668 return (char *) gettext (re_error_msgid[(int) ret]);
5669 }
5670
5671
5672 int
5673 #ifdef _LIBC
5674 weak_function
5675 #endif
5676 re_exec (s)
5677 const char *s;
5678 {
5679 const int len = strlen (s);
5680 return
5681 0 <= re_search (&re_comp_buf, s, len, 0, len, (struct re_registers *) 0);
5682 }
5683 #endif /* _REGEX_RE_COMP */
5684 \f
5685 /* POSIX.2 functions. Don't define these for Emacs. */
5686
5687 #ifndef emacs
5688
5689 /* regcomp takes a regular expression as a string and compiles it.
5690
5691 PREG is a regex_t *. We do not expect any fields to be initialized,
5692 since POSIX says we shouldn't. Thus, we set
5693
5694 `buffer' to the compiled pattern;
5695 `used' to the length of the compiled pattern;
5696 `syntax' to RE_SYNTAX_POSIX_EXTENDED if the
5697 REG_EXTENDED bit in CFLAGS is set; otherwise, to
5698 RE_SYNTAX_POSIX_BASIC;
5699 `newline_anchor' to REG_NEWLINE being set in CFLAGS;
5700 `fastmap' and `fastmap_accurate' to zero;
5701 `re_nsub' to the number of subexpressions in PATTERN.
5702
5703 PATTERN is the address of the pattern string.
5704
5705 CFLAGS is a series of bits which affect compilation.
5706
5707 If REG_EXTENDED is set, we use POSIX extended syntax; otherwise, we
5708 use POSIX basic syntax.
5709
5710 If REG_NEWLINE is set, then . and [^...] don't match newline.
5711 Also, regexec will try a match beginning after every newline.
5712
5713 If REG_ICASE is set, then we considers upper- and lowercase
5714 versions of letters to be equivalent when matching.
5715
5716 If REG_NOSUB is set, then when PREG is passed to regexec, that
5717 routine will report only success or failure, and nothing about the
5718 registers.
5719
5720 It returns 0 if it succeeds, nonzero if it doesn't. (See regex.h for
5721 the return codes and their meanings.) */
5722
5723 int
5724 regcomp (preg, pattern, cflags)
5725 regex_t *preg;
5726 const char *pattern;
5727 int cflags;
5728 {
5729 reg_errcode_t ret;
5730 unsigned syntax
5731 = (cflags & REG_EXTENDED) ?
5732 RE_SYNTAX_POSIX_EXTENDED : RE_SYNTAX_POSIX_BASIC;
5733
5734 /* regex_compile will allocate the space for the compiled pattern. */
5735 preg->buffer = 0;
5736 preg->allocated = 0;
5737 preg->used = 0;
5738
5739 /* Don't bother to use a fastmap when searching. This simplifies the
5740 REG_NEWLINE case: if we used a fastmap, we'd have to put all the
5741 characters after newlines into the fastmap. This way, we just try
5742 every character. */
5743 preg->fastmap = 0;
5744
5745 if (cflags & REG_ICASE)
5746 {
5747 unsigned i;
5748
5749 preg->translate
5750 = (RE_TRANSLATE_TYPE) malloc (CHAR_SET_SIZE
5751 * sizeof (*(RE_TRANSLATE_TYPE)0));
5752 if (preg->translate == NULL)
5753 return (int) REG_ESPACE;
5754
5755 /* Map uppercase characters to corresponding lowercase ones. */
5756 for (i = 0; i < CHAR_SET_SIZE; i++)
5757 preg->translate[i] = ISUPPER (i) ? tolower (i) : i;
5758 }
5759 else
5760 preg->translate = NULL;
5761
5762 /* If REG_NEWLINE is set, newlines are treated differently. */
5763 if (cflags & REG_NEWLINE)
5764 { /* REG_NEWLINE implies neither . nor [^...] match newline. */
5765 syntax &= ~RE_DOT_NEWLINE;
5766 syntax |= RE_HAT_LISTS_NOT_NEWLINE;
5767 /* It also changes the matching behavior. */
5768 preg->newline_anchor = 1;
5769 }
5770 else
5771 preg->newline_anchor = 0;
5772
5773 preg->no_sub = !!(cflags & REG_NOSUB);
5774
5775 /* POSIX says a null character in the pattern terminates it, so we
5776 can use strlen here in compiling the pattern. */
5777 ret = regex_compile (pattern, strlen (pattern), syntax, preg);
5778
5779 /* POSIX doesn't distinguish between an unmatched open-group and an
5780 unmatched close-group: both are REG_EPAREN. */
5781 if (ret == REG_ERPAREN) ret = REG_EPAREN;
5782
5783 return (int) ret;
5784 }
5785
5786
5787 /* regexec searches for a given pattern, specified by PREG, in the
5788 string STRING.
5789
5790 If NMATCH is zero or REG_NOSUB was set in the cflags argument to
5791 `regcomp', we ignore PMATCH. Otherwise, we assume PMATCH has at
5792 least NMATCH elements, and we set them to the offsets of the
5793 corresponding matched substrings.
5794
5795 EFLAGS specifies `execution flags' which affect matching: if
5796 REG_NOTBOL is set, then ^ does not match at the beginning of the
5797 string; if REG_NOTEOL is set, then $ does not match at the end.
5798
5799 We return 0 if we find a match and REG_NOMATCH if not. */
5800
5801 int
5802 regexec (preg, string, nmatch, pmatch, eflags)
5803 const regex_t *preg;
5804 const char *string;
5805 size_t nmatch;
5806 regmatch_t pmatch[];
5807 int eflags;
5808 {
5809 int ret;
5810 struct re_registers regs;
5811 regex_t private_preg;
5812 int len = strlen (string);
5813 boolean want_reg_info = !preg->no_sub && nmatch > 0;
5814
5815 private_preg = *preg;
5816
5817 private_preg.not_bol = !!(eflags & REG_NOTBOL);
5818 private_preg.not_eol = !!(eflags & REG_NOTEOL);
5819
5820 /* The user has told us exactly how many registers to return
5821 information about, via `nmatch'. We have to pass that on to the
5822 matching routines. */
5823 private_preg.regs_allocated = REGS_FIXED;
5824
5825 if (want_reg_info)
5826 {
5827 regs.num_regs = nmatch;
5828 regs.start = TALLOC (nmatch, regoff_t);
5829 regs.end = TALLOC (nmatch, regoff_t);
5830 if (regs.start == NULL || regs.end == NULL)
5831 return (int) REG_NOMATCH;
5832 }
5833
5834 /* Perform the searching operation. */
5835 ret = re_search (&private_preg, string, len,
5836 /* start: */ 0, /* range: */ len,
5837 want_reg_info ? &regs : (struct re_registers *) 0);
5838
5839 /* Copy the register information to the POSIX structure. */
5840 if (want_reg_info)
5841 {
5842 if (ret >= 0)
5843 {
5844 unsigned r;
5845
5846 for (r = 0; r < nmatch; r++)
5847 {
5848 pmatch[r].rm_so = regs.start[r];
5849 pmatch[r].rm_eo = regs.end[r];
5850 }
5851 }
5852
5853 /* If we needed the temporary register info, free the space now. */
5854 free (regs.start);
5855 free (regs.end);
5856 }
5857
5858 /* We want zero return to mean success, unlike `re_search'. */
5859 return ret >= 0 ? (int) REG_NOERROR : (int) REG_NOMATCH;
5860 }
5861
5862
5863 /* Returns a message corresponding to an error code, ERRCODE, returned
5864 from either regcomp or regexec. We don't use PREG here. */
5865
5866 size_t
5867 regerror (errcode, preg, errbuf, errbuf_size)
5868 int errcode;
5869 const regex_t *preg;
5870 char *errbuf;
5871 size_t errbuf_size;
5872 {
5873 const char *msg;
5874 size_t msg_size;
5875
5876 if (errcode < 0
5877 || errcode >= (sizeof (re_error_msgid) / sizeof (re_error_msgid[0])))
5878 /* Only error codes returned by the rest of the code should be passed
5879 to this routine. If we are given anything else, or if other regex
5880 code generates an invalid error code, then the program has a bug.
5881 Dump core so we can fix it. */
5882 abort ();
5883
5884 msg = gettext (re_error_msgid[errcode]);
5885
5886 msg_size = strlen (msg) + 1; /* Includes the null. */
5887
5888 if (errbuf_size != 0)
5889 {
5890 if (msg_size > errbuf_size)
5891 {
5892 strncpy (errbuf, msg, errbuf_size - 1);
5893 errbuf[errbuf_size - 1] = 0;
5894 }
5895 else
5896 strcpy (errbuf, msg);
5897 }
5898
5899 return msg_size;
5900 }
5901
5902
5903 /* Free dynamically allocated space used by PREG. */
5904
5905 void
5906 regfree (preg)
5907 regex_t *preg;
5908 {
5909 if (preg->buffer != NULL)
5910 free (preg->buffer);
5911 preg->buffer = NULL;
5912
5913 preg->allocated = 0;
5914 preg->used = 0;
5915
5916 if (preg->fastmap != NULL)
5917 free (preg->fastmap);
5918 preg->fastmap = NULL;
5919 preg->fastmap_accurate = 0;
5920
5921 if (preg->translate != NULL)
5922 free (preg->translate);
5923 preg->translate = NULL;
5924 }
5925
5926 #endif /* not emacs */