/contrib/cvs/lib/regex.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 6375 lines · 3925 code · 1031 blank · 1419 comment · 970 complexity · 31ee443b3692f366763a0c58c9d6d303 MD5 · raw file

Large files are truncated click here to view the full file

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