PageRenderTime 68ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/regex.c

https://bitbucket.org/cyanogenmod/android_external_lsof
C | 6328 lines | 3947 code | 971 blank | 1410 comment | 985 complexity | 3689ba3b841d387b5ac2efcd77106f5c MD5 | raw file

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

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

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