Annotation of researchv10dc/cmd/egrep/egrep.bundle, revision 1.1.1.1

1.1       root        1: # To unbundle, sh this file
                      2: echo mkfile 1>&2
                      3: sed 's/.//' >mkfile <<'//GO.SYSIN DD mkfile'
                      4: -CFLAGS=-O
                      5: -YFLAGS=-d
                      6: -NPROC=2
                      7: -
                      8: -egrep:        gram.o main.o egrep.o bm.o anal.o
                      9: -      $CC $CFLAGS -o $target $prereq
                     10: -
                     11: -install:      egrep
                     12: -      cp egrep /usr/bin/egrep
                     13: -      strip /usr/bin/egrep
                     14: -      chmod 775 /usr/bin/egrep
                     15: -      chown bin,bin /usr/bin/egrep
                     16: -
                     17: -clean:V:
                     18: -      rm -f *.o egrep
                     19: -
                     20: -y.tab.h:      gram.o
                     21: -egrep.o anal.o:       y.tab.h
                     22: -anal.o bm.o egrep.o main.o gram.o: hdr.h
                     23: -
                     24: -pp:
                     25: -      smallpr mkfile hdr.h anal.c bm.c egrep.c gram.y main.c
                     26: -
                     27: -egrep.bundle:V:
                     28: -      bundle mkfile *.h *.c *.y > $target
                     29: //GO.SYSIN DD mkfile
                     30: echo hdr.h 1>&2
                     31: sed 's/.//' >hdr.h <<'//GO.SYSIN DD hdr.h'
                     32: -#include      <fio.h>
                     33: -#include      <ctype.h>
                     34: -#include      <libc.h>
                     35: -
                     36: -#define BLKSIZE 1024  /* size of reported disk blocks */
                     37: -#define MAXLIN 10000
                     38: -#define MAXPOS 20000
                     39: -#define NCHARS 256
                     40: -#define NSTATES 128
                     41: -#define FINAL -1
                     42: -#define LEFT '\177'   /* serves as ^ */
                     43: -#define RIGHT '\n'    /* serves as record separator and as $ */
                     44: -
                     45: -typedef struct State
                     46: -{
                     47: -      struct State *gotofn[NCHARS];
                     48: -      int out;
                     49: -} State;
                     50: -extern State states[];
                     51: -State *nxtst();
                     52: -extern int state[];
                     53: -extern int line;
                     54: -extern int name[];
                     55: -extern int left[];
                     56: -extern int right[];
                     57: -extern int parent[];
                     58: -extern int foll[];
                     59: -extern int positions[];
                     60: -extern char chars[];
                     61: -extern int nxtpos;
                     62: -extern int nxtfoll;
                     63: -extern int inxtpos;
                     64: -extern int nxtfoll;
                     65: -extern int nxtchar;
                     66: -extern int tmpstat[];
                     67: -extern State *istat;
                     68: -extern int nstate;
                     69: -extern int xstate;
                     70: -extern int count;
                     71: -extern char *input;
                     72: -extern char *progname;
                     73: -
                     74: -extern char reinit;
                     75: -
                     76: -extern int begout;
                     77: -extern int begcnt;
                     78: -extern int begstat[];
                     79: -
                     80: -extern int colpos[];
                     81: -extern int cntpos;
                     82: -
                     83: -extern long   lnum;
                     84: -extern int    bflag;
                     85: -extern int    cflag;
                     86: -extern int    fflag;
                     87: -extern int    hflag;
                     88: -extern int    iflag;
                     89: -extern int    lflag;
                     90: -extern int    nflag;
                     91: -extern int    sflag;
                     92: -extern int    vflag;
                     93: -extern int    nfile;
                     94: -extern long   tln;
                     95: -extern int    nsucc;
                     96: -extern int    badbotch;
                     97: -
                     98: -extern int    expfile;
                     99: -
                    100: -extern int bmegrep;
                    101: -extern int scanexit;
                    102: //GO.SYSIN DD hdr.h
                    103: echo y.tab.h 1>&2
                    104: sed 's/.//' >y.tab.h <<'//GO.SYSIN DD y.tab.h'
                    105: -# define CHAR 257
                    106: -# define DOT 258
                    107: -# define CCL 259
                    108: -# define NCCL 260
                    109: -# define OR 261
                    110: -# define CAT 262
                    111: -# define STAR 263
                    112: -# define PLUS 264
                    113: -# define QUEST 265
                    114: //GO.SYSIN DD y.tab.h
                    115: echo anal.c 1>&2
                    116: sed 's/.//' >anal.c <<'//GO.SYSIN DD anal.c'
                    117: -#include      "hdr.h"
                    118: -#include      "y.tab.h"
                    119: -
                    120: -#define       NLIT    256
                    121: -#define       LSTART  litp = litb
                    122: -#define       LEND    {*litp = 0; if(strlen(litb) > strlen(blit))strcpy(blit,litb);}
                    123: -
                    124: -static char *litp, litb[NLIT], *blit;
                    125: -
                    126: -
                    127: -/*
                    128: -      islit has to return 1 if we are to use boyer-moore.
                    129: -      in this case, set buf to the literal string and
                    130: -      set bmegrep to 1 if there is a regular expression
                    131: -      beside the literal (literal can be anywhere).
                    132: -*/
                    133: -islit(buf)
                    134: -      char *buf;
                    135: -{
                    136: -      bmegrep = 0;
                    137: -      blit = buf;
                    138: -      *blit = 0;
                    139: -      LSTART;
                    140: -
                    141: -/* pr(line-2, ""); */
                    142: -
                    143: -      lit(line-2);
                    144: -      LEND;
                    145: -      if(strlen(blit) > 1){
                    146: -              /* bmegrep set by lit */
                    147: -              return(1);
                    148: -      }
                    149: -      return(0);
                    150: -}
                    151: -
                    152: -/*
                    153: -      lit builds literal strings in litb. we ensure these are compulsory
                    154: -      literal string by only descending the right nodes.
                    155: -*/
                    156: -lit(n)
                    157: -{
                    158: -      if(name[n] == CAT){
                    159: -              lit(left[n]);
                    160: -              lit(right[n]);
                    161: -      } else if(name[n] == PLUS){
                    162: -              lit(left[n]);
                    163: -              LEND;           /* can't go on past a + */
                    164: -              LSTART;
                    165: -              bmegrep = 1;
                    166: -              lit(left[n]);   /* but we can start with one! */
                    167: -      } else if((name[n] == CCL) && (chars[right[n]] == 1)){
                    168: -              *litp++ = chars[right[n]+1];
                    169: -              if(litp == &litb[NLIT])
                    170: -                      litp--;
                    171: -      } else if(left[n] == 0 && name[n] < 256
                    172: -                      && name[n] != LEFT && name[n] != RIGHT){
                    173: -              *litp++ = name[n];
                    174: -              if(litp == &litb[NLIT])
                    175: -                      litp--;
                    176: -      } else {
                    177: -              LEND;
                    178: -              LSTART;
                    179: -              bmegrep = 1;
                    180: -      }
                    181: -}
                    182: -
                    183: -/*#ifdef      DEBUG*/
                    184: -
                    185: -pr(n, s)
                    186: -      char *s;
                    187: -{
                    188: -      char buf[256];
                    189: -      int i, cnt;
                    190: -
                    191: -      print("%s", s);
                    192: -      sprint(buf, "%s   ", s);
                    193: -      switch(name[n])
                    194: -      {
                    195: -      case FINAL:     print("expr=\n"); pr(left[n], buf); break;
                    196: -      case DOT:       print("%d: .\n",n); break;
                    197: -      case STAR:      print("%d: *\n",n); pr(left[n], buf); break;
                    198: -      case PLUS:      print("%d: +\n",n); pr(left[n], buf); break;
                    199: -      case QUEST:     print("%d: ?\n",n); pr(left[n], buf); break;
                    200: -      case CAT:       print("%d: cat\n", n); pr(left[n], buf); pr(right[n], buf); break;
                    201: -      case OR:        print("%d: or\n", n); pr(left[n], buf); pr(right[n], buf); break;
                    202: -      case NCCL:
                    203: -      case CCL:       print("%d: [%s", n, name[n]==NCCL? "^":"");
                    204: -                      for(i = right[n], cnt = chars[i++]; cnt > 0; cnt--)
                    205: -                              print("%c", chars[i++]);
                    206: -                      print("]\n");
                    207: -                      break;
                    208: -      default:
                    209: -                      if(name[n] < 256)
                    210: -                              print("%d: '%c'\n", n, name[n]);
                    211: -                      else
                    212: -                              print("URK %d\n", name[n]);
                    213: -                      break;
                    214: -      }
                    215: -}
                    216: -/*#endif*/
                    217: //GO.SYSIN DD anal.c
                    218: echo bm.c 1>&2
                    219: sed 's/.//' >bm.c <<'//GO.SYSIN DD bm.c'
                    220: -#include      "hdr.h"
                    221: -
                    222: -#define       LARGE   100000
                    223: -
                    224: -static int delta0[256];
                    225: -static char cmap[256];
                    226: -static char *bmpat;
                    227: -
                    228: -bmprep(pattern)
                    229: -      char *pattern;
                    230: -{
                    231: -      register int j, patlen;
                    232: -
                    233: -      patlen = strlen(bmpat = pattern);
                    234: -      for(j = 0; j < 256; j++){
                    235: -              delta0[j] = patlen;
                    236: -              cmap[j] = j;
                    237: -      }
                    238: -      for(j = 0; j < patlen-1; j++)
                    239: -              delta0[pattern[j]] = patlen-j-1;
                    240: -      delta0[pattern[patlen-1]] = LARGE;
                    241: -      if(iflag){
                    242: -              for(j = 0; j < patlen-1; j++)
                    243: -                      if(islower(pattern[j]))
                    244: -                              delta0[toupper(pattern[j])] = patlen-j-1;
                    245: -              if(islower(pattern[patlen-1]))
                    246: -                      delta0[toupper(pattern[patlen-1])] = LARGE;
                    247: -              for(j = 'A'; j <= 'Z'; j++)
                    248: -                      cmap[j] = tolower(j);
                    249: -      }
                    250: -}
                    251: -
                    252: -bmexecute(file)
                    253: -char *file;
                    254: -{
                    255: -      register char *p;
                    256: -      register char *endpt;
                    257: -      register char *s;
                    258: -      register int j;
                    259: -      int len, patlen = strlen(bmpat);
                    260: -      char *rdpt;
                    261: -      int fd, eof, n;
                    262: -      long seek;
                    263: -      char *nlp, *proc, *np, *op;
                    264: -      char buf[8*8192];
                    265: -
                    266: -      if(file){
                    267: -              if((fd = open(file, 0)) < 0){
                    268: -                      fprint(2, "%s: can't open %s\n", progname, file);
                    269: -                      badbotch=1;
                    270: -                      return;
                    271: -              }
                    272: -      } else
                    273: -              fd = 0;
                    274: -      lnum = 1;
                    275: -      tln = 0;
                    276: -      rdpt = buf;
                    277: -      seek = 0;
                    278: -      for(eof = 0; eof == 0;){
                    279: -              Fflush(1);
                    280: -              if((n = read(fd, rdpt, &buf[sizeof buf]-rdpt)) <= 0){
                    281: -                      if(rdpt == buf)
                    282: -                              break;          /* eof, nothing left to process */
                    283: -                      *rdpt = '\n';           /* terminate */
                    284: -                      endpt = rdpt+1;
                    285: -                      eof = 1;
                    286: -              } else {
                    287: -                      for(p = &rdpt[n]; p >= rdpt;)
                    288: -                              if(*--p == '\n')
                    289: -                                      break;
                    290: -                      if(p < rdpt){   /* line bigger than buf!! */
                    291: -                              rdpt = &buf[sizeof buf/2];      /* chop in half */
                    292: -                              continue;       /* reading */
                    293: -                      }
                    294: -                      endpt = p+1;
                    295: -              }
                    296: -              /*
                    297: -                      invariants:
                    298: -                              no newline between buf and rdpt[-1]
                    299: -                              lnum = nlines before buf
                    300: -                              seek = char offset of buf[0]
                    301: -                              last char read is rdpt[n-1]
                    302: -                              first char after last \n is endpt
                    303: -
                    304: -                      following is exited to three places:
                    305: -                      succeed:        match
                    306: -                      refresh:        !match (readjust ptrs and loop)
                    307: -
                    308: -              */
                    309: -              p = buf+patlen-1;
                    310: -              proc = buf-1;
                    311: -      scan:
                    312: -              for(;;){
                    313: -                      while((p += delta0[*(unsigned char *)p]) < endpt)
                    314: -                              ;
                    315: -                      if(p < (buf+LARGE)){    /* no match */
                    316: -                              goto refresh;
                    317: -                      }
                    318: -                      p -= LARGE;
                    319: -                      for(j = patlen-2, s = p-1; j >= 0; j--)
                    320: -                              if(cmap[*s--] != bmpat[j])
                    321: -                                      break;
                    322: -                      if(j < 0)       /* match!! */
                    323: -                              goto succeed;
                    324: -              fail:
                    325: -                      p++;
                    326: -              }
                    327: -      succeed:
                    328: -              if(bmegrep || !cflag || !sflag){        /* lflag doesn't matter */
                    329: -                      nlp = memchr(p, '\n', (endpt+1)-p);
                    330: -                      op = p;
                    331: -                      p -= patlen-1;
                    332: -                      while(*p != '\n')
                    333: -                              if(--p < buf) break;
                    334: -                      p++;
                    335: -                      if(bmegrep)
                    336: -                              if(legrep(p) == 0){
                    337: -                                      p = op;
                    338: -                                      goto fail;
                    339: -                              }
                    340: -              }
                    341: -              nsucc = 1;
                    342: -              if (cflag){
                    343: -                      tln++;
                    344: -                      p = nlp+1;
                    345: -                      goto scan;
                    346: -              } else if(sflag){
                    347: -                      if(scanexit) exit(0);
                    348: -              } else if(lflag){
                    349: -                      Fprint(1, "%s\n", file);
                    350: -                      close(fd);
                    351: -                      return;
                    352: -              } else {
                    353: -                      if (nfile > 1 && hflag)
                    354: -                              Fprint(1, "%s:", file);
                    355: -                      if (bflag)
                    356: -                              Fprint(1, "%ld:", (seek+(p-buf))/BLKSIZE);
                    357: -                      if (nflag){
                    358: -                              while(np = memchr(proc+1, '\n', p-proc)){
                    359: -                                      lnum++;
                    360: -                                      proc = np;
                    361: -                              }
                    362: -                              Fprint(1, "%ld:", lnum);
                    363: -                      }
                    364: -                      Fwrite(1, p, nlp-p+1);
                    365: -                      p = nlp+1;
                    366: -                      if(p < endpt)
                    367: -                              goto scan;
                    368: -              }
                    369: -      refresh:
                    370: -              if(nflag){      /* count newlines that we haven't proc */
                    371: -                      while(proc = memchr(proc+1, '\n', endpt-proc))
                    372: -                              lnum++;
                    373: -              }
                    374: -              memcpy(buf, endpt, j = &rdpt[n]-endpt);
                    375: -              rdpt = &buf[j];
                    376: -              seek += n;
                    377: -      }
                    378: -done: close(fd);
                    379: -      if (cflag) {
                    380: -              if (nfile > 1)
                    381: -                      Fprint(1, "%s:", file);
                    382: -              Fprint(1, "%ld\n", tln);
                    383: -      }
                    384: -}
                    385: -
                    386: -/*
                    387: -      isn't egrep pretty when it just has to mtach or not?
                    388: -*/
                    389: -
                    390: -legrep(p)
                    391: -      register char *p;
                    392: -{
                    393: -      register State *cstat, *t;
                    394: -
                    395: -      if(reinit == 1)
                    396: -              clearg();
                    397: -      cstat = istat;
                    398: -      if(cstat->out)
                    399: -              return(1);
                    400: -      for(;;){
                    401: -              if((t = cstat->gotofn[*(unsigned char *)p]) == 0)
                    402: -                      cstat = nxtst(cstat, *(unsigned char *)p);
                    403: -              else
                    404: -                      cstat = t;
                    405: -              if(cstat->out){
                    406: -                      return(1);
                    407: -              }
                    408: -              if(*p++ == RIGHT){
                    409: -                      return(0);
                    410: -              }
                    411: -      }
                    412: -}
                    413: //GO.SYSIN DD bm.c
                    414: echo egrep.c 1>&2
                    415: sed 's/.//' >egrep.c <<'//GO.SYSIN DD egrep.c'
                    416: -#include      "hdr.h"
                    417: -#include      "y.tab.h"
                    418: -
                    419: -cgotofn() {
                    420: -      register i;
                    421: -      count = cntpos = 0;
                    422: -      nxtpos = 0;
                    423: -      nxtfoll = MAXPOS-1;
                    424: -      begout = 0;
                    425: -      for (i=1; i<=line; i++) colpos[i] = 0;
                    426: -      if (first(line-1)==0) {
                    427: -              colpos[line] = 1;
                    428: -              cntpos++;
                    429: -              begout = 1;
                    430: -      }
                    431: -      for (i=1; i<=line; i++) tmpstat[i] = begstat[i] = colpos[i];
                    432: -      count = begcnt = cntpos-1;      /* leave out position 1 */
                    433: -      tmpstat[1] = begstat[1] = 0;
                    434: -      addstate(1);
                    435: -      inxtpos = nxtpos;
                    436: -      istat = nxtst(states+1,LEFT);
                    437: -}
                    438: -
                    439: -State *
                    440: -nxtst(ss,c)
                    441: -State *ss;
                    442: -char c;
                    443: -{
                    444: -      register i, num, k;
                    445: -      int pos, curpos, number, newpos;
                    446: -      int s = ss-states;
                    447: -      num = positions[state[s]];
                    448: -      count = begcnt;
                    449: -      for (i=3; i<=line; i++) tmpstat[i] = begstat[i];
                    450: -      pos = state[s] + 1;
                    451: -      for (i=0; i<num; i++) {
                    452: -              curpos = positions[pos];
                    453: -              if ((k = name[curpos]) >= 0) {
                    454: -                      if (
                    455: -                              (k == c)
                    456: -                              | (k == DOT && c != LEFT && c != RIGHT)
                    457: -                              | (k == CCL && member(c, right[curpos], 1))
                    458: -                              | (k == NCCL && member(c, right[curpos], 0) && c != LEFT && c != RIGHT)
                    459: -                      ) {
                    460: -                              if (foll[curpos] == 0) {
                    461: -                                      cntpos = 0;
                    462: -                                      for (k=1; k<=line; k++) colpos[k] = 0;
                    463: -                                      follow(curpos);
                    464: -                                      addfoll(curpos);
                    465: -                              }
                    466: -                              number = positions[foll[curpos]];
                    467: -                              newpos = foll[curpos] - 1;
                    468: -                              for (k=0; k<number; k++) {
                    469: -                                      if (tmpstat[positions[newpos]] != 1) {
                    470: -                                              tmpstat[positions[newpos]] = 1;
                    471: -                                              count++;
                    472: -                                      }
                    473: -                                      newpos--;
                    474: -                              }
                    475: -                      }
                    476: -              }
                    477: -              pos++;
                    478: -      }
                    479: -      if (notin(nstate)) {
                    480: -              if (++nstate >= NSTATES) {
                    481: -                      nxtpos = inxtpos;
                    482: -                      reinit = 1;
                    483: -                      nstate = 1;
                    484: -                      addstate(1);
                    485: -                      return states+1;
                    486: -              }
                    487: -              addstate(nstate);
                    488: -              return(states[s].gotofn[c] = states+nstate);
                    489: -      }
                    490: -      else {
                    491: -              return(states[s].gotofn[c] = states+xstate);
                    492: -      }
                    493: -}
                    494: -
                    495: -first(v) {
                    496: -      register b;
                    497: -      if (left[v] == 0) {
                    498: -              if (colpos[v] != 1) {
                    499: -                      colpos[v] = 1;
                    500: -                      cntpos++;
                    501: -              }
                    502: -              return(1);
                    503: -      }
                    504: -      else if (right[v] == 0) {
                    505: -              if (first(left[v]) == 0) return (0);
                    506: -              else if (name[v] == PLUS) return (1);
                    507: -              else return (0);
                    508: -      }
                    509: -      else if (name[v] == CAT) {
                    510: -              if (first(left[v]) == 0 && first(right[v]) == 0) return (0);
                    511: -              else return (1);
                    512: -      }
                    513: -      else { /* name[v] == OR */
                    514: -              b = first(right[v]);
                    515: -              if (first(left[v]) == 0 || b == 0) return (0);
                    516: -              else return (1);
                    517: -      }
                    518: -}
                    519: -
                    520: -member(symb, set, torf) {
                    521: -      register i, num, pos;
                    522: -      num = chars[set];
                    523: -      pos = set + 1;
                    524: -      for (i=0; i<num; i++)
                    525: -              if (symb == chars[pos++]) return (torf);
                    526: -      return (!torf);
                    527: -}
                    528: -
                    529: -notin(n) {
                    530: -      register i, j, pos;
                    531: -      for (i=1; i<=n; i++) {
                    532: -              if (positions[state[i]] == count) {
                    533: -                      pos = state[i] + 1;
                    534: -                      for (j=0; j < count; j++) {
                    535: -                              if (tmpstat[positions[pos++]] != 1) goto nxt; }
                    536: -                      xstate = i;
                    537: -                      return (0);
                    538: -              }
                    539: -              nxt: ;
                    540: -      }
                    541: -      return (1);
                    542: -}
                    543: -
                    544: -addstate(n) {
                    545: -      register i;
                    546: -      if (nxtpos + count >= nxtfoll) {
                    547: -              overflo(); }
                    548: -      for (i=0; i<NCHARS; i++)
                    549: -              states[n].gotofn[i] = 0;
                    550: -      state[n] = nxtpos;
                    551: -      positions[nxtpos++] = count;
                    552: -      for (i=3; i <= line; i++) {
                    553: -              if (tmpstat[i] == 1) {
                    554: -                      positions[nxtpos++] = i;
                    555: -              }
                    556: -      }
                    557: -      if (tmpstat[line] == 1)
                    558: -              states[n].out = 1;
                    559: -      else
                    560: -              states[n].out = 0;
                    561: -}
                    562: -
                    563: -addfoll(n) {
                    564: -      register i;
                    565: -      if (nxtfoll - cntpos <= nxtpos) {
                    566: -              overflo(); }
                    567: -      foll[n] = nxtfoll;
                    568: -      positions[nxtfoll--] = cntpos;
                    569: -      for (i=3; i <= line; i++) {
                    570: -              if (colpos[i] == 1) {
                    571: -                      positions[nxtfoll--] = i;
                    572: -              }
                    573: -      }
                    574: -}
                    575: -
                    576: -follow(v) int v; {
                    577: -      int p;
                    578: -      if (v == line) return;
                    579: -      p = parent[v];
                    580: -      switch(name[p]) {
                    581: -              case STAR:
                    582: -              case PLUS:      first(v);
                    583: -                              follow(p);
                    584: -                              return;
                    585: -
                    586: -              case OR:
                    587: -              case QUEST:     follow(p);
                    588: -                              return;
                    589: -
                    590: -              case CAT:       if (v == left[p]) {
                    591: -                                      if (first(right[p]) == 0) {
                    592: -                                              follow(p);
                    593: -                                              return;
                    594: -                                      }
                    595: -                              }
                    596: -                              else follow(p);
                    597: -                              return;
                    598: -              case FINAL:     if (colpos[line] != 1) {
                    599: -                                      colpos[line] = 1;
                    600: -                                      cntpos++;
                    601: -                              }
                    602: -                              return;
                    603: -      }
                    604: -}
                    605: -
                    606: -clearg() {
                    607: -      register i;
                    608: -      reinit = 0;
                    609: -      states[1].out = begout;
                    610: -      for (i=0; i<NCHARS; i++)
                    611: -              states[1].gotofn[i] = 0;
                    612: -      nstate = 1;
                    613: -      state[1] = 0;
                    614: -      istat = nxtst(states+1,LEFT);
                    615: -}
                    616: -
                    617: -execute(file)
                    618: -char *file;
                    619: -{
                    620: -      register char *p;
                    621: -      register State *cstat, *t;
                    622: -      int len;
                    623: -      char *nlp;
                    624: -      int f;
                    625: -
                    626: -#define       READLINE        if((p = nlp = Frdline(f)) == 0)\
                    627: -                              goto done;\
                    628: -                      else    /* Frdline nulls the \n, put it back */\
                    629: -                              len = FIOLINELEN(f), nlp[len++] = RIGHT
                    630: -
                    631: -      if (file) {
                    632: -              if ((f = open(file, 0)) < 0) {
                    633: -                      fprint(2, "%s: can't open %s\n", progname, file);
                    634: -                      badbotch=1;
                    635: -                      return;
                    636: -              }
                    637: -      }
                    638: -      else f = 0;
                    639: -      Finit(f, (char *)0);
                    640: -      Ftie(f, 1);             /* link input f with stdout */
                    641: -      lnum = 1;
                    642: -      tln = 0;
                    643: -      READLINE;
                    644: -      cstat = istat;
                    645: -      if (cstat->out) goto found;
                    646: -      for (;;) {
                    647: -              if ((t = cstat->gotofn[*p]) == 0)
                    648: -                      cstat = nxtst(cstat, *p);
                    649: -              else
                    650: -                      cstat = t;
                    651: -              if (cstat->out) {
                    652: -              found:
                    653: -                      if (vflag == 0) {
                    654: -              succeed:        nsucc = 1;
                    655: -                              if (cflag) tln++;
                    656: -                              else if (sflag){
                    657: -                                      if(scanexit) exit(0);
                    658: -                              } else if (lflag) {
                    659: -                                      Fprint(1, "%s\n", file);
                    660: -                                      close(f);
                    661: -                                      return;
                    662: -                              }
                    663: -                              else {
                    664: -                                      if (nfile > 1 && hflag)
                    665: -                                              Fprint(1, "%s:", file);
                    666: -                                      if (bflag)
                    667: -                                              Fprint(1, "%ld:", (FIOSEEK(f)-len)/BLKSIZE);
                    668: -                                      if (nflag)
                    669: -                                              Fprint(1, "%ld:", lnum);
                    670: -                                      Fwrite(1, nlp, len);
                    671: -                              }
                    672: -                      }
                    673: -                      lnum++;
                    674: -                      READLINE;
                    675: -                      if (reinit == 1) clearg();
                    676: -                      if ((cstat = istat)->out)
                    677: -                              goto found;     /* we are a match already */
                    678: -                      else
                    679: -                              continue;       /* normal pattern matching loop */
                    680: -              }
                    681: -              if (*p++ == RIGHT) {
                    682: -                      if (vflag) goto succeed;
                    683: -                      else {
                    684: -                              lnum++;
                    685: -                              READLINE;
                    686: -                              if (reinit == 1) clearg();
                    687: -                              if ((cstat = istat)->out)
                    688: -                                      goto found;     /* we are a match already */
                    689: -                      }
                    690: -              }
                    691: -      }
                    692: -done: close(f);
                    693: -      if (cflag) {
                    694: -              if (nfile > 1)
                    695: -                      Fprint(1, "%s:", file);
                    696: -              Fprint(1, "%ld\n", tln);
                    697: -      }
                    698: -}
                    699: //GO.SYSIN DD egrep.c
                    700: echo main.c 1>&2
                    701: sed 's/.//' >main.c <<'//GO.SYSIN DD main.c'
                    702: -#include      "hdr.h"
                    703: -
                    704: -State states[NSTATES];
                    705: -State *nxtst();
                    706: -int state[NSTATES];
                    707: -int line = 1;
                    708: -int name[MAXLIN];
                    709: -int left[MAXLIN];
                    710: -int right[MAXLIN];
                    711: -int parent[MAXLIN];
                    712: -int foll[MAXLIN];
                    713: -int positions[MAXPOS];
                    714: -char chars[MAXLIN];
                    715: -int nxtpos = 0;
                    716: -int inxtpos;
                    717: -int nxtchar = 0;
                    718: -int tmpstat[MAXLIN];
                    719: -int begstat[MAXLIN];
                    720: -int colpos[MAXLIN];
                    721: -State *istat;
                    722: -int nstate = 1;
                    723: -int xstate;
                    724: -int count;
                    725: -int icount;
                    726: -char *input;
                    727: -char *progname;
                    728: -int begout;
                    729: -int begcnt;
                    730: -int cntpos;
                    731: -int nxtfoll;
                    732: -
                    733: -char reinit = 0;
                    734: -
                    735: -long  lnum;
                    736: -int   bflag;
                    737: -int   cflag;
                    738: -int   fflag;
                    739: -int   hflag = 1;
                    740: -int   iflag;
                    741: -int   lflag;
                    742: -int   nflag;
                    743: -int   sflag;
                    744: -int   vflag;
                    745: -int   nfile;
                    746: -long  tln;
                    747: -int   nsucc;
                    748: -int   badbotch;
                    749: -
                    750: -int   expfile;
                    751: -int   bmegrep = 0;
                    752: -int   scanexit = 0;
                    753: -
                    754: -extern char *optarg;
                    755: -extern int optind, getopt();
                    756: -
                    757: -usage()
                    758: -{
                    759: -      fprint(2, "usage: %s [ -bchilnsv ] [ -e pattern ] [ -f file ] [ pattern ] [ file ] ...\n", progname);
                    760: -      exit(2);
                    761: -}
                    762: -
                    763: -main(argc, argv)
                    764: -char **argv;
                    765: -{
                    766: -      register c;
                    767: -      int errflg = 0;
                    768: -      int (*fn)(), execute(), bmexecute();
                    769: -      int etext();
                    770: -      char *ffile;
                    771: -      char buf[2048];
                    772: -
                    773: -      if(progname = strrchr(argv[0], '/'))
                    774: -              progname++;
                    775: -      else
                    776: -              progname = argv[0];
                    777: -
                    778: -#ifdef forfutureuse
                    779: -      switch (progname[0]) {
                    780: -      case 'f': fgrep++; break;
                    781: -      case 'e': egrep++; break;
                    782: -      case 'g':  grep++; break;
                    783: -      }
                    784: -#endif
                    785: -
                    786: -      while(( c = getopt(argc, argv, "bchie:f:lnsv?")) != -1)
                    787: -              switch(c) {
                    788: -
                    789: -              case 'b':
                    790: -                      bflag++;
                    791: -                      continue;
                    792: -
                    793: -              case 'c':
                    794: -                      cflag++;
                    795: -                      continue;
                    796: -
                    797: -              case 'e':
                    798: -                      input = optarg;
                    799: -                      continue;
                    800: -
                    801: -              case 'f':
                    802: -                      fflag++;
                    803: -                      ffile = optarg;
                    804: -                      continue;
                    805: -
                    806: -              case 'h':
                    807: -                      hflag = 0;
                    808: -                      continue;
                    809: -
                    810: -              case 'i':
                    811: -                      iflag++;
                    812: -                      continue;
                    813: -
                    814: -              case 'l':
                    815: -                      lflag++;
                    816: -                      continue;
                    817: -
                    818: -              case 'n':
                    819: -                      nflag++;
                    820: -                      continue;
                    821: -
                    822: -              case 's':
                    823: -                      sflag++;
                    824: -                      continue;
                    825: -
                    826: -              case 'v':
                    827: -                      vflag++;
                    828: -                      continue;
                    829: -
                    830: -              case '?':
                    831: -                      errflg++;
                    832: -                      continue;
                    833: -              }
                    834: -
                    835: -      if (errflg)
                    836: -              usage();
                    837: -
                    838: -      argc -= optind;
                    839: -      argv += optind;
                    840: -      if (fflag) {
                    841: -              if ((expfile = open(ffile, 0)) < 0) {
                    842: -                      fprint(2, "%s: can't open %s\n", progname, ffile);
                    843: -                      exit(2);
                    844: -              }
                    845: -      } else if (input == 0) {
                    846: -              if ((input = *argv++) == 0)
                    847: -                      usage();
                    848: -              argc--;
                    849: -      }
                    850: -      Finit(1, (char *)0);
                    851: -
                    852: -#ifdef MAILPREP
                    853: -      mailprep();
                    854: -#endif /* MAILPREP */
                    855: -
                    856: -      yyparse();
                    857: -
                    858: -#ifdef MAILPREP
                    859: -      maildone();
                    860: -#endif /* MAILPREP */
                    861: -
                    862: -      if(!vflag && islit(buf)){
                    863: -              bmprep(buf);
                    864: -              fn = bmexecute;
                    865: -      } else
                    866: -              fn = execute;
                    867: -
                    868: -      cgotofn();
                    869: -      nfile = argc;
                    870: -      if (argc<=0) {
                    871: -              if (lflag) exit(1);
                    872: -              scanexit = 1;
                    873: -              (*fn)((char *)0);
                    874: -      }
                    875: -      else while (--argc >= 0) {
                    876: -              if (reinit == 1) clearg();
                    877: -              scanexit = argc == 0;
                    878: -              (*fn)(*argv++);
                    879: -      }
                    880: -      exit(badbotch ? 2 : nsucc==0);
                    881: -}
                    882: //GO.SYSIN DD main.c
                    883: echo y.tab.c 1>&2
                    884: sed 's/.//' >y.tab.c <<'//GO.SYSIN DD y.tab.c'
                    885: -# define CHAR 257
                    886: -# define DOT 258
                    887: -# define CCL 259
                    888: -# define NCCL 260
                    889: -# define OR 261
                    890: -# define CAT 262
                    891: -# define STAR 263
                    892: -# define PLUS 264
                    893: -# define QUEST 265
                    894: -
                    895: -# line 16 "gram.y"
                    896: -#include "hdr.h"
                    897: -#define yyclearin yychar = -1
                    898: -#define yyerrok yyerrflag = 0
                    899: -extern int yychar;
                    900: -extern short yyerrflag;
                    901: -#ifndef YYMAXDEPTH
                    902: -#define YYMAXDEPTH 150
                    903: -#endif
                    904: -#ifndef YYSTYPE
                    905: -#define YYSTYPE int
                    906: -#endif
                    907: -YYSTYPE yylval, yyval;
                    908: -# define YYERRCODE 256
                    909: -
                    910: -# line 63 "gram.y"
                    911: -
                    912: -yyerror(s) {
                    913: -      fprint(2, "egrep: %s\n", s);
                    914: -      exit(2);
                    915: -}
                    916: -
                    917: -yylex() {
                    918: -      extern int yylval;
                    919: -      int cclcnt, x;
                    920: -      register char c, d;
                    921: -      switch(c = nextch()) {
                    922: -              case '^': c = LEFT;
                    923: -                      goto defchar;
                    924: -              case '$': c = RIGHT;
                    925: -                      goto defchar;
                    926: -              case '|': return (OR);
                    927: -              case '*': return (STAR);
                    928: -              case '+': return (PLUS);
                    929: -              case '?': return (QUEST);
                    930: -              case '(': return (c);
                    931: -              case ')': return (c);
                    932: -              case '.': return (DOT);
                    933: -              case '\0': return (0);
                    934: -              case RIGHT: return (OR);
                    935: -              case '[': 
                    936: -                      x = CCL;
                    937: -                      cclcnt = 0;
                    938: -                      count = nxtchar++;
                    939: -                      if ((c = nextch()) == '^') {
                    940: -                              x = NCCL;
                    941: -                              c = nextch();
                    942: -                      }
                    943: -                      do {
                    944: -                              if (c == '\0') synerror();
                    945: -                              if (c == '-' && cclcnt > 0 && chars[nxtchar-1] != 0) {
                    946: -                                      if ((d = nextch()) != 0) {
                    947: -                                              c = chars[nxtchar-1];
                    948: -                                              while (c < d) {
                    949: -                                                      if (iflag && isalpha(c)) {
                    950: -                                                              if (nxtchar >= MAXLIN-1) overflo();
                    951: -                                                                      chars[nxtchar++] = isupper(++c)?tolower(c):toupper(c);
                    952: -                                                                      chars[nxtchar++] = c;
                    953: -                                                                      cclcnt += 2;
                    954: -                                                      }
                    955: -                                                      else {
                    956: -                                                              if (nxtchar >= MAXLIN) overflo();
                    957: -                                                              chars[nxtchar++] = ++c;
                    958: -                                                              cclcnt++;
                    959: -                                                      }
                    960: -                                              }
                    961: -                                              continue;
                    962: -                                      }
                    963: -                              }
                    964: -                              if (iflag&&isalpha(c)) {
                    965: -                                      if (nxtchar >= MAXLIN-1) overflo();
                    966: -                                      chars[nxtchar++] = isupper(c)?tolower(c):toupper(c);
                    967: -                                      chars[nxtchar++] = c;
                    968: -                                      cclcnt += 2;
                    969: -                              }
                    970: -                              else {
                    971: -                                      if (nxtchar >= MAXLIN) overflo();
                    972: -                                      chars[nxtchar++] = c;
                    973: -                                      cclcnt++;
                    974: -                              }
                    975: -                      } while ((c = nextch()) != ']');
                    976: -                      chars[count] = cclcnt;
                    977: -                      return (x);
                    978: -              case '\\':
                    979: -                      if ((c = nextch()) == '\0') synerror();
                    980: -                      else if (c == '\n') c = nextch();
                    981: -              defchar:
                    982: -              default: yylval = c; return (CHAR);
                    983: -      }
                    984: -}
                    985: -
                    986: -static int mailfd = -1;
                    987: -
                    988: -nextch() {
                    989: -      register c;
                    990: -      if (fflag) {
                    991: -              if ((c = Fgetc(expfile)) < 0)
                    992: -                      c = 0;
                    993: -      }
                    994: -      else c = *input++;
                    995: -if(mailfd >= 0) Fputc(mailfd, c? c : '\n');
                    996: -      return(c);
                    997: -}
                    998: -
                    999: -synerror() {
                   1000: -      fprint(2, "egrep: syntax error\n");
                   1001: -      exit(2);
                   1002: -}
                   1003: -
                   1004: -enter(x) int x; {
                   1005: -      if(line >= MAXLIN) overflo();
                   1006: -      name[line] = x;
                   1007: -      left[line] = 0;
                   1008: -      right[line] = 0;
                   1009: -      return(line++);
                   1010: -}
                   1011: -
                   1012: -cclenter(x) int x; {
                   1013: -      register linno;
                   1014: -      linno = enter(x);
                   1015: -      right[linno] = count;
                   1016: -      return (linno);
                   1017: -}
                   1018: -
                   1019: -node(x, l, r) {
                   1020: -      if(line >= MAXLIN) overflo();
                   1021: -      name[line] = x;
                   1022: -      left[line] = l;
                   1023: -      right[line] = r;
                   1024: -      parent[l] = line;
                   1025: -      parent[r] = line;
                   1026: -      return(line++);
                   1027: -}
                   1028: -
                   1029: -unary(x, d) {
                   1030: -      if(line >= MAXLIN) overflo();
                   1031: -      name[line] = x;
                   1032: -      left[line] = d;
                   1033: -      right[line] = 0;
                   1034: -      parent[d] = line;
                   1035: -      return(line++);
                   1036: -}
                   1037: -overflo() {
                   1038: -      fprint(2, "egrep: regular expression too long\n");
                   1039: -      exit(2);
                   1040: -}
                   1041: -#include      <errno.h>
                   1042: -#define               NAME            "/tmp/grepdata"
                   1043: -mailprep()
                   1044: -{
                   1045: -      umask(0);
                   1046: -      mailfd = open(NAME, 1);
                   1047: -      if((mailfd < 0) && (errno != ECONC))
                   1048: -              mailfd = creat(NAME, 03666);
                   1049: -      if(mailfd >= 0){
                   1050: -              Finit(mailfd, (char *)0);
                   1051: -              Fseek(mailfd, 0L, 2);
                   1052: -              Fprint(mailfd, "\321egrep: ");
                   1053: -      }
                   1054: -}
                   1055: -
                   1056: -maildone()
                   1057: -{
                   1058: -      if(mailfd >= 0){
                   1059: -              Fflush(mailfd);
                   1060: -              close(mailfd);
                   1061: -      }
                   1062: -}
                   1063: -short yyexca[] ={
                   1064: --1, 1,
                   1065: -      0, -1,
                   1066: -      -2, 0,
                   1067: --1, 5,
                   1068: -      0, 2,
                   1069: -      -2, 0,
                   1070: --1, 13,
                   1071: -      0, 5,
                   1072: -      -2, 0,
                   1073: --1, 14,
                   1074: -      0, 12,
                   1075: -      257, 12,
                   1076: -      258, 12,
                   1077: -      259, 12,
                   1078: -      260, 12,
                   1079: -      261, 12,
                   1080: -      40, 12,
                   1081: -      41, 12,
                   1082: -      -2, 0,
                   1083: --1, 19,
                   1084: -      0, 4,
                   1085: -      -2, 0,
                   1086: --1, 20,
                   1087: -      0, 11,
                   1088: -      261, 11,
                   1089: -      41, 11,
                   1090: -      -2, 0,
                   1091: --1, 23,
                   1092: -      0, 3,
                   1093: -      -2, 0,
                   1094: -      };
                   1095: -# define YYNPROD 18
                   1096: -# define YYLAST 261
                   1097: -short yyact[]={
                   1098: -
                   1099: -  10,  22,   4,  14,  11,   2,   1,   5,   0,   0,
                   1100: -  10,  15,  16,  17,  18,   0,  19,  20,   3,   0,
                   1101: -  10,   0,   0,  12,   0,  20,   0,  20,   0,   0,
                   1102: -  10,   0,   0,   0,   0,   0,   0,   0,   0,   0,
                   1103: -  10,   0,   0,   0,   0,   0,   0,   0,   0,   0,
                   1104: -   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
                   1105: -   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
                   1106: -   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
                   1107: -   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
                   1108: -   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
                   1109: -   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
                   1110: -   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
                   1111: -   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
                   1112: -   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
                   1113: -   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
                   1114: -   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
                   1115: -   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
                   1116: -   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
                   1117: -   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
                   1118: -   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
                   1119: -   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
                   1120: -   0,   0,   0,   0,   0,   0,  11,   6,   7,   8,
                   1121: -   9,  21,   0,  15,  16,  17,  11,   6,   7,   8,
                   1122: -   9,  23,   0,  15,  16,  17,  11,   6,   7,   8,
                   1123: -   9,  13,   0,  15,  16,  17,  11,   6,   7,   8,
                   1124: -   9,   0,   0,  15,  16,  17,  11,   6,   7,   8,
                   1125: -   9 };
                   1126: -short yypact[]={
                   1127: -
                   1128: --259,-1000,-1000,   0,-1000, -20,-1000,-1000,-1000,-1000,
                   1129: -   0,-1000,   0,   0,-252,-1000,-1000,-1000, -40, -30,
                   1130: - -10,   0,-1000,   0 };
                   1131: -short yypgo[]={
                   1132: -
                   1133: -   0,   6,   5,  18,   3 };
                   1134: -short yyr1[]={
                   1135: -
                   1136: -   0,   1,   2,   2,   2,   2,   3,   4,   4,   4,
                   1137: -   4,   4,   4,   4,   4,   4,   4,   4 };
                   1138: -short yyr2[]={
                   1139: -
                   1140: -   0,   1,   2,   4,   3,   3,   0,   1,   1,   1,
                   1141: -   1,   3,   2,   2,   2,   2,   3,   1 };
                   1142: -short yychk[]={
                   1143: -
                   1144: --1000,  -1,  -2,  -3, 261,  -4, 257, 258, 259, 260,
                   1145: -  40, 256,  -3, 261,  -4, 263, 264, 265,  -4,  -4,
                   1146: -  -4, 261,  41, 261 };
                   1147: -short yydef[]={
                   1148: -
                   1149: -   6,  -2,   1,   0,   6,  -2,   7,   8,   9,  10,
                   1150: -   0,  17,   0,  -2,  -2,  13,  14,  15,   0,  -2,
                   1151: -  -2,   0,  16,  -2 };
                   1152: -# ifdef YYDEBUG
                   1153: -# include "y.debug"
                   1154: -# endif
                   1155: -
                   1156: -# define YYFLAG -1000
                   1157: -# define YYERROR goto yyerrlab
                   1158: -# define YYACCEPT return(0)
                   1159: -# define YYABORT return(1)
                   1160: -
                   1161: -/*    parser for yacc output  */
                   1162: -
                   1163: -#ifdef YYDEBUG
                   1164: -int yydebug = 0; /* 1 for debugging */
                   1165: -#endif
                   1166: -YYSTYPE yyv[YYMAXDEPTH]; /* where the values are stored */
                   1167: -int yychar = -1; /* current input token number */
                   1168: -int yynerrs = 0;  /* number of errors */
                   1169: -short yyerrflag = 0;  /* error recovery flag */
                   1170: -
                   1171: -yyparse()
                   1172: -{     short yys[YYMAXDEPTH];
                   1173: -      int yyj, yym;
                   1174: -      register YYSTYPE *yypvt;
                   1175: -      register int yystate, yyn;
                   1176: -      register short *yyps;
                   1177: -      register YYSTYPE *yypv;
                   1178: -      register short *yyxi;
                   1179: -
                   1180: -      yystate = 0;
                   1181: -      yychar = -1;
                   1182: -      yynerrs = 0;
                   1183: -      yyerrflag = 0;
                   1184: -      yyps= &yys[-1];
                   1185: -      yypv= &yyv[-1];
                   1186: -
                   1187: -yystack:    /* put a state and value onto the stack */
                   1188: -#ifdef YYDEBUG
                   1189: -      if(yydebug >= 3)
                   1190: -              if(yychar < 0 || yytoknames[yychar] == 0)
                   1191: -                      printf("char %d in %s", yychar, yystates[yystate]);
                   1192: -              else
                   1193: -                      printf("%s in %s", yytoknames[yychar], yystates[yystate]);
                   1194: -#endif
                   1195: -      if( ++yyps >= &yys[YYMAXDEPTH] ) { 
                   1196: -              yyerror( "yacc stack overflow" ); 
                   1197: -              return(1); 
                   1198: -      }
                   1199: -      *yyps = yystate;
                   1200: -      ++yypv;
                   1201: -      *yypv = yyval;
                   1202: -yynewstate:
                   1203: -      yyn = yypact[yystate];
                   1204: -      if(yyn <= YYFLAG) goto yydefault; /* simple state */
                   1205: -      if(yychar<0) {
                   1206: -              yychar = yylex();
                   1207: -#ifdef YYDEBUG
                   1208: -              if(yydebug >= 2) {
                   1209: -                      if(yychar <= 0)
                   1210: -                              printf("lex EOF\n");
                   1211: -                      else if(yytoknames[yychar])
                   1212: -                              printf("lex %s\n", yytoknames[yychar]);
                   1213: -                      else
                   1214: -                              printf("lex (%c)\n", yychar);
                   1215: -              }
                   1216: -#endif
                   1217: -              if(yychar < 0)
                   1218: -                      yychar = 0;
                   1219: -      }
                   1220: -      if((yyn += yychar) < 0 || yyn >= YYLAST)
                   1221: -              goto yydefault;
                   1222: -      if( yychk[ yyn=yyact[ yyn ] ] == yychar ){ /* valid shift */
                   1223: -              yychar = -1;
                   1224: -              yyval = yylval;
                   1225: -              yystate = yyn;
                   1226: -              if( yyerrflag > 0 ) --yyerrflag;
                   1227: -              goto yystack;
                   1228: -      }
                   1229: -yydefault:
                   1230: -      /* default state action */
                   1231: -      if( (yyn=yydef[yystate]) == -2 ) {
                   1232: -              if(yychar < 0) {
                   1233: -                      yychar = yylex();
                   1234: -#ifdef YYDEBUG
                   1235: -                      if(yydebug >= 2)
                   1236: -                              if(yychar < 0)
                   1237: -                                      printf("lex EOF\n");
                   1238: -                              else
                   1239: -                                      printf("lex %s\n", yytoknames[yychar]);
                   1240: -#endif
                   1241: -                      if(yychar < 0)
                   1242: -                              yychar = 0;
                   1243: -              }
                   1244: -              /* look through exception table */
                   1245: -              for(yyxi=yyexca; (*yyxi!= (-1)) || (yyxi[1]!=yystate);
                   1246: -                      yyxi += 2 ) ; /* VOID */
                   1247: -              while( *(yyxi+=2) >= 0 ){
                   1248: -                      if( *yyxi == yychar ) break;
                   1249: -              }
                   1250: -              if( (yyn = yyxi[1]) < 0 ) return(0);   /* accept */
                   1251: -      }
                   1252: -      if( yyn == 0 ){ /* error */
                   1253: -              /* error ... attempt to resume parsing */
                   1254: -              switch( yyerrflag ){
                   1255: -              case 0:   /* brand new error */
                   1256: -#ifdef YYDEBUG
                   1257: -                      yyerror("syntax error\n%s", yystates[yystate]);
                   1258: -                      if(yytoknames[yychar])
                   1259: -                              yyerror("saw %s\n", yytoknames[yychar]);
                   1260: -                      else if(yychar >= ' ' && yychar < '\177')
                   1261: -                              yyerror("saw `%c'\n", yychar);
                   1262: -                      else if(yychar == 0)
                   1263: -                              yyerror("saw EOF\n");
                   1264: -                      else
                   1265: -                              yyerror("saw char 0%o\n", yychar);
                   1266: -#else
                   1267: -                      yyerror( "syntax error" );
                   1268: -#endif
                   1269: -yyerrlab:
                   1270: -                      ++yynerrs;
                   1271: -              case 1:
                   1272: -              case 2: /* incompletely recovered error ... try again */
                   1273: -                      yyerrflag = 3;
                   1274: -                      /* find a state where "error" is a legal shift action */
                   1275: -                      while ( yyps >= yys ) {
                   1276: -                              yyn = yypact[*yyps] + YYERRCODE;
                   1277: -                              if( yyn>= 0 && yyn < YYLAST && yychk[yyact[yyn]] == YYERRCODE ){
                   1278: -                                      yystate = yyact[yyn];  /* simulate a shift of "error" */
                   1279: -                                      goto yystack;
                   1280: -                              }
                   1281: -                              yyn = yypact[*yyps];
                   1282: -                              /* the current yyps has no shift onn "error", pop stack */
                   1283: -#ifdef YYDEBUG
                   1284: -                              if( yydebug ) printf( "error recovery pops state %d, uncovers %d\n", *yyps, yyps[-1] );
                   1285: -#endif
                   1286: -                              --yyps;
                   1287: -                              --yypv;
                   1288: -                      }
                   1289: -                      /* there is no state on the stack with an error shift ... abort */
                   1290: -yyabort:
                   1291: -                      return(1);
                   1292: -              case 3:  /* no shift yet; clobber input char */
                   1293: -#ifdef YYDEBUG
                   1294: -                      if( yydebug ) {
                   1295: -                              printf("error recovery discards ");
                   1296: -                              if(yytoknames[yychar])
                   1297: -                                      printf("%s\n", yytoknames[yychar]);
                   1298: -                              else if(yychar >= ' ' && yychar < '\177')
                   1299: -                                      printf("`%c'\n", yychar);
                   1300: -                              else if(yychar == 0)
                   1301: -                                      printf("EOF\n");
                   1302: -                              else
                   1303: -                                      printf("char 0%o\n", yychar);
                   1304: -                      }
                   1305: -#endif
                   1306: -                      if( yychar == 0 ) goto yyabort; /* don't discard EOF, quit */
                   1307: -                      yychar = -1;
                   1308: -                      goto yynewstate;   /* try again in the same state */
                   1309: -              }
                   1310: -      }
                   1311: -      /* reduction by production yyn */
                   1312: -#ifdef YYDEBUG
                   1313: -      if(yydebug) {   char *s;
                   1314: -              printf("reduce %d in:\n\t", yyn);
                   1315: -              for(s = yystates[yystate]; *s; s++) {
                   1316: -                      putchar(*s);
                   1317: -                      if(*s == '\n' && *(s+1))
                   1318: -                              putchar('\t');
                   1319: -              }
                   1320: -      }
                   1321: -#endif
                   1322: -      yyps -= yyr2[yyn];
                   1323: -      yypvt = yypv;
                   1324: -      yypv -= yyr2[yyn];
                   1325: -      yyval = yypv[1];
                   1326: -      yym=yyn;
                   1327: -      /* consult goto table to find next state */
                   1328: -      yyn = yyr1[yyn];
                   1329: -      yyj = yypgo[yyn] + *yyps + 1;
                   1330: -      if( yyj>=YYLAST || yychk[ yystate = yyact[yyj] ] != -yyn ) yystate = yyact[yypgo[yyn]];
                   1331: -      switch(yym){
                   1332: -              
                   1333: -case 1:
                   1334: -# line 21 "gram.y"
                   1335: -{ unary(FINAL, yypvt[-0]);
                   1336: -                line--;
                   1337: -              } break;
                   1338: -case 2:
                   1339: -# line 26 "gram.y"
                   1340: -{ yyval = node(CAT, yypvt[-1], yypvt[-0]); } break;
                   1341: -case 3:
                   1342: -# line 28 "gram.y"
                   1343: -{ yyval = node(CAT, yypvt[-2], yypvt[-1]); } break;
                   1344: -case 4:
                   1345: -# line 30 "gram.y"
                   1346: -{ yyval = node(CAT, yypvt[-1], yypvt[-0]); } break;
                   1347: -case 5:
                   1348: -# line 32 "gram.y"
                   1349: -{ yyval = node(CAT, yypvt[-2], yypvt[-1]); } break;
                   1350: -case 6:
                   1351: -# line 35 "gram.y"
                   1352: -{ yyval = enter(DOT);
                   1353: -                 yyval = unary(STAR, yyval); } break;
                   1354: -case 7:
                   1355: -# line 39 "gram.y"
                   1356: -{ yyval = iflag?node(OR, enter(tolower(yypvt[-0])), enter(toupper(yypvt[-0]))):enter(yypvt[-0]); } break;
                   1357: -case 8:
                   1358: -# line 41 "gram.y"
                   1359: -{ yyval = enter(DOT); } break;
                   1360: -case 9:
                   1361: -# line 43 "gram.y"
                   1362: -{ yyval = cclenter(CCL); } break;
                   1363: -case 10:
                   1364: -# line 45 "gram.y"
                   1365: -{ yyval = cclenter(NCCL); } break;
                   1366: -case 11:
                   1367: -# line 49 "gram.y"
                   1368: -{ yyval = node(OR, yypvt[-2], yypvt[-0]); } break;
                   1369: -case 12:
                   1370: -# line 51 "gram.y"
                   1371: -{ yyval = node(CAT, yypvt[-1], yypvt[-0]); } break;
                   1372: -case 13:
                   1373: -# line 53 "gram.y"
                   1374: -{ yyval = unary(STAR, yypvt[-1]); } break;
                   1375: -case 14:
                   1376: -# line 55 "gram.y"
                   1377: -{ yyval = unary(PLUS, yypvt[-1]); } break;
                   1378: -case 15:
                   1379: -# line 57 "gram.y"
                   1380: -{ yyval = unary(QUEST, yypvt[-1]); } break;
                   1381: -case 16:
                   1382: -# line 59 "gram.y"
                   1383: -{ yyval = yypvt[-1]; } break;
                   1384: -      }
                   1385: -      goto yystack;  /* stack new state and value */
                   1386: -}
                   1387: //GO.SYSIN DD y.tab.c
                   1388: echo gram.y 1>&2
                   1389: sed 's/.//' >gram.y <<'//GO.SYSIN DD gram.y'
                   1390: -/*
                   1391: - * egrep -- print lines containing (or not containing) a regular expression
                   1392: - *
                   1393: - *    status returns:
                   1394: - *            0 - ok, and some matches
                   1395: - *            1 - ok, but no matches
                   1396: - *            2 - some error; matches irrelevant
                   1397: - */
                   1398: -%token CHAR DOT CCL NCCL OR CAT STAR PLUS QUEST
                   1399: -%left OR
                   1400: -%left CHAR DOT CCL NCCL '('
                   1401: -%left CAT
                   1402: -%left STAR PLUS QUEST
                   1403: -
                   1404: -%{
                   1405: -#include "hdr.h"
                   1406: -%}
                   1407: -
                   1408: -%%
                   1409: -s:    t
                   1410: -              { unary(FINAL, $1);
                   1411: -                line--;
                   1412: -              }
                   1413: -      ;
                   1414: -t:    b r
                   1415: -              { $$ = node(CAT, $1, $2); }
                   1416: -      | OR b r OR
                   1417: -              { $$ = node(CAT, $2, $3); }
                   1418: -      | OR b r
                   1419: -              { $$ = node(CAT, $2, $3); }
                   1420: -      | b r OR
                   1421: -              { $$ = node(CAT, $1, $2); }
                   1422: -      ;
                   1423: -b:
                   1424: -              { $$ = enter(DOT);
                   1425: -                 $$ = unary(STAR, $$); }
                   1426: -      ;
                   1427: -r:    CHAR
                   1428: -              { $$ = iflag?node(OR, enter(tolower($1)), enter(toupper($1))):enter($1); }
                   1429: -      | DOT
                   1430: -              { $$ = enter(DOT); }
                   1431: -      | CCL
                   1432: -              { $$ = cclenter(CCL); }
                   1433: -      | NCCL
                   1434: -              { $$ = cclenter(NCCL); }
                   1435: -      ;
                   1436: -
                   1437: -r:    r OR r
                   1438: -              { $$ = node(OR, $1, $3); }
                   1439: -      | r r %prec CAT
                   1440: -              { $$ = node(CAT, $1, $2); }
                   1441: -      | r STAR
                   1442: -              { $$ = unary(STAR, $1); }
                   1443: -      | r PLUS
                   1444: -              { $$ = unary(PLUS, $1); }
                   1445: -      | r QUEST
                   1446: -              { $$ = unary(QUEST, $1); }
                   1447: -      | '(' r ')'
                   1448: -              { $$ = $2; }
                   1449: -      | error 
                   1450: -      ;
                   1451: -
                   1452: -%%
                   1453: -yyerror(s) {
                   1454: -      fprint(2, "%s: %s\n", progname, s);
                   1455: -      exit(2);
                   1456: -}
                   1457: -
                   1458: -yylex() {
                   1459: -      extern int yylval;
                   1460: -      int cclcnt, x;
                   1461: -      register char c, d;
                   1462: -      switch(c = nextch()) {
                   1463: -              case '^': c = LEFT;
                   1464: -                      goto defchar;
                   1465: -              case '$': c = RIGHT;
                   1466: -                      goto defchar;
                   1467: -              case '|': return (OR);
                   1468: -              case '*': return (STAR);
                   1469: -              case '+': return (PLUS);
                   1470: -              case '?': return (QUEST);
                   1471: -              case '(': return (c);
                   1472: -              case ')': return (c);
                   1473: -              case '.': return (DOT);
                   1474: -              case '\0': return (0);
                   1475: -              case RIGHT: return (OR);
                   1476: -              case '[': 
                   1477: -                      x = CCL;
                   1478: -                      cclcnt = 0;
                   1479: -                      count = nxtchar++;
                   1480: -                      if ((c = nextch()) == '^') {
                   1481: -                              x = NCCL;
                   1482: -                              c = nextch();
                   1483: -                      }
                   1484: -                      do {
                   1485: -                              if (c == '\0') synerror();
                   1486: -                              if (c == '-' && cclcnt > 0 && chars[nxtchar-1] != 0) {
                   1487: -                                      if ((d = nextch()) != 0) {
                   1488: -                                              c = chars[nxtchar-1];
                   1489: -                                              while (c < d) {
                   1490: -                                                      if (iflag && isalpha(c)) {
                   1491: -                                                              if (nxtchar >= MAXLIN-1) overflo();
                   1492: -                                                                      chars[nxtchar++] = isupper(++c)?tolower(c):toupper(c);
                   1493: -                                                                      chars[nxtchar++] = c;
                   1494: -                                                                      cclcnt += 2;
                   1495: -                                                      }
                   1496: -                                                      else {
                   1497: -                                                              if (nxtchar >= MAXLIN) overflo();
                   1498: -                                                              chars[nxtchar++] = ++c;
                   1499: -                                                              cclcnt++;
                   1500: -                                                      }
                   1501: -                                              }
                   1502: -                                              continue;
                   1503: -                                      }
                   1504: -                              }
                   1505: -                              if (iflag&&isalpha(c)) {
                   1506: -                                      if (nxtchar >= MAXLIN-1) overflo();
                   1507: -                                      chars[nxtchar++] = isupper(c)?tolower(c):toupper(c);
                   1508: -                                      chars[nxtchar++] = c;
                   1509: -                                      cclcnt += 2;
                   1510: -                              }
                   1511: -                              else {
                   1512: -                                      if (nxtchar >= MAXLIN) overflo();
                   1513: -                                      chars[nxtchar++] = c;
                   1514: -                                      cclcnt++;
                   1515: -                              }
                   1516: -                      } while ((c = nextch()) != ']');
                   1517: -                      chars[count] = cclcnt;
                   1518: -                      return (x);
                   1519: -              case '\\':
                   1520: -                      if ((c = nextch()) == '\0') synerror();
                   1521: -                      else if (c == '\n') c = nextch();
                   1522: -              defchar:
                   1523: -              default: yylval = c; return (CHAR);
                   1524: -      }
                   1525: -}
                   1526: -
                   1527: -#ifdef MAILPREP
                   1528: -static int mailfd = -1;
                   1529: -#endif /* MAILPREP */
                   1530: -
                   1531: -nextch() {
                   1532: -      register c;
                   1533: -      if (fflag) {
                   1534: -              if ((c = Fgetc(expfile)) < 0)
                   1535: -                      c = 0;
                   1536: -      }
                   1537: -      else c = *input++;
                   1538: -#ifdef MAILPREP
                   1539: -if(mailfd >= 0) Fputc(mailfd, c? c : '\n');
                   1540: -#endif /* MAILPREP */
                   1541: -      return(c);
                   1542: -}
                   1543: -
                   1544: -synerror() {
                   1545: -      fprint(2, "%s: syntax error\n", progname);
                   1546: -      exit(2);
                   1547: -}
                   1548: -
                   1549: -enter(x) int x; {
                   1550: -      if(line >= MAXLIN) overflo();
                   1551: -      name[line] = x;
                   1552: -      left[line] = 0;
                   1553: -      right[line] = 0;
                   1554: -      return(line++);
                   1555: -}
                   1556: -
                   1557: -cclenter(x) int x; {
                   1558: -      register linno;
                   1559: -      linno = enter(x);
                   1560: -      right[linno] = count;
                   1561: -      return (linno);
                   1562: -}
                   1563: -
                   1564: -node(x, l, r) {
                   1565: -      if(line >= MAXLIN) overflo();
                   1566: -      name[line] = x;
                   1567: -      left[line] = l;
                   1568: -      right[line] = r;
                   1569: -      parent[l] = line;
                   1570: -      parent[r] = line;
                   1571: -      return(line++);
                   1572: -}
                   1573: -
                   1574: -unary(x, d) {
                   1575: -      if(line >= MAXLIN) overflo();
                   1576: -      name[line] = x;
                   1577: -      left[line] = d;
                   1578: -      right[line] = 0;
                   1579: -      parent[d] = line;
                   1580: -      return(line++);
                   1581: -}
                   1582: -overflo() {
                   1583: -      fprint(2, "%s: regular expression too long\n", progname);
                   1584: -      exit(2);
                   1585: -}
                   1586: -
                   1587: -#ifdef MAILPREP
                   1588: -#include      <errno.h>
                   1589: -#define               NAME            "/tmp/grepdata"
                   1590: -mailprep()
                   1591: -{
                   1592: -      umask(0);
                   1593: -      mailfd = open(NAME, 1);
                   1594: -      if((mailfd < 0) && (errno != ECONC))
                   1595: -              mailfd = creat(NAME, 03666);
                   1596: -      if(mailfd >= 0){
                   1597: -              Finit(mailfd, (char *)0);
                   1598: -              Fseek(mailfd, 0L, 2);
                   1599: -              Fprint(mailfd, "\321egrep: ");
                   1600: -      }
                   1601: -}
                   1602: -
                   1603: -maildone()
                   1604: -{
                   1605: -      if(mailfd >= 0){
                   1606: -              Fflush(mailfd);
                   1607: -              close(mailfd);
                   1608: -      }
                   1609: -}
                   1610: -#endif /* MAILPREP */
                   1611: //GO.SYSIN DD gram.y

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.