home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Interactive Guide / c-cplusplus-interactive-guide.iso / c_ref / csource3 / 169_01 / grep.c86 < prev    next >
Text File  |  1984-07-29  |  15KB  |  642 lines

  1. /*
  2.  *
  3.  *
  4.  * The    information  in  this  document  is  subject  to  change
  5.  * without  notice  and  should not be construed as a commitment
  6.  * by Digital Equipment Corporation or by DECUS.
  7.  *
  8.  * Neither Digital Equipment Corporation, DECUS, nor the authors
  9.  * assume any responsibility for the use or reliability of  this
  10.  * document or the described software.
  11.  *
  12.  *    Copyright (C) 1980, DECUS
  13.  *
  14.  *
  15.  * General permission to copy or modify, but not for profit,  is
  16.  * hereby  granted,  provided that the above copyright notice is
  17.  * included and reference made to  the    fact  that  reproduction
  18.  * privileges were granted by DECUS.
  19.  *
  20.  */
  21.  
  22. #include "stdio.h"
  23.  
  24. /*
  25.  * grep.
  26.  *
  27.  * Runs on the Decus compiler or on vms.
  28.  * Converted for BDS compiler (under CP/M-80), 20-Jan-83, by Chris Kern.
  29.  *
  30.  * Converted to IBM PC with CI-C86 C Compiler June 1983 by David N. Smith
  31.  *
  32.  * On vms, define as:
  33.  *
  34.  *    grep :== "$disk:[account]grep"     (native)
  35.  *    grep :== "$disk:[account]grep grep"     (Decus)
  36.  *
  37.  * See below for more information.
  38.  *
  39.  */
  40.  
  41.  
  42. char    *documentation[] = {
  43. "grep searches a file for a given pattern.  Execute by",
  44. "   grep [flags] regular_expression file_list",
  45. "",
  46. "Flags are single characters preceeded by '-':",
  47. "   -c      Only a count of matching lines is printed",
  48. "   -f      Print file name for matching lines switch, see below",
  49. "   -n      Each line is preceeded by its line number",
  50. "   -v      Only print non-matching lines",
  51. "",
  52. "The file_list is a list of files (wildcards are acceptable on RSX modes).",
  53.  
  54. "",
  55. "The file name is normally printed if there is a file given.",
  56. "The -f flag reverses this action (print name no file, not if more).",
  57. "",
  58. 0 };
  59.  
  60.  
  61. char    *patdoc[] = {
  62. "The regular_expression defines the pattern to search for.  Upper- and",
  63. "lower-case are always ignored.  Blank lines never match.  The expression",
  64. "should be quoted to prevent file-name translation.",
  65. "x      An ordinary character (not mentioned below) matches that character.",
  66. "'\\'    The backslash quotes any character.  \"\\$\" matches a dollar-sign.",
  67. "'^'    A circumflex at the beginning of an expression matches the",
  68. "       beginning of a line.",
  69. "'$'    A dollar-sign at the end of an expression matches the end of a line.",
  70. "'.'    A period matches any character except \"new-line\".",
  71. "':a'   A colon matches a class of characters described by the following",
  72. "':d'     character.  \":a\" matches any alphabetic, \":d\" matches digits,",
  73. "':n'     \":n\" matches alphanumerics, \": \" matches spaces, tabs, and",
  74. "': '     other control characters, such as new-line.",
  75. "'*'    An expression followed by an asterisk matches zero or more",
  76. "       occurrances of that expression: \"fo*\" matches \"f\", \"fo\"",
  77. "       \"foo\", etc.",
  78. "'+'    An expression followed by a plus sign matches one or more",
  79. "       occurrances of that expression: \"fo+\" matches \"fo\", etc.",
  80. "'-'    An expression followed by a minus sign optionally matches",
  81. "       the expression.",
  82. "'[]'   A string enclosed in square brackets matches any character in",
  83. "       that string, but no others.  If the first character in the",
  84. "       string is a circumflex, the expression matches any character",
  85. "       except \"new-line\" and the characters in the string.  For",
  86. "       example, \"[xyz]\" matches \"xx\" and \"zyx\", while \"[^xyz]\"",
  87. "       matches \"abc\" but not \"axb\".  A range of characters may be",
  88. "       specified by two characters separated by \"-\".  Note that,",
  89. "       [a-z] matches alphabetics, while [z-a] never matches.",
  90. "The concatenation of regular expressions is a regular expression.",
  91. 0};
  92.  
  93. #define LMAX    512
  94. #define PMAX    256
  95.  
  96. #define CHAR    1
  97. #define BOL    2
  98. #define EOL    3
  99. #define ANY    4
  100. #define CLASS    5
  101. #define NCLASS    6
  102. #define STAR    7
  103. #define PLUS    8
  104. #define MINUS    9
  105. #define ALPHA    10
  106. #define DIGIT    11
  107. #define NALPHA    12
  108. #define PUNCT    13
  109. #define RANGE    14
  110. #define ENDPAT    15
  111.  
  112. int    cflag;
  113. int    fflag;
  114. int    nflag;
  115. int    vflag;
  116. int    nfile;
  117.  
  118. int    debug    =    0;       /* Set for debug code      */
  119.  
  120. char    *pp;
  121.  
  122. #ifndef vms
  123. char    file_name[81];
  124. #endif
  125.  
  126. char    lbuf[LMAX];
  127. char    pbuf[PMAX];
  128.  
  129. /*******************************************************/
  130.  
  131. main(argc, argv)
  132. char *argv[];
  133. {
  134.    register char   *p;
  135.    register int    c, i;
  136.    int           gotpattern;
  137.    int           gotcha;
  138.  
  139.    FILE        *f;
  140.  
  141.    if (argc <= 1)
  142.       usage("No arguments");
  143.    if (argc == 2 && argv[1][0] == '?' && argv[1][1] == 0) {
  144.       help(documentation);
  145.       help(patdoc);
  146.       return;
  147.       }
  148.    nfile = argc-1;
  149.    gotpattern = 0;
  150.    for (i=1; i < argc; ++i) {
  151.       p = argv[i];
  152.       if (*p == '-') {
  153.      ++p;
  154.      while (c = *p++) {
  155.         switch(tolower(c)) {
  156.  
  157.         case '?':
  158.            help(documentation);
  159.            break;
  160.  
  161.         case 'C':
  162.         case 'c':
  163.            ++cflag;
  164.            break;
  165.  
  166.         case 'D':
  167.         case 'd':
  168.            ++debug;
  169.            break;
  170.  
  171.         case 'F':
  172.         case 'f':
  173.            ++fflag;
  174.            break;
  175.  
  176.         case 'n':
  177.         case 'N':
  178.            ++nflag;
  179.            break;
  180.  
  181.         case 'v':
  182.         case 'V':
  183.            ++vflag;
  184.            break;
  185.  
  186.         default:
  187.            usage("Unknown flag");
  188.         }
  189.      }
  190.      argv[i] = 0;
  191.      --nfile;
  192.       } else if (!gotpattern) {
  193.      compile(p);
  194.      argv[i] = 0;
  195.      ++gotpattern;
  196.      --nfile;
  197.       }
  198.    }
  199.    if (!gotpattern)
  200.       usage("No pattern");
  201.    if (nfile == 0)
  202.       grep(stdin, 0);
  203.    else {
  204.       fflag = fflag ^ (nfile > 0);
  205.       for (i=1; i < argc; ++i) {
  206.      if (p = argv[i]) {
  207.         if ((f=fopen(p, "r")) == NULL)
  208.            cant(p);
  209.         else {
  210.            grep(f, p);
  211.            fclose(f);
  212.         }
  213.      }
  214.       }
  215.    }
  216. }
  217.  
  218. /*******************************************************/
  219.  
  220. file(s)
  221. char *s;
  222. {
  223.    printf("File %s:\n", s);
  224. }
  225.  
  226. /*******************************************************/
  227.  
  228. cant(s)
  229. char *s;
  230. {
  231.    fprintf(stderr, "%s: cannot open\n", s);
  232. }
  233.  
  234.  
  235. /*******************************************************/
  236.  
  237. help(hp)
  238. char **hp;  /* dns added extra '*'  */
  239. /*
  240.  * Give good help
  241.  */
  242. {
  243.    register char   **dp;
  244.  
  245.    for (dp = hp; *dp; dp++)
  246.       printf("%s\n", *dp);
  247. }
  248.  
  249.  
  250. /*******************************************************/
  251.  
  252. usage(s)
  253. char    *s;
  254. {
  255.    fprintf(stderr, "?GREP-E-%s\n", s);
  256.    fprintf(stderr,
  257.       "Usage: grep [-cfnv] pattern [file ...].  grep ? for help\n");
  258.    exit(1);
  259. }
  260.  
  261.  
  262.  
  263. /*******************************************************/
  264.  
  265.  
  266. compile(source)
  267. char       *source;   /* Pattern to compile        */
  268. /*
  269.  * Compile the pattern into global pbuf[]
  270.  */
  271. {
  272.    register char  *s;          /* Source string pointer       */
  273.    register char  *lp;          /* Last pattern pointer       */
  274.    register int   c;          /* Current character       */
  275.    int          o;          /* Temp               */
  276.    char       *spp;       /* Save beginning of pattern */
  277.    char       *cclass();  /* Compile class routine       */
  278.  
  279.    s = source;
  280.    if (debug)
  281.       printf("Pattern = \"%s\"\n", s);
  282.    pp = pbuf;
  283.    while (c = *s++) {
  284.       /*
  285.        * STAR, PLUS and MINUS are special.
  286.        */
  287.       if (c == '*' || c == '+' || c == '-') {
  288.      if (pp == pbuf ||
  289.           (o=pp[-1]) == BOL ||
  290.           o == EOL ||
  291.           o == STAR ||
  292.           o == PLUS ||
  293.           o == MINUS)
  294.         badpat("Illegal occurrance op.", source, s);
  295.      store(ENDPAT);
  296.      store(ENDPAT);
  297.      spp = pp;         /* Save pattern end     */
  298.      while (--pp > lp)     /* Move pattern down     */
  299.         *pp = pp[-1];     /* one byte         */
  300.      *pp =     (c == '*') ? STAR :
  301.         (c == '-') ? MINUS : PLUS;
  302.      pp = spp;         /* Restore pattern end  */
  303.      continue;
  304.       }
  305.       /*
  306.        * All the rest.
  307.        */
  308.       lp = pp;           /* Remember start       */
  309.       switch(c) {
  310.  
  311.       case '^':
  312.      store(BOL);
  313.      break;
  314.  
  315.       case '$':
  316.      store(EOL);
  317.      break;
  318.  
  319.       case '.':
  320.      store(ANY);
  321.      break;
  322.  
  323.       case '[':
  324.      s = cclass(source, s);
  325.      break;
  326.  
  327.       case ':':
  328.      if (*s) {
  329.         c = *s++;
  330.         switch(tolower(c)) {
  331.  
  332.         case 'a':
  333.         case 'A':
  334.            store(ALPHA);
  335.            break;
  336.  
  337.         case 'd':
  338.         case 'D':
  339.            store(DIGIT);
  340.            break;
  341.  
  342.         case 'n':
  343.         case 'N':
  344.