PageRenderTime 36ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/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
Possible License(s): MPL-2.0-no-copyleft-exception, BSD-3-Clause, LGPL-2.0, LGPL-2.1, BSD-2-Clause, 0BSD, JSON, AGPL-1.0, GPL-2.0
  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. /* Extend the buffer by twice its current size via realloc and
  1330. reset the pointers that pointed into the old block to point to the
  1331. correct places in the new one. If extending the buffer results in it
  1332. being larger than MAX_BUF_SIZE, then flag memory exhausted. */
  1333. #define EXTEND_BUFFER() \
  1334. do { \
  1335. unsigned char *old_buffer = bufp->buffer; \
  1336. if (bufp->allocated == MAX_BUF_SIZE) \
  1337. return REG_ESIZE; \
  1338. bufp->allocated <<= 1; \
  1339. if (bufp->allocated > MAX_BUF_SIZE) \
  1340. bufp->allocated = MAX_BUF_SIZE; \
  1341. bufp->buffer = (unsigned char *) realloc (bufp->buffer, bufp->allocated);\
  1342. if (bufp->buffer == NULL) \
  1343. return REG_ESPACE; \
  1344. /* If the buffer moved, move all the pointers into it. */ \
  1345. if (old_buffer != bufp->buffer) \
  1346. { \
  1347. b = (b - old_buffer) + bufp->buffer; \
  1348. begalt = (begalt - old_buffer) + bufp->buffer; \
  1349. if (fixup_alt_jump) \
  1350. fixup_alt_jump = (fixup_alt_jump - old_buffer) + bufp->buffer;\
  1351. if (laststart) \
  1352. laststart = (laststart - old_buffer) + bufp->buffer; \
  1353. if (pending_exact) \
  1354. pending_exact = (pending_exact - old_buffer) + bufp->buffer; \
  1355. } \
  1356. } while (0)
  1357. /* Since we have one byte reserved for the register number argument to
  1358. {start,stop}_memory, the maximum number of groups we can report
  1359. things about is what fits in that byte. */
  1360. #define MAX_REGNUM 255
  1361. /* But patterns can have more than `MAX_REGNUM' registers. We just
  1362. ignore the excess. */
  1363. typedef unsigned regnum_t;
  1364. /* Macros for the compile stack. */
  1365. /* Since offsets can go either forwards or backwards, this type needs to
  1366. be able to hold values from -(MAX_BUF_SIZE - 1) to MAX_BUF_SIZE - 1. */
  1367. typedef int pattern_offset_t;
  1368. typedef struct
  1369. {
  1370. pattern_offset_t begalt_offset;
  1371. pattern_offset_t fixup_alt_jump;
  1372. pattern_offset_t inner_group_offset;
  1373. pattern_offset_t laststart_offset;
  1374. regnum_t regnum;
  1375. } compile_stack_elt_t;
  1376. typedef struct
  1377. {
  1378. compile_stack_elt_t *stack;
  1379. unsigned size;
  1380. unsigned avail; /* Offset of next open position. */
  1381. } compile_stack_type;
  1382. #define INIT_COMPILE_STACK_SIZE 32
  1383. #define COMPILE_STACK_EMPTY (compile_stack.avail == 0)
  1384. #define COMPILE_STACK_FULL (compile_stack.avail == compile_stack.size)
  1385. /* The next available element. */
  1386. #define COMPILE_STACK_TOP (compile_stack.stack[compile_stack.avail])
  1387. /* Structure to manage work area for range table. */
  1388. struct range_table_work_area
  1389. {
  1390. int *table; /* actual work area. */
  1391. int allocated; /* allocated size for work area in bytes. */
  1392. int used; /* actually used size in words. */
  1393. };
  1394. /* Make sure that WORK_AREA can hold more N multibyte characters. */
  1395. #define EXTEND_RANGE_TABLE_WORK_AREA(work_area, n) \
  1396. do { \
  1397. if (((work_area).used + (n)) * sizeof (int) > (work_area).allocated) \
  1398. { \
  1399. (work_area).allocated += 16 * sizeof (int); \
  1400. if ((work_area).table) \
  1401. (work_area).table \
  1402. = (int *) realloc ((work_area).table, (work_area).allocated); \
  1403. else \
  1404. (work_area).table \
  1405. = (int *) malloc ((work_area).allocated); \
  1406. if ((work_area).table == 0) \
  1407. FREE_STACK_RETURN (REG_ESPACE); \
  1408. } \
  1409. } while (0)
  1410. /* Set a range (RANGE_START, RANGE_END) to WORK_AREA. */
  1411. #define SET_RANGE_TABLE_WORK_AREA(work_area, range_start, range_end) \
  1412. do { \
  1413. EXTEND_RANGE_TABLE_WORK_AREA ((work_area), 2); \
  1414. (work_area).table[(work_area).used++] = (range_start); \
  1415. (work_area).table[(work_area).used++] = (range_end); \
  1416. } while (0)
  1417. /* Free allocated memory for WORK_AREA. */
  1418. #define FREE_RANGE_TABLE_WORK_AREA(work_area) \
  1419. do { \
  1420. if ((work_area).table) \
  1421. free ((work_area).table); \
  1422. } while (0)
  1423. #define CLEAR_RANGE_TABLE_WORK_USED(work_area) ((work_area).used = 0)
  1424. #define RANGE_TABLE_WORK_USED(work_area) ((work_area).used)
  1425. #define RANGE_TABLE_WORK_ELT(work_area, i) ((work_area).table[i])
  1426. /* Set the bit for character C in a list. */
  1427. #define SET_LIST_BIT(c) \
  1428. (b[((unsigned char) (c)) / BYTEWIDTH] \
  1429. |= 1 << (((unsigned char) c) % BYTEWIDTH))
  1430. /* Get the next unsigned number in the uncompiled pattern. */
  1431. #define GET_UNSIGNED_NUMBER(num) \
  1432. { if (p != pend) \
  1433. { \
  1434. PATFETCH (c); \
  1435. while (ISDIGIT (c)) \
  1436. { \
  1437. if (num < 0) \
  1438. num = 0; \
  1439. num = num * 10 + c - '0'; \
  1440. if (p == pend) \
  1441. break; \
  1442. PATFETCH (c); \
  1443. } \
  1444. } \
  1445. }
  1446. #define CHAR_CLASS_MAX_LENGTH 6 /* Namely, `xdigit'. */
  1447. #define IS_CHAR_CLASS(string) \
  1448. (STREQ (string, "alpha") || STREQ (string, "upper") \
  1449. || STREQ (string, "lower") || STREQ (string, "digit") \
  1450. || STREQ (string, "alnum") || STREQ (string, "xdigit") \
  1451. || STREQ (string, "space") || STREQ (string, "print") \
  1452. || STREQ (string, "punct") || STREQ (string, "graph") \
  1453. || STREQ (string, "cntrl") || STREQ (string, "blank"))
  1454. #ifndef MATCH_MAY_ALLOCATE
  1455. /* If we cannot allocate large objects within re_match_2_internal,
  1456. we make the fail stack and register vectors global.
  1457. The fail stack, we grow to the maximum size when a regexp
  1458. is compiled.
  1459. The register vectors, we adjust in size each time we
  1460. compile a regexp, according to the number of registers it needs. */
  1461. static fail_stack_type fail_stack;
  1462. /* Size with which the following vectors are currently allocated.
  1463. That is so we can make them bigger as needed,
  1464. but never make them smaller. */
  1465. static int regs_allocated_size;
  1466. static const char ** regstart, ** regend;
  1467. static const char ** old_regstart, ** old_regend;
  1468. static const char **best_regstart, **best_regend;
  1469. static register_info_type *reg_info;
  1470. static const char **reg_dummy;
  1471. static register_info_type *reg_info_dummy;
  1472. /* Make the register vectors big enough for NUM_REGS registers,
  1473. but don't make them smaller. */
  1474. static
  1475. regex_grow_registers (num_regs)
  1476. int num_regs;
  1477. {
  1478. if (num_regs > regs_allocated_size)
  1479. {
  1480. RETALLOC_IF (regstart, num_regs, const char *);
  1481. RETALLOC_IF (regend, num_regs, const char *);
  1482. RETALLOC_IF (old_regstart, num_regs, const char *);
  1483. RETALLOC_IF (old_regend, num_regs, const char *);
  1484. RETALLOC_IF (best_regstart, num_regs, const char *);
  1485. RETALLOC_IF (best_regend, num_regs, const char *);
  1486. RETALLOC_IF (reg_info, num_regs, register_info_type);
  1487. RETALLOC_IF (reg_dummy, num_regs, const char *);
  1488. RETALLOC_IF (reg_info_dummy, num_regs, register_info_type);
  1489. regs_allocated_size = num_regs;
  1490. }
  1491. }
  1492. #endif /* not MATCH_MAY_ALLOCATE */
  1493. /* `regex_compile' compiles PATTERN (of length SIZE) according to SYNTAX.
  1494. Returns one of error codes defined in `regex.h', or zero for success.
  1495. Assumes the `allocated' (and perhaps `buffer') and `translate'
  1496. fields are set in BUFP on entry.
  1497. If it succeeds, results are put in BUFP (if it returns an error, the
  1498. contents of BUFP are undefined):
  1499. `buffer' is the compiled pattern;
  1500. `syntax' is set to SYNTAX;
  1501. `used' is set to the length of the compiled pattern;
  1502. `fastmap_accurate' is zero;
  1503. `re_nsub' is the number of subexpressions in PATTERN;
  1504. `not_bol' and `not_eol' are zero;
  1505. The `fastmap' and `newline_anchor' fields are neither
  1506. examined nor set. */
  1507. /* Return, freeing storage we allocated. */
  1508. #define FREE_STACK_RETURN(value) \
  1509. do { \
  1510. FREE_RANGE_TABLE_WORK_AREA (range_table_work); \
  1511. free (compile_stack.stack); \
  1512. return value; \
  1513. } while (0)
  1514. static reg_errcode_t
  1515. regex_compile (pattern, size, syntax, bufp)
  1516. const char *pattern;
  1517. int size;
  1518. reg_syntax_t syntax;
  1519. struct re_pattern_buffer *bufp;
  1520. {
  1521. /* We fetch characters from PATTERN here. Even though PATTERN is
  1522. `char *' (i.e., signed), we declare these variables as unsigned, so
  1523. they can be reliably used as array indices. */
  1524. register unsigned int c, c1;
  1525. /* A random temporary spot in PATTERN. */
  1526. const char *p1;
  1527. /* Points to the end of the buffer, where we should append. */
  1528. register unsigned char *b;
  1529. /* Keeps track of unclosed groups. */
  1530. compile_stack_type compile_stack;
  1531. /* Points to the current (ending) position in the pattern. */
  1532. #ifdef AIX
  1533. /* `const' makes AIX compiler fail. */
  1534. char *p = pattern;
  1535. #else
  1536. const char *p = pattern;
  1537. #endif
  1538. const char *pend = pattern + size;
  1539. /* How to translate the characters in the pattern. */
  1540. RE_TRANSLATE_TYPE translate = bufp->translate;
  1541. /* Address of the count-byte of the most recently inserted `exactn'
  1542. command. This makes it possible to tell if a new exact-match
  1543. character can be added to that command or if the character requires
  1544. a new `exactn' command. */
  1545. unsigned char *pending_exact = 0;
  1546. /* Address of start of the most recently finished expression.
  1547. This tells, e.g., postfix * where to find the start of its
  1548. operand. Reset at the beginning of groups and alternatives. */
  1549. unsigned char *laststart = 0;
  1550. /* Address of beginning of regexp, or inside of last group. */
  1551. unsigned char *begalt;
  1552. /* Place in the uncompiled pattern (i.e., the {) to
  1553. which to go back if the interval is invalid. */
  1554. const char *beg_interval;
  1555. /* Address of the place where a forward jump should go to the end of
  1556. the containing expression. Each alternative of an `or' -- except the
  1557. last -- ends with a forward jump of this sort. */
  1558. unsigned char *fixup_alt_jump = 0;
  1559. /* Counts open-groups as they are encountered. Remembered for the
  1560. matching close-group on the compile stack, so the same register
  1561. number is put in the stop_memory as the start_memory. */
  1562. regnum_t regnum = 0;
  1563. /* Work area for range table of charset. */
  1564. struct range_table_work_area range_table_work;
  1565. #ifdef DEBUG
  1566. DEBUG_PRINT1 ("\nCompiling pattern: ");
  1567. if (debug)
  1568. {
  1569. unsigned debug_count;
  1570. for (debug_count = 0; debug_count < size; debug_count++)
  1571. putchar (pattern[debug_count]);
  1572. putchar ('\n');
  1573. }
  1574. #endif /* DEBUG */
  1575. /* Initialize the compile stack. */
  1576. compile_stack.stack = TALLOC (INIT_COMPILE_STACK_SIZE, compile_stack_elt_t);
  1577. if (compile_stack.stack == NULL)
  1578. return REG_ESPACE;
  1579. compile_stack.size = INIT_COMPILE_STACK_SIZE;
  1580. compile_stack.avail = 0;
  1581. range_table_work.table = 0;
  1582. range_table_work.allocated = 0;
  1583. /* Initialize the pattern buffer. */
  1584. bufp->syntax = syntax;
  1585. bufp->fastmap_accurate = 0;
  1586. bufp->not_bol = bufp->not_eol = 0;
  1587. /* Set `used' to zero, so that if we return an error, the pattern
  1588. printer (for debugging) will think there's no pattern. We reset it
  1589. at the end. */
  1590. bufp->used = 0;
  1591. /* Always count groups, whether or not bufp->no_sub is set. */
  1592. bufp->re_nsub = 0;
  1593. #ifdef emacs
  1594. /* bufp->multibyte is set before regex_compile is called, so don't alter
  1595. it. */
  1596. #else /* not emacs */
  1597. /* Nothing is recognized as a multibyte character. */
  1598. bufp->multibyte = 0;
  1599. #endif
  1600. #if !defined (emacs) && !defined (SYNTAX_TABLE)
  1601. /* Initialize the syntax table. */
  1602. init_syntax_once ();
  1603. #endif
  1604. if (bufp->allocated == 0)
  1605. {
  1606. if (bufp->buffer)
  1607. { /* If zero allocated, but buffer is non-null, try to realloc
  1608. enough space. This loses if buffer's address is bogus, but
  1609. that is the user's responsibility. */
  1610. RETALLOC (bufp->buffer, INIT_BUF_SIZE, unsigned char);
  1611. }
  1612. else
  1613. { /* Caller did not allocate a buffer. Do it for them. */
  1614. bufp->buffer = TALLOC (INIT_BUF_SIZE, unsigned char);
  1615. }
  1616. if (!bufp->buffer) FREE_STACK_RETURN (REG_ESPACE);
  1617. bufp->allocated = INIT_BUF_SIZE;
  1618. }
  1619. begalt = b = bufp->buffer;
  1620. /* Loop through the uncompiled pattern until we're at the end. */
  1621. while (p != pend)
  1622. {
  1623. PATFETCH (c);
  1624. switch (c)
  1625. {
  1626. case '^':
  1627. {
  1628. if ( /* If at start of pattern, it's an operator. */
  1629. p == pattern + 1
  1630. /* If context independent, it's an operator. */
  1631. || syntax & RE_CONTEXT_INDEP_ANCHORS
  1632. /* Otherwise, depends on what's come before. */
  1633. || at_begline_loc_p (pattern, p, syntax))
  1634. BUF_PUSH (begline);
  1635. else
  1636. goto normal_char;
  1637. }
  1638. break;
  1639. case '$':
  1640. {
  1641. if ( /* If at end of pattern, it's an operator. */
  1642. p == pend
  1643. /* If context independent, it's an operator. */
  1644. || syntax & RE_CONTEXT_INDEP_ANCHORS
  1645. /* Otherwise, depends on what's next. */
  1646. || at_endline_loc_p (p, pend, syntax))
  1647. BUF_PUSH (endline);
  1648. else
  1649. goto normal_char;
  1650. }
  1651. break;
  1652. case '+':
  1653. case '?':
  1654. if ((syntax & RE_BK_PLUS_QM)
  1655. || (syntax & RE_LIMITED_OPS))
  1656. goto normal_char;
  1657. handle_plus:
  1658. case '*':
  1659. /* If there is no previous pattern... */
  1660. if (!laststart)
  1661. {
  1662. if (syntax & RE_CONTEXT_INVALID_OPS)
  1663. FREE_STACK_RETURN (REG_BADRPT);
  1664. else if (!(syntax & RE_CONTEXT_INDEP_OPS))
  1665. goto normal_char;
  1666. }
  1667. {
  1668. /* Are we optimizing this jump? */
  1669. boolean keep_string_p = false;
  1670. /* 1 means zero (many) matches is allowed. */
  1671. char zero_times_ok = 0, many_times_ok = 0;
  1672. /* If there is a sequence of repetition chars, collapse it
  1673. down to just one (the right one). We can't combine
  1674. interval operators with these because of, e.g., `a{2}*',
  1675. which should only match an even number of `a's. */
  1676. for (;;)
  1677. {
  1678. zero_times_ok |= c != '+';
  1679. many_times_ok |= c != '?';
  1680. if (p == pend)
  1681. break;
  1682. PATFETCH (c);
  1683. if (c == '*'
  1684. || (!(syntax & RE_BK_PLUS_QM) && (c == '+' || c == '?')))
  1685. ;
  1686. else if (syntax & RE_BK_PLUS_QM && c == '\\')
  1687. {
  1688. if (p == pend) FREE_STACK_RETURN (REG_EESCAPE);
  1689. PATFETCH (c1);
  1690. if (!(c1 == '+' || c1 == '?'))
  1691. {
  1692. PATUNFETCH;
  1693. PATUNFETCH;
  1694. break;
  1695. }
  1696. c = c1;
  1697. }
  1698. else
  1699. {
  1700. PATUNFETCH;
  1701. break;
  1702. }
  1703. /* If we get here, we found another repeat character. */
  1704. }
  1705. /* Star, etc. applied to an empty pattern is equivalent
  1706. to an empty pattern. */
  1707. if (!laststart)
  1708. break;
  1709. /* Now we know whether or not zero matches is allowed
  1710. and also whether or not two or more matches is allowed. */
  1711. if (many_times_ok)
  1712. { /* More than one repetition is allowed, so put in at the
  1713. end a backward relative jump from `b' to before the next
  1714. jump we're going to put in below (which jumps from
  1715. laststart to after this jump).
  1716. But if we are at the `*' in the exact sequence `.*\n',
  1717. insert an unconditional jump backwards to the .,
  1718. instead of the beginning of the loop. This way we only
  1719. push a failure point once, instead of every time
  1720. through the loop. */
  1721. assert (p - 1 > pattern);
  1722. /* Allocate the space for the jump. */
  1723. GET_BUFFER_SPACE (3);
  1724. /* We know we are not at the first character of the pattern,
  1725. because laststart was nonzero. And we've already
  1726. incremented `p', by the way, to be the character after
  1727. the `*'. Do we have to do something analogous here
  1728. for null bytes, because of RE_DOT_NOT_NULL? */
  1729. if (TRANSLATE ((unsigned char)*(p - 2)) == TRANSLATE ('.')
  1730. && zero_times_ok
  1731. && p < pend
  1732. && TRANSLATE ((unsigned char)*p) == TRANSLATE ('\n')
  1733. && !(syntax & RE_DOT_NEWLINE))
  1734. { /* We have .*\n. */
  1735. STORE_JUMP (jump, b, laststart);
  1736. keep_string_p = true;
  1737. }
  1738. else
  1739. /* Anything else. */
  1740. STORE_JUMP (maybe_pop_jump, b, laststart - 3);
  1741. /* We've added more stuff to the buffer. */
  1742. b += 3;
  1743. }
  1744. /* On failure, jump from laststart to b + 3, which will be the
  1745. end of the buffer after this jump is inserted. */
  1746. GET_BUFFER_SPACE (3);
  1747. INSERT_JUMP (keep_string_p ? on_failure_keep_string_jump
  1748. : on_failure_jump,
  1749. laststart, b + 3);
  1750. pending_exact = 0;
  1751. b += 3;
  1752. if (!zero_times_ok)
  1753. {
  1754. /* At least one repetition is required, so insert a
  1755. `dummy_failure_jump' before the initial
  1756. `on_failure_jump' instruction of the loop. This
  1757. effects a skip over that instruction the first time
  1758. we hit that loop. */
  1759. GET_BUFFER_SPACE (3);
  1760. INSERT_JUMP (dummy_failure_jump, laststart, laststart + 6);
  1761. b += 3;
  1762. }
  1763. }
  1764. break;
  1765. case '.':
  1766. laststart = b;
  1767. BUF_PUSH (anychar);
  1768. break;
  1769. case '[':
  1770. {
  1771. CLEAR_RANGE_TABLE_WORK_USED (range_table_work);
  1772. if (p == pend) FREE_STACK_RETURN (REG_EBRACK);
  1773. /* Ensure that we have enough space to push a charset: the
  1774. opcode, the length count, and the bitset; 34 bytes in all. */
  1775. GET_BUFFER_SPACE (34);
  1776. laststart = b;
  1777. /* We test `*p == '^' twice, instead of using an if
  1778. statement, so we only need one BUF_PUSH. */
  1779. BUF_PUSH (*p == '^' ? charset_not : charset);
  1780. if (*p == '^')
  1781. p++;
  1782. /* Remember the first position in the bracket expression. */
  1783. p1 = p;
  1784. /* Push the number of bytes in the bitmap. */
  1785. BUF_PUSH ((1 << BYTEWIDTH) / BYTEWIDTH);
  1786. /* Clear the whole map. */
  1787. bzero (b, (1 << BYTEWIDTH) / BYTEWIDTH);
  1788. /* charset_not matches newline according to a syntax bit. */
  1789. if ((re_opcode_t) b[-2] == charset_not
  1790. && (syntax & RE_HAT_LISTS_NOT_NEWLINE))
  1791. SET_LIST_BIT ('\n');
  1792. /* Read in characters and ranges, setting map bits. */
  1793. for (;;)
  1794. {
  1795. int len;
  1796. boolean escaped_char = false;
  1797. if (p == pend) FREE_STACK_RETURN (REG_EBRACK);
  1798. PATFETCH (c);
  1799. /* \ might escape characters inside [...] and [^...]. */
  1800. if ((syntax & RE_BACKSLASH_ESCAPE_IN_LISTS) && c == '\\')
  1801. {
  1802. if (p == pend) FREE_STACK_RETURN (REG_EESCAPE);
  1803. PATFETCH (c);
  1804. escaped_char = true;
  1805. }
  1806. else
  1807. {
  1808. /* Could be the end of the bracket expression. If it's
  1809. not (i.e., when the bracket expression is `[]' so
  1810. far), the ']' character bit gets set way below. */
  1811. if (c == ']' && p != p1 + 1)
  1812. break;
  1813. }
  1814. /* If C indicates start of multibyte char, get the
  1815. actual character code in C, and set the pattern
  1816. pointer P to the next character boundary. */
  1817. if (bufp->multibyte && BASE_LEADING_CODE_P (c))
  1818. {
  1819. PATUNFETCH;
  1820. c = STRING_CHAR_AND_LENGTH (p, pend - p, len);
  1821. p += len;
  1822. }
  1823. /* What should we do for the character which is
  1824. greater than 0x7F, but not BASE_LEADING_CODE_P?
  1825. XXX */
  1826. /* See if we're at the beginning of a possible character
  1827. class. */
  1828. else if (!escaped_char &&
  1829. syntax & RE_CHAR_CLASSES && c == '[' && *p == ':')
  1830. {
  1831. /* Leave room for the null. */
  1832. char str[CHAR_CLASS_MAX_LENGTH + 1];
  1833. PATFETCH (c);
  1834. c1 = 0;
  1835. /* If pattern is `[[:'. */
  1836. if (p == pend) FREE_STACK_RETURN (REG_EBRACK);
  1837. for (;;)
  1838. {
  1839. PATFETCH (c);
  1840. if (c == ':' || c == ']' || p == pend
  1841. || c1 == CHAR_CLASS_MAX_LENGTH)
  1842. break;
  1843. str[c1++] = c;
  1844. }
  1845. str[c1] = '\0';
  1846. /* If isn't a word bracketed by `[:' and `:]':
  1847. undo the ending character, the letters, and
  1848. leave the leading `:' and `[' (but set bits for
  1849. them). */
  1850. if (c == ':' && *p == ']')
  1851. {
  1852. int ch;
  1853. boolean is_alnum = STREQ (str, "alnum");
  1854. boolean is_alpha = STREQ (str, "alpha");
  1855. boolean is_blank = STREQ (str, "blank");
  1856. boolean is_cntrl = STREQ (str, "cntrl");
  1857. boolean is_digit = STREQ (str, "digit");
  1858. boolean is_graph = STREQ (str, "graph");
  1859. boolean is_lower = STREQ (str, "lower");
  1860. boolean is_print = STREQ (str, "print");
  1861. boolean is_punct = STREQ (str, "punct");
  1862. boolean is_space = STREQ (str, "space");
  1863. boolean is_upper = STREQ (str, "upper");
  1864. boolean is_xdigit = STREQ (str, "xdigit");
  1865. if (!IS_CHAR_CLASS (str))
  1866. FREE_STACK_RETURN (REG_ECTYPE);
  1867. /* Throw away the ] at the end of the character
  1868. class. */
  1869. PATFETCH (c);
  1870. if (p == pend) FREE_STACK_RETURN (REG_EBRACK);
  1871. for (ch = 0; ch < 1 << BYTEWIDTH; ch++)
  1872. {
  1873. int translated = TRANSLATE (ch);
  1874. /* This was split into 3 if's to
  1875. avoid an arbitrary limit in some compiler. */
  1876. if ( (is_alnum && ISALNUM (ch))
  1877. || (is_alpha && ISALPHA (ch))
  1878. || (is_blank && ISBLANK (ch))
  1879. || (is_cntrl && ISCNTRL (ch)))
  1880. SET_LIST_BIT (translated);
  1881. if ( (is_digit && ISDIGIT (ch))
  1882. || (is_graph && ISGRAPH (ch))
  1883. || (is_lower && ISLOWER (ch))
  1884. || (is_print && ISPRINT (ch)))
  1885. SET_LIST_BIT (translated);
  1886. if ( (is_punct && ISPUNCT (ch))
  1887. || (is_space && ISSPACE (ch))
  1888. || (is_upper && ISUPPER (ch))
  1889. || (is_xdigit && ISXDIGIT (ch)))
  1890. SET_LIST_BIT (translated);
  1891. }
  1892. /* Repeat the loop. */
  1893. continue;
  1894. }
  1895. else
  1896. {
  1897. c1++;
  1898. while (c1--)
  1899. PATUNFETCH;
  1900. SET_LIST_BIT ('[');
  1901. /* Because the `:' may starts the range, we
  1902. can't simply set bit and repeat the loop.
  1903. Instead, just set it to C and handle below. */
  1904. c = ':';
  1905. }
  1906. }
  1907. if (p < pend && p[0] == '-' && p[1] != ']')
  1908. {
  1909. /* Discard the `-'. */
  1910. PATFETCH (c1);
  1911. /* Fetch the character which ends the range. */
  1912. PATFETCH (c1);
  1913. if (bufp->multibyte && BASE_LEADING_CODE_P (c1))
  1914. {
  1915. PATUNFETCH;
  1916. c1 = STRING_CHAR_AND_LENGTH (p, pend - p, len);
  1917. p += len;
  1918. }
  1919. if (SINGLE_BYTE_CHAR_P (c)
  1920. && ! SINGLE_BYTE_CHAR_P (c1))
  1921. {
  1922. /* Handle a range such as \177-\377 in multibyte mode.
  1923. Split that into two ranges,,
  1924. the low one ending at 0237, and the high one
  1925. starting at ...040. */
  1926. int c1_base = (c1 & ~0177) | 040;
  1927. SET_RANGE_TABLE_WORK_AREA (range_table_work, c, c1);
  1928. c1 = 0237;
  1929. }
  1930. else if (!SAME_CHARSET_P (c, c1))
  1931. FREE_STACK_RETURN (REG_ERANGE);
  1932. }
  1933. else
  1934. /* Range from C to C. */
  1935. c1 = c;
  1936. /* Set the range ... */
  1937. if (SINGLE_BYTE_CHAR_P (c))
  1938. /* ... into bitmap. */
  1939. {
  1940. unsigned this_char;
  1941. int range_start = c, range_end = c1;
  1942. /* If the start is after the end, the range is empty. */
  1943. if (range_start > range_end)
  1944. {
  1945. if (syntax & RE_NO_EMPTY_RANGES)
  1946. FREE_STACK_RETURN (REG_ERANGE);
  1947. /* Else, repeat the loop. */
  1948. }
  1949. else
  1950. {
  1951. for (this_char = range_start; this_char <= range_end;
  1952. this_char++)
  1953. SET_LIST_BIT (TRANSLATE (this_char));
  1954. }
  1955. }
  1956. else
  1957. /* ... into range table. */
  1958. SET_RANGE_TABLE_WORK_AREA (range_table_work, c, c1);
  1959. }
  1960. /* Discard any (non)matching list bytes that are all 0 at the
  1961. end of the map. Decrease the map-length byte too. */
  1962. while ((int) b[-1] > 0 && b[b[-1] - 1] == 0)
  1963. b[-1]--;
  1964. b += b[-1];
  1965. /* Build real range table from work area. */
  1966. if (RANGE_TABLE_WORK_USED (range_table_work))
  1967. {
  1968. int i;
  1969. int used = RANGE_TABLE_WORK_USED (range_table_work);
  1970. /* Allocate space for COUNT + RANGE_TABLE. Needs two
  1971. bytes for COUNT and three bytes for each character. */
  1972. GET_BUFFER_SPACE (2 + used * 3);
  1973. /* Indicate the existence of range table. */
  1974. laststart[1] |= 0x80;
  1975. STORE_NUMBER_AND_INCR (b, used / 2);
  1976. for (i = 0; i < used; i++)
  1977. STORE_CHARACTER_AND_INCR
  1978. (b, RANGE_TABLE_WORK_ELT (range_table_work, i));
  1979. }
  1980. }
  1981. break;
  1982. case '(':
  1983. if (syntax & RE_NO_BK_PARENS)
  1984. goto handle_open;
  1985. else
  1986. goto normal_char;
  1987. case ')':
  1988. if (syntax & RE_NO_BK_PARENS)
  1989. goto handle_close;
  1990. else
  1991. goto normal_char;
  1992. case '\n':
  1993. if (syntax & RE_NEWLINE_ALT)
  1994. goto handle_alt;
  1995. else
  1996. goto normal_char;
  1997. case '|':
  1998. if (syntax & RE_NO_BK_VBAR)
  1999. goto handle_alt;
  2000. else
  2001. goto normal_char;
  2002. case '{':
  2003. if (syntax & RE_INTERVALS && syntax & RE_NO_BK_BRACES)
  2004. goto handle_interval;
  2005. else
  2006. goto normal_char;
  2007. case '\\':
  2008. if (p == pend) FREE_STACK_RETURN (REG_EESCAPE);
  2009. /* Do not translate the character after the \, so that we can
  2010. distinguish, e.g., \B from \b, even if we normally would
  2011. translate, e.g., B to b. */
  2012. PATFETCH_RAW (c);
  2013. switch (c)
  2014. {
  2015. case '(':
  2016. if (syntax & RE_NO_BK_PARENS)
  2017. goto normal_backslash;
  2018. handle_open:
  2019. bufp->re_nsub++;
  2020. regnum++;
  2021. if (COMPILE_STACK_FULL)
  2022. {
  2023. RETALLOC (compile_stack.stack, compile_stack.size << 1,
  2024. compile_stack_elt_t);
  2025. if (compile_stack.stack == NULL) return REG_ESPACE;
  2026. compile_stack.size <<= 1;
  2027. }
  2028. /* These are the values to restore when we hit end of this
  2029. group. They are all relative offsets, so that if the
  2030. whole pattern moves because of realloc, they will still
  2031. be valid. */
  2032. COMPILE_STACK_TOP.begalt_offset = begalt - bufp->buffer;
  2033. COMPILE_STACK_TOP.fixup_alt_jump
  2034. = fixup_alt_jump ? fixup_alt_jump - bufp->buffer + 1 : 0;
  2035. COMPILE_STACK_TOP.laststart_offset = b - bufp->buffer;
  2036. COMPILE_STACK_TOP.regnum = regnum;
  2037. /* We will eventually replace the 0 with the number of
  2038. groups inner to this one. But do not push a
  2039. start_memory for groups beyond the last one we can
  2040. represent in the compiled pattern. */
  2041. if (regnum <= MAX_REGNUM)
  2042. {
  2043. COMPILE_STACK_TOP.inner_group_offset = b - bufp->buffer + 2;
  2044. BUF_PUSH_3 (start_memory, regnum, 0);
  2045. }
  2046. compile_stack.avail++;
  2047. fixup_alt_jump = 0;
  2048. laststart = 0;
  2049. begalt = b;
  2050. /* If we've reached MAX_REGNUM groups, then this open
  2051. won't actually generate any code, so we'll have to
  2052. clear pending_exact explicitly. */
  2053. pending_exact = 0;
  2054. break;
  2055. case ')':
  2056. if (syntax & RE_NO_BK_PARENS) goto normal_backslash;
  2057. if (COMPILE_STACK_EMPTY)
  2058. if (syntax & RE_UNMATCHED_RIGHT_PAREN_ORD)
  2059. goto normal_backslash;
  2060. else
  2061. FREE_STACK_RETURN (REG_ERPAREN);
  2062. handle_close:
  2063. if (fixup_alt_jump)
  2064. { /* Push a dummy failure point at the end of the
  2065. alternative for a possible future
  2066. `pop_failure_jump' to pop. See comments at
  2067. `push_dummy_failure' in `re_match_2'. */
  2068. BUF_PUSH (push_dummy_failure);
  2069. /* We allocated space for this jump when we assigned
  2070. to `fixup_alt_jump', in the `handle_alt' case below. */
  2071. STORE_JUMP (jump_past_alt, fixup_alt_jump, b - 1);
  2072. }
  2073. /* See similar code for backslashed left paren above. */
  2074. if (COMPILE_STACK_EMPTY)
  2075. if (syntax & RE_UNMATCHED_RIGHT_PAREN_ORD)
  2076. goto normal_char;
  2077. else
  2078. FREE_STACK_RETURN (REG_ERPAREN);
  2079. /* Since we just checked for an empty stack above, this
  2080. ``can't happen''. */
  2081. assert (compile_stack.avail != 0);
  2082. {
  2083. /* We don't just want to restore into `regnum', because
  2084. later groups should continue to be numbered higher,
  2085. as in `(ab)c(de)' -- the second group is #2. */
  2086. regnum_t this_group_regnum;
  2087. compile_stack.avail--;
  2088. begalt = bufp->buffer + COMPILE_STACK_TOP.begalt_offset;
  2089. fixup_alt_jump
  2090. = COMPILE_STACK_TOP.fixup_alt_jump
  2091. ? bufp->buffer + COMPILE_STACK_TOP.fixup_alt_jump - 1
  2092. : 0;
  2093. laststart = bufp->buffer + COMPILE_STACK_TOP.laststart_offset;
  2094. this_group_regnum = COMPILE_STACK_TOP.regnum;
  2095. /* If we've reached MAX_REGNUM groups, then this open
  2096. won't actually generate any code, so we'll have to
  2097. clear pending_exact explicitly. */
  2098. pending_exact = 0;
  2099. /* We're at the end of the group, so now we know how many
  2100. groups were inside this one. */
  2101. if (this_group_regnum <= MAX_REGNUM)
  2102. {
  2103. unsigned char *inner_group_loc
  2104. = bufp->buffer + COMPILE_STACK_TOP.inner_group_offset;
  2105. *inner_group_loc = regnum - this_group_regnum;
  2106. BUF_PUSH_3 (stop_memory, this_group_regnum,
  2107. regnum - this_group_regnum);
  2108. }
  2109. }
  2110. break;
  2111. case '|': /* `\|'. */
  2112. if (syntax & RE_LIMITED_OPS || syntax & RE_NO_BK_VBAR)
  2113. goto normal_backslash;
  2114. handle_alt:
  2115. if (syntax & RE_LIMITED_OPS)
  2116. goto normal_char;
  2117. /* Insert before the previous alternative a jump which
  2118. jumps to this alternative if the former fails. */
  2119. GET_BUFFER_SPACE (3);
  2120. INSERT_JUMP (on_failure_jump, begalt, b + 6);
  2121. pending_exact = 0;
  2122. b += 3;
  2123. /* The alternative before this one has a jump after it
  2124. which gets executed if it gets matched. Adjust that
  2125. jump so it will jump to this alternative's analogous
  2126. jump (put in below, which in turn will jump to the next
  2127. (if any) alternative's such jump, etc.). The last such
  2128. jump jumps to the correct final destination. A picture:
  2129. _____ _____
  2130. | | | |
  2131. | v | v
  2132. a | b | c
  2133. If we are at `b', then fixup_alt_jump right now points to a
  2134. three-byte space after `a'. We'll put in the jump, set
  2135. fixup_alt_jump to right after `b', and leave behind three
  2136. bytes which we'll fill in when we get to after `c'. */
  2137. if (fixup_alt_jump)
  2138. STORE_JUMP (jump_past_alt, fixup_alt_jump, b);
  2139. /* Mark and leave space for a jump after this alternative,
  2140. to be filled in later either by next alternative or
  2141. when know we're at the end of a series of alternatives. */
  2142. fixup_alt_jump = b;
  2143. GET_BUFFER_SPACE (3);
  2144. b += 3;
  2145. laststart = 0;
  2146. begalt = b;
  2147. break;
  2148. case '{':
  2149. /* If \{ is a literal. */
  2150. if (!(syntax & RE_INTERVALS)
  2151. /* If we're at `\{' and it's not the open-interval
  2152. operator. */
  2153. || ((syntax & RE_INTERVALS) && (syntax & RE_NO_BK_BRACES))
  2154. || (p - 2 == pattern && p == pend))
  2155. goto normal_backslash;
  2156. handle_interval:
  2157. {
  2158. /* If got here, then the syntax allows intervals. */
  2159. /* At least (most) this many matches must be made. */
  2160. int lower_bound = -1, upper_bound = -1;
  2161. beg_interval = p - 1;
  2162. if (p == pend)
  2163. {
  2164. if (syntax & RE_NO_BK_BRACES)
  2165. goto unfetch_interval;
  2166. else
  2167. FREE_STACK_RETURN (REG_EBRACE);
  2168. }
  2169. GET_UNSIGNED_NUMBER (lower_bound);
  2170. if (c == ',')
  2171. {
  2172. GET_UNSIGNED_NUMBER (upper_bound);
  2173. if (upper_bound < 0) upper_bound = RE_DUP_MAX;
  2174. }
  2175. else
  2176. /* Interval such as `{1}' => match exactly once. */
  2177. upper_bound = lower_bound;
  2178. if (lower_bound < 0 || upper_bound > RE_DUP_MAX
  2179. || lower_bound > upper_bound)
  2180. {
  2181. if (syntax & RE_NO_BK_BRACES)
  2182. goto unfetch_interval;
  2183. else
  2184. FREE_STACK_RETURN (REG_BADBR);
  2185. }
  2186. if (!(syntax & RE_NO_BK_BRACES))
  2187. {
  2188. if (c != '\\') FREE_STACK_RETURN (REG_EBRACE);
  2189. PATFETCH (c);
  2190. }
  2191. if (c != '}')
  2192. {
  2193. if (syntax & RE_NO_BK_BRACES)
  2194. goto unfetch_interval;
  2195. else
  2196. FREE_STACK_RETURN (REG_BADBR);
  2197. }
  2198. /* We just parsed a valid interval. */
  2199. /* If it's invalid to have no preceding re. */
  2200. if (!laststart)
  2201. {
  2202. if (syntax & RE_CONTEXT_INVALID_OPS)
  2203. FREE_STACK_RETURN (REG_BADRPT);
  2204. else if (syntax & RE_CONTEXT_INDEP_OPS)
  2205. laststart = b;
  2206. else
  2207. goto unfetch_interval;
  2208. }
  2209. /* If the upper bound is zero, don't want to succeed at
  2210. all; jump from `laststart' to `b + 3', which will be
  2211. the end of the buffer after we insert the jump. */
  2212. if (upper_bound == 0)
  2213. {
  2214. GET_BUFFER_SPACE (3);
  2215. INSERT_JUMP (jump, laststart, b + 3);
  2216. b += 3;
  2217. }
  2218. /* Otherwise, we have a nontrivial interval. When
  2219. we're all done, the pattern will look like:
  2220. set_number_at <jump count> <upper bound>
  2221. set_number_at <succeed_n count> <lower bound>
  2222. succeed_n <after jump addr> <succeed_n count>
  2223. <body of loop>
  2224. jump_n <succeed_n addr> <jump count>
  2225. (The upper bound and `jump_n' are omitted if
  2226. `upper_bound' is 1, though.) */
  2227. else
  2228. { /* If the upper bound is > 1, we need to insert
  2229. more at the end of the loop. */
  2230. unsigned nbytes = 10 + (upper_bound > 1) * 10;
  2231. GET_BUFFER_SPACE (nbytes);
  2232. /* Initialize lower bound of the `succeed_n', even
  2233. though it will be set during matching by its
  2234. attendant `set_number_at' (inserted next),
  2235. because `re_compile_fastmap' needs to know.
  2236. Jump to the `jump_n' we might insert below. */
  2237. INSERT_JUMP2 (succeed_n, laststart,
  2238. b + 5 + (upper_bound > 1) * 5,
  2239. lower_bound);
  2240. b += 5;
  2241. /* Code to initialize the lower bound. Insert
  2242. before the `succeed_n'. The `5' is the last two
  2243. bytes of this `set_number_at', plus 3 bytes of
  2244. the following `succeed_n'. */
  2245. insert_op2 (set_number_at, laststart, 5, lower_bound, b);
  2246. b += 5;
  2247. if (upper_bound > 1)
  2248. { /* More than one repetition is allowed, so
  2249. append a backward jump to the `succeed_n'
  2250. that starts this interval.
  2251. When we've reached this during matching,
  2252. we'll have matched the interval once, so
  2253. jump back only `upper_bound - 1' times. */
  2254. STORE_JUMP2 (jump_n, b, laststart + 5,
  2255. upper_bound - 1);
  2256. b += 5;
  2257. /* The location we want to set is the second
  2258. parameter of the `jump_n'; that is `b-2' as
  2259. an absolute address. `laststart' will be
  2260. the `set_number_at' we're about to insert;
  2261. `laststart+3' the number to set, the source
  2262. for the relative address. But we are
  2263. inserting into the middle of the pattern --
  2264. so everything is getting moved up by 5.
  2265. Conclusion: (b - 2) - (laststart + 3) + 5,
  2266. i.e., b - laststart.
  2267. We insert this at the beginning of the loop
  2268. so that if we fail during matching, we'll
  2269. reinitialize the bounds. */
  2270. insert_op2 (set_number_at, laststart, b - laststart,
  2271. upper_bound - 1, b);
  2272. b += 5;
  2273. }
  2274. }
  2275. pending_exact = 0;
  2276. beg_interval = NULL;
  2277. }
  2278. break;
  2279. unfetch_interval:
  2280. /* If an invalid interval, match the characters as literals. */
  2281. assert (beg_interval);
  2282. p = beg_interval;
  2283. beg_interval = NULL;
  2284. /* normal_char and normal_backslash need `c'. */
  2285. PATFETCH (c);
  2286. if (!(syntax & RE_NO_BK_BRACES))
  2287. {
  2288. if (p > pattern && p[-1] == '\\')
  2289. goto normal_backslash;
  2290. }
  2291. goto normal_char;
  2292. #ifdef emacs
  2293. /* There is no way to specify the before_dot and after_dot
  2294. operators. rms says this is ok. --karl */
  2295. case '=':
  2296. BUF_PUSH (at_dot);
  2297. break;
  2298. case 's':
  2299. laststart = b;
  2300. PATFETCH (c);
  2301. BUF_PUSH_2 (syntaxspec, syntax_spec_code[c]);
  2302. break;
  2303. case 'S':
  2304. laststart = b;
  2305. PATFETCH (c);
  2306. BUF_PUSH_2 (notsyntaxspec, syntax_spec_code[c]);
  2307. break;
  2308. case 'c':
  2309. laststart = b;
  2310. PATFETCH_RAW (c);
  2311. BUF_PUSH_2 (categoryspec, c);
  2312. break;
  2313. case 'C':
  2314. laststart = b;
  2315. PATFETCH_RAW (c);
  2316. BUF_PUSH_2 (notcategoryspec, c);
  2317. break;
  2318. #endif /* emacs */
  2319. case 'w':
  2320. laststart = b;
  2321. BUF_PUSH (wordchar);
  2322. break;
  2323. case 'W':
  2324. laststart = b;
  2325. BUF_PUSH (notwordchar);
  2326. break;
  2327. case '<':
  2328. BUF_PUSH (wordbeg);
  2329. break;
  2330. case '>':
  2331. BUF_PUSH (wordend);
  2332. break;
  2333. case 'b':
  2334. BUF_PUSH (wordbound);
  2335. break;
  2336. case 'B':
  2337. BUF_PUSH (notwordbound);
  2338. break;
  2339. case '`':
  2340. BUF_PUSH (begbuf);
  2341. break;
  2342. case '\'':
  2343. BUF_PUSH (endbuf);
  2344. break;
  2345. case '1': case '2': case '3': case '4': case '5':
  2346. case '6': case '7': case '8': case '9':
  2347. if (syntax & RE_NO_BK_REFS)
  2348. goto normal_char;
  2349. c1 = c - '0';
  2350. if (c1 > regnum)
  2351. FREE_STACK_RETURN (REG_ESUBREG);
  2352. /* Can't back reference to a subexpression if inside of it. */
  2353. if (group_in_compile_stack (compile_stack, c1))
  2354. goto normal_char;
  2355. laststart = b;
  2356. BUF_PUSH_2 (duplicate, c1);
  2357. break;
  2358. case '+':
  2359. case '?':
  2360. if (syntax & RE_BK_PLUS_QM)
  2361. goto handle_plus;
  2362. else
  2363. goto normal_backslash;
  2364. default:
  2365. normal_backslash:
  2366. /* You might think it would be useful for \ to mean
  2367. not to translate; but if we don't translate it
  2368. it will never match anything. */
  2369. c = TRANSLATE (c);
  2370. goto normal_char;
  2371. }
  2372. break;
  2373. default:
  2374. /* Expects the character in `c'. */
  2375. normal_char:
  2376. p1 = p - 1; /* P1 points the head of C. */
  2377. #ifdef emacs
  2378. if (bufp->multibyte)
  2379. {
  2380. c = STRING_CHAR (p1, pend - p1);
  2381. c = TRANSLATE (c);
  2382. /* Set P to the next character boundary. */
  2383. p += MULTIBYTE_FORM_LENGTH (p1, pend - p1) - 1;
  2384. }
  2385. #endif
  2386. /* If no exactn currently being built. */
  2387. if (!pending_exact
  2388. /* If last exactn not at current position. */
  2389. || pending_exact + *pending_exact + 1 != b
  2390. /* We have only one byte following the exactn for the count. */
  2391. || *pending_exact >= (1 << BYTEWIDTH) - (p - p1)
  2392. /* If followed by a repetition operator. */
  2393. || (p != pend && (*p == '*' || *p == '^'))
  2394. || ((syntax & RE_BK_PLUS_QM)
  2395. ? p + 1 < pend && *p == '\\' && (p[1] == '+' || p[1] == '?')
  2396. : p != pend && (*p == '+' || *p == '?'))
  2397. || ((syntax & RE_INTERVALS)
  2398. && ((syntax & RE_NO_BK_BRACES)
  2399. ? p != pend && *p == '{'
  2400. : p + 1 < pend && p[0] == '\\' && p[1] == '{')))
  2401. {
  2402. /* Start building a new exactn. */
  2403. laststart = b;
  2404. BUF_PUSH_2 (exactn, 0);
  2405. pending_exact = b - 1;
  2406. }
  2407. #ifdef emacs
  2408. if (! SINGLE_BYTE_CHAR_P (c))
  2409. {
  2410. unsigned char work[4], *str;
  2411. int i = CHAR_STRING (c, work, str);
  2412. int j;
  2413. for (j = 0; j < i; j++)
  2414. {
  2415. BUF_PUSH (str[j]);
  2416. (*pending_exact)++;
  2417. }
  2418. }
  2419. else
  2420. #endif
  2421. {
  2422. BUF_PUSH (c);
  2423. (*pending_exact)++;
  2424. }
  2425. break;
  2426. } /* switch (c) */
  2427. } /* while p != pend */
  2428. /* Through the pattern now. */
  2429. if (fixup_alt_jump)
  2430. STORE_JUMP (jump_past_alt, fixup_alt_jump, b);
  2431. if (!COMPILE_STACK_EMPTY)
  2432. FREE_STACK_RETURN (REG_EPAREN);
  2433. /* If we don't want backtracking, force success
  2434. the first time we reach the end of the compiled pattern. */
  2435. if (syntax & RE_NO_POSIX_BACKTRACKING)
  2436. BUF_PUSH (succeed);
  2437. free (compile_stack.stack);
  2438. /* We have succeeded; set the length of the buffer. */
  2439. bufp->used = b - bufp->buffer;
  2440. #ifdef DEBUG
  2441. if (debug)
  2442. {
  2443. DEBUG_PRINT1 ("\nCompiled pattern: \n");
  2444. print_compiled_pattern (bufp);
  2445. }
  2446. #endif /* DEBUG */
  2447. #ifndef MATCH_MAY_ALLOCATE
  2448. /* Initialize the failure stack to the largest possible stack. This
  2449. isn't necessary unless we're trying to avoid calling alloca in
  2450. the search and match routines. */
  2451. {
  2452. int num_regs = bufp->re_nsub + 1;
  2453. if (fail_stack.size < re_max_failures * TYPICAL_FAILURE_SIZE)
  2454. {
  2455. fail_stack.size = re_max_failures * TYPICAL_FAILURE_SIZE;
  2456. #ifdef emacs
  2457. if (! fail_stack.stack)
  2458. fail_stack.stack
  2459. = (fail_stack_elt_t *) xmalloc (fail_stack.size
  2460. * sizeof (fail_stack_elt_t));
  2461. else
  2462. fail_stack.stack
  2463. = (fail_stack_elt_t *) xrealloc (fail_stack.stack,
  2464. (fail_stack.size
  2465. * sizeof (fail_stack_elt_t)));
  2466. #else /* not emacs */
  2467. if (! fail_stack.stack)
  2468. fail_stack.stack
  2469. = (fail_stack_elt_t *) malloc (fail_stack.size
  2470. * sizeof (fail_stack_elt_t));
  2471. else
  2472. fail_stack.stack
  2473. = (fail_stack_elt_t *) realloc (fail_stack.stack,
  2474. (fail_stack.size
  2475. * sizeof (fail_stack_elt_t)));
  2476. #endif /* not emacs */
  2477. }
  2478. regex_grow_registers (num_regs);
  2479. }
  2480. #endif /* not MATCH_MAY_ALLOCATE */
  2481. return REG_NOERROR;
  2482. } /* regex_compile */
  2483. /* Subroutines for `regex_compile'. */
  2484. /* Store OP at LOC followed by two-byte integer parameter ARG. */
  2485. static void
  2486. store_op1 (op, loc, arg)
  2487. re_opcode_t op;
  2488. unsigned char *loc;
  2489. int arg;
  2490. {
  2491. *loc = (unsigned char) op;
  2492. STORE_NUMBER (loc + 1, arg);
  2493. }
  2494. /* Like `store_op1', but for two two-byte parameters ARG1 and ARG2. */
  2495. static void
  2496. store_op2 (op, loc, arg1, arg2)
  2497. re_opcode_t op;
  2498. unsigned char *loc;
  2499. int arg1, arg2;
  2500. {
  2501. *loc = (unsigned char) op;
  2502. STORE_NUMBER (loc + 1, arg1);
  2503. STORE_NUMBER (loc + 3, arg2);
  2504. }
  2505. /* Copy the bytes from LOC to END to open up three bytes of space at LOC
  2506. for OP followed by two-byte integer parameter ARG. */
  2507. static void
  2508. insert_op1 (op, loc, arg, end)
  2509. re_opcode_t op;
  2510. unsigned char *loc;
  2511. int arg;
  2512. unsigned char *end;
  2513. {
  2514. register unsigned char *pfrom = end;
  2515. register unsigned char *pto = end + 3;
  2516. while (pfrom != loc)
  2517. *--pto = *--pfrom;
  2518. store_op1 (op, loc, arg);
  2519. }
  2520. /* Like `insert_op1', but for two two-byte parameters ARG1 and ARG2. */
  2521. static void
  2522. insert_op2 (op, loc, arg1, arg2, end)
  2523. re_opcode_t op;
  2524. unsigned char *loc;
  2525. int arg1, arg2;
  2526. unsigned char *end;
  2527. {
  2528. register unsigned char *pfrom = end;
  2529. register unsigned char *pto = end + 5;
  2530. while (pfrom != loc)
  2531. *--pto = *--pfrom;
  2532. store_op2 (op, loc, arg1, arg2);
  2533. }
  2534. /* P points to just after a ^ in PATTERN. Return true if that ^ comes
  2535. after an alternative or a begin-subexpression. We assume there is at
  2536. least one character before the ^. */
  2537. static boolean
  2538. at_begline_loc_p (pattern, p, syntax)
  2539. const char *pattern, *p;
  2540. reg_syntax_t syntax;
  2541. {
  2542. const char *prev = p - 2;
  2543. boolean prev_prev_backslash = prev > pattern && prev[-1] == '\\';
  2544. return
  2545. /* After a subexpression? */
  2546. (*prev == '(' && (syntax & RE_NO_BK_PARENS || prev_prev_backslash))
  2547. /* After an alternative? */
  2548. || (*prev == '|' && (syntax & RE_NO_BK_VBAR || prev_prev_backslash));
  2549. }
  2550. /* The dual of at_begline_loc_p. This one is for $. We assume there is
  2551. at least one character after the $, i.e., `P < PEND'. */
  2552. static boolean
  2553. at_endline_loc_p (p, pend, syntax)
  2554. const char *p, *pend;
  2555. int syntax;
  2556. {
  2557. const char *next = p;
  2558. boolean next_backslash = *next == '\\';
  2559. const char *next_next = p + 1 < pend ? p + 1 : 0;
  2560. return
  2561. /* Before a subexpression? */
  2562. (syntax & RE_NO_BK_PARENS ? *next == ')'
  2563. : next_backslash && next_next && *next_next == ')')
  2564. /* Before an alternative? */
  2565. || (syntax & RE_NO_BK_VBAR ? *next == '|'
  2566. : next_backslash && next_next && *next_next == '|');
  2567. }
  2568. /* Returns true if REGNUM is in one of COMPILE_STACK's elements and
  2569. false if it's not. */
  2570. static boolean
  2571. group_in_compile_stack (compile_stack, regnum)
  2572. compile_stack_type compile_stack;
  2573. regnum_t regnum;
  2574. {
  2575. int this_element;
  2576. for (this_element = compile_stack.avail - 1;
  2577. this_element >= 0;
  2578. this_element--)
  2579. if (compile_stack.stack[this_element].regnum == regnum)
  2580. return true;
  2581. return false;
  2582. }
  2583. /* re_compile_fastmap computes a ``fastmap'' for the compiled pattern in
  2584. BUFP. A fastmap records which of the (1 << BYTEWIDTH) possible
  2585. characters can start a string that matches the pattern. This fastmap
  2586. is used by re_search to skip quickly over impossible starting points.
  2587. The caller must supply the address of a (1 << BYTEWIDTH)-byte data
  2588. area as BUFP->fastmap.
  2589. We set the `fastmap', `fastmap_accurate', and `can_be_null' fields in
  2590. the pattern buffer.
  2591. Returns 0 if we succeed, -2 if an internal error. */
  2592. int
  2593. re_compile_fastmap (bufp)
  2594. struct re_pattern_buffer *bufp;
  2595. {
  2596. int i, j, k;
  2597. #ifdef MATCH_MAY_ALLOCATE
  2598. fail_stack_type fail_stack;
  2599. #endif
  2600. #ifndef REGEX_MALLOC
  2601. char *destination;
  2602. #endif
  2603. /* We don't push any register information onto the failure stack. */
  2604. unsigned num_regs = 0;
  2605. register char *fastmap = bufp->fastmap;
  2606. unsigned char *pattern = bufp->buffer;
  2607. unsigned long size = bufp->used;
  2608. unsigned char *p = pattern;
  2609. register unsigned char *pend = pattern + size;
  2610. /* This holds the pointer to the failure stack, when
  2611. it is allocated relocatably. */
  2612. fail_stack_elt_t *failure_stack_ptr;
  2613. /* Assume that each path through the pattern can be null until
  2614. proven otherwise. We set this false at the bottom of switch
  2615. statement, to which we get only if a particular path doesn't
  2616. match the empty string. */
  2617. boolean path_can_be_null = true;
  2618. /* We aren't doing a `succeed_n' to begin with. */
  2619. boolean succeed_n_p = false;
  2620. /* If all elements for base leading-codes in fastmap is set, this
  2621. flag is set true. */
  2622. boolean match_any_multibyte_characters = false;
  2623. /* Maximum code of simple (single byte) character. */
  2624. int simple_char_max;
  2625. assert (fastmap != NULL && p != NULL);
  2626. INIT_FAIL_STACK ();
  2627. bzero (fastmap, 1 << BYTEWIDTH); /* Assume nothing's valid. */
  2628. bufp->fastmap_accurate = 1; /* It will be when we're done. */
  2629. bufp->can_be_null = 0;
  2630. while (1)
  2631. {
  2632. if (p == pend || *p == succeed)
  2633. {
  2634. /* We have reached the (effective) end of pattern. */
  2635. if (!FAIL_STACK_EMPTY ())
  2636. {
  2637. bufp->can_be_null |= path_can_be_null;
  2638. /* Reset for next path. */
  2639. path_can_be_null = true;
  2640. p = fail_stack.stack[--fail_stack.avail].pointer;
  2641. continue;
  2642. }
  2643. else
  2644. break;
  2645. }
  2646. /* We should never be about to go beyond the end of the pattern. */
  2647. assert (p < pend);
  2648. switch (SWITCH_ENUM_CAST ((re_opcode_t) *p++))
  2649. {
  2650. /* I guess the idea here is to simply not bother with a fastmap
  2651. if a backreference is used, since it's too hard to figure out
  2652. the fastmap for the corresponding group. Setting
  2653. `can_be_null' stops `re_search_2' from using the fastmap, so
  2654. that is all we do. */
  2655. case duplicate:
  2656. bufp->can_be_null = 1;
  2657. goto done;
  2658. /* Following are the cases which match a character. These end
  2659. with `break'. */
  2660. case exactn:
  2661. fastmap[p[1]] = 1;
  2662. break;
  2663. #ifndef emacs
  2664. case charset:
  2665. for (j = *p++ * BYTEWIDTH - 1; j >= 0; j--)
  2666. if (p[j / BYTEWIDTH] & (1 << (j % BYTEWIDTH)))
  2667. fastmap[j] = 1;
  2668. break;
  2669. case charset_not:
  2670. /* Chars beyond end of map must be allowed. */
  2671. for (j = *p * BYTEWIDTH; j < (1 << BYTEWIDTH); j++)
  2672. fastmap[j] = 1;
  2673. for (j = *p++ * BYTEWIDTH - 1; j >= 0; j--)
  2674. if (!(p[j / BYTEWIDTH] & (1 << (j % BYTEWIDTH))))
  2675. fastmap[j] = 1;
  2676. break;
  2677. case wordchar:
  2678. for (j = 0; j < (1 << BYTEWIDTH); j++)
  2679. if (SYNTAX (j) == Sword)
  2680. fastmap[j] = 1;
  2681. break;
  2682. case notwordchar:
  2683. for (j = 0; j < (1 << BYTEWIDTH); j++)
  2684. if (SYNTAX (j) != Sword)
  2685. fastmap[j] = 1;
  2686. break;
  2687. #else /* emacs */
  2688. case charset:
  2689. for (j = CHARSET_BITMAP_SIZE (&p[-1]) * BYTEWIDTH - 1, p++;
  2690. j >= 0; j--)
  2691. if (p[j / BYTEWIDTH] & (1 << (j % BYTEWIDTH)))
  2692. fastmap[j] = 1;
  2693. if (CHARSET_RANGE_TABLE_EXISTS_P (&p[-2])
  2694. && match_any_multibyte_characters == false)
  2695. {
  2696. /* Set fastmap[I] 1 where I is a base leading code of each
  2697. multibyte character in the range table. */
  2698. int c, count;
  2699. /* Make P points the range table. */
  2700. p += CHARSET_BITMAP_SIZE (&p[-2]);
  2701. /* Extract the number of ranges in range table into
  2702. COUNT. */
  2703. EXTRACT_NUMBER_AND_INCR (count, p);
  2704. for (; count > 0; count--, p += 2 * 3) /* XXX */
  2705. {
  2706. /* Extract the start of each range. */
  2707. EXTRACT_CHARACTER (c, p);
  2708. j = CHAR_CHARSET (c);
  2709. fastmap[CHARSET_LEADING_CODE_BASE (j)] = 1;
  2710. }
  2711. }
  2712. break;
  2713. case charset_not:
  2714. /* Chars beyond end of bitmap are possible matches.
  2715. All the single-byte codes can occur in multibyte buffers.
  2716. So any that are not listed in the charset
  2717. are possible matches, even in multibyte buffers. */
  2718. simple_char_max = (1 << BYTEWIDTH);
  2719. for (j = CHARSET_BITMAP_SIZE (&p[-1]) * BYTEWIDTH;
  2720. j < simple_char_max; j++)
  2721. fastmap[j] = 1;
  2722. for (j = CHARSET_BITMAP_SIZE (&p[-1]) * BYTEWIDTH - 1, p++;
  2723. j >= 0; j--)
  2724. if (!(p[j / BYTEWIDTH] & (1 << (j % BYTEWIDTH))))
  2725. fastmap[j] = 1;
  2726. if (bufp->multibyte)
  2727. /* Any character set can possibly contain a character
  2728. which doesn't match the specified set of characters. */
  2729. {
  2730. set_fastmap_for_multibyte_characters:
  2731. if (match_any_multibyte_characters == false)
  2732. {
  2733. for (j = 0x80; j < 0xA0; j++) /* XXX */
  2734. if (BASE_LEADING_CODE_P (j))
  2735. fastmap[j] = 1;
  2736. match_any_multibyte_characters = true;
  2737. }
  2738. }
  2739. break;
  2740. case wordchar:
  2741. /* All the single-byte codes can occur in multibyte buffers,
  2742. and they may have word syntax. So do consider them. */
  2743. simple_char_max = (1 << BYTEWIDTH);
  2744. for (j = 0; j < simple_char_max; j++)
  2745. if (SYNTAX (j) == Sword)
  2746. fastmap[j] = 1;
  2747. if (bufp->multibyte)
  2748. /* Any character set can possibly contain a character
  2749. whose syntax is `Sword'. */
  2750. goto set_fastmap_for_multibyte_characters;
  2751. break;
  2752. case notwordchar:
  2753. /* All the single-byte codes can occur in multibyte buffers,
  2754. and they may not have word syntax. So do consider them. */
  2755. simple_char_max = (1 << BYTEWIDTH);
  2756. for (j = 0; j < simple_char_max; j++)
  2757. if (SYNTAX (j) != Sword)
  2758. fastmap[j] = 1;
  2759. if (bufp->multibyte)
  2760. /* Any character set can possibly contain a character
  2761. whose syntax is not `Sword'. */
  2762. goto set_fastmap_for_multibyte_characters;
  2763. break;
  2764. #endif
  2765. case anychar:
  2766. {
  2767. int fastmap_newline = fastmap['\n'];
  2768. /* `.' matches anything, except perhaps newline.
  2769. Even in a multibyte buffer, it should match any
  2770. conceivable byte value for the fastmap. */
  2771. if (bufp->multibyte)
  2772. match_any_multibyte_characters = true;
  2773. simple_char_max = (1 << BYTEWIDTH);
  2774. for (j = 0; j < simple_char_max; j++)
  2775. fastmap[j] = 1;
  2776. /* ... except perhaps newline. */
  2777. if (!(bufp->syntax & RE_DOT_NEWLINE))
  2778. fastmap['\n'] = fastmap_newline;
  2779. /* Return if we have already set `can_be_null'; if we have,
  2780. then the fastmap is irrelevant. Something's wrong here. */
  2781. else if (bufp->can_be_null)
  2782. goto done;
  2783. /* Otherwise, have to check alternative paths. */
  2784. break;
  2785. }
  2786. #ifdef emacs
  2787. case wordbound:
  2788. case notwordbound:
  2789. case wordbeg:
  2790. case wordend:
  2791. case notsyntaxspec:
  2792. case syntaxspec:
  2793. /* This match depends on text properties. These end with
  2794. aborting optimizations. */
  2795. bufp->can_be_null = 1;
  2796. goto done;
  2797. #if 0
  2798. k = *p++;
  2799. simple_char_max = bufp->multibyte ? 0x80 : (1 << BYTEWIDTH);
  2800. for (j = 0; j < simple_char_max; j++)
  2801. if (SYNTAX (j) == (enum syntaxcode) k)
  2802. fastmap[j] = 1;
  2803. if (bufp->multibyte)
  2804. /* Any character set can possibly contain a character
  2805. whose syntax is K. */
  2806. goto set_fastmap_for_multibyte_characters;
  2807. break;
  2808. case notsyntaxspec:
  2809. k = *p++;
  2810. simple_char_max = bufp->multibyte ? 0x80 : (1 << BYTEWIDTH);
  2811. for (j = 0; j < simple_char_max; j++)
  2812. if (SYNTAX (j) != (enum syntaxcode) k)
  2813. fastmap[j] = 1;
  2814. if (bufp->multibyte)
  2815. /* Any character set can possibly contain a character
  2816. whose syntax is not K. */
  2817. goto set_fastmap_for_multibyte_characters;
  2818. break;
  2819. #endif
  2820. case categoryspec:
  2821. k = *p++;
  2822. simple_char_max = (1 << BYTEWIDTH);
  2823. for (j = 0; j < simple_char_max; j++)
  2824. if (CHAR_HAS_CATEGORY (j, k))
  2825. fastmap[j] = 1;
  2826. if (bufp->multibyte)
  2827. /* Any character set can possibly contain a character
  2828. whose category is K. */
  2829. goto set_fastmap_for_multibyte_characters;
  2830. break;
  2831. case notcategoryspec:
  2832. k = *p++;
  2833. simple_char_max = (1 << BYTEWIDTH);
  2834. for (j = 0; j < simple_char_max; j++)
  2835. if (!CHAR_HAS_CATEGORY (j, k))
  2836. fastmap[j] = 1;
  2837. if (bufp->multibyte)
  2838. /* Any character set can possibly contain a character
  2839. whose category is not K. */
  2840. goto set_fastmap_for_multibyte_characters;
  2841. break;
  2842. /* All cases after this match the empty string. These end with
  2843. `continue'. */
  2844. case before_dot:
  2845. case at_dot:
  2846. case after_dot:
  2847. continue;
  2848. #endif /* emacs */
  2849. case no_op:
  2850. case begline:
  2851. case endline:
  2852. case begbuf:
  2853. case endbuf:
  2854. #ifndef emacs
  2855. case wordbound:
  2856. case notwordbound:
  2857. case wordbeg:
  2858. case wordend:
  2859. #endif
  2860. case push_dummy_failure:
  2861. continue;
  2862. case jump_n:
  2863. case pop_failure_jump:
  2864. case maybe_pop_jump:
  2865. case jump:
  2866. case jump_past_alt:
  2867. case dummy_failure_jump:
  2868. EXTRACT_NUMBER_AND_INCR (j, p);
  2869. p += j;
  2870. if (j > 0)
  2871. continue;
  2872. /* Jump backward implies we just went through the body of a
  2873. loop and matched nothing. Opcode jumped to should be
  2874. `on_failure_jump' or `succeed_n'. Just treat it like an
  2875. ordinary jump. For a * loop, it has pushed its failure
  2876. point already; if so, discard that as redundant. */
  2877. if ((re_opcode_t) *p != on_failure_jump
  2878. && (re_opcode_t) *p != succeed_n)
  2879. continue;
  2880. p++;
  2881. EXTRACT_NUMBER_AND_INCR (j, p);
  2882. p += j;
  2883. /* If what's on the stack is where we are now, pop it. */
  2884. if (!FAIL_STACK_EMPTY ()
  2885. && fail_stack.stack[fail_stack.avail - 1].pointer == p)
  2886. fail_stack.avail--;
  2887. continue;
  2888. case on_failure_jump:
  2889. case on_failure_keep_string_jump:
  2890. handle_on_failure_jump:
  2891. EXTRACT_NUMBER_AND_INCR (j, p);
  2892. /* For some patterns, e.g., `(a?)?', `p+j' here points to the
  2893. end of the pattern. We don't want to push such a point,
  2894. since when we restore it above, entering the switch will
  2895. increment `p' past the end of the pattern. We don't need
  2896. to push such a point since we obviously won't find any more
  2897. fastmap entries beyond `pend'. Such a pattern can match
  2898. the null string, though. */
  2899. if (p + j < pend)
  2900. {
  2901. if (!PUSH_PATTERN_OP (p + j, fail_stack))
  2902. {
  2903. RESET_FAIL_STACK ();
  2904. return -2;
  2905. }
  2906. }
  2907. else
  2908. bufp->can_be_null = 1;
  2909. if (succeed_n_p)
  2910. {
  2911. EXTRACT_NUMBER_AND_INCR (k, p); /* Skip the n. */
  2912. succeed_n_p = false;
  2913. }
  2914. continue;
  2915. case succeed_n:
  2916. /* Get to the number of times to succeed. */
  2917. p += 2;
  2918. /* Increment p past the n for when k != 0. */
  2919. EXTRACT_NUMBER_AND_INCR (k, p);
  2920. if (k == 0)
  2921. {
  2922. p -= 4;
  2923. succeed_n_p = true; /* Spaghetti code alert. */
  2924. goto handle_on_failure_jump;
  2925. }
  2926. continue;
  2927. case set_number_at:
  2928. p += 4;
  2929. continue;
  2930. case start_memory:
  2931. case stop_memory:
  2932. p += 2;
  2933. continue;
  2934. default:
  2935. abort (); /* We have listed all the cases. */
  2936. } /* switch *p++ */
  2937. /* Getting here means we have found the possible starting
  2938. characters for one path of the pattern -- and that the empty
  2939. string does not match. We need not follow this path further.
  2940. Instead, look at the next alternative (remembered on the
  2941. stack), or quit if no more. The test at the top of the loop
  2942. does these things. */
  2943. path_can_be_null = false;
  2944. p = pend;
  2945. } /* while p */
  2946. /* Set `can_be_null' for the last path (also the first path, if the
  2947. pattern is empty). */
  2948. bufp->can_be_null |= path_can_be_null;
  2949. done:
  2950. RESET_FAIL_STACK ();
  2951. return 0;
  2952. } /* re_compile_fastmap */
  2953. /* Set REGS to hold NUM_REGS registers, storing them in STARTS and
  2954. ENDS. Subsequent matches using PATTERN_BUFFER and REGS will use
  2955. this memory for recording register information. STARTS and ENDS
  2956. must be allocated using the malloc library routine, and must each
  2957. be at least NUM_REGS * sizeof (regoff_t) bytes long.
  2958. If NUM_REGS == 0, then subsequent matches should allocate their own
  2959. register data.
  2960. Unless this function is called, the first search or match using
  2961. PATTERN_BUFFER will allocate its own register data, without
  2962. freeing the old data. */
  2963. void
  2964. re_set_registers (bufp, regs, num_regs, starts, ends)
  2965. struct re_pattern_buffer *bufp;
  2966. struct re_registers *regs;
  2967. unsigned num_regs;
  2968. regoff_t *starts, *ends;
  2969. {
  2970. if (num_regs)
  2971. {
  2972. bufp->regs_allocated = REGS_REALLOCATE;
  2973. regs->num_regs = num_regs;
  2974. regs->start = starts;
  2975. regs->end = ends;
  2976. }
  2977. else
  2978. {
  2979. bufp->regs_allocated = REGS_UNALLOCATED;
  2980. regs->num_regs = 0;
  2981. regs->start = regs->end = (regoff_t *) 0;
  2982. }
  2983. }
  2984. /* Searching routines. */
  2985. /* Like re_search_2, below, but only one string is specified, and
  2986. doesn't let you say where to stop matching. */
  2987. int
  2988. re_search (bufp, string, size, startpos, range, regs)
  2989. struct re_pattern_buffer *bufp;
  2990. const char *string;
  2991. int size, startpos, range;
  2992. struct re_registers *regs;
  2993. {
  2994. return re_search_2 (bufp, NULL, 0, string, size, startpos, range,
  2995. regs, size);
  2996. }
  2997. /* End address of virtual concatenation of string. */
  2998. #define STOP_ADDR_VSTRING(P) \
  2999. (((P) >= size1 ? string2 + size2 : string1 + size1))
  3000. /* Address of POS in the concatenation of virtual string. */
  3001. #define POS_ADDR_VSTRING(POS) \
  3002. (((POS) >= size1 ? string2 - size1 : string1) + (POS))
  3003. /* Using the compiled pattern in BUFP->buffer, first tries to match the
  3004. virtual concatenation of STRING1 and STRING2, starting first at index
  3005. STARTPOS, then at STARTPOS + 1, and so on.
  3006. STRING1 and STRING2 have length SIZE1 and SIZE2, respectively.
  3007. RANGE is how far to scan while trying to match. RANGE = 0 means try
  3008. only at STARTPOS; in general, the last start tried is STARTPOS +
  3009. RANGE.
  3010. In REGS, return the indices of the virtual concatenation of STRING1
  3011. and STRING2 that matched the entire BUFP->buffer and its contained
  3012. subexpressions.
  3013. Do not consider matching one past the index STOP in the virtual
  3014. concatenation of STRING1 and STRING2.
  3015. We return either the position in the strings at which the match was
  3016. found, -1 if no match, or -2 if error (such as failure
  3017. stack overflow). */
  3018. int
  3019. re_search_2 (bufp, string1, size1, string2, size2, startpos, range, regs, stop)
  3020. struct re_pattern_buffer *bufp;
  3021. const char *string1, *string2;
  3022. int size1, size2;
  3023. int startpos;
  3024. int range;
  3025. struct re_registers *regs;
  3026. int stop;
  3027. {
  3028. int val;
  3029. register char *fastmap = bufp->fastmap;
  3030. register RE_TRANSLATE_TYPE translate = bufp->translate;
  3031. int total_size = size1 + size2;
  3032. int endpos = startpos + range;
  3033. int anchored_start = 0;
  3034. /* Nonzero if we have to concern multibyte character. */
  3035. int multibyte = bufp->multibyte;
  3036. /* Check for out-of-range STARTPOS. */
  3037. if (startpos < 0 || startpos > total_size)
  3038. return -1;
  3039. /* Fix up RANGE if it might eventually take us outside
  3040. the virtual concatenation of STRING1 and STRING2.
  3041. Make sure we won't move STARTPOS below 0 or above TOTAL_SIZE. */
  3042. if (endpos < 0)
  3043. range = 0 - startpos;
  3044. else if (endpos > total_size)
  3045. range = total_size - startpos;
  3046. /* If the search isn't to be a backwards one, don't waste time in a
  3047. search for a pattern anchored at beginning of buffer. */
  3048. if (bufp->used > 0 && (re_opcode_t) bufp->buffer[0] == begbuf && range > 0)
  3049. {
  3050. if (startpos > 0)
  3051. return -1;
  3052. else
  3053. range = 0;
  3054. }
  3055. #ifdef emacs
  3056. /* In a forward search for something that starts with \=.
  3057. don't keep searching past point. */
  3058. if (bufp->used > 0 && (re_opcode_t) bufp->buffer[0] == at_dot && range > 0)
  3059. {
  3060. range = PT_BYTE - BEGV_BYTE - startpos;
  3061. if (range < 0)
  3062. return -1;
  3063. }
  3064. #endif /* emacs */
  3065. /* Update the fastmap now if not correct already. */
  3066. if (fastmap && !bufp->fastmap_accurate)
  3067. if (re_compile_fastmap (bufp) == -2)
  3068. return -2;
  3069. /* See whether the pattern is anchored. */
  3070. if (bufp->buffer[0] == begline)
  3071. anchored_start = 1;
  3072. #ifdef emacs
  3073. gl_state.object = re_match_object;
  3074. {
  3075. int adjpos = NILP (re_match_object) || BUFFERP (re_match_object);
  3076. int charpos = SYNTAX_TABLE_BYTE_TO_CHAR (startpos + adjpos);
  3077. SETUP_SYNTAX_TABLE_FOR_OBJECT (re_match_object, charpos, 1);
  3078. }
  3079. #endif
  3080. /* Loop through the string, looking for a place to start matching. */
  3081. for (;;)
  3082. {
  3083. /* If the pattern is anchored,
  3084. skip quickly past places we cannot match.
  3085. We don't bother to treat startpos == 0 specially
  3086. because that case doesn't repeat. */
  3087. if (anchored_start && startpos > 0)
  3088. {
  3089. if (! (bufp->newline_anchor
  3090. && ((startpos <= size1 ? string1[startpos - 1]
  3091. : string2[startpos - size1 - 1])
  3092. == '\n')))
  3093. goto advance;
  3094. }
  3095. /* If a fastmap is supplied, skip quickly over characters that
  3096. cannot be the start of a match. If the pattern can match the
  3097. null string, however, we don't need to skip characters; we want
  3098. the first null string. */
  3099. if (fastmap && startpos < total_size && !bufp->can_be_null)
  3100. {
  3101. register const char *d;
  3102. register unsigned int buf_ch;
  3103. d = POS_ADDR_VSTRING (startpos);
  3104. if (range > 0) /* Searching forwards. */
  3105. {
  3106. register int lim = 0;
  3107. int irange = range;
  3108. if (startpos < size1 && startpos + range >= size1)
  3109. lim = range - (size1 - startpos);
  3110. /* Written out as an if-else to avoid testing `translate'
  3111. inside the loop. */
  3112. if (RE_TRANSLATE_P (translate))
  3113. {
  3114. if (multibyte)
  3115. while (range > lim)
  3116. {
  3117. int buf_charlen;
  3118. buf_ch = STRING_CHAR_AND_LENGTH (d, range - lim,
  3119. buf_charlen);
  3120. buf_ch = RE_TRANSLATE (translate, buf_ch);
  3121. if (buf_ch >= 0400
  3122. || fastmap[buf_ch])
  3123. break;
  3124. range -= buf_charlen;
  3125. d += buf_charlen;
  3126. }
  3127. else
  3128. while (range > lim
  3129. && !fastmap[(unsigned char)
  3130. RE_TRANSLATE (translate, (unsigned char) *d)])
  3131. {
  3132. d++;
  3133. range--;
  3134. }
  3135. }
  3136. else
  3137. while (range > lim && !fastmap[(unsigned char) *d])
  3138. {
  3139. d++;
  3140. range--;
  3141. }
  3142. startpos += irange - range;
  3143. }
  3144. else /* Searching backwards. */
  3145. {
  3146. int room = (size1 == 0 || startpos >= size1
  3147. ? size2 + size1 - startpos
  3148. : size1 - startpos);
  3149. buf_ch = STRING_CHAR (d, room);
  3150. if (RE_TRANSLATE_P (translate))
  3151. buf_ch = RE_TRANSLATE (translate, buf_ch);
  3152. if (! (buf_ch >= 0400
  3153. || fastmap[buf_ch]))
  3154. goto advance;
  3155. }
  3156. }
  3157. /* If can't match the null string, and that's all we have left, fail. */
  3158. if (range >= 0 && startpos == total_size && fastmap
  3159. && !bufp->can_be_null)
  3160. return -1;
  3161. val = re_match_2_internal (bufp, string1, size1, string2, size2,
  3162. startpos, regs, stop);
  3163. #ifndef REGEX_MALLOC
  3164. #ifdef C_ALLOCA
  3165. alloca (0);
  3166. #endif
  3167. #endif
  3168. if (val >= 0)
  3169. return startpos;
  3170. if (val == -2)
  3171. return -2;
  3172. advance:
  3173. if (!range)
  3174. break;
  3175. else if (range > 0)
  3176. {
  3177. /* Update STARTPOS to the next character boundary. */
  3178. if (multibyte)
  3179. {
  3180. const unsigned char *p
  3181. = (const unsigned char *) POS_ADDR_VSTRING (startpos);
  3182. const unsigned char *pend
  3183. = (const unsigned char *) STOP_ADDR_VSTRING (startpos);
  3184. int len = MULTIBYTE_FORM_LENGTH (p, pend - p);
  3185. range -= len;
  3186. if (range < 0)
  3187. break;
  3188. startpos += len;
  3189. }
  3190. else
  3191. {
  3192. range--;
  3193. startpos++;
  3194. }
  3195. }
  3196. else
  3197. {
  3198. range++;
  3199. startpos--;
  3200. /* Update STARTPOS to the previous character boundary. */
  3201. if (multibyte)
  3202. {
  3203. const unsigned char *p
  3204. = (const unsigned char *) POS_ADDR_VSTRING (startpos);
  3205. int len = 0;
  3206. /* Find the head of multibyte form. */
  3207. while (!CHAR_HEAD_P (*p))
  3208. p--, len++;
  3209. /* Adjust it. */
  3210. #if 0 /* XXX */
  3211. if (MULTIBYTE_FORM_LENGTH (p, len + 1) != (len + 1))
  3212. ;
  3213. else
  3214. #endif
  3215. {
  3216. range += len;
  3217. if (range > 0)
  3218. break;
  3219. startpos -= len;
  3220. }
  3221. }
  3222. }
  3223. }
  3224. return -1;
  3225. } /* re_search_2 */
  3226. /* Declarations and macros for re_match_2. */
  3227. static int bcmp_translate ();
  3228. static boolean alt_match_null_string_p (),
  3229. common_op_match_null_string_p (),
  3230. group_match_null_string_p ();
  3231. /* This converts PTR, a pointer into one of the search strings `string1'
  3232. and `string2' into an offset from the beginning of that string. */
  3233. #define POINTER_TO_OFFSET(ptr) \
  3234. (FIRST_STRING_P (ptr) \
  3235. ? ((regoff_t) ((ptr) - string1)) \
  3236. : ((regoff_t) ((ptr) - string2 + size1)))
  3237. /* Macros for dealing with the split strings in re_match_2. */
  3238. #define MATCHING_IN_FIRST_STRING (dend == end_match_1)
  3239. /* Call before fetching a character with *d. This switches over to
  3240. string2 if necessary. */
  3241. #define PREFETCH() \
  3242. while (d == dend) \
  3243. { \
  3244. /* End of string2 => fail. */ \
  3245. if (dend == end_match_2) \
  3246. goto fail; \
  3247. /* End of string1 => advance to string2. */ \
  3248. d = string2; \
  3249. dend = end_match_2; \
  3250. }
  3251. /* Test if at very beginning or at very end of the virtual concatenation
  3252. of `string1' and `string2'. If only one string, it's `string2'. */
  3253. #define AT_STRINGS_BEG(d) ((d) == (size1 ? string1 : string2) || !size2)
  3254. #define AT_STRINGS_END(d) ((d) == end2)
  3255. /* Test if D points to a character which is word-constituent. We have
  3256. two special cases to check for: if past the end of string1, look at
  3257. the first character in string2; and if before the beginning of
  3258. string2, look at the last character in string1. */
  3259. #define WORDCHAR_P(d) \
  3260. (SYNTAX ((d) == end1 ? *string2 \
  3261. : (d) == string2 - 1 ? *(end1 - 1) : *(d)) \
  3262. == Sword)
  3263. /* Disabled due to a compiler bug -- see comment at case wordbound */
  3264. /* The comment at case wordbound is following one, but we don't use
  3265. AT_WORD_BOUNDARY anymore to support multibyte form.
  3266. The DEC Alpha C compiler 3.x generates incorrect code for the
  3267. test WORDCHAR_P (d - 1) != WORDCHAR_P (d) in the expansion of
  3268. AT_WORD_BOUNDARY, so this code is disabled. Expanding the
  3269. macro and introducing temporary variables works around the bug. */
  3270. #if 0
  3271. /* Test if the character before D and the one at D differ with respect
  3272. to being word-constituent. */
  3273. #define AT_WORD_BOUNDARY(d) \
  3274. (AT_STRINGS_BEG (d) || AT_STRINGS_END (d) \
  3275. || WORDCHAR_P (d - 1) != WORDCHAR_P (d))
  3276. #endif
  3277. /* Free everything we malloc. */
  3278. #ifdef MATCH_MAY_ALLOCATE
  3279. #define FREE_VAR(var) if (var) { REGEX_FREE (var); var = NULL; } else
  3280. #define FREE_VARIABLES() \
  3281. do { \
  3282. REGEX_FREE_STACK (fail_stack.stack); \
  3283. FREE_VAR (regstart); \
  3284. FREE_VAR (regend); \
  3285. FREE_VAR (old_regstart); \
  3286. FREE_VAR (old_regend); \
  3287. FREE_VAR (best_regstart); \
  3288. FREE_VAR (best_regend); \
  3289. FREE_VAR (reg_info); \
  3290. FREE_VAR (reg_dummy); \
  3291. FREE_VAR (reg_info_dummy); \
  3292. } while (0)
  3293. #else
  3294. #define FREE_VARIABLES() ((void)0) /* Do nothing! But inhibit gcc warning. */
  3295. #endif /* not MATCH_MAY_ALLOCATE */
  3296. /* These values must meet several constraints. They must not be valid
  3297. register values; since we have a limit of 255 registers (because
  3298. we use only one byte in the pattern for the register number), we can
  3299. use numbers larger than 255. They must differ by 1, because of
  3300. NUM_FAILURE_ITEMS above. And the value for the lowest register must
  3301. be larger than the value for the highest register, so we do not try
  3302. to actually save any registers when none are active. */
  3303. #define NO_HIGHEST_ACTIVE_REG (1 << BYTEWIDTH)
  3304. #define NO_LOWEST_ACTIVE_REG (NO_HIGHEST_ACTIVE_REG + 1)
  3305. /* Matching routines. */
  3306. #ifndef emacs /* Emacs never uses this. */
  3307. /* re_match is like re_match_2 except it takes only a single string. */
  3308. int
  3309. re_match (bufp, string, size, pos, regs)
  3310. struct re_pattern_buffer *bufp;
  3311. const char *string;
  3312. int size, pos;
  3313. struct re_registers *regs;
  3314. {
  3315. int result = re_match_2_internal (bufp, NULL, 0, string, size,
  3316. pos, regs, size);
  3317. #ifndef REGEX_MALLOC /* CVS */
  3318. #ifdef C_ALLOCA /* CVS */
  3319. alloca (0);
  3320. #endif /* CVS */
  3321. #endif /* CVS */
  3322. return result;
  3323. }
  3324. #endif /* not emacs */
  3325. #ifdef emacs
  3326. /* In Emacs, this is the string or buffer in which we
  3327. are matching. It is used for looking up syntax properties. */
  3328. Lisp_Object re_match_object;
  3329. #endif
  3330. /* re_match_2 matches the compiled pattern in BUFP against the
  3331. the (virtual) concatenation of STRING1 and STRING2 (of length SIZE1
  3332. and SIZE2, respectively). We start matching at POS, and stop
  3333. matching at STOP.
  3334. If REGS is non-null and the `no_sub' field of BUFP is nonzero, we
  3335. store offsets for the substring each group matched in REGS. See the
  3336. documentation for exactly how many groups we fill.
  3337. We return -1 if no match, -2 if an internal error (such as the
  3338. failure stack overflowing). Otherwise, we return the length of the
  3339. matched substring. */
  3340. int
  3341. re_match_2 (bufp, string1, size1, string2, size2, pos, regs, stop)
  3342. struct re_pattern_buffer *bufp;
  3343. const char *string1, *string2;
  3344. int size1, size2;
  3345. int pos;
  3346. struct re_registers *regs;
  3347. int stop;
  3348. {
  3349. int result;
  3350. #ifdef emacs
  3351. int charpos;
  3352. int adjpos = NILP (re_match_object) || BUFFERP (re_match_object);
  3353. gl_state.object = re_match_object;
  3354. charpos = SYNTAX_TABLE_BYTE_TO_CHAR (pos + adjpos);
  3355. SETUP_SYNTAX_TABLE_FOR_OBJECT (re_match_object, charpos, 1);
  3356. #endif
  3357. result = re_match_2_internal (bufp, string1, size1, string2, size2,
  3358. pos, regs, stop);
  3359. #ifndef REGEX_MALLOC /* CVS */
  3360. #ifdef C_ALLOCA /* CVS */
  3361. alloca (0);
  3362. #endif /* CVS */
  3363. #endif /* CVS */
  3364. return result;
  3365. }
  3366. /* This is a separate function so that we can force an alloca cleanup
  3367. afterwards. */
  3368. static int
  3369. re_match_2_internal (bufp, string1, size1, string2, size2, pos, regs, stop)
  3370. struct re_pattern_buffer *bufp;
  3371. const char *string1, *string2;
  3372. int size1, size2;
  3373. int pos;
  3374. struct re_registers *regs;
  3375. int stop;
  3376. {
  3377. /* General temporaries. */
  3378. int mcnt;
  3379. unsigned char *p1;
  3380. /* Just past the end of the corresponding string. */
  3381. const char *end1, *end2;
  3382. /* Pointers into string1 and string2, just past the last characters in
  3383. each to consider matching. */
  3384. const char *end_match_1, *end_match_2;
  3385. /* Where we are in the data, and the end of the current string. */
  3386. const char *d, *dend;
  3387. /* Where we are in the pattern, and the end of the pattern. */
  3388. unsigned char *p = bufp->buffer;
  3389. register unsigned char *pend = p + bufp->used;
  3390. /* Mark the opcode just after a start_memory, so we can test for an
  3391. empty subpattern when we get to the stop_memory. */
  3392. unsigned char *just_past_start_mem = 0;
  3393. /* We use this to map every character in the string. */
  3394. RE_TRANSLATE_TYPE translate = bufp->translate;
  3395. /* Nonzero if we have to concern multibyte character. */
  3396. int multibyte = bufp->multibyte;
  3397. /* Failure point stack. Each place that can handle a failure further
  3398. down the line pushes a failure point on this stack. It consists of
  3399. restart, regend, and reg_info for all registers corresponding to
  3400. the subexpressions we're currently inside, plus the number of such
  3401. registers, and, finally, two char *'s. The first char * is where
  3402. to resume scanning the pattern; the second one is where to resume
  3403. scanning the strings. If the latter is zero, the failure point is
  3404. a ``dummy''; if a failure happens and the failure point is a dummy,
  3405. it gets discarded and the next next one is tried. */
  3406. #ifdef MATCH_MAY_ALLOCATE /* otherwise, this is global. */
  3407. fail_stack_type fail_stack;
  3408. #endif
  3409. #ifdef DEBUG
  3410. static unsigned failure_id = 0;
  3411. unsigned nfailure_points_pushed = 0, nfailure_points_popped = 0;
  3412. #endif
  3413. /* This holds the pointer to the failure stack, when
  3414. it is allocated relocatably. */
  3415. fail_stack_elt_t *failure_stack_ptr;
  3416. /* We fill all the registers internally, independent of what we
  3417. return, for use in backreferences. The number here includes
  3418. an element for register zero. */
  3419. unsigned num_regs = bufp->re_nsub + 1;
  3420. /* The currently active registers. */
  3421. unsigned lowest_active_reg = NO_LOWEST_ACTIVE_REG;
  3422. unsigned highest_active_reg = NO_HIGHEST_ACTIVE_REG;
  3423. /* Information on the contents of registers. These are pointers into
  3424. the input strings; they record just what was matched (on this
  3425. attempt) by a subexpression part of the pattern, that is, the
  3426. regnum-th regstart pointer points to where in the pattern we began
  3427. matching and the regnum-th regend points to right after where we
  3428. stopped matching the regnum-th subexpression. (The zeroth register
  3429. keeps track of what the whole pattern matches.) */
  3430. #ifdef MATCH_MAY_ALLOCATE /* otherwise, these are global. */
  3431. const char **regstart, **regend;
  3432. #endif
  3433. /* If a group that's operated upon by a repetition operator fails to
  3434. match anything, then the register for its start will need to be
  3435. restored because it will have been set to wherever in the string we
  3436. are when we last see its open-group operator. Similarly for a
  3437. register's end. */
  3438. #ifdef MATCH_MAY_ALLOCATE /* otherwise, these are global. */
  3439. const char **old_regstart, **old_regend;
  3440. #endif
  3441. /* The is_active field of reg_info helps us keep track of which (possibly
  3442. nested) subexpressions we are currently in. The matched_something
  3443. field of reg_info[reg_num] helps us tell whether or not we have
  3444. matched any of the pattern so far this time through the reg_num-th
  3445. subexpression. These two fields get reset each time through any
  3446. loop their register is in. */
  3447. #ifdef MATCH_MAY_ALLOCATE /* otherwise, this is global. */
  3448. register_info_type *reg_info;
  3449. #endif
  3450. /* The following record the register info as found in the above
  3451. variables when we find a match better than any we've seen before.
  3452. This happens as we backtrack through the failure points, which in
  3453. turn happens only if we have not yet matched the entire string. */
  3454. unsigned best_regs_set = false;
  3455. #ifdef MATCH_MAY_ALLOCATE /* otherwise, these are global. */
  3456. const char **best_regstart, **best_regend;
  3457. #endif
  3458. /* Logically, this is `best_regend[0]'. But we don't want to have to
  3459. allocate space for that if we're not allocating space for anything
  3460. else (see below). Also, we never need info about register 0 for
  3461. any of the other register vectors, and it seems rather a kludge to
  3462. treat `best_regend' differently than the rest. So we keep track of
  3463. the end of the best match so far in a separate variable. We
  3464. initialize this to NULL so that when we backtrack the first time
  3465. and need to test it, it's not garbage. */
  3466. const char *match_end = NULL;
  3467. /* This helps SET_REGS_MATCHED avoid doing redundant work. */
  3468. int set_regs_matched_done = 0;
  3469. /* Used when we pop values we don't care about. */
  3470. #ifdef MATCH_MAY_ALLOCATE /* otherwise, these are global. */
  3471. const char **reg_dummy;
  3472. register_info_type *reg_info_dummy;
  3473. #endif
  3474. #ifdef DEBUG
  3475. /* Counts the total number of registers pushed. */
  3476. unsigned num_regs_pushed = 0;
  3477. #endif
  3478. DEBUG_PRINT1 ("\n\nEntering re_match_2.\n");
  3479. INIT_FAIL_STACK ();
  3480. #ifdef MATCH_MAY_ALLOCATE
  3481. /* Do not bother to initialize all the register variables if there are
  3482. no groups in the pattern, as it takes a fair amount of time. If
  3483. there are groups, we include space for register 0 (the whole
  3484. pattern), even though we never use it, since it simplifies the
  3485. array indexing. We should fix this. */
  3486. if (bufp->re_nsub)
  3487. {
  3488. regstart = REGEX_TALLOC (num_regs, const char *);
  3489. regend = REGEX_TALLOC (num_regs, const char *);
  3490. old_regstart = REGEX_TALLOC (num_regs, const char *);
  3491. old_regend = REGEX_TALLOC (num_regs, const char *);
  3492. best_regstart = REGEX_TALLOC (num_regs, const char *);
  3493. best_regend = REGEX_TALLOC (num_regs, const char *);
  3494. reg_info = REGEX_TALLOC (num_regs, register_info_type);
  3495. reg_dummy = REGEX_TALLOC (num_regs, const char *);
  3496. reg_info_dummy = REGEX_TALLOC (num_regs, register_info_type);
  3497. if (!(regstart && regend && old_regstart && old_regend && reg_info
  3498. && best_regstart && best_regend && reg_dummy && reg_info_dummy))
  3499. {
  3500. FREE_VARIABLES ();
  3501. return -2;
  3502. }
  3503. }
  3504. else
  3505. {
  3506. /* We must initialize all our variables to NULL, so that
  3507. `FREE_VARIABLES' doesn't try to free them. */
  3508. regstart = regend = old_regstart = old_regend = best_regstart
  3509. = best_regend = reg_dummy = NULL;
  3510. reg_info = reg_info_dummy = (register_info_type *) NULL;
  3511. }
  3512. #endif /* MATCH_MAY_ALLOCATE */
  3513. /* The starting position is bogus. */
  3514. if (pos < 0 || pos > size1 + size2)
  3515. {
  3516. FREE_VARIABLES ();
  3517. return -1;
  3518. }
  3519. /* Initialize subexpression text positions to -1 to mark ones that no
  3520. start_memory/stop_memory has been seen for. Also initialize the
  3521. register information struct. */
  3522. for (mcnt = 1; mcnt < num_regs; mcnt++)
  3523. {
  3524. regstart[mcnt] = regend[mcnt]
  3525. = old_regstart[mcnt] = old_regend[mcnt] = REG_UNSET_VALUE;
  3526. REG_MATCH_NULL_STRING_P (reg_info[mcnt]) = MATCH_NULL_UNSET_VALUE;
  3527. IS_ACTIVE (reg_info[mcnt]) = 0;
  3528. MATCHED_SOMETHING (reg_info[mcnt]) = 0;
  3529. EVER_MATCHED_SOMETHING (reg_info[mcnt]) = 0;
  3530. }
  3531. /* We move `string1' into `string2' if the latter's empty -- but not if
  3532. `string1' is null. */
  3533. if (size2 == 0 && string1 != NULL)
  3534. {
  3535. string2 = string1;
  3536. size2 = size1;
  3537. string1 = 0;
  3538. size1 = 0;
  3539. }
  3540. end1 = string1 + size1;
  3541. end2 = string2 + size2;
  3542. /* Compute where to stop matching, within the two strings. */
  3543. if (stop <= size1)
  3544. {
  3545. end_match_1 = string1 + stop;
  3546. end_match_2 = string2;
  3547. }
  3548. else
  3549. {
  3550. end_match_1 = end1;
  3551. end_match_2 = string2 + stop - size1;
  3552. }
  3553. /* `p' scans through the pattern as `d' scans through the data.
  3554. `dend' is the end of the input string that `d' points within. `d'
  3555. is advanced into the following input string whenever necessary, but
  3556. this happens before fetching; therefore, at the beginning of the
  3557. loop, `d' can be pointing at the end of a string, but it cannot
  3558. equal `string2'. */
  3559. if (size1 > 0 && pos <= size1)
  3560. {
  3561. d = string1 + pos;
  3562. dend = end_match_1;
  3563. }
  3564. else
  3565. {
  3566. d = string2 + pos - size1;
  3567. dend = end_match_2;
  3568. }
  3569. DEBUG_PRINT1 ("The compiled pattern is: ");
  3570. DEBUG_PRINT_COMPILED_PATTERN (bufp, p, pend);
  3571. DEBUG_PRINT1 ("The string to match is: `");
  3572. DEBUG_PRINT_DOUBLE_STRING (d, string1, size1, string2, size2);
  3573. DEBUG_PRINT1 ("'\n");
  3574. /* This loops over pattern commands. It exits by returning from the
  3575. function if the match is complete, or it drops through if the match
  3576. fails at this starting point in the input data. */
  3577. for (;;)
  3578. {
  3579. DEBUG_PRINT2 ("\n0x%x: ", p);
  3580. if (p == pend)
  3581. { /* End of pattern means we might have succeeded. */
  3582. DEBUG_PRINT1 ("end of pattern ... ");
  3583. /* If we haven't matched the entire string, and we want the
  3584. longest match, try backtracking. */
  3585. if (d != end_match_2)
  3586. {
  3587. /* 1 if this match ends in the same string (string1 or string2)
  3588. as the best previous match. */
  3589. boolean same_str_p = (FIRST_STRING_P (match_end)
  3590. == MATCHING_IN_FIRST_STRING);
  3591. /* 1 if this match is the best seen so far. */
  3592. boolean best_match_p;
  3593. /* AIX compiler got confused when this was combined
  3594. with the previous declaration. */
  3595. if (same_str_p)
  3596. best_match_p = d > match_end;
  3597. else
  3598. best_match_p = !MATCHING_IN_FIRST_STRING;
  3599. DEBUG_PRINT1 ("backtracking.\n");
  3600. if (!FAIL_STACK_EMPTY ())
  3601. { /* More failure points to try. */
  3602. /* If exceeds best match so far, save it. */
  3603. if (!best_regs_set || best_match_p)
  3604. {
  3605. best_regs_set = true;
  3606. match_end = d;
  3607. DEBUG_PRINT1 ("\nSAVING match as best so far.\n");
  3608. for (mcnt = 1; mcnt < num_regs; mcnt++)
  3609. {
  3610. best_regstart[mcnt] = regstart[mcnt];
  3611. best_regend[mcnt] = regend[mcnt];
  3612. }
  3613. }
  3614. goto fail;
  3615. }
  3616. /* If no failure points, don't restore garbage. And if
  3617. last match is real best match, don't restore second
  3618. best one. */
  3619. else if (best_regs_set && !best_match_p)
  3620. {
  3621. restore_best_regs:
  3622. /* Restore best match. It may happen that `dend ==
  3623. end_match_1' while the restored d is in string2.
  3624. For example, the pattern `x.*y.*z' against the
  3625. strings `x-' and `y-z-', if the two strings are
  3626. not consecutive in memory. */
  3627. DEBUG_PRINT1 ("Restoring best registers.\n");
  3628. d = match_end;
  3629. dend = ((d >= string1 && d <= end1)
  3630. ? end_match_1 : end_match_2);
  3631. for (mcnt = 1; mcnt < num_regs; mcnt++)
  3632. {
  3633. regstart[mcnt] = best_regstart[mcnt];
  3634. regend[mcnt] = best_regend[mcnt];
  3635. }
  3636. }
  3637. } /* d != end_match_2 */
  3638. succeed_label:
  3639. DEBUG_PRINT1 ("Accepting match.\n");
  3640. /* If caller wants register contents data back, do it. */
  3641. if (regs && !bufp->no_sub)
  3642. {
  3643. /* Have the register data arrays been allocated? */
  3644. if (bufp->regs_allocated == REGS_UNALLOCATED)
  3645. { /* No. So allocate them with malloc. We need one
  3646. extra element beyond `num_regs' for the `-1' marker
  3647. GNU code uses. */
  3648. regs->num_regs = MAX (RE_NREGS, num_regs + 1);
  3649. regs->start = TALLOC (regs->num_regs, regoff_t);
  3650. regs->end = TALLOC (regs->num_regs, regoff_t);
  3651. if (regs->start == NULL || regs->end == NULL)
  3652. {
  3653. FREE_VARIABLES ();
  3654. return -2;
  3655. }
  3656. bufp->regs_allocated = REGS_REALLOCATE;
  3657. }
  3658. else if (bufp->regs_allocated == REGS_REALLOCATE)
  3659. { /* Yes. If we need more elements than were already
  3660. allocated, reallocate them. If we need fewer, just
  3661. leave it alone. */
  3662. if (regs->num_regs < num_regs + 1)
  3663. {
  3664. regs->num_regs = num_regs + 1;
  3665. RETALLOC (regs->start, regs->num_regs, regoff_t);
  3666. RETALLOC (regs->end, regs->num_regs, regoff_t);
  3667. if (regs->start == NULL || regs->end == NULL)
  3668. {
  3669. FREE_VARIABLES ();
  3670. return -2;
  3671. }
  3672. }
  3673. }
  3674. else
  3675. {
  3676. /* These braces fend off a "empty body in an else-statement"
  3677. warning under GCC when assert expands to nothing. */
  3678. assert (bufp->regs_allocated == REGS_FIXED);
  3679. }
  3680. /* Convert the pointer data in `regstart' and `regend' to
  3681. indices. Register zero has to be set differently,
  3682. since we haven't kept track of any info for it. */
  3683. if (regs->num_regs > 0)
  3684. {
  3685. regs->start[0] = pos;
  3686. regs->end[0] = (MATCHING_IN_FIRST_STRING
  3687. ? ((regoff_t) (d - string1))
  3688. : ((regoff_t) (d - string2 + size1)));
  3689. }
  3690. /* Go through the first `min (num_regs, regs->num_regs)'
  3691. registers, since that is all we initialized. */
  3692. for (mcnt = 1; mcnt < MIN (num_regs, regs->num_regs); mcnt++)
  3693. {
  3694. if (REG_UNSET (regstart[mcnt]) || REG_UNSET (regend[mcnt]))
  3695. regs->start[mcnt] = regs->end[mcnt] = -1;
  3696. else
  3697. {
  3698. regs->start[mcnt]
  3699. = (regoff_t) POINTER_TO_OFFSET (regstart[mcnt]);
  3700. regs->end[mcnt]
  3701. = (regoff_t) POINTER_TO_OFFSET (regend[mcnt]);
  3702. }
  3703. }
  3704. /* If the regs structure we return has more elements than
  3705. were in the pattern, set the extra elements to -1. If
  3706. we (re)allocated the registers, this is the case,
  3707. because we always allocate enough to have at least one
  3708. -1 at the end. */
  3709. for (mcnt = num_regs; mcnt < regs->num_regs; mcnt++)
  3710. regs->start[mcnt] = regs->end[mcnt] = -1;
  3711. } /* regs && !bufp->no_sub */
  3712. DEBUG_PRINT4 ("%u failure points pushed, %u popped (%u remain).\n",
  3713. nfailure_points_pushed, nfailure_points_popped,
  3714. nfailure_points_pushed - nfailure_points_popped);
  3715. DEBUG_PRINT2 ("%u registers pushed.\n", num_regs_pushed);
  3716. mcnt = d - pos - (MATCHING_IN_FIRST_STRING
  3717. ? string1
  3718. : string2 - size1);
  3719. DEBUG_PRINT2 ("Returning %d from re_match_2.\n", mcnt);
  3720. FREE_VARIABLES ();
  3721. return mcnt;
  3722. }
  3723. /* Otherwise match next pattern command. */
  3724. switch (SWITCH_ENUM_CAST ((re_opcode_t) *p++))
  3725. {
  3726. /* Ignore these. Used to ignore the n of succeed_n's which
  3727. currently have n == 0. */
  3728. case no_op:
  3729. DEBUG_PRINT1 ("EXECUTING no_op.\n");
  3730. break;
  3731. case succeed:
  3732. DEBUG_PRINT1 ("EXECUTING succeed.\n");
  3733. goto succeed_label;
  3734. /* Match the next n pattern characters exactly. The following
  3735. byte in the pattern defines n, and the n bytes after that
  3736. are the characters to match. */
  3737. case exactn:
  3738. mcnt = *p++;
  3739. DEBUG_PRINT2 ("EXECUTING exactn %d.\n", mcnt);
  3740. /* This is written out as an if-else so we don't waste time
  3741. testing `translate' inside the loop. */
  3742. if (RE_TRANSLATE_P (translate))
  3743. {
  3744. #ifdef emacs
  3745. if (multibyte)
  3746. do
  3747. {
  3748. int pat_charlen, buf_charlen;
  3749. unsigned int pat_ch, buf_ch;
  3750. PREFETCH ();
  3751. pat_ch = STRING_CHAR_AND_LENGTH (p, pend - p, pat_charlen);
  3752. buf_ch = STRING_CHAR_AND_LENGTH (d, dend - d, buf_charlen);
  3753. if (RE_TRANSLATE (translate, buf_ch)
  3754. != pat_ch)
  3755. goto fail;
  3756. p += pat_charlen;
  3757. d += buf_charlen;
  3758. mcnt -= pat_charlen;
  3759. }
  3760. while (mcnt > 0);
  3761. else
  3762. #endif /* not emacs */
  3763. do
  3764. {
  3765. PREFETCH ();
  3766. if ((unsigned char) RE_TRANSLATE (translate, (unsigned char) *d)
  3767. != (unsigned char) *p++)
  3768. goto fail;
  3769. d++;
  3770. }
  3771. while (--mcnt);
  3772. }
  3773. else
  3774. {
  3775. do
  3776. {
  3777. PREFETCH ();
  3778. if (*d++ != (char) *p++) goto fail;
  3779. }
  3780. while (--mcnt);
  3781. }
  3782. SET_REGS_MATCHED ();
  3783. break;
  3784. /* Match any character except possibly a newline or a null. */
  3785. case anychar:
  3786. {
  3787. int buf_charlen;
  3788. unsigned int buf_ch;
  3789. DEBUG_PRINT1 ("EXECUTING anychar.\n");
  3790. PREFETCH ();
  3791. #ifdef emacs
  3792. if (multibyte)
  3793. buf_ch = STRING_CHAR_AND_LENGTH (d, dend - d, buf_charlen);
  3794. else
  3795. #endif /* not emacs */
  3796. {
  3797. buf_ch = (unsigned char) *d;
  3798. buf_charlen = 1;
  3799. }
  3800. buf_ch = TRANSLATE (buf_ch);
  3801. if ((!(bufp->syntax & RE_DOT_NEWLINE)
  3802. && buf_ch == '\n')
  3803. || ((bufp->syntax & RE_DOT_NOT_NULL)
  3804. && buf_ch == '\000'))
  3805. goto fail;
  3806. SET_REGS_MATCHED ();
  3807. DEBUG_PRINT2 (" Matched `%d'.\n", *d);
  3808. d += buf_charlen;
  3809. }
  3810. break;
  3811. case charset:
  3812. case charset_not:
  3813. {
  3814. register unsigned int c;
  3815. boolean not = (re_opcode_t) *(p - 1) == charset_not;
  3816. int len;
  3817. /* Start of actual range_table, or end of bitmap if there is no
  3818. range table. */
  3819. unsigned char *range_table;
  3820. /* Nonzero if there is range table. */
  3821. int range_table_exists;
  3822. /* Number of ranges of range table. Not in bytes. */
  3823. int count;
  3824. DEBUG_PRINT2 ("EXECUTING charset%s.\n", not ? "_not" : "");
  3825. PREFETCH ();
  3826. c = (unsigned char) *d;
  3827. range_table = CHARSET_RANGE_TABLE (&p[-1]); /* Past the bitmap. */
  3828. range_table_exists = CHARSET_RANGE_TABLE_EXISTS_P (&p[-1]);
  3829. if (range_table_exists)
  3830. EXTRACT_NUMBER_AND_INCR (count, range_table);
  3831. else
  3832. count = 0;
  3833. if (multibyte && BASE_LEADING_CODE_P (c))
  3834. c = STRING_CHAR_AND_LENGTH (d, dend - d, len);
  3835. if (SINGLE_BYTE_CHAR_P (c))
  3836. { /* Lookup bitmap. */
  3837. c = TRANSLATE (c); /* The character to match. */
  3838. len = 1;
  3839. /* Cast to `unsigned' instead of `unsigned char' in
  3840. case the bit list is a full 32 bytes long. */
  3841. if (c < (unsigned) (CHARSET_BITMAP_SIZE (&p[-1]) * BYTEWIDTH)
  3842. && p[1 + c / BYTEWIDTH] & (1 << (c % BYTEWIDTH)))
  3843. not = !not;
  3844. }
  3845. else if (range_table_exists)
  3846. CHARSET_LOOKUP_RANGE_TABLE_RAW (not, c, range_table, count);
  3847. p = CHARSET_RANGE_TABLE_END (range_table, count);
  3848. if (!not) goto fail;
  3849. SET_REGS_MATCHED ();
  3850. d += len;
  3851. break;
  3852. }
  3853. /* The beginning of a group is represented by start_memory.
  3854. The arguments are the register number in the next byte, and the
  3855. number of groups inner to this one in the next. The text
  3856. matched within the group is recorded (in the internal
  3857. registers data structure) under the register number. */
  3858. case start_memory:
  3859. DEBUG_PRINT3 ("EXECUTING start_memory %d (%d):\n", *p, p[1]);
  3860. /* Find out if this group can match the empty string. */
  3861. p1 = p; /* To send to group_match_null_string_p. */
  3862. if (REG_MATCH_NULL_STRING_P (reg_info[*p]) == MATCH_NULL_UNSET_VALUE)
  3863. REG_MATCH_NULL_STRING_P (reg_info[*p])
  3864. = group_match_null_string_p (&p1, pend, reg_info);
  3865. /* Save the position in the string where we were the last time
  3866. we were at this open-group operator in case the group is
  3867. operated upon by a repetition operator, e.g., with `(a*)*b'
  3868. against `ab'; then we want to ignore where we are now in
  3869. the string in case this attempt to match fails. */
  3870. old_regstart[*p] = REG_MATCH_NULL_STRING_P (reg_info[*p])
  3871. ? REG_UNSET (regstart[*p]) ? d : regstart[*p]
  3872. : regstart[*p];
  3873. DEBUG_PRINT2 (" old_regstart: %d\n",
  3874. POINTER_TO_OFFSET (old_regstart[*p]));
  3875. regstart[*p] = d;
  3876. DEBUG_PRINT2 (" regstart: %d\n", POINTER_TO_OFFSET (regstart[*p]));
  3877. IS_ACTIVE (reg_info[*p]) = 1;
  3878. MATCHED_SOMETHING (reg_info[*p]) = 0;
  3879. /* Clear this whenever we change the register activity status. */
  3880. set_regs_matched_done = 0;
  3881. /* This is the new highest active register. */
  3882. highest_active_reg = *p;
  3883. /* If nothing was active before, this is the new lowest active
  3884. register. */
  3885. if (lowest_active_reg == NO_LOWEST_ACTIVE_REG)
  3886. lowest_active_reg = *p;
  3887. /* Move past the register number and inner group count. */
  3888. p += 2;
  3889. just_past_start_mem = p;
  3890. break;
  3891. /* The stop_memory opcode represents the end of a group. Its
  3892. arguments are the same as start_memory's: the register
  3893. number, and the number of inner groups. */
  3894. case stop_memory:
  3895. DEBUG_PRINT3 ("EXECUTING stop_memory %d (%d):\n", *p, p[1]);
  3896. /* We need to save the string position the last time we were at
  3897. this close-group operator in case the group is operated
  3898. upon by a repetition operator, e.g., with `((a*)*(b*)*)*'
  3899. against `aba'; then we want to ignore where we are now in
  3900. the string in case this attempt to match fails. */
  3901. old_regend[*p] = REG_MATCH_NULL_STRING_P (reg_info[*p])
  3902. ? REG_UNSET (regend[*p]) ? d : regend[*p]
  3903. : regend[*p];
  3904. DEBUG_PRINT2 (" old_regend: %d\n",
  3905. POINTER_TO_OFFSET (old_regend[*p]));
  3906. regend[*p] = d;
  3907. DEBUG_PRINT2 (" regend: %d\n", POINTER_TO_OFFSET (regend[*p]));
  3908. /* This register isn't active anymore. */
  3909. IS_ACTIVE (reg_info[*p]) = 0;
  3910. /* Clear this whenever we change the register activity status. */
  3911. set_regs_matched_done = 0;
  3912. /* If this was the only register active, nothing is active
  3913. anymore. */
  3914. if (lowest_active_reg == highest_active_reg)
  3915. {
  3916. lowest_active_reg = NO_LOWEST_ACTIVE_REG;
  3917. highest_active_reg = NO_HIGHEST_ACTIVE_REG;
  3918. }
  3919. else
  3920. { /* We must scan for the new highest active register, since
  3921. it isn't necessarily one less than now: consider
  3922. (a(b)c(d(e)f)g). When group 3 ends, after the f), the
  3923. new highest active register is 1. */
  3924. unsigned char r = *p - 1;
  3925. while (r > 0 && !IS_ACTIVE (reg_info[r]))
  3926. r--;
  3927. /* If we end up at register zero, that means that we saved
  3928. the registers as the result of an `on_failure_jump', not
  3929. a `start_memory', and we jumped to past the innermost
  3930. `stop_memory'. For example, in ((.)*) we save
  3931. registers 1 and 2 as a result of the *, but when we pop
  3932. back to the second ), we are at the stop_memory 1.
  3933. Thus, nothing is active. */
  3934. if (r == 0)
  3935. {
  3936. lowest_active_reg = NO_LOWEST_ACTIVE_REG;
  3937. highest_active_reg = NO_HIGHEST_ACTIVE_REG;
  3938. }
  3939. else
  3940. highest_active_reg = r;
  3941. }
  3942. /* If just failed to match something this time around with a
  3943. group that's operated on by a repetition operator, try to
  3944. force exit from the ``loop'', and restore the register
  3945. information for this group that we had before trying this
  3946. last match. */
  3947. if ((!MATCHED_SOMETHING (reg_info[*p])
  3948. || just_past_start_mem == p - 1)
  3949. && (p + 2) < pend)
  3950. {
  3951. boolean is_a_jump_n = false;
  3952. p1 = p + 2;
  3953. mcnt = 0;
  3954. switch ((re_opcode_t) *p1++)
  3955. {
  3956. case jump_n:
  3957. is_a_jump_n = true;
  3958. case pop_failure_jump:
  3959. case maybe_pop_jump:
  3960. case jump:
  3961. case dummy_failure_jump:
  3962. EXTRACT_NUMBER_AND_INCR (mcnt, p1);
  3963. if (is_a_jump_n)
  3964. p1 += 2;
  3965. break;
  3966. default:
  3967. /* do nothing */ ;
  3968. }
  3969. p1 += mcnt;
  3970. /* If the next operation is a jump backwards in the pattern
  3971. to an on_failure_jump right before the start_memory
  3972. corresponding to this stop_memory, exit from the loop
  3973. by forcing a failure after pushing on the stack the
  3974. on_failure_jump's jump in the pattern, and d. */
  3975. if (mcnt < 0 && (re_opcode_t) *p1 == on_failure_jump
  3976. && (re_opcode_t) p1[3] == start_memory && p1[4] == *p)
  3977. {
  3978. /* If this group ever matched anything, then restore
  3979. what its registers were before trying this last
  3980. failed match, e.g., with `(a*)*b' against `ab' for
  3981. regstart[1], and, e.g., with `((a*)*(b*)*)*'
  3982. against `aba' for regend[3].
  3983. Also restore the registers for inner groups for,
  3984. e.g., `((a*)(b*))*' against `aba' (register 3 would
  3985. otherwise get trashed). */
  3986. if (EVER_MATCHED_SOMETHING (reg_info[*p]))
  3987. {
  3988. unsigned r;
  3989. EVER_MATCHED_SOMETHING (reg_info[*p]) = 0;
  3990. /* Restore this and inner groups' (if any) registers. */
  3991. for (r = *p; r < *p + *(p + 1); r++)
  3992. {
  3993. regstart[r] = old_regstart[r];
  3994. /* xx why this test? */
  3995. if (old_regend[r] >= regstart[r])
  3996. regend[r] = old_regend[r];
  3997. }
  3998. }
  3999. p1++;
  4000. EXTRACT_NUMBER_AND_INCR (mcnt, p1);
  4001. PUSH_FAILURE_POINT (p1 + mcnt, d, -2);
  4002. goto fail;
  4003. }
  4004. }
  4005. /* Move past the register number and the inner group count. */
  4006. p += 2;
  4007. break;
  4008. /* \<digit> has been turned into a `duplicate' command which is
  4009. followed by the numeric value of <digit> as the register number. */
  4010. case duplicate:
  4011. {
  4012. register const char *d2, *dend2;
  4013. int regno = *p++; /* Get which register to match against. */
  4014. DEBUG_PRINT2 ("EXECUTING duplicate %d.\n", regno);
  4015. /* Can't back reference a group which we've never matched. */
  4016. if (REG_UNSET (regstart[regno]) || REG_UNSET (regend[regno]))
  4017. goto fail;
  4018. /* Where in input to try to start matching. */
  4019. d2 = regstart[regno];
  4020. /* Where to stop matching; if both the place to start and
  4021. the place to stop matching are in the same string, then
  4022. set to the place to stop, otherwise, for now have to use
  4023. the end of the first string. */
  4024. dend2 = ((FIRST_STRING_P (regstart[regno])
  4025. == FIRST_STRING_P (regend[regno]))
  4026. ? regend[regno] : end_match_1);
  4027. for (;;)
  4028. {
  4029. /* If necessary, advance to next segment in register
  4030. contents. */
  4031. while (d2 == dend2)
  4032. {
  4033. if (dend2 == end_match_2) break;
  4034. if (dend2 == regend[regno]) break;
  4035. /* End of string1 => advance to string2. */
  4036. d2 = string2;
  4037. dend2 = regend[regno];
  4038. }
  4039. /* At end of register contents => success */
  4040. if (d2 == dend2) break;
  4041. /* If necessary, advance to next segment in data. */
  4042. PREFETCH ();
  4043. /* How many characters left in this segment to match. */
  4044. mcnt = dend - d;
  4045. /* Want how many consecutive characters we can match in
  4046. one shot, so, if necessary, adjust the count. */
  4047. if (mcnt > dend2 - d2)
  4048. mcnt = dend2 - d2;
  4049. /* Compare that many; failure if mismatch, else move
  4050. past them. */
  4051. if (RE_TRANSLATE_P (translate)
  4052. ? bcmp_translate (d, d2, mcnt, translate)
  4053. : bcmp (d, d2, mcnt))
  4054. goto fail;
  4055. d += mcnt, d2 += mcnt;
  4056. /* Do this because we've match some characters. */
  4057. SET_REGS_MATCHED ();
  4058. }
  4059. }
  4060. break;
  4061. /* begline matches the empty string at the beginning of the string
  4062. (unless `not_bol' is set in `bufp'), and, if
  4063. `newline_anchor' is set, after newlines. */
  4064. case begline:
  4065. DEBUG_PRINT1 ("EXECUTING begline.\n");
  4066. if (AT_STRINGS_BEG (d))
  4067. {
  4068. if (!bufp->not_bol) break;
  4069. }
  4070. else if (d[-1] == '\n' && bufp->newline_anchor)
  4071. {
  4072. break;
  4073. }
  4074. /* In all other cases, we fail. */
  4075. goto fail;
  4076. /* endline is the dual of begline. */
  4077. case endline:
  4078. DEBUG_PRINT1 ("EXECUTING endline.\n");
  4079. if (AT_STRINGS_END (d))
  4080. {
  4081. if (!bufp->not_eol) break;
  4082. }
  4083. /* We have to ``prefetch'' the next character. */
  4084. else if ((d == end1 ? *string2 : *d) == '\n'
  4085. && bufp->newline_anchor)
  4086. {
  4087. break;
  4088. }
  4089. goto fail;
  4090. /* Match at the very beginning of the data. */
  4091. case begbuf:
  4092. DEBUG_PRINT1 ("EXECUTING begbuf.\n");
  4093. if (AT_STRINGS_BEG (d))
  4094. break;
  4095. goto fail;
  4096. /* Match at the very end of the data. */
  4097. case endbuf:
  4098. DEBUG_PRINT1 ("EXECUTING endbuf.\n");
  4099. if (AT_STRINGS_END (d))
  4100. break;
  4101. goto fail;
  4102. /* on_failure_keep_string_jump is used to optimize `.*\n'. It
  4103. pushes NULL as the value for the string on the stack. Then
  4104. `pop_failure_point' will keep the current value for the
  4105. string, instead of restoring it. To see why, consider
  4106. matching `foo\nbar' against `.*\n'. The .* matches the foo;
  4107. then the . fails against the \n. But the next thing we want
  4108. to do is match the \n against the \n; if we restored the
  4109. string value, we would be back at the foo.
  4110. Because this is used only in specific cases, we don't need to
  4111. check all the things that `on_failure_jump' does, to make
  4112. sure the right things get saved on the stack. Hence we don't
  4113. share its code. The only reason to push anything on the
  4114. stack at all is that otherwise we would have to change
  4115. `anychar's code to do something besides goto fail in this
  4116. case; that seems worse than this. */
  4117. case on_failure_keep_string_jump:
  4118. DEBUG_PRINT1 ("EXECUTING on_failure_keep_string_jump");
  4119. EXTRACT_NUMBER_AND_INCR (mcnt, p);
  4120. DEBUG_PRINT3 (" %d (to 0x%x):\n", mcnt, p + mcnt);
  4121. PUSH_FAILURE_POINT (p + mcnt, NULL, -2);
  4122. break;
  4123. /* Uses of on_failure_jump:
  4124. Each alternative starts with an on_failure_jump that points
  4125. to the beginning of the next alternative. Each alternative
  4126. except the last ends with a jump that in effect jumps past
  4127. the rest of the alternatives. (They really jump to the
  4128. ending jump of the following alternative, because tensioning
  4129. these jumps is a hassle.)
  4130. Repeats start with an on_failure_jump that points past both
  4131. the repetition text and either the following jump or
  4132. pop_failure_jump back to this on_failure_jump. */
  4133. case on_failure_jump:
  4134. on_failure:
  4135. DEBUG_PRINT1 ("EXECUTING on_failure_jump");
  4136. #if defined (WINDOWSNT) && defined (emacs)
  4137. QUIT;
  4138. #endif
  4139. EXTRACT_NUMBER_AND_INCR (mcnt, p);
  4140. DEBUG_PRINT3 (" %d (to 0x%x)", mcnt, p + mcnt);
  4141. /* If this on_failure_jump comes right before a group (i.e.,
  4142. the original * applied to a group), save the information
  4143. for that group and all inner ones, so that if we fail back
  4144. to this point, the group's information will be correct.
  4145. For example, in \(a*\)*\1, we need the preceding group,
  4146. and in \(zz\(a*\)b*\)\2, we need the inner group. */
  4147. /* We can't use `p' to check ahead because we push
  4148. a failure point to `p + mcnt' after we do this. */
  4149. p1 = p;
  4150. /* We need to skip no_op's before we look for the
  4151. start_memory in case this on_failure_jump is happening as
  4152. the result of a completed succeed_n, as in \(a\)\{1,3\}b\1
  4153. against aba. */
  4154. while (p1 < pend && (re_opcode_t) *p1 == no_op)
  4155. p1++;
  4156. if (p1 < pend && (re_opcode_t) *p1 == start_memory)
  4157. {
  4158. /* We have a new highest active register now. This will
  4159. get reset at the start_memory we are about to get to,
  4160. but we will have saved all the registers relevant to
  4161. this repetition op, as described above. */
  4162. highest_active_reg = *(p1 + 1) + *(p1 + 2);
  4163. if (lowest_active_reg == NO_LOWEST_ACTIVE_REG)
  4164. lowest_active_reg = *(p1 + 1);
  4165. }
  4166. DEBUG_PRINT1 (":\n");
  4167. PUSH_FAILURE_POINT (p + mcnt, d, -2);
  4168. break;
  4169. /* A smart repeat ends with `maybe_pop_jump'.
  4170. We change it to either `pop_failure_jump' or `jump'. */
  4171. case maybe_pop_jump:
  4172. #if defined (WINDOWSNT) && defined (emacs)
  4173. QUIT;
  4174. #endif
  4175. EXTRACT_NUMBER_AND_INCR (mcnt, p);
  4176. DEBUG_PRINT2 ("EXECUTING maybe_pop_jump %d.\n", mcnt);
  4177. {
  4178. register unsigned char *p2 = p;
  4179. /* Compare the beginning of the repeat with what in the
  4180. pattern follows its end. If we can establish that there
  4181. is nothing that they would both match, i.e., that we
  4182. would have to backtrack because of (as in, e.g., `a*a')
  4183. then we can change to pop_failure_jump, because we'll
  4184. never have to backtrack.
  4185. This is not true in the case of alternatives: in
  4186. `(a|ab)*' we do need to backtrack to the `ab' alternative
  4187. (e.g., if the string was `ab'). But instead of trying to
  4188. detect that here, the alternative has put on a dummy
  4189. failure point which is what we will end up popping. */
  4190. /* Skip over open/close-group commands.
  4191. If what follows this loop is a ...+ construct,
  4192. look at what begins its body, since we will have to
  4193. match at least one of that. */
  4194. while (1)
  4195. {
  4196. if (p2 + 2 < pend
  4197. && ((re_opcode_t) *p2 == stop_memory
  4198. || (re_opcode_t) *p2 == start_memory))
  4199. p2 += 3;
  4200. else if (p2 + 6 < pend
  4201. && (re_opcode_t) *p2 == dummy_failure_jump)
  4202. p2 += 6;
  4203. else
  4204. break;
  4205. }
  4206. p1 = p + mcnt;
  4207. /* p1[0] ... p1[2] are the `on_failure_jump' corresponding
  4208. to the `maybe_finalize_jump' of this case. Examine what
  4209. follows. */
  4210. /* If we're at the end of the pattern, we can change. */
  4211. if (p2 == pend)
  4212. {
  4213. /* Consider what happens when matching ":\(.*\)"
  4214. against ":/". I don't really understand this code
  4215. yet. */
  4216. p[-3] = (unsigned char) pop_failure_jump;
  4217. DEBUG_PRINT1
  4218. (" End of pattern: change to `pop_failure_jump'.\n");
  4219. }
  4220. else if ((re_opcode_t) *p2 == exactn
  4221. || (bufp->newline_anchor && (re_opcode_t) *p2 == endline))
  4222. {
  4223. register unsigned int c
  4224. = *p2 == (unsigned char) endline ? '\n' : p2[2];
  4225. if ((re_opcode_t) p1[3] == exactn)
  4226. {
  4227. if (!(multibyte /* && (c != '\n') */
  4228. && BASE_LEADING_CODE_P (c))
  4229. ? c != p1[5]
  4230. : (STRING_CHAR (&p2[2], pend - &p2[2])
  4231. != STRING_CHAR (&p1[5], pend - &p1[5])))
  4232. {
  4233. p[-3] = (unsigned char) pop_failure_jump;
  4234. DEBUG_PRINT3 (" %c != %c => pop_failure_jump.\n",
  4235. c, p1[5]);
  4236. }
  4237. }
  4238. else if ((re_opcode_t) p1[3] == charset
  4239. || (re_opcode_t) p1[3] == charset_not)
  4240. {
  4241. int not = (re_opcode_t) p1[3] == charset_not;
  4242. if (multibyte /* && (c != '\n') */
  4243. && BASE_LEADING_CODE_P (c))
  4244. c = STRING_CHAR (&p2[2], pend - &p2[2]);
  4245. /* Test if C is listed in charset (or charset_not)
  4246. at `&p1[3]'. */
  4247. if (SINGLE_BYTE_CHAR_P (c))
  4248. {
  4249. if (c < CHARSET_BITMAP_SIZE (&p1[3]) * BYTEWIDTH
  4250. && p1[5 + c / BYTEWIDTH] & (1 << (c % BYTEWIDTH)))
  4251. not = !not;
  4252. }
  4253. else if (CHARSET_RANGE_TABLE_EXISTS_P (&p1[3]))
  4254. CHARSET_LOOKUP_RANGE_TABLE (not, c, &p1[3]);
  4255. /* `not' is equal to 1 if c would match, which means
  4256. that we can't change to pop_failure_jump. */
  4257. if (!not)
  4258. {
  4259. p[-3] = (unsigned char) pop_failure_jump;
  4260. DEBUG_PRINT1 (" No match => pop_failure_jump.\n");
  4261. }
  4262. }
  4263. }
  4264. else if ((re_opcode_t) *p2 == charset)
  4265. {
  4266. if ((re_opcode_t) p1[3] == exactn)
  4267. {
  4268. register unsigned int c = p1[5];
  4269. int not = 0;
  4270. if (multibyte && BASE_LEADING_CODE_P (c))
  4271. c = STRING_CHAR (&p1[5], pend - &p1[5]);
  4272. /* Test if C is listed in charset at `p2'. */
  4273. if (SINGLE_BYTE_CHAR_P (c))
  4274. {
  4275. if (c < CHARSET_BITMAP_SIZE (p2) * BYTEWIDTH
  4276. && (p2[2 + c / BYTEWIDTH]
  4277. & (1 << (c % BYTEWIDTH))))
  4278. not = !not;
  4279. }
  4280. else if (CHARSET_RANGE_TABLE_EXISTS_P (p2))
  4281. CHARSET_LOOKUP_RANGE_TABLE (not, c, p2);
  4282. if (!not)
  4283. {
  4284. p[-3] = (unsigned char) pop_failure_jump;
  4285. DEBUG_PRINT1 (" No match => pop_failure_jump.\n");
  4286. }
  4287. }
  4288. /* It is hard to list up all the character in charset
  4289. P2 if it includes multibyte character. Give up in
  4290. such case. */
  4291. else if (!multibyte || !CHARSET_RANGE_TABLE_EXISTS_P (p2))
  4292. {
  4293. /* Now, we are sure that P2 has no range table.
  4294. So, for the size of bitmap in P2, `p2[1]' is
  4295. enough. But P1 may have range table, so the
  4296. size of bitmap table of P1 is extracted by
  4297. using macro `CHARSET_BITMAP_SIZE'.
  4298. Since we know that all the character listed in
  4299. P2 is ASCII, it is enough to test only bitmap
  4300. table of P1. */
  4301. if ((re_opcode_t) p1[3] == charset_not)
  4302. {
  4303. int idx;
  4304. /* We win if the charset_not inside the loop lists
  4305. every character listed in the charset after. */
  4306. for (idx = 0; idx < (int) p2[1]; idx++)
  4307. if (! (p2[2 + idx] == 0
  4308. || (idx < CHARSET_BITMAP_SIZE (&p1[3])
  4309. && ((p2[2 + idx] & ~ p1[5 + idx]) == 0))))
  4310. break;
  4311. if (idx == p2[1])
  4312. {
  4313. p[-3] = (unsigned char) pop_failure_jump;
  4314. DEBUG_PRINT1 (" No match => pop_failure_jump.\n");
  4315. }
  4316. }
  4317. else if ((re_opcode_t) p1[3] == charset)
  4318. {
  4319. int idx;
  4320. /* We win if the charset inside the loop
  4321. has no overlap with the one after the loop. */
  4322. for (idx = 0;
  4323. (idx < (int) p2[1]
  4324. && idx < CHARSET_BITMAP_SIZE (&p1[3]));
  4325. idx++)
  4326. if ((p2[2 + idx] & p1[5 + idx]) != 0)
  4327. break;
  4328. if (idx == p2[1]
  4329. || idx == CHARSET_BITMAP_SIZE (&p1[3]))
  4330. {
  4331. p[-3] = (unsigned char) pop_failure_jump;
  4332. DEBUG_PRINT1 (" No match => pop_failure_jump.\n");
  4333. }
  4334. }
  4335. }
  4336. }
  4337. }
  4338. p -= 2; /* Point at relative address again. */
  4339. if ((re_opcode_t) p[-1] != pop_failure_jump)
  4340. {
  4341. p[-1] = (unsigned char) jump;
  4342. DEBUG_PRINT1 (" Match => jump.\n");
  4343. goto unconditional_jump;
  4344. }
  4345. /* Note fall through. */
  4346. /* The end of a simple repeat has a pop_failure_jump back to
  4347. its matching on_failure_jump, where the latter will push a
  4348. failure point. The pop_failure_jump takes off failure
  4349. points put on by this pop_failure_jump's matching
  4350. on_failure_jump; we got through the pattern to here from the
  4351. matching on_failure_jump, so didn't fail. */
  4352. case pop_failure_jump:
  4353. {
  4354. /* We need to pass separate storage for the lowest and
  4355. highest registers, even though we don't care about the
  4356. actual values. Otherwise, we will restore only one
  4357. register from the stack, since lowest will == highest in
  4358. `pop_failure_point'. */
  4359. unsigned dummy_low_reg, dummy_high_reg;
  4360. unsigned char *pdummy;
  4361. const char *sdummy;
  4362. DEBUG_PRINT1 ("EXECUTING pop_failure_jump.\n");
  4363. POP_FAILURE_POINT (sdummy, pdummy,
  4364. dummy_low_reg, dummy_high_reg,
  4365. reg_dummy, reg_dummy, reg_info_dummy);
  4366. }
  4367. /* Note fall through. */
  4368. /* Unconditionally jump (without popping any failure points). */
  4369. case jump:
  4370. unconditional_jump:
  4371. #if defined (WINDOWSNT) && defined (emacs)
  4372. QUIT;
  4373. #endif
  4374. EXTRACT_NUMBER_AND_INCR (mcnt, p); /* Get the amount to jump. */
  4375. DEBUG_PRINT2 ("EXECUTING jump %d ", mcnt);
  4376. p += mcnt; /* Do the jump. */
  4377. DEBUG_PRINT2 ("(to 0x%x).\n", p);
  4378. break;
  4379. /* We need this opcode so we can detect where alternatives end
  4380. in `group_match_null_string_p' et al. */
  4381. case jump_past_alt:
  4382. DEBUG_PRINT1 ("EXECUTING jump_past_alt.\n");
  4383. goto unconditional_jump;
  4384. /* Normally, the on_failure_jump pushes a failure point, which
  4385. then gets popped at pop_failure_jump. We will end up at
  4386. pop_failure_jump, also, and with a pattern of, say, `a+', we
  4387. are skipping over the on_failure_jump, so we have to push
  4388. something meaningless for pop_failure_jump to pop. */
  4389. case dummy_failure_jump:
  4390. DEBUG_PRINT1 ("EXECUTING dummy_failure_jump.\n");
  4391. /* It doesn't matter what we push for the string here. What
  4392. the code at `fail' tests is the value for the pattern. */
  4393. PUSH_FAILURE_POINT (0, 0, -2);
  4394. goto unconditional_jump;
  4395. /* At the end of an alternative, we need to push a dummy failure
  4396. point in case we are followed by a `pop_failure_jump', because
  4397. we don't want the failure point for the alternative to be
  4398. popped. For example, matching `(a|ab)*' against `aab'
  4399. requires that we match the `ab' alternative. */
  4400. case push_dummy_failure:
  4401. DEBUG_PRINT1 ("EXECUTING push_dummy_failure.\n");
  4402. /* See comments just above at `dummy_failure_jump' about the
  4403. two zeroes. */
  4404. PUSH_FAILURE_POINT (0, 0, -2);
  4405. break;
  4406. /* Have to succeed matching what follows at least n times.
  4407. After that, handle like `on_failure_jump'. */
  4408. case succeed_n:
  4409. EXTRACT_NUMBER (mcnt, p + 2);
  4410. DEBUG_PRINT2 ("EXECUTING succeed_n %d.\n", mcnt);
  4411. assert (mcnt >= 0);
  4412. /* Originally, this is how many times we HAVE to succeed. */
  4413. if (mcnt > 0)
  4414. {
  4415. mcnt--;
  4416. p += 2;
  4417. STORE_NUMBER_AND_INCR (p, mcnt);
  4418. DEBUG_PRINT3 (" Setting 0x%x to %d.\n", p, mcnt);
  4419. }
  4420. else if (mcnt == 0)
  4421. {
  4422. DEBUG_PRINT2 (" Setting two bytes from 0x%x to no_op.\n", p+2);
  4423. p[2] = (unsigned char) no_op;
  4424. p[3] = (unsigned char) no_op;
  4425. goto on_failure;
  4426. }
  4427. break;
  4428. case jump_n:
  4429. EXTRACT_NUMBER (mcnt, p + 2);
  4430. DEBUG_PRINT2 ("EXECUTING jump_n %d.\n", mcnt);
  4431. /* Originally, this is how many times we CAN jump. */
  4432. if (mcnt)
  4433. {
  4434. mcnt--;
  4435. STORE_NUMBER (p + 2, mcnt);
  4436. goto unconditional_jump;
  4437. }
  4438. /* If don't have to jump any more, skip over the rest of command. */
  4439. else
  4440. p += 4;
  4441. break;
  4442. case set_number_at:
  4443. {
  4444. DEBUG_PRINT1 ("EXECUTING set_number_at.\n");
  4445. EXTRACT_NUMBER_AND_INCR (mcnt, p);
  4446. p1 = p + mcnt;
  4447. EXTRACT_NUMBER_AND_INCR (mcnt, p);
  4448. DEBUG_PRINT3 (" Setting 0x%x to %d.\n", p1, mcnt);
  4449. STORE_NUMBER (p1, mcnt);
  4450. break;
  4451. }
  4452. case wordbound:
  4453. DEBUG_PRINT1 ("EXECUTING wordbound.\n");
  4454. /* We SUCCEED in one of the following cases: */
  4455. /* Case 1: D is at the beginning or the end of string. */
  4456. if (AT_STRINGS_BEG (d) || AT_STRINGS_END (d))
  4457. break;
  4458. else
  4459. {
  4460. /* C1 is the character before D, S1 is the syntax of C1, C2
  4461. is the character at D, and S2 is the syntax of C2. */
  4462. int c1, c2, s1, s2;
  4463. int pos1 = PTR_TO_OFFSET (d - 1);
  4464. int charpos;
  4465. GET_CHAR_BEFORE_2 (c1, d, string1, end1, string2, end2);
  4466. GET_CHAR_AFTER_2 (c2, d, string1, end1, string2, end2);
  4467. #ifdef emacs
  4468. charpos = SYNTAX_TABLE_BYTE_TO_CHAR (pos1);
  4469. UPDATE_SYNTAX_TABLE (charpos);
  4470. #endif
  4471. s1 = SYNTAX (c1);
  4472. #ifdef emacs
  4473. UPDATE_SYNTAX_TABLE_FORWARD (charpos + 1);
  4474. #endif
  4475. s2 = SYNTAX (c2);
  4476. if (/* Case 2: Only one of S1 and S2 is Sword. */
  4477. ((s1 == Sword) != (s2 == Sword))
  4478. /* Case 3: Both of S1 and S2 are Sword, and macro
  4479. WORD_BOUNDARY_P (C1, C2) returns nonzero. */
  4480. || ((s1 == Sword) && WORD_BOUNDARY_P (c1, c2)))
  4481. break;
  4482. }
  4483. goto fail;
  4484. case notwordbound:
  4485. DEBUG_PRINT1 ("EXECUTING notwordbound.\n");
  4486. /* We FAIL in one of the following cases: */
  4487. /* Case 1: D is at the beginning or the end of string. */
  4488. if (AT_STRINGS_BEG (d) || AT_STRINGS_END (d))
  4489. goto fail;
  4490. else
  4491. {
  4492. /* C1 is the character before D, S1 is the syntax of C1, C2
  4493. is the character at D, and S2 is the syntax of C2. */
  4494. int c1, c2, s1, s2;
  4495. int pos1 = PTR_TO_OFFSET (d - 1);
  4496. int charpos;
  4497. GET_CHAR_BEFORE_2 (c1, d, string1, end1, string2, end2);
  4498. GET_CHAR_AFTER_2 (c2, d, string1, end1, string2, end2);
  4499. #ifdef emacs
  4500. charpos = SYNTAX_TABLE_BYTE_TO_CHAR (pos1);
  4501. UPDATE_SYNTAX_TABLE (charpos);
  4502. #endif
  4503. s1 = SYNTAX (c1);
  4504. #ifdef emacs
  4505. UPDATE_SYNTAX_TABLE_FORWARD (charpos + 1);
  4506. #endif
  4507. s2 = SYNTAX (c2);
  4508. if (/* Case 2: Only one of S1 and S2 is Sword. */
  4509. ((s1 == Sword) != (s2 == Sword))
  4510. /* Case 3: Both of S1 and S2 are Sword, and macro
  4511. WORD_BOUNDARY_P (C1, C2) returns nonzero. */
  4512. || ((s1 == Sword) && WORD_BOUNDARY_P (c1, c2)))
  4513. goto fail;
  4514. }
  4515. break;
  4516. case wordbeg:
  4517. DEBUG_PRINT1 ("EXECUTING wordbeg.\n");
  4518. /* We FAIL in one of the following cases: */
  4519. /* Case 1: D is at the end of string. */
  4520. if (AT_STRINGS_END (d))
  4521. goto fail;
  4522. else
  4523. {
  4524. /* C1 is the character before D, S1 is the syntax of C1, C2
  4525. is the character at D, and S2 is the syntax of C2. */
  4526. int c1, c2, s1, s2;
  4527. int pos1 = PTR_TO_OFFSET (d);
  4528. int charpos;
  4529. GET_CHAR_AFTER_2 (c2, d, string1, end1, string2, end2);
  4530. #ifdef emacs
  4531. charpos = SYNTAX_TABLE_BYTE_TO_CHAR (pos1);
  4532. UPDATE_SYNTAX_TABLE (charpos);
  4533. #endif
  4534. s2 = SYNTAX (c2);
  4535. /* Case 2: S2 is not Sword. */
  4536. if (s2 != Sword)
  4537. goto fail;
  4538. /* Case 3: D is not at the beginning of string ... */
  4539. if (!AT_STRINGS_BEG (d))
  4540. {
  4541. GET_CHAR_BEFORE_2 (c1, d, string1, end1, string2, end2);
  4542. #ifdef emacs
  4543. UPDATE_SYNTAX_TABLE_BACKWARD (charpos - 1);
  4544. #endif
  4545. s1 = SYNTAX (c1);
  4546. /* ... and S1 is Sword, and WORD_BOUNDARY_P (C1, C2)
  4547. returns 0. */
  4548. if ((s1 == Sword) && !WORD_BOUNDARY_P (c1, c2))
  4549. goto fail;
  4550. }
  4551. }
  4552. break;
  4553. case wordend:
  4554. DEBUG_PRINT1 ("EXECUTING wordend.\n");
  4555. /* We FAIL in one of the following cases: */
  4556. /* Case 1: D is at the beginning of string. */
  4557. if (AT_STRINGS_BEG (d))
  4558. goto fail;
  4559. else
  4560. {
  4561. /* C1 is the character before D, S1 is the syntax of C1, C2
  4562. is the character at D, and S2 is the syntax of C2. */
  4563. int c1, c2, s1, s2;
  4564. int pos1 = PTR_TO_OFFSET (d);
  4565. int charpos;
  4566. GET_CHAR_BEFORE_2 (c1, d, string1, end1, string2, end2);
  4567. #ifdef emacs
  4568. charpos = SYNTAX_TABLE_BYTE_TO_CHAR (pos1 - 1);
  4569. UPDATE_SYNTAX_TABLE (charpos);
  4570. #endif
  4571. s1 = SYNTAX (c1);
  4572. /* Case 2: S1 is not Sword. */
  4573. if (s1 != Sword)
  4574. goto fail;
  4575. /* Case 3: D is not at the end of string ... */
  4576. if (!AT_STRINGS_END (d))
  4577. {
  4578. GET_CHAR_AFTER_2 (c2, d, string1, end1, string2, end2);
  4579. #ifdef emacs
  4580. UPDATE_SYNTAX_TABLE_FORWARD (charpos);
  4581. #endif
  4582. s2 = SYNTAX (c2);
  4583. /* ... and S2 is Sword, and WORD_BOUNDARY_P (C1, C2)
  4584. returns 0. */
  4585. if ((s2 == Sword) && !WORD_BOUNDARY_P (c1, c2))
  4586. goto fail;
  4587. }
  4588. }
  4589. break;
  4590. #ifdef emacs
  4591. case before_dot:
  4592. DEBUG_PRINT1 ("EXECUTING before_dot.\n");
  4593. if (PTR_BYTE_POS ((unsigned char *) d) >= PT_BYTE)
  4594. goto fail;
  4595. break;
  4596. case at_dot:
  4597. DEBUG_PRINT1 ("EXECUTING at_dot.\n");
  4598. if (PTR_BYTE_POS ((unsigned char *) d) != PT_BYTE)
  4599. goto fail;
  4600. break;
  4601. case after_dot:
  4602. DEBUG_PRINT1 ("EXECUTING after_dot.\n");
  4603. if (PTR_BYTE_POS ((unsigned char *) d) <= PT_BYTE)
  4604. goto fail;
  4605. break;
  4606. case syntaxspec:
  4607. DEBUG_PRINT2 ("EXECUTING syntaxspec %d.\n", mcnt);
  4608. mcnt = *p++;
  4609. goto matchsyntax;
  4610. case wordchar:
  4611. DEBUG_PRINT1 ("EXECUTING Emacs wordchar.\n");
  4612. mcnt = (int) Sword;
  4613. matchsyntax:
  4614. PREFETCH ();
  4615. #ifdef emacs
  4616. {
  4617. int pos1 = SYNTAX_TABLE_BYTE_TO_CHAR (PTR_TO_OFFSET (d));
  4618. UPDATE_SYNTAX_TABLE (pos1);
  4619. }
  4620. #endif
  4621. {
  4622. int c, len;
  4623. if (multibyte)
  4624. /* we must concern about multibyte form, ... */
  4625. c = STRING_CHAR_AND_LENGTH (d, dend - d, len);
  4626. else
  4627. /* everything should be handled as ASCII, even though it
  4628. looks like multibyte form. */
  4629. c = *d, len = 1;
  4630. if (SYNTAX (c) != (enum syntaxcode) mcnt)
  4631. goto fail;
  4632. d += len;
  4633. }
  4634. SET_REGS_MATCHED ();
  4635. break;
  4636. case notsyntaxspec:
  4637. DEBUG_PRINT2 ("EXECUTING notsyntaxspec %d.\n", mcnt);
  4638. mcnt = *p++;
  4639. goto matchnotsyntax;
  4640. case notwordchar:
  4641. DEBUG_PRINT1 ("EXECUTING Emacs notwordchar.\n");
  4642. mcnt = (int) Sword;
  4643. matchnotsyntax:
  4644. PREFETCH ();
  4645. #ifdef emacs
  4646. {
  4647. int pos1 = SYNTAX_TABLE_BYTE_TO_CHAR (PTR_TO_OFFSET (d));
  4648. UPDATE_SYNTAX_TABLE (pos1);
  4649. }
  4650. #endif
  4651. {
  4652. int c, len;
  4653. if (multibyte)
  4654. c = STRING_CHAR_AND_LENGTH (d, dend - d, len);
  4655. else
  4656. c = *d, len = 1;
  4657. if (SYNTAX (c) == (enum syntaxcode) mcnt)
  4658. goto fail;
  4659. d += len;
  4660. }
  4661. SET_REGS_MATCHED ();
  4662. break;
  4663. case categoryspec:
  4664. DEBUG_PRINT2 ("EXECUTING categoryspec %d.\n", *p);
  4665. mcnt = *p++;
  4666. PREFETCH ();
  4667. {
  4668. int c, len;
  4669. if (multibyte)
  4670. c = STRING_CHAR_AND_LENGTH (d, dend - d, len);
  4671. else
  4672. c = *d, len = 1;
  4673. if (!CHAR_HAS_CATEGORY (c, mcnt))
  4674. goto fail;
  4675. d += len;
  4676. }
  4677. SET_REGS_MATCHED ();
  4678. break;
  4679. case notcategoryspec:
  4680. DEBUG_PRINT2 ("EXECUTING notcategoryspec %d.\n", *p);
  4681. mcnt = *p++;
  4682. PREFETCH ();
  4683. {
  4684. int c, len;
  4685. if (multibyte)
  4686. c = STRING_CHAR_AND_LENGTH (d, dend - d, len);
  4687. else
  4688. c = *d, len = 1;
  4689. if (CHAR_HAS_CATEGORY (c, mcnt))
  4690. goto fail;
  4691. d += len;
  4692. }
  4693. SET_REGS_MATCHED ();
  4694. break;
  4695. #else /* not emacs */
  4696. case wordchar:
  4697. DEBUG_PRINT1 ("EXECUTING non-Emacs wordchar.\n");
  4698. PREFETCH ();
  4699. if (!WORDCHAR_P (d))
  4700. goto fail;
  4701. SET_REGS_MATCHED ();
  4702. d++;
  4703. break;
  4704. case notwordchar:
  4705. DEBUG_PRINT1 ("EXECUTING non-Emacs notwordchar.\n");
  4706. PREFETCH ();
  4707. if (WORDCHAR_P (d))
  4708. goto fail;
  4709. SET_REGS_MATCHED ();
  4710. d++;
  4711. break;
  4712. #endif /* not emacs */
  4713. default:
  4714. abort ();
  4715. }
  4716. continue; /* Successfully executed one pattern command; keep going. */
  4717. /* We goto here if a matching operation fails. */
  4718. fail:
  4719. #if defined (WINDOWSNT) && defined (emacs)
  4720. QUIT;
  4721. #endif
  4722. if (!FAIL_STACK_EMPTY ())
  4723. { /* A restart point is known. Restore to that state. */
  4724. DEBUG_PRINT1 ("\nFAIL:\n");
  4725. POP_FAILURE_POINT (d, p,
  4726. lowest_active_reg, highest_active_reg,
  4727. regstart, regend, reg_info);
  4728. /* If this failure point is a dummy, try the next one. */
  4729. if (!p)
  4730. goto fail;
  4731. /* If we failed to the end of the pattern, don't examine *p. */
  4732. assert (p <= pend);
  4733. if (p < pend)
  4734. {
  4735. boolean is_a_jump_n = false;
  4736. /* If failed to a backwards jump that's part of a repetition
  4737. loop, need to pop this failure point and use the next one. */
  4738. switch ((re_opcode_t) *p)
  4739. {
  4740. case jump_n:
  4741. is_a_jump_n = true;
  4742. case maybe_pop_jump:
  4743. case pop_failure_jump:
  4744. case jump:
  4745. p1 = p + 1;
  4746. EXTRACT_NUMBER_AND_INCR (mcnt, p1);
  4747. p1 += mcnt;
  4748. if ((is_a_jump_n && (re_opcode_t) *p1 == succeed_n)
  4749. || (!is_a_jump_n
  4750. && (re_opcode_t) *p1 == on_failure_jump))
  4751. goto fail;
  4752. break;
  4753. default:
  4754. /* do nothing */ ;
  4755. }
  4756. }
  4757. if (d >= string1 && d <= end1)
  4758. dend = end_match_1;
  4759. }
  4760. else
  4761. break; /* Matching at this starting point really fails. */
  4762. } /* for (;;) */
  4763. if (best_regs_set)
  4764. goto restore_best_regs;
  4765. FREE_VARIABLES ();
  4766. return -1; /* Failure to match. */
  4767. } /* re_match_2 */
  4768. /* Subroutine definitions for re_match_2. */
  4769. /* We are passed P pointing to a register number after a start_memory.
  4770. Return true if the pattern up to the corresponding stop_memory can
  4771. match the empty string, and false otherwise.
  4772. If we find the matching stop_memory, sets P to point to one past its number.
  4773. Otherwise, sets P to an undefined byte less than or equal to END.
  4774. We don't handle duplicates properly (yet). */
  4775. static boolean
  4776. group_match_null_string_p (p, end, reg_info)
  4777. unsigned char **p, *end;
  4778. register_info_type *reg_info;
  4779. {
  4780. int mcnt;
  4781. /* Point to after the args to the start_memory. */
  4782. unsigned char *p1 = *p + 2;
  4783. while (p1 < end)
  4784. {
  4785. /* Skip over opcodes that can match nothing, and return true or
  4786. false, as appropriate, when we get to one that can't, or to the
  4787. matching stop_memory. */
  4788. switch ((re_opcode_t) *p1)
  4789. {
  4790. /* Could be either a loop or a series of alternatives. */
  4791. case on_failure_jump:
  4792. p1++;
  4793. EXTRACT_NUMBER_AND_INCR (mcnt, p1);
  4794. /* If the next operation is not a jump backwards in the
  4795. pattern. */
  4796. if (mcnt >= 0)
  4797. {
  4798. /* Go through the on_failure_jumps of the alternatives,
  4799. seeing if any of the alternatives cannot match nothing.
  4800. The last alternative starts with only a jump,
  4801. whereas the rest start with on_failure_jump and end
  4802. with a jump, e.g., here is the pattern for `a|b|c':
  4803. /on_failure_jump/0/6/exactn/1/a/jump_past_alt/0/6
  4804. /on_failure_jump/0/6/exactn/1/b/jump_past_alt/0/3
  4805. /exactn/1/c
  4806. So, we have to first go through the first (n-1)
  4807. alternatives and then deal with the last one separately. */
  4808. /* Deal with the first (n-1) alternatives, which start
  4809. with an on_failure_jump (see above) that jumps to right
  4810. past a jump_past_alt. */
  4811. while ((re_opcode_t) p1[mcnt-3] == jump_past_alt)
  4812. {
  4813. /* `mcnt' holds how many bytes long the alternative
  4814. is, including the ending `jump_past_alt' and
  4815. its number. */
  4816. if (!alt_match_null_string_p (p1, p1 + mcnt - 3,
  4817. reg_info))
  4818. return false;
  4819. /* Move to right after this alternative, including the
  4820. jump_past_alt. */
  4821. p1 += mcnt;
  4822. /* Break if it's the beginning of an n-th alternative
  4823. that doesn't begin with an on_failure_jump. */
  4824. if ((re_opcode_t) *p1 != on_failure_jump)
  4825. break;
  4826. /* Still have to check that it's not an n-th
  4827. alternative that starts with an on_failure_jump. */
  4828. p1++;
  4829. EXTRACT_NUMBER_AND_INCR (mcnt, p1);
  4830. if ((re_opcode_t) p1[mcnt-3] != jump_past_alt)
  4831. {
  4832. /* Get to the beginning of the n-th alternative. */
  4833. p1 -= 3;
  4834. break;
  4835. }
  4836. }
  4837. /* Deal with the last alternative: go back and get number
  4838. of the `jump_past_alt' just before it. `mcnt' contains
  4839. the length of the alternative. */
  4840. EXTRACT_NUMBER (mcnt, p1 - 2);
  4841. if (!alt_match_null_string_p (p1, p1 + mcnt, reg_info))
  4842. return false;
  4843. p1 += mcnt; /* Get past the n-th alternative. */
  4844. } /* if mcnt > 0 */
  4845. break;
  4846. case stop_memory:
  4847. assert (p1[1] == **p);
  4848. *p = p1 + 2;
  4849. return true;
  4850. default:
  4851. if (!common_op_match_null_string_p (&p1, end, reg_info))
  4852. return false;
  4853. }
  4854. } /* while p1 < end */
  4855. return false;
  4856. } /* group_match_null_string_p */
  4857. /* Similar to group_match_null_string_p, but doesn't deal with alternatives:
  4858. It expects P to be the first byte of a single alternative and END one
  4859. byte past the last. The alternative can contain groups. */
  4860. static boolean
  4861. alt_match_null_string_p (p, end, reg_info)
  4862. unsigned char *p, *end;
  4863. register_info_type *reg_info;
  4864. {
  4865. int mcnt;
  4866. unsigned char *p1 = p;
  4867. while (p1 < end)
  4868. {
  4869. /* Skip over opcodes that can match nothing, and break when we get
  4870. to one that can't. */
  4871. switch ((re_opcode_t) *p1)
  4872. {
  4873. /* It's a loop. */
  4874. case on_failure_jump:
  4875. p1++;
  4876. EXTRACT_NUMBER_AND_INCR (mcnt, p1);
  4877. p1 += mcnt;
  4878. break;
  4879. default:
  4880. if (!common_op_match_null_string_p (&p1, end, reg_info))
  4881. return false;
  4882. }
  4883. } /* while p1 < end */
  4884. return true;
  4885. } /* alt_match_null_string_p */
  4886. /* Deals with the ops common to group_match_null_string_p and
  4887. alt_match_null_string_p.
  4888. Sets P to one after the op and its arguments, if any. */
  4889. static boolean
  4890. common_op_match_null_string_p (p, end, reg_info)
  4891. unsigned char **p, *end;
  4892. register_info_type *reg_info;
  4893. {
  4894. int mcnt;
  4895. boolean ret;
  4896. int reg_no;
  4897. unsigned char *p1 = *p;
  4898. switch ((re_opcode_t) *p1++)
  4899. {
  4900. case no_op:
  4901. case begline:
  4902. case endline:
  4903. case begbuf:
  4904. case endbuf:
  4905. case wordbeg:
  4906. case wordend:
  4907. case wordbound:
  4908. case notwordbound:
  4909. #ifdef emacs
  4910. case before_dot:
  4911. case at_dot:
  4912. case after_dot:
  4913. #endif
  4914. break;
  4915. case start_memory:
  4916. reg_no = *p1;
  4917. assert (reg_no > 0 && reg_no <= MAX_REGNUM);
  4918. ret = group_match_null_string_p (&p1, end, reg_info);
  4919. /* Have to set this here in case we're checking a group which
  4920. contains a group and a back reference to it. */
  4921. if (REG_MATCH_NULL_STRING_P (reg_info[reg_no]) == MATCH_NULL_UNSET_VALUE)
  4922. REG_MATCH_NULL_STRING_P (reg_info[reg_no]) = ret;
  4923. if (!ret)
  4924. return false;
  4925. break;
  4926. /* If this is an optimized succeed_n for zero times, make the jump. */
  4927. case jump:
  4928. EXTRACT_NUMBER_AND_INCR (mcnt, p1);
  4929. if (mcnt >= 0)
  4930. p1 += mcnt;
  4931. else
  4932. return false;
  4933. break;
  4934. case succeed_n:
  4935. /* Get to the number of times to succeed. */
  4936. p1 += 2;
  4937. EXTRACT_NUMBER_AND_INCR (mcnt, p1);
  4938. if (mcnt == 0)
  4939. {
  4940. p1 -= 4;
  4941. EXTRACT_NUMBER_AND_INCR (mcnt, p1);
  4942. p1 += mcnt;
  4943. }
  4944. else
  4945. return false;
  4946. break;
  4947. case duplicate:
  4948. if (!REG_MATCH_NULL_STRING_P (reg_info[*p1]))
  4949. return false;
  4950. break;
  4951. case set_number_at:
  4952. p1 += 4;
  4953. default:
  4954. /* All other opcodes mean we cannot match the empty string. */
  4955. return false;
  4956. }
  4957. *p = p1;
  4958. return true;
  4959. } /* common_op_match_null_string_p */
  4960. /* Return zero if TRANSLATE[S1] and TRANSLATE[S2] are identical for LEN
  4961. bytes; nonzero otherwise. */
  4962. static int
  4963. bcmp_translate (s1, s2, len, translate)
  4964. unsigned char *s1, *s2;
  4965. register int len;
  4966. RE_TRANSLATE_TYPE translate;
  4967. {
  4968. register unsigned char *p1 = s1, *p2 = s2;
  4969. unsigned char *p1_end = s1 + len;
  4970. unsigned char *p2_end = s2 + len;
  4971. while (p1 != p1_end && p2 != p2_end)
  4972. {
  4973. int p1_charlen, p2_charlen;
  4974. int p1_ch, p2_ch;
  4975. p1_ch = STRING_CHAR_AND_LENGTH (p1, p1_end - p1, p1_charlen);
  4976. p2_ch = STRING_CHAR_AND_LENGTH (p2, p2_end - p2, p2_charlen);
  4977. if (RE_TRANSLATE (translate, p1_ch)
  4978. != RE_TRANSLATE (translate, p2_ch))
  4979. return 1;
  4980. p1 += p1_charlen, p2 += p2_charlen;
  4981. }
  4982. if (p1 != p1_end || p2 != p2_end)
  4983. return 1;
  4984. return 0;
  4985. }
  4986. /* Entry points for GNU code. */
  4987. /* re_compile_pattern is the GNU regular expression compiler: it
  4988. compiles PATTERN (of length SIZE) and puts the result in BUFP.
  4989. Returns 0 if the pattern was valid, otherwise an error string.
  4990. Assumes the `allocated' (and perhaps `buffer') and `translate' fields
  4991. are set in BUFP on entry.
  4992. We call regex_compile to do the actual compilation. */
  4993. const char *
  4994. re_compile_pattern (pattern, length, bufp)
  4995. const char *pattern;
  4996. int length;
  4997. struct re_pattern_buffer *bufp;
  4998. {
  4999. reg_errcode_t ret;
  5000. /* GNU code is written to assume at least RE_NREGS registers will be set
  5001. (and at least one extra will be -1). */
  5002. bufp->regs_allocated = REGS_UNALLOCATED;
  5003. /* And GNU code determines whether or not to get register information
  5004. by passing null for the REGS argument to re_match, etc., not by
  5005. setting no_sub. */
  5006. bufp->no_sub = 0;
  5007. /* Match anchors at newline. */
  5008. bufp->newline_anchor = 1;
  5009. ret = regex_compile (pattern, length, re_syntax_options, bufp);
  5010. if (!ret)
  5011. return NULL;
  5012. return gettext (re_error_msgid[(int) ret]);
  5013. }
  5014. /* Entry points compatible with 4.2 BSD regex library. We don't define
  5015. them unless specifically requested. */
  5016. #if defined (_REGEX_RE_COMP) || defined (_LIBC)
  5017. /* BSD has one and only one pattern buffer. */
  5018. static struct re_pattern_buffer re_comp_buf;
  5019. char *
  5020. #ifdef _LIBC
  5021. /* Make these definitions weak in libc, so POSIX programs can redefine
  5022. these names if they don't use our functions, and still use
  5023. regcomp/regexec below without link errors. */
  5024. weak_function
  5025. #endif
  5026. re_comp (s)
  5027. const char *s;
  5028. {
  5029. reg_errcode_t ret;
  5030. if (!s)
  5031. {
  5032. if (!re_comp_buf.buffer)
  5033. return (char *) gettext ("No previous regular expression");
  5034. return 0;
  5035. }
  5036. if (!re_comp_buf.buffer)
  5037. {
  5038. re_comp_buf.buffer = (unsigned char *) malloc (200);
  5039. if (re_comp_buf.buffer == NULL)
  5040. /* CVS: Yes, we're discarding `const' here if !HAVE_LIBINTL. */
  5041. return (char *) gettext (re_error_msgid[(int) REG_ESPACE]);
  5042. re_comp_buf.allocated = 200;
  5043. re_comp_buf.fastmap = (char *) malloc (1 << BYTEWIDTH);
  5044. if (re_comp_buf.fastmap == NULL)
  5045. /* CVS: Yes, we're discarding `const' here if !HAVE_LIBINTL. */
  5046. return (char *) gettext (re_error_msgid[(int) REG_ESPACE]);
  5047. }
  5048. /* Since `re_exec' always passes NULL for the `regs' argument, we
  5049. don't need to initialize the pattern buffer fields which affect it. */
  5050. /* Match anchors at newlines. */
  5051. re_comp_buf.newline_anchor = 1;
  5052. ret = regex_compile (s, strlen (s), re_syntax_options, &re_comp_buf);
  5053. if (!ret)
  5054. return NULL;
  5055. /* Yes, we're discarding `const' here if !HAVE_LIBINTL. */
  5056. return (char *) gettext (re_error_msgid[(int) ret]);
  5057. }
  5058. int
  5059. #ifdef _LIBC
  5060. weak_function
  5061. #endif
  5062. re_exec (s)
  5063. const char *s;
  5064. {
  5065. const int len = strlen (s);
  5066. return
  5067. 0 <= re_search (&re_comp_buf, s, len, 0, len, (struct re_registers *) 0);
  5068. }
  5069. #endif /* _REGEX_RE_COMP */
  5070. /* POSIX.2 functions. Don't define these for Emacs. */
  5071. #ifndef emacs
  5072. /* regcomp takes a regular expression as a string and compiles it.
  5073. PREG is a regex_t *. We do not expect any fields to be initialized,
  5074. since POSIX says we shouldn't. Thus, we set
  5075. `buffer' to the compiled pattern;
  5076. `used' to the length of the compiled pattern;
  5077. `syntax' to RE_SYNTAX_POSIX_EXTENDED if the
  5078. REG_EXTENDED bit in CFLAGS is set; otherwise, to
  5079. RE_SYNTAX_POSIX_BASIC;
  5080. `newline_anchor' to REG_NEWLINE being set in CFLAGS;
  5081. `fastmap' and `fastmap_accurate' to zero;
  5082. `re_nsub' to the number of subexpressions in PATTERN.
  5083. PATTERN is the address of the pattern string.
  5084. CFLAGS is a series of bits which affect compilation.
  5085. If REG_EXTENDED is set, we use POSIX extended syntax; otherwise, we
  5086. use POSIX basic syntax.
  5087. If REG_NEWLINE is set, then . and [^...] don't match newline.
  5088. Also, regexec will try a match beginning after every newline.
  5089. If REG_ICASE is set, then we considers upper- and lowercase
  5090. versions of letters to be equivalent when matching.
  5091. If REG_NOSUB is set, then when PREG is passed to regexec, that
  5092. routine will report only success or failure, and nothing about the
  5093. registers.
  5094. It returns 0 if it succeeds, nonzero if it doesn't. (See regex.h for
  5095. the return codes and their meanings.) */
  5096. int
  5097. regcomp (preg, pattern, cflags)
  5098. regex_t *preg;
  5099. const char *pattern;
  5100. int cflags;
  5101. {
  5102. reg_errcode_t ret;
  5103. unsigned syntax
  5104. = (cflags & REG_EXTENDED) ?
  5105. RE_SYNTAX_POSIX_EXTENDED : RE_SYNTAX_POSIX_BASIC;
  5106. /* regex_compile will allocate the space for the compiled pattern. */
  5107. preg->buffer = 0;
  5108. preg->allocated = 0;
  5109. preg->used = 0;
  5110. /* Don't bother to use a fastmap when searching. This simplifies the
  5111. REG_NEWLINE case: if we used a fastmap, we'd have to put all the
  5112. characters after newlines into the fastmap. This way, we just try
  5113. every character. */
  5114. preg->fastmap = 0;
  5115. if (cflags & REG_ICASE)
  5116. {
  5117. unsigned i;
  5118. preg->translate
  5119. = (RE_TRANSLATE_TYPE) malloc (CHAR_SET_SIZE
  5120. * sizeof (*(RE_TRANSLATE_TYPE)0));
  5121. if (preg->translate == NULL)
  5122. return (int) REG_ESPACE;
  5123. /* Map uppercase characters to corresponding lowercase ones. */
  5124. for (i = 0; i < CHAR_SET_SIZE; i++)
  5125. preg->translate[i] = ISUPPER (i) ? tolower (i) : i;
  5126. }
  5127. else
  5128. preg->translate = NULL;
  5129. /* If REG_NEWLINE is set, newlines are treated differently. */
  5130. if (cflags & REG_NEWLINE)
  5131. { /* REG_NEWLINE implies neither . nor [^...] match newline. */
  5132. syntax &= ~RE_DOT_NEWLINE;
  5133. syntax |= RE_HAT_LISTS_NOT_NEWLINE;
  5134. /* It also changes the matching behavior. */
  5135. preg->newline_anchor = 1;
  5136. }
  5137. else
  5138. preg->newline_anchor = 0;
  5139. preg->no_sub = !!(cflags & REG_NOSUB);
  5140. /* POSIX says a null character in the pattern terminates it, so we
  5141. can use strlen here in compiling the pattern. */
  5142. ret = regex_compile (pattern, strlen (pattern), syntax, preg);
  5143. /* POSIX doesn't distinguish between an unmatched open-group and an
  5144. unmatched close-group: both are REG_EPAREN. */
  5145. if (ret == REG_ERPAREN) ret = REG_EPAREN;
  5146. return (int) ret;
  5147. }
  5148. /* regexec searches for a given pattern, specified by PREG, in the
  5149. string STRING.
  5150. If NMATCH is zero or REG_NOSUB was set in the cflags argument to
  5151. `regcomp', we ignore PMATCH. Otherwise, we assume PMATCH has at
  5152. least NMATCH elements, and we set them to the offsets of the
  5153. corresponding matched substrings.
  5154. EFLAGS specifies `execution flags' which affect matching: if
  5155. REG_NOTBOL is set, then ^ does not match at the beginning of the
  5156. string; if REG_NOTEOL is set, then $ does not match at the end.
  5157. We return 0 if we find a match and REG_NOMATCH if not. */
  5158. int
  5159. regexec (preg, string, nmatch, pmatch, eflags)
  5160. const regex_t *preg;
  5161. const char *string;
  5162. size_t nmatch;
  5163. regmatch_t pmatch[];
  5164. int eflags;
  5165. {
  5166. int ret;
  5167. struct re_registers regs;
  5168. regex_t private_preg;
  5169. int len = strlen (string);
  5170. boolean want_reg_info = !preg->no_sub && nmatch > 0;
  5171. private_preg = *preg;
  5172. private_preg.not_bol = !!(eflags & REG_NOTBOL);
  5173. private_preg.not_eol = !!(eflags & REG_NOTEOL);
  5174. /* The user has told us exactly how many registers to return
  5175. information about, via `nmatch'. We have to pass that on to the
  5176. matching routines. */
  5177. private_preg.regs_allocated = REGS_FIXED;
  5178. if (want_reg_info)
  5179. {
  5180. regs.num_regs = nmatch;
  5181. regs.start = TALLOC (nmatch, regoff_t);
  5182. regs.end = TALLOC (nmatch, regoff_t);
  5183. if (regs.start == NULL || regs.end == NULL)
  5184. return (int) REG_NOMATCH;
  5185. }
  5186. /* Perform the searching operation. */
  5187. ret = re_search (&private_preg, string, len,
  5188. /* start: */ 0, /* range: */ len,
  5189. want_reg_info ? &regs : (struct re_registers *) 0);
  5190. /* Copy the register information to the POSIX structure. */
  5191. if (want_reg_info)
  5192. {
  5193. if (ret >= 0)
  5194. {
  5195. unsigned r;
  5196. for (r = 0; r < nmatch; r++)
  5197. {
  5198. pmatch[r].rm_so = regs.start[r];
  5199. pmatch[r].rm_eo = regs.end[r];
  5200. }
  5201. }
  5202. /* If we needed the temporary register info, free the space now. */
  5203. free (regs.start);
  5204. free (regs.end);
  5205. }
  5206. /* We want zero return to mean success, unlike `re_search'. */
  5207. return ret >= 0 ? (int) REG_NOERROR : (int) REG_NOMATCH;
  5208. }
  5209. /* Returns a message corresponding to an error code, ERRCODE, returned
  5210. from either regcomp or regexec. We don't use PREG here. */
  5211. size_t
  5212. regerror (errcode, preg, errbuf, errbuf_size)
  5213. int errcode;
  5214. const regex_t *preg;
  5215. char *errbuf;
  5216. size_t errbuf_size;
  5217. {
  5218. const char *msg;
  5219. size_t msg_size;
  5220. if (errcode < 0
  5221. || errcode >= (sizeof (re_error_msgid) / sizeof (re_error_msgid[0])))
  5222. /* Only error codes returned by the rest of the code should be passed
  5223. to this routine. If we are given anything else, or if other regex
  5224. code generates an invalid error code, then the program has a bug.
  5225. Dump core so we can fix it. */
  5226. abort ();
  5227. msg = gettext (re_error_msgid[errcode]);
  5228. msg_size = strlen (msg) + 1; /* Includes the null. */
  5229. if (errbuf_size != 0)
  5230. {
  5231. if (msg_size > errbuf_size)
  5232. {
  5233. strncpy (errbuf, msg, errbuf_size - 1);
  5234. errbuf[errbuf_size - 1] = 0;
  5235. }
  5236. else
  5237. strcpy (errbuf, msg);
  5238. }
  5239. return msg_size;
  5240. }
  5241. /* Free dynamically allocated space used by PREG. */
  5242. void
  5243. regfree (preg)
  5244. regex_t *preg;
  5245. {
  5246. if (preg->buffer != NULL)
  5247. free (preg->buffer);
  5248. preg->buffer = NULL;
  5249. preg->allocated = 0;
  5250. preg->used = 0;
  5251. if (preg->fastmap != NULL)
  5252. free (preg->fastmap);
  5253. preg->fastmap = NULL;
  5254. preg->fastmap_accurate = 0;
  5255. if (preg->translate != NULL)
  5256. free (preg->translate);
  5257. preg->translate = NULL;
  5258. }
  5259. #endif /* not emacs */