PageRenderTime 71ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/core/src/syslib/regex.c

#
C | 2038 lines | 1232 code | 391 blank | 415 comment | 347 complexity | adf793193ef3bc5c942733795323e099 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-3.0

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

  1. /* AIX requires this to be the first thing in the file. */
  2. #if defined(_MSC_VER)
  3. #pragma warning(disable:4090)
  4. #pragma warning(disable:4022)
  5. #pragma warning(disable:4018)
  6. #pragma warning(disable:4101)
  7. #define STDC_HEADERS 1
  8. #define REGEX_MALLOC
  9. #endif
  10. #if defined (_AIX) && !defined (REGEX_MALLOC)
  11. #pragma alloca
  12. #endif
  13. #define _GNU_SOURCE
  14. /* We need this for `regex.h', and perhaps for the Emacs include files. */
  15. #include <sys/types.h>
  16. #ifdef HAVE_CONFIG_H
  17. #include "config.h"
  18. #endif
  19. /* The `emacs' switch turns on certain matching commands
  20. that make sense only in Emacs. */
  21. #ifdef emacs
  22. #include "lisp.h"
  23. #include "buffer.h"
  24. #include "syntax.h"
  25. /* Emacs uses `NULL' as a predicate. */
  26. #undef NULL
  27. #else /* not emacs */
  28. /* We used to test for `BSTRING' here, but only GCC and Emacs define
  29. `BSTRING', as far as I know, and neither of them use this code. */
  30. #if HAVE_STRING_H || STDC_HEADERS
  31. #include <string.h>
  32. #ifndef bcmp
  33. #define bcmp(s1, s2, n) memcmp ((s1), (s2), (n))
  34. #endif
  35. #ifndef bcopy
  36. #define bcopy(s, d, n) memcpy ((d), (s), (n))
  37. #endif
  38. #ifndef bzero
  39. #define bzero(s, n) memset ((s), 0, (n))
  40. #endif
  41. #else
  42. #include <strings.h>
  43. #endif
  44. #include <stdlib.h>
  45. /* Define the syntax stuff for \<, \>, etc. */
  46. /* This must be nonzero for the wordchar and notwordchar pattern
  47. commands in re_match_2. */
  48. #ifndef Sword
  49. #define Sword 1
  50. #endif
  51. #ifdef SYNTAX_TABLE
  52. extern char *re_syntax_table;
  53. #else /* not SYNTAX_TABLE */
  54. /* How many characters in the character set. */
  55. #define CHAR_SET_SIZE 256
  56. static char re_syntax_table[CHAR_SET_SIZE];
  57. static void
  58. init_syntax_once ()
  59. {
  60. register int c;
  61. static int done = 0;
  62. if (done)
  63. return;
  64. bzero (re_syntax_table, sizeof re_syntax_table);
  65. for (c = 'a'; c <= 'z'; c++)
  66. re_syntax_table[c] = Sword;
  67. for (c = 'A'; c <= 'Z'; c++)
  68. re_syntax_table[c] = Sword;
  69. for (c = '0'; c <= '9'; c++)
  70. re_syntax_table[c] = Sword;
  71. re_syntax_table['_'] = Sword;
  72. done = 1;
  73. }
  74. #endif /* not SYNTAX_TABLE */
  75. #define SYNTAX(c) re_syntax_table[c]
  76. #endif /* not emacs */
  77. /* Get the interface, including the syntax bits. */
  78. #include "regex.h"
  79. /* isalpha etc. are used for the character classes. */
  80. #include <ctype.h>
  81. #ifndef isascii
  82. #define isascii(c) 1
  83. #endif
  84. #ifdef isblank
  85. #define ISBLANK(c) (isascii (c) && isblank (c))
  86. #else
  87. #define ISBLANK(c) ((c) == ' ' || (c) == '\t')
  88. #endif
  89. #ifdef isgraph
  90. #define ISGRAPH(c) (isascii (c) && isgraph (c))
  91. #else
  92. #define ISGRAPH(c) (isascii (c) && isprint (c) && !isspace (c))
  93. #endif
  94. #define ISPRINT(c) (isascii (c) && isprint (c))
  95. #define ISDIGIT(c) (isascii (c) && isdigit (c))
  96. #define ISALNUM(c) (isascii (c) && isalnum (c))
  97. #define ISALPHA(c) (isascii (c) && isalpha (c))
  98. #define ISCNTRL(c) (isascii (c) && iscntrl (c))
  99. #define ISLOWER(c) (isascii (c) && islower (c))
  100. #define ISPUNCT(c) (isascii (c) && ispunct (c))
  101. #define ISSPACE(c) (isascii (c) && isspace (c))
  102. #define ISUPPER(c) (isascii (c) && isupper (c))
  103. #define ISXDIGIT(c) (isascii (c) && isxdigit (c))
  104. #ifndef NULL
  105. #define NULL 0
  106. #endif
  107. /* We remove any previous definition of `SIGN_EXTEND_CHAR',
  108. since ours (we hope) works properly with all combinations of
  109. machines, compilers, `char' and `unsigned char' argument types.
  110. (Per Bothner suggested the basic approach.) */
  111. #undef SIGN_EXTEND_CHAR
  112. #if __STDC__
  113. #define SIGN_EXTEND_CHAR(c) ((signed char) (c))
  114. #else /* not __STDC__ */
  115. /* As in Harbison and Steele. */
  116. #define SIGN_EXTEND_CHAR(c) ((((unsigned char) (c)) ^ 128) - 128)
  117. #endif
  118. /* Should we use malloc or alloca? If REGEX_MALLOC is not defined, we
  119. use `alloca' instead of `malloc'. This is because using malloc in
  120. re_search* or re_match* could cause memory leaks when C-g is used in
  121. Emacs; also, malloc is slower and causes storage fragmentation. On
  122. the other hand, malloc is more portable, and easier to debug.
  123. Because we sometimes use alloca, some routines have to be macros,
  124. not functions -- `alloca'-allocated space disappears at the end of the
  125. function it is called in. */
  126. #ifdef REGEX_MALLOC
  127. #define REGEX_ALLOCATE malloc
  128. #define REGEX_REALLOCATE(source, osize, nsize) realloc (source, nsize)
  129. #else /* not REGEX_MALLOC */
  130. /* Emacs already defines alloca, sometimes. */
  131. #ifndef alloca
  132. /* Make alloca work the best possible way. */
  133. #ifdef __GNUC__
  134. #define alloca __builtin_alloca
  135. #else /* not __GNUC__ */
  136. #if HAVE_ALLOCA_H
  137. #include <alloca.h>
  138. #else /* not __GNUC__ or HAVE_ALLOCA_H */
  139. #ifndef _AIX /* Already did AIX, up at the top. */
  140. char *alloca ();
  141. #endif /* not _AIX */
  142. #endif /* not HAVE_ALLOCA_H */
  143. #endif /* not __GNUC__ */
  144. #endif /* not alloca */
  145. #define REGEX_ALLOCATE malloc
  146. /* Assumes a `char *destination' variable. */
  147. #define REGEX_REALLOCATE(source, osize, nsize) \
  148. (destination = (char *) alloca (nsize), \
  149. bcopy (source, destination, osize), \
  150. destination)
  151. #endif /* not REGEX_MALLOC */
  152. /* True if `size1' is non-NULL and PTR is pointing anywhere inside
  153. `string1' or just past its end. This works if PTR is NULL, which is
  154. a good thing. */
  155. #define FIRST_STRING_P(ptr) \
  156. (size1 && string1 <= (ptr) && (ptr) <= string1 + size1)
  157. /* (Re)Allocate N items of type T using malloc, or fail. */
  158. #define TALLOC(n, t) ((t *) malloc ((n) * sizeof (t)))
  159. #define RETALLOC(addr, n, t) ((addr) = (t *) realloc (addr, (n) * sizeof (t)))
  160. #define REGEX_TALLOC(n, t) ((t *) REGEX_ALLOCATE ((n) * sizeof (t)))
  161. #define BYTEWIDTH 8 /* In bits. */
  162. #define STREQ(s1, s2) ((strcmp (s1, s2) == 0))
  163. #define MAX(a, b) ((a) > (b) ? (a) : (b))
  164. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  165. typedef char boolean;
  166. #define false 0
  167. #define true 1
  168. /* These are the command codes that appear in compiled regular
  169. expressions. Some opcodes are followed by argument bytes. A
  170. command code can specify any interpretation whatsoever for its
  171. arguments. Zero bytes may appear in the compiled regular expression.
  172. The value of `exactn' is needed in search.c (search_buffer) in Emacs.
  173. So regex.h defines a symbol `RE_EXACTN_VALUE' to be 1; the value of
  174. `exactn' we use here must also be 1. */
  175. typedef enum
  176. {
  177. no_op = 0,
  178. /* Followed by one byte giving n, then by n literal bytes. */
  179. exactn = 1,
  180. /* Matches any (more or less) character. */
  181. anychar,
  182. /* Matches any one char belonging to specified set. First
  183. following byte is number of bitmap bytes. Then come bytes
  184. for a bitmap saying which chars are in. Bits in each byte
  185. are ordered low-bit-first. A character is in the set if its
  186. bit is 1. A character too large to have a bit in the map is
  187. automatically not in the set. */
  188. charset,
  189. /* Same parameters as charset, but match any character that is
  190. not one of those specified. */
  191. charset_not,
  192. /* Start remembering the text that is matched, for storing in a
  193. register. Followed by one byte with the register number, in
  194. the range 0 to one less than the pattern buffer's re_nsub
  195. field. Then followed by one byte with the number of groups
  196. inner to this one. (This last has to be part of the
  197. start_memory only because we need it in the on_failure_jump
  198. of re_match_2.) */
  199. start_memory,
  200. /* Stop remembering the text that is matched and store it in a
  201. memory register. Followed by one byte with the register
  202. number, in the range 0 to one less than `re_nsub' in the
  203. pattern buffer, and one byte with the number of inner groups,
  204. just like `start_memory'. (We need the number of inner
  205. groups here because we don't have any easy way of finding the
  206. corresponding start_memory when we're at a stop_memory.) */
  207. stop_memory,
  208. /* Match a duplicate of something remembered. Followed by one
  209. byte containing the register number. */
  210. duplicate,
  211. /* Fail unless at beginning of line. */
  212. begline,
  213. /* Fail unless at end of line. */
  214. endline,
  215. /* Succeeds if at beginning of buffer (if emacs) or at beginning
  216. of string to be matched (if not). */
  217. begbuf,
  218. /* Analogously, for end of buffer/string. */
  219. endbuf,
  220. /* Followed by two byte relative address to which to jump. */
  221. jump,
  222. /* Same as jump, but marks the end of an alternative. */
  223. jump_past_alt,
  224. /* Followed by two-byte relative address of place to resume at
  225. in case of failure. */
  226. on_failure_jump,
  227. /* Like on_failure_jump, but pushes a placeholder instead of the
  228. current string position when executed. */
  229. on_failure_keep_string_jump,
  230. /* Throw away latest failure point and then jump to following
  231. two-byte relative address. */
  232. pop_failure_jump,
  233. /* Change to pop_failure_jump if know won't have to backtrack to
  234. match; otherwise change to jump. This is used to jump
  235. back to the beginning of a repeat. If what follows this jump
  236. clearly won't match what the repeat does, such that we can be
  237. sure that there is no use backtracking out of repetitions
  238. already matched, then we change it to a pop_failure_jump.
  239. Followed by two-byte address. */
  240. maybe_pop_jump,
  241. /* Jump to following two-byte address, and push a dummy failure
  242. point. This failure point will be thrown away if an attempt
  243. is made to use it for a failure. A `+' construct makes this
  244. before the first repeat. Also used as an intermediary kind
  245. of jump when compiling an alternative. */
  246. dummy_failure_jump,
  247. /* Push a dummy failure point and continue. Used at the end of
  248. alternatives. */
  249. push_dummy_failure,
  250. /* Followed by two-byte relative address and two-byte number n.
  251. After matching N times, jump to the address upon failure. */
  252. succeed_n,
  253. /* Followed by two-byte relative address, and two-byte number n.
  254. Jump to the address N times, then fail. */
  255. jump_n,
  256. /* Set the following two-byte relative address to the
  257. subsequent two-byte number. The address *includes* the two
  258. bytes of number. */
  259. set_number_at,
  260. wordchar, /* Matches any word-constituent character. */
  261. notwordchar, /* Matches any char that is not a word-constituent. */
  262. wordbeg, /* Succeeds if at word beginning. */
  263. wordend, /* Succeeds if at word end. */
  264. wordbound, /* Succeeds if at a word boundary. */
  265. notwordbound /* Succeeds if not at a word boundary. */
  266. #ifdef emacs
  267. ,before_dot, /* Succeeds if before point. */
  268. at_dot, /* Succeeds if at point. */
  269. after_dot, /* Succeeds if after point. */
  270. /* Matches any character whose syntax is specified. Followed by
  271. a byte which contains a syntax code, e.g., Sword. */
  272. syntaxspec,
  273. /* Matches any character whose syntax is not that specified. */
  274. notsyntaxspec
  275. #endif /* emacs */
  276. } re_opcode_t;
  277. /* Common operations on the compiled pattern. */
  278. /* Store NUMBER in two contiguous bytes starting at DESTINATION. */
  279. #define STORE_NUMBER(destination, number) \
  280. do { \
  281. (destination)[0] = (number) & 0377; \
  282. (destination)[1] = (number) >> 8; \
  283. } while (0)
  284. /* Same as STORE_NUMBER, except increment DESTINATION to
  285. the byte after where the number is stored. Therefore, DESTINATION
  286. must be an lvalue. */
  287. #define STORE_NUMBER_AND_INCR(destination, number) \
  288. do { \
  289. STORE_NUMBER (destination, number); \
  290. (destination) += 2; \
  291. } while (0)
  292. /* Put into DESTINATION a number stored in two contiguous bytes starting
  293. at SOURCE. */
  294. #define EXTRACT_NUMBER(destination, source) \
  295. do { \
  296. (destination) = *(source) & 0377; \
  297. (destination) += SIGN_EXTEND_CHAR (*((source) + 1)) << 8; \
  298. } while (0)
  299. #ifdef DEBUG
  300. static void
  301. extract_number (dest, source)
  302. int *dest;
  303. unsigned char *source;
  304. {
  305. int temp = SIGN_EXTEND_CHAR (*(source + 1));
  306. *dest = *source & 0377;
  307. *dest += temp << 8;
  308. }
  309. #ifndef EXTRACT_MACROS /* To debug the macros. */
  310. #undef EXTRACT_NUMBER
  311. #define EXTRACT_NUMBER(dest, src) extract_number (&dest, src)
  312. #endif /* not EXTRACT_MACROS */
  313. #endif /* DEBUG */
  314. /* Same as EXTRACT_NUMBER, except increment SOURCE to after the number.
  315. SOURCE must be an lvalue. */
  316. #define EXTRACT_NUMBER_AND_INCR(destination, source) \
  317. do { \
  318. EXTRACT_NUMBER (destination, source); \
  319. (source) += 2; \
  320. } while (0)
  321. #ifdef DEBUG
  322. static void
  323. extract_number_and_incr (destination, source)
  324. int *destination;
  325. unsigned char **source;
  326. {
  327. extract_number (destination, *source);
  328. *source += 2;
  329. }
  330. #ifndef EXTRACT_MACROS
  331. #undef EXTRACT_NUMBER_AND_INCR
  332. #define EXTRACT_NUMBER_AND_INCR(dest, src) \
  333. extract_number_and_incr (&dest, &src)
  334. #endif /* not EXTRACT_MACROS */
  335. #endif /* DEBUG */
  336. /* If DEBUG is defined, Regex prints many voluminous messages about what
  337. it is doing (if the variable `debug' is nonzero). If linked with the
  338. main program in `iregex.c', you can enter patterns and strings
  339. interactively. And if linked with the main program in `main.c' and
  340. the other test files, you can run the already-written tests. */
  341. #ifdef DEBUG
  342. /* We use standard I/O for debugging. */
  343. #include <stdio.h>
  344. /* It is useful to test things that ``must'' be true when debugging. */
  345. #include <assert.h>
  346. static int debug = 0;
  347. #define DEBUG_STATEMENT(e) e
  348. #define DEBUG_PRINT1(x) if (debug) printf (x)
  349. #define DEBUG_PRINT2(x1, x2) if (debug) printf (x1, x2)
  350. #define DEBUG_PRINT3(x1, x2, x3) if (debug) printf (x1, x2, x3)
  351. #define DEBUG_PRINT4(x1, x2, x3, x4) if (debug) printf (x1, x2, x3, x4)
  352. #define DEBUG_PRINT_COMPILED_PATTERN(p, s, e) \
  353. if (debug) print_partial_compiled_pattern (s, e)
  354. #define DEBUG_PRINT_DOUBLE_STRING(w, s1, sz1, s2, sz2) \
  355. if (debug) print_double_string (w, s1, sz1, s2, sz2)
  356. extern void printchar ();
  357. /* Print the fastmap in human-readable form. */
  358. void
  359. print_fastmap (fastmap)
  360. char *fastmap;
  361. {
  362. unsigned was_a_range = 0;
  363. unsigned i = 0;
  364. while (i < (1 << BYTEWIDTH))
  365. {
  366. if (fastmap[i++])
  367. {
  368. was_a_range = 0;
  369. printchar (i - 1);
  370. while (i < (1 << BYTEWIDTH) && fastmap[i])
  371. {
  372. was_a_range = 1;
  373. i++;
  374. }
  375. if (was_a_range)
  376. {
  377. printf ("-");
  378. printchar (i - 1);
  379. }
  380. }
  381. }
  382. putchar ('\n');
  383. }
  384. /* Print a compiled pattern string in human-readable form, starting at
  385. the START pointer into it and ending just before the pointer END. */
  386. void
  387. print_partial_compiled_pattern (start, end)
  388. unsigned char *start;
  389. unsigned char *end;
  390. {
  391. int mcnt, mcnt2;
  392. unsigned char *p = start;
  393. unsigned char *pend = end;
  394. if (start == NULL)
  395. {
  396. printf ("(null)\n");
  397. return;
  398. }
  399. /* Loop over pattern commands. */
  400. while (p < pend)
  401. {
  402. switch ((re_opcode_t) *p++)
  403. {
  404. case no_op:
  405. printf ("/no_op");
  406. break;
  407. case exactn:
  408. mcnt = *p++;
  409. printf ("/exactn/%d", mcnt);
  410. do
  411. {
  412. putchar ('/');
  413. printchar (*p++);
  414. }
  415. while (--mcnt);
  416. break;
  417. case start_memory:
  418. mcnt = *p++;
  419. printf ("/start_memory/%d/%d", mcnt, *p++);
  420. break;
  421. case stop_memory:
  422. mcnt = *p++;
  423. printf ("/stop_memory/%d/%d", mcnt, *p++);
  424. break;
  425. case duplicate:
  426. printf ("/duplicate/%d", *p++);
  427. break;
  428. case anychar:
  429. printf ("/anychar");
  430. break;
  431. case charset:
  432. case charset_not:
  433. {
  434. register int c;
  435. printf ("/charset%s",
  436. (re_opcode_t) *(p - 1) == charset_not ? "_not" : "");
  437. assert (p + *p < pend);
  438. for (c = 0; c < *p; c++)
  439. {
  440. unsigned bit;
  441. unsigned char map_byte = p[1 + c];
  442. putchar ('/');
  443. for (bit = 0; bit < BYTEWIDTH; bit++)
  444. if (map_byte & (1 << bit))
  445. printchar (c * BYTEWIDTH + bit);
  446. }
  447. p += 1 + *p;
  448. break;
  449. }
  450. case begline:
  451. printf ("/begline");
  452. break;
  453. case endline:
  454. printf ("/endline");
  455. break;
  456. case on_failure_jump:
  457. extract_number_and_incr (&mcnt, &p);
  458. printf ("/on_failure_jump/0/%d", mcnt);
  459. break;
  460. case on_failure_keep_string_jump:
  461. extract_number_and_incr (&mcnt, &p);
  462. printf ("/on_failure_keep_string_jump/0/%d", mcnt);
  463. break;
  464. case dummy_failure_jump:
  465. extract_number_and_incr (&mcnt, &p);
  466. printf ("/dummy_failure_jump/0/%d", mcnt);
  467. break;
  468. case push_dummy_failure:
  469. printf ("/push_dummy_failure");
  470. break;
  471. case maybe_pop_jump:
  472. extract_number_and_incr (&mcnt, &p);
  473. printf ("/maybe_pop_jump/0/%d", mcnt);
  474. break;
  475. case pop_failure_jump:
  476. extract_number_and_incr (&mcnt, &p);
  477. printf ("/pop_failure_jump/0/%d", mcnt);
  478. break;
  479. case jump_past_alt:
  480. extract_number_and_incr (&mcnt, &p);
  481. printf ("/jump_past_alt/0/%d", mcnt);
  482. break;
  483. case jump:
  484. extract_number_and_incr (&mcnt, &p);
  485. printf ("/jump/0/%d", mcnt);
  486. break;
  487. case succeed_n:
  488. extract_number_and_incr (&mcnt, &p);
  489. extract_number_and_incr (&mcnt2, &p);
  490. printf ("/succeed_n/0/%d/0/%d", mcnt, mcnt2);
  491. break;
  492. case jump_n:
  493. extract_number_and_incr (&mcnt, &p);
  494. extract_number_and_incr (&mcnt2, &p);
  495. printf ("/jump_n/0/%d/0/%d", mcnt, mcnt2);
  496. break;
  497. case set_number_at:
  498. extract_number_and_incr (&mcnt, &p);
  499. extract_number_and_incr (&mcnt2, &p);
  500. printf ("/set_number_at/0/%d/0/%d", mcnt, mcnt2);
  501. break;
  502. case wordbound:
  503. printf ("/wordbound");
  504. break;
  505. case notwordbound:
  506. printf ("/notwordbound");
  507. break;
  508. case wordbeg:
  509. printf ("/wordbeg");
  510. break;
  511. case wordend:
  512. printf ("/wordend");
  513. #ifdef emacs
  514. case before_dot:
  515. printf ("/before_dot");
  516. break;
  517. case at_dot:
  518. printf ("/at_dot");
  519. break;
  520. case after_dot:
  521. printf ("/after_dot");
  522. break;
  523. case syntaxspec:
  524. printf ("/syntaxspec");
  525. mcnt = *p++;
  526. printf ("/%d", mcnt);
  527. break;
  528. case notsyntaxspec:
  529. printf ("/notsyntaxspec");
  530. mcnt = *p++;
  531. printf ("/%d", mcnt);
  532. break;
  533. #endif /* emacs */
  534. case wordchar:
  535. printf ("/wordchar");
  536. break;
  537. case notwordchar:
  538. printf ("/notwordchar");
  539. break;
  540. case begbuf:
  541. printf ("/begbuf");
  542. break;
  543. case endbuf:
  544. printf ("/endbuf");
  545. break;
  546. default:
  547. printf ("?%d", *(p-1));
  548. }
  549. }
  550. printf ("/\n");
  551. }
  552. void
  553. print_compiled_pattern (bufp)
  554. struct re_pattern_buffer *bufp;
  555. {
  556. unsigned char *buffer = bufp->buffer;
  557. print_partial_compiled_pattern (buffer, buffer + bufp->used);
  558. printf ("%d bytes used/%d bytes allocated.\n", bufp->used, bufp->allocated);
  559. if (bufp->fastmap_accurate && bufp->fastmap)
  560. {
  561. printf ("fastmap: ");
  562. print_fastmap (bufp->fastmap);
  563. }
  564. printf ("re_nsub: %d\t", bufp->re_nsub);
  565. printf ("regs_alloc: %d\t", bufp->regs_allocated);
  566. printf ("can_be_null: %d\t", bufp->can_be_null);
  567. printf ("newline_anchor: %d\n", bufp->newline_anchor);
  568. printf ("no_sub: %d\t", bufp->no_sub);
  569. printf ("not_bol: %d\t", bufp->not_bol);
  570. printf ("not_eol: %d\t", bufp->not_eol);
  571. printf ("syntax: %d\n", bufp->syntax);
  572. /* Perhaps we should print the translate table? */
  573. }
  574. void
  575. print_double_string (where, string1, size1, string2, size2)
  576. const char *where;
  577. const char *string1;
  578. const char *string2;
  579. int size1;
  580. int size2;
  581. {
  582. unsigned this_char;
  583. if (where == NULL)
  584. printf ("(null)");
  585. else
  586. {
  587. if (FIRST_STRING_P (where))
  588. {
  589. for (this_char = where - string1; this_char < size1; this_char++)
  590. printchar (string1[this_char]);
  591. where = string2;
  592. }
  593. for (this_char = where - string2; this_char < size2; this_char++)
  594. printchar (string2[this_char]);
  595. }
  596. }
  597. #else /* not DEBUG */
  598. #undef assert
  599. #define assert(e)
  600. #define DEBUG_STATEMENT(e)
  601. #define DEBUG_PRINT1(x)
  602. #define DEBUG_PRINT2(x1, x2)
  603. #define DEBUG_PRINT3(x1, x2, x3)
  604. #define DEBUG_PRINT4(x1, x2, x3, x4)
  605. #define DEBUG_PRINT_COMPILED_PATTERN(p, s, e)
  606. #define DEBUG_PRINT_DOUBLE_STRING(w, s1, sz1, s2, sz2)
  607. #endif /* not DEBUG */
  608. /* Set by `re_set_syntax' to the current regexp syntax to recognize. Can
  609. also be assigned to arbitrarily: each pattern buffer stores its own
  610. syntax, so it can be changed between regex compilations. */
  611. reg_syntax_t re_syntax_options = RE_SYNTAX_EMACS;
  612. /* Specify the precise syntax of regexps for compilation. This provides
  613. for compatibility for various utilities which historically have
  614. different, incompatible syntaxes.
  615. The argument SYNTAX is a bit mask comprised of the various bits
  616. defined in regex.h. We return the old syntax. */
  617. reg_syntax_t
  618. re_set_syntax (syntax)
  619. reg_syntax_t syntax;
  620. {
  621. reg_syntax_t ret = re_syntax_options;
  622. re_syntax_options = syntax;
  623. return ret;
  624. }
  625. /* This table gives an error message for each of the error codes listed
  626. in regex.h. Obviously the order here has to be same as there. */
  627. static const char *re_error_msg[] =
  628. { NULL, /* REG_NOERROR */
  629. "No match", /* REG_NOMATCH */
  630. "Invalid regular expression", /* REG_BADPAT */
  631. "Invalid collation character", /* REG_ECOLLATE */
  632. "Invalid character class name", /* REG_ECTYPE */
  633. "Trailing backslash", /* REG_EESCAPE */
  634. "Invalid back reference", /* REG_ESUBREG */
  635. "Unmatched [ or [^", /* REG_EBRACK */
  636. "Unmatched ( or \\(", /* REG_EPAREN */
  637. "Unmatched \\{", /* REG_EBRACE */
  638. "Invalid content of \\{\\}", /* REG_BADBR */
  639. "Invalid range end", /* REG_ERANGE */
  640. "Memory exhausted", /* REG_ESPACE */
  641. "Invalid preceding regular expression", /* REG_BADRPT */
  642. "Premature end of regular expression", /* REG_EEND */
  643. "Regular expression too big", /* REG_ESIZE */
  644. "Unmatched ) or \\)", /* REG_ERPAREN */
  645. };
  646. /* Subroutine declarations and macros for regex_compile. */
  647. static void store_op1 (), store_op2 ();
  648. static void insert_op1 (), insert_op2 ();
  649. static boolean at_begline_loc_p (), at_endline_loc_p ();
  650. static boolean group_in_compile_stack ();
  651. static reg_errcode_t compile_range ();
  652. /* Fetch the next character in the uncompiled pattern---translating it
  653. if necessary. Also cast from a signed character in the constant
  654. string passed to us by the user to an unsigned char that we can use
  655. as an array index (in, e.g., `translate'). */
  656. #define PATFETCH(c) \
  657. do {if (p == pend) return REG_EEND; \
  658. c = (unsigned char) *p++; \
  659. if (translate) c = translate[c]; \
  660. } while (0)
  661. /* Fetch the next character in the uncompiled pattern, with no
  662. translation. */
  663. #define PATFETCH_RAW(c) \
  664. do {if (p == pend) return REG_EEND; \
  665. c = (unsigned char) *p++; \
  666. } while (0)
  667. /* Go backwards one character in the pattern. */
  668. #define PATUNFETCH p--
  669. /* If `translate' is non-null, return translate[D], else just D. We
  670. cast the subscript to translate because some data is declared as
  671. `char *', to avoid warnings when a string constant is passed. But
  672. when we use a character as a subscript we must make it unsigned. */
  673. #define TRANSLATE(d) (translate ? translate[(unsigned char) (d)] : (d))
  674. /* Macros for outputting the compiled pattern into `buffer'. */
  675. /* If the buffer isn't allocated when it comes in, use this. */
  676. #define INIT_BUF_SIZE 32
  677. /* Make sure we have at least N more bytes of space in buffer. */
  678. #define GET_BUFFER_SPACE(n) \
  679. while (b - bufp->buffer + (n) > bufp->allocated) \
  680. EXTEND_BUFFER ()
  681. /* Make sure we have one more byte of buffer space and then add C to it. */
  682. #define BUF_PUSH(c) \
  683. do { \
  684. GET_BUFFER_SPACE (1); \
  685. *b++ = (unsigned char) (c); \
  686. } while (0)
  687. /* Ensure we have two more bytes of buffer space and then append C1 and C2. */
  688. #define BUF_PUSH_2(c1, c2) \
  689. do { \
  690. GET_BUFFER_SPACE (2); \
  691. *b++ = (unsigned char) (c1); \
  692. *b++ = (unsigned char) (c2); \
  693. } while (0)
  694. /* As with BUF_PUSH_2, except for three bytes. */
  695. #define BUF_PUSH_3(c1, c2, c3) \
  696. do { \
  697. GET_BUFFER_SPACE (3); \
  698. *b++ = (unsigned char) (c1); \
  699. *b++ = (unsigned char) (c2); \
  700. *b++ = (unsigned char) (c3); \
  701. } while (0)
  702. /* Store a jump with opcode OP at LOC to location TO. We store a
  703. relative address offset by the three bytes the jump itself occupies. */
  704. #define STORE_JUMP(op, loc, to) \
  705. store_op1 (op, loc, (to) - (loc) - 3)
  706. /* Likewise, for a two-argument jump. */
  707. #define STORE_JUMP2(op, loc, to, arg) \
  708. store_op2 (op, loc, (to) - (loc) - 3, arg)
  709. /* Like `STORE_JUMP', but for inserting. Assume `b' is the buffer end. */
  710. #define INSERT_JUMP(op, loc, to) \
  711. insert_op1 (op, loc, (to) - (loc) - 3, b)
  712. /* Like `STORE_JUMP2', but for inserting. Assume `b' is the buffer end. */
  713. #define INSERT_JUMP2(op, loc, to, arg) \
  714. insert_op2 (op, loc, (to) - (loc) - 3, arg, b)
  715. /* This is not an arbitrary limit: the arguments which represent offsets
  716. into the pattern are two bytes long. So if 2^16 bytes turns out to
  717. be too small, many things would have to change. */
  718. #define MAX_BUF_SIZE (1L << 16)
  719. /* Extend the buffer by twice its current size via realloc and
  720. reset the pointers that pointed into the old block to point to the
  721. correct places in the new one. If extending the buffer results in it
  722. being larger than MAX_BUF_SIZE, then flag memory exhausted. */
  723. #define EXTEND_BUFFER() \
  724. do { \
  725. unsigned char *old_buffer = bufp->buffer; \
  726. if (bufp->allocated == MAX_BUF_SIZE) \
  727. return REG_ESIZE; \
  728. bufp->allocated <<= 1; \
  729. if (bufp->allocated > MAX_BUF_SIZE) \
  730. bufp->allocated = MAX_BUF_SIZE; \
  731. bufp->buffer = (unsigned char *) realloc (bufp->buffer, bufp->allocated);\
  732. if (bufp->buffer == NULL) \
  733. return REG_ESPACE; \
  734. /* If the buffer moved, move all the pointers into it. */ \
  735. if (old_buffer != bufp->buffer) \
  736. { \
  737. b = (b - old_buffer) + bufp->buffer; \
  738. begalt = (begalt - old_buffer) + bufp->buffer; \
  739. if (fixup_alt_jump) \
  740. fixup_alt_jump = (fixup_alt_jump - old_buffer) + bufp->buffer;\
  741. if (laststart) \
  742. laststart = (laststart - old_buffer) + bufp->buffer; \
  743. if (pending_exact) \
  744. pending_exact = (pending_exact - old_buffer) + bufp->buffer; \
  745. } \
  746. } while (0)
  747. /* Since we have one byte reserved for the register number argument to
  748. {start,stop}_memory, the maximum number of groups we can report
  749. things about is what fits in that byte. */
  750. #define MAX_REGNUM 255
  751. /* But patterns can have more than `MAX_REGNUM' registers. We just
  752. ignore the excess. */
  753. typedef unsigned regnum_t;
  754. /* Macros for the compile stack. */
  755. /* Since offsets can go either forwards or backwards, this type needs to
  756. be able to hold values from -(MAX_BUF_SIZE - 1) to MAX_BUF_SIZE - 1. */
  757. typedef int pattern_offset_t;
  758. typedef struct
  759. {
  760. pattern_offset_t begalt_offset;
  761. pattern_offset_t fixup_alt_jump;
  762. pattern_offset_t inner_group_offset;
  763. pattern_offset_t laststart_offset;
  764. regnum_t regnum;
  765. } compile_stack_elt_t;
  766. typedef struct
  767. {
  768. compile_stack_elt_t *stack;
  769. unsigned size;
  770. unsigned avail; /* Offset of next open position. */
  771. } compile_stack_type;
  772. #define INIT_COMPILE_STACK_SIZE 32
  773. #define COMPILE_STACK_EMPTY (compile_stack.avail == 0)
  774. #define COMPILE_STACK_FULL (compile_stack.avail == compile_stack.size)
  775. /* The next available element. */
  776. #define COMPILE_STACK_TOP (compile_stack.stack[compile_stack.avail])
  777. /* Set the bit for character C in a list. */
  778. #define SET_LIST_BIT(c) \
  779. (b[((unsigned char) (c)) / BYTEWIDTH] \
  780. |= 1 << (((unsigned char) c) % BYTEWIDTH))
  781. /* Get the next unsigned number in the uncompiled pattern. */
  782. #define GET_UNSIGNED_NUMBER(num) \
  783. { if (p != pend) \
  784. { \
  785. PATFETCH (c); \
  786. while (ISDIGIT (c)) \
  787. { \
  788. if (num < 0) \
  789. num = 0; \
  790. num = num * 10 + c - '0'; \
  791. if (p == pend) \
  792. break; \
  793. PATFETCH (c); \
  794. } \
  795. } \
  796. }
  797. #define CHAR_CLASS_MAX_LENGTH 6 /* Namely, `xdigit'. */
  798. #define IS_CHAR_CLASS(string) \
  799. (STREQ (string, "alpha") || STREQ (string, "upper") \
  800. || STREQ (string, "lower") || STREQ (string, "digit") \
  801. || STREQ (string, "alnum") || STREQ (string, "xdigit") \
  802. || STREQ (string, "space") || STREQ (string, "print") \
  803. || STREQ (string, "punct") || STREQ (string, "graph") \
  804. || STREQ (string, "cntrl") || STREQ (string, "blank"))
  805. /* `regex_compile' compiles PATTERN (of length SIZE) according to SYNTAX.
  806. Returns one of error codes defined in `regex.h', or zero for success.
  807. Assumes the `allocated' (and perhaps `buffer') and `translate'
  808. fields are set in BUFP on entry.
  809. If it succeeds, results are put in BUFP (if it returns an error, the
  810. contents of BUFP are undefined):
  811. `buffer' is the compiled pattern;
  812. `syntax' is set to SYNTAX;
  813. `used' is set to the length of the compiled pattern;
  814. `fastmap_accurate' is zero;
  815. `re_nsub' is the number of subexpressions in PATTERN;
  816. `not_bol' and `not_eol' are zero;
  817. The `fastmap' and `newline_anchor' fields are neither
  818. examined nor set. */
  819. static reg_errcode_t
  820. regex_compile (pattern, size, syntax, bufp)
  821. const char *pattern;
  822. int size;
  823. reg_syntax_t syntax;
  824. struct re_pattern_buffer *bufp;
  825. {
  826. /* We fetch characters from PATTERN here. Even though PATTERN is
  827. `char *' (i.e., signed), we declare these variables as unsigned, so
  828. they can be reliably used as array indices. */
  829. register unsigned char c, c1;
  830. /* A random tempory spot in PATTERN. */
  831. const char *p1;
  832. /* Points to the end of the buffer, where we should append. */
  833. register unsigned char *b;
  834. /* Keeps track of unclosed groups. */
  835. compile_stack_type compile_stack;
  836. /* Points to the current (ending) position in the pattern. */
  837. const char *p = pattern;
  838. const char *pend = pattern + size;
  839. /* How to translate the characters in the pattern. */
  840. char *translate = bufp->translate;
  841. /* Address of the count-byte of the most recently inserted `exactn'
  842. command. This makes it possible to tell if a new exact-match
  843. character can be added to that command or if the character requires
  844. a new `exactn' command. */
  845. unsigned char *pending_exact = 0;
  846. /* Address of start of the most recently finished expression.
  847. This tells, e.g., postfix * where to find the start of its
  848. operand. Reset at the beginning of groups and alternatives. */
  849. unsigned char *laststart = 0;
  850. /* Address of beginning of regexp, or inside of last group. */
  851. unsigned char *begalt;
  852. /* Place in the uncompiled pattern (i.e., the {) to
  853. which to go back if the interval is invalid. */
  854. const char *beg_interval;
  855. /* Address of the place where a forward jump should go to the end of
  856. the containing expression. Each alternative of an `or' -- except the
  857. last -- ends with a forward jump of this sort. */
  858. unsigned char *fixup_alt_jump = 0;
  859. /* Counts open-groups as they are encountered. Remembered for the
  860. matching close-group on the compile stack, so the same register
  861. number is put in the stop_memory as the start_memory. */
  862. regnum_t regnum = 0;
  863. #ifdef DEBUG
  864. DEBUG_PRINT1 ("\nCompiling pattern: ");
  865. if (debug)
  866. {
  867. unsigned debug_count;
  868. for (debug_count = 0; debug_count < size; debug_count++)
  869. printchar (pattern[debug_count]);
  870. putchar ('\n');
  871. }
  872. #endif /* DEBUG */
  873. /* Initialize the compile stack. */
  874. compile_stack.stack = TALLOC (INIT_COMPILE_STACK_SIZE, compile_stack_elt_t);
  875. if (compile_stack.stack == NULL)
  876. return REG_ESPACE;
  877. compile_stack.size = INIT_COMPILE_STACK_SIZE;
  878. compile_stack.avail = 0;
  879. /* Initialize the pattern buffer. */
  880. bufp->syntax = syntax;
  881. bufp->fastmap_accurate = 0;
  882. bufp->not_bol = bufp->not_eol = 0;
  883. /* Set `used' to zero, so that if we return an error, the pattern
  884. printer (for debugging) will think there's no pattern. We reset it
  885. at the end. */
  886. bufp->used = 0;
  887. /* Always count groups, whether or not bufp->no_sub is set. */
  888. bufp->re_nsub = 0;
  889. #if !defined (emacs) && !defined (SYNTAX_TABLE)
  890. /* Initialize the syntax table. */
  891. init_syntax_once ();
  892. #endif
  893. if (bufp->allocated == 0)
  894. {
  895. if (bufp->buffer)
  896. { /* If zero allocated, but buffer is non-null, try to realloc
  897. enough space. This loses if buffer's address is bogus, but
  898. that is the user's responsibility. */
  899. RETALLOC (bufp->buffer, INIT_BUF_SIZE, unsigned char);
  900. }
  901. else
  902. { /* Caller did not allocate a buffer. Do it for them. */
  903. bufp->buffer = TALLOC (INIT_BUF_SIZE, unsigned char);
  904. }
  905. if (!bufp->buffer) return REG_ESPACE;
  906. bufp->allocated = INIT_BUF_SIZE;
  907. }
  908. begalt = b = bufp->buffer;
  909. /* Loop through the uncompiled pattern until we're at the end. */
  910. while (p != pend)
  911. {
  912. PATFETCH (c);
  913. switch (c)
  914. {
  915. case '^':
  916. {
  917. if ( /* If at start of pattern, it's an operator. */
  918. p == pattern + 1
  919. /* If context independent, it's an operator. */
  920. || syntax & RE_CONTEXT_INDEP_ANCHORS
  921. /* Otherwise, depends on what's come before. */
  922. || at_begline_loc_p (pattern, p, syntax))
  923. BUF_PUSH (begline);
  924. else
  925. goto normal_char;
  926. }
  927. break;
  928. case '$':
  929. {
  930. if ( /* If at end of pattern, it's an operator. */
  931. p == pend
  932. /* If context independent, it's an operator. */
  933. || syntax & RE_CONTEXT_INDEP_ANCHORS
  934. /* Otherwise, depends on what's next. */
  935. || at_endline_loc_p (p, pend, syntax))
  936. BUF_PUSH (endline);
  937. else
  938. goto normal_char;
  939. }
  940. break;
  941. case '+':
  942. case '?':
  943. if ((syntax & RE_BK_PLUS_QM)
  944. || (syntax & RE_LIMITED_OPS))
  945. goto normal_char;
  946. handle_plus:
  947. case '*':
  948. /* If there is no previous pattern... */
  949. if (!laststart)
  950. {
  951. if (syntax & RE_CONTEXT_INVALID_OPS)
  952. return REG_BADRPT;
  953. else if (!(syntax & RE_CONTEXT_INDEP_OPS))
  954. goto normal_char;
  955. }
  956. {
  957. /* Are we optimizing this jump? */
  958. boolean keep_string_p = false;
  959. /* 1 means zero (many) matches is allowed. */
  960. char zero_times_ok = 0, many_times_ok = 0;
  961. /* If there is a sequence of repetition chars, collapse it
  962. down to just one (the right one). We can't combine
  963. interval operators with these because of, e.g., `a{2}*',
  964. which should only match an even number of `a's. */
  965. for (;;)
  966. {
  967. zero_times_ok |= c != '+';
  968. many_times_ok |= c != '?';
  969. if (p == pend)
  970. break;
  971. PATFETCH (c);
  972. if (c == '*'
  973. || (!(syntax & RE_BK_PLUS_QM) && (c == '+' || c == '?')))
  974. ;
  975. else if (syntax & RE_BK_PLUS_QM && c == '\\')
  976. {
  977. if (p == pend) return REG_EESCAPE;
  978. PATFETCH (c1);
  979. if (!(c1 == '+' || c1 == '?'))
  980. {
  981. PATUNFETCH;
  982. PATUNFETCH;
  983. break;
  984. }
  985. c = c1;
  986. }
  987. else
  988. {
  989. PATUNFETCH;
  990. break;
  991. }
  992. /* If we get here, we found another repeat character. */
  993. }
  994. /* Star, etc. applied to an empty pattern is equivalent
  995. to an empty pattern. */
  996. if (!laststart)
  997. break;
  998. /* Now we know whether or not zero matches is allowed
  999. and also whether or not two or more matches is allowed. */
  1000. if (many_times_ok)
  1001. { /* More than one repetition is allowed, so put in at the
  1002. end a backward relative jump from `b' to before the next
  1003. jump we're going to put in below (which jumps from
  1004. laststart to after this jump).
  1005. But if we are at the `*' in the exact sequence `.*\n',
  1006. insert an unconditional jump backwards to the .,
  1007. instead of the beginning of the loop. This way we only
  1008. push a failure point once, instead of every time
  1009. through the loop. */
  1010. assert (p - 1 > pattern);
  1011. /* Allocate the space for the jump. */
  1012. GET_BUFFER_SPACE (3);
  1013. /* We know we are not at the first character of the pattern,
  1014. because laststart was nonzero. And we've already
  1015. incremented `p', by the way, to be the character after
  1016. the `*'. Do we have to do something analogous here
  1017. for null bytes, because of RE_DOT_NOT_NULL? */
  1018. if (TRANSLATE (*(p - 2)) == TRANSLATE ('.')
  1019. && zero_times_ok
  1020. && p < pend && TRANSLATE (*p) == TRANSLATE ('\n')
  1021. && !(syntax & RE_DOT_NEWLINE))
  1022. { /* We have .*\n. */
  1023. STORE_JUMP (jump, b, laststart);
  1024. keep_string_p = true;
  1025. }
  1026. else
  1027. /* Anything else. */
  1028. STORE_JUMP (maybe_pop_jump, b, laststart - 3);
  1029. /* We've added more stuff to the buffer. */
  1030. b += 3;
  1031. }
  1032. /* On failure, jump from laststart to b + 3, which will be the
  1033. end of the buffer after this jump is inserted. */
  1034. GET_BUFFER_SPACE (3);
  1035. INSERT_JUMP (keep_string_p ? on_failure_keep_string_jump
  1036. : on_failure_jump,
  1037. laststart, b + 3);
  1038. pending_exact = 0;
  1039. b += 3;
  1040. if (!zero_times_ok)
  1041. {
  1042. /* At least one repetition is required, so insert a
  1043. `dummy_failure_jump' before the initial
  1044. `on_failure_jump' instruction of the loop. This
  1045. effects a skip over that instruction the first time
  1046. we hit that loop. */
  1047. GET_BUFFER_SPACE (3);
  1048. INSERT_JUMP (dummy_failure_jump, laststart, laststart + 6);
  1049. b += 3;
  1050. }
  1051. }
  1052. break;
  1053. case '.':
  1054. laststart = b;
  1055. BUF_PUSH (anychar);
  1056. break;
  1057. case '[':
  1058. {
  1059. boolean had_char_class = false;
  1060. if (p == pend) return REG_EBRACK;
  1061. /* Ensure that we have enough space to push a charset: the
  1062. opcode, the length count, and the bitset; 34 bytes in all. */
  1063. GET_BUFFER_SPACE (34);
  1064. laststart = b;
  1065. /* We test `*p == '^' twice, instead of using an if
  1066. statement, so we only need one BUF_PUSH. */
  1067. BUF_PUSH (*p == '^' ? charset_not : charset);
  1068. if (*p == '^')
  1069. p++;
  1070. /* Remember the first position in the bracket expression. */
  1071. p1 = p;
  1072. /* Push the number of bytes in the bitmap. */
  1073. BUF_PUSH ((1 << BYTEWIDTH) / BYTEWIDTH);
  1074. /* Clear the whole map. */
  1075. bzero (b, (1 << BYTEWIDTH) / BYTEWIDTH);
  1076. /* charset_not matches newline according to a syntax bit. */
  1077. if ((re_opcode_t) b[-2] == charset_not
  1078. && (syntax & RE_HAT_LISTS_NOT_NEWLINE))
  1079. SET_LIST_BIT ('\n');
  1080. /* Read in characters and ranges, setting map bits. */
  1081. for (;;)
  1082. {
  1083. if (p == pend) return REG_EBRACK;
  1084. PATFETCH (c);
  1085. /* \ might escape characters inside [...] and [^...]. */
  1086. if ((syntax & RE_BACKSLASH_ESCAPE_IN_LISTS) && c == '\\')
  1087. {
  1088. if (p == pend) return REG_EESCAPE;
  1089. PATFETCH (c1);
  1090. SET_LIST_BIT (c1);
  1091. continue;
  1092. }
  1093. /* Could be the end of the bracket expression. If it's
  1094. not (i.e., when the bracket expression is `[]' so
  1095. far), the ']' character bit gets set way below. */
  1096. if (c == ']' && p != p1 + 1)
  1097. break;
  1098. /* Look ahead to see if it's a range when the last thing
  1099. was a character class. */
  1100. if (had_char_class && c == '-' && *p != ']')
  1101. return REG_ERANGE;
  1102. /* Look ahead to see if it's a range when the last thing
  1103. was a character: if this is a hyphen not at the
  1104. beginning or the end of a list, then it's the range
  1105. operator. */
  1106. if (c == '-'
  1107. && !(p - 2 >= pattern && p[-2] == '[')
  1108. && !(p - 3 >= pattern && p[-3] == '[' && p[-2] == '^')
  1109. && *p != ']')
  1110. {
  1111. reg_errcode_t ret
  1112. = compile_range (&p, pend, translate, syntax, b);
  1113. if (ret != REG_NOERROR) return ret;
  1114. }
  1115. else if (p[0] == '-' && p[1] != ']')
  1116. { /* This handles ranges made up of characters only. */
  1117. reg_errcode_t ret;
  1118. /* Move past the `-'. */
  1119. PATFETCH (c1);
  1120. ret = compile_range (&p, pend, translate, syntax, b);
  1121. if (ret != REG_NOERROR) return ret;
  1122. }
  1123. /* See if we're at the beginning of a possible character
  1124. class. */
  1125. else if (syntax & RE_CHAR_CLASSES && c == '[' && *p == ':')
  1126. { /* Leave room for the null. */
  1127. char str[CHAR_CLASS_MAX_LENGTH + 1];
  1128. PATFETCH (c);
  1129. c1 = 0;
  1130. /* If pattern is `[[:'. */
  1131. if (p == pend) return REG_EBRACK;
  1132. for (;;)
  1133. {
  1134. PATFETCH (c);
  1135. if (c == ':' || c == ']' || p == pend
  1136. || c1 == CHAR_CLASS_MAX_LENGTH)
  1137. break;
  1138. str[c1++] = c;
  1139. }
  1140. str[c1] = '\0';
  1141. /* If isn't a word bracketed by `[:' and:`]':
  1142. undo the ending character, the letters, and leave
  1143. the leading `:' and `[' (but set bits for them). */
  1144. if (c == ':' && *p == ']')
  1145. {
  1146. int ch;
  1147. boolean is_alnum = STREQ (str, "alnum");
  1148. boolean is_alpha = STREQ (str, "alpha");
  1149. boolean is_blank = STREQ (str, "blank");
  1150. boolean is_cntrl = STREQ (str, "cntrl");
  1151. boolean is_digit = STREQ (str, "digit");
  1152. boolean is_graph = STREQ (str, "graph");
  1153. boolean is_lower = STREQ (str, "lower");
  1154. boolean is_print = STREQ (str, "print");
  1155. boolean is_punct = STREQ (str, "punct");
  1156. boolean is_space = STREQ (str, "space");
  1157. boolean is_upper = STREQ (str, "upper");
  1158. boolean is_xdigit = STREQ (str, "xdigit");
  1159. if (!IS_CHAR_CLASS (str)) return REG_ECTYPE;
  1160. /* Throw away the ] at the end of the character
  1161. class. */
  1162. PATFETCH (c);
  1163. if (p == pend) return REG_EBRACK;
  1164. for (ch = 0; ch < 1 << BYTEWIDTH; ch++)
  1165. {
  1166. if ( (is_alnum && ISALNUM (ch))
  1167. || (is_alpha && ISALPHA (ch))
  1168. || (is_blank && ISBLANK (ch))
  1169. || (is_cntrl && ISCNTRL (ch))
  1170. || (is_digit && ISDIGIT (ch))
  1171. || (is_graph && ISGRAPH (ch))
  1172. || (is_lower && ISLOWER (ch))
  1173. || (is_print && ISPRINT (ch))
  1174. || (is_punct && ISPUNCT (ch))
  1175. || (is_space && ISSPACE (ch))
  1176. || (is_upper && ISUPPER (ch))
  1177. || (is_xdigit && ISXDIGIT (ch)))
  1178. SET_LIST_BIT (ch);
  1179. }
  1180. had_char_class = true;
  1181. }
  1182. else
  1183. {
  1184. c1++;
  1185. while (c1--)
  1186. PATUNFETCH;
  1187. SET_LIST_BIT ('[');
  1188. SET_LIST_BIT (':');
  1189. had_char_class = false;
  1190. }
  1191. }
  1192. else
  1193. {
  1194. had_char_class = false;
  1195. SET_LIST_BIT (c);
  1196. }
  1197. }
  1198. /* Discard any (non)matching list bytes that are all 0 at the
  1199. end of the map. Decrease the map-length byte too. */
  1200. while ((int) b[-1] > 0 && b[b[-1] - 1] == 0)
  1201. b[-1]--;
  1202. b += b[-1];
  1203. }
  1204. break;
  1205. case '(':
  1206. if (syntax & RE_NO_BK_PARENS)
  1207. goto handle_open;
  1208. else
  1209. goto normal_char;
  1210. case ')':
  1211. if (syntax & RE_NO_BK_PARENS)
  1212. goto handle_close;
  1213. else
  1214. goto normal_char;
  1215. case '\n':
  1216. if (syntax & RE_NEWLINE_ALT)
  1217. goto handle_alt;
  1218. else
  1219. goto normal_char;
  1220. case '|':
  1221. if (syntax & RE_NO_BK_VBAR)
  1222. goto handle_alt;
  1223. else
  1224. goto normal_char;
  1225. case '{':
  1226. if (syntax & RE_INTERVALS && syntax & RE_NO_BK_BRACES)
  1227. goto handle_interval;
  1228. else
  1229. goto normal_char;
  1230. case '\\':
  1231. if (p == pend) return REG_EESCAPE;
  1232. /* Do not translate the character after the \, so that we can
  1233. distinguish, e.g., \B from \b, even if we normally would
  1234. translate, e.g., B to b. */
  1235. PATFETCH_RAW (c);
  1236. switch (c)
  1237. {
  1238. case '(':
  1239. if (syntax & RE_NO_BK_PARENS)
  1240. goto normal_backslash;
  1241. handle_open:
  1242. bufp->re_nsub++;
  1243. regnum++;
  1244. if (COMPILE_STACK_FULL)
  1245. {
  1246. RETALLOC (compile_stack.stack, compile_stack.size << 1,
  1247. compile_stack_elt_t);
  1248. if (compile_stack.stack == NULL) return REG_ESPACE;
  1249. compile_stack.size <<= 1;
  1250. }
  1251. /* These are the values to restore when we hit end of this
  1252. group. They are all relative offsets, so that if the
  1253. whole pattern moves because of realloc, they will still
  1254. be valid. */
  1255. COMPILE_STACK_TOP.begalt_offset = begalt - bufp->buffer;
  1256. COMPILE_STACK_TOP.fixup_alt_jump
  1257. = fixup_alt_jump ? fixup_alt_jump - bufp->buffer + 1 : 0;
  1258. COMPILE_STACK_TOP.laststart_offset = b - bufp->buffer;
  1259. COMPILE_STACK_TOP.regnum = regnum;
  1260. /* We will eventually replace the 0 with the number of
  1261. groups inner to this one. But do not push a
  1262. start_memory for groups beyond the last one we can
  1263. represent in the compiled pattern. */
  1264. if (regnum <= MAX_REGNUM)
  1265. {
  1266. COMPILE_STACK_TOP.inner_group_offset = b - bufp->buffer + 2;
  1267. BUF_PUSH_3 (start_memory, regnum, 0);
  1268. }
  1269. compile_stack.avail++;
  1270. fixup_alt_jump = 0;
  1271. laststart = 0;
  1272. begalt = b;
  1273. /* If we've reached MAX_REGNUM groups, then this open
  1274. won't actually generate any code, so we'll have to
  1275. clear pending_exact explicitly. */
  1276. pending_exact = 0;
  1277. break;
  1278. cas

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