home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1994 March / Source_Code_CD-ROM_Walnut_Creek_March_1994.iso / compsrcs / misc / volume32 / restrcmp / part01 < prev    next >
Encoding:
Text File  |  1992-09-10  |  53.2 KB  |  2,130 lines

  1. Newsgroups: comp.sources.misc
  2. From: tasos@cs.bu.edu (Anastasios Kotsikonas)
  3. Subject:  v32i034:  re_strcmp - extened egrep pattern matching v1.0, Part01/01
  4. Message-ID: <1992Sep11.172404.18848@sparky.imd.sterling.com>
  5. X-Md4-Signature: b38f372e7bb07ba6376bc595f6249f5a
  6. Date: Fri, 11 Sep 1992 17:24:04 GMT
  7. Approved: kent@sparky.imd.sterling.com
  8.  
  9. Submitted-by: tasos@cs.bu.edu (Anastasios Kotsikonas)
  10. Posting-number: Volume 32, Issue 34
  11. Archive-name: re_strcmp/part01
  12. Environment: UNIX
  13.  
  14. In brief: compare a string against a regular expression. Regular expressions
  15.   may be egrep(1) style with additional support for ~ (logical NOT) and &
  16.   (logical AND).
  17.  
  18. In detail:
  19.   Do pattern matching of two flavors: strict egrep(1), or extended egrep(1).
  20.   The main routine is re_strcmp() which takes a regular expression, a
  21.   string to match against, and a string specifying which of the matched
  22.   subparts are to be used to form a new string -- if this string is NULL no
  23.   such action is taken.
  24.  
  25.   In extended mode, regular expressions may use the logical operators ~ (not),
  26.   & (and) and group expressions further with < and >.
  27.  
  28.   It is possible to use the ls(1) style wild characters * and ?
  29.  
  30.   It is also possible to turn off ed(1) style pattern matching (i.e. turn
  31.   off the meaning of [ ] { } etc.).
  32.  
  33.   The system uses modified pattern matching routines written by Henry Spencer
  34.   (Copyright (c) 1986 by University of Toronto) -- actually only regerror.c
  35.   was modified. Many thanks and kudos to Henry for this wonderful piece of code.
  36.  
  37. Enjoy!
  38. -----------
  39. #! /bin/sh
  40. # This is a shell archive.  Remove anything before this line, then feed it
  41. # into a shell via "sh file" or similar.  To overwrite existing files,
  42. # type "sh file -c".
  43. # Contents:  README Makefile regerror.c regex.c regexp.c regexp.h
  44. #   regmagic.h regsub.c
  45. # Wrapped by kent@sparky on Fri Sep 11 12:19:04 1992
  46. PATH=/bin:/usr/bin:/usr/ucb:/usr/local/bin:/usr/lbin ; export PATH
  47. echo If this archive is complete, you will see the following message:
  48. echo '          "shar: End of archive 1 (of 1)."'
  49. if test -f 'README' -a "${1}" != "-c" ; then 
  50.   echo shar: Will not clobber existing file \"'README'\"
  51. else
  52.   echo shar: Extracting \"'README'\" \(59 characters\)
  53.   sed "s/^X//" >'README' <<'END_OF_FILE'
  54. XFor a description of what this software does, read regex.c
  55. END_OF_FILE
  56.   if test 59 -ne `wc -c <'README'`; then
  57.     echo shar: \"'README'\" unpacked with wrong size!
  58.   fi
  59.   # end of 'README'
  60. fi
  61. if test -f 'Makefile' -a "${1}" != "-c" ; then 
  62.   echo shar: Will not clobber existing file \"'Makefile'\"
  63. else
  64.   echo shar: Extracting \"'Makefile'\" \(1123 characters\)
  65.   sed "s/^X//" >'Makefile' <<'END_OF_FILE'
  66. X# This is a modified Makefile from Henry Spencer's original.
  67. X# Things you might want to put in ENV and LENV:
  68. X# -Dvoid=int        compilers that don't do void
  69. X# -DCHARBITS=0377    compilers that don't do unsigned char
  70. X# -DSTATIC=extern    compilers that don't like "static foo();" as forward decl
  71. X# -DSTRCSPN        library does not have strcspn()
  72. X# -Dstrchr=index    library does not have strchr()
  73. X# -DERRAVAIL        have utzoo-compatible error() function and friends
  74. XENV=
  75. XLENV=
  76. X
  77. X# Things you might want to put in TEST:
  78. X# -DDEBUG        debugging hooks
  79. X# -I.            regexp.h from current directory, not /usr/include
  80. X# -Dtest        to run independetly
  81. XTEST=-I. -Dtest
  82. X
  83. X# Things you might want to put in PROF:
  84. X# -Dstatic='/* */'    make everything global so profiler can see it.
  85. X# -p            profiler
  86. XPROF=
  87. X
  88. XCC=cc
  89. XCFLAGS=-O $(ENV) $(TEST) $(PROF)
  90. XLINTFLAGS=$(LENV) $(TEST) -ha
  91. XLDFLAGS=
  92. X
  93. XOBJ=regexp.o regsub.o regex.o regerror.o
  94. XLSRC=regexp.c regsub.c regerror.c
  95. X
  96. Xall:    regex
  97. X
  98. Xregex:    $(OBJ)
  99. X    $(CC) $(LDFLAGS) $(OBJ) -o regex
  100. X
  101. Xregexp.o:    regexp.c regexp.h regmagic.h
  102. Xregsub.o:    regsub.c regexp.h regmagic.h
  103. Xregerror.o:    regerror.c
  104. Xregex.o:    regex.c regexp.h
  105. X
  106. Xclean:
  107. X    rm -f *.o regex
  108. END_OF_FILE
  109.   if test 1123 -ne `wc -c <'Makefile'`; then
  110.     echo shar: \"'Makefile'\" unpacked with wrong size!
  111.   fi
  112.   # end of 'Makefile'
  113. fi
  114. if test -f 'regerror.c' -a "${1}" != "-c" ; then 
  115.   echo shar: Will not clobber existing file \"'regerror.c'\"
  116. else
  117.   echo shar: Extracting \"'regerror.c'\" \(263 characters\)
  118.   sed "s/^X//" >'regerror.c' <<'END_OF_FILE'
  119. X/*
  120. X  Tasos Kotsikonas (8/20/92): Changed function so that it stores the error
  121. X  message to a global variable.
  122. X*/
  123. X
  124. X#include <stdio.h>
  125. Xchar *regerr;
  126. X
  127. Xvoid
  128. Xregerror(s)
  129. Xchar *s;
  130. X{
  131. X  regerr = (char *) malloc ((strlen (s) + 1) * sizeof (char));
  132. X  strcpy (regerr, s);
  133. X}
  134. END_OF_FILE
  135.   if test 263 -ne `wc -c <'regerror.c'`; then
  136.     echo shar: \"'regerror.c'\" unpacked with wrong size!
  137.   fi
  138.   # end of 'regerror.c'
  139. fi
  140. if test -f 'regex.c' -a "${1}" != "-c" ; then 
  141.   echo shar: Will not clobber existing file \"'regex.c'\"
  142. else
  143.   echo shar: Extracting \"'regex.c'\" \(15430 characters\)
  144.   sed "s/^X//" >'regex.c' <<'END_OF_FILE'
  145. X/*
  146. X  Author: Tasos Kotsikonas (tasos@cs.bu.edu)
  147. X  Disclaimer: All proper disclaimers apply; these include death, damage of any
  148. X          kind to anything and anyone, and this kind of legal garbage.
  149. X  Copying: You may copy, alter and redistribute this software as you see fit,
  150. X         but you may not sell it for profit.
  151. X
  152. X  Do pattern matching of two flavors: strict egrep(1), or extended egrep(1).
  153. X  If the symbol egrep is #define'd below, strict egrep(1) syntax is used.
  154. X
  155. X  The main routine is re_strcmp() which takes a regular expression, a
  156. X  string to match against, and a string specifying which of the matched
  157. X  subparts are to be used to form a new string -- if this string is NULL no
  158. X  such action is taken.
  159. X
  160. X  The new string is stored in the same space, so this string should be
  161. X  long enough not to cause memory overwrites. To be able to do this match
  162. X  substitution, the desired subparts of the regular expression should
  163. X  be enclosed in ( and ). Each subpart is referenced to as \n where n is
  164. X  a digit from 1 to 9.
  165. X
  166. X  For example, the following call:
  167. X
  168. X  strcpy (match, "\\2@\\1.UUCP");
  169. X  re_strcmp ("[^!@]*!([^!@.]*)!([^!@]*)@.*", "GATE!HOP!USER@UUCP.SOME.COM",
  170. X           match);
  171. X
  172. X  will return TRUE and store in 'match' "USER@HOP.UUCP". See the man page
  173. X  for Henry Spencer's routines included for more information.
  174. X
  175. X  In extended mode, regular expressions may use the logical operators ~ (not),
  176. X  & (and) and group expressions further with < and >. Because of the latter,
  177. X  < and > may not be used in the ed(1) sense (\< is the default in the
  178. X  context these routines were developed, and \> is useless in the same context).
  179. X  To use any of these characters literally, precede them with a backslash (\).
  180. X
  181. X  For example:
  182. X
  183. X  re_strcmp ("~<.*\.COM|.*\.EDU>&~TASOS*", "TASOS@FOO.US", NULL)
  184. X
  185. X  will return FALSE.
  186. X
  187. X  re_strcmp() returns TRUE on success, FALSE if no match was found, or
  188. X  -1 on error. In the latter case, the (char *) variable regerr contains the
  189. X  actual error message.
  190. X
  191. X  The system uses modified pattern matching routines written by Henry Spencer
  192. X  (Copyright (c) 1986 by University of Toronto) -- actually only regerror.c
  193. X  was modified. Many thanks and kudos to Henry for this wonderful piece of code.
  194. X
  195. X  It is possible to use the ls(1) style wild characters * and ? by altering
  196. X  the symbols START and QMARK below.
  197. X
  198. X  It is also possible to turn off ed(1) style pattern matching (i.e. turn
  199. X  off the meaning of [ ] { } etc.) by using the function escape_re() where
  200. X  marked with ###.
  201. X
  202. X  To test these routines compile with -Dtest and run independently as follows:
  203. X
  204. X        % cc -Dtest -I. regexp.c regsub.c regex.c regerror.c
  205. X        % a.out 'regular-expression' file
  206. X*/
  207. X
  208. X#include <stdio.h>
  209. X#include <string.h>
  210. X#include <ctype.h>
  211. X#include "regexp.h"
  212. X
  213. X/*
  214. X#define egrep    Define it if you want to use strict egrep(1) regular expressions
  215. X*/
  216. X
  217. X#define MAXLENGTH    1024    /* Maximum length of each regular expression */
  218. X#define EOS        '\0'
  219. X
  220. Xint  re_strcmp (char *, char *, char *);
  221. X
  222. X#ifdef test
  223. Xint  main (int, char **);
  224. X#else
  225. Xextern void report_progress (FILE *, char *, int);
  226. Xextern FILE *report;
  227. X#endif
  228. X#ifndef egrep
  229. Xvoid escape_re (char *);
  230. Xchar *convert_re (char *);
  231. Xint  icp (int);
  232. Xint  push (int);
  233. Xint  pop (void);
  234. Xint  do_op (int, int, int);
  235. Xvoid pop_op (void);
  236. Xint  new_op (int);
  237. Xint  eval (void);
  238. Xint  isop (char *, char *);
  239. Xint  prevch (char *, char *);
  240. Xint  nextch (char *);
  241. X
  242. X#define STAR        "*"    /* If ".*" then the meaning is that of ls(1) */
  243. X#define QMARK        "?"    /* If "." then the meaning is that of ls(1) */
  244. X#define LGROUPCH    '<'    /* Avoid ( and ) */
  245. X#define RGROUPCH    '>'
  246. X#define OR        1    /* Operators should be > 0 */
  247. X#define AND        2
  248. X#define    NOT        3
  249. X#define LPAREN        4
  250. X#define RPAREN        5
  251. X
  252. Xtypedef struct _operator_stack {
  253. X  int op;
  254. X  int isp;    /* In-stack priority */
  255. X  struct _operator_stack *next, *prev;
  256. X} OPERATOR_STACK;
  257. X
  258. Xtypedef struct _operand_stack {
  259. X  int val;
  260. X  struct _operand_stack *next, *prev;
  261. X} OPERAND_STACK;
  262. X
  263. XOPERATOR_STACK *op_top;
  264. XOPERAND_STACK *val_top;
  265. Xint pliteral, literal, nliteral;
  266. X#endif
  267. X
  268. Xextern char *regerr;
  269. X
  270. X#ifdef test
  271. Xint main (int argc, char **argv)
  272. X{
  273. X  FILE *f;
  274. X  char s [1024], matches [1024];
  275. X  int match;
  276. X
  277. X  if (argc < 2)
  278. X    printf ("Usage: %s 'regular-expression' file\n", argv[0]), exit (1);
  279. X  if ((f = fopen (argv[2], "r")) == NULL)
  280. X    printf ("%s: ", argv[2]), fflush (stdout), perror (""), exit (1);
  281. X  while (!feof (f)) {
  282. X    memset (s, EOS, sizeof (s));
  283. X    fgets (s, sizeof (s) - 1, f);
  284. X    strcpy (matches, "\\2@\\1.UUCP");
  285. X    if ((match = re_strcmp (argv[1], s, matches)) > 0)
  286. X      printf ("MATCHES:%s\n", matches),
  287. X      printf ("%s", s);
  288. X    else if (match < 0)
  289. X      printf ("Error in regular expression\n"),
  290. X      exit (1);
  291. X  }
  292. X  fclose (f);
  293. X  exit (0);
  294. X}
  295. X#endif
  296. X
  297. X/*
  298. X  Check 'subject' againt the 'regexpr'. Return 1 on match, 0 if no match,
  299. X  or -1 on error. To negegate a regular expression precede it with '~';
  300. X  multiple regular expressions are separated by '|' or '&' (logical OR and
  301. X  AND) and may be grouped with LGROUPCH and RGROUPCH. To escape the key
  302. X  characters LGROUPCH * ? | & RGROUPCH use \.
  303. X*/
  304. X
  305. Xint re_strcmp (char *regexpr, char *subject, char *result)
  306. X{
  307. X  char *re, *readdr, s[MAXLENGTH], _re[MAXLENGTH], matches [1024];
  308. X  int op, i;
  309. X  regexp *cmp;
  310. X
  311. X  strcpy (s, regexpr);
  312. X#ifndef egrep
  313. X/*###        Do not escape ed(1) special characters.
  314. X  escape_re (s);
  315. X*/
  316. X  readdr = re = convert_re (s);
  317. X#else
  318. X  re = s;
  319. X#endif
  320. X  do {
  321. X#ifndef egrep
  322. X    if ((op = isop (re, readdr))) {
  323. X      if (new_op (op)) {
  324. X    eval ();    /* Empty stacks */
  325. X    return -1;
  326. X      }
  327. X      ++re;
  328. X    }
  329. X    else 
  330. X#endif
  331. X    {
  332. X      i = 0;
  333. X      while (*re != EOS && i < MAXLENGTH
  334. X#ifndef egrep
  335. X         && !isop (re, readdr)
  336. X#endif
  337. X)
  338. X    _re [i++] = *re,
  339. X    ++re;
  340. X      if (i == MAXLENGTH)    /* Overflow */
  341. X    return -1;
  342. X      _re [i] = EOS;
  343. X      if (! (cmp = (regexp *) regcomp (_re))) {
  344. X#ifndef test
  345. X    sprintf (s, "RE %s: %s\n", _re, regerr);
  346. X    report_progress (report, s, 1);
  347. X#else
  348. X        printf ("RE %s: %s\n", _re, regerr);
  349. X#endif
  350. X    free ((char *) regerr);
  351. X#ifndef egrep
  352. X    eval ();    /* Empty stacks */
  353. X    free ((char *) readdr);
  354. X#endif
  355. X        return -1;
  356. X      }
  357. X      if (regexec (cmp, subject)) {
  358. X    if (result)
  359. X      regsub (cmp, result, matches),
  360. X      strcpy (result, matches);
  361. X    free ((regexp *) cmp);
  362. X#ifndef egrep
  363. X    push (1);
  364. X#else
  365. X    return 1;
  366. X#endif
  367. X      }
  368. X      else {
  369. X    free ((regexp *) cmp);
  370. X#ifndef egrep
  371. X    push (0);
  372. X#else
  373. X    return 0;
  374. X#endif
  375. X      }
  376. X    }
  377. X  } while (*re != EOS);
  378. X#ifndef egrep
  379. X  free ((char *) readdr);
  380. X  return eval ();
  381. X#endif
  382. X}
  383. X
  384. X/*
  385. X  Scan 's' and escape the following characters: [ ] < > { } , ; . ^ $ + -
  386. X  for regular expression matching.
  387. X
  388. X  To be used if one does not want ed(1) style pattern matching. Currently, this
  389. X  function is unused.
  390. X*/
  391. X
  392. X#ifndef egrep
  393. Xvoid escape_re (char *s)
  394. X{
  395. X  char *r;
  396. X
  397. X  while (*s != EOS) {
  398. X    switch (*s) {
  399. X    case '[': case ']': case '<': case '>': case '+': case '-':
  400. X    case ';': case ',': case '.': case '^': case '$': case '{': case '}':
  401. X      r = s + strlen (s);       /* Start from the end */
  402. X      while (r != s)
  403. X        *(r + 1) = *r,
  404. X        --r;
  405. X      *(r + 1) = *r;
  406. X      *r = '\\';
  407. X      ++s;
  408. X      break;
  409. X    }
  410. X    ++s;
  411. X  }
  412. X}
  413. X
  414. X/*
  415. X  Possibly convert the wild characters * and ? to ed(1) regular expressions.
  416. X  Handle escaped characters.
  417. X*/
  418. X
  419. Xchar *convert_re (char *re)
  420. X{
  421. X  char *r, *b = re;
  422. X  int i = 0;
  423. X
  424. X  r = (char *) malloc (sizeof (char));
  425. X  while (*re != EOS) {
  426. X    prevch (re, b);    /* See if *re is literal */
  427. X    switch (*re) {
  428. X    case '\\':
  429. X      if (re != b && literal)
  430. X    r = (char *) realloc (r, (i + 2) * sizeof (char)),
  431. X    strncpy (r + i, "\\\\", 2),
  432. X    i += 2;
  433. X      break;
  434. X    case '?':        /* Match one character */
  435. X     if (re != b && literal)    /* Literal */
  436. X    r = (char *) realloc (r, (i + 1) * sizeof (char)),
  437. X    *(r + i) = *re,
  438. X    ++i;
  439. X      else
  440. X    r = (char *) realloc (r, (i + strlen (QMARK)) * sizeof (char)),
  441. X    strncpy (r + i, QMARK, strlen (QMARK)),
  442. X    i += strlen (QMARK);
  443. X      break;
  444. X    case '*':        /* Match multiple characters */
  445. X      if (re != b && literal)    /* Literal */
  446. X    r = (char *) realloc (r, (i + 2) * sizeof (char)),
  447. X    strncpy (r + i, "\\*", 2),
  448. X    i += 2;
  449. X      else
  450. X    r = (char *) realloc (r, (i + strlen (STAR)) * sizeof (char)),
  451. X    strncpy (r + i, STAR, strlen (STAR)),
  452. X    i += strlen (STAR);
  453. X      break;
  454. X    default:
  455. X      if (re != b && literal)      /* carry over */
  456. X    r = (char *) realloc (r, (i + 2) * sizeof (char)),
  457. X    *(r + i) = *(re - 1),
  458. X    *(r + i + 1) = *re,
  459. X    i += 2;
  460. X      else
  461. X    r = (char *) realloc (r, (i + 1) * sizeof (char)),
  462. X    *(r + i) = *re,
  463. X    ++i;
  464. X    }
  465. X    ++re;
  466. X  }
  467. X  r = (char *) realloc (r, (i + 1) * sizeof (char));
  468. X  *(r + i) = EOS;
  469. X  return r;
  470. X}
  471. X
  472. X/*
  473. X  Return the in-coming priority of an operator.
  474. X*/
  475. X
  476. Xint icp (int op)
  477. X{
  478. X  if (op == OR || op == AND) return 1;
  479. X  if (op == LPAREN || op == NOT) return 4;
  480. X  return 0;
  481. X}
  482. X
  483. X/*
  484. X  Push a new operand onto OPERAND_STACK.
  485. X*/
  486. X
  487. Xint push (int val)
  488. X{
  489. X  OPERAND_STACK *s;
  490. X
  491. X  s = (OPERAND_STACK *) malloc (sizeof (OPERAND_STACK));
  492. X  s->val = val;
  493. X  s->prev = val_top;
  494. X  s->next = NULL;
  495. X  if (val_top)
  496. X    val_top->next = s;
  497. X  val_top = s;
  498. X  return val;
  499. X}
  500. X
  501. X/*
  502. X  Pop an operand from OPERAND_STACK. Return 0 or 1, or -1 on error.
  503. X*/
  504. X
  505. Xint pop ()
  506. X{
  507. X  int val;
  508. X
  509. X  if (!val_top)    /* Empty stack */
  510. X    return -1;
  511. X  val = val_top->val;
  512. X  if (val_top->prev)
  513. X    val_top = val_top->prev,
  514. X    free ((OPERAND_STACK *) val_top->next),
  515. X    val_top->next = NULL;
  516. X  else
  517. X    free ((OPERAND_STACK *) val_top),
  518. X    val_top = NULL;
  519. X  return val;
  520. X}
  521. X
  522. X/*
  523. X  Perform a boolean operation and return the result, or -1 on error.
  524. X*/
  525. X
  526. Xint do_op (int op, int val1, int val2)
  527. X{
  528. X  if (val1 < 0 || val2 < 0)
  529. X    return -1;
  530. X  if (op == OR)
  531. X    return val1 | val2;
  532. X  if (op == AND)
  533. X    return val1 & val2;
  534. X  return !val1;
  535. X}
  536. X
  537. X/*
  538. X  Pop an operator. An operator will always be present.
  539. X*/
  540. X
  541. Xvoid pop_op ()
  542. X{
  543. X  OPERATOR_STACK *s;
  544. X
  545. X  s = op_top;
  546. X  free ((OPERATOR_STACK *) s);
  547. X  op_top = op_top->prev;
  548. X  if (op_top)
  549. X    op_top->next = NULL;
  550. X}
  551. X
  552. X/*
  553. X  Process a new operator: push it anyway, or push it after popping other
  554. X  operators with higher priority. Return -1 on error condition.
  555. X*/
  556. X
  557. Xint new_op (int op)
  558. X{
  559. X  OPERATOR_STACK *s;
  560. X  int res;
  561. X
  562. X  if (op < OR)
  563. X    return -1;
  564. X  if (op == RPAREN) {
  565. X    do {    /* Pop and process operators till LPAREN */
  566. X      if (!op_top)
  567. X    return -1;
  568. X      if (op_top->op != LPAREN) {
  569. X    if (op_top->op == OR || op_top->op == AND)
  570. X      res = push (do_op (op_top->op, pop (), pop ()));
  571. X    else    /* NOT */
  572. X      res = push (do_op (op_top->op, pop (), 0));
  573. X    if (res < 0)    /* Error with operands */
  574. X      return res;
  575. X    pop_op ();    /* Pop processed operator */
  576. X    if (!op_top)
  577. X      return -1;
  578. X      }
  579. X    } while (op_top->op != LPAREN);
  580. X    pop_op ();    /* LPAREN */
  581. X  }
  582. X  else {    /* Push new operator */
  583. X    while (op_top && op_top->isp >= icp (op)) {    /* Process op w/ > priority */
  584. X      if (op_top->op == OR || op_top->op == AND)
  585. X    res = push (do_op (op_top->op, pop (), pop ()));
  586. X      else    /* NOT */
  587. X    res = push (do_op (op_top->op, pop (), 0));
  588. X      if (res < 0)
  589. X    return res;
  590. X      pop_op ();
  591. X    }
  592. X    s = (OPERATOR_STACK *) malloc (sizeof (OPERATOR_STACK));
  593. X    s->op = op;
  594. X    if (op == OR) s->isp = 1;
  595. X    else if (op == AND) s->isp = 1;
  596. X    else if (op == NOT) s->isp = 2;
  597. X    else s->isp = 0;
  598. X    s->prev = op_top;
  599. X    s->next = NULL;
  600. X    if (op_top)
  601. X      op_top->next = s;
  602. X    op_top = s;
  603. X  }
  604. X  return 0;
  605. X}
  606. X
  607. X/*
  608. X  Process the remaining operators and values. Return -1 on error.
  609. X*/
  610. X
  611. Xint eval ()
  612. X{
  613. X  OPERATOR_STACK *op;
  614. X  OPERAND_STACK *val;
  615. X  int res;
  616. X
  617. X  if (!op_top && !val_top)
  618. X    return 0;
  619. X  while (op_top) {
  620. X    if (op_top->op != AND && op_top->op != OR && op_top->op != NOT) {
  621. X      if (op_top->op < OR || op_top->op > RPAREN)
  622. X#ifndef test
  623. X    report_progress (report, "Internal error: unexpected op", 1);
  624. X#else
  625. X    printf ("Internal error: unexpected op %d\n", op_top->op);
  626. X#endif
  627. X      return -1;
  628. X    }
  629. X    if (op_top->op == OR || op_top->op == AND)
  630. X      res = push (do_op (op_top->op, pop (), pop ()));
  631. X    else    /* NOT */
  632. X      res = push (do_op (op_top->op, pop (), 0));
  633. X    if (res < 0)
  634. X      return res;
  635. X    op = op_top;
  636. X    free ((OPERATOR_STACK *) op);
  637. X    op_top = op_top->prev;
  638. X  }
  639. X  if (!val_top) {
  640. X#ifndef test
  641. X    report_progress (report, "Internal error: empty operand stack", 1);
  642. X#else
  643. X    printf ("Internal error: empty operand stack\n");
  644. X#endif
  645. X    return -1;
  646. X  }
  647. X  return pop ();
  648. X}
  649. X
  650. X/*
  651. X  Return the operator id if indeed 's' points to an operator. Return -1
  652. X  on error.
  653. X*/
  654. X
  655. Xint isop (char *s, char *b)
  656. X{
  657. X  char pch, nch;
  658. X
  659. X  pch = prevch (s, b);
  660. X  if (literal) return 0;
  661. X  nch = nextch (s);
  662. X  if (*s == LGROUPCH)
  663. X    if ((pch == '|' || pch == '&' || pch == '~' || pch == LGROUPCH) &&
  664. X    !pliteral && ((nch != '|' && nch != '&' && nch != EOS) || nliteral))
  665. X      return LPAREN;
  666. X    else
  667. X      return -1;
  668. X  if (*s == RGROUPCH)
  669. X    if (((pch != '|' && pch != '&' && pch != '~') || pliteral) && !nliteral &&
  670. X    (nch == RGROUPCH || nch == '&' || nch == '|' || nch == EOS))
  671. X      return RPAREN;
  672. X    else
  673. X      return -1;
  674. X  if (*s == '~')
  675. X    if ((pch == LGROUPCH || pch == '|' || pch == '&' || pch == '~') &&
  676. X    !pliteral &&
  677. X    ((nch != '|' && nch != '&' && nch != RGROUPCH && nch != EOS) ||
  678. X     nliteral))
  679. X      return NOT;
  680. X    else
  681. X      return -1;
  682. X  if (*s == '|' || *s == '&')
  683. X    if (((pch != '~' && pch != '|' && pch != '&') || pliteral) &&
  684. X    ((nch != '|' && nch != '&' && nch != RGROUPCH &&
  685. X      nch != EOS) || nliteral))
  686. X      return (*s == '|' ? OR : AND);
  687. X    else
  688. X      return -1;
  689. X  return 0;
  690. X}
  691. X
  692. X/*
  693. X  Return the previous character from the current position in the
  694. X  string. Set 'pliteral' to 1 if that previous character is escaped with \ and
  695. X  'literal' to 1 if the current character is escaped with \.
  696. X  In the comments, ^ means "beginning of the string", ? matches any character
  697. X  except \, * matches absolutely any character and x is the current position.
  698. X*/
  699. X
  700. Xint prevch (char *s, char *b)
  701. X{
  702. X  return
  703. X    ((s) <= (b) ? 
  704. X     (pliteral = literal = 0, *(s)) : /* ^x */
  705. X     (((s) - 1) == (b) ? 
  706. X      (*((s) - 1) == '\\' ? (pliteral = !(literal = 1), *((s) - 1)) /* ^\x */
  707. X       : (pliteral = literal = 0, *((s) - 1))) /* ^?x */
  708. X      : (*(s) == '\\' ? 
  709. X     (*((s) - 2) == '\\' ? (pliteral = !(literal = 0), *((s) - 1)) /* \*\ */
  710. X      : (*((s) - 1) == '\\' ? (pliteral = !(literal = 1), *((s) - 2)) /* ?\\ */
  711. X         : (pliteral = literal = 0, *((s) - 1)))) /* ??\ */
  712. X     : (*((s) - 1) == '\\' ? 
  713. X        (*((s) - 2) == '\\' ? 
  714. X         (((s) - 2) == (b) ? (pliteral = !(literal = 0), *((s) - 1)) /* ^\\x */
  715. X          : (*((s) - 3) == '\\' ? (pliteral = literal = 1,*((s) - 2)) /* \\\x */
  716. X         : (pliteral = !(literal = 0), *((s) - 1)))) /* ?\\x */
  717. X         : (((s) - 2) == (b) ? (pliteral = !(literal = 1),*((s) - 2)) /* ^?\x */
  718. X        : (*((s) - 3) == '\\' ? (pliteral = literal = 1, *((s) - 2)) /* \?\x */
  719. X           : (pliteral = !(literal = 1),  *((s) - 2))))) /* ??\x */
  720. X        : (*((s) - 2) == '\\' ? (pliteral = !(literal = 0),*((s) - 1)) /* \?x */
  721. X           : (pliteral = literal = 0, *((s) - 1))))))); /* ??x */
  722. X}
  723. X
  724. X/*
  725. X  Return the next character from the current position. The current character
  726. X  is guarranteed not to be a literal. In the comments below * matches
  727. X  absolutely any character, ? matches anything but EOS, $ is the EOS and
  728. X  x marks the current position. Also set 'nliteral' to 1 if the next character
  729. X  is escaped with \.
  730. X*/
  731. X
  732. Xint nextch (char *s)
  733. X{
  734. X  return
  735. X    (*((s) + 1) == EOS ? (nliteral = 0, EOS) /* x$ */
  736. X     : (*((s) + 1) == '\\' ? 
  737. X    (*((s) + 2) == EOS ? (nliteral = 0, *((s) + 1)) /* x\$ */
  738. X     : (nliteral = 1, *((s) + 2))) /* x\? */
  739. X    : (nliteral = 0, *((s) + 1)))); /* x* */
  740. X}
  741. X#endif
  742. END_OF_FILE
  743.   if test 15430 -ne `wc -c <'regex.c'`; then
  744.     echo shar: \"'regex.c'\" unpacked with wrong size!
  745.   fi
  746.   # end of 'regex.c'
  747. fi
  748. if test -f 'regexp.c' -a "${1}" != "-c" ; then 
  749.   echo shar: Will not clobber existing file \"'regexp.c'\"
  750. else
  751.   echo shar: Extracting \"'regexp.c'\" \(27614 characters\)
  752.   sed "s/^X//" >'regexp.c' <<'END_OF_FILE'
  753. X/*
  754. X * regcomp and regexec -- regsub and regerror are elsewhere
  755. X *
  756. X *    Copyright (c) 1986 by University of Toronto.
  757. X *    Written by Henry Spencer.  Not derived from licensed software.
  758. X *
  759. X *    Permission is granted to anyone to use this software for any
  760. X *    purpose on any computer system, and to redistribute it freely,
  761. X *    subject to the following restrictions:
  762. X *
  763. X *    1. The author is not responsible for the consequences of use of
  764. X *        this software, no matter how awful, even if they arise
  765. X *        from defects in it.
  766. X *
  767. X *    2. The origin of this software must not be misrepresented, either
  768. X *        by explicit claim or by omission.
  769. X *
  770. X *    3. Altered versions must be plainly marked as such, and must not
  771. X *        be misrepresented as being the original software.
  772. X *
  773. X * Beware that some of this code is subtly aware of the way operator
  774. X * precedence is structured in regular expressions.  Serious changes in
  775. X * regular-expression syntax might require a total rethink.
  776. X */
  777. X#include <stdio.h>
  778. X#include <regexp.h>
  779. X#include "regmagic.h"
  780. X
  781. X/*
  782. X * The "internal use only" fields in regexp.h are present to pass info from
  783. X * compile to execute that permits the execute phase to run lots faster on
  784. X * simple cases.  They are:
  785. X *
  786. X * regstart    char that must begin a match; '\0' if none obvious
  787. X * reganch    is the match anchored (at beginning-of-line only)?
  788. X * regmust    string (pointer into program) that match must include, or NULL
  789. X * regmlen    length of regmust string
  790. X *
  791. X * Regstart and reganch permit very fast decisions on suitable starting points
  792. X * for a match, cutting down the work a lot.  Regmust permits fast rejection
  793. X * of lines that cannot possibly match.  The regmust tests are costly enough
  794. X * that regcomp() supplies a regmust only if the r.e. contains something
  795. X * potentially expensive (at present, the only such thing detected is * or +
  796. X * at the start of the r.e., which can involve a lot of backup).  Regmlen is
  797. X * supplied because the test in regexec() needs it and regcomp() is computing
  798. X * it anyway.
  799. X */
  800. X
  801. X/*
  802. X * Structure for regexp "program".  This is essentially a linear encoding
  803. X * of a nondeterministic finite-state machine (aka syntax charts or
  804. X * "railroad normal form" in parsing technology).  Each node is an opcode
  805. X * plus a "next" pointer, possibly plus an operand.  "Next" pointers of
  806. X * all nodes except BRANCH implement concatenation; a "next" pointer with
  807. X * a BRANCH on both ends of it is connecting two alternatives.  (Here we
  808. X * have one of the subtle syntax dependencies:  an individual BRANCH (as
  809. X * opposed to a collection of them) is never concatenated with anything
  810. X * because of operator precedence.)  The operand of some types of node is
  811. X * a literal string; for others, it is a node leading into a sub-FSM.  In
  812. X * particular, the operand of a BRANCH node is the first node of the branch.
  813. X * (NB this is *not* a tree structure:  the tail of the branch connects
  814. X * to the thing following the set of BRANCHes.)  The opcodes are:
  815. X */
  816. X
  817. X/* definition    number    opnd?    meaning */
  818. X#define    END    0    /* no    End of program. */
  819. X#define    BOL    1    /* no    Match "" at beginning of line. */
  820. X#define    EOL    2    /* no    Match "" at end of line. */
  821. X#define    ANY    3    /* no    Match any one character. */
  822. X#define    ANYOF    4    /* str    Match any character in this string. */
  823. X#define    ANYBUT    5    /* str    Match any character not in this string. */
  824. X#define    BRANCH    6    /* node    Match this alternative, or the next... */
  825. X#define    BACK    7    /* no    Match "", "next" ptr points backward. */
  826. X#define    EXACTLY    8    /* str    Match this string. */
  827. X#define    NOTHING    9    /* no    Match empty string. */
  828. X#define    STAR    10    /* node    Match this (simple) thing 0 or more times. */
  829. X#define    PLUS    11    /* node    Match this (simple) thing 1 or more times. */
  830. X#define    OPEN    20    /* no    Mark this point in input as start of #n. */
  831. X            /*    OPEN+1 is number 1, etc. */
  832. X#define    CLOSE    30    /* no    Analogous to OPEN. */
  833. X
  834. X/*
  835. X * Opcode notes:
  836. X *
  837. X * BRANCH    The set of branches constituting a single choice are hooked
  838. X *        together with their "next" pointers, since precedence prevents
  839. X *        anything being concatenated to any individual branch.  The
  840. X *        "next" pointer of the last BRANCH in a choice points to the
  841. X *        thing following the whole choice.  This is also where the
  842. X *        final "next" pointer of each individual branch points; each
  843. X *        branch starts with the operand node of a BRANCH node.
  844. X *
  845. X * BACK        Normal "next" pointers all implicitly point forward; BACK
  846. X *        exists to make loop structures possible.
  847. X *
  848. X * STAR,PLUS    '?', and complex '*' and '+', are implemented as circular
  849. X *        BRANCH structures using BACK.  Simple cases (one character
  850. X *        per match) are implemented with STAR and PLUS for speed
  851. X *        and to minimize recursive plunges.
  852. X *
  853. X * OPEN,CLOSE    ...are numbered at compile time.
  854. X */
  855. X
  856. X/*
  857. X * A node is one char of opcode followed by two chars of "next" pointer.
  858. X * "Next" pointers are stored as two 8-bit pieces, high order first.  The
  859. X * value is a positive offset from the opcode of the node containing it.
  860. X * An operand, if any, simply follows the node.  (Note that much of the
  861. X * code generation knows about this implicit relationship.)
  862. X *
  863. X * Using two bytes for the "next" pointer is vast overkill for most things,
  864. X * but allows patterns to get big without disasters.
  865. X */
  866. X#define    OP(p)    (*(p))
  867. X#define    NEXT(p)    (((*((p)+1)&0377)<<8) + *((p)+2)&0377)
  868. X#define    OPERAND(p)    ((p) + 3)
  869. X
  870. X/*
  871. X * See regmagic.h for one further detail of program structure.
  872. X */
  873. X
  874. X
  875. X/*
  876. X * Utility definitions.
  877. X */
  878. X#ifndef CHARBITS
  879. X#define    UCHARAT(p)    ((int)*(unsigned char *)(p))
  880. X#else
  881. X#define    UCHARAT(p)    ((int)*(p)&CHARBITS)
  882. X#endif
  883. X
  884. X#define    FAIL(m)    { regerror(m); return(NULL); }
  885. X#define    ISMULT(c)    ((c) == '*' || (c) == '+' || (c) == '?')
  886. X#define    META    "^$.[()|?+*\\"
  887. X
  888. X/*
  889. X * Flags to be passed up and down.
  890. X */
  891. X#define    HASWIDTH    01    /* Known never to match null string. */
  892. X#define    SIMPLE        02    /* Simple enough to be STAR/PLUS operand. */
  893. X#define    SPSTART        04    /* Starts with * or +. */
  894. X#define    WORST        0    /* Worst case. */
  895. X
  896. X/*
  897. X * Global work variables for regcomp().
  898. X */
  899. Xstatic char *regparse;        /* Input-scan pointer. */
  900. Xstatic int regnpar;        /* () count. */
  901. Xstatic char regdummy;
  902. Xstatic char *regcode;        /* Code-emit pointer; ®dummy = don't. */
  903. Xstatic long regsize;        /* Code size. */
  904. X
  905. X/*
  906. X * Forward declarations for regcomp()'s friends.
  907. X */
  908. X#ifndef STATIC
  909. X#define    STATIC    static
  910. X#endif
  911. XSTATIC char *reg();
  912. XSTATIC char *regbranch();
  913. XSTATIC char *regpiece();
  914. XSTATIC char *regatom();
  915. XSTATIC char *regnode();
  916. XSTATIC char *regnext();
  917. XSTATIC void regc();
  918. XSTATIC void reginsert();
  919. XSTATIC void regtail();
  920. XSTATIC void regoptail();
  921. X#ifdef STRCSPN
  922. XSTATIC int strcspn();
  923. X#endif
  924. X
  925. X/*
  926. X - regcomp - compile a regular expression into internal code
  927. X *
  928. X * We can't allocate space until we know how big the compiled form will be,
  929. X * but we can't compile it (and thus know how big it is) until we've got a
  930. X * place to put the code.  So we cheat:  we compile it twice, once with code
  931. X * generation turned off and size counting turned on, and once "for real".
  932. X * This also means that we don't allocate space until we are sure that the
  933. X * thing really will compile successfully, and we never have to move the
  934. X * code and thus invalidate pointers into it.  (Note that it has to be in
  935. X * one piece because free() must be able to free it all.)
  936. X *
  937. X * Beware that the optimization-preparation code in here knows about some
  938. X * of the structure of the compiled regexp.
  939. X */
  940. Xregexp *
  941. Xregcomp(exp)
  942. Xchar *exp;
  943. X{
  944. X    register regexp *r;
  945. X    register char *scan;
  946. X    register char *longest;
  947. X    register int len;
  948. X    int flags;
  949. X    extern char *malloc();
  950. X
  951. X    if (exp == NULL)
  952. X        FAIL("NULL argument");
  953. X
  954. X    /* First pass: determine size, legality. */
  955. X    regparse = exp;
  956. X    regnpar = 1;
  957. X    regsize = 0L;
  958. X    regcode = ®dummy;
  959. X    regc(MAGIC);
  960. X    if (reg(0, &flags) == NULL)
  961. X        return(NULL);
  962. X
  963. X    /* Small enough for pointer-storage convention? */
  964. X    if (regsize >= 32767L)        /* Probably could be 65535L. */
  965. X        FAIL("regexp too big");
  966. X
  967. X    /* Allocate space. */
  968. X    r = (regexp *)malloc(sizeof(regexp) + (unsigned)regsize);
  969. X    if (r == NULL)
  970. X        FAIL("out of space");
  971. X
  972. X    /* Second pass: emit code. */
  973. X    regparse = exp;
  974. X    regnpar = 1;
  975. X    regcode = r->program;
  976. X    regc(MAGIC);
  977. X    if (reg(0, &flags) == NULL)
  978. X        return(NULL);
  979. X
  980. X    /* Dig out information for optimizations. */
  981. X    r->regstart = '\0';    /* Worst-case defaults. */
  982. X    r->reganch = 0;
  983. X    r->regmust = NULL;
  984. X    r->regmlen = 0;
  985. X    scan = r->program+1;            /* First BRANCH. */
  986. X    if (OP(regnext(scan)) == END) {        /* Only one top-level choice. */
  987. X        scan = OPERAND(scan);
  988. X
  989. X        /* Starting-point info. */
  990. X        if (OP(scan) == EXACTLY)
  991. X            r->regstart = *OPERAND(scan);
  992. X        else if (OP(scan) == BOL)
  993. X            r->reganch++;
  994. X
  995. X        /*
  996. X         * If there's something expensive in the r.e., find the
  997. X         * longest literal string that must appear and make it the
  998. X         * regmust.  Resolve ties in favor of later strings, since
  999. X         * the regstart check works with the beginning of the r.e.
  1000. X         * and avoiding duplication strengthens checking.  Not a
  1001. X         * strong reason, but sufficient in the absence of others.
  1002. X         */
  1003. X        if (flags&SPSTART) {
  1004. X            longest = NULL;
  1005. X            len = 0;
  1006. X            for (; scan != NULL; scan = regnext(scan))
  1007. X                if (OP(scan) == EXACTLY && strlen(OPERAND(scan)) >= len) {
  1008. X                    longest = OPERAND(scan);
  1009. X                    len = strlen(OPERAND(scan));
  1010. X                }
  1011. X            r->regmust = longest;
  1012. X            r->regmlen = len;
  1013. X        }
  1014. X    }
  1015. X
  1016. X    return(r);
  1017. X}
  1018. X
  1019. X/*
  1020. X - reg - regular expression, i.e. main body or parenthesized thing
  1021. X *
  1022. X * Caller must absorb opening parenthesis.
  1023. X *
  1024. X * Combining parenthesis handling with the base level of regular expression
  1025. X * is a trifle forced, but the need to tie the tails of the branches to what
  1026. X * follows makes it hard to avoid.
  1027. X */
  1028. Xstatic char *
  1029. Xreg(paren, flagp)
  1030. Xint paren;            /* Parenthesized? */
  1031. Xint *flagp;
  1032. X{
  1033. X    register char *ret;
  1034. X    register char *br;
  1035. X    register char *ender;
  1036. X    register int parno;
  1037. X    int flags;
  1038. X
  1039. X    *flagp = HASWIDTH;    /* Tentatively. */
  1040. X
  1041. X    /* Make an OPEN node, if parenthesized. */
  1042. X    if (paren) {
  1043. X        if (regnpar >= NSUBEXP)
  1044. X            FAIL("too many ()");
  1045. X        parno = regnpar;
  1046. X        regnpar++;
  1047. X        ret = regnode(OPEN+parno);
  1048. X    } else
  1049. X        ret = NULL;
  1050. X
  1051. X    /* Pick up the branches, linking them together. */
  1052. X    br = regbranch(&flags);
  1053. X    if (br == NULL)
  1054. X        return(NULL);
  1055. X    if (ret != NULL)
  1056. X        regtail(ret, br);    /* OPEN -> first. */
  1057. X    else
  1058. X        ret = br;
  1059. X    if (!(flags&HASWIDTH))
  1060. X        *flagp &= ~HASWIDTH;
  1061. X    *flagp |= flags&SPSTART;
  1062. X    while (*regparse == '|') {
  1063. X        regparse++;
  1064. X        br = regbranch(&flags);
  1065. X        if (br == NULL)
  1066. X            return(NULL);
  1067. X        regtail(ret, br);    /* BRANCH -> BRANCH. */
  1068. X        if (!(flags&HASWIDTH))
  1069. X            *flagp &= ~HASWIDTH;
  1070. X        *flagp |= flags&SPSTART;
  1071. X    }
  1072. X
  1073. X    /* Make a closing node, and hook it on the end. */
  1074. X    ender = regnode((paren) ? CLOSE+parno : END);    
  1075. X    regtail(ret, ender);
  1076. X
  1077. X    /* Hook the tails of the branches to the closing node. */
  1078. X    for (br = ret; br != NULL; br = regnext(br))
  1079. X        regoptail(br, ender);
  1080. X
  1081. X    /* Check for proper termination. */
  1082. X    if (paren && *regparse++ != ')') {
  1083. X        FAIL("unmatched ()");
  1084. X    } else if (!paren && *regparse != '\0') {
  1085. X        if (*regparse == ')') {
  1086. X            FAIL("unmatched ()");
  1087. X        } else
  1088. X            FAIL("junk on end");    /* "Can't happen". */
  1089. X        /* NOTREACHED */
  1090. X    }
  1091. X
  1092. X    return(ret);
  1093. X}
  1094. X
  1095. X/*
  1096. X - regbranch - one alternative of an | operator
  1097. X *
  1098. X * Implements the concatenation operator.
  1099. X */
  1100. Xstatic char *
  1101. Xregbranch(flagp)
  1102. Xint *flagp;
  1103. X{
  1104. X    register char *ret;
  1105. X    register char *chain;
  1106. X    register char *latest;
  1107. X    int flags;
  1108. X
  1109. X    *flagp = WORST;        /* Tentatively. */
  1110. X
  1111. X    ret = regnode(BRANCH);
  1112. X    chain = NULL;
  1113. X    while (*regparse != '\0' && *regparse != '|' && *regparse != ')') {
  1114. X        latest = regpiece(&flags);
  1115. X        if (latest == NULL)
  1116. X            return(NULL);
  1117. X        *flagp |= flags&HASWIDTH;
  1118. X        if (chain == NULL)    /* First piece. */
  1119. X            *flagp |= flags&SPSTART;
  1120. X        else
  1121. X            regtail(chain, latest);
  1122. X        chain = latest;
  1123. X    }
  1124. X    if (chain == NULL)    /* Loop ran zero times. */
  1125. X        (void) regnode(NOTHING);
  1126. X
  1127. X    return(ret);
  1128. X}
  1129. X
  1130. X/*
  1131. X - regpiece - something followed by possible [*+?]
  1132. X *
  1133. X * Note that the branching code sequences used for ? and the general cases
  1134. X * of * and + are somewhat optimized:  they use the same NOTHING node as
  1135. X * both the endmarker for their branch list and the body of the last branch.
  1136. X * It might seem that this node could be dispensed with entirely, but the
  1137. X * endmarker role is not redundant.
  1138. X */
  1139. Xstatic char *
  1140. Xregpiece(flagp)
  1141. Xint *flagp;
  1142. X{
  1143. X    register char *ret;
  1144. X    register char op;
  1145. X    register char *next;
  1146. X    int flags;
  1147. X
  1148. X    ret = regatom(&flags);
  1149. X    if (ret == NULL)
  1150. X        return(NULL);
  1151. X
  1152. X    op = *regparse;
  1153. X    if (!ISMULT(op)) {
  1154. X        *flagp = flags;
  1155. X        return(ret);
  1156. X    }
  1157. X
  1158. X    if (!(flags&HASWIDTH) && op != '?')
  1159. X        FAIL("*+ operand could be empty");
  1160. X    *flagp = (op != '+') ? (WORST|SPSTART) : (WORST|HASWIDTH);
  1161. X
  1162. X    if (op == '*' && (flags&SIMPLE))
  1163. X        reginsert(STAR, ret);
  1164. X    else if (op == '*') {
  1165. X        /* Emit x* as (x&|), where & means "self". */
  1166. X        reginsert(BRANCH, ret);            /* Either x */
  1167. X        regoptail(ret, regnode(BACK));        /* and loop */
  1168. X        regoptail(ret, ret);            /* back */
  1169. X        regtail(ret, regnode(BRANCH));        /* or */
  1170. X        regtail(ret, regnode(NOTHING));        /* null. */
  1171. X    } else if (op == '+' && (flags&SIMPLE))
  1172. X        reginsert(PLUS, ret);
  1173. X    else if (op == '+') {
  1174. X        /* Emit x+ as x(&|), where & means "self". */
  1175. X        next = regnode(BRANCH);            /* Either */
  1176. X        regtail(ret, next);
  1177. X        regtail(regnode(BACK), ret);        /* loop back */
  1178. X        regtail(next, regnode(BRANCH));        /* or */
  1179. X        regtail(ret, regnode(NOTHING));        /* null. */
  1180. X    } else if (op == '?') {
  1181. X        /* Emit x? as (x|) */
  1182. X        reginsert(BRANCH, ret);            /* Either x */
  1183. X        regtail(ret, regnode(BRANCH));        /* or */
  1184. X        next = regnode(NOTHING);        /* null. */
  1185. X        regtail(ret, next);
  1186. X        regoptail(ret, next);
  1187. X    }
  1188. X    regparse++;
  1189. X    if (ISMULT(*regparse))
  1190. X        FAIL("nested *?+");
  1191. X
  1192. X    return(ret);
  1193. X}
  1194. X
  1195. X/*
  1196. X - regatom - the lowest level
  1197. X *
  1198. X * Optimization:  gobbles an entire sequence of ordinary characters so that
  1199. X * it can turn them into a single node, which is smaller to store and
  1200. X * faster to run.  Backslashed characters are exceptions, each becoming a
  1201. X * separate node; the code is simpler that way and it's not worth fixing.
  1202. X */
  1203. Xstatic char *
  1204. Xregatom(flagp)
  1205. Xint *flagp;
  1206. X{
  1207. X    register char *ret;
  1208. X    int flags;
  1209. X
  1210. X    *flagp = WORST;        /* Tentatively. */
  1211. X
  1212. X    switch (*regparse++) {
  1213. X    case '^':
  1214. X        ret = regnode(BOL);
  1215. X        break;
  1216. X    case '$':
  1217. X        ret = regnode(EOL);
  1218. X        break;
  1219. X    case '.':
  1220. X        ret = regnode(ANY);
  1221. X        *flagp |= HASWIDTH|SIMPLE;
  1222. X        break;
  1223. X    case '[': {
  1224. X            register int class;
  1225. X            register int classend;
  1226. X
  1227. X            if (*regparse == '^') {    /* Complement of range. */
  1228. X                ret = regnode(ANYBUT);
  1229. X                regparse++;
  1230. X            } else
  1231. X                ret = regnode(ANYOF);
  1232. X            if (*regparse == ']' || *regparse == '-')
  1233. X                regc(*regparse++);
  1234. X            while (*regparse != '\0' && *regparse != ']') {
  1235. X                if (*regparse == '-') {
  1236. X                    regparse++;
  1237. X                    if (*regparse == ']' || *regparse == '\0')
  1238. X                        regc('-');
  1239. X                    else {
  1240. X                        class = UCHARAT(regparse-2)+1;
  1241. X                        classend = UCHARAT(regparse);
  1242. X                        if (class > classend+1)
  1243. X                            FAIL("invalid [] range");
  1244. X                        for (; class <= classend; class++)
  1245. X                            regc(class);
  1246. X                        regparse++;
  1247. X                    }
  1248. X                } else
  1249. X                    regc(*regparse++);
  1250. X            }
  1251. X            regc('\0');
  1252. X            if (*regparse != ']')
  1253. X                FAIL("unmatched []");
  1254. X            regparse++;
  1255. X            *flagp |= HASWIDTH|SIMPLE;
  1256. X        }
  1257. X        break;
  1258. X    case '(':
  1259. X        ret = reg(1, &flags);
  1260. X        if (ret == NULL)
  1261. X            return(NULL);
  1262. X        *flagp |= flags&(HASWIDTH|SPSTART);
  1263. X        break;
  1264. X    case '\0':
  1265. X    case '|':
  1266. X    case ')':
  1267. X        FAIL("internal urp");    /* Supposed to be caught earlier. */
  1268. X        break;
  1269. X    case '?':
  1270. X    case '+':
  1271. X    case '*':
  1272. X        FAIL("?+* follows nothing");
  1273. X        break;
  1274. X    case '\\':
  1275. X        if (*regparse == '\0')
  1276. X            FAIL("trailing \\");
  1277. X        ret = regnode(EXACTLY);
  1278. X        regc(*regparse++);
  1279. X        regc('\0');
  1280. X        *flagp |= HASWIDTH|SIMPLE;
  1281. X        break;
  1282. X    default: {
  1283. X            register int len;
  1284. X            register char ender;
  1285. X
  1286. X            regparse--;
  1287. X            len = strcspn(regparse, META);
  1288. X            if (len <= 0)
  1289. X                FAIL("internal disaster");
  1290. X            ender = *(regparse+len);
  1291. X            if (len > 1 && ISMULT(ender))
  1292. X                len--;        /* Back off clear of ?+* operand. */
  1293. X            *flagp |= HASWIDTH;
  1294. X            if (len == 1)
  1295. X                *flagp |= SIMPLE;
  1296. X            ret = regnode(EXACTLY);
  1297. X            while (len > 0) {
  1298. X                regc(*regparse++);
  1299. X                len--;
  1300. X            }
  1301. X            regc('\0');
  1302. X        }
  1303. X        break;
  1304. X    }
  1305. X
  1306. X    return(ret);
  1307. X}
  1308. X
  1309. X/*
  1310. X - regnode - emit a node
  1311. X */
  1312. Xstatic char *            /* Location. */
  1313. Xregnode(op)
  1314. Xchar op;
  1315. X{
  1316. X    register char *ret;
  1317. X    register char *ptr;
  1318. X
  1319. X    ret = regcode;
  1320. X    if (ret == ®dummy) {
  1321. X        regsize += 3;
  1322. X        return(ret);
  1323. X    }
  1324. X
  1325. X    ptr = ret;
  1326. X    *ptr++ = op;
  1327. X    *ptr++ = '\0';        /* Null "next" pointer. */
  1328. X    *ptr++ = '\0';
  1329. X    regcode = ptr;
  1330. X
  1331. X    return(ret);
  1332. X}
  1333. X
  1334. X/*
  1335. X - regc - emit (if appropriate) a byte of code
  1336. X */
  1337. Xstatic void
  1338. Xregc(b)
  1339. Xchar b;
  1340. X{
  1341. X    if (regcode != ®dummy)
  1342. X        *regcode++ = b;
  1343. X    else
  1344. X        regsize++;
  1345. X}
  1346. X
  1347. X/*
  1348. X - reginsert - insert an operator in front of already-emitted operand
  1349. X *
  1350. X * Means relocating the operand.
  1351. X */
  1352. Xstatic void
  1353. Xreginsert(op, opnd)
  1354. Xchar op;
  1355. Xchar *opnd;
  1356. X{
  1357. X    register char *src;
  1358. X    register char *dst;
  1359. X    register char *place;
  1360. X
  1361. X    if (regcode == ®dummy) {
  1362. X        regsize += 3;
  1363. X        return;
  1364. X    }
  1365. X
  1366. X    src = regcode;
  1367. X    regcode += 3;
  1368. X    dst = regcode;
  1369. X    while (src > opnd)
  1370. X        *--dst = *--src;
  1371. X
  1372. X    place = opnd;        /* Op node, where operand used to be. */
  1373. X    *place++ = op;
  1374. X    *place++ = '\0';
  1375. X    *place++ = '\0';
  1376. X}
  1377. X
  1378. X/*
  1379. X - regtail - set the next-pointer at the end of a node chain
  1380. X */
  1381. Xstatic void
  1382. Xregtail(p, val)
  1383. Xchar *p;
  1384. Xchar *val;
  1385. X{
  1386. X    register char *scan;
  1387. X    register char *temp;
  1388. X    register int offset;
  1389. X
  1390. X    if (p == ®dummy)
  1391. X        return;
  1392. X
  1393. X    /* Find last node. */
  1394. X    scan = p;
  1395. X    for (;;) {
  1396. X        temp = regnext(scan);
  1397. X        if (temp == NULL)
  1398. X            break;
  1399. X        scan = temp;
  1400. X    }
  1401. X
  1402. X    if (OP(scan) == BACK)
  1403. X        offset = scan - val;
  1404. X    else
  1405. X        offset = val - scan;
  1406. X    *(scan+1) = (offset>>8)&0377;
  1407. X    *(scan+2) = offset&0377;
  1408. X}
  1409. X
  1410. X/*
  1411. X - regoptail - regtail on operand of first argument; nop if operandless
  1412. X */
  1413. Xstatic void
  1414. Xregoptail(p, val)
  1415. Xchar *p;
  1416. Xchar *val;
  1417. X{
  1418. X    /* "Operandless" and "op != BRANCH" are synonymous in practice. */
  1419. X    if (p == NULL || p == ®dummy || OP(p) != BRANCH)
  1420. X        return;
  1421. X    regtail(OPERAND(p), val);
  1422. X}
  1423. X
  1424. X/*
  1425. X * regexec and friends
  1426. X */
  1427. X
  1428. X/*
  1429. X * Global work variables for regexec().
  1430. X */
  1431. Xstatic char *reginput;        /* String-input pointer. */
  1432. Xstatic char *regbol;        /* Beginning of input, for ^ check. */
  1433. Xstatic char **regstartp;    /* Pointer to startp array. */
  1434. Xstatic char **regendp;        /* Ditto for endp. */
  1435. X
  1436. X/*
  1437. X * Forwards.
  1438. X */
  1439. XSTATIC int regtry();
  1440. XSTATIC int regmatch();
  1441. XSTATIC int regrepeat();
  1442. X
  1443. X#ifdef DEBUG
  1444. Xint regnarrate = 0;
  1445. Xvoid regdump();
  1446. XSTATIC char *regprop();
  1447. X#endif
  1448. X
  1449. X/*
  1450. X - regexec - match a regexp against a string
  1451. X */
  1452. Xint
  1453. Xregexec(prog, string)
  1454. Xregister regexp *prog;
  1455. Xregister char *string;
  1456. X{
  1457. X    register char *s;
  1458. X    extern char *strchr();
  1459. X
  1460. X    /* Be paranoid... */
  1461. X    if (prog == NULL || string == NULL) {
  1462. X        regerror("NULL parameter");
  1463. X        return(0);
  1464. X    }
  1465. X
  1466. X    /* Check validity of program. */
  1467. X    if (UCHARAT(prog->program) != MAGIC) {
  1468. X        regerror("corrupted program");
  1469. X        return(0);
  1470. X    }
  1471. X
  1472. X    /* If there is a "must appear" string, look for it. */
  1473. X    if (prog->regmust != NULL) {
  1474. X        s = string;
  1475. X        while ((s = strchr(s, prog->regmust[0])) != NULL) {
  1476. X            if (strncmp(s, prog->regmust, prog->regmlen) == 0)
  1477. X                break;    /* Found it. */
  1478. X            s++;
  1479. X        }
  1480. X        if (s == NULL)    /* Not present. */
  1481. X            return(0);
  1482. X    }
  1483. X
  1484. X    /* Mark beginning of line for ^ . */
  1485. X    regbol = string;
  1486. X
  1487. X    /* Simplest case:  anchored match need be tried only once. */
  1488. X    if (prog->reganch)
  1489. X        return(regtry(prog, string));
  1490. X
  1491. X    /* Messy cases:  unanchored match. */
  1492. X    s = string;
  1493. X    if (prog->regstart != '\0')
  1494. X        /* We know what char it must start with. */
  1495. X        while ((s = strchr(s, prog->regstart)) != NULL) {
  1496. X            if (regtry(prog, s))
  1497. X                return(1);
  1498. X            s++;
  1499. X        }
  1500. X    else
  1501. X        /* We don't -- general case. */
  1502. X        do {
  1503. X            if (regtry(prog, s))
  1504. X                return(1);
  1505. X        } while (*s++ != '\0');
  1506. X
  1507. X    /* Failure. */
  1508. X    return(0);
  1509. X}
  1510. X
  1511. X/*
  1512. X - regtry - try match at specific point
  1513. X */
  1514. Xstatic int            /* 0 failure, 1 success */
  1515. Xregtry(prog, string)
  1516. Xregexp *prog;
  1517. Xchar *string;
  1518. X{
  1519. X    register int i;
  1520. X    register char **sp;
  1521. X    register char **ep;
  1522. X
  1523. X    reginput = string;
  1524. X    regstartp = prog->startp;
  1525. X    regendp = prog->endp;
  1526. X
  1527. X    sp = prog->startp;
  1528. X    ep = prog->endp;
  1529. X    for (i = NSUBEXP; i > 0; i--) {
  1530. X        *sp++ = NULL;
  1531. X        *ep++ = NULL;
  1532. X    }
  1533. X    if (regmatch(prog->program + 1)) {
  1534. X        prog->startp[0] = string;
  1535. X        prog->endp[0] = reginput;
  1536. X        return(1);
  1537. X    } else
  1538. X        return(0);
  1539. X}
  1540. X
  1541. X/*
  1542. X - regmatch - main matching routine
  1543. X *
  1544. X * Conceptually the strategy is simple:  check to see whether the current
  1545. X * node matches, call self recursively to see whether the rest matches,
  1546. X * and then act accordingly.  In practice we make some effort to avoid
  1547. X * recursion, in particular by going through "ordinary" nodes (that don't
  1548. X * need to know whether the rest of the match failed) by a loop instead of
  1549. X * by recursion.
  1550. X */
  1551. Xstatic int            /* 0 failure, 1 success */
  1552. Xregmatch(prog)
  1553. Xchar *prog;
  1554. X{
  1555. X    register char *scan;    /* Current node. */
  1556. X    char *next;        /* Next node. */
  1557. X    extern char *strchr();
  1558. X
  1559. X    scan = prog;
  1560. X#ifdef DEBUG
  1561. X    if (scan != NULL && regnarrate)
  1562. X        fprintf(stderr, "%s(\n", regprop(scan));
  1563. X#endif
  1564. X    while (scan != NULL) {
  1565. X#ifdef DEBUG
  1566. X        if (regnarrate)
  1567. X            fprintf(stderr, "%s...\n", regprop(scan));
  1568. X#endif
  1569. X        next = regnext(scan);
  1570. X
  1571. X        switch (OP(scan)) {
  1572. X        case BOL:
  1573. X            if (reginput != regbol)
  1574. X                return(0);
  1575. X            break;
  1576. X        case EOL:
  1577. X            if (*reginput != '\0')
  1578. X                return(0);
  1579. X            break;
  1580. X        case ANY:
  1581. X            if (*reginput == '\0')
  1582. X                return(0);
  1583. X            reginput++;
  1584. X            break;
  1585. X        case EXACTLY: {
  1586. X                register int len;
  1587. X                register char *opnd;
  1588. X
  1589. X                opnd = OPERAND(scan);
  1590. X                /* Inline the first character, for speed. */
  1591. X                if (*opnd != *reginput)
  1592. X                    return(0);
  1593. X                len = strlen(opnd);
  1594. X                if (len > 1 && strncmp(opnd, reginput, len) != 0)
  1595. X                    return(0);
  1596. X                reginput += len;
  1597. X            }
  1598. X            break;
  1599. X        case ANYOF:
  1600. X            if (*reginput == '\0' || strchr(OPERAND(scan), *reginput) == NULL)
  1601. X                return(0);
  1602. X            reginput++;
  1603. X            break;
  1604. X        case ANYBUT:
  1605. X            if (*reginput == '\0' || strchr(OPERAND(scan), *reginput) != NULL)
  1606. X                return(0);
  1607. X            reginput++;
  1608. X            break;
  1609. X        case NOTHING:
  1610. X            break;
  1611. X        case BACK:
  1612. X            break;
  1613. X        case OPEN+1:
  1614. X        case OPEN+2:
  1615. X        case OPEN+3:
  1616. X        case OPEN+4:
  1617. X        case OPEN+5:
  1618. X        case OPEN+6:
  1619. X        case OPEN+7:
  1620. X        case OPEN+8:
  1621. X        case OPEN+9: {
  1622. X                register int no;
  1623. X                register char *save;
  1624. X
  1625. X                no = OP(scan) - OPEN;
  1626. X                save = reginput;
  1627. X
  1628. X                if (regmatch(next)) {
  1629. X                    /*
  1630. X                     * Don't set startp if some later
  1631. X                     * invocation of the same parentheses
  1632. X                     * already has.
  1633. X                     */
  1634. X                    if (regstartp[no] == NULL)
  1635. X                        regstartp[no] = save;
  1636. X                    return(1);
  1637. X                } else
  1638. X                    return(0);
  1639. X            }
  1640. X            break;
  1641. X        case CLOSE+1:
  1642. X        case CLOSE+2:
  1643. X        case CLOSE+3:
  1644. X        case CLOSE+4:
  1645. X        case CLOSE+5:
  1646. X        case CLOSE+6:
  1647. X        case CLOSE+7:
  1648. X        case CLOSE+8:
  1649. X        case CLOSE+9: {
  1650. X                register int no;
  1651. X                register char *save;
  1652. X
  1653. X                no = OP(scan) - CLOSE;
  1654. X                save = reginput;
  1655. X
  1656. X                if (regmatch(next)) {
  1657. X                    /*
  1658. X                     * Don't set endp if some later
  1659. X                     * invocation of the same parentheses
  1660. X                     * already has.
  1661. X                     */
  1662. X                    if (regendp[no] == NULL)
  1663. X                        regendp[no] = save;
  1664. X                    return(1);
  1665. X                } else
  1666. X                    return(0);
  1667. X            }
  1668. X            break;
  1669. X        case BRANCH: {
  1670. X                register char *save;
  1671. X
  1672. X                if (OP(next) != BRANCH)        /* No choice. */
  1673. X                    next = OPERAND(scan);    /* Avoid recursion. */
  1674. X                else {
  1675. X                    do {
  1676. X                        save = reginput;
  1677. X                        if (regmatch(OPERAND(scan)))
  1678. X                            return(1);
  1679. X                        reginput = save;
  1680. X                        scan = regnext(scan);
  1681. X                    } while (scan != NULL && OP(scan) == BRANCH);
  1682. X                    return(0);
  1683. X                    /* NOTREACHED */
  1684. X                }
  1685. X            }
  1686. X            break;
  1687. X        case STAR:
  1688. X        case PLUS: {
  1689. X                register char nextch;
  1690. X                register int no;
  1691. X                register char *save;
  1692. X                register int min;
  1693. X
  1694. X                /*
  1695. X                 * Lookahead to avoid useless match attempts
  1696. X                 * when we know what character comes next.
  1697. X                 */
  1698. X                nextch = '\0';
  1699. X                if (OP(next) == EXACTLY)
  1700. X                    nextch = *OPERAND(next);
  1701. X                min = (OP(scan) == STAR) ? 0 : 1;
  1702. X                save = reginput;
  1703. X                no = regrepeat(OPERAND(scan));
  1704. X                while (no >= min) {
  1705. X                    /* If it could work, try it. */
  1706. X                    if (nextch == '\0' || *reginput == nextch)
  1707. X                        if (regmatch(next))
  1708. X                            return(1);
  1709. X                    /* Couldn't or didn't -- back up. */
  1710. X                    no--;
  1711. X                    reginput = save + no;
  1712. X                }
  1713. X                return(0);
  1714. X            }
  1715. X            break;
  1716. X        case END:
  1717. X            return(1);    /* Success! */
  1718. X            break;
  1719. X        default:
  1720. X            regerror("memory corruption");
  1721. X            return(0);
  1722. X            break;
  1723. X        }
  1724. X
  1725. X        scan = next;
  1726. X    }
  1727. X
  1728. X    /*
  1729. X     * We get here only if there's trouble -- normally "case END" is
  1730. X     * the terminating point.
  1731. X     */
  1732. X    regerror("corrupted pointers");
  1733. X    return(0);
  1734. X}
  1735. X
  1736. X/*
  1737. X - regrepeat - repeatedly match something simple, report how many
  1738. X */
  1739. Xstatic int
  1740. Xregrepeat(p)
  1741. Xchar *p;
  1742. X{
  1743. X    register int count = 0;
  1744. X    register char *scan;
  1745. X    register char *opnd;
  1746. X
  1747. X    scan = reginput;
  1748. X    opnd = OPERAND(p);
  1749. X    switch (OP(p)) {
  1750. X    case ANY:
  1751. X        count = strlen(scan);
  1752. X        scan += count;
  1753. X        break;
  1754. X    case EXACTLY:
  1755. X        while (*opnd == *scan) {
  1756. X            count++;
  1757. X            scan++;
  1758. X        }
  1759. X        break;
  1760. X    case ANYOF:
  1761. X        while (*scan != '\0' && strchr(opnd, *scan) != NULL) {
  1762. X            count++;
  1763. X            scan++;
  1764. X        }
  1765. X        break;
  1766. X    case ANYBUT:
  1767. X        while (*scan != '\0' && strchr(opnd, *scan) == NULL) {
  1768. X            count++;
  1769. X            scan++;
  1770. X        }
  1771. X        break;
  1772. X    default:        /* Oh dear.  Called inappropriately. */
  1773. X        regerror("internal foulup");
  1774. X        count = 0;    /* Best compromise. */
  1775. X        break;
  1776. X    }
  1777. X    reginput = scan;
  1778. X
  1779. X    return(count);
  1780. X}
  1781. X
  1782. X/*
  1783. X - regnext - dig the "next" pointer out of a node
  1784. X */
  1785. Xstatic char *
  1786. Xregnext(p)
  1787. Xregister char *p;
  1788. X{
  1789. X    register int offset;
  1790. X
  1791. X    if (p == ®dummy)
  1792. X        return(NULL);
  1793. X
  1794. X    offset = NEXT(p);
  1795. X    if (offset == 0)
  1796. X        return(NULL);
  1797. X
  1798. X    if (OP(p) == BACK)
  1799. X        return(p-offset);
  1800. X    else
  1801. X        return(p+offset);
  1802. X}
  1803. X
  1804. X#ifdef DEBUG
  1805. X
  1806. XSTATIC char *regprop();
  1807. X
  1808. X/*
  1809. X - regdump - dump a regexp onto stdout in vaguely comprehensible form
  1810. X */
  1811. Xvoid
  1812. Xregdump(r)
  1813. Xregexp *r;
  1814. X{
  1815. X    register char *s;
  1816. X    register char op = EXACTLY;    /* Arbitrary non-END op. */
  1817. X    register char *next;
  1818. X    extern char *strchr();
  1819. X
  1820. X
  1821. X    s = r->program + 1;
  1822. X    while (op != END) {    /* While that wasn't END last time... */
  1823. X        op = OP(s);
  1824. X        printf("%2d%s", s-r->program, regprop(s));    /* Where, what. */
  1825. X        next = regnext(s);
  1826. X        if (next == NULL)        /* Next ptr. */
  1827. X            printf("(0)");
  1828. X        else 
  1829. X            printf("(%d)", (s-r->program)+(next-s));
  1830. X        s += 3;
  1831. X        if (op == ANYOF || op == ANYBUT || op == EXACTLY) {
  1832. X            /* Literal string, where present. */
  1833. X            while (*s != '\0') {
  1834. X                putchar(*s);
  1835. X                s++;
  1836. X            }
  1837. X            s++;
  1838. X        }
  1839. X        putchar('\n');
  1840. X    }
  1841. X
  1842. X    /* Header fields of interest. */
  1843. X    if (r->regstart != '\0')
  1844. X        printf("start `%c' ", r->regstart);
  1845. X    if (r->reganch)
  1846. X        printf("anchored ");
  1847. X    if (r->regmust != NULL)
  1848. X        printf("must have \"%s\"", r->regmust);
  1849. X    printf("\n");
  1850. X}
  1851. X
  1852. X/*
  1853. X - regprop - printable representation of opcode
  1854. X */
  1855. Xstatic char *
  1856. Xregprop(op)
  1857. Xchar *op;
  1858. X{
  1859. X    register char *p;
  1860. X    static char buf[50];
  1861. X
  1862. X    (void) strcpy(buf, ":");
  1863. X
  1864. X    switch (OP(op)) {
  1865. X    case BOL:
  1866. X        p = "BOL";
  1867. X        break;
  1868. X    case EOL:
  1869. X        p = "EOL";
  1870. X        break;
  1871. X    case ANY:
  1872. X        p = "ANY";
  1873. X        break;
  1874. X    case ANYOF:
  1875. X        p = "ANYOF";
  1876. X        break;
  1877. X    case ANYBUT:
  1878. X        p = "ANYBUT";
  1879. X        break;
  1880. X    case BRANCH:
  1881. X        p = "BRANCH";
  1882. X        break;
  1883. X    case EXACTLY:
  1884. X        p = "EXACTLY";
  1885. X        break;
  1886. X    case NOTHING:
  1887. X        p = "NOTHING";
  1888. X        break;
  1889. X    case BACK:
  1890. X        p = "BACK";
  1891. X        break;
  1892. X    case END:
  1893. X        p = "END";
  1894. X        break;
  1895. X    case OPEN+1:
  1896. X    case OPEN+2:
  1897. X    case OPEN+3:
  1898. X    case OPEN+4:
  1899. X    case OPEN+5:
  1900. X    case OPEN+6:
  1901. X    case OPEN+7:
  1902. X    case OPEN+8:
  1903. X    case OPEN+9:
  1904. X        sprintf(buf+strlen(buf), "OPEN%d", OP(op)-OPEN);
  1905. X        p = NULL;
  1906. X        break;
  1907. X    case CLOSE+1:
  1908. X    case CLOSE+2:
  1909. X    case CLOSE+3:
  1910. X    case CLOSE+4:
  1911. X    case CLOSE+5:
  1912. X    case CLOSE+6:
  1913. X    case CLOSE+7:
  1914. X    case CLOSE+8:
  1915. X    case CLOSE+9:
  1916. X        sprintf(buf+strlen(buf), "CLOSE%d", OP(op)-CLOSE);
  1917. X        p = NULL;
  1918. X        break;
  1919. X    case STAR:
  1920. X        p = "STAR";
  1921. X        break;
  1922. X    case PLUS:
  1923. X        p = "PLUS";
  1924. X        break;
  1925. X    default:
  1926. X        regerror("corrupted opcode");
  1927. X        break;
  1928. X    }
  1929. X    if (p != NULL)
  1930. X        (void) strcat(buf, p);
  1931. X    return(buf);
  1932. X}
  1933. X#endif
  1934. X
  1935. X/*
  1936. X * The following is provided for those people who do not have strcspn() in
  1937. X * their C libraries.  They should get off their butts and do something
  1938. X * about it; at least one public-domain implementation of those (highly
  1939. X * useful) string routines has been published on Usenet.
  1940. X */
  1941. X#ifdef STRCSPN
  1942. X/*
  1943. X * strcspn - find length of initial segment of s1 consisting entirely
  1944. X * of characters not from s2
  1945. X */
  1946. X
  1947. Xstatic int
  1948. Xstrcspn(s1, s2)
  1949. Xchar *s1;
  1950. Xchar *s2;
  1951. X{
  1952. X    register char *scan1;
  1953. X    register char *scan2;
  1954. X    register int count;
  1955. X
  1956. X    count = 0;
  1957. X    for (scan1 = s1; *scan1 != '\0'; scan1++) {
  1958. X        for (scan2 = s2; *scan2 != '\0';)    /* ++ moved down. */
  1959. X            if (*scan1 == *scan2++)
  1960. X                return(count);
  1961. X        count++;
  1962. X    }
  1963. X    return(count);
  1964. X}
  1965. X#endif
  1966. END_OF_FILE
  1967.   if test 27614 -ne `wc -c <'regexp.c'`; then
  1968.     echo shar: \"'regexp.c'\" unpacked with wrong size!
  1969.   fi
  1970.   # end of 'regexp.c'
  1971. fi
  1972. if test -f 'regexp.h' -a "${1}" != "-c" ; then 
  1973.   echo shar: Will not clobber existing file \"'regexp.h'\"
  1974. else
  1975.   echo shar: Extracting \"'regexp.h'\" \(574 characters\)
  1976.   sed "s/^X//" >'regexp.h' <<'END_OF_FILE'
  1977. X/*
  1978. X * Definitions etc. for regexp(3) routines.
  1979. X *
  1980. X * Caveat:  this is V8 regexp(3) [actually, a reimplementation thereof],
  1981. X * not the System V one.
  1982. X */
  1983. X#define NSUBEXP  10
  1984. Xtypedef struct regexp {
  1985. X    char *startp[NSUBEXP];
  1986. X    char *endp[NSUBEXP];
  1987. X    char regstart;        /* Internal use only. */
  1988. X    char reganch;        /* Internal use only. */
  1989. X    char *regmust;        /* Internal use only. */
  1990. X    int regmlen;        /* Internal use only. */
  1991. X    char program[1];    /* Unwarranted chumminess with compiler. */
  1992. X} regexp;
  1993. X
  1994. Xextern regexp *regcomp();
  1995. Xextern int regexec();
  1996. Xextern void regsub();
  1997. Xextern void regerror();
  1998. END_OF_FILE
  1999.   if test 574 -ne `wc -c <'regexp.h'`; then
  2000.     echo shar: \"'regexp.h'\" unpacked with wrong size!
  2001.   fi
  2002.   # end of 'regexp.h'
  2003. fi
  2004. if test -f 'regmagic.h' -a "${1}" != "-c" ; then 
  2005.   echo shar: Will not clobber existing file \"'regmagic.h'\"
  2006. else
  2007.   echo shar: Extracting \"'regmagic.h'\" \(153 characters\)
  2008.   sed "s/^X//" >'regmagic.h' <<'END_OF_FILE'
  2009. X/*
  2010. X * The first byte of the regexp internal "program" is actually this magic
  2011. X * number; the start node begins in the second byte.
  2012. X */
  2013. X#define    MAGIC    0234
  2014. END_OF_FILE
  2015.   if test 153 -ne `wc -c <'regmagic.h'`; then
  2016.     echo shar: \"'regmagic.h'\" unpacked with wrong size!
  2017.   fi
  2018.   # end of 'regmagic.h'
  2019. fi
  2020. if test -f 'regsub.c' -a "${1}" != "-c" ; then 
  2021.   echo shar: Will not clobber existing file \"'regsub.c'\"
  2022. else
  2023.   echo shar: Extracting \"'regsub.c'\" \(1961 characters\)
  2024.   sed "s/^X//" >'regsub.c' <<'END_OF_FILE'
  2025. X/*
  2026. X * regsub
  2027. X *
  2028. X *    Copyright (c) 1986 by University of Toronto.
  2029. X *    Written by Henry Spencer.  Not derived from licensed software.
  2030. X *
  2031. X *    Permission is granted to anyone to use this software for any
  2032. X *    purpose on any computer system, and to redistribute it freely,
  2033. X *    subject to the following restrictions:
  2034. X *
  2035. X *    1. The author is not responsible for the consequences of use of
  2036. X *        this software, no matter how awful, even if they arise
  2037. X *        from defects in it.
  2038. X *
  2039. X *    2. The origin of this software must not be misrepresented, either
  2040. X *        by explicit claim or by omission.
  2041. X *
  2042. X *    3. Altered versions must be plainly marked as such, and must not
  2043. X *        be misrepresented as being the original software.
  2044. X */
  2045. X#include <stdio.h>
  2046. X#include <regexp.h>
  2047. X#include "regmagic.h"
  2048. X
  2049. X#ifndef CHARBITS
  2050. X#define    UCHARAT(p)    ((int)*(unsigned char *)(p))
  2051. X#else
  2052. X#define    UCHARAT(p)    ((int)*(p)&CHARBITS)
  2053. X#endif
  2054. X
  2055. X/*
  2056. X - regsub - perform substitutions after a regexp match
  2057. X */
  2058. Xvoid
  2059. Xregsub(prog, source, dest)
  2060. Xregexp *prog;
  2061. Xchar *source;
  2062. Xchar *dest;
  2063. X{
  2064. X    register char *src;
  2065. X    register char *dst;
  2066. X    register char c;
  2067. X    register int no;
  2068. X    register int len;
  2069. X    extern char *strncpy();
  2070. X
  2071. X    if (prog == NULL || source == NULL || dest == NULL) {
  2072. X        regerror("NULL parm to regsub");
  2073. X        return;
  2074. X    }
  2075. X    if (UCHARAT(prog->program) != MAGIC) {
  2076. X        regerror("damaged regexp fed to regsub");
  2077. X        return;
  2078. X    }
  2079. X
  2080. X    src = source;
  2081. X    dst = dest;
  2082. X    while ((c = *src++) != '\0') {
  2083. X        if (c == '&')
  2084. X            no = 0;
  2085. X        else if (c == '\\' && '0' <= *src && *src <= '9')
  2086. X            no = *src++ - '0';
  2087. X        else
  2088. X            no = -1;
  2089. X
  2090. X        if (no < 0) {    /* Ordinary character. */
  2091. X            if (c == '\\' && (*src == '\\' || *src == '&'))
  2092. X                c = *src++;
  2093. X            *dst++ = c;
  2094. X        }
  2095. X        else if (prog->startp[no] != NULL && prog->endp[no] != NULL) {
  2096. X            len = prog->endp[no] - prog->startp[no];
  2097. X            (void) strncpy(dst, prog->startp[no], len);
  2098. X            dst += len;
  2099. X            if (len != 0 && *(dst-1) == '\0') {        /* strncpy hit NUL. */
  2100. X                regerror("damaged match string");
  2101. X                return;
  2102. X            }
  2103. X        }
  2104. X    }
  2105. X    *dst++ = '\0';
  2106. X}
  2107. END_OF_FILE
  2108.   if test 1961 -ne `wc -c <'regsub.c'`; then
  2109.     echo shar: \"'regsub.c'\" unpacked with wrong size!
  2110.   fi
  2111.   # end of 'regsub.c'
  2112. fi
  2113. echo shar: End of archive 1 \(of 1\).
  2114. cp /dev/null ark1isdone
  2115. MISSING=""
  2116. for I in 1 ; do
  2117.     if test ! -f ark${I}isdone ; then
  2118.     MISSING="${MISSING} ${I}"
  2119.     fi
  2120. done
  2121. if test "${MISSING}" = "" ; then
  2122.     echo You have the archive.
  2123.     rm -f ark[1-9]isdone
  2124. else
  2125.     echo You still must unpack the following archives:
  2126.     echo "        " ${MISSING}
  2127. fi
  2128. exit 0
  2129. exit 0 # Just in case...
  2130.