PageRenderTime 31ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/src/regex.c

#
C | 2068 lines | 1278 code | 347 blank | 443 comment | 213 complexity | 9ff6f3d0878a73e8769db319e32ec58b MD5 | raw file

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

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

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