PageRenderTime 66ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/linux265/lkcd/archive/lkcdutils/crash/gdb-5.1/gdb/gnu-regex.c

#
C | 2068 lines | 1257 code | 346 blank | 465 comment | 219 complexity | 9f22631536bfcf7aeb9b9c90e1023800 MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.0, LGPL-2.1, LGPL-2.0

Large files files are truncated, but you can click here to view the full file

  1. /* gdb-5.1/gdb/gnu-regex.c - core analysis suite
  2. *
  3. * Portions Copyright (C) 2001, 2002 Mission Critical Linux, Inc.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * CVS: $Revision: 484 $ $Date: 2002-04-13 02:35:36 +0200 (Sat, 13 Apr 2002) $
  16. */
  17. /* *INDENT-OFF* */ /* keep in sync with glibc */
  18. /* Extended regular expression matching and search library,
  19. version 0.12.
  20. (Implements POSIX draft P1003.2/D11.2, except for some of the
  21. internationalization features.)
  22. Copyright 1993, 1994, 1995, 1996, 1998, 1999, 2000
  23. Free Software Foundation, Inc.
  24. NOTE: The canonical source of this file is maintained with the
  25. GNU C Library. Bugs can be reported to bug-glibc@gnu.org.
  26. This program is free software; you can redistribute it and/or modify it
  27. under the terms of the GNU General Public License as published by the
  28. Free Software Foundation; either version 2, or (at your option) any
  29. later version.
  30. This program is distributed in the hope that it will be useful,
  31. but WITHOUT ANY WARRANTY; without even the implied warranty of
  32. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  33. GNU General Public License for more details.
  34. You should have received a copy of the GNU General Public License
  35. along with this program; if not, write to the Free Software Foundation,
  36. Inc., 59 Temple Place - Suite 330,
  37. Boston, MA 02111-1307, USA. */
  38. /* AIX requires this to be the first thing in the file. */
  39. #if defined _AIX && !defined REGEX_MALLOC
  40. #pragma alloca
  41. #endif
  42. #undef _GNU_SOURCE
  43. #define _GNU_SOURCE
  44. #ifdef HAVE_CONFIG_H
  45. # include <config.h>
  46. #endif
  47. #ifndef PARAMS
  48. # if defined __GNUC__ || (defined __STDC__ && __STDC__)
  49. # define PARAMS(args) args
  50. # else
  51. # define PARAMS(args) ()
  52. # endif /* GCC. */
  53. #endif /* Not PARAMS. */
  54. #if defined STDC_HEADERS && !defined emacs
  55. # include <stddef.h>
  56. #else
  57. /* We need this for `gnu-regex.h', and perhaps for the Emacs include files. */
  58. # include <sys/types.h>
  59. #endif
  60. /* For platform which support the ISO C amendement 1 functionality we
  61. support user defined character classes. */
  62. #if defined _LIBC || (defined HAVE_WCTYPE_H && defined HAVE_WCHAR_H)
  63. /* Solaris 2.5 has a bug: <wchar.h> must be included before <wctype.h>. */
  64. # include <wchar.h>
  65. # include <wctype.h>
  66. #endif
  67. /* This is for other GNU distributions with internationalized messages. */
  68. /* CYGNUS LOCAL: ../intl will handle this for us */
  69. #ifdef ENABLE_NLS
  70. # include <libintl.h>
  71. #else
  72. # define gettext(msgid) (msgid)
  73. #endif
  74. #ifndef gettext_noop
  75. /* This define is so xgettext can find the internationalizable
  76. strings. */
  77. # define gettext_noop(String) String
  78. #endif
  79. /* The `emacs' switch turns on certain matching commands
  80. that make sense only in Emacs. */
  81. #ifdef emacs
  82. # include "lisp.h"
  83. # include "buffer.h"
  84. # include "syntax.h"
  85. #else /* not emacs */
  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. */
  89. # undef REL_ALLOC
  90. # if defined STDC_HEADERS || defined _LIBC
  91. # include <stdlib.h>
  92. # else
  93. char *malloc ();
  94. char *realloc ();
  95. # endif
  96. /* When used in Emacs's lib-src, we need to get bzero and bcopy somehow.
  97. If nothing else has been done, use the method below. */
  98. # ifdef INHIBIT_STRING_HEADER
  99. # if !(defined HAVE_BZERO && defined HAVE_BCOPY)
  100. # if !defined bzero && !defined bcopy
  101. # undef INHIBIT_STRING_HEADER
  102. # endif
  103. # endif
  104. # endif
  105. /* This is the normal way of making sure we have a bcopy and a bzero.
  106. This is used in most programs--a few other programs avoid this
  107. by defining INHIBIT_STRING_HEADER. */
  108. # ifndef INHIBIT_STRING_HEADER
  109. # if defined HAVE_STRING_H || defined STDC_HEADERS || defined _LIBC
  110. # include <string.h>
  111. # ifndef bzero
  112. # ifndef _LIBC
  113. # define bzero(s, n) (memset (s, '\0', n), (s))
  114. # else
  115. # define bzero(s, n) __bzero (s, n)
  116. # endif
  117. # endif
  118. # else
  119. # include <strings.h>
  120. # ifndef memcmp
  121. # define memcmp(s1, s2, n) bcmp (s1, s2, n)
  122. # endif
  123. # ifndef memcpy
  124. # define memcpy(d, s, n) (bcopy (s, d, n), (d))
  125. # endif
  126. # endif
  127. # endif
  128. /* Define the syntax stuff for \<, \>, etc. */
  129. /* This must be nonzero for the wordchar and notwordchar pattern
  130. commands in re_match_2. */
  131. # ifndef Sword
  132. # define Sword 1
  133. # endif
  134. # ifdef SWITCH_ENUM_BUG
  135. # define SWITCH_ENUM_CAST(x) ((int)(x))
  136. # else
  137. # define SWITCH_ENUM_CAST(x) (x)
  138. # endif
  139. /* How many characters in the character set. */
  140. # define CHAR_SET_SIZE 256
  141. /* GDB LOCAL: define _REGEX_RE_COMP to get BSD style re_comp and re_exec */
  142. #ifndef _REGEX_RE_COMP
  143. #define _REGEX_RE_COMP
  144. #endif
  145. # ifdef SYNTAX_TABLE
  146. extern char *re_syntax_table;
  147. # else /* not SYNTAX_TABLE */
  148. static char re_syntax_table[CHAR_SET_SIZE];
  149. static void
  150. init_syntax_once ()
  151. {
  152. register int c;
  153. static int done = 0;
  154. if (done)
  155. return;
  156. bzero (re_syntax_table, sizeof re_syntax_table);
  157. for (c = 'a'; c <= 'z'; c++)
  158. re_syntax_table[c] = Sword;
  159. for (c = 'A'; c <= 'Z'; c++)
  160. re_syntax_table[c] = Sword;
  161. for (c = '0'; c <= '9'; c++)
  162. re_syntax_table[c] = Sword;
  163. re_syntax_table['_'] = Sword;
  164. done = 1;
  165. }
  166. # endif /* not SYNTAX_TABLE */
  167. # define SYNTAX(c) re_syntax_table[c]
  168. #endif /* not emacs */
  169. /* Get the interface, including the syntax bits. */
  170. /* CYGNUS LOCAL: call it gnu-regex.h, not regex.h, to avoid name conflicts */
  171. #include "gnu-regex.h"
  172. /* isalpha etc. are used for the character classes. */
  173. #include <ctype.h>
  174. /* Jim Meyering writes:
  175. "... Some ctype macros are valid only for character codes that
  176. isascii says are ASCII (SGI's IRIX-4.0.5 is one such system --when
  177. using /bin/cc or gcc but without giving an ansi option). So, all
  178. ctype uses should be through macros like ISPRINT... If
  179. STDC_HEADERS is defined, then autoconf has verified that the ctype
  180. macros don't need to be guarded with references to isascii. ...
  181. Defining isascii to 1 should let any compiler worth its salt
  182. eliminate the && through constant folding."
  183. Solaris defines some of these symbols so we must undefine them first. */
  184. #undef ISASCII
  185. #if defined STDC_HEADERS || (!defined isascii && !defined HAVE_ISASCII)
  186. # define ISASCII(c) 1
  187. #else
  188. # define ISASCII(c) isascii(c)
  189. #endif
  190. #ifdef isblank
  191. # define ISBLANK(c) (ISASCII (c) && isblank (c))
  192. #else
  193. # define ISBLANK(c) ((c) == ' ' || (c) == '\t')
  194. #endif
  195. #ifdef isgraph
  196. # define ISGRAPH(c) (ISASCII (c) && isgraph (c))
  197. #else
  198. # define ISGRAPH(c) (ISASCII (c) && isprint (c) && !isspace (c))
  199. #endif
  200. #undef ISPRINT
  201. #define ISPRINT(c) (ISASCII (c) && isprint (c))
  202. #define ISDIGIT(c) (ISASCII (c) && isdigit (c))
  203. #define ISALNUM(c) (ISASCII (c) && isalnum (c))
  204. #define ISALPHA(c) (ISASCII (c) && isalpha (c))
  205. #define ISCNTRL(c) (ISASCII (c) && iscntrl (c))
  206. #define ISLOWER(c) (ISASCII (c) && islower (c))
  207. #define ISPUNCT(c) (ISASCII (c) && ispunct (c))
  208. #define ISSPACE(c) (ISASCII (c) && isspace (c))
  209. #define ISUPPER(c) (ISASCII (c) && isupper (c))
  210. #define ISXDIGIT(c) (ISASCII (c) && isxdigit (c))
  211. #ifndef NULL
  212. # define NULL (void *)0
  213. #endif
  214. /* We remove any previous definition of `SIGN_EXTEND_CHAR',
  215. since ours (we hope) works properly with all combinations of
  216. machines, compilers, `char' and `unsigned char' argument types.
  217. (Per Bothner suggested the basic approach.) */
  218. #undef SIGN_EXTEND_CHAR
  219. #if __STDC__
  220. # define SIGN_EXTEND_CHAR(c) ((signed char) (c))
  221. #else /* not __STDC__ */
  222. /* As in Harbison and Steele. */
  223. # define SIGN_EXTEND_CHAR(c) ((((unsigned char) (c)) ^ 128) - 128)
  224. #endif
  225. /* Should we use malloc or alloca? If REGEX_MALLOC is not defined, we
  226. use `alloca' instead of `malloc'. This is because using malloc in
  227. re_search* or re_match* could cause memory leaks when C-g is used in
  228. Emacs; also, malloc is slower and causes storage fragmentation. On
  229. the other hand, malloc is more portable, and easier to debug.
  230. Because we sometimes use alloca, some routines have to be macros,
  231. not functions -- `alloca'-allocated space disappears at the end of the
  232. function it is called in. */
  233. #ifdef REGEX_MALLOC
  234. # define REGEX_ALLOCATE malloc
  235. # define REGEX_REALLOCATE(source, osize, nsize) realloc (source, nsize)
  236. # define REGEX_FREE free
  237. #else /* not REGEX_MALLOC */
  238. /* Emacs already defines alloca, sometimes. */
  239. # ifndef alloca
  240. /* Make alloca work the best possible way. */
  241. # ifdef __GNUC__
  242. # define alloca __builtin_alloca
  243. # else /* not __GNUC__ */
  244. # if HAVE_ALLOCA_H
  245. # include <alloca.h>
  246. # endif /* HAVE_ALLOCA_H */
  247. # endif /* not __GNUC__ */
  248. # endif /* not alloca */
  249. # define REGEX_ALLOCATE alloca
  250. /* Assumes a `char *destination' variable. */
  251. # define REGEX_REALLOCATE(source, osize, nsize) \
  252. (destination = (char *) alloca (nsize), \
  253. memcpy (destination, source, osize))
  254. /* No need to do anything to free, after alloca. */
  255. # define REGEX_FREE(arg) ((void)0) /* Do nothing! But inhibit gcc warning. */
  256. #endif /* not REGEX_MALLOC */
  257. /* Define how to allocate the failure stack. */
  258. #if defined REL_ALLOC && defined REGEX_MALLOC
  259. # define REGEX_ALLOCATE_STACK(size) \
  260. r_alloc (&failure_stack_ptr, (size))
  261. # define REGEX_REALLOCATE_STACK(source, osize, nsize) \
  262. r_re_alloc (&failure_stack_ptr, (nsize))
  263. # define REGEX_FREE_STACK(ptr) \
  264. r_alloc_free (&failure_stack_ptr)
  265. #else /* not using relocating allocator */
  266. # ifdef REGEX_MALLOC
  267. # define REGEX_ALLOCATE_STACK malloc
  268. # define REGEX_REALLOCATE_STACK(source, osize, nsize) realloc (source, nsize)
  269. # define REGEX_FREE_STACK free
  270. # else /* not REGEX_MALLOC */
  271. # define REGEX_ALLOCATE_STACK alloca
  272. # define REGEX_REALLOCATE_STACK(source, osize, nsize) \
  273. REGEX_REALLOCATE (source, osize, nsize)
  274. /* No need to explicitly free anything. */
  275. # define REGEX_FREE_STACK(arg)
  276. # endif /* not REGEX_MALLOC */
  277. #endif /* not using relocating allocator */
  278. /* True if `size1' is non-NULL and PTR is pointing anywhere inside
  279. `string1' or just past its end. This works if PTR is NULL, which is
  280. a good thing. */
  281. #define FIRST_STRING_P(ptr) \
  282. (size1 && string1 <= (ptr) && (ptr) <= string1 + size1)
  283. /* (Re)Allocate N items of type T using malloc, or fail. */
  284. #define TALLOC(n, t) ((t *) malloc ((n) * sizeof (t)))
  285. #define RETALLOC(addr, n, t) ((addr) = (t *) realloc (addr, (n) * sizeof (t)))
  286. #define RETALLOC_IF(addr, n, t) \
  287. if (addr) RETALLOC((addr), (n), t); else (addr) = TALLOC ((n), t)
  288. #define REGEX_TALLOC(n, t) ((t *) REGEX_ALLOCATE ((n) * sizeof (t)))
  289. #define BYTEWIDTH 8 /* In bits. */
  290. #define STREQ(s1, s2) ((strcmp (s1, s2) == 0))
  291. #undef MAX
  292. #undef MIN
  293. #define MAX(a, b) ((a) > (b) ? (a) : (b))
  294. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  295. typedef char boolean;
  296. #define false 0
  297. #define true 1
  298. static int re_match_2_internal PARAMS ((struct re_pattern_buffer *bufp,
  299. const char *string1, int size1,
  300. const char *string2, int size2,
  301. int pos,
  302. struct re_registers *regs,
  303. int stop));
  304. /* These are the command codes that appear in compiled regular
  305. expressions. Some opcodes are followed by argument bytes. A
  306. command code can specify any interpretation whatsoever for its
  307. arguments. Zero bytes may appear in the compiled regular expression. */
  308. typedef enum
  309. {
  310. no_op = 0,
  311. /* Succeed right away--no more backtracking. */
  312. succeed,
  313. /* Followed by one byte giving n, then by n literal bytes. */
  314. exactn,
  315. /* Matches any (more or less) character. */
  316. anychar,
  317. /* Matches any one char belonging to specified set. First
  318. following byte is number of bitmap bytes. Then come bytes
  319. for a bitmap saying which chars are in. Bits in each byte
  320. are ordered low-bit-first. A character is in the set if its
  321. bit is 1. A character too large to have a bit in the map is
  322. automatically not in the set. */
  323. charset,
  324. /* Same parameters as charset, but match any character that is
  325. not one of those specified. */
  326. charset_not,
  327. /* Start remembering the text that is matched, for storing in a
  328. register. Followed by one byte with the register number, in
  329. the range 0 to one less than the pattern buffer's re_nsub
  330. field. Then followed by one byte with the number of groups
  331. inner to this one. (This last has to be part of the
  332. start_memory only because we need it in the on_failure_jump
  333. of re_match_2.) */
  334. start_memory,
  335. /* Stop remembering the text that is matched and store it in a
  336. memory register. Followed by one byte with the register
  337. number, in the range 0 to one less than `re_nsub' in the
  338. pattern buffer, and one byte with the number of inner groups,
  339. just like `start_memory'. (We need the number of inner
  340. groups here because we don't have any easy way of finding the
  341. corresponding start_memory when we're at a stop_memory.) */
  342. stop_memory,
  343. /* Match a duplicate of something remembered. Followed by one
  344. byte containing the register number. */
  345. duplicate,
  346. /* Fail unless at beginning of line. */
  347. begline,
  348. /* Fail unless at end of line. */
  349. endline,
  350. /* Succeeds if at beginning of buffer (if emacs) or at beginning
  351. of string to be matched (if not). */
  352. begbuf,
  353. /* Analogously, for end of buffer/string. */
  354. endbuf,
  355. /* Followed by two byte relative address to which to jump. */
  356. jump,
  357. /* Same as jump, but marks the end of an alternative. */
  358. jump_past_alt,
  359. /* Followed by two-byte relative address of place to resume at
  360. in case of failure. */
  361. on_failure_jump,
  362. /* Like on_failure_jump, but pushes a placeholder instead of the
  363. current string position when executed. */
  364. on_failure_keep_string_jump,
  365. /* Throw away latest failure point and then jump to following
  366. two-byte relative address. */
  367. pop_failure_jump,
  368. /* Change to pop_failure_jump if know won't have to backtrack to
  369. match; otherwise change to jump. This is used to jump
  370. back to the beginning of a repeat. If what follows this jump
  371. clearly won't match what the repeat does, such that we can be
  372. sure that there is no use backtracking out of repetitions
  373. already matched, then we change it to a pop_failure_jump.
  374. Followed by two-byte address. */
  375. maybe_pop_jump,
  376. /* Jump to following two-byte address, and push a dummy failure
  377. point. This failure point will be thrown away if an attempt
  378. is made to use it for a failure. A `+' construct makes this
  379. before the first repeat. Also used as an intermediary kind
  380. of jump when compiling an alternative. */
  381. dummy_failure_jump,
  382. /* Push a dummy failure point and continue. Used at the end of
  383. alternatives. */
  384. push_dummy_failure,
  385. /* Followed by two-byte relative address and two-byte number n.
  386. After matching N times, jump to the address upon failure. */
  387. succeed_n,
  388. /* Followed by two-byte relative address, and two-byte number n.
  389. Jump to the address N times, then fail. */
  390. jump_n,
  391. /* Set the following two-byte relative address to the
  392. subsequent two-byte number. The address *includes* the two
  393. bytes of number. */
  394. set_number_at,
  395. wordchar, /* Matches any word-constituent character. */
  396. notwordchar, /* Matches any char that is not a word-constituent. */
  397. wordbeg, /* Succeeds if at word beginning. */
  398. wordend, /* Succeeds if at word end. */
  399. wordbound, /* Succeeds if at a word boundary. */
  400. notwordbound /* Succeeds if not at a word boundary. */
  401. #ifdef emacs
  402. ,before_dot, /* Succeeds if before point. */
  403. at_dot, /* Succeeds if at point. */
  404. after_dot, /* Succeeds if after point. */
  405. /* Matches any character whose syntax is specified. Followed by
  406. a byte which contains a syntax code, e.g., Sword. */
  407. syntaxspec,
  408. /* Matches any character whose syntax is not that specified. */
  409. notsyntaxspec
  410. #endif /* emacs */
  411. } re_opcode_t;
  412. /* Common operations on the compiled pattern. */
  413. /* Store NUMBER in two contiguous bytes starting at DESTINATION. */
  414. #define STORE_NUMBER(destination, number) \
  415. do { \
  416. (destination)[0] = (number) & 0377; \
  417. (destination)[1] = (number) >> 8; \
  418. } while (0)
  419. /* Same as STORE_NUMBER, except increment DESTINATION to
  420. the byte after where the number is stored. Therefore, DESTINATION
  421. must be an lvalue. */
  422. #define STORE_NUMBER_AND_INCR(destination, number) \
  423. do { \
  424. STORE_NUMBER (destination, number); \
  425. (destination) += 2; \
  426. } while (0)
  427. /* Put into DESTINATION a number stored in two contiguous bytes starting
  428. at SOURCE. */
  429. #define EXTRACT_NUMBER(destination, source) \
  430. do { \
  431. (destination) = *(source) & 0377; \
  432. (destination) += SIGN_EXTEND_CHAR (*((source) + 1)) << 8; \
  433. } while (0)
  434. #ifdef DEBUG
  435. static void extract_number _RE_ARGS ((int *dest, unsigned char *source));
  436. static void
  437. extract_number (dest, source)
  438. int *dest;
  439. unsigned char *source;
  440. {
  441. int temp = SIGN_EXTEND_CHAR (*(source + 1));
  442. *dest = *source & 0377;
  443. *dest += temp << 8;
  444. }
  445. # ifndef EXTRACT_MACROS /* To debug the macros. */
  446. # undef EXTRACT_NUMBER
  447. # define EXTRACT_NUMBER(dest, src) extract_number (&dest, src)
  448. # endif /* not EXTRACT_MACROS */
  449. #endif /* DEBUG */
  450. /* Same as EXTRACT_NUMBER, except increment SOURCE to after the number.
  451. SOURCE must be an lvalue. */
  452. #define EXTRACT_NUMBER_AND_INCR(destination, source) \
  453. do { \
  454. EXTRACT_NUMBER (destination, source); \
  455. (source) += 2; \
  456. } while (0)
  457. #ifdef DEBUG
  458. static void extract_number_and_incr _RE_ARGS ((int *destination,
  459. unsigned char **source));
  460. static void
  461. extract_number_and_incr (destination, source)
  462. int *destination;
  463. unsigned char **source;
  464. {
  465. extract_number (destination, *source);
  466. *source += 2;
  467. }
  468. # ifndef EXTRACT_MACROS
  469. # undef EXTRACT_NUMBER_AND_INCR
  470. # define EXTRACT_NUMBER_AND_INCR(dest, src) \
  471. extract_number_and_incr (&dest, &src)
  472. # endif /* not EXTRACT_MACROS */
  473. #endif /* DEBUG */
  474. /* If DEBUG is defined, Regex prints many voluminous messages about what
  475. it is doing (if the variable `debug' is nonzero). If linked with the
  476. main program in `iregex.c', you can enter patterns and strings
  477. interactively. And if linked with the main program in `main.c' and
  478. the other test files, you can run the already-written tests. */
  479. #ifdef DEBUG
  480. /* We use standard I/O for debugging. */
  481. # include <stdio.h>
  482. /* It is useful to test things that ``must'' be true when debugging. */
  483. # include <assert.h>
  484. static int debug = 0;
  485. # define DEBUG_STATEMENT(e) e
  486. # define DEBUG_PRINT1(x) if (debug) printf (x)
  487. # define DEBUG_PRINT2(x1, x2) if (debug) printf (x1, x2)
  488. # define DEBUG_PRINT3(x1, x2, x3) if (debug) printf (x1, x2, x3)
  489. # define DEBUG_PRINT4(x1, x2, x3, x4) if (debug) printf (x1, x2, x3, x4)
  490. # define DEBUG_PRINT_COMPILED_PATTERN(p, s, e) \
  491. if (debug) print_partial_compiled_pattern (s, e)
  492. # define DEBUG_PRINT_DOUBLE_STRING(w, s1, sz1, s2, sz2) \
  493. if (debug) print_double_string (w, s1, sz1, s2, sz2)
  494. /* Print the fastmap in human-readable form. */
  495. void
  496. print_fastmap (fastmap)
  497. char *fastmap;
  498. {
  499. unsigned was_a_range = 0;
  500. unsigned i = 0;
  501. while (i < (1 << BYTEWIDTH))
  502. {
  503. if (fastmap[i++])
  504. {
  505. was_a_range = 0;
  506. putchar (i - 1);
  507. while (i < (1 << BYTEWIDTH) && fastmap[i])
  508. {
  509. was_a_range = 1;
  510. i++;
  511. }
  512. if (was_a_range)
  513. {
  514. printf ("-");
  515. putchar (i - 1);
  516. }
  517. }
  518. }
  519. putchar ('\n');
  520. }
  521. /* Print a compiled pattern string in human-readable form, starting at
  522. the START pointer into it and ending just before the pointer END. */
  523. void
  524. print_partial_compiled_pattern (start, end)
  525. unsigned char *start;
  526. unsigned char *end;
  527. {
  528. int mcnt, mcnt2;
  529. unsigned char *p1;
  530. unsigned char *p = start;
  531. unsigned char *pend = end;
  532. if (start == NULL)
  533. {
  534. printf ("(null)\n");
  535. return;
  536. }
  537. /* Loop over pattern commands. */
  538. while (p < pend)
  539. {
  540. printf ("%d:\t", p - start);
  541. switch ((re_opcode_t) *p++)
  542. {
  543. case no_op:
  544. printf ("/no_op");
  545. break;
  546. case exactn:
  547. mcnt = *p++;
  548. printf ("/exactn/%d", mcnt);
  549. do
  550. {
  551. putchar ('/');
  552. putchar (*p++);
  553. }
  554. while (--mcnt);
  555. break;
  556. case start_memory:
  557. mcnt = *p++;
  558. printf ("/start_memory/%d/%d", mcnt, *p++);
  559. break;
  560. case stop_memory:
  561. mcnt = *p++;
  562. printf ("/stop_memory/%d/%d", mcnt, *p++);
  563. break;
  564. case duplicate:
  565. printf ("/duplicate/%d", *p++);
  566. break;
  567. case anychar:
  568. printf ("/anychar");
  569. break;
  570. case charset:
  571. case charset_not:
  572. {
  573. register int c, last = -100;
  574. register int in_range = 0;
  575. printf ("/charset [%s",
  576. (re_opcode_t) *(p - 1) == charset_not ? "^" : "");
  577. assert (p + *p < pend);
  578. for (c = 0; c < 256; c++)
  579. if (c / 8 < *p
  580. && (p[1 + (c/8)] & (1 << (c % 8))))
  581. {
  582. /* Are we starting a range? */
  583. if (last + 1 == c && ! in_range)
  584. {
  585. putchar ('-');
  586. in_range = 1;
  587. }
  588. /* Have we broken a range? */
  589. else if (last + 1 != c && in_range)
  590. {
  591. putchar (last);
  592. in_range = 0;
  593. }
  594. if (! in_range)
  595. putchar (c);
  596. last = c;
  597. }
  598. if (in_range)
  599. putchar (last);
  600. putchar (']');
  601. p += 1 + *p;
  602. }
  603. break;
  604. case begline:
  605. printf ("/begline");
  606. break;
  607. case endline:
  608. printf ("/endline");
  609. break;
  610. case on_failure_jump:
  611. extract_number_and_incr (&mcnt, &p);
  612. printf ("/on_failure_jump to %d", p + mcnt - start);
  613. break;
  614. case on_failure_keep_string_jump:
  615. extract_number_and_incr (&mcnt, &p);
  616. printf ("/on_failure_keep_string_jump to %d", p + mcnt - start);
  617. break;
  618. case dummy_failure_jump:
  619. extract_number_and_incr (&mcnt, &p);
  620. printf ("/dummy_failure_jump to %d", p + mcnt - start);
  621. break;
  622. case push_dummy_failure:
  623. printf ("/push_dummy_failure");
  624. break;
  625. case maybe_pop_jump:
  626. extract_number_and_incr (&mcnt, &p);
  627. printf ("/maybe_pop_jump to %d", p + mcnt - start);
  628. break;
  629. case pop_failure_jump:
  630. extract_number_and_incr (&mcnt, &p);
  631. printf ("/pop_failure_jump to %d", p + mcnt - start);
  632. break;
  633. case jump_past_alt:
  634. extract_number_and_incr (&mcnt, &p);
  635. printf ("/jump_past_alt to %d", p + mcnt - start);
  636. break;
  637. case jump:
  638. extract_number_and_incr (&mcnt, &p);
  639. printf ("/jump to %d", p + mcnt - start);
  640. break;
  641. case succeed_n:
  642. extract_number_and_incr (&mcnt, &p);
  643. p1 = p + mcnt;
  644. extract_number_and_incr (&mcnt2, &p);
  645. printf ("/succeed_n to %d, %d times", p1 - start, mcnt2);
  646. break;
  647. case jump_n:
  648. extract_number_and_incr (&mcnt, &p);
  649. p1 = p + mcnt;
  650. extract_number_and_incr (&mcnt2, &p);
  651. printf ("/jump_n to %d, %d times", p1 - start, mcnt2);
  652. break;
  653. case set_number_at:
  654. extract_number_and_incr (&mcnt, &p);
  655. p1 = p + mcnt;
  656. extract_number_and_incr (&mcnt2, &p);
  657. printf ("/set_number_at location %d to %d", p1 - start, mcnt2);
  658. break;
  659. case wordbound:
  660. printf ("/wordbound");
  661. break;
  662. case notwordbound:
  663. printf ("/notwordbound");
  664. break;
  665. case wordbeg:
  666. printf ("/wordbeg");
  667. break;
  668. case wordend:
  669. printf ("/wordend");
  670. # ifdef emacs
  671. case before_dot:
  672. printf ("/before_dot");
  673. break;
  674. case at_dot:
  675. printf ("/at_dot");
  676. break;
  677. case after_dot:
  678. printf ("/after_dot");
  679. break;
  680. case syntaxspec:
  681. printf ("/syntaxspec");
  682. mcnt = *p++;
  683. printf ("/%d", mcnt);
  684. break;
  685. case notsyntaxspec:
  686. printf ("/notsyntaxspec");
  687. mcnt = *p++;
  688. printf ("/%d", mcnt);
  689. break;
  690. # endif /* emacs */
  691. case wordchar:
  692. printf ("/wordchar");
  693. break;
  694. case notwordchar:
  695. printf ("/notwordchar");
  696. break;
  697. case begbuf:
  698. printf ("/begbuf");
  699. break;
  700. case endbuf:
  701. printf ("/endbuf");
  702. break;
  703. default:
  704. printf ("?%d", *(p-1));
  705. }
  706. putchar ('\n');
  707. }
  708. printf ("%d:\tend of pattern.\n", p - start);
  709. }
  710. void
  711. print_compiled_pattern (bufp)
  712. struct re_pattern_buffer *bufp;
  713. {
  714. unsigned char *buffer = bufp->buffer;
  715. print_partial_compiled_pattern (buffer, buffer + bufp->used);
  716. printf ("%ld bytes used/%ld bytes allocated.\n",
  717. bufp->used, bufp->allocated);
  718. if (bufp->fastmap_accurate && bufp->fastmap)
  719. {
  720. printf ("fastmap: ");
  721. print_fastmap (bufp->fastmap);
  722. }
  723. printf ("re_nsub: %d\t", bufp->re_nsub);
  724. printf ("regs_alloc: %d\t", bufp->regs_allocated);
  725. printf ("can_be_null: %d\t", bufp->can_be_null);
  726. printf ("newline_anchor: %d\n", bufp->newline_anchor);
  727. printf ("no_sub: %d\t", bufp->no_sub);
  728. printf ("not_bol: %d\t", bufp->not_bol);
  729. printf ("not_eol: %d\t", bufp->not_eol);
  730. printf ("syntax: %lx\n", bufp->syntax);
  731. /* Perhaps we should print the translate table? */
  732. }
  733. void
  734. print_double_string (where, string1, size1, string2, size2)
  735. const char *where;
  736. const char *string1;
  737. const char *string2;
  738. int size1;
  739. int size2;
  740. {
  741. int this_char;
  742. if (where == NULL)
  743. printf ("(null)");
  744. else
  745. {
  746. if (FIRST_STRING_P (where))
  747. {
  748. for (this_char = where - string1; this_char < size1; this_char++)
  749. putchar (string1[this_char]);
  750. where = string2;
  751. }
  752. for (this_char = where - string2; this_char < size2; this_char++)
  753. putchar (string2[this_char]);
  754. }
  755. }
  756. void
  757. printchar (c)
  758. int c;
  759. {
  760. putc (c, stderr);
  761. }
  762. #else /* not DEBUG */
  763. # undef assert
  764. # define assert(e)
  765. # define DEBUG_STATEMENT(e)
  766. # define DEBUG_PRINT1(x)
  767. # define DEBUG_PRINT2(x1, x2)
  768. # define DEBUG_PRINT3(x1, x2, x3)
  769. # define DEBUG_PRINT4(x1, x2, x3, x4)
  770. # define DEBUG_PRINT_COMPILED_PATTERN(p, s, e)
  771. # define DEBUG_PRINT_DOUBLE_STRING(w, s1, sz1, s2, sz2)
  772. #endif /* not DEBUG */
  773. /* Set by `re_set_syntax' to the current regexp syntax to recognize. Can
  774. also be assigned to arbitrarily: each pattern buffer stores its own
  775. syntax, so it can be changed between regex compilations. */
  776. /* This has no initializer because initialized variables in Emacs
  777. become read-only after dumping. */
  778. reg_syntax_t re_syntax_options;
  779. /* Specify the precise syntax of regexps for compilation. This provides
  780. for compatibility for various utilities which historically have
  781. different, incompatible syntaxes.
  782. The argument SYNTAX is a bit mask comprised of the various bits
  783. defined in gnu-regex.h. We return the old syntax. */
  784. reg_syntax_t
  785. re_set_syntax (syntax)
  786. reg_syntax_t syntax;
  787. {
  788. reg_syntax_t ret = re_syntax_options;
  789. re_syntax_options = syntax;
  790. #ifdef DEBUG
  791. if (syntax & RE_DEBUG)
  792. debug = 1;
  793. else if (debug) /* was on but now is not */
  794. debug = 0;
  795. #endif /* DEBUG */
  796. return ret;
  797. }
  798. #ifdef _LIBC
  799. weak_alias (__re_set_syntax, re_set_syntax)
  800. #endif
  801. /* This table gives an error message for each of the error codes listed
  802. in gnu-regex.h. Obviously the order here has to be same as there.
  803. POSIX doesn't require that we do anything for REG_NOERROR,
  804. but why not be nice? */
  805. static const char *re_error_msgid[] =
  806. {
  807. gettext_noop ("Success"), /* REG_NOERROR */
  808. gettext_noop ("No match"), /* REG_NOMATCH */
  809. gettext_noop ("Invalid regular expression"), /* REG_BADPAT */
  810. gettext_noop ("Invalid collation character"), /* REG_ECOLLATE */
  811. gettext_noop ("Invalid character class name"), /* REG_ECTYPE */
  812. gettext_noop ("Trailing backslash"), /* REG_EESCAPE */
  813. gettext_noop ("Invalid back reference"), /* REG_ESUBREG */
  814. gettext_noop ("Unmatched [ or [^"), /* REG_EBRACK */
  815. gettext_noop ("Unmatched ( or \\("), /* REG_EPAREN */
  816. gettext_noop ("Unmatched \\{"), /* REG_EBRACE */
  817. gettext_noop ("Invalid content of \\{\\}"), /* REG_BADBR */
  818. gettext_noop ("Invalid range end"), /* REG_ERANGE */
  819. gettext_noop ("Memory exhausted"), /* REG_ESPACE */
  820. gettext_noop ("Invalid preceding regular expression"), /* REG_BADRPT */
  821. gettext_noop ("Premature end of regular expression"), /* REG_EEND */
  822. gettext_noop ("Regular expression too big"), /* REG_ESIZE */
  823. gettext_noop ("Unmatched ) or \\)"), /* REG_ERPAREN */
  824. };
  825. /* Avoiding alloca during matching, to placate r_alloc. */
  826. /* Define MATCH_MAY_ALLOCATE unless we need to make sure that the
  827. searching and matching functions should not call alloca. On some
  828. systems, alloca is implemented in terms of malloc, and if we're
  829. using the relocating allocator routines, then malloc could cause a
  830. relocation, which might (if the strings being searched are in the
  831. ralloc heap) shift the data out from underneath the regexp
  832. routines.
  833. Here's another reason to avoid allocation: Emacs
  834. processes input from X in a signal handler; processing X input may
  835. call malloc; if input arrives while a matching routine is calling
  836. malloc, then we're scrod. But Emacs can't just block input while
  837. calling matching routines; then we don't notice interrupts when
  838. they come in. So, Emacs blocks input around all regexp calls
  839. except the matching calls, which it leaves unprotected, in the
  840. faith that they will not malloc. */
  841. /* Normally, this is fine. */
  842. #define MATCH_MAY_ALLOCATE
  843. /* When using GNU C, we are not REALLY using the C alloca, no matter
  844. what config.h may say. So don't take precautions for it. */
  845. #ifdef __GNUC__
  846. # undef C_ALLOCA
  847. #endif
  848. /* The match routines may not allocate if (1) they would do it with malloc
  849. and (2) it's not safe for them to use malloc.
  850. Note that if REL_ALLOC is defined, matching would not use malloc for the
  851. failure stack, but we would still use it for the register vectors;
  852. so REL_ALLOC should not affect this. */
  853. #if (defined C_ALLOCA || defined REGEX_MALLOC) && defined emacs
  854. # undef MATCH_MAY_ALLOCATE
  855. #endif
  856. /* Failure stack declarations and macros; both re_compile_fastmap and
  857. re_match_2 use a failure stack. These have to be macros because of
  858. REGEX_ALLOCATE_STACK. */
  859. /* Number of failure points for which to initially allocate space
  860. when matching. If this number is exceeded, we allocate more
  861. space, so it is not a hard limit. */
  862. #ifndef INIT_FAILURE_ALLOC
  863. # define INIT_FAILURE_ALLOC 5
  864. #endif
  865. /* Roughly the maximum number of failure points on the stack. Would be
  866. exactly that if always used MAX_FAILURE_ITEMS items each time we failed.
  867. This is a variable only so users of regex can assign to it; we never
  868. change it ourselves. */
  869. #ifdef INT_IS_16BIT
  870. # if defined MATCH_MAY_ALLOCATE
  871. /* 4400 was enough to cause a crash on Alpha OSF/1,
  872. whose default stack limit is 2mb. */
  873. long int re_max_failures = 4000;
  874. # else
  875. long int re_max_failures = 2000;
  876. # endif
  877. union fail_stack_elt
  878. {
  879. unsigned char *pointer;
  880. long int integer;
  881. };
  882. typedef union fail_stack_elt fail_stack_elt_t;
  883. typedef struct
  884. {
  885. fail_stack_elt_t *stack;
  886. unsigned long int size;
  887. unsigned long int avail; /* Offset of next open position. */
  888. } fail_stack_type;
  889. #else /* not INT_IS_16BIT */
  890. # if defined MATCH_MAY_ALLOCATE
  891. /* 4400 was enough to cause a crash on Alpha OSF/1,
  892. whose default stack limit is 2mb. */
  893. #ifdef MCLX
  894. static int re_max_failures = 20000;
  895. #else
  896. int re_max_failures = 20000;
  897. #endif
  898. # else
  899. int re_max_failures = 2000;
  900. # endif
  901. union fail_stack_elt
  902. {
  903. unsigned char *pointer;
  904. int integer;
  905. };
  906. typedef union fail_stack_elt fail_stack_elt_t;
  907. typedef struct
  908. {
  909. fail_stack_elt_t *stack;
  910. unsigned size;
  911. unsigned avail; /* Offset of next open position. */
  912. } fail_stack_type;
  913. #endif /* INT_IS_16BIT */
  914. #define FAIL_STACK_EMPTY() (fail_stack.avail == 0)
  915. #define FAIL_STACK_PTR_EMPTY() (fail_stack_ptr->avail == 0)
  916. #define FAIL_STACK_FULL() (fail_stack.avail == fail_stack.size)
  917. /* Define macros to initialize and free the failure stack.
  918. Do `return -2' if the alloc fails. */
  919. #ifdef MATCH_MAY_ALLOCATE
  920. # define INIT_FAIL_STACK() \
  921. do { \
  922. fail_stack.stack = (fail_stack_elt_t *) \
  923. REGEX_ALLOCATE_STACK (INIT_FAILURE_ALLOC * sizeof (fail_stack_elt_t)); \
  924. \
  925. if (fail_stack.stack == NULL) \
  926. return -2; \
  927. \
  928. fail_stack.size = INIT_FAILURE_ALLOC; \
  929. fail_stack.avail = 0; \
  930. } while (0)
  931. # define RESET_FAIL_STACK() REGEX_FREE_STACK (fail_stack.stack)
  932. #else
  933. # define INIT_FAIL_STACK() \
  934. do { \
  935. fail_stack.avail = 0; \
  936. } while (0)
  937. # define RESET_FAIL_STACK()
  938. #endif
  939. /* Double the size of FAIL_STACK, up to approximately `re_max_failures' items.
  940. Return 1 if succeeds, and 0 if either ran out of memory
  941. allocating space for it or it was already too large.
  942. REGEX_REALLOCATE_STACK requires `destination' be declared. */
  943. #define DOUBLE_FAIL_STACK(fail_stack) \
  944. ((fail_stack).size > (unsigned) (re_max_failures * MAX_FAILURE_ITEMS) \
  945. ? 0 \
  946. : ((fail_stack).stack = (fail_stack_elt_t *) \
  947. REGEX_REALLOCATE_STACK ((fail_stack).stack, \
  948. (fail_stack).size * sizeof (fail_stack_elt_t), \
  949. ((fail_stack).size << 1) * sizeof (fail_stack_elt_t)), \
  950. \
  951. (fail_stack).stack == NULL \
  952. ? 0 \
  953. : ((fail_stack).size <<= 1, \
  954. 1)))
  955. /* Push pointer POINTER on FAIL_STACK.
  956. Return 1 if was able to do so and 0 if ran out of memory allocating
  957. space to do so. */
  958. #define PUSH_PATTERN_OP(POINTER, FAIL_STACK) \
  959. ((FAIL_STACK_FULL () \
  960. && !DOUBLE_FAIL_STACK (FAIL_STACK)) \
  961. ? 0 \
  962. : ((FAIL_STACK).stack[(FAIL_STACK).avail++].pointer = POINTER, \
  963. 1))
  964. /* Push a pointer value onto the failure stack.
  965. Assumes the variable `fail_stack'. Probably should only
  966. be called from within `PUSH_FAILURE_POINT'. */
  967. #define PUSH_FAILURE_POINTER(item) \
  968. fail_stack.stack[fail_stack.avail++].pointer = (unsigned char *) (item)
  969. /* This pushes an integer-valued item onto the failure stack.
  970. Assumes the variable `fail_stack'. Probably should only
  971. be called from within `PUSH_FAILURE_POINT'. */
  972. #define PUSH_FAILURE_INT(item) \
  973. fail_stack.stack[fail_stack.avail++].integer = (item)
  974. /* Push a fail_stack_elt_t value onto the failure stack.
  975. Assumes the variable `fail_stack'. Probably should only
  976. be called from within `PUSH_FAILURE_POINT'. */
  977. #define PUSH_FAILURE_ELT(item) \
  978. fail_stack.stack[fail_stack.avail++] = (item)
  979. /* These three POP... operations complement the three PUSH... operations.
  980. All assume that `fail_stack' is nonempty. */
  981. #define POP_FAILURE_POINTER() fail_stack.stack[--fail_stack.avail].pointer
  982. #define POP_FAILURE_INT() fail_stack.stack[--fail_stack.avail].integer
  983. #define POP_FAILURE_ELT() fail_stack.stack[--fail_stack.avail]
  984. /* Used to omit pushing failure point id's when we're not debugging. */
  985. #ifdef DEBUG
  986. # define DEBUG_PUSH PUSH_FAILURE_INT
  987. # define DEBUG_POP(item_addr) *(item_addr) = POP_FAILURE_INT ()
  988. #else
  989. # define DEBUG_PUSH(item)
  990. # define DEBUG_POP(item_addr)
  991. #endif
  992. /* Push the information about the state we will need
  993. if we ever fail back to it.
  994. Requires variables fail_stack, regstart, regend, reg_info, and
  995. num_regs_pushed be declared. DOUBLE_FAIL_STACK requires `destination'
  996. be declared.
  997. Does `return FAILURE_CODE' if runs out of memory. */
  998. #define PUSH_FAILURE_POINT(pattern_place, string_place, failure_code) \
  999. do { \
  1000. char *destination; \
  1001. /* Must be int, so when we don't save any registers, the arithmetic \
  1002. of 0 + -1 isn't done as unsigned. */ \
  1003. /* Can't be int, since there is not a shred of a guarantee that int \
  1004. is wide enough to hold a value of something to which pointer can \
  1005. be assigned */ \
  1006. active_reg_t this_reg; \
  1007. \
  1008. DEBUG_STATEMENT (failure_id++); \
  1009. DEBUG_STATEMENT (nfailure_points_pushed++); \
  1010. DEBUG_PRINT2 ("\nPUSH_FAILURE_POINT #%u:\n", failure_id); \
  1011. DEBUG_PRINT2 (" Before push, next avail: %d\n", (fail_stack).avail);\
  1012. DEBUG_PRINT2 (" size: %d\n", (fail_stack).size);\
  1013. \
  1014. DEBUG_PRINT2 (" slots needed: %ld\n", NUM_FAILURE_ITEMS); \
  1015. DEBUG_PRINT2 (" available: %d\n", REMAINING_AVAIL_SLOTS); \
  1016. \
  1017. /* Ensure we have enough space allocated for what we will push. */ \
  1018. while (REMAINING_AVAIL_SLOTS < NUM_FAILURE_ITEMS) \
  1019. { \
  1020. if (!DOUBLE_FAIL_STACK (fail_stack)) \
  1021. return failure_code; \
  1022. \
  1023. DEBUG_PRINT2 ("\n Doubled stack; size now: %d\n", \
  1024. (fail_stack).size); \
  1025. DEBUG_PRINT2 (" slots available: %d\n", REMAINING_AVAIL_SLOTS);\
  1026. } \
  1027. \
  1028. /* Push the info, starting with the registers. */ \
  1029. DEBUG_PRINT1 ("\n"); \
  1030. \
  1031. if (1) \
  1032. for (this_reg = lowest_active_reg; this_reg <= highest_active_reg; \
  1033. this_reg++) \
  1034. { \
  1035. DEBUG_PRINT2 (" Pushing reg: %lu\n", this_reg); \
  1036. DEBUG_STATEMENT (num_regs_pushed++); \
  1037. \
  1038. DEBUG_PRINT2 (" start: %p\n", regstart[this_reg]); \
  1039. PUSH_FAILURE_POINTER (regstart[this_reg]); \
  1040. \
  1041. DEBUG_PRINT2 (" end: %p\n", regend[this_reg]); \
  1042. PUSH_FAILURE_POINTER (regend[this_reg]); \
  1043. \
  1044. DEBUG_PRINT2 (" info: %p\n ", \
  1045. reg_info[this_reg].word.pointer); \
  1046. DEBUG_PRINT2 (" match_null=%d", \
  1047. REG_MATCH_NULL_STRING_P (reg_info[this_reg])); \
  1048. DEBUG_PRINT2 (" active=%d", IS_ACTIVE (reg_info[this_reg])); \
  1049. DEBUG_PRINT2 (" matched_something=%d", \
  1050. MATCHED_SOMETHING (reg_info[this_reg])); \
  1051. DEBUG_PRINT2 (" ever_matched=%d", \
  1052. EVER_MATCHED_SOMETHING (reg_info[this_reg])); \
  1053. DEBUG_PRINT1 ("\n"); \
  1054. PUSH_FAILURE_ELT (reg_info[this_reg].word); \
  1055. } \
  1056. \
  1057. DEBUG_PRINT2 (" Pushing low active reg: %ld\n", lowest_active_reg);\
  1058. PUSH_FAILURE_INT (lowest_active_reg); \
  1059. \
  1060. DEBUG_PRINT2 (" Pushing high active reg: %ld\n", highest_active_reg);\
  1061. PUSH_FAILURE_INT (highest_active_reg); \
  1062. \
  1063. DEBUG_PRINT2 (" Pushing pattern %p:\n", pattern_place); \
  1064. DEBUG_PRINT_COMPILED_PATTERN (bufp, pattern_place, pend); \
  1065. PUSH_FAILURE_POINTER (pattern_place); \
  1066. \
  1067. DEBUG_PRINT2 (" Pushing string %p: `", string_place); \
  1068. DEBUG_PRINT_DOUBLE_STRING (string_place, string1, size1, string2, \
  1069. size2); \
  1070. DEBUG_PRINT1 ("'\n"); \
  1071. PUSH_FAILURE_POINTER (string_place); \
  1072. \
  1073. DEBUG_PRINT2 (" Pushing failure id: %u\n", failure_id); \
  1074. DEBUG_PUSH (failure_id); \
  1075. } while (0)
  1076. /* This is the number of items that are pushed and popped on the stack
  1077. for each register. */
  1078. #define NUM_REG_ITEMS 3
  1079. /* Individual items aside from the registers. */
  1080. #ifdef DEBUG
  1081. # define NUM_NONREG_ITEMS 5 /* Includes failure point id. */
  1082. #else
  1083. # define NUM_NONREG_ITEMS 4
  1084. #endif
  1085. /* We push at most this many items on the stack. */
  1086. /* We used to use (num_regs - 1), which is the number of registers
  1087. this regexp will save; but that was changed to 5
  1088. to avoid stack overflow for a regexp with lots of parens. */
  1089. #define MAX_FAILURE_ITEMS (5 * NUM_REG_ITEMS + NUM_NONREG_ITEMS)
  1090. /* We actually push this many items. */
  1091. #define NUM_FAILURE_ITEMS \
  1092. (((0 \
  1093. ? 0 : highest_active_reg - lowest_active_reg + 1) \
  1094. * NUM_REG_ITEMS) \
  1095. + NUM_NONREG_ITEMS)
  1096. /* How many items can still be added to the stack without overflowing it. */
  1097. #define REMAINING_AVAIL_SLOTS ((fail_stack).size - (fail_stack).avail)
  1098. /* Pops what PUSH_FAIL_STACK pushes.
  1099. We restore into the parameters, all of which should be lvalues:
  1100. STR -- the saved data position.
  1101. PAT -- the saved pattern position.
  1102. LOW_REG, HIGH_REG -- the highest and lowest active registers.
  1103. REGSTART, REGEND -- arrays of string positions.
  1104. REG_INFO -- array of information about each subexpression.
  1105. Also assumes the variables `fail_stack' and (if debugging), `bufp',
  1106. `pend', `string1', `size1', `string2', and `size2'. */
  1107. #define POP_FAILURE_POINT(str, pat, low_reg, high_reg, regstart, regend, reg_info)\
  1108. { \
  1109. DEBUG_STATEMENT (unsigned failure_id;) \
  1110. active_reg_t this_reg; \
  1111. const unsigned char *string_temp; \
  1112. \
  1113. assert (!FAIL_STACK_EMPTY ()); \
  1114. \
  1115. /* Remove failure points and point to how many regs pushed. */ \
  1116. DEBUG_PRINT1 ("POP_FAILURE_POINT:\n"); \
  1117. DEBUG_PRINT2 (" Before pop, next avail: %d\n", fail_stack.avail); \
  1118. DEBUG_PRINT2 (" size: %d\n", fail_stack.size); \
  1119. \
  1120. assert (fail_stack.avail >= NUM_NONREG_ITEMS); \
  1121. \
  1122. DEBUG_POP (&failure_id); \
  1123. DEBUG_PRINT2 (" Popping failure id: %u\n", failure_id); \
  1124. \
  1125. /* If the saved string location is NULL, it came from an \
  1126. on_failure_keep_string_jump opcode, and we want to throw away the \
  1127. saved NULL, thus retaining our current position in the string. */ \
  1128. string_temp = POP_FAILURE_POINTER (); \
  1129. if (string_temp != NULL) \
  1130. str = (const char *) string_temp; \
  1131. \
  1132. DEBUG_PRINT2 (" Popping string %p: `", str); \
  1133. DEBUG_PRINT_DOUBLE_STRING (str, string1, size1, string2, size2); \
  1134. DEBUG_PRINT1 ("'\n"); \
  1135. \
  1136. pat = (unsigned char *) POP_FAILURE_POINTER (); \
  1137. DEBUG_PRINT2 (" Popping pattern %p:\n", pat); \
  1138. DEBUG_PRINT_COMPILED_PATTERN (bufp, pat, pend); \
  1139. \
  1140. /* Restore register info. */ \
  1141. high_reg = (active_reg_t) POP_FAILURE_INT (); \
  1142. DEBUG_PRINT2 (" Popping high active reg: %ld\n", high_reg); \
  1143. \
  1144. low_reg = (active_reg_t) POP_FAILURE_INT (); \
  1145. DEBUG_PRINT2 (" Popping low active reg: %ld\n", low_reg); \
  1146. \
  1147. if (1) \
  1148. for (this_reg = high_reg; this_reg >= low_reg; this_reg--) \
  1149. { \
  1150. DEBUG_PRINT2 (" Popping reg: %ld\n", this_reg); \
  1151. \
  1152. reg_info[this_reg].word = POP_FAILURE_ELT (); \
  1153. DEBUG_PRINT2 (" info: %p\n", \
  1154. reg_info[this_reg].word.pointer); \
  1155. \
  1156. regend[this_reg] = (const char *) POP_FAILURE_POINTER (); \
  1157. DEBUG_PRINT2 (" end: %p\n", regend[this_reg]); \
  1158. \
  1159. regstart[this_reg] = (const char *) POP_FAILURE_POINTER (); \
  1160. DEBUG_PRINT2 (" start: %p\n", regstart[this_reg]); \
  1161. } \
  1162. else \
  1163. { \
  1164. for (this_reg = highest_active_reg; this_reg > high_reg; this_reg--) \
  1165. { \
  1166. reg_info[this_reg].word.integer = 0; \
  1167. regend[this_reg] = 0; \
  1168. regstart[this_reg] = 0; \
  1169. } \
  1170. highest_active_reg = high_reg; \
  1171. } \
  1172. \
  1173. set_regs_matched_done = 0; \
  1174. DEBUG_STATEMENT (nfailure_points_popped++); \
  1175. } /* POP_FAILURE_POINT */
  1176. /* Structure for per-register (a.k.a. per-group) information.
  1177. Other register information, such as the
  1178. starting and ending positions (which are addresses), and the list of
  1179. inner groups (which is a bits list) are maintained in separate
  1180. variables.
  1181. We are making a (strictly speaking) nonportable assumption here: that
  1182. the compiler will pack our bit fields into something that fits into
  1183. the type of `word', i.e., is something that fits into one item on the
  1184. failure stack. */
  1185. /* Declarations and macros for re_match_2. */
  1186. typedef union
  1187. {
  1188. fail_stack_elt_t word;
  1189. struct
  1190. {
  1191. /* This field is one if this group can match the empty string,
  1192. zero if not. If not yet determined, `MATCH_NULL_UNSET_VALUE'. */
  1193. #define MATCH_NULL_UNSET_VALUE 3
  1194. unsigned match_null_string_p : 2;
  1195. unsigned is_active : 1;
  1196. unsigned matched_something : 1;
  1197. unsigned ever_matched_something : 1;
  1198. } bits;
  1199. } register_info_type;
  1200. #define REG_MATCH_NULL_STRING_P(R) ((R).bits.match_null_string_p)
  1201. #define IS_ACTIVE(R) ((R).bits.is_active)
  1202. #define MATCHED_SOMETHING(R) ((R).bits.matched_something)
  1203. #define EVER_MATCHED_SOMETHING(R) ((R).bits.ever_matched_something)
  1204. /* Call this when have matched a real character; it sets `matched' flags
  1205. for the subexpressions which we are currently inside. Also records
  1206. that those subexprs have matched. */
  1207. #define SET_REGS_MATCHED() \
  1208. do \
  1209. { \
  1210. if (!set_regs_matched_done) \
  1211. { \
  1212. active_reg_t r; \
  1213. set_regs_matched_done = 1; \
  1214. for (r = lowest_active_reg; r <= highest_active_reg; r++) \
  1215. { \
  1216. MATCHED_SOMETHING (reg_info[r]) \
  1217. = EVER_MATCHED_SOMETHING (reg_info[r]) \
  1218. = 1; \
  1219. } \
  1220. } \
  1221. } \
  1222. while (0)
  1223. /* Registers are set to a sentinel when they haven't yet matched. */
  1224. static char reg_unset_dummy;
  1225. #define REG_UNSET_VALUE (&reg_unset_dummy)
  1226. #define REG_UNSET(e) ((e) == REG_UNSET_VALUE)
  1227. /* Subroutine declarations and macros for regex_compile. */
  1228. static reg_errcode_t regex_compile _RE_ARGS ((const char *pattern, size_t size,
  1229. reg_syntax_t syntax,
  1230. struct re_pattern_buffer *bufp));
  1231. static void store_op1 _RE_ARGS ((re_opcode_t op, unsigned char *loc, int arg));
  1232. static void store_op2 _RE_ARGS ((re_opcode_t op, unsigned char *loc,
  1233. int arg1, int arg2));
  1234. static void insert_op1 _RE_ARGS ((re_opcode_t op, unsigned char *loc,
  1235. int arg, unsigned char *end));
  1236. static void insert_op2 _RE_ARGS ((re_opcode_t op, unsigned char *loc,
  1237. int arg1, int arg2, unsigned char *end));
  1238. static boolean at_begline_loc_p _RE_ARGS ((const char *pattern, const char *p,
  1239. reg_syntax_t syntax));
  1240. static boolean at_endline_loc_p _RE_ARGS ((const char *p, const char *pend,
  1241. reg_syntax_t syntax));
  1242. static reg_errcode_t compile_range _RE_ARGS ((const char **p_ptr,
  1243. const char *pend,
  1244. char *translate,
  1245. reg_syntax_t syntax,
  1246. unsigned char *b));
  1247. /* Fetch the next character in the uncompiled pattern---translating it
  1248. if necessary. Also cast from a signed character in the constant
  1249. string passed to us by the user to an unsigned char that we can use
  1250. as an array index (in, e.g., `translate'). */
  1251. #ifndef PATFETCH
  1252. # define PATFETCH(c) \
  1253. do {if (p == pend) return REG_EEND; \
  1254. c = (unsigned char) *p++; \
  1255. if (translate) c = (unsigned char) translate[c]; \
  1256. } while (0)
  1257. #endif
  1258. /* Fetch the next character in the uncompiled pattern, with no
  1259. translation. */
  1260. #define PATFETCH_RAW(c) \
  1261. do {if (p == pend) return REG_EEND; \
  1262. c = (unsigned char) *p++; \
  1263. } while (0)
  1264. /* Go backwards one character in the pattern. */
  1265. #define PATUNFETCH p--
  1266. /* If `translate' is non-null, return translate[D], else just D. We
  1267. cast the subscript to translate because some data is declared as
  1268. `char *', to avoid warnings when a string constant is passed. But
  1269. when we use a character as a subscript we must make it unsigned. */
  1270. #ifndef TRANSLATE
  1271. # define TRANSLATE(d) \
  1272. (translate ? (char) translate[(unsigned char) (d)] : (d))
  1273. #endif
  1274. /* Macros for outputting the compiled pattern into `buffer'. */
  1275. /* If the buffer isn't allocated when it comes in, use this. */
  1276. #define INIT_BUF_SIZE 32
  1277. /* Make sure we have at least N more bytes of space in buffer. */
  1278. #define GET_BUFFER_SPACE(n) \
  1279. while ((unsigned long) (b - bufp->buffer + (n)) > bufp->allocated) \
  1280. EXTEND_BUFFER ()
  1281. /* Make sure we have one more byte of buffer space and then add C to it. */
  1282. #define BUF_PUSH(c) \
  1283. do { \
  1284. GET_BUFFER_SPACE (1); \
  1285. *b++ = (unsigned char) (c); \
  1286. } while (0)
  1287. /* Ensure we have two more bytes of buffer space and then append C1 and C2. */
  1288. #define BUF_PUSH_2(c1, c2) \
  1289. do { \
  1290. GET_BUFFER_SPACE (2); \
  1291. *b++ = (unsigned char) (c1); \
  1292. *b++ = (unsigned char) (c2); \
  1293. } while (0)
  1294. /* As with BUF_PUSH_2, except for three bytes. */
  1295. #define BUF_PUSH_3(c1, c2, c3) \
  1296. do { \
  1297. GET_BUFFER_SPACE (3); \
  1298. *b++ = (unsigned char) (c1); \
  1299. *b++ = (unsigned char) (c2); \
  1300. *b++ = (unsigned char) (c3); \
  1301. } while (0)
  1302. /* Store a jump with opcode OP at LOC to location TO. We store a
  1303. relative address offset by the three bytes the jump itself occupies. */
  1304. #define STORE_JUMP(op, loc, to) \
  1305. store_op1 (op, loc, (int) ((to) - (loc) - 3))
  1306. /* Likewise, for a two-argument jump. */
  1307. #define STORE_JUMP2(op, loc, to, arg) \
  1308. store_op2 (op, loc, (int) ((to) - (loc) - 3), arg)
  1309. /* Like `STORE_JUMP', but for inserting. Assume `b' is the buffer end. */
  1310. #define INSERT_JUMP(op, loc, to) \
  1311. insert_op1 (op, loc, (int) ((to) - (loc) - 3), b)
  1312. /* Like `STORE_JUMP2', but for inserting. Assume `b' is the buffer end. */
  1313. #define INSERT_JUMP2(op, loc, to, arg) \
  1314. insert_op2 (op, loc, (int) ((to) - (loc) - 3), arg, b)
  1315. /* This is not an arbitrary limit: the arguments which represent offsets
  1316. into the pattern are two bytes long. So if 2^16 bytes turns out to
  1317. be too small, many things would have to change. */
  1318. #define MAX_BUF_SIZE (1L << 16)
  1319. #define REALLOC(p,s) realloc ((p), (s))
  1320. /* Extend the buffer by twice its current size via…

Large files files are truncated, but you can click here to view the full file