BSD 4 release
[unix-history] / usr / src / cmd / fgrep.c
index 1c8eba9..0c2b97a 100644 (file)
@@ -1,3 +1,4 @@
+static char *sccsid = "@(#)fgrep.c     4.1 (Berkeley) 10/1/80";
 /*
  * fgrep -- print all lines containing any of a set of keywords
  *
 /*
  * fgrep -- print all lines containing any of a set of keywords
  *
@@ -7,7 +8,8 @@
  *             2 - some error
  */
 
  *             2 - some error
  */
 
-#include <stdio.h>
+#include "stdio.h"
+# include "ctype.h"
 
 #define        MAXSIZ 6000
 #define QSIZE 400
 
 #define        MAXSIZ 6000
 #define QSIZE 400
@@ -20,11 +22,12 @@ struct words {
 } w[MAXSIZ], *smax, *q;
 
 long   lnum;
 } w[MAXSIZ], *smax, *q;
 
 long   lnum;
-int    bflag, cflag, fflag, lflag, nflag, vflag, xflag;
+int    bflag, cflag, fflag, lflag, nflag, vflag, xflag, yflag;
 int    hflag   = 1;
 int    sflag;
 int    hflag   = 1;
 int    sflag;
+int    retcode = 0;
 int    nfile;
 int    nfile;
-int    blkno;
+long   blkno;
 int    nsucc;
 long   tln;
 FILE   *wordf;
 int    nsucc;
 long   tln;
 FILE   *wordf;
@@ -77,8 +80,12 @@ char **argv;
                        xflag++;
                        continue;
 
                        xflag++;
                        continue;
 
+               case 'i':               /* Berkeley */
+               case 'y':               /* Btl */
+                       yflag++;
+                       continue;
                default:
                default:
-                       fprintf(stderr, "egrep: unknown flag\n");
+                       fprintf(stderr, "fgrep: unknown flag\n");
                        continue;
                }
 out:
                        continue;
                }
 out:
@@ -87,7 +94,7 @@ out:
        if (fflag) {
                wordf = fopen(*argv, "r");
                if (wordf==NULL) {
        if (fflag) {
                wordf = fopen(*argv, "r");
                if (wordf==NULL) {
-                       fprintf(stderr, "egrep: can't open %s\n", *argv);
+                       fprintf(stderr, "fgrep: can't open %s\n", *argv);
                        exit(2);
                }
        }
                        exit(2);
                }
        }
@@ -106,23 +113,27 @@ out:
                execute(*argv);
                argv++;
        }
                execute(*argv);
                argv++;
        }
-       exit(nsucc == 0);
+       exit(retcode != 0 ? retcode : nsucc == 0);
 }
 
 }
 
