home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / sources / misc / 3926 < prev    next >
Encoding:
Text File  |  1992-09-11  |  53.4 KB  |  2,135 lines

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