grep.c revision 1.3 1 1.3 rillig /* $NetBSD: grep.c,v 1.3 2006/05/15 21:12:21 rillig Exp $ */
2 1.2 cjep
3 1.1 cjep /*-
4 1.1 cjep * Copyright (c) 1999 James Howard and Dag-Erling Codan Smrgrav
5 1.1 cjep * All rights reserved.
6 1.1 cjep *
7 1.1 cjep * Redistribution and use in source and binary forms, with or without
8 1.1 cjep * modification, are permitted provided that the following conditions
9 1.1 cjep * are met:
10 1.1 cjep * 1. Redistributions of source code must retain the above copyright
11 1.1 cjep * notice, this list of conditions and the following disclaimer.
12 1.1 cjep * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cjep * notice, this list of conditions and the following disclaimer in the
14 1.1 cjep * documentation and/or other materials provided with the distribution.
15 1.1 cjep *
16 1.1 cjep * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 1.1 cjep * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 1.1 cjep * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 1.1 cjep * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 1.1 cjep * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 1.1 cjep * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 1.1 cjep * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 1.1 cjep * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 1.1 cjep * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1 cjep * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1 cjep * SUCH DAMAGE.
27 1.1 cjep *
28 1.1 cjep */
29 1.2 cjep
30 1.2 cjep
31 1.2 cjep
32 1.2 cjep #include <sys/cdefs.h>
33 1.2 cjep #ifndef lint
34 1.3 rillig __RCSID("$NetBSD: grep.c,v 1.3 2006/05/15 21:12:21 rillig Exp $");
35 1.2 cjep #endif /* not lint */
36 1.1 cjep
37 1.1 cjep #include <sys/types.h>
38 1.1 cjep #include <sys/stat.h>
39 1.1 cjep
40 1.1 cjep #include <err.h>
41 1.1 cjep #include <errno.h>
42 1.1 cjep #include <getopt.h>
43 1.2 cjep #include <limits.h>
44 1.1 cjep #include <regex.h>
45 1.1 cjep #include <stdio.h>
46 1.1 cjep #include <stdlib.h>
47 1.1 cjep #include <string.h>
48 1.1 cjep #include <unistd.h>
49 1.1 cjep
50 1.1 cjep #include "grep.h"
51 1.1 cjep
52 1.2 cjep /*
53 1.2 cjep * Upper bound of number of digits to represent an int in decimal
54 1.2 cjep * 2^8n <= 10^3n. Allow a terminator.
55 1.2 cjep */
56 1.2 cjep #define MAX_BUF_DIGITS (sizeof(int) * 3) + 1
57 1.2 cjep
58 1.1 cjep /* Flags passed to regcomp() and regexec() */
59 1.2 cjep int cflags = REG_BASIC;
60 1.2 cjep int eflags = REG_STARTEND;
61 1.1 cjep
62 1.2 cjep int matchall; /* shortcut */
63 1.2 cjep int patterns, pattern_sz;
64 1.2 cjep char **pattern;
65 1.1 cjep regex_t *r_pattern;
66 1.1 cjep
67 1.1 cjep /* For regex errors */
68 1.2 cjep char re_error[RE_ERROR_BUF + 1];
69 1.1 cjep
70 1.1 cjep /* Command-line flags */
71 1.2 cjep int Aflag; /* -A x: print x lines trailing each match */
72 1.2 cjep int Bflag; /* -B x: print x lines leading each match */
73 1.2 cjep int Eflag; /* -E: interpret pattern as extended regexp */
74 1.2 cjep int Fflag; /* -F: interpret pattern as list of fixed strings */
75 1.2 cjep int Gflag; /* -G: interpret pattern as basic regexp */
76 1.2 cjep int Hflag; /* -H: Always print filenames */
77 1.2 cjep int Lflag; /* -L: only show names of files with no matches */
78 1.2 cjep /*int Pflag; *//* -P: if -r, no symlinks are followed */
79 1.2 cjep /*int Sflag; *//* -S: if -r, follow all symlinks */
80 1.2 cjep int bflag; /* -b: show block numbers for each match */
81 1.2 cjep int cflag; /* -c: only show a count of matching lines */
82 1.2 cjep int hflag; /* -h: Never print filenames. -H overrides */
83 1.2 cjep int lflag; /* -l: only show names of files with matches */
84 1.2 cjep int mflag; /* -m: specify maximum line matches (per file) */
85 1.2 cjep int nflag; /* -n: show line numbers in front of matching lines */
86 1.2 cjep int oflag; /* -o: only print out matches */
87 1.2 cjep int qflag; /* -q: quiet mode (don't output anything) */
88 1.2 cjep int sflag; /* -s: silent mode (ignore errors) */
89 1.2 cjep int vflag; /* -v: only show non-matching lines */
90 1.2 cjep int wflag; /* -w: pattern must start and end on word boundaries */
91 1.2 cjep int xflag; /* -x: pattern must match entire line */
92 1.2 cjep int lbflag; /* --line-buffered */
93 1.2 cjep
94 1.2 cjep int colours = 0; /* Attempt to use terminal colours */
95 1.2 cjep const char *grep_colour = "01;32"; /* Default colour string, green */
96 1.2 cjep char *uc;
97 1.2 cjep
98 1.2 cjep /* Characters to print after filenames */
99 1.2 cjep char fn_endchar = '\n';
100 1.2 cjep char fn_colonchar = ':';
101 1.2 cjep char fn_dashchar = '-';
102 1.2 cjep char line_endchar = '\n'; /* End of line character */
103 1.2 cjep
104 1.2 cjep int maxcount = 0; /* Maximum line matches per file */
105 1.2 cjep int output_filenames = 0;
106 1.2 cjep
107 1.2 cjep /* Argv[0] flags */
108 1.2 cjep int zgrep; /* If we are invoked as zgrep */
109 1.2 cjep
110 1.2 cjep int binbehave = BIN_FILE_BIN;
111 1.2 cjep int dirbehave = GREP_READ;
112 1.2 cjep int devbehave = GREP_READ;
113 1.2 cjep /*int linkbehave = LINK_FOLLOW;*/
114 1.3 rillig const char *stdin_label;
115 1.2 cjep
116 1.2 cjep enum {
117 1.2 cjep BIN_OPT = CHAR_MAX + 1,
118 1.2 cjep HELP_OPT,
119 1.2 cjep LABEL_OPT,
120 1.2 cjep MMAP_OPT,
121 1.2 cjep LINK_OPT,
122 1.2 cjep COLOUR_OPT,
123 1.2 cjep LINEBUF_OPT
124 1.2 cjep };
125 1.1 cjep
126 1.1 cjep /* Housekeeping */
127 1.2 cjep int first; /* flag whether or not this is our first match */
128 1.2 cjep int tail; /* lines left to print */
129 1.1 cjep
130 1.1 cjep static void
131 1.1 cjep usage(void)
132 1.1 cjep {
133 1.1 cjep fprintf(stderr, "usage: %s %s %s\n",
134 1.2 cjep getprogname(),
135 1.2 cjep "[-[ABC] num] [-EFGHILVZabcdhilnoqrsvwxz]",
136 1.2 cjep "[-D action] [-d action] [-e pattern] [-f file]");
137 1.1 cjep exit(2);
138 1.1 cjep }
139 1.1 cjep
140 1.3 rillig static const char *optstr = "0123456789A:B:C:D:EFGHILUVZabcd:e:f:hilm:noqrsuvwxyz";
141 1.1 cjep
142 1.2 cjep struct option long_options[] =
143 1.1 cjep {
144 1.2 cjep {"binary-files", required_argument, NULL, BIN_OPT},
145 1.2 cjep {"help", no_argument, NULL, HELP_OPT},
146 1.2 cjep {"label", required_argument, NULL, LABEL_OPT},
147 1.2 cjep {"mmap", no_argument, NULL, MMAP_OPT},
148 1.2 cjep {"line-buffered", no_argument, NULL, LINEBUF_OPT},
149 1.2 cjep /* {"links", required_argument, NULL, LINK_OPT},*/
150 1.1 cjep {"after-context", required_argument, NULL, 'A'},
151 1.1 cjep {"before-context", required_argument, NULL, 'B'},
152 1.2 cjep {"color", optional_argument, NULL, COLOUR_OPT},
153 1.2 cjep {"colour", optional_argument, NULL, COLOUR_OPT},
154 1.1 cjep {"context", optional_argument, NULL, 'C'},
155 1.2 cjep {"devices", required_argument, NULL, 'D'},
156 1.2 cjep {"extended-regexp", no_argument, NULL, 'E'},
157 1.2 cjep {"fixed-strings", no_argument, NULL, 'F'},
158 1.2 cjep {"fixed-regexp", no_argument, NULL, 'F'},
159 1.2 cjep {"basic-regexp", no_argument, NULL, 'G'},
160 1.2 cjep {"with-filename", no_argument, NULL, 'H'},
161 1.2 cjep {"files-without-match", no_argument, NULL, 'L'},
162 1.2 cjep {"binary", no_argument, NULL, 'U'},
163 1.1 cjep {"version", no_argument, NULL, 'V'},
164 1.2 cjep {"null", no_argument, NULL, 'Z'},
165 1.2 cjep {"text", no_argument, NULL, 'a'},
166 1.1 cjep {"byte-offset", no_argument, NULL, 'b'},
167 1.1 cjep {"count", no_argument, NULL, 'c'},
168 1.2 cjep {"directories", required_argument, NULL, 'd'},
169 1.1 cjep {"regexp", required_argument, NULL, 'e'},
170 1.1 cjep {"file", required_argument, NULL, 'f'},
171 1.1 cjep {"no-filename", no_argument, NULL, 'h'},
172 1.1 cjep {"ignore-case", no_argument, NULL, 'i'},
173 1.1 cjep {"files-with-matches", no_argument, NULL, 'l'},
174 1.2 cjep {"max-count", required_argument, NULL, 'm'},
175 1.1 cjep {"line-number", no_argument, NULL, 'n'},
176 1.2 cjep {"only-matching", no_argument, NULL, 'o'},
177 1.1 cjep {"quiet", no_argument, NULL, 'q'},
178 1.1 cjep {"silent", no_argument, NULL, 'q'},
179 1.1 cjep {"recursive", no_argument, NULL, 'r'},
180 1.1 cjep {"no-messages", no_argument, NULL, 's'},
181 1.2 cjep {"unix-byte-offsets", no_argument, NULL, 'u'},
182 1.2 cjep {"invert-match", no_argument, NULL, 'v'},
183 1.1 cjep {"word-regexp", no_argument, NULL, 'w'},
184 1.1 cjep {"line-regexp", no_argument, NULL, 'x'},
185 1.2 cjep {"null-data", no_argument, NULL, 'z'},
186 1.2 cjep
187 1.1 cjep {NULL, no_argument, NULL, 0}
188 1.1 cjep };
189 1.1 cjep
190 1.1 cjep static void
191 1.1 cjep add_pattern(char *pat, size_t len)
192 1.1 cjep {
193 1.1 cjep if (len == 0 || matchall) {
194 1.1 cjep matchall = 1;
195 1.1 cjep return;
196 1.1 cjep }
197 1.1 cjep if (patterns == pattern_sz) {
198 1.1 cjep pattern_sz *= 2;
199 1.2 cjep pattern = grep_realloc(pattern, ++pattern_sz * sizeof(*pattern));
200 1.1 cjep }
201 1.2 cjep if (pat[len - 1] == '\n')
202 1.1 cjep --len;
203 1.2 cjep pattern[patterns] = grep_malloc(len + 1);
204 1.1 cjep strncpy(pattern[patterns], pat, len);
205 1.1 cjep pattern[patterns][len] = '\0';
206 1.1 cjep ++patterns;
207 1.1 cjep }
208 1.1 cjep
209 1.1 cjep static void
210 1.1 cjep read_patterns(char *fn)
211 1.1 cjep {
212 1.1 cjep FILE *f;
213 1.1 cjep char *line;
214 1.1 cjep size_t len;
215 1.1 cjep int nl;
216 1.1 cjep
217 1.1 cjep if ((f = fopen(fn, "r")) == NULL)
218 1.2 cjep err(2, "%s", fn);
219 1.1 cjep nl = 0;
220 1.1 cjep while ((line = fgetln(f, &len)) != NULL) {
221 1.1 cjep if (*line == '\n') {
222 1.1 cjep ++nl;
223 1.1 cjep continue;
224 1.1 cjep }
225 1.1 cjep if (nl) {
226 1.1 cjep matchall = 1;
227 1.1 cjep break;
228 1.1 cjep }
229 1.1 cjep nl = 0;
230 1.1 cjep add_pattern(line, len);
231 1.1 cjep }
232 1.1 cjep if (ferror(f))
233 1.2 cjep err(2, "%s", fn);
234 1.1 cjep fclose(f);
235 1.1 cjep }
236 1.1 cjep
237 1.2 cjep static int
238 1.2 cjep check_context_arg(char const *str) {
239 1.2 cjep char *ep;
240 1.2 cjep long lval;
241 1.2 cjep
242 1.2 cjep errno = 0;
243 1.2 cjep lval = strtol(str, &ep, 10);
244 1.2 cjep
245 1.2 cjep if (str[0] == '\0' || *ep != '\0')
246 1.2 cjep errx(2, "Invalid context argument");
247 1.2 cjep
248 1.2 cjep if ((errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN)) ||
249 1.2 cjep (lval > INT_MAX || lval < INT_MIN))
250 1.2 cjep errx(2, "Context argument out of range");
251 1.2 cjep
252 1.2 cjep return lval;
253 1.2 cjep
254 1.2 cjep }
255 1.2 cjep
256 1.2 cjep static int
257 1.2 cjep grep_getopt(int argc, char *const *argv)
258 1.2 cjep {
259 1.3 rillig size_t ptr;
260 1.3 rillig int c;
261 1.2 cjep char buffer[MAX_BUF_DIGITS];
262 1.2 cjep
263 1.2 cjep ptr = 0;
264 1.2 cjep while (c = getopt_long(argc, argv, optstr, long_options,
265 1.2 cjep (int *)NULL), '0' <= c &&
266 1.2 cjep c <= '9' && ptr < MAX_BUF_DIGITS) {
267 1.2 cjep
268 1.2 cjep /* Avoid leading zeros */
269 1.2 cjep if (ptr != 0 || (ptr == 0 && c != '0'))
270 1.2 cjep buffer[ptr++] = c;
271 1.2 cjep }
272 1.2 cjep
273 1.2 cjep if (ptr >= MAX_BUF_DIGITS)
274 1.2 cjep errx(2, "Context argument out of range");
275 1.2 cjep
276 1.3 rillig if (ptr != 0) {
277 1.2 cjep buffer[ptr] = '\0'; /* We now have a string of digits */
278 1.2 cjep Aflag = Bflag = check_context_arg(buffer);
279 1.2 cjep }
280 1.2 cjep
281 1.2 cjep return c;
282 1.2 cjep }
283 1.2 cjep
284 1.1 cjep int
285 1.1 cjep main(int argc, char *argv[])
286 1.1 cjep {
287 1.2 cjep const char *progname;
288 1.1 cjep int c, i;
289 1.2 cjep struct stat sb;
290 1.2 cjep
291 1.2 cjep stdin_label = "(standard input)";
292 1.1 cjep
293 1.2 cjep progname = getprogname();
294 1.2 cjep switch (progname[0]) {
295 1.2 cjep case 'e':
296 1.2 cjep Eflag++;
297 1.2 cjep break;
298 1.2 cjep case 'f':
299 1.2 cjep Fflag++;
300 1.2 cjep break;
301 1.2 cjep case 'g':
302 1.2 cjep Gflag++;
303 1.2 cjep break;
304 1.2 cjep case 'z':
305 1.2 cjep zgrep++;
306 1.2 cjep switch (progname[1]) {
307 1.2 cjep case 'e':
308 1.2 cjep Eflag++;
309 1.2 cjep break;
310 1.2 cjep case 'f':
311 1.2 cjep Fflag++;
312 1.2 cjep break;
313 1.2 cjep case 'g':
314 1.2 cjep Gflag++;
315 1.2 cjep break;
316 1.2 cjep }
317 1.2 cjep break;
318 1.2 cjep }
319 1.2 cjep
320 1.2 cjep while ((c = grep_getopt(argc, argv)) != -1) {
321 1.1 cjep
322 1.1 cjep switch (c) {
323 1.2 cjep
324 1.1 cjep case 'A':
325 1.2 cjep Aflag = check_context_arg(optarg);
326 1.1 cjep break;
327 1.1 cjep case 'B':
328 1.2 cjep Bflag = check_context_arg(optarg);
329 1.1 cjep break;
330 1.1 cjep case 'C':
331 1.1 cjep if (optarg == NULL)
332 1.1 cjep Aflag = Bflag = 2;
333 1.1 cjep else
334 1.2 cjep Aflag = Bflag = check_context_arg(optarg);
335 1.2 cjep break;
336 1.2 cjep case 'D':
337 1.2 cjep if (strcmp("read", optarg) == 0)
338 1.2 cjep devbehave = GREP_READ;
339 1.2 cjep else if (strcmp("skip", optarg) == 0)
340 1.2 cjep devbehave = GREP_SKIP;
341 1.2 cjep else {
342 1.2 cjep errx(2, "Unknown device option");
343 1.2 cjep }
344 1.1 cjep break;
345 1.2 cjep
346 1.1 cjep case 'E':
347 1.2 cjep Fflag = Gflag = 0;
348 1.1 cjep Eflag++;
349 1.1 cjep break;
350 1.1 cjep case 'F':
351 1.2 cjep Eflag = Gflag = 0;
352 1.1 cjep Fflag++;
353 1.1 cjep break;
354 1.1 cjep case 'G':
355 1.2 cjep Eflag = Fflag = 0;
356 1.1 cjep Gflag++;
357 1.1 cjep break;
358 1.1 cjep case 'H':
359 1.2 cjep Hflag = 1;
360 1.2 cjep break;
361 1.2 cjep case 'I':
362 1.2 cjep binbehave = BIN_FILE_SKIP;
363 1.1 cjep break;
364 1.1 cjep case 'L':
365 1.1 cjep lflag = 0;
366 1.1 cjep Lflag = qflag = 1;
367 1.1 cjep break;
368 1.2 cjep /* case 'P':
369 1.2 cjep linkbehave = LINK_SKIP;
370 1.1 cjep break;
371 1.1 cjep case 'S':
372 1.2 cjep linkbehave = LINK_FOLLOW;
373 1.2 cjep break;*/
374 1.1 cjep case 'R':
375 1.1 cjep case 'r':
376 1.2 cjep dirbehave = GREP_RECURSE;
377 1.1 cjep break;
378 1.1 cjep case 'U':
379 1.1 cjep case 'u':
380 1.1 cjep /* these are here for compatability */
381 1.1 cjep break;
382 1.1 cjep case 'V':
383 1.2 cjep fprintf(stdout, "grep version %s\n", VERSION);
384 1.2 cjep exit(0);
385 1.1 cjep break;
386 1.1 cjep case 'Z':
387 1.2 cjep fn_colonchar = fn_endchar = fn_dashchar = 0;
388 1.1 cjep break;
389 1.1 cjep case 'a':
390 1.2 cjep binbehave = BIN_FILE_TEXT;
391 1.1 cjep break;
392 1.1 cjep case 'b':
393 1.1 cjep bflag = 1;
394 1.1 cjep break;
395 1.1 cjep case 'c':
396 1.1 cjep cflag = 1;
397 1.1 cjep break;
398 1.2 cjep case 'd':
399 1.2 cjep if (strcmp("read", optarg) == 0)
400 1.2 cjep dirbehave = GREP_READ;
401 1.2 cjep else if (strcmp("skip", optarg) == 0)
402 1.2 cjep dirbehave = GREP_SKIP;
403 1.2 cjep else if (strcmp("recurse", optarg) == 0)
404 1.2 cjep dirbehave = GREP_RECURSE;
405 1.2 cjep else {
406 1.2 cjep errx(2, "Unknown directory option\n");
407 1.2 cjep }
408 1.2 cjep break;
409 1.2 cjep
410 1.1 cjep case 'e':
411 1.1 cjep add_pattern(optarg, strlen(optarg));
412 1.1 cjep break;
413 1.1 cjep case 'f':
414 1.1 cjep read_patterns(optarg);
415 1.1 cjep break;
416 1.1 cjep case 'h':
417 1.1 cjep hflag = 1;
418 1.1 cjep break;
419 1.1 cjep case 'i':
420 1.1 cjep case 'y':
421 1.1 cjep cflags |= REG_ICASE;
422 1.1 cjep break;
423 1.1 cjep case 'l':
424 1.1 cjep Lflag = 0;
425 1.1 cjep lflag = qflag = 1;
426 1.1 cjep break;
427 1.2 cjep case 'm':
428 1.2 cjep mflag = 1;
429 1.2 cjep maxcount = strtol(optarg, (char **)NULL, 10);
430 1.2 cjep break;
431 1.1 cjep case 'n':
432 1.1 cjep nflag = 1;
433 1.1 cjep break;
434 1.1 cjep case 'o':
435 1.1 cjep oflag = 1;
436 1.1 cjep break;
437 1.1 cjep case 'q':
438 1.1 cjep qflag = 1;
439 1.1 cjep break;
440 1.1 cjep case 's':
441 1.1 cjep sflag = 1;
442 1.1 cjep break;
443 1.1 cjep case 'v':
444 1.1 cjep vflag = 1;
445 1.1 cjep break;
446 1.1 cjep case 'w':
447 1.1 cjep wflag = 1;
448 1.1 cjep break;
449 1.1 cjep case 'x':
450 1.1 cjep xflag = 1;
451 1.1 cjep break;
452 1.2 cjep case 'z':
453 1.2 cjep line_endchar = 0;
454 1.2 cjep break;
455 1.2 cjep case BIN_OPT:
456 1.2 cjep if (strcmp("binary", optarg) == 0)
457 1.2 cjep binbehave = BIN_FILE_BIN;
458 1.2 cjep else if (strcmp("without-match", optarg) == 0)
459 1.2 cjep binbehave = BIN_FILE_SKIP;
460 1.2 cjep else if (strcmp("text", optarg) == 0)
461 1.2 cjep binbehave = BIN_FILE_TEXT;
462 1.2 cjep else {
463 1.2 cjep errx(2, "Unknown binary-files option\n");
464 1.2 cjep }
465 1.2 cjep break;
466 1.2 cjep
467 1.2 cjep case COLOUR_OPT:
468 1.2 cjep if (optarg == NULL || strcmp("auto", optarg) == 0 ||
469 1.2 cjep strcmp("tty", optarg) == 0 ||
470 1.2 cjep strcmp("if-tty", optarg) == 0) {
471 1.2 cjep
472 1.2 cjep /* Check that stdout is a terminal */
473 1.2 cjep if (isatty(STDOUT_FILENO) &&
474 1.2 cjep getenv("TERM") &&
475 1.2 cjep strcmp(getenv("TERM"), "dumb") != 0)
476 1.2 cjep colours = 1;
477 1.2 cjep else
478 1.2 cjep colours = 0;
479 1.2 cjep
480 1.2 cjep } else if (strcmp("always", optarg) == 0 ||
481 1.2 cjep strcmp("yes", optarg) == 0 ||
482 1.2 cjep strcmp("force", optarg) == 0)
483 1.2 cjep colours = 1;
484 1.2 cjep else if (strcmp("never", optarg) == 0 ||
485 1.2 cjep strcmp("no", optarg) == 0 ||
486 1.2 cjep strcmp("none", optarg) == 0)
487 1.2 cjep colours = 0;
488 1.2 cjep else
489 1.2 cjep errx(2, "Unknown color option\n");
490 1.2 cjep
491 1.2 cjep uc = getenv("GREP_COLOR");
492 1.2 cjep if (colours == 1 && uc != NULL && *uc != '\0')
493 1.2 cjep grep_colour = uc;
494 1.2 cjep break;
495 1.2 cjep case LABEL_OPT:
496 1.2 cjep stdin_label = optarg;
497 1.2 cjep break;
498 1.2 cjep case MMAP_OPT:
499 1.2 cjep break;
500 1.2 cjep /*
501 1.2 cjep * case LINK_OPT:
502 1.2 cjep * if (strcmp("explicit", optarg) == 0)
503 1.2 cjep * linkbehave = LINK_EXPLICIT;
504 1.2 cjep * else if (strcmp("follow", optarg) == 0)
505 1.2 cjep * linkbehave = LINK_FOLLOW;
506 1.2 cjep * else if (strcmp("skip", optarg) == 0)
507 1.2 cjep * linkbehave = LINK_SKIP;
508 1.2 cjep * else {
509 1.2 cjep * errx(2, "Unknown links option\n");
510 1.2 cjep * }
511 1.2 cjep * break;
512 1.2 cjep */
513 1.2 cjep case LINEBUF_OPT:
514 1.2 cjep lbflag = 1;
515 1.2 cjep break;
516 1.2 cjep
517 1.2 cjep case HELP_OPT:
518 1.1 cjep default:
519 1.1 cjep usage();
520 1.1 cjep }
521 1.2 cjep
522 1.1 cjep }
523 1.2 cjep
524 1.1 cjep argc -= optind;
525 1.1 cjep argv += optind;
526 1.1 cjep
527 1.1 cjep if (argc == 0 && patterns == 0)
528 1.1 cjep usage();
529 1.1 cjep if (patterns == 0) {
530 1.1 cjep add_pattern(*argv, strlen(*argv));
531 1.1 cjep --argc;
532 1.1 cjep ++argv;
533 1.1 cjep }
534 1.1 cjep
535 1.2 cjep if (Eflag)
536 1.2 cjep cflags |= REG_EXTENDED;
537 1.2 cjep else if (Fflag)
538 1.2 cjep cflags |= REG_NOSPEC;
539 1.2 cjep r_pattern = grep_malloc(patterns * sizeof(*r_pattern));
540 1.1 cjep for (i = 0; i < patterns; ++i) {
541 1.1 cjep if ((c = regcomp(&r_pattern[i], pattern[i], cflags))) {
542 1.1 cjep regerror(c, &r_pattern[i], re_error, RE_ERROR_BUF);
543 1.2 cjep errx(2, "%s", re_error);
544 1.1 cjep }
545 1.1 cjep }
546 1.1 cjep
547 1.2 cjep if ((argc > 1 && !hflag) || Hflag)
548 1.2 cjep output_filenames = 1;
549 1.1 cjep
550 1.2 cjep if (argc == 1 && !hflag && dirbehave == GREP_RECURSE)
551 1.2 cjep if (!stat(*argv, &sb) && (sb.st_mode & S_IFMT) == S_IFDIR)
552 1.2 cjep output_filenames = 1;
553 1.2 cjep
554 1.1 cjep if (argc == 0)
555 1.1 cjep exit(!procfile(NULL));
556 1.2 cjep
557 1.2 cjep if (lbflag)
558 1.2 cjep setlinebuf(stdout);
559 1.2 cjep
560 1.2 cjep if (dirbehave == GREP_RECURSE)
561 1.1 cjep c = grep_tree(argv);
562 1.1 cjep else
563 1.1 cjep for (c = 0; argc--; ++argv)
564 1.1 cjep c += procfile(*argv);
565 1.1 cjep
566 1.1 cjep exit(!c);
567 1.1 cjep }
568