PageRenderTime 64ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/quakeforge/branches/release_0_5_4/libs/gib/regex.c

#
C | 1998 lines | 1196 code | 373 blank | 429 comment | 320 complexity | e3a4ad55843b54638c0bae5921ded925 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, AGPL-3.0, AGPL-1.0, Unlicense

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

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

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