comparison src/regex.c @ 1333:1b0339b048ce

[xemacs-hg @ 2003-03-02 09:38:37 by ben] To: xemacs-patches@xemacs.org PROBLEMS: Include nt/PROBLEMS and update. Add note about incremental linking badness. cmdloop.el, custom.el, dumped-lisp.el, files.el, keydefs.el, keymap.el, lisp-mode.el, make-docfile.el, replace.el, simple.el, subr.el, view-less.el, wid-edit.el: Lots of syncing with FSF 21.2. Use if-fboundp in wid-edit.el. New file newcomment.el from FSF. internals/internals.texi: Fix typo. (Build-Time Dependencies): New node. PROBLEMS: Delete. config.inc.samp, xemacs.mak: Eliminate HAVE_VC6, use SUPPORT_EDIT_AND_CONTINUE in its place. No incremental linking unless SUPPORT_EDIT_AND_CONTINUE, since it can cause nasty crashes in pdump. Put warnings about this in config.inc.samp. Report the full compile flags used for src and lib-src in the Installation output. alloc.c, lisp.h, ralloc.c, regex.c: Use ALLOCA() in regex.c to avoid excessive stack allocation. Also fix subtle problem with REL_ALLOC() -- any call to malloc() (direct or indirect) may relocate rel-alloced data, causing buffer text to shift. After any such call, regex must update all its pointers to such data. Add a system, when ERROR_CHECK_MALLOC, whereby regex.c indicates all the places it is prepared to handle malloc()/realloc()/free(), and any calls anywhere in XEmacs outside of this will trigger an abort. alloc.c, dialog-msw.c, eval.c, event-stream.c, general-slots.h, insdel.c, lisp.h, menubar-msw.c, menubar-x.c: Change *run_hook*_trapping_problems to take a warning class, not a string. Factor out code to issue warnings, add flag to call_trapping_problems() to postpone warning issue, and make *run_hook*_trapping_problems issue their own warnings tailored to the hook, postponed in the case of safe_run_hook_trapping_problems() so that the appropriate message can be issued about resetting to nil only when not `quit'. Make record_unwind_protect_restoring_int() non-static. dumper.c: Issue notes about incremental linking problems under Windows. fileio.c: Mule-ize encrypt/decrypt-string code. text.h: Spacing changes.
author ben
date Sun, 02 Mar 2003 09:38:54 +0000
parents b03923afd851
children 01c57eb70ae9
comparison
equal deleted inserted replaced
1332:6aa23bb3da6b 1333:1b0339b048ce
3 (Implements POSIX draft P10003.2/D11.2, except for 3 (Implements POSIX draft P10003.2/D11.2, except for
4 internationalization features.) 4 internationalization features.)
5 5
6 Copyright (C) 1993, 1994, 1995 Free Software Foundation, Inc. 6 Copyright (C) 1993, 1994, 1995 Free Software Foundation, Inc.
7 Copyright (C) 1995 Sun Microsystems, Inc. 7 Copyright (C) 1995 Sun Microsystems, Inc.
8 Copyright (C) 1995, 2001, 2002 Ben Wing. 8 Copyright (C) 1995, 2001, 2002, 2003 Ben Wing.
9 9
10 This program is free software; you can redistribute it and/or modify 10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by 11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2, or (at your option) 12 the Free Software Foundation; either version 2, or (at your option)
13 any later version. 13 any later version.
124 off + BYTE_BUF_BEGV (XBUFFER (lispobj))); 124 off + BYTE_BUF_BEGV (XBUFFER (lispobj)));
125 else 125 else
126 return 0; 126 return 0;
127 } 127 }
128 128
129 #ifdef REL_ALLOC
130
131 /* STRING1 is the value of STRING1 given to re_match_2(). LISPOBJ is
132 the Lisp object (if any) from which the string is taken. If LISPOBJ
133 is a buffer, return a relocation offset to be added to all pointers to
134 string data so that they will be accurate again, after an allocation or
135 reallocation that potentially relocated the buffer data.
136 */
137 static Bytecount
138 offset_post_relocation (Lisp_Object lispobj, char *string1)
139 {
140 struct buffer *buf;
141
142 if (!BUFFERP (lispobj))
143 return 0;
144 return (BYTE_BUF_BYTE_ADDRESS (XBUFFER (lispobj),
145 BYTE_BUF_BEGV (XBUFFER (lispobj))) -
146 string1);
147 }
148
149 #endif /* REL_ALLOC */
150
151 #ifdef ERROR_CHECK_MALLOC
152
153 /* NOTE that this can run malloc() so you need to adjust afterwards. */
154
155 static int
156 bind_regex_malloc_disallowed (int value)
157 {
158 /* Tricky, because the act of binding can run malloc(). */
159 int old_regex_malloc_disallowed = regex_malloc_disallowed;
160 int depth;
161 regex_malloc_disallowed = 0;
162 depth = record_unwind_protect_restoring_int (&regex_malloc_disallowed,
163 old_regex_malloc_disallowed);
164 regex_malloc_disallowed = value;
165 return depth;
166 }
167
168 #endif /* ERROR_CHECK_MALLOC */
169
129 #else /* not emacs */ 170 #else /* not emacs */
130 171
131 /* If we are not linking with Emacs proper, 172 /* If we are not linking with Emacs proper,
132 we can't use the relocating allocator 173 we can't use the relocating allocator
133 even if config.h says that we can. */ 174 even if config.h says that we can. */
298 339
299 Because we sometimes use alloca, some routines have to be macros, 340 Because we sometimes use alloca, some routines have to be macros,
300 not functions -- `alloca'-allocated space disappears at the end of the 341 not functions -- `alloca'-allocated space disappears at the end of the
301 function it is called in. */ 342 function it is called in. */
302 343
344 #ifndef emacs
345 #define ALLOCA alloca
346 #define xmalloc malloc
347 #define xrealloc realloc
348 #define xfree free
349 #endif
350
351 #ifdef emacs
352 #define ALLOCA_GARBAGE_COLLECT() \
353 do \
354 { \
355 if (need_to_check_c_alloca) \
356 xemacs_c_alloca (0); \
357 } while (0)
358 #elif defined (C_ALLOCA)
359 #define ALLOCA_GARBAGE_COLLECT() alloca (0)
360 #else
361 #define ALLOCA_GARBAGE_COLLECT()
362 #endif
363
364 #ifndef emacs
365 /* So we can use just it to conditionalize on */
366 #undef ERROR_CHECK_MALLOC
367 #endif
368
369 #ifdef ERROR_CHECK_MALLOC
370 /* When REL_ALLOC, malloc() is problematic because it could potentially
371 cause all rel-alloc()ed data -- including buffer text -- to be relocated.
372 We deal with this by checking for such relocation whenever we have
373 executed a statement that may call malloc() -- or alloca(), which may
374 end up calling malloc() in some circumstances -- and recomputing all
375 of our string pointers in re_match_2_internal() and re_search_2().
376 However, if malloc() or alloca() happens and we don't know about it,
377 we could still be screwed. So we set up a system where we indicate all
378 places where we are prepared for malloc() or alloca(), and in any
379 other circumstances, calls to those functions (from anywhere inside of
380 XEmacs!) will abort(). We do this even when REL_ALLOC is not defined
381 so that we catch these problems sooner, since many developers and beta
382 testers will not be running with REL_ALLOC. */
383 int regex_malloc_disallowed;
384 #define BEGIN_REGEX_MALLOC_OK() regex_malloc_disallowed = 0
385 #define END_REGEX_MALLOC_OK() regex_malloc_disallowed = 1
386 #define UNBIND_REGEX_MALLOC_CHECK() unbind_to (depth)
387 #else
388 #define BEGIN_REGEX_MALLOC_OK()
389 #define END_REGEX_MALLOC_OK()
390 #define UNBIND_REGEX_MALLOC_CHECK()
391 #endif
392
393
303 #ifdef REGEX_MALLOC 394 #ifdef REGEX_MALLOC
304 395
305 #define REGEX_ALLOCATE malloc 396 #define REGEX_ALLOCATE xmalloc
306 #define REGEX_REALLOCATE(source, osize, nsize) realloc (source, nsize) 397 #define REGEX_REALLOCATE(source, osize, nsize) xrealloc (source, nsize)
307 #define REGEX_FREE free 398 #define REGEX_FREE xfree
308 399
309 #else /* not REGEX_MALLOC */ 400 #else /* not REGEX_MALLOC */
310 401
311 /* Emacs already defines alloca, sometimes. */ 402 /* Emacs already defines alloca, sometimes. */
312 #ifndef alloca 403 #ifndef alloca
327 #endif /* HAVE_ALLOCA_H */ 418 #endif /* HAVE_ALLOCA_H */
328 #endif /* __GNUC__ */ 419 #endif /* __GNUC__ */
329 420
330 #endif /* not alloca */ 421 #endif /* not alloca */
331 422
332 #define REGEX_ALLOCATE alloca 423 #define REGEX_ALLOCATE ALLOCA
333 424
334 /* Assumes a `char *destination' variable. */ 425 /* Assumes a `char *destination' variable. */
335 #define REGEX_REALLOCATE(source, osize, nsize) \ 426 #define REGEX_REALLOCATE(source, osize, nsize) \
336 (destination = (char *) alloca (nsize), \ 427 (destination = (char *) ALLOCA (nsize), \
337 memmove (destination, source, osize), \ 428 memmove (destination, source, osize), \
338 destination) 429 destination)
339 430
340 /* No need to do anything to free, after alloca. */ 431 /* No need to do anything to free, after alloca. */
341 #define REGEX_FREE(arg) ((void)0) /* Do nothing! But inhibit gcc warning. */ 432 #define REGEX_FREE(arg) ((void)0) /* Do nothing! But inhibit gcc warning. */
354 445
355 #else /* not REGEX_REL_ALLOC */ 446 #else /* not REGEX_REL_ALLOC */
356 447
357 #ifdef REGEX_MALLOC 448 #ifdef REGEX_MALLOC
358 449
359 #define REGEX_ALLOCATE_STACK malloc 450 #define REGEX_ALLOCATE_STACK xmalloc
360 #define REGEX_REALLOCATE_STACK(source, osize, nsize) realloc (source, nsize) 451 #define REGEX_REALLOCATE_STACK(source, osize, nsize) xrealloc (source, nsize)
361 #define REGEX_FREE_STACK free 452 #define REGEX_FREE_STACK xfree
362 453
363 #else /* not REGEX_MALLOC */ 454 #else /* not REGEX_MALLOC */
364 455
365 #define REGEX_ALLOCATE_STACK alloca 456 #define REGEX_ALLOCATE_STACK ALLOCA
366 457
367 #define REGEX_REALLOCATE_STACK(source, osize, nsize) \ 458 #define REGEX_REALLOCATE_STACK(source, osize, nsize) \
368 REGEX_REALLOCATE (source, osize, nsize) 459 REGEX_REALLOCATE (source, osize, nsize)
369 /* No need to explicitly free anything. */ 460 /* No need to explicitly free anything. */
370 #define REGEX_FREE_STACK(arg) 461 #define REGEX_FREE_STACK(arg)
378 a good thing. */ 469 a good thing. */
379 #define FIRST_STRING_P(ptr) \ 470 #define FIRST_STRING_P(ptr) \
380 (size1 && string1 <= (ptr) && (ptr) <= string1 + size1) 471 (size1 && string1 <= (ptr) && (ptr) <= string1 + size1)
381 472
382 /* (Re)Allocate N items of type T using malloc, or fail. */ 473 /* (Re)Allocate N items of type T using malloc, or fail. */
383 #define TALLOC(n, t) ((t *) malloc ((n) * sizeof (t))) 474 #define TALLOC(n, t) ((t *) xmalloc ((n) * sizeof (t)))
384 #define RETALLOC(addr, n, t) ((addr) = (t *) realloc (addr, (n) * sizeof (t))) 475 #define RETALLOC(addr, n, t) ((addr) = (t *) xrealloc (addr, (n) * sizeof (t)))
385 #define REGEX_TALLOC(n, t) ((t *) REGEX_ALLOCATE ((n) * sizeof (t))) 476 #define REGEX_TALLOC(n, t) ((t *) REGEX_ALLOCATE ((n) * sizeof (t)))
386 477
387 #define BYTEWIDTH 8 /* In bits. */ 478 #define BYTEWIDTH 8 /* In bits. */
388 479
389 #define STREQ(s1, s2) (strcmp (s1, s2) == 0) 480 #define STREQ(s1, s2) (strcmp (s1, s2) == 0)
1112 "Invalid category designator", /* REG_ECATEGORY */ 1203 "Invalid category designator", /* REG_ECATEGORY */
1113 #endif 1204 #endif
1114 }; 1205 };
1115 1206
1116 /* Avoiding alloca during matching, to placate r_alloc. */ 1207 /* Avoiding alloca during matching, to placate r_alloc. */
1208
1209 /* About these various flags:
1210
1211 MATCH_MAY_ALLOCATE indicates that it's OK to do allocation in the
1212 searching and matching functions. In this case, we use local variables
1213 to hold the values allocated. If not, we use *global* variables, which
1214 are pre-allocated. NOTE: XEmacs ***MUST*** run with MATCH_MAY_ALLOCATE,
1215 because the regexp routines may get called reentrantly as a result of
1216 QUIT processing (e.g. under Windows: re_match -> QUIT -> quit_p -> drain
1217 events -> process WM_INITMENU -> call filter -> re_match; see stack
1218 trace in signal.c), so we cannot have any global variables (unless we do
1219 lots of trickiness including some unwind-protects, which isn't worth it
1220 at this point).
1221
1222 REL_ALLOC means that the relocating allocator is in use, for buffers
1223 and such. REGEX_REL_ALLOC means that we use rel-alloc to manage the
1224 fail stack, which may grow quite large. REGEX_MALLOC means we use
1225 malloc() in place of alloca() to allocate the fail stack -- only
1226 applicable if REGEX_REL_ALLOC is not defined.
1227 */
1117 1228
1118 /* Define MATCH_MAY_ALLOCATE unless we need to make sure that the 1229 /* Define MATCH_MAY_ALLOCATE unless we need to make sure that the
1119 searching and matching functions should not call alloca. On some 1230 searching and matching functions should not call alloca. On some
1120 systems, alloca is implemented in terms of malloc, and if we're 1231 systems, alloca is implemented in terms of malloc, and if we're
1121 using the relocating allocator routines, then malloc could cause a 1232 using the relocating allocator routines, then malloc could cause a
1145 input arrives while a matching routine is calling malloc, then 1256 input arrives while a matching routine is calling malloc, then
1146 we're scrod. But Emacs can't just block input while calling 1257 we're scrod. But Emacs can't just block input while calling
1147 matching routines; then we don't notice interrupts when they come 1258 matching routines; then we don't notice interrupts when they come
1148 in. So, Emacs blocks input around all regexp calls except the 1259 in. So, Emacs blocks input around all regexp calls except the
1149 matching calls, which it leaves unprotected, in the faith that they 1260 matching calls, which it leaves unprotected, in the faith that they
1150 will not malloc.]] This previous paragraph is irrelevant. 1261 will not malloc.]] This previous paragraph is irrelevant under XEmacs,
1151 1262 as we *do not* do anything so stupid as process input from within a
1152 XEmacs: We *do not* do anything so stupid as process input from within a 1263 signal handler.
1153 signal handler. However, the regexp routines may get called reentrantly 1264
1154 as a result of QUIT processing (e.g. under Windows: re_match -> QUIT -> 1265 However, the regexp routines may get called reentrantly as a result of
1155 quit_p -> drain events -> process WM_INITMENU -> call filter -> 1266 QUIT processing (e.g. under Windows: re_match -> QUIT -> quit_p -> drain
1156 re_match; see stack trace in signal.c), so we cannot have any global 1267 events -> process WM_INITMENU -> call filter -> re_match; see stack
1157 variables (unless we do lots of trickiness including some 1268 trace in signal.c), so we cannot have any global variables (unless we do
1158 unwind-protects, which isn't worth it at this point). The first 1269 lots of trickiness including some unwind-protects, which isn't worth it
1159 paragraph appears utterly garbled to me -- shouldn't *ANY* use of 1270 at this point). Hence we MUST have MATCH_MAY_ALLOCATE defined.
1160 rel-alloc to different potentially cause buffer data to be relocated? I 1271
1272 Also, the first paragraph does not make complete sense to me -- what
1273 about the use of rel-alloc to handle the fail stacks? Shouldn't these
1274 reallocations potentially cause buffer data to be relocated as well? I
1161 must be missing something, though -- perhaps the writer above is 1275 must be missing something, though -- perhaps the writer above is
1162 assuming that the failure stack(s) will always be allocated after the 1276 assuming that the failure stack(s) will always be allocated after the
1163 buffer data, and thus reallocating them with rel-alloc won't move buffer 1277 buffer data, and thus reallocating them with rel-alloc won't move buffer
1164 data. --ben */ 1278 data. (In fact, a cursory glance at the code in ralloc.c seems to
1279 confirm this.) --ben */
1165 1280
1166 /* Normally, this is fine. */ 1281 /* Normally, this is fine. */
1167 #define MATCH_MAY_ALLOCATE 1282 #define MATCH_MAY_ALLOCATE
1168 1283
1169 /* When using GNU C, we are not REALLY using the C alloca, no matter 1284 /* When using GNU C, we are not REALLY using the C alloca, no matter
1176 and (2) it's not safe for them to use malloc. 1291 and (2) it's not safe for them to use malloc.
1177 Note that if REL_ALLOC is defined, matching would not use malloc for the 1292 Note that if REL_ALLOC is defined, matching would not use malloc for the
1178 failure stack, but we would still use it for the register vectors; 1293 failure stack, but we would still use it for the register vectors;
1179 so REL_ALLOC should not affect this. */ 1294 so REL_ALLOC should not affect this. */
1180 1295
1181 /* XEmacs change emacs -> REL_ALLOC */ 1296 /* XEmacs can handle REL_ALLOC and malloc() OK */
1182 #if (defined (C_ALLOCA) || defined (REGEX_MALLOC)) && defined (REL_ALLOC) 1297 #if !defined (emacs) && (defined (C_ALLOCA) || defined (REGEX_MALLOC)) && defined (REL_ALLOC)
1183 #undef MATCH_MAY_ALLOCATE 1298 #undef MATCH_MAY_ALLOCATE
1184 #endif 1299 #endif
1185 1300
1186 /* #### need better check */ 1301 #if !defined (MATCH_MAY_ALLOCATE) && defined (emacs)
1187
1188 #if !defined (MATCH_MAY_ALLOCATE) && defined (emacs) && defined (HAVE_MS_WINDOWS)
1189 #error regex must be handle reentrancy; MATCH_MAY_ALLOCATE must be defined 1302 #error regex must be handle reentrancy; MATCH_MAY_ALLOCATE must be defined
1190 #endif 1303 #endif
1191 1304
1192 1305
1193 /* Failure stack declarations and macros; both re_compile_fastmap and 1306 /* Failure stack declarations and macros; both re_compile_fastmap and
1236 1349
1237 /* Define macros to initialize and free the failure stack. 1350 /* Define macros to initialize and free the failure stack.
1238 Do `return -2' if the alloc fails. */ 1351 Do `return -2' if the alloc fails. */
1239 1352
1240 #ifdef MATCH_MAY_ALLOCATE 1353 #ifdef MATCH_MAY_ALLOCATE
1241 #define INIT_FAIL_STACK() \ 1354 #define INIT_FAIL_STACK() \
1242 do { \ 1355 do { \
1243 fail_stack.stack = (fail_stack_elt_t *) \ 1356 fail_stack.stack = (fail_stack_elt_t *) \
1244 REGEX_ALLOCATE_STACK (INIT_FAILURE_ALLOC * sizeof (fail_stack_elt_t)); \ 1357 REGEX_ALLOCATE_STACK (INIT_FAILURE_ALLOC * \
1245 \ 1358 sizeof (fail_stack_elt_t)); \
1246 if (fail_stack.stack == NULL) \ 1359 \
1247 return -2; \ 1360 if (fail_stack.stack == NULL) \
1248 \ 1361 { \
1249 fail_stack.size = INIT_FAILURE_ALLOC; \ 1362 UNBIND_REGEX_MALLOC_CHECK (); \
1250 fail_stack.avail = 0; \ 1363 return -2; \
1364 } \
1365 \
1366 fail_stack.size = INIT_FAILURE_ALLOC; \
1367 fail_stack.avail = 0; \
1251 } while (0) 1368 } while (0)
1252 1369
1253 #define RESET_FAIL_STACK() REGEX_FREE_STACK (fail_stack.stack) 1370 #define RESET_FAIL_STACK() REGEX_FREE_STACK (fail_stack.stack)
1254 #else 1371 #else
1255 #define INIT_FAIL_STACK() \ 1372 #define INIT_FAIL_STACK() \
1279 (fail_stack).stack == NULL \ 1396 (fail_stack).stack == NULL \
1280 ? 0 \ 1397 ? 0 \
1281 : ((fail_stack).size <<= 1, \ 1398 : ((fail_stack).size <<= 1, \
1282 1))) 1399 1)))
1283 1400
1401 #if !defined (emacs) || !defined (REL_ALLOC)
1402 #define RE_MATCH_RELOCATE_MOVEABLE_DATA_POINTERS()
1403 #else
1404 /* Don't change NULL pointers */
1405 #define ADD_IF_NZ(val) if (val) val += rmdp_offset
1406 #define RE_MATCH_RELOCATE_MOVEABLE_DATA_POINTERS() \
1407 do \
1408 { \
1409 Bytecount rmdp_offset = offset_post_relocation (lispobj, string1); \
1410 \
1411 if (rmdp_offset) \
1412 { \
1413 int i; \
1414 \
1415 ADD_IF_NZ (string1); \
1416 ADD_IF_NZ (string2); \
1417 ADD_IF_NZ (d); \
1418 ADD_IF_NZ (dend); \
1419 ADD_IF_NZ (end1); \
1420 ADD_IF_NZ (end2); \
1421 ADD_IF_NZ (end_match_1); \
1422 ADD_IF_NZ (end_match_2); \
1423 \
1424 if (bufp->re_ngroups) \
1425 { \
1426 for (i = 0; i < numregs; i++) \
1427 { \
1428 ADD_IF_NZ (regstart[i]); \
1429 ADD_IF_NZ (regend[i]); \
1430 ADD_IF_NZ (old_regstart[i]); \
1431 ADD_IF_NZ (old_regend[i]); \
1432 ADD_IF_NZ (best_regstart[i]); \
1433 ADD_IF_NZ (best_regend[i]); \
1434 ADD_IF_NZ (reg_dummy[i]); \
1435 } \
1436 } \
1437 \
1438 ADD_IF_NZ (match_end); \
1439 } \
1440 } while (0)
1441 #endif /* !defined (emacs) || !defined (REL_ALLOC) */
1442
1443 #if !defined (emacs) || !defined (REL_ALLOC)
1444 #define RE_SEARCH_RELOCATE_MOVEABLE_DATA_POINTERS()
1445 #else
1446 #define RE_SEARCH_RELOCATE_MOVEABLE_DATA_POINTERS() \
1447 do \
1448 { \
1449 Bytecount rmdp_offset = offset_post_relocation (lispobj, str1); \
1450 \
1451 if (rmdp_offset) \
1452 { \
1453 int i; \
1454 \
1455 ADD_IF_NZ (str1); \
1456 ADD_IF_NZ (str2); \
1457 ADD_IF_NZ (string1); \
1458 ADD_IF_NZ (string2); \
1459 ADD_IF_NZ (d); \
1460 \
1461 \
1462 \
1463 ADD_IF_NZ (dend); \
1464 ADD_IF_NZ (end1); \
1465 ADD_IF_NZ (end2); \
1466 ADD_IF_NZ (end_match_1); \
1467 ADD_IF_NZ (end_match_2); \
1468 \
1469 if (bufp->re_ngroups) \
1470 { \
1471 for (i = 0; i < numregs; i++) \
1472 { \
1473 ADD_IF_NZ (regstart[i]); \
1474 ADD_IF_NZ (regend[i]); \
1475 ADD_IF_NZ (old_regstart[i]); \
1476 ADD_IF_NZ (old_regend[i]); \
1477 ADD_IF_NZ (best_regstart[i]); \
1478 ADD_IF_NZ (best_regend[i]); \
1479 ADD_IF_NZ (reg_dummy[i]); \
1480 } \
1481 } \
1482 \
1483 ADD_IF_NZ (match_end); \
1484 } \
1485 } while (0)
1486
1487 #endif /* emacs */
1284 1488
1285 /* Push pointer POINTER on FAIL_STACK. 1489 /* Push pointer POINTER on FAIL_STACK.
1286 Return 1 if was able to do so and 0 if ran out of memory allocating 1490 Return 1 if was able to do so and 0 if ran out of memory allocating
1287 space to do so. */ 1491 space to do so. */
1288 #define PUSH_PATTERN_OP(POINTER, FAIL_STACK) \ 1492 #define PUSH_PATTERN_OP(POINTER, FAIL_STACK) \
1361 (long) REMAINING_AVAIL_SLOTS); \ 1565 (long) REMAINING_AVAIL_SLOTS); \
1362 \ 1566 \
1363 /* Ensure we have enough space allocated for what we will push. */ \ 1567 /* Ensure we have enough space allocated for what we will push. */ \
1364 while (REMAINING_AVAIL_SLOTS < NUM_FAILURE_ITEMS) \ 1568 while (REMAINING_AVAIL_SLOTS < NUM_FAILURE_ITEMS) \
1365 { \ 1569 { \
1570 BEGIN_REGEX_MALLOC_OK (); \
1366 if (!DOUBLE_FAIL_STACK (fail_stack)) \ 1571 if (!DOUBLE_FAIL_STACK (fail_stack)) \
1367 return failure_code; \ 1572 { \
1368 \ 1573 END_REGEX_MALLOC_OK (); \
1574 UNBIND_REGEX_MALLOC_CHECK (); \
1575 return failure_code; \
1576 } \
1577 END_REGEX_MALLOC_OK (); \
1369 DEBUG_PRINT2 ("\n Doubled stack; size now: %ld\n", \ 1578 DEBUG_PRINT2 ("\n Doubled stack; size now: %ld\n", \
1370 (long) (fail_stack).size); \ 1579 (long) (fail_stack).size); \
1371 DEBUG_PRINT2 (" slots available: %ld\n", \ 1580 DEBUG_PRINT2 (" slots available: %ld\n", \
1372 (long) REMAINING_AVAIL_SLOTS); \ 1581 (long) REMAINING_AVAIL_SLOTS); \
1582 \
1583 RE_MATCH_RELOCATE_MOVEABLE_DATA_POINTERS (); \
1373 } \ 1584 } \
1374 \ 1585 \
1375 /* Push the info, starting with the registers. */ \ 1586 /* Push the info, starting with the registers. */ \
1376 DEBUG_PRINT1 ("\n"); \ 1587 DEBUG_PRINT1 ("\n"); \
1377 \ 1588 \
1674 1885
1675 /* Extend the buffer by twice its current size via realloc and 1886 /* Extend the buffer by twice its current size via realloc and
1676 reset the pointers that pointed into the old block to point to the 1887 reset the pointers that pointed into the old block to point to the
1677 correct places in the new one. If extending the buffer results in it 1888 correct places in the new one. If extending the buffer results in it
1678 being larger than MAX_BUF_SIZE, then flag memory exhausted. */ 1889 being larger than MAX_BUF_SIZE, then flag memory exhausted. */
1679 #define EXTEND_BUFFER() \ 1890 #define EXTEND_BUFFER() \
1680 do { \ 1891 do { \
1681 re_char *old_buffer = bufp->buffer; \ 1892 re_char *old_buffer = bufp->buffer; \
1682 if (bufp->allocated == MAX_BUF_SIZE) \ 1893 if (bufp->allocated == MAX_BUF_SIZE) \
1683 return REG_ESIZE; \ 1894 return REG_ESIZE; \
1684 bufp->allocated <<= 1; \ 1895 bufp->allocated <<= 1; \
1685 if (bufp->allocated > MAX_BUF_SIZE) \ 1896 if (bufp->allocated > MAX_BUF_SIZE) \
1686 bufp->allocated = MAX_BUF_SIZE; \ 1897 bufp->allocated = MAX_BUF_SIZE; \
1687 bufp->buffer = (unsigned char *) realloc (bufp->buffer, bufp->allocated);\ 1898 bufp->buffer = \
1688 if (bufp->buffer == NULL) \ 1899 (unsigned char *) xrealloc (bufp->buffer, bufp->allocated); \
1689 return REG_ESPACE; \ 1900 if (bufp->buffer == NULL) \
1690 /* If the buffer moved, move all the pointers into it. */ \ 1901 return REG_ESPACE; \
1691 if (old_buffer != bufp->buffer) \ 1902 /* If the buffer moved, move all the pointers into it. */ \
1692 { \ 1903 if (old_buffer != bufp->buffer) \
1693 buf_end = (buf_end - old_buffer) + bufp->buffer; \ 1904 { \
1694 begalt = (begalt - old_buffer) + bufp->buffer; \ 1905 buf_end = (buf_end - old_buffer) + bufp->buffer; \
1695 if (fixup_alt_jump) \ 1906 begalt = (begalt - old_buffer) + bufp->buffer; \
1696 fixup_alt_jump = (fixup_alt_jump - old_buffer) + bufp->buffer;\ 1907 if (fixup_alt_jump) \
1697 if (laststart) \ 1908 fixup_alt_jump = (fixup_alt_jump - old_buffer) + bufp->buffer; \
1698 laststart = (laststart - old_buffer) + bufp->buffer; \ 1909 if (laststart) \
1699 if (pending_exact) \ 1910 laststart = (laststart - old_buffer) + bufp->buffer; \
1700 pending_exact = (pending_exact - old_buffer) + bufp->buffer; \ 1911 if (pending_exact) \
1701 } \ 1912 pending_exact = (pending_exact - old_buffer) + bufp->buffer; \
1913 } \
1702 } while (0) 1914 } while (0)
1703 1915
1704 1916
1705 /* Since we have one byte reserved for the register number argument to 1917 /* Since we have one byte reserved for the register number argument to
1706 {start,stop}_memory, the maximum number of groups we can report 1918 {start,stop}_memory, the maximum number of groups we can report
1911 The `fastmap' and `newline_anchor' fields are neither 2123 The `fastmap' and `newline_anchor' fields are neither
1912 examined nor set. */ 2124 examined nor set. */
1913 2125
1914 /* Return, freeing storage we allocated. */ 2126 /* Return, freeing storage we allocated. */
1915 #define FREE_STACK_RETURN(value) \ 2127 #define FREE_STACK_RETURN(value) \
1916 return (free (compile_stack.stack), value) 2128 do \
2129 { \
2130 xfree (compile_stack.stack); \
2131 return value; \
2132 } while (0)
1917 2133
1918 static reg_errcode_t 2134 static reg_errcode_t
1919 regex_compile (re_char *pattern, int size, reg_syntax_t syntax, 2135 regex_compile (re_char *pattern, int size, reg_syntax_t syntax,
1920 struct re_pattern_buffer *bufp) 2136 struct re_pattern_buffer *bufp)
1921 { 2137 {
3214 /* If we don't want backtracking, force success 3430 /* If we don't want backtracking, force success
3215 the first time we reach the end of the compiled pattern. */ 3431 the first time we reach the end of the compiled pattern. */
3216 if (syntax & RE_NO_POSIX_BACKTRACKING) 3432 if (syntax & RE_NO_POSIX_BACKTRACKING)
3217 BUF_PUSH (succeed); 3433 BUF_PUSH (succeed);
3218 3434
3219 free (compile_stack.stack); 3435 xfree (compile_stack.stack);
3220 3436
3221 /* We have succeeded; set the length of the buffer. */ 3437 /* We have succeeded; set the length of the buffer. */
3222 bufp->used = buf_end - bufp->buffer; 3438 bufp->used = buf_end - bufp->buffer;
3223 3439
3224 #ifdef DEBUG 3440 #ifdef DEBUG
3241 is 2 * re_max_failures failure points. */ 3457 is 2 * re_max_failures failure points. */
3242 if (fail_stack.size < (2 * re_max_failures * MAX_FAILURE_ITEMS)) 3458 if (fail_stack.size < (2 * re_max_failures * MAX_FAILURE_ITEMS))
3243 { 3459 {
3244 fail_stack.size = (2 * re_max_failures * MAX_FAILURE_ITEMS); 3460 fail_stack.size = (2 * re_max_failures * MAX_FAILURE_ITEMS);
3245 3461
3246 #ifdef emacs
3247 if (! fail_stack.stack) 3462 if (! fail_stack.stack)
3248 fail_stack.stack 3463 fail_stack.stack
3249 = (fail_stack_elt_t *) xmalloc (fail_stack.size 3464 = (fail_stack_elt_t *) xmalloc (fail_stack.size
3250 * sizeof (fail_stack_elt_t)); 3465 * sizeof (fail_stack_elt_t));
3251 else 3466 else
3252 fail_stack.stack 3467 fail_stack.stack
3253 = (fail_stack_elt_t *) xrealloc (fail_stack.stack, 3468 = (fail_stack_elt_t *) xrealloc (fail_stack.stack,
3254 (fail_stack.size 3469 (fail_stack.size
3255 * sizeof (fail_stack_elt_t))); 3470 * sizeof (fail_stack_elt_t)));
3256 #else /* not emacs */
3257 if (! fail_stack.stack)
3258 fail_stack.stack
3259 = (fail_stack_elt_t *) malloc (fail_stack.size
3260 * sizeof (fail_stack_elt_t));
3261 else
3262 fail_stack.stack
3263 = (fail_stack_elt_t *) realloc (fail_stack.stack,
3264 (fail_stack.size
3265 * sizeof (fail_stack_elt_t)));
3266 #endif /* emacs */
3267 } 3471 }
3268 3472
3269 regex_grow_registers (num_regs); 3473 regex_grow_registers (num_regs);
3270 } 3474 }
3271 #endif /* not MATCH_MAY_ALLOCATE */ 3475 #endif /* not MATCH_MAY_ALLOCATE */
3546 match the empty string. */ 3750 match the empty string. */
3547 re_bool path_can_be_null = true; 3751 re_bool path_can_be_null = true;
3548 3752
3549 /* We aren't doing a `succeed_n' to begin with. */ 3753 /* We aren't doing a `succeed_n' to begin with. */
3550 re_bool succeed_n_p = false; 3754 re_bool succeed_n_p = false;
3755
3756 #ifdef ERROR_CHECK_MALLOC
3757 /* The pattern comes from string data, not buffer data. We don't access
3758 any buffer data, so we don't have to worry about malloc() (but the
3759 disallowed flag may have been set by a caller). */
3760 int depth = bind_regex_malloc_disallowed (0);
3761 #endif
3551 3762
3552 assert (fastmap != NULL && p != NULL); 3763 assert (fastmap != NULL && p != NULL);
3553 3764
3554 INIT_FAIL_STACK (); 3765 INIT_FAIL_STACK ();
3555 memset (fastmap, 0, 1 << BYTEWIDTH); /* Assume nothing's valid. */ 3766 memset (fastmap, 0, 1 << BYTEWIDTH); /* Assume nothing's valid. */
3836 #ifdef MULE 4047 #ifdef MULE
3837 /* 97/2/17 jhod category patch */ 4048 /* 97/2/17 jhod category patch */
3838 case categoryspec: 4049 case categoryspec:
3839 case notcategoryspec: 4050 case notcategoryspec:
3840 bufp->can_be_null = 1; 4051 bufp->can_be_null = 1;
4052 UNBIND_REGEX_MALLOC_CHECK ();
3841 return 0; 4053 return 0;
3842 /* end if category patch */ 4054 /* end if category patch */
3843 #endif /* MULE */ 4055 #endif /* MULE */
3844 4056
3845 /* All cases after this match the empty string. These end with 4057 /* All cases after this match the empty string. These end with
3913 if (p + j < pend) 4125 if (p + j < pend)
3914 { 4126 {
3915 if (!PUSH_PATTERN_OP (p + j, fail_stack)) 4127 if (!PUSH_PATTERN_OP (p + j, fail_stack))
3916 { 4128 {
3917 RESET_FAIL_STACK (); 4129 RESET_FAIL_STACK ();
4130 UNBIND_REGEX_MALLOC_CHECK ();
3918 return -2; 4131 return -2;
3919 } 4132 }
3920 } 4133 }
3921 else 4134 else
3922 bufp->can_be_null = 1; 4135 bufp->can_be_null = 1;
3974 pattern is empty). */ 4187 pattern is empty). */
3975 bufp->can_be_null |= path_can_be_null; 4188 bufp->can_be_null |= path_can_be_null;
3976 4189
3977 done: 4190 done:
3978 RESET_FAIL_STACK (); 4191 RESET_FAIL_STACK ();
4192 UNBIND_REGEX_MALLOC_CHECK ();
3979 return 0; 4193 return 0;
3980 } /* re_compile_fastmap */ 4194 } /* re_compile_fastmap */
3981 4195
3982 /* Set REGS to hold NUM_REGS registers, storing them in STARTS and 4196 /* Set REGS to hold NUM_REGS registers, storing them in STARTS and
3983 ENDS. Subsequent matches using PATTERN_BUFFER and REGS will use 4197 ENDS. Subsequent matches using PATTERN_BUFFER and REGS will use
4070 int anchored_at_begline = 0; 4284 int anchored_at_begline = 0;
4071 #endif 4285 #endif
4072 re_char *d; 4286 re_char *d;
4073 #ifdef emacs 4287 #ifdef emacs
4074 Internal_Format fmt = buffer_or_other_internal_format (lispobj); 4288 Internal_Format fmt = buffer_or_other_internal_format (lispobj);
4289 #ifdef ERROR_CHECK_MALLOC
4290 int depth;
4291 #endif
4075 #endif /* emacs */ 4292 #endif /* emacs */
4076 #if 1 4293 #if 1
4077 int forward_search_p; 4294 int forward_search_p;
4078 #endif 4295 #endif
4079 4296
4118 if (range < 0) 4335 if (range < 0)
4119 return -1; 4336 return -1;
4120 } 4337 }
4121 #endif /* emacs */ 4338 #endif /* emacs */
4122 4339
4340 #ifdef ERROR_CHECK_MALLOC
4341 /* Do this after the above return()s. */
4342 depth = bind_regex_malloc_disallowed (1);
4343 #endif
4344
4123 /* Update the fastmap now if not correct already. */ 4345 /* Update the fastmap now if not correct already. */
4346 BEGIN_REGEX_MALLOC_OK ();
4124 if (fastmap && !bufp->fastmap_accurate) 4347 if (fastmap && !bufp->fastmap_accurate)
4125 if (re_compile_fastmap (bufp RE_LISP_SHORT_CONTEXT_ARGS) == -2) 4348 if (re_compile_fastmap (bufp RE_LISP_SHORT_CONTEXT_ARGS) == -2)
4126 return -2; 4349 {
4350 END_REGEX_MALLOC_OK ();
4351 UNBIND_REGEX_MALLOC_CHECK ();
4352 return -2;
4353 }
4354
4355 END_REGEX_MALLOC_OK ();
4356 RE_SEARCH_RELOCATE_MOVEABLE_DATA_POINTERS ();
4127 4357
4128 #ifdef REGEX_BEGLINE_CHECK 4358 #ifdef REGEX_BEGLINE_CHECK
4129 { 4359 {
4130 long i = 0; 4360 long i = 0;
4131 4361
4140 anchored_at_begline = i < bufp->used && bufp->buffer[i] == begline; 4370 anchored_at_begline = i < bufp->used && bufp->buffer[i] == begline;
4141 } 4371 }
4142 #endif 4372 #endif
4143 4373
4144 #ifdef emacs 4374 #ifdef emacs
4375 BEGIN_REGEX_MALLOC_OK ();
4145 scache = setup_syntax_cache (scache, lispobj, lispbuf, 4376 scache = setup_syntax_cache (scache, lispobj, lispbuf,
4146 offset_to_charxpos (lispobj, startpos), 4377 offset_to_charxpos (lispobj, startpos),
4147 1); 4378 1);
4379 END_REGEX_MALLOC_OK ();
4380 RE_SEARCH_RELOCATE_MOVEABLE_DATA_POINTERS ();
4148 #endif 4381 #endif
4149 4382
4150 /* Loop through the string, looking for a place to start matching. */ 4383 /* Loop through the string, looking for a place to start matching. */
4151 for (;;) 4384 for (;;)
4152 { 4385 {
4272 break; 4505 break;
4273 #endif /* MULE */ 4506 #endif /* MULE */
4274 INC_IBYTEPTR_FMT (d, fmt); 4507 INC_IBYTEPTR_FMT (d, fmt);
4275 range -= (d - old_d); 4508 range -= (d - old_d);
4276 #if 1 4509 #if 1
4277 assert (!forward_search_p || range >= 0); 4510 assert (!forward_search_p || range >= 0);
4278 #endif 4511 #endif
4279 } 4512 }
4280 } 4513 }
4281 #ifdef MULE 4514 #ifdef MULE
4282 else if (fmt != FORMAT_DEFAULT) 4515 else if (fmt != FORMAT_DEFAULT)
4290 if (fastmap[*tempch]) 4523 if (fastmap[*tempch])
4291 break; 4524 break;
4292 INC_IBYTEPTR_FMT (d, fmt); 4525 INC_IBYTEPTR_FMT (d, fmt);
4293 range -= (d - old_d); 4526 range -= (d - old_d);
4294 #if 1 4527 #if 1
4295 assert (!forward_search_p || range >= 0); 4528 assert (!forward_search_p || range >= 0);
4296 #endif 4529 #endif
4297 } 4530 }
4298 } 4531 }
4299 #endif /* MULE */ 4532 #endif /* MULE */
4300 else 4533 else
4336 } 4569 }
4337 4570
4338 /* If can't match the null string, and that's all we have left, fail. */ 4571 /* If can't match the null string, and that's all we have left, fail. */
4339 if (range >= 0 && startpos == total_size && fastmap 4572 if (range >= 0 && startpos == total_size && fastmap
4340 && !bufp->can_be_null) 4573 && !bufp->can_be_null)
4341 return -1; 4574 {
4575 UNBIND_REGEX_MALLOC_CHECK ();
4576 return -1;
4577 }
4342 4578
4343 #ifdef emacs /* XEmacs added, w/removal of immediate_quit */ 4579 #ifdef emacs /* XEmacs added, w/removal of immediate_quit */
4344 if (!no_quit_in_re_search) 4580 if (!no_quit_in_re_search)
4345 QUIT; 4581 {
4346 #endif 4582 BEGIN_REGEX_MALLOC_OK ();
4583 QUIT;
4584 END_REGEX_MALLOC_OK ();
4585 RE_SEARCH_RELOCATE_MOVEABLE_DATA_POINTERS ();
4586 }
4587
4588 #endif
4589 BEGIN_REGEX_MALLOC_OK ();
4347 val = re_match_2_internal (bufp, string1, size1, string2, size2, 4590 val = re_match_2_internal (bufp, string1, size1, string2, size2,
4348 startpos, regs, stop 4591 startpos, regs, stop
4349 RE_LISP_CONTEXT_ARGS); 4592 RE_LISP_CONTEXT_ARGS);
4350 #ifndef REGEX_MALLOC 4593 #ifndef REGEX_MALLOC
4351 #ifdef C_ALLOCA 4594 ALLOCA_GARBAGE_COLLECT ();
4352 alloca (0); 4595 #endif
4353 #endif 4596 END_REGEX_MALLOC_OK ();
4354 #endif 4597 RE_SEARCH_RELOCATE_MOVEABLE_DATA_POINTERS ();
4355 4598
4356 if (val >= 0) 4599 if (val >= 0)
4357 return startpos; 4600 {
4601 UNBIND_REGEX_MALLOC_CHECK ();
4602 return startpos;
4603 }
4358 4604
4359 if (val == -2) 4605 if (val == -2)
4360 return -2; 4606 {
4361 4607 UNBIND_REGEX_MALLOC_CHECK ();
4608 return -2;
4609 }
4610
4611 RE_SEARCH_RELOCATE_MOVEABLE_DATA_POINTERS ();
4362 advance: 4612 advance:
4363 if (!range) 4613 if (!range)
4364 break; 4614 break;
4365 else if (range > 0) 4615 else if (range > 0)
4366 { 4616 {
4388 assert (!forward_search_p || range >= 0); 4638 assert (!forward_search_p || range >= 0);
4389 #endif 4639 #endif
4390 startpos -= d_size; 4640 startpos -= d_size;
4391 } 4641 }
4392 } 4642 }
4643 UNBIND_REGEX_MALLOC_CHECK ();
4393 return -1; 4644 return -1;
4394 } /* re_search_2 */ 4645 } /* re_search_2 */
4395 4646
4396 4647
4397 /* Declarations and macros for re_match_2. */ 4648 /* Declarations and macros for re_match_2. */
4440 /* Free everything we malloc. */ 4691 /* Free everything we malloc. */
4441 #ifdef MATCH_MAY_ALLOCATE 4692 #ifdef MATCH_MAY_ALLOCATE
4442 #define FREE_VAR(var) if (var) REGEX_FREE (var); var = NULL 4693 #define FREE_VAR(var) if (var) REGEX_FREE (var); var = NULL
4443 #define FREE_VARIABLES() \ 4694 #define FREE_VARIABLES() \
4444 do { \ 4695 do { \
4696 UNBIND_REGEX_MALLOC_CHECK (); \
4445 REGEX_FREE_STACK (fail_stack.stack); \ 4697 REGEX_FREE_STACK (fail_stack.stack); \
4446 FREE_VAR (regstart); \ 4698 FREE_VAR (regstart); \
4447 FREE_VAR (regend); \ 4699 FREE_VAR (regend); \
4448 FREE_VAR (old_regstart); \ 4700 FREE_VAR (old_regstart); \
4449 FREE_VAR (old_regend); \ 4701 FREE_VAR (old_regend); \
4452 FREE_VAR (reg_info); \ 4704 FREE_VAR (reg_info); \
4453 FREE_VAR (reg_dummy); \ 4705 FREE_VAR (reg_dummy); \
4454 FREE_VAR (reg_info_dummy); \ 4706 FREE_VAR (reg_info_dummy); \
4455 } while (0) 4707 } while (0)
4456 #else /* not MATCH_MAY_ALLOCATE */ 4708 #else /* not MATCH_MAY_ALLOCATE */
4457 #define FREE_VARIABLES() ((void)0) /* Do nothing! But inhibit gcc warning. */ 4709 #define FREE_VARIABLES() \
4710 do { \
4711 UNBIND_REGEX_MALLOC_CHECK (); \
4712 } while (0)
4458 #endif /* MATCH_MAY_ALLOCATE */ 4713 #endif /* MATCH_MAY_ALLOCATE */
4459 4714
4460 /* These values must meet several constraints. They must not be valid 4715 /* These values must meet several constraints. They must not be valid
4461 register values; since we have a limit of 255 registers (because 4716 register values; since we have a limit of 255 registers (because
4462 we use only one byte in the pattern for the register number), we can 4717 we use only one byte in the pattern for the register number), we can
4478 RE_LISP_CONTEXT_ARGS_DECL) 4733 RE_LISP_CONTEXT_ARGS_DECL)
4479 { 4734 {
4480 int result = re_match_2_internal (bufp, NULL, 0, (re_char *) string, size, 4735 int result = re_match_2_internal (bufp, NULL, 0, (re_char *) string, size,
4481 pos, regs, size 4736 pos, regs, size
4482 RE_LISP_CONTEXT_ARGS); 4737 RE_LISP_CONTEXT_ARGS);
4483 alloca (0); 4738 ALLOCA_GARBAGE_COLLECT ();
4484 return result; 4739 return result;
4485 } 4740 }
4486 #endif /* not emacs */ 4741 #endif /* not emacs */
4487 4742
4488 /* re_match_2 matches the compiled pattern in BUFP against the 4743 /* re_match_2 matches the compiled pattern in BUFP against the
4515 result = re_match_2_internal (bufp, (re_char *) string1, size1, 4770 result = re_match_2_internal (bufp, (re_char *) string1, size1,
4516 (re_char *) string2, size2, 4771 (re_char *) string2, size2,
4517 pos, regs, stop 4772 pos, regs, stop
4518 RE_LISP_CONTEXT_ARGS); 4773 RE_LISP_CONTEXT_ARGS);
4519 4774
4520 alloca (0); 4775 ALLOCA_GARBAGE_COLLECT ();
4521 return result; 4776 return result;
4522 } 4777 }
4523 4778
4524 /* This is a separate function so that we can force an alloca cleanup 4779 /* This is a separate function so that we can force an alloca cleanup
4525 afterwards. */ 4780 afterwards. */
4657 /* 1 if this match is the best seen so far. */ 4912 /* 1 if this match is the best seen so far. */
4658 re_bool best_match_p; 4913 re_bool best_match_p;
4659 4914
4660 #ifdef emacs 4915 #ifdef emacs
4661 Internal_Format fmt = buffer_or_other_internal_format (lispobj); 4916 Internal_Format fmt = buffer_or_other_internal_format (lispobj);
4917 #ifdef ERROR_CHECK_MALLOC
4918 int depth = bind_regex_malloc_disallowed (1);
4919 #endif
4662 #endif /* emacs */ 4920 #endif /* emacs */
4663 4921
4664 DEBUG_PRINT1 ("\n\nEntering re_match_2.\n"); 4922 DEBUG_PRINT1 ("\n\nEntering re_match_2.\n");
4665 4923
4924 BEGIN_REGEX_MALLOC_OK ();
4666 INIT_FAIL_STACK (); 4925 INIT_FAIL_STACK ();
4926 END_REGEX_MALLOC_OK ();
4667 4927
4668 #ifdef MATCH_MAY_ALLOCATE 4928 #ifdef MATCH_MAY_ALLOCATE
4669 /* Do not bother to initialize all the register variables if there are 4929 /* Do not bother to initialize all the register variables if there are
4670 no groups in the pattern, as it takes a fair amount of time. If 4930 no groups in the pattern, as it takes a fair amount of time. If
4671 there are groups, we include space for register 0 (the whole 4931 there are groups, we include space for register 0 (the whole
4672 pattern), even though we never use it, since it simplifies the 4932 pattern), even though we never use it, since it simplifies the
4673 array indexing. We should fix this. */ 4933 array indexing. We should fix this. */
4674 if (bufp->re_ngroups) 4934 if (bufp->re_ngroups)
4675 { 4935 {
4936 BEGIN_REGEX_MALLOC_OK ();
4676 regstart = REGEX_TALLOC (num_regs, re_char *); 4937 regstart = REGEX_TALLOC (num_regs, re_char *);
4677 regend = REGEX_TALLOC (num_regs, re_char *); 4938 regend = REGEX_TALLOC (num_regs, re_char *);
4678 old_regstart = REGEX_TALLOC (num_regs, re_char *); 4939 old_regstart = REGEX_TALLOC (num_regs, re_char *);
4679 old_regend = REGEX_TALLOC (num_regs, re_char *); 4940 old_regend = REGEX_TALLOC (num_regs, re_char *);
4680 best_regstart = REGEX_TALLOC (num_regs, re_char *); 4941 best_regstart = REGEX_TALLOC (num_regs, re_char *);
4681 best_regend = REGEX_TALLOC (num_regs, re_char *); 4942 best_regend = REGEX_TALLOC (num_regs, re_char *);
4682 reg_info = REGEX_TALLOC (num_regs, register_info_type); 4943 reg_info = REGEX_TALLOC (num_regs, register_info_type);
4683 reg_dummy = REGEX_TALLOC (num_regs, re_char *); 4944 reg_dummy = REGEX_TALLOC (num_regs, re_char *);
4684 reg_info_dummy = REGEX_TALLOC (num_regs, register_info_type); 4945 reg_info_dummy = REGEX_TALLOC (num_regs, register_info_type);
4946 END_REGEX_MALLOC_OK ();
4685 4947
4686 if (!(regstart && regend && old_regstart && old_regend && reg_info 4948 if (!(regstart && regend && old_regstart && old_regend && reg_info
4687 && best_regstart && best_regend && reg_dummy && reg_info_dummy)) 4949 && best_regstart && best_regend && reg_dummy && reg_info_dummy))
4688 { 4950 {
4689 FREE_VARIABLES (); 4951 FREE_VARIABLES ();
4698 = best_regend = reg_dummy = NULL; 4960 = best_regend = reg_dummy = NULL;
4699 reg_info = reg_info_dummy = (register_info_type *) NULL; 4961 reg_info = reg_info_dummy = (register_info_type *) NULL;
4700 } 4962 }
4701 #endif /* MATCH_MAY_ALLOCATE */ 4963 #endif /* MATCH_MAY_ALLOCATE */
4702 4964
4965 #if defined (emacs) && defined (REL_ALLOC)
4966 {
4967 /* If the allocations above (or the call to setup_syntax_cache() in
4968 re_match_2) caused a rel-alloc relocation, then fix up the data
4969 pointers */
4970 Bytecount offset = offset_post_relocation (lispobj, string1);
4971 if (offset)
4972 {
4973 string1 += offset;
4974 string2 += offset;
4975 }
4976 }
4977 #endif /* defined (emacs) && defined (REL_ALLOC) */
4978
4703 /* The starting position is bogus. */ 4979 /* The starting position is bogus. */
4704 if (pos < 0 || pos > size1 + size2) 4980 if (pos < 0 || pos > size1 + size2)
4705 { 4981 {
4706 FREE_VARIABLES (); 4982 FREE_VARIABLES ();
4707 return -1; 4983 return -1;
4773 for (;;) 5049 for (;;)
4774 { 5050 {
4775 DEBUG_PRINT2 ("\n0x%lx: ", (long) p); 5051 DEBUG_PRINT2 ("\n0x%lx: ", (long) p);
4776 #ifdef emacs /* XEmacs added, w/removal of immediate_quit */ 5052 #ifdef emacs /* XEmacs added, w/removal of immediate_quit */
4777 if (!no_quit_in_re_search) 5053 if (!no_quit_in_re_search)
4778 QUIT; 5054 {
5055 BEGIN_REGEX_MALLOC_OK ();
5056 QUIT;
5057 END_REGEX_MALLOC_OK ();
5058 RE_SEARCH_RELOCATE_MOVEABLE_DATA_POINTERS ();
5059 }
4779 #endif 5060 #endif
4780 5061
4781 if (p == pend) 5062 if (p == pend)
4782 { /* End of pattern means we might have succeeded. */ 5063 { /* End of pattern means we might have succeeded. */
4783 DEBUG_PRINT1 ("end of pattern ... "); 5064 DEBUG_PRINT1 ("end of pattern ... ");
4855 if (bufp->regs_allocated == REGS_UNALLOCATED) 5136 if (bufp->regs_allocated == REGS_UNALLOCATED)
4856 { /* No. So allocate them with malloc. We need one 5137 { /* No. So allocate them with malloc. We need one
4857 extra element beyond `num_regs' for the `-1' marker 5138 extra element beyond `num_regs' for the `-1' marker
4858 GNU code uses. */ 5139 GNU code uses. */
4859 regs->num_regs = MAX (RE_NREGS, num_nonshy_regs + 1); 5140 regs->num_regs = MAX (RE_NREGS, num_nonshy_regs + 1);
5141 BEGIN_REGEX_MALLOC_OK ();
4860 regs->start = TALLOC (regs->num_regs, regoff_t); 5142 regs->start = TALLOC (regs->num_regs, regoff_t);
4861 regs->end = TALLOC (regs->num_regs, regoff_t); 5143 regs->end = TALLOC (regs->num_regs, regoff_t);
5144 END_REGEX_MALLOC_OK ();
5145 RE_MATCH_RELOCATE_MOVEABLE_DATA_POINTERS ();
4862 if (regs->start == NULL || regs->end == NULL) 5146 if (regs->start == NULL || regs->end == NULL)
4863 { 5147 {
4864 FREE_VARIABLES (); 5148 FREE_VARIABLES ();
4865 return -2; 5149 return -2;
4866 } 5150 }
4871 allocated, reallocate them. If we need fewer, just 5155 allocated, reallocate them. If we need fewer, just
4872 leave it alone. */ 5156 leave it alone. */
4873 if (regs->num_regs < num_nonshy_regs + 1) 5157 if (regs->num_regs < num_nonshy_regs + 1)
4874 { 5158 {
4875 regs->num_regs = num_nonshy_regs + 1; 5159 regs->num_regs = num_nonshy_regs + 1;
5160 BEGIN_REGEX_MALLOC_OK ();
4876 RETALLOC (regs->start, regs->num_regs, regoff_t); 5161 RETALLOC (regs->start, regs->num_regs, regoff_t);
4877 RETALLOC (regs->end, regs->num_regs, regoff_t); 5162 RETALLOC (regs->end, regs->num_regs, regoff_t);
5163 END_REGEX_MALLOC_OK ();
5164 RE_MATCH_RELOCATE_MOVEABLE_DATA_POINTERS ();
4878 if (regs->start == NULL || regs->end == NULL) 5165 if (regs->start == NULL || regs->end == NULL)
4879 { 5166 {
4880 FREE_VARIABLES (); 5167 FREE_VARIABLES ();
4881 return -2; 5168 return -2;
4882 } 5169 }
5813 emch2 = itext_ichar_fmt (d_after, fmt, lispobj); 6100 emch2 = itext_ichar_fmt (d_after, fmt, lispobj);
5814 6101
5815 #ifdef emacs 6102 #ifdef emacs
5816 pos_before = 6103 pos_before =
5817 offset_to_charxpos (lispobj, PTR_TO_OFFSET (d)) - 1; 6104 offset_to_charxpos (lispobj, PTR_TO_OFFSET (d)) - 1;
6105 BEGIN_REGEX_MALLOC_OK ();
5818 UPDATE_SYNTAX_CACHE (scache, pos_before); 6106 UPDATE_SYNTAX_CACHE (scache, pos_before);
5819 #endif 6107 #endif
5820 syn1 = SYNTAX_FROM_CACHE (scache, emch1); 6108 syn1 = SYNTAX_FROM_CACHE (scache, emch1);
5821 #ifdef emacs 6109 #ifdef emacs
5822 UPDATE_SYNTAX_CACHE_FORWARD (scache, pos_before + 1); 6110 UPDATE_SYNTAX_CACHE_FORWARD (scache, pos_before + 1);
5823 #endif 6111 #endif
5824 syn2 = SYNTAX_FROM_CACHE (scache, emch2); 6112 syn2 = SYNTAX_FROM_CACHE (scache, emch2);
5825 6113
5826 result = ((syn1 == Sword) != (syn2 == Sword)); 6114 result = ((syn1 == Sword) != (syn2 == Sword));
6115 END_REGEX_MALLOC_OK ();
6116 RE_MATCH_RELOCATE_MOVEABLE_DATA_POINTERS ();
5827 } 6117 }
5828 if (result == should_succeed) 6118 if (result == should_succeed)
5829 break; 6119 break;
5830 goto fail; 6120 goto fail;
5831 } 6121 }
5846 break; 6136 break;
5847 6137
5848 */ 6138 */
5849 re_char *dtmp = POS_AFTER_GAP_UNSAFE (d); 6139 re_char *dtmp = POS_AFTER_GAP_UNSAFE (d);
5850 Ichar emch = itext_ichar_fmt (dtmp, fmt, lispobj); 6140 Ichar emch = itext_ichar_fmt (dtmp, fmt, lispobj);
6141 int tempres;
6142 BEGIN_REGEX_MALLOC_OK ();
5851 #ifdef emacs 6143 #ifdef emacs
5852 Charxpos charpos = offset_to_charxpos (lispobj, PTR_TO_OFFSET (d)); 6144 Charxpos charpos = offset_to_charxpos (lispobj, PTR_TO_OFFSET (d));
5853 UPDATE_SYNTAX_CACHE (scache, charpos); 6145 UPDATE_SYNTAX_CACHE (scache, charpos);
5854 #endif 6146 #endif
5855 if (SYNTAX_FROM_CACHE (scache, emch) != Sword) 6147 tempres = (SYNTAX_FROM_CACHE (scache, emch) != Sword);
6148 END_REGEX_MALLOC_OK ();
6149 RE_MATCH_RELOCATE_MOVEABLE_DATA_POINTERS ();
6150 if (tempres)
5856 goto fail; 6151 goto fail;
5857 if (AT_STRINGS_BEG (d)) 6152 if (AT_STRINGS_BEG (d))
5858 break; 6153 break;
5859 dtmp = POS_BEFORE_GAP_UNSAFE (d); 6154 dtmp = POS_BEFORE_GAP_UNSAFE (d);
5860 DEC_IBYTEPTR_FMT (dtmp, fmt); 6155 DEC_IBYTEPTR_FMT (dtmp, fmt);
5861 emch = itext_ichar_fmt (dtmp, fmt, lispobj); 6156 emch = itext_ichar_fmt (dtmp, fmt, lispobj);
6157 BEGIN_REGEX_MALLOC_OK ();
5862 #ifdef emacs 6158 #ifdef emacs
5863 UPDATE_SYNTAX_CACHE_BACKWARD (scache, charpos - 1); 6159 UPDATE_SYNTAX_CACHE_BACKWARD (scache, charpos - 1);
5864 #endif 6160 #endif
5865 if (SYNTAX_FROM_CACHE (scache, emch) != Sword) 6161 tempres = (SYNTAX_FROM_CACHE (scache, emch) != Sword);
6162 END_REGEX_MALLOC_OK ();
6163 RE_MATCH_RELOCATE_MOVEABLE_DATA_POINTERS ();
6164 if (tempres)
5866 break; 6165 break;
5867 goto fail; 6166 goto fail;
5868 } 6167 }
5869 6168
5870 case wordend: 6169 case wordend:
5880 6179
5881 The or condition is incorrect (reversed). 6180 The or condition is incorrect (reversed).
5882 */ 6181 */
5883 re_char *dtmp; 6182 re_char *dtmp;
5884 Ichar emch; 6183 Ichar emch;
6184 int tempres;
6185 BEGIN_REGEX_MALLOC_OK ();
5885 #ifdef emacs 6186 #ifdef emacs
5886 Charxpos charpos = offset_to_charxpos (lispobj, PTR_TO_OFFSET (d)); 6187 Charxpos charpos = offset_to_charxpos (lispobj, PTR_TO_OFFSET (d));
5887 UPDATE_SYNTAX_CACHE (scache, charpos); 6188 UPDATE_SYNTAX_CACHE (scache, charpos);
5888 #endif 6189 #endif
6190 END_REGEX_MALLOC_OK ();
6191 RE_MATCH_RELOCATE_MOVEABLE_DATA_POINTERS ();
5889 dtmp = POS_BEFORE_GAP_UNSAFE (d); 6192 dtmp = POS_BEFORE_GAP_UNSAFE (d);
5890 DEC_IBYTEPTR_FMT (dtmp, fmt); 6193 DEC_IBYTEPTR_FMT (dtmp, fmt);
5891 emch = itext_ichar_fmt (dtmp, fmt, lispobj); 6194 emch = itext_ichar_fmt (dtmp, fmt, lispobj);
5892 if (SYNTAX_FROM_CACHE (scache, emch) != Sword) 6195 BEGIN_REGEX_MALLOC_OK ();
6196 tempres = (SYNTAX_FROM_CACHE (scache, emch) != Sword);
6197 END_REGEX_MALLOC_OK ();
6198 RE_MATCH_RELOCATE_MOVEABLE_DATA_POINTERS ();
6199 if (tempres)
5893 goto fail; 6200 goto fail;
5894 if (AT_STRINGS_END (d)) 6201 if (AT_STRINGS_END (d))
5895 break; 6202 break;
5896 dtmp = POS_AFTER_GAP_UNSAFE (d); 6203 dtmp = POS_AFTER_GAP_UNSAFE (d);
5897 emch = itext_ichar_fmt (dtmp, fmt, lispobj); 6204 emch = itext_ichar_fmt (dtmp, fmt, lispobj);
6205 BEGIN_REGEX_MALLOC_OK ();
5898 #ifdef emacs 6206 #ifdef emacs
5899 UPDATE_SYNTAX_CACHE_FORWARD (scache, charpos + 1); 6207 UPDATE_SYNTAX_CACHE_FORWARD (scache, charpos + 1);
5900 #endif 6208 #endif
5901 if (SYNTAX_FROM_CACHE (scache, emch) != Sword) 6209 tempres = (SYNTAX_FROM_CACHE (scache, emch) != Sword);
6210 END_REGEX_MALLOC_OK ();
6211 RE_MATCH_RELOCATE_MOVEABLE_DATA_POINTERS ();
6212 if (tempres)
5902 break; 6213 break;
5903 goto fail; 6214 goto fail;
5904 } 6215 }
5905 6216
5906 #ifdef emacs 6217 #ifdef emacs
5942 { 6253 {
5943 int matches; 6254 int matches;
5944 Ichar emch; 6255 Ichar emch;
5945 6256
5946 REGEX_PREFETCH (); 6257 REGEX_PREFETCH ();
6258 BEGIN_REGEX_MALLOC_OK ();
5947 UPDATE_SYNTAX_CACHE 6259 UPDATE_SYNTAX_CACHE
5948 (scache, offset_to_charxpos (lispobj, PTR_TO_OFFSET (d))); 6260 (scache, offset_to_charxpos (lispobj, PTR_TO_OFFSET (d)));
6261 END_REGEX_MALLOC_OK ();
6262 RE_MATCH_RELOCATE_MOVEABLE_DATA_POINTERS ();
5949 6263
5950 emch = itext_ichar_fmt (d, fmt, lispobj); 6264 emch = itext_ichar_fmt (d, fmt, lispobj);
6265 BEGIN_REGEX_MALLOC_OK ();
5951 matches = (SYNTAX_FROM_CACHE (scache, emch) == 6266 matches = (SYNTAX_FROM_CACHE (scache, emch) ==
5952 (enum syntaxcode) mcnt); 6267 (enum syntaxcode) mcnt);
6268 END_REGEX_MALLOC_OK ();
6269 RE_MATCH_RELOCATE_MOVEABLE_DATA_POINTERS ();
5953 INC_IBYTEPTR_FMT (d, fmt); 6270 INC_IBYTEPTR_FMT (d, fmt);
5954 if (matches != should_succeed) 6271 if (matches != should_succeed)
5955 goto fail; 6272 goto fail;
5956 SET_REGS_MATCHED (); 6273 SET_REGS_MATCHED ();
5957 } 6274 }
6072 goto restore_best_regs; 6389 goto restore_best_regs;
6073 6390
6074 FREE_VARIABLES (); 6391 FREE_VARIABLES ();
6075 6392
6076 return -1; /* Failure to match. */ 6393 return -1; /* Failure to match. */
6077 } /* re_match_2 */ 6394 } /* re_match_2_internal */
6078 6395
6079 /* Subroutine definitions for re_match_2. */ 6396 /* Subroutine definitions for re_match_2. */
6080 6397
6081 6398
6082 /* We are passed P pointing to a register number after a start_memory. 6399 /* We are passed P pointing to a register number after a start_memory.
6413 return 0; 6730 return 0;
6414 } 6731 }
6415 6732
6416 if (!re_comp_buf.buffer) 6733 if (!re_comp_buf.buffer)
6417 { 6734 {
6418 re_comp_buf.buffer = (unsigned char *) malloc (200); 6735 re_comp_buf.buffer = (unsigned char *) xmalloc (200);
6419 if (re_comp_buf.buffer == NULL) 6736 if (re_comp_buf.buffer == NULL)
6420 return gettext (re_error_msgid[(int) REG_ESPACE]); 6737 return gettext (re_error_msgid[(int) REG_ESPACE]);
6421 re_comp_buf.allocated = 200; 6738 re_comp_buf.allocated = 200;
6422 6739
6423 re_comp_buf.fastmap = (char *) malloc (1 << BYTEWIDTH); 6740 re_comp_buf.fastmap = (char *) xmalloc (1 << BYTEWIDTH);
6424 if (re_comp_buf.fastmap == NULL) 6741 if (re_comp_buf.fastmap == NULL)
6425 return gettext (re_error_msgid[(int) REG_ESPACE]); 6742 return gettext (re_error_msgid[(int) REG_ESPACE]);
6426 } 6743 }
6427 6744
6428 /* Since `re_exec' always passes NULL for the `regs' argument, we 6745 /* Since `re_exec' always passes NULL for the `regs' argument, we
6512 6829
6513 if (cflags & REG_ICASE) 6830 if (cflags & REG_ICASE)
6514 { 6831 {
6515 int i; 6832 int i;
6516 6833
6517 preg->translate = (char *) malloc (CHAR_SET_SIZE); 6834 preg->translate = (char *) xmalloc (CHAR_SET_SIZE);
6518 if (preg->translate == NULL) 6835 if (preg->translate == NULL)
6519 return (int) REG_ESPACE; 6836 return (int) REG_ESPACE;
6520 6837
6521 /* Map uppercase characters to corresponding lowercase ones. */ 6838 /* Map uppercase characters to corresponding lowercase ones. */
6522 for (i = 0; i < CHAR_SET_SIZE; i++) 6839 for (i = 0; i < CHAR_SET_SIZE; i++)
6611 pmatch[r].rm_eo = regs.end[r]; 6928 pmatch[r].rm_eo = regs.end[r];
6612 } 6929 }
6613 } 6930 }
6614 6931
6615 /* If we needed the temporary register info, free the space now. */ 6932 /* If we needed the temporary register info, free the space now. */
6616 free (regs.start); 6933 xfree (regs.start);
6617 free (regs.end); 6934 xfree (regs.end);
6618 } 6935 }
6619 6936
6620 /* We want zero return to mean success, unlike `re_search'. */ 6937 /* We want zero return to mean success, unlike `re_search'. */
6621 return ret >= 0 ? (int) REG_NOERROR : (int) REG_NOMATCH; 6938 return ret >= 0 ? (int) REG_NOERROR : (int) REG_NOMATCH;
6622 } 6939 }
6664 6981
6665 void 6982 void
6666 regfree (regex_t *preg) 6983 regfree (regex_t *preg)
6667 { 6984 {
6668 if (preg->buffer != NULL) 6985 if (preg->buffer != NULL)
6669 free (preg->buffer); 6986 xfree (preg->buffer);
6670 preg->buffer = NULL; 6987 preg->buffer = NULL;
6671 6988
6672 preg->allocated = 0; 6989 preg->allocated = 0;
6673 preg->used = 0; 6990 preg->used = 0;
6674 6991
6675 if (preg->fastmap != NULL) 6992 if (preg->fastmap != NULL)
6676 free (preg->fastmap); 6993 xfree (preg->fastmap);
6677 preg->fastmap = NULL; 6994 preg->fastmap = NULL;
6678 preg->fastmap_accurate = 0; 6995 preg->fastmap_accurate = 0;
6679 6996
6680 if (preg->translate != NULL) 6997 if (preg->translate != NULL)
6681 free (preg->translate); 6998 xfree (preg->translate);
6682 preg->translate = NULL; 6999 preg->translate = NULL;
6683 } 7000 }
6684 7001
6685 #endif /* not emacs */ 7002 #endif /* not emacs */
6686 7003