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