+# define ccomp(a,b) (yflag ? lca(a)==lca(b) : a==b)
+# define lca(x) (isupper(x) ? tolower(x) : x)
 execute(file)
 char *file;
 {
 execute(file)
 char *file;
 {
-       register char *p;
        register struct words *c;
        register ccount;
        register struct words *c;
        register ccount;
-       char buf[1024];
+       register char ch;
+       register char *p;
+       char buf[2*BUFSIZ];
        int f;
        int f;
-       int failed, ecnt;
+       int failed;
        char *nlp;
        if (file) {
                if ((f = open(file, 0)) < 0) {
                        fprintf(stderr, "fgrep: can't open %s\n", file);
        char *nlp;
        if (file) {
                if ((f = open(file, 0)) < 0) {
                        fprintf(stderr, "fgrep: can't open %s\n", file);
-                       exit(2);
+                       retcode = 2;
+                       return;
                }
        }
        else f = 0;
                }
        }
        else f = 0;
@@ -130,21 +141,21 @@ char *file;
        failed = 0;
        lnum = 1;
        tln = 0;
        failed = 0;
        lnum = 1;
        tln = 0;
-       blkno = -1;
+       blkno = 0;
        p = buf;
        nlp = p;
        c = w;
        for (;;) {
                if (--ccount <= 0) {
        p = buf;
        nlp = p;
        c = w;
        for (;;) {
                if (--ccount <= 0) {
-                       if (p == &buf[1024]) p = buf;
-                       if (p > &buf[512]) {
-                               if ((ccount = read(f, p, &buf[1024] - p)) <= 0) break;
+                       if (p == &buf[2*BUFSIZ]) p = buf;
+                       if (p > &buf[BUFSIZ]) {
+                               if ((ccount = read(f, p, &buf[2*BUFSIZ] - p)) <= 0) break;
                        }
                        }
-                       else if ((ccount = read(f, p, 512)) <= 0) break;
-                       blkno++;
+                       else if ((ccount = read(f, p, BUFSIZ)) <= 0) break;
+                       blkno += ccount;
                }
                nstate:
                }
                nstate:
-                       if (c->inp == *p) {
+                       if (ccomp(c->inp, *p)) {
                                c = c->nst;
                        }
                        else if (c->link != 0) {
                                c = c->nst;
                        }
                        else if (c->link != 0) {
@@ -157,7 +168,7 @@ char *file;
                                if (c==0) {
                                        c = w;
                                        istate:
                                if (c==0) {
                                        c = w;
                                        istate:
-                                       if (c->inp == *p) {
+                                       if (ccomp(c->inp ,  *p)) {
                                                c = c->nst;
                                        }
                                        else if (c->link != 0) {
                                                c = c->nst;
                                        }
                                        else if (c->link != 0) {
@@ -168,42 +179,38 @@ char *file;
                                else goto nstate;
                        }
                if (c->out) {
                                else goto nstate;
                        }
                if (c->out) {
-                       ecnt = 0;
                        while (*p++ != '\n') {
                        while (*p++ != '\n') {
-                               ecnt++;
-                                  if (--ccount <= 0) {
-                                       if (p == &buf[1024]) p = buf;
-                                       if (p > &buf[512]) {
-                                               if ((ccount = read(f, p, &buf[1024] - p)) <= 0) break;
+                               if (--ccount <= 0) {
+                                       if (p == &buf[2*BUFSIZ]) p = buf;
+                                       if (p > &buf[BUFSIZ]) {
+                                               if ((ccount = read(f, p, &buf[2&BUFSIZ] - p)) <= 0) break;
                                        }
                                        }
-                                       else if ((ccount = read(f, p, 512)) <= 0) break;
-                                       blkno++;
-                                  }
-                       }
-                       if (vflag == 0) {
-                               if (xflag)
-                                       if (failed || ecnt > 1) goto nogood;
-               succeed:        nsucc = 1;
-                               if (cflag) tln++;
-                               else if (sflag)
-                                       ;       /* ugh */
-                               else if (lflag) {
-                                       printf("%s\n", file);
-                                       close(f);
-                                       return;
+                                       else if ((ccount = read(f, p, BUFSIZ)) <= 0) break;
+                                       blkno += ccount;
                                }
                                }
-                               else {
-                                       if (nfile > 1 && hflag) printf("%s:", file);
-                                       if (bflag) printf("%d:", blkno);
-                                       if (nflag) printf("%ld:", lnum);
-                                       if (p <= nlp) {
-                                               while (nlp < &buf[1024]) putchar(*nlp++);
-                                               nlp = buf;
-                                       }
-                                       while (nlp < p) putchar(*nlp++);
+                       }
+                       if ( (vflag && (failed == 0 || xflag == 0)) || (vflag == 0 && xflag && failed) )
+                               goto nomatch;
+       succeed:        nsucc = 1;
+                       if (cflag) tln++;
+                       else if (sflag)
+                               ;       /* ugh */
+                       else if (lflag) {
+                               printf("%s\n", file);
+                               close(f);
+                               return;
+                       }
+                       else {
+                               if (nfile > 1 && hflag) printf("%s:", file);
+                               if (bflag) printf("%ld:", (blkno-ccount-1)/BUFSIZ);
+                               if (nflag) printf("%ld:", lnum);
+                               if (p <= nlp) {
+                                       while (nlp < &buf[2*BUFSIZ]) putchar(*nlp++);
+                                       nlp = buf;
                                }
                                }
+                               while (nlp < p) putchar(*nlp++);
                        }
                        }
-               nogood: lnum++;
+       nomatch:        lnum++;
                        nlp = p;
                        c = w;
                        failed = 0;
                        nlp = p;
                        c = w;
                        failed = 0;
@@ -307,6 +314,7 @@ cfail() {
        struct words *queue[QSIZE];
        struct words **front, **rear;
        struct words *state;
        struct words *queue[QSIZE];
        struct words **front, **rear;
        struct words *state;
+       int bstart;
        register char c;
        register struct words *s;
        s = w;
        register char c;
        register struct words *s;
        s = w;
@@ -325,6 +333,7 @@ init:       if ((s->inp) != 0) {
                        front = queue;
                else front++;
        cloop:  if ((c = s->inp) != 0) {
                        front = queue;
                else front++;
        cloop:  if ((c = s->inp) != 0) {
+                       bstart = 0;
                        *rear = (q = s->nst);
                        if (front < rear)
                                if (rear >= &queue[QSIZE-1])
                        *rear = (q = s->nst);
                        if (front < rear)
                                if (rear >= &queue[QSIZE-1])
@@ -334,14 +343,21 @@ init:     if ((s->inp) != 0) {
                        else
                                if (++rear == front) overflo();
                        state = s->fail;
                        else
                                if (++rear == front) overflo();
                        state = s->fail;
-               floop:  if (state == 0) state = w;
+               floop:  if (state == 0) {
+                               state = w;
+                               bstart = 1;
+                       }
                        if (state->inp == c) {
                        if (state->inp == c) {
-                               q->fail = state->nst;
+                       qloop:  q->fail = state->nst;
                                if ((state->nst)->out == 1) q->out = 1;
                                if ((state->nst)->out == 1) q->out = 1;
-                               continue;
+                               if ((q = q->link) != 0) goto qloop;
                        }
                        else if ((state = state->link) != 0)
                                goto floop;
                        }
                        else if ((state = state->link) != 0)
                                goto floop;
+                       else if(bstart == 0){
+                               state = 0;
+                               goto floop;
+                       }
                }
                if ((s = s->link) != 0)
                        goto cloop;
                }
                if ((s = s->link) != 0)
                        goto cloop;