home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_100 / 152_01 / grep.c < prev    next >
Text File  |  1985-03-10  |  18KB  |  669 lines

  1. /*
  2. HEADER:                 CUG152.08;
  3. TITLE:                  Source code for Unix-like GREP command
  4.                            to find strings in files;
  5. DATE:                   09/04/85;
  6. DESCRIPTION:
  7.   "A program to find strings in a file. The program
  8.       uses the GREP command like UNIX."
  9. KEYWORDS:               find, string, file;
  10. FILENAME:               GREP.C;
  11. WARNINGS:
  12.   "Upper and lower case are always ignored.
  13.    Blank lines always fail to match.
  14.    The authors claim copyrights and authorize non-commercial
  15.       use only.
  16.    The typist of this heading does not have equipment to run
  17.       this program and prepared this header from comments
  18.       within the source code or companion files."
  19. AUTHORS:                David N. Smith
  20.                         Digital Equipment Corp.(DECUS);
  21. COMPILERS:              CI/C86;
  22. REFERENCES:
  23.     AUTHERS:            " ";
  24.     TITLE:              " ";
  25.     CITATION:           " ";
  26. ENDREF
  27. */
  28. /*
  29.  *
  30.  *
  31.  * The  information  in  this  document  is  subject  to  change
  32.  * without  notice  and  should not be construed as a commitment
  33.  * by Digital Equipment Corporation or by DECUS.
  34.  *
  35.  * Neither Digital Equipment Corporation, DECUS, nor the authors
  36.  * assume any responsibility for the use or reliability of  this
  37.  * document or the described software.
  38.  *
  39.  *      Copyright (C) 1980, DECUS
  40.  *
  41.  *
  42.  * General permission to copy or modify, but not for profit,  is
  43.  * hereby  granted,  provided that the above copyright notice is
  44.  * included and reference made to  the  fact  that  reproduction
  45.  * privileges were granted by DECUS.
  46.  *
  47.  */
  48.  
  49. #include "stdio.h"
  50.  
  51. /*
  52.  * grep.
  53.  *
  54.  * Runs on the Decus compiler or on vms.
  55.  * Converted for BDS compiler (under CP/M-80), 20-Jan-83, by Chris Kern.
  56.  *
  57.  * Converted to IBM PC with CI-C86 C Compiler June 1983 by David N. Smith
  58.  *
  59.  * On vms, define as:
  60.  *
  61.  *      grep :== "$disk:[account]grep"     (native)
  62.  *      grep :== "$disk:[account]grep grep"     (Decus)
  63.  *
  64.  * See below for more information.
  65.  *
  66.  */
  67.  
  68.  
  69. char    *documentation[] = {
  70. "grep searches a file for a given pattern.  Execute by",
  71. "   grep [flags] regular_expression file_list",
  72. "",
  73. "Flags are single characters preceeded by '-':",
  74. "   -c      Only a count of matching lines is printed",
  75. "   -f      Print file name for matching lines switch, see below",
  76. "   -n      Each line is preceeded by its line number",
  77. "   -v      Only print non-matching lines",
  78. "",
  79. "The file_list is a list of files (wildcards are acceptable on RSX modes).",
  80.  
  81. "",
  82. "The file name is normally printed if there is a file given.",
  83. "The -f flag reverses this action (print name no file, not if more).",
  84. "",
  85. 0 };
  86.  
  87.  
  88. char    *patdoc[] = {
  89. "The regular_expression defines the pattern to search for.  Upper- and",
  90. "lower-case are always ignored.  Blank lines never match.  The expression",
  91. "should be quoted to prevent file-name translation.",
  92. "x      An ordinary character (not mentioned below) matches that character.",
  93. "'\\'    The backslash quotes any character.  \"\\$\" matches a dollar-sign.",
  94. "'^'    A circumflex at the beginning of an expression matches the",
  95. "       beginning of a line.",
  96. "'$'    A dollar-sign at the end of an expression matches the end of a line.",
  97. "'.'    A period matches any character except \"new-line\".",
  98. "':a'   A colon matches a class of characters described by the following",
  99. "':d'     character.  \":a\" matches any alphabetic, \":d\" matches digits,",
  100. "':n'     \":n\" matches alphanumerics, \": \" matches spaces, tabs, and",
  101. "': '     other control characters, such as new-line.",
  102. "'*'    An expression followed by an asterisk matches zero or more",
  103. "       occurrances of that expression: \"fo*\" matches \"f\", \"fo\"",
  104. "       \"foo\", etc.",
  105. "'+'    An expression followed by a plus sign matches one or more",
  106. "       occurrances of that expression: \"fo+\" matches \"fo\", etc.",
  107. "'-'    An expression followed by a minus sign optionally matches",
  108. "       the expression.",
  109. "'[]'   A string enclosed in square brackets matches any character in",
  110. "       that string, but no others.  If the first character in the",
  111. "       string is a circumflex, the expression matches any character",
  112. "       except \"new-line\" and the characters in the string.  For",
  113. "       example, \"[xyz]\" matches \"xx\" and \"zyx\", while \"[^xyz]\"",
  114. "       matches \"abc\" but not \"axb\".  A range of characters may be",
  115. "       specified by two characters separated by \"-\".  Note that,",
  116. "       [a-z] matches alphabetics, while [z-a] never matches.",
  117. "The concatenation of regular expressions is a regular expression.",
  118. 0};
  119.  
  120. #define LMAX    512
  121. #define PMAX    256
  122.  
  123. #define CHAR    1
  124. #define BOL     2
  125. #define EOL     3
  126. #define ANY     4
  127. #define CLASS   5
  128. #define NCLASS  6
  129. #define STAR    7
  130. #define PLUS    8
  131. #define MINUS   9
  132. #define ALPHA   10
  133. #define DIGIT   11
  134. #define NALPHA  12
  135. #define PUNCT   13
  136. #define RANGE   14
  137. #define ENDPAT  15
  138.  
  139. int     cflag;
  140. int     fflag;
  141. int     nflag;
  142. int     vflag;
  143. int     nfile;
  144.  
  145. int     debug   =       0;         /* Set for debug code      */
  146.  
  147. char    *pp;
  148.  
  149. #ifndef vms
  150. char    file_name[81];
  151. #endif
  152.  
  153. char    lbuf[LMAX];
  154. char    pbuf[PMAX];
  155.  
  156. /*******************************************************/
  157.  
  158. main(argc, argv)
  159. char *argv[];
  160. {
  161.    register char   *p;
  162.    register int    c, i;
  163.    int             gotpattern;
  164.    int             gotcha;
  165.  
  166.    FILE            *f;
  167.  
  168.    if (argc <= 1)
  169.       usage("No arguments");
  170.    if (argc == 2 && argv[1][0] == '?' && argv[1][1] == 0) {
  171.       help(documentation);
  172.       help(patdoc);
  173.       return;
  174.       }
  175.    nfile = argc-1;
  176.    gotpattern = 0;
  177.    for (i=1; i < argc; ++i) {
  178.       p = argv[i];
  179.       if (*p == '-') {
  180.          ++p;
  181.          while (c = *p++) {
  182.             switch(tolower(c)) {
  183.  
  184.             case '?':
  185.                help(documentation);
  186.                break;
  187.  
  188.             case 'C':
  189.             case 'c':
  190.                ++cflag;
  191.                break;
  192.  
  193.             case 'D':
  194.             case 'd':
  195.                ++debug;
  196.                break;
  197.  
  198.             case 'F':
  199.             case 'f':
  200.                ++fflag;
  201.                break;
  202.  
  203.             case 'n':
  204.             case 'N':
  205.                ++nflag;
  206.                break;
  207.  
  208.             case 'v':
  209.             case 'V':
  210.                ++vflag;
  211.                break;
  212.  
  213.             default:
  214.                usage("Unknown flag");
  215.             }
  216.          }
  217.          argv[i] = 0;
  218.          --nfile;
  219.       } else if (!gotpattern) {
  220.          compile(p);
  221.          argv[i] = 0;
  222.          ++gotpattern;
  223.          --nfile;
  224.       }
  225.    }
  226.    if (!gotpattern)
  227.       usage("No pattern");
  228.    if (nfile == 0)
  229.       grep(stdin, 0);
  230.    else {
  231.       fflag = fflag ^ (nfile > 0);
  232.       for (i=1; i < argc; ++i) {
  233.          if (p = argv[i]) {
  234.             if ((f=fopen(p, "r")) == NULL)
  235.                cant(p);
  236.             else {
  237.                grep(f, p);
  238.                fclose(f);
  239.             }
  240.          }
  241.       }
  242.    }
  243. }
  244.  
  245. /*******************************************************/
  246.  
  247. file(s)
  248. char *s;
  249. {
  250.    printf("File %s:\n", s);
  251. }
  252.  
  253. /*******************************************************/
  254.  
  255. cant(s)
  256. char *s;
  257. {
  258.    fprintf(stderr, "%s: cannot open\n", s);
  259. }
  260.  
  261.  
  262. /*******************************************************/
  263.  
  264. help(hp)
  265. char **hp;  /* dns added extra '*'  */
  266. /*
  267.  * Give good help
  268.  */
  269. {
  270.    register char   **dp;
  271.  
  272.    for (dp = hp; *dp; dp++)
  273.       printf("%s\n", *dp);
  274. }
  275.  
  276.  
  277. /*******************************************************/
  278.  
  279. usage(s)
  280. char    *s;
  281. {
  282.    fprintf(stderr, "?GREP-E-%s\n", s);
  283.    fprintf(stderr,
  284.       "Usage: grep [-cfnv] pattern [file ...].  grep ? for help\n");
  285.    exit(1);
  286. }
  287.  
  288.  
  289.  
  290. /*******************************************************/
  291.  
  292.  
  293. compile(source)
  294. char       *source;   /* Pattern to compile         */
  295. /*
  296.  * Compile the pattern into global pbuf[]
  297.  */
  298. {
  299.    register char  *s;         /* Source string pointer     */
  300.    register char  *lp;        /* Last pattern pointer      */
  301.    register int   c;          /* Current character         */
  302.    int            o;          /* Temp                      */
  303.    char           *spp;       /* Save beginning of pattern */
  304.    char           *cclass();  /* Compile class routine     */
  305.  
  306.    s = source;
  307.    if (debug)
  308.       printf("Pattern = \"%s\"\n", s);
  309.    pp = pbuf;
  310.    while (c = *s++) {
  311.       /*
  312.        * STAR, PLUS and MINUS are special.
  313.        */
  314.       if (c == '*' || c == '+' || c == '-') {
  315.          if (pp == pbuf ||
  316.               (o=pp[-1]) == BOL ||
  317.               o == EOL ||
  318.               o == STAR ||
  319.               o == PLUS ||
  320.               o == MINUS)
  321.             badpat("Illegal occurrance op.", source, s);
  322.          store(ENDPAT);
  323.          store(ENDPAT);
  324.          spp = pp;               /* Save pattern end     */
  325.          while (--pp > lp)       /* Move pattern down    */
  326.             *pp = pp[-1];        /* one byte             */
  327.          *pp =   (c == '*') ? STAR :
  328.             (c == '-') ? MINUS : PLUS;
  329.          pp = spp;               /* Restore pattern end  */
  330.          continue;
  331.       }
  332.       /*
  333.        * All the rest.
  334.        */
  335.       lp = pp;         /* Remember start       */
  336.       switch(c) {
  337.  
  338.       case '^':
  339.          store(BOL);
  340.          break;
  341.  
  342.       case '$':
  343.          store(EOL);
  344.          break;
  345.  
  346.       case '.':
  347.          store(ANY);
  348.          break;
  349.  
  350.       case '[':
  351.          s = cclass(source, s);
  352.          break;
  353.  
  354.       case ':':
  355.          if (*s) {
  356.             c = *s++;
  357.             switch(tolower(c)) {
  358.  
  359.             case 'a':
  360.             case 'A':
  361.                store(ALPHA);
  362.                break;
  363.  
  364.             case 'd':
  365.             case 'D':
  366.                store(DIGIT);
  367.                break;
  368.  
  369.             case 'n':
  370.             case 'N':
  371.                store(NALPHA);
  372.                break;
  373.  
  374.             case ' ':
  375.                store(PUNCT);
  376.                break;
  377.  
  378.             default:
  379.                badpat("Unknown : type", source, s);
  380.  
  381.             }
  382.             break;
  383.          }
  384.          else    badpat("No : type", source, s);
  385.  
  386.       case '\\':
  387.          if (*s)
  388.             c = *s++;
  389.  
  390.       default:
  391.          store(CHAR);
  392.          store(tolower(c));
  393.       }
  394.    }
  395.    store(ENDPAT);
  396.    store(0);                /* Terminate string     */
  397.    if (debug) {
  398.       for (lp = pbuf; lp < pp;) {
  399.          if ((c = (*lp++ & 0377)) < ' ')
  400.             printf("\\%o ", c);
  401.          else    printf("%c ", c);
  402.         }
  403.         printf("\n");
  404.    }
  405. }
  406.  
  407. /*******************************************************/
  408.  
  409. char *
  410. cclass(source, src)
  411. char       *source;   /* Pattern start -- for error msg.      */
  412. char       *src;      /* Class start           */
  413. /*
  414.  * Compile a class (within [])
  415.  */
  416. {
  417.    register char   *s;        /* Source pointer    */
  418.    register char   *cp;       /* Pattern start     */
  419.    register int    c;         /* Current character */
  420.    int             o;         /* Temp              */
  421.  
  422.    s = src;
  423.    o = CLASS;
  424.    if (*s == '^') {
  425.       ++s;
  426.       o = NCLASS;
  427.    }
  428.    store(o);
  429.    cp = pp;
  430.    store(0);                          /* Byte count      */
  431.    while ((c = *s++) && c!=']') {
  432.       if (c == '\\') {                /* Store quoted char    */
  433.          if ((c = *s++) == '\0')      /* Gotta get something  */
  434.             badpat("Class terminates badly", source, s);
  435.          else    store(tolower(c));
  436.       }
  437.       else if (c == '-' &&
  438.             (pp - cp) > 1 && *s != ']' && *s != '\0') {
  439.          c = pp[-1];             /* Range start     */
  440.          pp[-1] = RANGE;         /* Range signal    */
  441.          store(c);               /* Re-store start  */
  442.          c = *s++;               /* Get end char and*/
  443.          store(tolower(c));      /* Store it        */
  444.       }
  445.       else {
  446.          store(tolower(c));      /* Store normal char */
  447.       }
  448.    }
  449.    if (c != ']')
  450.       badpat("Unterminated class", source, s);
  451.    if ((c = (pp - cp)) >= 256)
  452.       badpat("Class too large", source, s);
  453.    if (c == 0)
  454.       badpat("Empty class", source, s);
  455.    *cp = c;
  456.    return(s);
  457. }
  458.  
  459. /*******************************************************/
  460.  
  461. store(op)
  462. {
  463.    if (pp >= &pbuf[PMAX])
  464.       error("Pattern too complex\n");
  465.    *pp++ = op;
  466. }
  467.  
  468.  
  469. /*******************************************************/
  470.  
  471. badpat(message, source, stop)
  472. char  *message;       /* Error message */
  473. char  *source;        /* Pattern start */
  474. char  *stop;          /* Pattern end   */
  475. {
  476.    register int    c;
  477.  
  478.    fprintf(stderr, "-GREP-E-%s, pattern is\"%s\"\n", message, source);
  479.    fprintf(stderr, "-GREP-E-Stopped at byte %d, '%c'\n",
  480.          stop-source, stop[-1]);
  481.    error("?GREP-E-Bad pattern\n");
  482. }
  483.  
  484.  
  485.  
  486. /*******************************************************/
  487.  
  488. grep(fp, fn)
  489. FILE       *fp;       /* File to process            */
  490. char       *fn;       /* File name (for -f option)  */
  491. /*
  492.  * Scan the file for the pattern in pbuf[]
  493.  */
  494. {
  495.    register int lno, count, m;
  496.  
  497.    lno = 0;
  498.    count = 0;
  499.    while (fgets(lbuf, LMAX, fp)) {
  500.       ++lno;
  501.       m = match();
  502.       if ((m && !vflag) || (!m && vflag)) {
  503.          ++count;
  504.          if (!cflag) {
  505.             if (fflag && fn) {
  506.                file(fn);
  507.                fn = 0;
  508.             }
  509.             if (nflag)
  510.                printf("%d\t", lno);
  511.             printf("%s\n", lbuf);
  512.          }
  513.       }
  514.    }
  515.    if (cflag) {
  516.       if (fflag && fn)
  517.          file(fn);
  518.       printf("%d\n", count);
  519.    }
  520. }
  521.  
  522.  
  523. /*******************************************************/
  524.  
  525. match()
  526. /*
  527.  * Match the current line (in lbuf[]), return 1 if it does.
  528.  */
  529. {
  530.    register char   *l;        /* Line pointer       */
  531.    char *pmatch();
  532.  
  533.    for (l = lbuf; *l; l++) {
  534.       if (pmatch(l, pbuf))
  535.          return(1);
  536.    }
  537.    return(0);
  538. }
  539.  
  540. /*******************************************************/
  541.  
  542. char *
  543. pmatch(line, pattern)
  544. char               *line;     /* (partial) line to match      */
  545. char               *pattern;  /* (partial) pattern to match   */
  546. {
  547.    register char   *l;        /* Current line pointer         */
  548.    register char   *p;        /* Current pattern pointer      */
  549.    register char   c;         /* Current character            */
  550.    char            *e;        /* End for STAR and PLUS match  */
  551.    int             op;        /* Pattern operation            */
  552.    int             n;         /* Class counter                */
  553.    char            *are;      /* Start of STAR match          */
  554.  
  555.    l = line;
  556.    if (debug > 1)
  557.       printf("pmatch(\"%s\")\n", line);
  558.    p = pattern;
  559.    while ((op = *p++) != ENDPAT) {
  560.       if (debug > 1)
  561.          printf("byte[%d] = 0%o, '%c', op = 0%o\n",
  562.                l-line, *l, *l, op);
  563.       switch(op) {
  564.  
  565.       case CHAR:
  566.          if (tolower(*l++) != *p++)
  567.             return(0);
  568.          break;
  569.  
  570.       case BOL:
  571.          if (l != lbuf)
  572.             return(0);
  573.          break;
  574.  
  575.       case EOL:
  576.          if (*l != '\0')
  577.             return(0);
  578.          break;
  579.  
  580.       case ANY:
  581.          if (*l++ == '\0')
  582.             return(0);
  583.          break;
  584.  
  585.       case DIGIT:
  586.          if ((c = *l++) < '0' || (c > '9'))
  587.             return(0);
  588.          break;
  589.  
  590.       case ALPHA:
  591.          c = tolower(*l++);
  592.          if (c < 'a' || c > 'z')
  593.             return(0);
  594.          break;
  595.  
  596.       case NALPHA:
  597.          c = tolower(*l++);
  598.          if (c >= 'a' && c <= 'z')
  599.             break;
  600.          else if (c < '0' || c > '9')
  601.             return(0);
  602.          break;
  603.  
  604.       case PUNCT:
  605.          c = *l++;
  606.          if (c == 0 || c > ' ')
  607.             return(0);
  608.          break;
  609.  
  610.       case CLASS:
  611.       case NCLASS:
  612.          c = tolower(*l++);
  613.          n = *p++ & 0377;
  614.          do {
  615.             if (*p == RANGE) {
  616.                p += 3;
  617.                n -= 2;
  618.                if (c >= p[-2] && c <= p[-1])
  619.                   break;
  620.             }
  621.             else if (c == *p++)
  622.                break;
  623.          } while (--n > 1);
  624.          if ((op == CLASS) == (n <= 1))
  625.             return(0);
  626.          if (op == CLASS)
  627.             p += n - 2;
  628.          break;
  629.  
  630.       case MINUS:
  631.          e = pmatch(l, p);       /* Look for a match    */
  632.          while (*p++ != ENDPAT); /* Skip over pattern   */
  633.          if (e)                  /* Got a match?        */
  634.             l = e;               /* Yes, update string  */
  635.          break;                  /* Always succeeds     */
  636.  
  637.       case PLUS:                 /* One or more ...     */
  638.          if ((l = pmatch(l, p)) == 0)
  639.             return(0);           /* Gotta have a match  */
  640.       case STAR:                 /* Zero or more ...    */
  641.          are = l;                /* Remember line start */
  642.          while (*l && (e = pmatch(l, p)))
  643.             l = e;               /* Get longest match   */
  644.          while (*p++ != ENDPAT); /* Skip over pattern   */
  645.          while (l >= are) {      /* Try to match rest   */
  646.             if (e = pmatch(l, p))
  647.                return(e);
  648.             --l;                 /* Nope, try earlier   */
  649.          }
  650.          return(0);              /* Nothing else worked */
  651.  
  652.       default:
  653.          printf("Bad op code %d\n", op);
  654.          error("Cannot happen -- match\n");
  655.       }
  656.    }
  657.    return(l);
  658. }
  659.  
  660. /*******************************************************/
  661.  
  662. error(s)
  663. char *s;
  664. {
  665.    fprintf(stderr, "%s", s);
  666.    exit(1);
  667. }
  668.  
  669.