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.
22 * Runs on the Decus compiler or on vms, On vms, define as:
23 * grep :== "$disk:[account]grep" (native)
24 * grep :== "$disk:[account]grep grep" (Decus)
25 * See below for more information.
29 char *documentation
[] = {
30 "grep searches a file for a given pattern. Execute by",
31 " grep [flags] regular_expression file_list\n",
32 "Flags are single characters preceeded by '-':",
33 " -c Only a count of matching lines is printed",
34 " -f Print file name for matching lines switch, see below",
35 " -n Each line is preceeded by its line number",
36 " -v Only print non-matching lines\n",
37 "The file_list is a list of files (wildcards are acceptable on RSX modes).",
38 "\nThe file name is normally printed if there is a file given.",
39 "The -f flag reverses this action (print name no file, not if more).\n",
43 "The regular_expression defines the pattern to search for. Upper- and",
44 "lower-case are always ignored. Blank lines never match. The expression",
45 "should be quoted to prevent file-name translation.",
46 "x An ordinary character (not mentioned below) matches that character.",
47 "'\\' The backslash quotes any character. \"\\$\" matches a dollar-sign.",
48 "'^' A circumflex at the beginning of an expression matches the",
49 " beginning of a line.",
50 "'$' A dollar-sign at the end of an expression matches the end of a line.",
51 "'.' A period matches any character except \"new-line\".",
52 "':a' A colon matches a class of characters described by the following",
53 "':d' character. \":a\" matches any alphabetic, \":d\" matches digits,",
54 "':n' \":n\" matches alphanumerics, \": \" matches spaces, tabs, and",
55 "': ' other control characters, such as new-line.",
56 "'*' An expression followed by an asterisk matches zero or more",
57 " occurrances of that expression: \"fo*\" matches \"f\", \"fo\"",
59 "'+' An expression followed by a plus sign matches one or more",
60 " occurrances of that expression: \"fo+\" matches \"fo\", etc.",
61 "'-' An expression followed by a minus sign optionally matches",
63 "'[]' A string enclosed in square brackets matches any character in",
64 " that string, but no others. If the first character in the",
65 " string is a circumflex, the expression matches any character",
66 " except \"new-line\" and the characters in the string. For",
67 " example, \"[xyz]\" matches \"xx\" and \"zyx\", while \"[^xyz]\"",
68 " matches \"abc\" but not \"axb\". A range of characters may be",
69 " specified by two characters separated by \"-\". Note that,",
70 " [a-z] matches alphabetics, while [z-a] never matches.",
71 "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 /*** Display a file name *******************************/
105 printf("File %s:\n", s
);
108 /*** Report unopenable file ****************************/
111 fprintf(stderr
, "%s: cannot open\n", s
);
114 /*** Give good help ************************************/
119 for (dp
= hp
; *dp
; ++dp
)
123 /*** Display usage summary *****************************/
126 fprintf(stderr
, "?GREP-E-%s\n", s
);
128 "Usage: grep [-cfnv] pattern [file ...]. grep ? for help\n");
132 /*** Compile the pattern into global pbuf[] ************/
133 void compile(char *source
)
135 char *s
; /* Source string pointer */
136 char *lp
; /* Last pattern pointer */
137 int c
; /* Current character */
139 char *spp
; /* Save beginning of pattern */
143 printf("Pattern = \"%s\"\n", s
);
147 * STAR, PLUS and MINUS are special.
149 if (c
== '*' || c
== '+' || c
== '-') {
156 badpat("Illegal occurrance op.", source
, s
);
159 spp
= pp
; /* Save pattern end */
160 while (--pp
> lp
) /* Move pattern down */
161 *pp
= pp
[-1]; /* one byte */
162 *pp
= (c
== '*') ? STAR
:
163 (c
== '-') ? MINUS
: PLUS
;
164 pp
= spp
; /* Restore pattern end */
170 lp
= pp
; /* Remember start */
186 s
= cclass(source
, s
);
191 switch(tolower(c
= *s
++)) {
213 badpat("Unknown : type", source
, s
);
218 else badpat("No : type", source
, s
);
230 store(0); /* Terminate string */
232 for (lp
= pbuf
; lp
< pp
;) {
233 if ((c
= (*lp
++ & 0377)) < ' ')
235 else printf("%c ", c
);
241 /*** Compile a class (within []) ***********************/
242 char *cclass(char *source
, char *src
)
243 /* char *source; // Pattern start -- for error msg. */
244 /* char *src; // Class start */
246 char *s
; /* Source pointer */
247 char *cp
; /* Pattern start */
248 int c
; /* Current character */
259 store(0); /* Byte count */
260 while ((c
= *s
++) && c
!=']') {
261 if (c
== '\\') { /* Store quoted char */
262 if ((c
= *s
++) == '\0') /* Gotta get something */
263 badpat("Class terminates badly", source
, s
);
264 else store(tolower(c
));
267 (pp
- cp
) > 1 && *s
!= ']' && *s
!= '\0') {
268 c
= pp
[-1]; /* Range start */
269 pp
[-1] = RANGE
; /* Range signal */
270 store(c
); /* Re-store start */
271 c
= *s
++; /* Get end char and*/
272 store(tolower(c
)); /* Store it */
275 store(tolower(c
)); /* Store normal char */
279 badpat("Unterminated class", source
, s
);
280 if ((c
= (pp
- cp
)) >= 256)
281 badpat("Class too large", source
, s
);
283 badpat("Empty class", source
, s
);
288 /*** Store an entry in the pattern buffer **************/
291 if (pp
>= &pbuf
[PMAX
])
292 error("Pattern too complex\n");
296 /*** Report a bad pattern specification ****************/
297 void badpat(char *message
, char *source
, char *stop
)
298 /* char *message; // Error message */
299 /* char *source; // Pattern start */
300 /* char *stop; // Pattern end */
302 fprintf(stderr
, "-GREP-E-%s, pattern is\"%s\"\n", message
, source
);
303 fprintf(stderr
, "-GREP-E-Stopped at byte %d, '%c'\n",
304 stop
-source
, stop
[-1]);
305 error("?GREP-E-Bad pattern\n");
308 /*** Scan the file for the pattern in pbuf[] ***********/
309 void grep(FILE *fp
, char *fn
)
310 /* FILE *fp; // File to process */
311 /* char *fn; // File name (for -f option) */
317 while (fgets(lbuf
, LMAX
, fp
)) {
320 if ((m
&& !vflag
) || (!m
&& vflag
)) {
329 printf("%s\n", lbuf
);
336 printf("%d\n", count
);
340 /*** Match line (lbuf) with pattern (pbuf) return 1 if match ***/
343 char *l
; /* Line pointer */
345 for (l
= lbuf
; *l
; ++l
) {
352 /*** Match partial line with pattern *******************/
353 char *pmatch(char *line
, char *pattern
)
354 /* char *line; // (partial) line to match */
355 /* char *pattern; // (partial) pattern to match */
357 char *l
; /* Current line pointer */
358 char *p
; /* Current pattern pointer */
359 char c
; /* Current character */
360 char *e
; /* End for STAR and PLUS match */
361 int op
; /* Pattern operation */
362 int n
; /* Class counter */
363 char *are
; /* Start of STAR match */
367 printf("pmatch(\"%s\")\n", line
);
369 while ((op
= *p
++) != ENDPAT
) {
371 printf("byte[%d] = 0%o, '%c', op = 0%o\n",
376 if (tolower(*l
++) != *p
++)
396 if ((c
= *l
++) < '0' || (c
> '9'))
402 if (c
< 'a' || c
> 'z')
408 if (c
>= 'a' && c
<= 'z')
410 else if (c
< '0' || c
> '9')
416 if (c
== 0 || c
> ' ')
428 if (c
>= p
[-2] && c
<= p
[-1])
434 if ((op
== CLASS
) == (n
<= 1))
441 e
= pmatch(l
, p
); /* Look for a match */
442 while (*p
++ != ENDPAT
); /* Skip over pattern */
443 if (e
) /* Got a match? */
444 l
= e
; /* Yes, update string */
445 break; /* Always succeeds */
447 case PLUS
: /* One or more ... */
448 if ((l
= pmatch(l
, p
)) == 0)
449 return(0); /* Gotta have a match */
450 case STAR
: /* Zero or more ... */
451 are
= l
; /* Remember line start */
452 while (*l
&& (e
= pmatch(l
, p
)))
453 l
= e
; /* Get longest match */
454 while (*p
++ != ENDPAT
); /* Skip over pattern */
455 while (l
>= are
) { /* Try to match rest */
456 if (e
= pmatch(l
, p
))
458 --l
; /* Nope, try earlier */
460 return(0); /* Nothing else worked */
463 printf("Bad op code %d\n", op
);
464 error("Cannot happen -- match\n");
470 /*** Report an error ***********************************/
473 fprintf(stderr
, "%s", s
);
477 /*** Main program - parse arguments & grep *************/
478 int main(int argc
, char **argv
)
487 usage("No arguments");
488 if (argc
== 2 && argv
[1][0] == '?' && argv
[1][1] == 0) {
495 for (i
=1; i
< argc
; ++i
) {
532 usage("Unknown flag");
537 } else if (!gotpattern
) {
549 fflag
= fflag
^ (nfile
> 0);
550 for (i
=1; i
< argc
; ++i
) {
552 if ((f
=fopen(p
, "r")) == NULL
)
564 /* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/