unstack, sort: cleanup and improvement
[minix.git] / commands / grep / grep.c
blob310abba75c08b74b40672662f85d11d7b98f0d3a
1 /* $OpenBSD: grep.c,v 1.38 2007/02/13 21:48:20 kili Exp $ */
3 /*-
4 * Copyright (c) 1999 James Howard and Dag-Erling Coïdan Smørgrav
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <sys/queue.h>
33 #include <ctype.h>
34 #include <err.h>
35 #include <errno.h>
36 #include <getopt.h>
37 #include <regex.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <unistd.h>
43 #include "grep.h"
45 /* Flags passed to regcomp() and regexec() */
46 int cflags;
47 int eflags = REG_STARTEND;
49 int matchall; /* shortcut */
50 int patterns, pattern_sz;
51 char **pattern;
52 regex_t *r_pattern;
53 fastgrep_t *fg_pattern;
55 /* For regex errors */
56 char re_error[RE_ERROR_BUF + 1];
58 /* Command-line flags */
59 int Aflag; /* -A x: print x lines trailing each match */
60 int Bflag; /* -B x: print x lines leading each match */
61 int Eflag; /* -E: interpret pattern as extended regexp */
62 int Fflag; /* -F: interpret pattern as list of fixed strings */
63 int Gflag; /* -G: interpret pattern as basic regexp */
64 int Hflag; /* -H: if -R, follow explicitly listed symlinks */
65 int Lflag; /* -L: only show names of files with no matches */
66 int Pflag; /* -P: if -R, no symlinks are followed */
67 int Rflag; /* -R: recursively search directory trees */
68 int Sflag; /* -S: if -R, follow all symlinks */
69 #ifndef NOZ
70 int Zflag; /* -Z: decompress input before processing */
71 #endif
72 int bflag; /* -b: show block numbers for each match */
73 int cflag; /* -c: only show a count of matching lines */
74 int hflag; /* -h: don't print filename headers */
75 int iflag; /* -i: ignore case */
76 int lflag; /* -l: only show names of files with matches */
77 int nflag; /* -n: show line numbers in front of matching lines */
78 int oflag; /* -o: always print file name */
79 int qflag; /* -q: quiet mode (don't output anything) */
80 int sflag; /* -s: silent mode (ignore errors) */
81 int vflag; /* -v: only show non-matching lines */
82 int wflag; /* -w: pattern must start and end on word boundaries */
83 int xflag; /* -x: pattern must match entire line */
84 int lbflag; /* --line-buffered */
86 int binbehave = BIN_FILE_BIN;
88 enum {
89 BIN_OPT = CHAR_MAX + 1,
90 HELP_OPT,
91 MMAP_OPT,
92 LINEBUF_OPT
95 /* Housekeeping */
96 int first; /* flag whether or not this is our first match */
97 int tail; /* lines left to print */
99 struct patfile {
100 const char *pf_file;
101 SLIST_ENTRY(patfile) pf_next;
103 SLIST_HEAD(, patfile) patfilelh;
105 static void
106 usage(void)
108 fprintf(stderr,
109 #ifdef NOZ
110 "usage: %s [-abcEFGHhIiLlnoPqRSsUVvwx] [-A num] [-B num] [-C[num]]\n"
111 #else
112 "usage: %s [-abcEFGHhIiLlnoPqRSsUVvwxZ] [-A num] [-B num] [-C[num]]\n"
113 #endif
114 "\t[-e pattern] [-f file] [--binary-files=value] [--context[=num]]\n"
115 "\t[--line-buffered] [pattern] [file ...]\n", getprogname());
116 exit(2);
119 #ifdef NOZ
120 static char *optstr = "0123456789A:B:CEFGHILPSRUVabce:f:hilnoqrsuvwxy";
121 #else
122 static char *optstr = "0123456789A:B:CEFGHILPSRUVZabce:f:hilnoqrsuvwxy";
123 #endif
125 struct option long_options[] =
127 {"binary-files", required_argument, NULL, BIN_OPT},
128 {"help", no_argument, NULL, HELP_OPT},
129 {"mmap", no_argument, NULL, MMAP_OPT},
130 {"line-buffered", no_argument, NULL, LINEBUF_OPT},
131 {"after-context", required_argument, NULL, 'A'},
132 {"before-context", required_argument, NULL, 'B'},
133 {"context", optional_argument, NULL, 'C'},
134 {"devices", required_argument, NULL, 'D'},
135 {"extended-regexp", no_argument, NULL, 'E'},
136 {"fixed-strings", no_argument, NULL, 'F'},
137 {"basic-regexp", no_argument, NULL, 'G'},
138 {"binary", no_argument, NULL, 'U'},
139 {"version", no_argument, NULL, 'V'},
140 {"text", no_argument, NULL, 'a'},
141 {"byte-offset", no_argument, NULL, 'b'},
142 {"count", no_argument, NULL, 'c'},
143 {"regexp", required_argument, NULL, 'e'},
144 {"file", required_argument, NULL, 'f'},
145 {"no-filename", no_argument, NULL, 'h'},
146 {"ignore-case", no_argument, NULL, 'i'},
147 {"files-without-match", no_argument, NULL, 'L'},
148 {"files-with-matches", no_argument, NULL, 'l'},
149 {"line-number", no_argument, NULL, 'n'},
150 {"quiet", no_argument, NULL, 'q'},
151 {"silent", no_argument, NULL, 'q'},
152 {"recursive", no_argument, NULL, 'r'},
153 {"no-messages", no_argument, NULL, 's'},
154 {"revert-match", no_argument, NULL, 'v'},
155 {"word-regexp", no_argument, NULL, 'w'},
156 {"line-regexp", no_argument, NULL, 'x'},
157 {"unix-byte-offsets", no_argument, NULL, 'u'},
158 #ifndef NOZ
159 {"decompress", no_argument, NULL, 'Z'},
160 #endif
161 {NULL, no_argument, NULL, 0}
165 static void
166 add_pattern(char *pat, size_t len)
168 if (!xflag && (len == 0 || matchall)) {
169 matchall = 1;
170 return;
172 if (patterns == pattern_sz) {
173 pattern_sz *= 2;
174 pattern = grep_realloc(pattern, ++pattern_sz * sizeof(*pattern));
176 if (len > 0 && pat[len - 1] == '\n')
177 --len;
178 /* pat may not be NUL-terminated */
179 if (wflag && !Fflag) {
180 int bol = 0, eol = 0, extra;
181 if (pat[0] == '^')
182 bol = 1;
183 if (len > 0 && pat[len - 1] == '$')
184 eol = 1;
185 extra = Eflag ? 2 : 4;
186 pattern[patterns] = grep_malloc(len + 15 + extra);
187 snprintf(pattern[patterns], len + 15 + extra,
188 "%s[[:<:]]%s%.*s%s[[:>:]]%s",
189 bol ? "^" : "",
190 Eflag ? "(" : "\\(",
191 (int)len - bol - eol, pat + bol,
192 Eflag ? ")" : "\\)",
193 eol ? "$" : "");
194 len += 14 + extra;
195 } else {
196 pattern[patterns] = grep_malloc(len + 1);
197 memcpy(pattern[patterns], pat, len);
198 pattern[patterns][len] = '\0';
200 ++patterns;
203 static void
204 add_patterns(char *pats)
206 char *nl;
208 while ((nl = strchr(pats, '\n')) != NULL) {
209 add_pattern(pats, nl - pats);
210 pats = nl + 1;
212 add_pattern(pats, strlen(pats));
215 static void
216 read_patterns(const char *fn)
218 FILE *f;
219 char *line;
220 size_t len;
222 if ((f = fopen(fn, "r")) == NULL)
223 err(2, "%s", fn);
224 while ((line = fgetln(f, &len)) != NULL)
225 add_pattern(line, *line == '\n' ? 0 : len);
226 if (ferror(f))
227 err(2, "%s", fn);
228 fclose(f);
232 main(int argc, char *argv[])
234 int c, lastc, prevoptind, newarg, i, needpattern;
235 struct patfile *patfile, *pf_next;
236 long l;
237 char *ep;
239 SLIST_INIT(&patfilelh);
241 switch (getprogname()[0]) {
242 case 'e':
243 Eflag++;
244 break;
245 case 'f':
246 Fflag++;
247 break;
248 case 'g':
249 Gflag++;
250 break;
251 #ifndef NOZ
252 case 'z':
253 Zflag++;
254 switch(getprogname()[1]) {
255 case 'e':
256 Eflag++;
257 break;
258 case 'f':
259 Fflag++;
260 break;
261 case 'g':
262 Gflag++;
263 break;
265 break;
266 #endif
269 lastc = '\0';
270 newarg = 1;
271 prevoptind = 1;
272 needpattern = 1;
273 while ((c = getopt_long(argc, argv, optstr,
274 long_options, NULL)) != -1) {
275 switch (c) {
276 case '0': case '1': case '2': case '3': case '4':
277 case '5': case '6': case '7': case '8': case '9':
278 if (newarg || !isdigit(lastc))
279 Aflag = 0;
280 else if (Aflag > INT_MAX / 10)
281 errx(2, "context out of range");
282 Aflag = Bflag = (Aflag * 10) + (c - '0');
283 break;
284 case 'A':
285 case 'B':
286 l = strtol(optarg, &ep, 10);
287 if (ep == optarg || *ep != '\0' ||
288 l <= 0 || l >= INT_MAX)
289 errx(2, "context out of range");
290 if (c == 'A')
291 Aflag = (int)l;
292 else
293 Bflag = (int)l;
294 break;
295 case 'C':
296 if (optarg == NULL)
297 Aflag = Bflag = 2;
298 else {
299 l = strtol(optarg, &ep, 10);
300 if (ep == optarg || *ep != '\0' ||
301 l <= 0 || l >= INT_MAX)
302 errx(2, "context out of range");
303 Aflag = Bflag = (int)l;
305 break;
306 case 'E':
307 Fflag = Gflag = 0;
308 Eflag++;
309 break;
310 case 'F':
311 Eflag = Gflag = 0;
312 Fflag++;
313 break;
314 case 'G':
315 Eflag = Fflag = 0;
316 Gflag++;
317 break;
318 case 'H':
319 Hflag++;
320 break;
321 case 'I':
322 binbehave = BIN_FILE_SKIP;
323 break;
324 case 'L':
325 lflag = 0;
326 Lflag = qflag = 1;
327 break;
328 case 'P':
329 Pflag++;
330 break;
331 case 'S':
332 Sflag++;
333 break;
334 case 'R':
335 case 'r':
336 Rflag++;
337 oflag++;
338 break;
339 case 'U':
340 binbehave = BIN_FILE_BIN;
341 break;
342 case 'V':
343 fprintf(stderr, "grep version %u.%u\n", VER_MAJ, VER_MIN);
344 exit(0);
345 break;
346 #ifndef NOZ
347 case 'Z':
348 Zflag++;
349 break;
350 #endif
351 case 'a':
352 binbehave = BIN_FILE_TEXT;
353 break;
354 case 'b':
355 bflag = 1;
356 break;
357 case 'c':
358 cflag = 1;
359 break;
360 case 'e':
361 add_patterns(optarg);
362 needpattern = 0;
363 break;
364 case 'f':
365 patfile = grep_malloc(sizeof(*patfile));
366 patfile->pf_file = optarg;
367 SLIST_INSERT_HEAD(&patfilelh, patfile, pf_next);
368 needpattern = 0;
369 break;
370 case 'h':
371 oflag = 0;
372 hflag = 1;
373 break;
374 case 'i':
375 case 'y':
376 iflag = 1;
377 cflags |= REG_ICASE;
378 break;
379 case 'l':
380 Lflag = 0;
381 lflag = qflag = 1;
382 break;
383 case 'n':
384 nflag = 1;
385 break;
386 case 'o':
387 hflag = 0;
388 oflag = 1;
389 break;
390 case 'q':
391 qflag = 1;
392 break;
393 case 's':
394 sflag = 1;
395 break;
396 case 'v':
397 vflag = 1;
398 break;
399 case 'w':
400 wflag = 1;
401 break;
402 case 'x':
403 xflag = 1;
404 break;
405 case BIN_OPT:
406 if (strcmp("binary", optarg) == 0)
407 binbehave = BIN_FILE_BIN;
408 else if (strcmp("without-match", optarg) == 0)
409 binbehave = BIN_FILE_SKIP;
410 else if (strcmp("text", optarg) == 0)
411 binbehave = BIN_FILE_TEXT;
412 else
413 errx(2, "Unknown binary-files option");
414 break;
415 case 'u':
416 case MMAP_OPT:
417 /* default, compatibility */
418 break;
419 case LINEBUF_OPT:
420 lbflag = 1;
421 break;
422 case HELP_OPT:
423 default:
424 usage();
426 lastc = c;
427 newarg = optind != prevoptind;
428 prevoptind = optind;
430 argc -= optind;
431 argv += optind;
433 for (patfile = SLIST_FIRST(&patfilelh); patfile != NULL;
434 patfile = pf_next) {
435 pf_next = SLIST_NEXT(patfile, pf_next);
436 read_patterns(patfile->pf_file);
437 free(patfile);
440 if (argc == 0 && needpattern)
441 usage();
443 if (argc != 0 && needpattern) {
444 add_patterns(*argv);
445 --argc;
446 ++argv;
449 if (Eflag)
450 cflags |= REG_EXTENDED;
451 fg_pattern = grep_calloc(patterns, sizeof(*fg_pattern));
452 r_pattern = grep_calloc(patterns, sizeof(*r_pattern));
453 for (i = 0; i < patterns; ++i) {
454 /* Check if cheating is allowed (always is for fgrep). */
455 if (Fflag) {
456 fgrepcomp(&fg_pattern[i], pattern[i]);
457 } else {
458 if (fastcomp(&fg_pattern[i], pattern[i])) {
459 /* Fall back to full regex library */
460 c = regcomp(&r_pattern[i], pattern[i], cflags);
461 if (c != 0) {
462 regerror(c, &r_pattern[i], re_error,
463 RE_ERROR_BUF);
464 errx(2, "%s", re_error);
470 #ifndef __minix
471 if (lbflag)
472 setlinebuf(stdout);
473 #endif
475 if ((argc == 0 || argc == 1) && !oflag)
476 hflag = 1;
478 if (argc == 0)
479 exit(!procfile(NULL));
481 if (Rflag)
482 c = grep_tree(argv);
483 else
484 for (c = 0; argc--; ++argv)
485 c += procfile(*argv);
487 exit(!c);