home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional / OS2PRO194.ISO / os2 / fileutil / cawf / regexp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-02-21  |  28.2 KB  |  1,219 lines

  1. /*
  2.  * regcomp and regexec -- regsub and regerror are elsewhere
  3.  *
  4.  *    Copyright (c) 1986 by University of Toronto.
  5.  *    Written by Henry Spencer.  Not derived from licensed software.
  6.  *
  7.  *    Permission is granted to anyone to use this software for any
  8.  *    purpose on any computer system, and to redistribute it freely,
  9.  *    subject to the following restrictions:
  10.  *
  11.  *    1. The author is not responsible for the consequences of use of
  12.  *        this software, no matter how awful, even if they arise
  13.  *        from defects in it.
  14.  *
  15.  *    2. The origin of this software must not be misrepresented, either
  16.  *        by explicit claim or by omission.
  17.  *
  18.  *    3. Altered versions must be plainly marked as such, and must not
  19.  *        be misrepresented as being the original software.
  20.  *
  21.  * Beware that some of this code is subtly aware of the way operator
  22.  * precedence is structured in regular expressions.  Serious changes in
  23.  * regular-expression syntax might require a total rethink.
  24.  */
  25. #include <stdio.h>
  26. #ifdef    UNIX
  27. #include <strings.h>
  28. #else
  29. #include <string.h>
  30. #endif
  31. #include "regexp.h"
  32. #include "regmagic.h"
  33.  
  34. /*
  35.  * The "internal use only" fields in regexp.h are present to pass info from
  36.  * compile to execute that permits the execute phase to run lots faster on
  37.  * simple cases.  They are:
  38.  *
  39.  * regstart    char that must begin a match; '\0' if none obvious
  40.  * reganch    is the match anchored (at beginning-of-line only)?
  41.  * regmust    string (pointer into program) that match must include, or NULL
  42.  * regmlen    length of regmust string
  43.  *
  44.  * Regstart and reganch permit very fast decisions on suitable starting points
  45.  * for a match, cutting down the work a lot.  Regmust permits fast rejection
  46.  * of lines that cannot possibly match.  The regmust tests are costly enough
  47.  * that regcomp() supplies a regmust only if the r.e. contains something
  48.  * potentially expensive (at present, the only such thing detected is * or +
  49.  * at the start of the r.e., which can involve a lot of backup).  Regmlen is
  50.  * supplied because the test in regexec() needs it and regcomp() is computing
  51.  * it anyway.
  52.  */
  53.  
  54. /*
  55.  * Structure for regexp "program".  This is essentially a linear encoding
  56.  * of a nondeterministic finite-state machine (aka syntax charts or
  57.  * "railroad normal form" in parsing technology).  Each node is an opcode
  58.  * plus a "next" pointer, possibly plus an operand.  "Next" pointers of
  59.  * all nodes except BRANCH implement concatenation; a "next" pointer with
  60.  * a BRANCH on both ends of it is connecting two alternatives.  (Here we
  61.  * have one of the subtle syntax dependencies:  an individual BRANCH (as
  62.  * opposed to a collection of them) is never concatenated with anything
  63.  * because of operator precedence.)  The operand of some types of node is
  64.  * a literal string; for others, it is a node leading into a sub-FSM.  In
  65.  * particular, the operand of a BRANCH node is the first node of the branch.
  66.  * (NB this is *not* a tree structure:  the tail of the branch connects
  67.  * to the thing following the set of BRANCHes.)  The opcodes are:
  68.  */
  69.  
  70. /* definition    number    opnd?    meaning */
  71. #define    END    0    /* no    End of program. */
  72. #define    BOL    1    /* no    Match "" at beginning of line. */
  73. #define    EOL    2    /* no    Match "" at end of line. */
  74. #define    ANY    3    /* no    Match any one character. */
  75. #define    ANYOF    4    /* str    Match any character in this string. */
  76. #define    ANYBUT    5    /* str    Match any character not in this string. */
  77. #define    BRANCH    6    /* node    Match this alternative, or the next... */
  78. #define    BACK    7    /* no    Match "", "next" ptr points backward. */
  79. #define    EXACTLY    8    /* str    Match this string. */
  80. #define    NOTHING    9    /* no    Match empty string. */
  81. #define    STAR    10    /* node    Match this (simple) thing 0 or more times. */
  82. #define    PLUS    11    /* node    Match this (simple) thing 1 or more times. */
  83. #define    OPEN    20    /* no    Mark this point in input as start of #n. */
  84.             /*    OPEN+1 is number 1, etc. */
  85. #define    CLOSE    30    /* no    Analogous to OPEN. */
  86.  
  87. /*
  88.  * Opcode notes:
  89.  *
  90.  * BRANCH    The set of branches constituting a single choice are hooked
  91.  *        together with their "next" pointers, since precedence prevents
  92.  *        anything being concatenated to any individual branch.  The
  93.  *        "next" pointer of the last BRANCH in a choice points to the
  94.  *        thing following the whole choice.  This is also where the
  95.  *        final "next" pointer of each individual branch points; each
  96.  *        branch starts with the operand node of a BRANCH node.
  97.  *
  98.  * BACK        Normal "next" pointers all implicitly point forward; BACK
  99.  *        exists to make loop structures possible.
  100.  *
  101.  * STAR,PLUS    '?', and complex '*' and '+', are implemented as circular
  102.  *        BRANCH structures using BACK.  Simple cases (one character
  103.  *        per match) are implemented with STAR and PLUS for speed
  104.  *        and to minimize recursive plunges.
  105.  *
  106.  * OPEN,CLOSE    ...are numbered at compile time.
  107.  */
  108.  
  109. /*
  110.  * A node is one char of opcode followed by two chars of "next" pointer.
  111.  * "Next" pointers are stored as two 8-bit pieces, high order first.  The
  112.  * value is a positive offset from the opcode of the node containing it.
  113.  * An operand, if any, simply follows the node.  (Note that much of the
  114.  * code generation knows about this implicit relationship.)
  115.  *
  116.  * Using two bytes for the "next" pointer is vast overkill for most things,
  117.  * but allows patterns to get big without disasters.
  118.  */
  119. #define    OP(p)    (*(p))
  120. #define    NEXT(p)    (((*((p)+1)&0377)<<8) + *((p)+2)&0377)
  121. #define    OPERAND(p)    ((p) + 3)
  122.  
  123. /*
  124.  * See regmagic.h for one further detail of program structure.
  125.  */
  126.  
  127.  
  128. /*
  129.  * Utility definitions.
  130.  */
  131. #ifndef CHARBITS
  132. #define    UCHARAT(p)    ((int)*(unsigned char *)(p))
  133. #else
  134. #define    UCHARAT(p)    ((int)*(p)&CHARBITS)
  135. #endif
  136.  
  137. #define    FAIL(m)    { regerror(m); return(NULL); }
  138. #define    ISMULT(c)    ((c) == '*' || (c) == '+' || (c) == '?')
  139. #define    META    "^$.[()|?+*\\"
  140.  
  141. /*
  142.  * Flags to be passed up and down.
  143.  */
  144. #define    HASWIDTH    01    /* Known never to match null string. */
  145. #define    SIMPLE        02    /* Simple enough to be STAR/PLUS operand. */
  146. #define    SPSTART        04    /* Starts with * or +. */
  147. #define    WORST        0    /* Worst case. */
  148.  
  149. /*
  150.  * Global work variables for regcomp().
  151.  */
  152. static char *regparse;        /* Input-scan pointer. */
  153. static int regnpar;        /* () count. */
  154. static char regdummy;
  155. static char *regcode;        /* Code-emit pointer; ®dummy = don't. */
  156. static long regsize;        /* Code size. */
  157.  
  158. /*
  159.  * Forward declarations for regcomp()'s friends.
  160.  */
  161. #ifndef STATIC
  162. #define    STATIC    static
  163. #endif
  164. STATIC char *reg();
  165. STATIC char *regbranch();
  166. STATIC char *regpiece();
  167. STATIC char *regatom();
  168. STATIC char *regnode();
  169. STATIC char *regnext();
  170. STATIC void regc();
  171. STATIC void reginsert();
  172. STATIC void regtail();
  173. STATIC void regoptail();
  174. #ifdef STRCSPN
  175. STATIC int strcspn();
  176. #endif
  177.  
  178. /*
  179.  - regcomp - compile a regular expression into internal code
  180.  *
  181.  * We can't allocate space until we know how big the compiled form will be,
  182.  * but we can't compile it (and thus know how big it is) until we've got a
  183.  * place to put the code.  So we cheat:  we compile it twice, once with code
  184.  * generation turned off and size counting turned on, and once "for real".
  185.  * This also means that we don't allocate space until we are sure that the
  186.  * thing really will compile successfully, and we never have to move the
  187.  * code and thus invalidate pointers into it.  (Note that it has to be in
  188.  * one piece because free() must be able to free it all.)
  189.  *
  190.  * Beware that the optimization-preparation code in here knows about some
  191.  * of the structure of the compiled regexp.
  192.  */
  193. regexp *
  194. regcomp(exp)
  195. char *exp;
  196. {
  197.     register regexp *r;
  198.     register char *scan;
  199.     register char *longest;
  200.     register int len;
  201.     int flags;
  202.     extern char *malloc();
  203.  
  204.     if (exp == NULL)
  205.         FAIL("NULL argument");
  206.  
  207.     /* First pass: determine size, legality. */
  208.     regparse = exp;
  209.     regnpar = 1;
  210.     regsize = 0L;
  211.     regcode = ®dummy;
  212.     regc(MAGIC);
  213.     if (reg(0, &flags) == NULL)
  214.         return(NULL);
  215.  
  216.     /* Small enough for pointer-storage convention? */
  217.     if (regsize >= 32767L)        /* Probably could be 65535L. */
  218.         FAIL("regexp too big");
  219.  
  220.     /* Allocate space. */
  221.     r = (regexp *)malloc(sizeof(regexp) + (unsigned)regsize);
  222.     if (r == NULL)
  223.         FAIL("out of space");
  224.  
  225.     /* Second pass: emit code. */
  226.     regparse = exp;
  227.     regnpar = 1;
  228.     regcode = r->program;
  229.     regc(MAGIC);
  230.     if (reg(0, &flags) == NULL)
  231.         return(NULL);
  232.  
  233.     /* Dig out information for optimizations. */
  234.     r->regstart = '\0';    /* Worst-case defaults. */
  235.     r->reganch = 0;
  236.     r->regmust = NULL;
  237.     r->regmlen = 0;
  238.     scan = r->program+1;            /* First BRANCH. */
  239.     if (OP(regnext(scan)) == END) {        /* Only one top-level choice. */
  240.         scan = OPERAND(scan);
  241.  
  242.         /* Starting-point info. */
  243.         if (OP(scan) == EXACTLY)
  244.             r->regstart = *OPERAND(scan);
  245.         else if (OP(scan) == BOL)
  246.             r->reganch++;
  247.  
  248.         /*
  249.          * If there's something expensive in the r.e., find the
  250.          * longest literal string that must appear and make it the
  251.          * regmust.  Resolve ties in favor of later strings, since
  252.          * the regstart check works with the beginning of the r.e.
  253.          * and avoiding duplication strengthens checking.  Not a
  254.          * strong reason, but sufficient in the absence of others.
  255.          */
  256.         if (flags&SPSTART) {
  257.             longest = NULL;
  258.             len = 0;
  259.             for (; scan != NULL; scan = regnext(scan))
  260.                 if (OP(scan) == EXACTLY && strlen(OPERAND(scan)) >= len) {
  261.                     longest = OPERAND(scan);
  262.                     len = strlen(OPERAND(scan));
  263.                 }
  264.             r->regmust = longest;
  265.             r->regmlen = len;
  266.         }
  267.     }
  268.  
  269.     return(r);
  270. }
  271.  
  272. /*
  273.  - reg - regular expression, i.e. main body or parenthesized thing
  274.  *
  275.  * Caller must absorb opening parenthesis.
  276.  *
  277.  * Combining parenthesis handling with the base level of regular expression
  278.  * is a trifle forced, but the need to tie the tails of the branches to what
  279.  * follows makes it hard to avoid.
  280.  */
  281. static char *
  282. reg(paren, flagp)
  283. int paren;            /* Parenthesized? */
  284. int *flagp;
  285. {
  286.     register char *ret;
  287.     register char *br;
  288.     register char *ender;
  289.     register int parno;
  290.     int flags;
  291.  
  292.     *flagp = HASWIDTH;    /* Tentatively. */
  293.  
  294.     /* Make an OPEN node, if parenthesized. */
  295.     if (paren) {
  296.         if (regnpar >= NSUBEXP)
  297.             FAIL("too many ()");
  298.         parno = regnpar;
  299.         regnpar++;
  300.         ret = regnode(OPEN+parno);
  301.     } else
  302.         ret = NULL;
  303.  
  304.     /* Pick up the branches, linking them together. */
  305.     br = regbranch(&flags);
  306.     if (br == NULL)
  307.         return(NULL);
  308.     if (ret != NULL)
  309.         regtail(ret, br);    /* OPEN -> first. */
  310.     else
  311.         ret = br;
  312.     if (!(flags&HASWIDTH))
  313.         *flagp &= ~HASWIDTH;
  314.     *flagp |= flags&SPSTART;
  315.     while (*regparse == '|') {
  316.         regparse++;
  317.         br = regbranch(&flags);
  318.         if (br == NULL)
  319.             return(NULL);
  320.         regtail(ret, br);    /* BRANCH -> BRANCH. */
  321.         if (!(flags&HASWIDTH))
  322.             *flagp &= ~HASWIDTH;
  323.         *flagp |= flags&SPSTART;
  324.     }
  325.  
  326.     /* Make a closing node, and hook it on the end. */
  327.     ender = regnode((paren) ? CLOSE+parno : END);    
  328.     regtail(ret, ender);
  329.  
  330.     /* Hook the tails of the branches to the closing node. */
  331.     for (br = ret; br != NULL; br = regnext(br))
  332.         regoptail(br, ender);
  333.  
  334.     /* Check for proper termination. */
  335.     if (paren && *regparse++ != ')') {
  336.         FAIL("unmatched ()");
  337.     } else if (!paren && *regparse != '\0') {
  338.         if (*regparse == ')') {
  339.             FAIL("unmatched ()");
  340.         } else
  341.             FAIL("junk on end");    /* "Can't happen". */
  342.         /* NOTREACHED */
  343.     }
  344.  
  345.     return(ret);
  346. }
  347.  
  348. /*
  349.  - regbranch - one alternative of an | operator
  350.  *
  351.  * Implements the concatenation operator.
  352.  */
  353. static char *
  354. regbranch(flagp)
  355. int *flagp;
  356. {
  357.     register char *ret;
  358.     register char *chain;
  359.     register char *latest;
  360.     int flags;
  361.  
  362.     *flagp = WORST;        /* Tentatively. */
  363.  
  364.     ret = regnode(BRANCH);
  365.     chain = NULL;
  366.     while (*regparse != '\0' && *regparse != '|' && *regparse != ')') {
  367.         latest = regpiece(&flags);
  368.         if (latest == NULL)
  369.             return(NULL);
  370.         *flagp |= flags&HASWIDTH;
  371.         if (chain == NULL)    /* First piece. */
  372.             *flagp |= flags&SPSTART;
  373.         else
  374.             regtail(chain, latest);
  375.         chain = latest;
  376.     }
  377.     if (chain == NULL)    /* Loop ran zero times. */
  378.         (void) regnode(NOTHING);
  379.  
  380.     return(ret);
  381. }
  382.  
  383. /*
  384.  - regpiece - something followed by possible [*+?]
  385.  *
  386.  * Note that the branching code sequences used for ? and the general cases
  387.  * of * and + are somewhat optimized:  they use the same NOTHING node as
  388.  * both the endmarker for their branch list and the body of the last branch.
  389.  * It might seem that this node could be dispensed with entirely, but the
  390.  * endmarker role is not redundant.
  391.  */
  392. static char *
  393. regpiece(flagp)
  394. int *flagp;
  395. {
  396.     register char *ret;
  397.     register char op;
  398.     register char *next;
  399.     int flags;
  400.  
  401.     ret = regatom(&flags);
  402.     if (ret == NULL)
  403.         return(NULL);
  404.  
  405.     op = *regparse;
  406.     if (!ISMULT(op)) {
  407.         *flagp = flags;
  408.         return(ret);
  409.     }
  410.  
  411.     if (!(flags&HASWIDTH) && op != '?')
  412.         FAIL("*+ operand could be empty");
  413.     *flagp = (op != '+') ? (WORST|SPSTART) : (WORST|HASWIDTH);
  414.  
  415.     if (op == '*' && (flags&SIMPLE))
  416.         reginsert(STAR, ret);
  417.     else if (op == '*') {
  418.         /* Emit x* as (x&|), where & means "self". */
  419.         reginsert(BRANCH, ret);            /* Either x */
  420.         regoptail(ret, regnode(BACK));        /* and loop */
  421.         regoptail(ret, ret);            /* back */
  422.         regtail(ret, regnode(BRANCH));        /* or */
  423.         regtail(ret, regnode(NOTHING));        /* null. */
  424.     } else if (op == '+' && (flags&SIMPLE))
  425.         reginsert(PLUS, ret);
  426.     else if (op == '+') {
  427.         /* Emit x+ as x(&|), where & means "self". */
  428.         next = regnode(BRANCH);            /* Either */
  429.         regtail(ret, next);
  430.         regtail(regnode(BACK), ret);        /* loop back */
  431.         regtail(next, regnode(BRANCH));        /* or */
  432.         regtail(ret, regnode(NOTHING));        /* null. */
  433.     } else if (op == '?') {
  434.         /* Emit x? as (x|) */
  435.         reginsert(BRANCH, ret);            /* Either x */
  436.         regtail(ret, regnode(BRANCH));        /* or */
  437.         next = regnode(NOTHING);        /* null. */
  438.         regtail(ret, next);
  439.         regoptail(ret, next);
  440.     }
  441.     regparse++;
  442.     if (ISMULT(*regparse))
  443.         FAIL("nested *?+");
  444.  
  445.     return(ret);
  446. }
  447.  
  448. /*
  449.  - regatom - the lowest level
  450.  *
  451.  * Optimization:  gobbles an entire sequence of ordinary characters so that
  452.  * it can turn them into a single node, which is smaller to store and
  453.  * faster to run.  Backslashed characters are exceptions, each becoming a
  454.  * separate node; the code is simpler that way and it's not worth fixing.
  455.  */
  456. static char *
  457. regatom(flagp)
  458. int *flagp;
  459. {
  460.     register char *ret;
  461.     int flags;
  462.  
  463.     *flagp = WORST;        /* Tentatively. */
  464.  
  465.     switch (*regparse++) {
  466.     case '^':
  467.         ret = regnode(BOL);
  468.         break;
  469.     case '$':
  470.         ret = regnode(EOL);
  471.         break;
  472.     case '.':
  473.         ret = regnode(ANY);
  474.         *flagp |= HASWIDTH|SIMPLE;
  475.         break;
  476.     case '[': {
  477.             register int class;
  478.             register int classend;
  479.  
  480.             if (*regparse == '^') {    /* Complement of range. */
  481.                 ret = regnode(ANYBUT);
  482.                 regparse++;
  483.             } else
  484.                 ret = regnode(ANYOF);
  485.             if (*regparse == ']' || *regparse == '-')
  486.                 regc(*regparse++);
  487.             while (*regparse != '\0' && *regparse != ']') {
  488.                 if (*regparse == '-') {
  489.                     regparse++;
  490.                     if (*regparse == ']' || *regparse == '\0')
  491.                         regc('-');
  492.                     else {
  493.                         class = UCHARAT(regparse-2)+1;
  494.                         classend = UCHARAT(regparse);
  495.                         if (class > classend+1)
  496.                             FAIL("invalid [] range");
  497.                         for (; class <= classend; class++)
  498.                             regc(class);
  499.                         regparse++;
  500.                     }
  501.                 } else
  502.                     regc(*regparse++);
  503.             }
  504.             regc('\0');
  505.             if (*regparse != ']')
  506.                 FAIL("unmatched []");
  507.             regparse++;
  508.             *flagp |= HASWIDTH|SIMPLE;
  509.         }
  510.         break;
  511.     case '(':
  512.         ret = reg(1, &flags);
  513.         if (ret == NULL)
  514.             return(NULL);
  515.         *flagp |= flags&(HASWIDTH|SPSTART);
  516.         break;
  517.     case '\0':
  518.     case '|':
  519.     case ')':
  520.         FAIL("internal urp");    /* Supposed to be caught earlier. */
  521.         break;
  522.     case '?':
  523.     case '+':
  524.     case '*':
  525.         FAIL("?+* follows nothing");
  526.         break;
  527.     case '\\':
  528.         if (*regparse == '\0')
  529.             FAIL("trailing \\");
  530.         ret = regnode(EXACTLY);
  531.         regc(*regparse++);
  532.         regc('\0');
  533.         *flagp |= HASWIDTH|SIMPLE;
  534.         break;
  535.     default: {
  536.             register int len;
  537.             register char ender;
  538.  
  539.             regparse--;
  540.             len = strcspn(regparse, META);
  541.             if (len <= 0)
  542.                 FAIL("internal disaster");
  543.             ender = *(regparse+len);
  544.             if (len > 1 && ISMULT(ender))
  545.                 len--;        /* Back off clear of ?+* operand. */
  546.             *flagp |= HASWIDTH;
  547.             if (len == 1)
  548.                 *flagp |= SIMPLE;
  549.             ret = regnode(EXACTLY);
  550.             while (len > 0) {
  551.                 regc(*regparse++);
  552.                 len--;
  553.             }
  554.             regc('\0');
  555.         }
  556.         break;
  557.     }
  558.  
  559.     return(ret);
  560. }
  561.  
  562. /*
  563.  - regnode - emit a node
  564.  */
  565. static char *            /* Location. */
  566. regnode(op)
  567. char op;
  568. {
  569.     register char *ret;
  570.     register char *ptr;
  571.  
  572.     ret = regcode;
  573.     if (ret == ®dummy) {
  574.         regsize += 3;
  575.         return(ret);
  576.     }
  577.  
  578.     ptr = ret;
  579.     *ptr++ = op;
  580.     *ptr++ = '\0';        /* Null "next" pointer. */
  581.     *ptr++ = '\0';
  582.     regcode = ptr;
  583.  
  584.     return(ret);
  585. }
  586.  
  587. /*
  588.  - regc - emit (if appropriate) a byte of code
  589.  */
  590. static void
  591. regc(b)
  592. char b;
  593. {
  594.     if (regcode != ®dummy)
  595.         *regcode++ = b;
  596.     else
  597.         regsize++;
  598. }
  599.  
  600. /*
  601.  - reginsert - insert an operator in front of already-emitted operand
  602.  *
  603.  * Means relocating the operand.
  604.  */
  605. static void
  606. reginsert(op, opnd)
  607. char op;
  608. char *opnd;
  609. {
  610.     register char *src;
  611.     register char *dst;
  612.     register char *place;
  613.  
  614.     if (regcode == ®dummy) {
  615.         regsize += 3;
  616.         return;
  617.     }
  618.  
  619.     src = regcode;
  620.     regcode += 3;
  621.     dst = regcode;
  622.     while (src > opnd)
  623.         *--dst = *--src;
  624.  
  625.     place = opnd;        /* Op node, where operand used to be. */
  626.     *place++ = op;
  627.     *place++ = '\0';
  628.     *place++ = '\0';
  629. }
  630.  
  631. /*
  632.  - regtail - set the next-pointer at the end of a node chain
  633.  */
  634. static void
  635. regtail(p, val)
  636. char *p;
  637. char *val;
  638. {
  639.     register char *scan;
  640.     register char *temp;
  641.     register int offset;
  642.  
  643.     if (p == ®dummy)
  644.         return;
  645.  
  646.     /* Find last node. */
  647.     scan = p;
  648.     for (;;) {
  649.         temp = regnext(scan);
  650.         if (temp == NULL)
  651.             break;
  652.         scan = temp;
  653.     }
  654.  
  655.     if (OP(scan) == BACK)
  656.         offset = scan - val;
  657.     else
  658.         offset = val - scan;
  659.     *(scan+1) = (offset>>8)&0377;
  660.     *(scan+2) = offset&0377;
  661. }
  662.  
  663. /*
  664.  - regoptail - regtail on operand of first argument; nop if operandless
  665.  */
  666. static void
  667. regoptail(p, val)
  668. char *p;
  669. char *val;
  670. {
  671.     /* "Operandless" and "op != BRANCH" are synonymous in practice. */
  672.     if (p == NULL || p == ®dummy || OP(p) != BRANCH)
  673.         return;
  674.     regtail(OPERAND(p), val);
  675. }
  676.  
  677. /*
  678.  * regexec and friends
  679.  */
  680.  
  681. /*
  682.  * Global work variables for regexec().
  683.  */
  684. static char *reginput;        /* String-input pointer. */
  685. static char *regbol;        /* Beginning of input, for ^ check. */
  686. static char **regstartp;    /* Pointer to startp array. */
  687. static char **regendp;        /* Ditto for endp. */
  688.  
  689. /*
  690.  * Forwards.
  691.  */
  692. STATIC int regtry();
  693. STATIC int regmatch();
  694. STATIC int regrepeat();
  695.  
  696. #ifdef DEBUG
  697. int regnarrate = 0;
  698. void regdump();
  699. STATIC char *regprop();
  700. #endif
  701.  
  702. /*
  703.  - regexec - match a regexp against a string
  704.  */
  705. int
  706. regexec(prog, string)
  707. register regexp *prog;
  708. register char *string;
  709. {
  710.     register char *s;
  711.     extern char *strchr();
  712.  
  713.     /* Be paranoid... */
  714.     if (prog == NULL || string == NULL) {
  715.         regerror("NULL parameter");
  716.         return(0);
  717.     }
  718.  
  719.     /* Check validity of program. */
  720.     if (UCHARAT(prog->program) != MAGIC) {
  721.         regerror("corrupted program");
  722.         return(0);
  723.     }
  724.  
  725.     /* If there is a "must appear" string, look for it. */
  726.     if (prog->regmust != NULL) {
  727.         s = string;
  728.         while ((s = strchr(s, prog->regmust[0])) != NULL) {
  729.             if (strncmp(s, prog->regmust, prog->regmlen) == 0)
  730.                 break;    /* Found it. */
  731.             s++;
  732.         }
  733.         if (s == NULL)    /* Not present. */
  734.             return(0);
  735.     }
  736.  
  737.     /* Mark beginning of line for ^ . */
  738.     regbol = string;
  739.  
  740.     /* Simplest case:  anchored match need be tried only once. */
  741.     if (prog->reganch)
  742.         return(regtry(prog, string));
  743.  
  744.     /* Messy cases:  unanchored match. */
  745.     s = string;
  746.     if (prog->regstart != '\0')
  747.         /* We know what char it must start with. */
  748.         while ((s = strchr(s, prog->regstart)) != NULL) {
  749.             if (regtry(prog, s))
  750.                 return(1);
  751.             s++;
  752.         }
  753.     else
  754.         /* We don't -- general case. */
  755.         do {
  756.             if (regtry(prog, s))
  757.                 return(1);
  758.         } while (*s++ != '\0');
  759.  
  760.     /* Failure. */
  761.     return(0);
  762. }
  763.  
  764. /*
  765.  - regtry - try match at specific point
  766.  */
  767. static int            /* 0 failure, 1 success */
  768. regtry(prog, string)
  769. regexp *prog;
  770. char *string;
  771. {
  772.     register int i;
  773.     register char **sp;
  774.     register char **ep;
  775.  
  776.     reginput = string;
  777.     regstartp = prog->startp;
  778.     regendp = prog->endp;
  779.  
  780.     sp = prog->startp;
  781.     ep = prog->endp;
  782.     for (i = NSUBEXP; i > 0; i--) {
  783.         *sp++ = NULL;
  784.         *ep++ = NULL;
  785.     }
  786.     if (regmatch(prog->program + 1)) {
  787.         prog->startp[0] = string;
  788.         prog->endp[0] = reginput;
  789.         return(1);
  790.     } else
  791.         return(0);
  792. }
  793.  
  794. /*
  795.  - regmatch - main matching routine
  796.  *
  797.  * Conceptually the strategy is simple:  check to see whether the current
  798.  * node matches, call self recursively to see whether the rest matches,
  799.  * and then act accordingly.  In practice we make some effort to avoid
  800.  * recursion, in particular by going through "ordinary" nodes (that don't
  801.  * need to know whether the rest of the match failed) by a loop instead of
  802.  * by recursion.
  803.  */
  804. static int            /* 0 failure, 1 success */
  805. regmatch(prog)
  806. char *prog;
  807. {
  808.     register char *scan;    /* Current node. */
  809.     char *next;        /* Next node. */
  810.     extern char *strchr();
  811.  
  812.     scan = prog;
  813. #ifdef DEBUG
  814.     if (scan != NULL && regnarrate)
  815.         fprintf(stderr, "%s(\n", regprop(scan));
  816. #endif
  817.     while (scan != NULL) {
  818. #ifdef DEBUG
  819.         if (regnarrate)
  820.             fprintf(stderr, "%s...\n", regprop(scan));
  821. #endif
  822.         next = regnext(scan);
  823.  
  824.         switch (OP(scan)) {
  825.         case BOL:
  826.             if (reginput != regbol)
  827.                 return(0);
  828.             break;
  829.         case EOL:
  830.             if (*reginput != '\0')
  831.                 return(0);
  832.             break;
  833.         case ANY:
  834.             if (*reginput == '\0')
  835.                 return(0);
  836.             reginput++;
  837.             break;
  838.         case EXACTLY: {
  839.                 register int len;
  840.                 register char *opnd;
  841.  
  842.                 opnd = OPERAND(scan);
  843.                 /* Inline the first character, for speed. */
  844.                 if (*opnd != *reginput)
  845.                     return(0);
  846.                 len = strlen(opnd);
  847.                 if (len > 1 && strncmp(opnd, reginput, len) != 0)
  848.                     return(0);
  849.                 reginput += len;
  850.             }
  851.             break;
  852.         case ANYOF:
  853.             if (*reginput == '\0' || strchr(OPERAND(scan), *reginput) == NULL)
  854.                 return(0);
  855.             reginput++;
  856.             break;
  857.         case ANYBUT:
  858.             if (*reginput == '\0' || strchr(OPERAND(scan), *reginput) != NULL)
  859.                 return(0);
  860.             reginput++;
  861.             break;
  862.         case NOTHING:
  863.             break;
  864.         case BACK:
  865.             break;
  866.         case OPEN+1:
  867.         case OPEN+2:
  868.         case OPEN+3:
  869.         case OPEN+4:
  870.         case OPEN+5:
  871.         case OPEN+6:
  872.         case OPEN+7:
  873.         case OPEN+8:
  874.         case OPEN+9: {
  875.                 register int no;
  876.                 register char *save;
  877.  
  878.                 no = OP(scan) - OPEN;
  879.                 save = reginput;
  880.  
  881.                 if (regmatch(next)) {
  882.                     /*
  883.                      * Don't set startp if some later
  884.                      * invocation of the same parentheses
  885.                      * already has.
  886.                      */
  887.                     if (regstartp[no] == NULL)
  888.                         regstartp[no] = save;
  889.                     return(1);
  890.                 } else
  891.                     return(0);
  892.             }
  893.             break;
  894.         case CLOSE+1:
  895.         case CLOSE+2:
  896.         case CLOSE+3:
  897.         case CLOSE+4:
  898.         case CLOSE+5:
  899.         case CLOSE+6:
  900.         case CLOSE+7:
  901.         case CLOSE+8:
  902.         case CLOSE+9: {
  903.                 register int no;
  904.                 register char *save;
  905.  
  906.                 no = OP(scan) - CLOSE;
  907.                 save = reginput;
  908.  
  909.                 if (regmatch(next)) {
  910.                     /*
  911.                      * Don't set endp if some later
  912.                      * invocation of the same parentheses
  913.                      * already has.
  914.                      */
  915.                     if (regendp[no] == NULL)
  916.                         regendp[no] = save;
  917.                     return(1);
  918.                 } else
  919.                     return(0);
  920.             }
  921.             break;
  922.         case BRANCH: {
  923.                 register char *save;
  924.  
  925.                 if (OP(next) != BRANCH)        /* No choice. */
  926.                     next = OPERAND(scan);    /* Avoid recursion. */
  927.                 else {
  928.                     do {
  929.                         save = reginput;
  930.                         if (regmatch(OPERAND(scan)))
  931.                             return(1);
  932.                         reginput = save;
  933.                         scan = regnext(scan);
  934.                     } while (scan != NULL && OP(scan) == BRANCH);
  935.                     return(0);
  936.                     /* NOTREACHED */
  937.                 }
  938.             }
  939.             break;
  940.         case STAR:
  941.         case PLUS: {
  942.                 register char nextch;
  943.                 register int no;
  944.                 register char *save;
  945.                 register int min;
  946.  
  947.                 /*
  948.                  * Lookahead to avoid useless match attempts
  949.                  * when we know what character comes next.
  950.                  */
  951.                 nextch = '\0';
  952.                 if (OP(next) == EXACTLY)
  953.                     nextch = *OPERAND(next);
  954.                 min = (OP(scan) == STAR) ? 0 : 1;
  955.                 save = reginput;
  956.                 no = regrepeat(OPERAND(scan));
  957.                 while (no >= min) {
  958.                     /* If it could work, try it. */
  959.                     if (nextch == '\0' || *reginput == nextch)
  960.                         if (regmatch(next))
  961.                             return(1);
  962.                     /* Couldn't or didn't -- back up. */
  963.                     no--;
  964.                     reginput = save + no;
  965.                 }
  966.                 return(0);
  967.             }
  968.             break;
  969.         case END:
  970.             return(1);    /* Success! */
  971.             break;
  972.         default:
  973.             regerror("memory corruption");
  974.             return(0);
  975.             break;
  976.         }
  977.  
  978.         scan = next;
  979.     }
  980.  
  981.     /*
  982.      * We get here only if there's trouble -- normally "case END" is
  983.      * the terminating point.
  984.      */
  985.     regerror("corrupted pointers");
  986.     return(0);
  987. }
  988.  
  989. /*
  990.  - regrepeat - repeatedly match something simple, report how many
  991.  */
  992. static int
  993. regrepeat(p)
  994. char *p;
  995. {
  996.     register int count = 0;
  997.     register char *scan;
  998.     register char *opnd;
  999.  
  1000.     scan = reginput;
  1001.     opnd = OPERAND(p);
  1002.     switch (OP(p)) {
  1003.     case ANY:
  1004.         count = strlen(scan);
  1005.         scan += count;
  1006.         break;
  1007.     case EXACTLY:
  1008.         while (*opnd == *scan) {
  1009.             count++;
  1010.             scan++;
  1011.         }
  1012.         break;
  1013.     case ANYOF:
  1014.         while (*scan != '\0' && strchr(opnd, *scan) != NULL) {
  1015.             count++;
  1016.             scan++;
  1017.         }
  1018.         break;
  1019.     case ANYBUT:
  1020.         while (*scan != '\0' && strchr(opnd, *scan) == NULL) {
  1021.             count++;
  1022.             scan++;
  1023.         }
  1024.         break;
  1025.     default:        /* Oh dear.  Called inappropriately. */
  1026.         regerror("internal foulup");
  1027.         count = 0;    /* Best compromise. */
  1028.         break;
  1029.     }
  1030.     reginput = scan;
  1031.  
  1032.     return(count);
  1033. }
  1034.  
  1035. /*
  1036.  - regnext - dig the "next" pointer out of a node
  1037.  */
  1038. static char *
  1039. regnext(p)
  1040. register char *p;
  1041. {
  1042.     register int offset;
  1043.  
  1044.     if (p == ®dummy)
  1045.         return(NULL);
  1046.  
  1047.     offset = NEXT(p);
  1048.     if (offset == 0)
  1049.         return(NULL);
  1050.  
  1051.     if (OP(p) == BACK)
  1052.         return(p-offset);
  1053.     else
  1054.         return(p+offset);
  1055. }
  1056.  
  1057. #ifdef DEBUG
  1058.  
  1059. STATIC char *regprop();
  1060.  
  1061. /*
  1062.  - regdump - dump a regexp onto stdout in vaguely comprehensible form
  1063.  */
  1064. void
  1065. regdump(r)
  1066. regexp *r;
  1067. {
  1068.     register char *s;
  1069.     register char op = EXACTLY;    /* Arbitrary non-END op. */
  1070.     register char *next;
  1071.     extern char *strchr();
  1072.  
  1073.  
  1074.     s = r->program + 1;
  1075.     while (op != END) {    /* While that wasn't END last time... */
  1076.         op = OP(s);
  1077.         printf("%2d%s", s-r->program, regprop(s));    /* Where, what. */
  1078.         next = regnext(s);
  1079.         if (next == NULL)        /* Next ptr. */
  1080.             printf("(0)");
  1081.         else 
  1082.             printf("(%d)", (s-r->program)+(next-s));
  1083.         s += 3;
  1084.         if (op == ANYOF || op == ANYBUT || op == EXACTLY) {
  1085.             /* Literal string, where present. */
  1086.             while (*s != '\0') {
  1087.                 putchar(*s);
  1088.                 s++;
  1089.             }
  1090.             s++;
  1091.         }
  1092.         putchar('\n');
  1093.     }
  1094.  
  1095.     /* Header fields of interest. */
  1096.     if (r->regstart != '\0')
  1097.         printf("start `%c' ", r->regstart);
  1098.     if (r->reganch)
  1099.         printf("anchored ");
  1100.     if (r->regmust != NULL)
  1101.         printf("must have \"%s\"", r->regmust);
  1102.     printf("\n");
  1103. }
  1104.  
  1105. /*
  1106.  - regprop - printable representation of opcode
  1107.  */
  1108. static char *
  1109. regprop(op)
  1110. char *op;
  1111. {
  1112.     register char *p;
  1113.     static char buf[50];
  1114.  
  1115.     (void) strcpy(buf, ":");
  1116.  
  1117.     switch (OP(op)) {
  1118.     case BOL:
  1119.         p = "BOL";
  1120.         break;
  1121.     case EOL:
  1122.         p = "EOL";
  1123.         break;
  1124.     case ANY:
  1125.         p = "ANY";
  1126.         break;
  1127.     case ANYOF:
  1128.         p = "ANYOF";
  1129.         break;
  1130.     case ANYBUT:
  1131.         p = "ANYBUT";
  1132.         break;
  1133.     case BRANCH:
  1134.         p = "BRANCH";
  1135.         break;
  1136.     case EXACTLY:
  1137.         p = "EXACTLY";
  1138.         break;
  1139.     case NOTHING:
  1140.         p = "NOTHING";
  1141.         break;
  1142.     case BACK:
  1143.         p = "BACK";
  1144.         break;
  1145.     case END:
  1146.         p = "END";
  1147.         break;
  1148.     case OPEN+1:
  1149.     case OPEN+2:
  1150.     case OPEN+3:
  1151.     case OPEN+4:
  1152.     case OPEN+5:
  1153.     case OPEN+6:
  1154.     case OPEN+7:
  1155.     case OPEN+8:
  1156.     case OPEN+9:
  1157.         sprintf(buf+strlen(buf), "OPEN%d", OP(op)-OPEN);
  1158.         p = NULL;
  1159.         break;
  1160.     case CLOSE+1:
  1161.     case CLOSE+2:
  1162.     case CLOSE+3:
  1163.     case CLOSE+4:
  1164.     case CLOSE+5:
  1165.     case CLOSE+6:
  1166.     case CLOSE+7:
  1167.     case CLOSE+8:
  1168.     case CLOSE+9:
  1169.         sprintf(buf+strlen(buf), "CLOSE%d", OP(op)-CLOSE);
  1170.         p = NULL;
  1171.         break;
  1172.     case STAR:
  1173.         p = "STAR";
  1174.         break;
  1175.     case PLUS:
  1176.         p = "PLUS";
  1177.         break;
  1178.     default:
  1179.         regerror("corrupted opcode");
  1180.         break;
  1181.     }
  1182.     if (p != NULL)
  1183.         (void) strcat(buf, p);
  1184.     return(buf);
  1185. }
  1186. #endif
  1187.  
  1188. /*
  1189.  * The following is provided for those people who do not have strcspn() in
  1190.  * their C libraries.  They should get off their butts and do something
  1191.  * about it; at least one public-domain implementation of those (highly
  1192.  * useful) string routines has been published on Usenet.
  1193.  */
  1194. #ifdef STRCSPN
  1195. /*
  1196.  * strcspn - find length of initial segment of s1 consisting entirely
  1197.  * of characters not from s2
  1198.  */
  1199.  
  1200. static int
  1201. strcspn(s1, s2)
  1202. char *s1;
  1203. char *s2;
  1204. {
  1205.     register char *scan1;
  1206.     register char *scan2;
  1207.     register int count;
  1208.  
  1209.     count = 0;
  1210.     for (scan1 = s1; *scan1 != '\0'; scan1++) {
  1211.         for (scan2 = s2; *scan2 != '\0';)    /* ++ moved down. */
  1212.             if (*scan1 == *scan2++)
  1213.                 return(count);
  1214.         count++;
  1215.     }
  1216.     return(count);
  1217. }
  1218. #endif
  1219.