2 * The information in this document is subject to change
3 * without notice and should not be construed as a commitment
4 * by Digital Equipment Corporation or by DECUS.
6 * Neither Digital Equipment Corporation, DECUS, nor the authors
7 * assume any responsibility for the use or reliability of this
8 * document or the described software.
10 * Copyright (C) 1980, DECUS
12 * General permission to copy or modify, but not for profit, is
13 * hereby granted, provided that the above copyright notice is
14 * included and reference made to the fact that reproduction
15 * privileges were granted by DECUS.
19 #include <ctype.h> // tolower()
24 * Runs on the Decus compiler or on vms, On vms, define as:
25 * grep :== "$disk:[account]grep" (native)
26 * grep :== "$disk:[account]grep grep" (Decus)
27 * See below for more information.
30 char *documentation[] = {
31 "grep searches a file for a given pattern. Execute by",
32 " grep [flags] regular_expression file_list\n",
33 "Flags are single characters preceded by '-':",
34 " -c Only a count of matching lines is printed",
35 " -f Print file name for matching lines switch, see below",
36 " -n Each line is preceded by its line number",
37 " -v Only print non-matching lines\n",
38 "The file_list is a list of files (wildcards are acceptable on RSX modes).",
39 "\nThe file name is normally printed if there is a file given.",
40 "The -f flag reverses this action (print name no file, not if more).\n",
44 "The regular_expression defines the pattern to search for. Upper- and",
45 "lower-case are always ignored. Blank lines never match. The expression",
46 "should be quoted to prevent file-name translation.",
47 "x An ordinary character (not mentioned below) matches that character.",
48 "'\\' The backslash quotes any character. \"\\$\" matches a dollar-sign.",
49 "'^' A circumflex at the beginning of an expression matches the",
50 " beginning of a line.",
51 "'$' A dollar-sign at the end of an expression matches the end of a line.",
52 "'.' A period matches any character except \"new-line\".",
53 "':a' A colon matches a class of characters described by the following",
54 "':d' character. \":a\" matches any alphabetic, \":d\" matches digits,",
55 "':n' \":n\" matches alphanumerics, \": \" matches spaces, tabs, and",
56 "': ' other control characters, such as new-line.",
57 "'*' An expression followed by an asterisk matches zero or more",
58 " occurrences of that expression: \"fo*\" matches \"f\", \"fo\"",
60 "'+' An expression followed by a plus sign matches one or more",
61 " occurrences of that expression: \"fo+\" matches \"fo\", etc.",
62 "'-' An expression followed by a minus sign optionally matches",
64 "'[]' A string enclosed in square brackets matches any character in",
65 " that string, but no others. If the first character in the",
66 " string is a circumflex, the expression matches any character",
67 " except \"new-line\" and the characters in the string. For",
68 " example, \"[xyz]\" matches \"xx\" and \"zyx\", while \"[^xyz]\"",
69 " matches \"abc\" but not \"axb\". A range of characters may be",
70 " specified by two characters separated by \"-\". Note that,",
71 " [a-z] matches alphabetics, while [z-a] never matches.",
72 "The concatenation of regular expressions is a regular expression.",
94 int cflag=0, fflag=0, nflag=0, vflag=0, nfile=0, debug=0;
96 char *pp, lbuf[LMAX], pbuf[PMAX];
102 void badpat(char *, char *, char *);
106 /*** Display a file name *******************************/
109 printf("File %s:\n", s);
112 /*** Report unopenable file ****************************/
115 fprintf(stderr, "%s: cannot open\n", s);
118 /*** Give good help ************************************/
123 for (dp = hp; *dp; ++dp)
127 /*** Display usage summary *****************************/
130 fprintf(stderr, "?GREP-E-%s\n", s);
132 "Usage: grep [-cfnv] pattern [file ...]. grep ? for help\n");
136 /*** Compile the pattern into global pbuf[] ************/
137 void compile(char *source)
139 char *s; /* Source string pointer */
140 char *lp; /* Last pattern pointer */
141 int c; /* Current character */
143 char *spp; /* Save beginning of pattern */
147 printf("Pattern = \"%s\"\n", s);
151 * STAR, PLUS and MINUS are special.
153 if (c == '*' || c == '+' || c == '-') {
160 badpat("Illegal occurrence op.", source, s);
163 spp = pp; /* Save pattern end */
164 while (--pp > lp) /* Move pattern down */
165 *pp = pp[-1]; /* one byte */
166 *pp = (c == '*') ? STAR :
167 (c == '-') ? MINUS : PLUS;
168 pp = spp; /* Restore pattern end */
174 lp = pp; /* Remember start */
190 s = cclass(source, s);
195 switch(tolower(c = *s++)) {
217 badpat("Unknown : type", source, s);
222 else badpat("No : type", source, s);
234 store(0); /* Terminate string */
236 for (lp = pbuf; lp < pp;) {
237 if ((c = (*lp++ & 0377)) < ' ')
239 else printf("%c ", c);
245 /*** Compile a class (within []) ***********************/
246 char *cclass(char *source, char *src)
247 /* char *source; // Pattern start -- for error msg. */
248 /* char *src; // Class start */
250 char *s; /* Source pointer */
251 char *cp; /* Pattern start */
252 int c; /* Current character */
263 store(0); /* Byte count */
264 while ((c = *s++) && c!=']') {
265 if (c == '\\') { /* Store quoted char */
266 if ((c = *s++) == '\0') /* Gotta get something */
267 badpat("Class terminates badly", source, s);
268 else store(tolower(c));
271 (pp - cp) > 1 && *s != ']' && *s != '\0') {
272 c = pp[-1]; /* Range start */
273 pp[-1] = RANGE; /* Range signal */
274 store(c); /* Re-store start */
275 c = *s++; /* Get end char and*/
276 store(tolower(c)); /* Store it */
279 store(tolower(c)); /* Store normal char */
283 badpat("Unterminated class", source, s);
284 if ((c = (pp - cp)) >= 256)
285 badpat("Class too large", source, s);
287 badpat("Empty class", source, s);
292 /*** Store an entry in the pattern buffer **************/
295 if (pp >= &pbuf[PMAX])
296 error("Pattern too complex\n");
300 /*** Report a bad pattern specification ****************/
301 void badpat(char *message, char *source, char *stop)
302 /* char *message; // Error message */
303 /* char *source; // Pattern start */
304 /* char *stop; // Pattern end */
306 fprintf(stderr, "-GREP-E-%s, pattern is\"%s\"\n", message, source);
307 fprintf(stderr, "-GREP-E-Stopped at byte %ld, '%c'\n",
308 stop-source, stop[-1]);
309 error("?GREP-E-Bad pattern\n");
312 /*** Scan the file for the pattern in pbuf[] ***********/
313 void grep(FILE *fp, char *fn)
314 /* FILE *fp; // File to process */
315 /* char *fn; // File name (for -f option) */
321 while (fgets(lbuf, LMAX, fp)) {
324 if ((m && !vflag) || (!m && vflag)) {
333 printf("%s\n", lbuf);
340 printf("%d\n", count);
344 /*** Match line (lbuf) with pattern (pbuf) return 1 if match ***/
347 char *l; /* Line pointer */
349 for (l = lbuf; *l; ++l) {
356 /*** Match partial line with pattern *******************/
357 char *pmatch(char *line, char *pattern)
358 /* char *line; // (partial) line to match */
359 /* char *pattern; // (partial) pattern to match */
361 char *l; /* Current line pointer */
362 char *p; /* Current pattern pointer */
363 char c; /* Current character */
364 char *e; /* End for STAR and PLUS match */
365 int op; /* Pattern operation */
366 int n; /* Class counter */
367 char *are; /* Start of STAR match */
371 printf("pmatch(\"%s\")\n", line);
373 while ((op = *p++) != ENDPAT) {
375 printf("byte[%ld] = 0%o, '%c', op = 0%o\n",
380 if (tolower(*l++) != *p++)
400 if ((c = *l++) < '0' || (c > '9'))
406 if (c < 'a' || c > 'z')
412 if (c >= 'a' && c <= 'z')
414 else if (c < '0' || c > '9')
420 if (c == 0 || c > ' ')
432 if (c >= p[-2] && c <= p[-1])
438 if ((op == CLASS) == (n <= 1))
445 e = pmatch(l, p); /* Look for a match */
446 while (*p++ != ENDPAT); /* Skip over pattern */
447 if (e) /* Got a match? */
448 l = e; /* Yes, update string */
449 break; /* Always succeeds */
451 case PLUS: /* One or more ... */
452 if ((l = pmatch(l, p)) == 0)
453 return(0); /* Gotta have a match */
454 case STAR: /* Zero or more ... */
455 are = l; /* Remember line start */
456 while (*l && (e = pmatch(l, p)))
457 l = e; /* Get longest match */
458 while (*p++ != ENDPAT); /* Skip over pattern */
459 while (l >= are) { /* Try to match rest */
460 if (e = pmatch(l, p))
462 --l; /* Nope, try earlier */
464 return(0); /* Nothing else worked */
467 printf("Bad op code %d\n", op);
468 error("Cannot happen -- match\n");
474 /*** Report an error ***********************************/
477 fprintf(stderr, "%s", s);
481 /*** Main program - parse arguments & grep *************/
482 int main(int argc, char **argv)
491 usage("No arguments");
492 if (argc == 2 && argv[1][0] == '?' && argv[1][1] == 0) {
499 for (i=1; i < argc; ++i) {
536 usage("Unknown flag");
541 } else if (!gotpattern) {
553 fflag = fflag ^ (nfile > 0);
554 for (i=1; i < argc; ++i) {
556 if ((f=fopen(p, "r")) == NULL)
568 /* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/