home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 1 / ARM_CLUB_CD.iso / contents / apps / program / d / elvis / Source / c / regexp < prev    next >
Encoding:
Text File  |  1991-08-28  |  29.5 KB  |  1,352 lines

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