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