home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 22 gnu / 22-gnu.zip / gnuawk.zip / re.c < prev    next >
C/C++ Source or Header  |  1997-02-19  |  7KB  |  311 lines

  1. /*
  2.  * re.c - compile regular expressions.
  3.  */
  4.  
  5. /* 
  6.  * Copyright (C) 1991-1996 the Free Software Foundation, Inc.
  7.  * 
  8.  * This file is part of GAWK, the GNU implementation of the
  9.  * AWK Programming Language.
  10.  * 
  11.  * GAWK is free software; you can redistribute it and/or modify
  12.  * it under the terms of the GNU General Public License as published by
  13.  * the Free Software Foundation; either version 2 of the License, or
  14.  * (at your option) any later version.
  15.  * 
  16.  * GAWK is distributed in the hope that it will be useful,
  17.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19.  * GNU General Public License for more details.
  20.  * 
  21.  * You should have received a copy of the GNU General Public License
  22.  * along with this program; if not, write to the Free Software
  23.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA
  24.  */
  25.  
  26. #include "awk.h"
  27.  
  28. static reg_syntax_t syn;
  29.  
  30. /* make_regexp --- generate compiled regular expressions */
  31.  
  32. Regexp *
  33. make_regexp(s, len, ignorecase, dfa)
  34. char *s;
  35. size_t len;
  36. int ignorecase;
  37. int dfa;
  38. {
  39.     Regexp *rp;
  40.     const char *rerr;
  41.     char *src = s;
  42.     char *temp;
  43.     char *end = s + len;
  44.     register char *dest;
  45.     register int c, c2;
  46.  
  47.     /* Handle escaped characters first. */
  48.  
  49.     /*
  50.      * Build a copy of the string (in dest) with the
  51.      * escaped characters translated, and generate the regex
  52.      * from that.  
  53.      */
  54.     emalloc(dest, char *, len + 2, "make_regexp");
  55.     temp = dest;
  56.  
  57.     while (src < end) {
  58.         if (*src == '\\') {
  59.             c = *++src;
  60.             switch (c) {
  61.             case 'a':
  62.             case 'b':
  63.             case 'f':
  64.             case 'n':
  65.             case 'r':
  66.             case 't':
  67.             case 'v':
  68.             case 'x':
  69.             case '0':
  70.             case '1':
  71.             case '2':
  72.             case '3':
  73.             case '4':
  74.             case '5':
  75.             case '6':
  76.             case '7':
  77.                 c2 = parse_escape(&src);
  78.                 if (c2 < 0)
  79.                     cant_happen();
  80.                 /*
  81.                  * Unix awk treats octal (and hex?) chars
  82.                  * literally in re's, so escape regexp
  83.                  * metacharacters.
  84.                  */
  85.                 if (do_traditional && ! do_posix && (isdigit(c) || c == 'x')
  86.                     && strchr("()|*+?.^$\\[]", c2) != NULL)
  87.                     *dest++ = '\\';
  88.                 *dest++ = (char) c2;
  89.                 break;
  90.             case '8':
  91.             case '9':    /* a\9b not valid */
  92.                 *dest++ = c;
  93.                 src++;
  94.                 break;
  95.             case 'y':    /* normally \b */
  96.                 /* gnu regex op */
  97.                 if (! do_traditional) {
  98.                     *dest++ = '\\';
  99.                     *dest++ = 'b';
  100.                     src++;
  101.                     break;
  102.                 }
  103.                 /* else, fall through */
  104.             default:
  105.                 *dest++ = '\\';
  106.                 *dest++ = (char) c;
  107.                 src++;
  108.                 break;
  109.             } /* switch */
  110.         } else
  111.             *dest++ = *src++;    /* not '\\' */
  112.     } /* for */
  113.  
  114.     *dest = '\0' ;    /* Only necessary if we print dest ? */
  115.     emalloc(rp, Regexp *, sizeof(*rp), "make_regexp");
  116.     memset((char *) rp, 0, sizeof(*rp));
  117.     rp->pat.allocated = 0;    /* regex will allocate the buffer */
  118.     emalloc(rp->pat.fastmap, char *, 256, "make_regexp");
  119.  
  120.     if (ignorecase)
  121.         rp->pat.translate = casetable;
  122.     else
  123.         rp->pat.translate = NULL;
  124.     len = dest - temp;
  125.     if ((rerr = re_compile_pattern(temp, len, &(rp->pat))) != NULL)
  126.         fatal("%s: /%s/", rerr, temp);
  127.  
  128.     /* gack. this must be done *after* re_compile_pattern */
  129.     rp->pat.newline_anchor = FALSE; /* don't get \n in middle of string */
  130.     if (dfa && ! ignorecase) {
  131.         dfacomp(temp, len, &(rp->dfareg), TRUE);
  132.         rp->dfa = TRUE;
  133.     } else
  134.         rp->dfa = FALSE;
  135.  
  136.     free(temp);
  137.     return rp;
  138. }
  139.  
  140. /* research --- do a regexp search. use dfa if possible */
  141.  
  142. int
  143. research(rp, str, start, len, need_start)
  144. Regexp *rp;
  145. register char *str;
  146. int start;
  147. register size_t len;
  148. int need_start;
  149. {
  150.     char *ret = str;
  151.     int try_backref;
  152.  
  153.     /*
  154.      * Always do dfa search if can; if it fails, then even if
  155.      * need_start is true, we won't bother with the regex search.
  156.      */
  157.     if (rp->dfa) {
  158.         char save;
  159.         int count = 0;
  160.  
  161.         /*
  162.          * dfa likes to stick a '\n' right after the matched
  163.          * text.  So we just save and restore the character.
  164.          */
  165.         save = str[start+len];
  166.         ret = dfaexec(&(rp->dfareg), str+start, str+start+len, TRUE,
  167.                     &count, &try_backref);
  168.         str[start+len] = save;
  169.     }
  170.     if (ret) {
  171.         if (need_start || rp->dfa == FALSE || try_backref) {
  172.             int result = re_search(&(rp->pat), str, start+len,
  173.                     start, len, &(rp->regs));
  174.             /* recover any space from C based alloca */
  175. #ifdef C_ALLOCA
  176.             (void) alloca(0);
  177. #endif
  178.             return result;
  179.         } else
  180.             return 1;
  181.     } else
  182.         return -1;
  183. }
  184.  
  185. /* refree --- free up the dynamic memory used by a compiled regexp */
  186.  
  187. void
  188. refree(rp)
  189. Regexp *rp;
  190. {
  191.     free(rp->pat.buffer);
  192.     free(rp->pat.fastmap);
  193.     if (rp->regs.start)
  194.         free(rp->regs.start);
  195.     if (rp->regs.end)
  196.         free(rp->regs.end);
  197.     if (rp->dfa)
  198.         dfafree(&(rp->dfareg));
  199.     free(rp);
  200. }
  201.  
  202. /* dfaerror --- print an error message for the dfa routines */
  203.  
  204. void
  205. dfaerror(s)
  206. const char *s;
  207. {
  208.     fatal("%s", s);
  209. }
  210.  
  211. /* re_update --- recompile a dynamic regexp */
  212.  
  213. Regexp *
  214. re_update(t)
  215. NODE *t;
  216. {
  217.     NODE *t1;
  218.  
  219. /* #    define    CASE    1 */
  220.     if ((t->re_flags & CASE) == IGNORECASE) {
  221.         if ((t->re_flags & CONST) != 0)
  222.             return t->re_reg;
  223.         t1 = force_string(tree_eval(t->re_exp));
  224.         if (t->re_text != NULL) {
  225.             if (cmp_nodes(t->re_text, t1) == 0) {
  226.                 free_temp(t1);
  227.                 return t->re_reg;
  228.             }
  229.             unref(t->re_text);
  230.         }
  231.         t->re_text = dupnode(t1);
  232.         free_temp(t1);
  233.     }
  234.     if (t->re_reg != NULL)
  235.         refree(t->re_reg);
  236.     if (t->re_cnt > 0)
  237.         t->re_cnt++;
  238.     if (t->re_cnt > 10)
  239.         t->re_cnt = 0;
  240.     if (t->re_text == NULL) {
  241.         t1 = force_string(tree_eval(t->re_exp));
  242.         t->re_text = dupnode(t1);
  243.         free_temp(t1);
  244.     }
  245.     t->re_reg = make_regexp(t->re_text->stptr, t->re_text->stlen,
  246.                 IGNORECASE, t->re_cnt);
  247.     t->re_flags &= ~CASE;
  248.     t->re_flags |= IGNORECASE;
  249.     return t->re_reg;
  250. }
  251.  
  252. /* resetup --- choose what kind of regexps we match */
  253.  
  254. void
  255. resetup()
  256. {
  257.     if (do_posix)
  258.         syn = RE_SYNTAX_POSIX_AWK;    /* strict POSIX re's */
  259.     else if (do_traditional)
  260.         syn = RE_SYNTAX_AWK;        /* traditional Unix awk re's */
  261.     else
  262.         syn = RE_SYNTAX_GNU_AWK;    /* POSIX re's + GNU ops */
  263.  
  264.     /*
  265.      * Interval expressions are off by default, since it's likely to
  266.      * break too many old programs to have them on.
  267.      */
  268.     if (do_intervals)
  269.         syn |= RE_INTERVALS;
  270.  
  271.     (void) re_set_syntax(syn);
  272.     dfasyntax(syn, FALSE);
  273. }
  274.  
  275. /* avoid_dfa --- FIXME: temporary kludge function until we have a new dfa.c */
  276.  
  277. int
  278. avoid_dfa(re, str, len)
  279. NODE *re;
  280. char *str;
  281. size_t len;
  282. {
  283.     char *restr;
  284.     int relen;
  285.     int anchor, i;
  286.     char *end;
  287.  
  288.     if ((re->re_flags & CONST) != 0) {
  289.         restr = re->re_exp->stptr;
  290.         relen = re->re_exp->stlen;
  291.     } else {
  292.         restr = re->re_text->stptr;
  293.         relen = re->re_text->stlen;
  294.     }
  295.  
  296.     for (anchor = FALSE, i = 0; i < relen; i++) {
  297.         if (restr[i] == '^' || restr[i] == '$') {
  298.             anchor = TRUE;
  299.             break;
  300.         }
  301.     }
  302.     if (! anchor)
  303.         return FALSE;
  304.  
  305.     for (end = str + len; str < end; str++)
  306.         if (*str == '\n')
  307.             return TRUE;
  308.  
  309.     return FALSE;
  310. }
  311.