Deleted Added
full compact
grep.c (210430) grep.c (210461)
1/* $OpenBSD: grep.c,v 1.42 2010/07/02 22:18:03 tedu Exp $ */
2
3/*-
4 * Copyright (c) 1999 James Howard and Dag-Erling Co�dan Sm�rgrav
5 * Copyright (C) 2008-2009 Gabor Kovesdan <gabor@FreeBSD.org>
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without

--- 14 unchanged lines hidden (view full) ---

23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#include <sys/cdefs.h>
1/* $OpenBSD: grep.c,v 1.42 2010/07/02 22:18:03 tedu Exp $ */
2
3/*-
4 * Copyright (c) 1999 James Howard and Dag-Erling Co�dan Sm�rgrav
5 * Copyright (C) 2008-2009 Gabor Kovesdan <gabor@FreeBSD.org>
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without

--- 14 unchanged lines hidden (view full) ---

23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#include <sys/cdefs.h>
31__FBSDID("$FreeBSD: head/usr.bin/grep/grep.c 210430 2010-07-23 19:36:11Z delphij $");
31__FBSDID("$FreeBSD: head/usr.bin/grep/grep.c 210461 2010-07-25 08:42:18Z gabor $");
32
33#include <sys/stat.h>
34#include <sys/types.h>
35
36#include <ctype.h>
37#include <err.h>
38#include <errno.h>
39#include <getopt.h>

--- 69 unchanged lines hidden (view full) ---

109bool sflag; /* -s: silent mode (ignore errors) */
110bool vflag; /* -v: only show non-matching lines */
111bool wflag; /* -w: pattern must start and end on word boundaries */
112bool xflag; /* -x: pattern must match entire line */
113bool lbflag; /* --line-buffered */
114bool nullflag; /* --null */
115bool exclflag; /* --exclude */
116char *label; /* --label */
32
33#include <sys/stat.h>
34#include <sys/types.h>
35
36#include <ctype.h>
37#include <err.h>
38#include <errno.h>
39#include <getopt.h>

--- 69 unchanged lines hidden (view full) ---

109bool sflag; /* -s: silent mode (ignore errors) */
110bool vflag; /* -v: only show non-matching lines */
111bool wflag; /* -w: pattern must start and end on word boundaries */
112bool xflag; /* -x: pattern must match entire line */
113bool lbflag; /* --line-buffered */
114bool nullflag; /* --null */
115bool exclflag; /* --exclude */
116char *label; /* --label */
117char *color; /* --color */
117const char *color; /* --color */
118int grepbehave = GREP_BASIC; /* -EFGP: type of the regex */
119int binbehave = BINFILE_BIN; /* -aIU: handling of binary files */
120int filebehave = FILE_STDIO; /* -JZ: normal, gzip or bzip2 file */
118int grepbehave = GREP_BASIC; /* -EFGP: type of the regex */
119int binbehave = BINFILE_BIN; /* -aIU: handling of binary files */
120int filebehave = FILE_STDIO; /* -JZ: normal, gzip or bzip2 file */
121int devbehave = DEV_GREP; /* -D: handling of devices */
122int dirbehave = DIR_GREP; /* -dRr: handling of directories */
123int linkbehave = LINK_GREP; /* -OpS: handling of symlinks */
121int devbehave = DEV_READ; /* -D: handling of devices */
122int dirbehave = DIR_READ; /* -dRr: handling of directories */
123int linkbehave = LINK_READ; /* -OpS: handling of symlinks */
124
125enum {
126 BIN_OPT = CHAR_MAX + 1,
127 COLOR_OPT,
128 HELP_OPT,
129 MMAP_OPT,
130 LINEBUF_OPT,
131 LABEL_OPT,
132 NULL_OPT,
133 R_EXCLUDE_OPT,
134 R_INCLUDE_OPT,
135 R_DEXCLUDE_OPT,
136 R_DINCLUDE_OPT
137};
138
124
125enum {
126 BIN_OPT = CHAR_MAX + 1,
127 COLOR_OPT,
128 HELP_OPT,
129 MMAP_OPT,
130 LINEBUF_OPT,
131 LABEL_OPT,
132 NULL_OPT,
133 R_EXCLUDE_OPT,
134 R_INCLUDE_OPT,
135 R_DEXCLUDE_OPT,
136 R_DINCLUDE_OPT
137};
138
139static inline const char *init_color(const char *);
140
139/* Housekeeping */
140bool first = true; /* flag whether we are processing the first match */
141bool prev; /* flag whether or not the previous line matched */
142int tail; /* lines left to print */
143bool notfound; /* file not found */
144
145extern char *__progname;
146

--- 127 unchanged lines hidden (view full) ---

274 err(2, "%s", fn);
275 while ((line = fgetln(f, &len)) != NULL)
276 add_pattern(line, *line == '\n' ? 0 : len);
277 if (ferror(f))
278 err(2, "%s", fn);
279 fclose(f);
280}
281
141/* Housekeeping */
142bool first = true; /* flag whether we are processing the first match */
143bool prev; /* flag whether or not the previous line matched */
144int tail; /* lines left to print */
145bool notfound; /* file not found */
146
147extern char *__progname;
148

--- 127 unchanged lines hidden (view full) ---

276 err(2, "%s", fn);
277 while ((line = fgetln(f, &len)) != NULL)
278 add_pattern(line, *line == '\n' ? 0 : len);
279 if (ferror(f))
280 err(2, "%s", fn);
281 fclose(f);
282}
283
284static inline const char *
285init_color(const char *d)
286{
287 char *c;
288
289 c = getenv("GREP_COLOR");
290 return (c != NULL ? c : d);
291}
292
282int
283main(int argc, char *argv[])
284{
285 char **aargv, **eargv, *eopts;
286 char *ep;
287 unsigned long long l;
288 unsigned int aargc, eargc, i;
289 int c, lastc, needpattern, newarg, prevoptind;

--- 120 unchanged lines hidden (view full) ---

410 break;
411 case 'b':
412 bflag = true;
413 break;
414 case 'c':
415 cflag = true;
416 break;
417 case 'D':
293int
294main(int argc, char *argv[])
295{
296 char **aargv, **eargv, *eopts;
297 char *ep;
298 unsigned long long l;
299 unsigned int aargc, eargc, i;
300 int c, lastc, needpattern, newarg, prevoptind;

--- 120 unchanged lines hidden (view full) ---

421 break;
422 case 'b':
423 bflag = true;
424 break;
425 case 'c':
426 cflag = true;
427 break;
428 case 'D':
418 if (strcmp(optarg, "skip") == 0)
429 if (strcasecmp(optarg, "skip") == 0)
419 devbehave = DEV_SKIP;
430 devbehave = DEV_SKIP;
431 else if (strcasecmp(optarg, "read") == 0)
432 devbehave = DEV_READ;
433 else {
434 errno = EINVAL;
435 err(2, NULL);
436 }
420 break;
421 case 'd':
437 break;
438 case 'd':
422 if (strcmp("recurse", optarg) == 0) {
439 if (strcasecmp("recurse", optarg) == 0) {
423 Hflag = true;
424 dirbehave = DIR_RECURSE;
440 Hflag = true;
441 dirbehave = DIR_RECURSE;
425 } else if (strcmp("skip", optarg) == 0)
442 } else if (strcasecmp("skip", optarg) == 0)
426 dirbehave = DIR_SKIP;
443 dirbehave = DIR_SKIP;
427 else if (strcmp("read", optarg) != 0) {
444 else if (strcasecmp("read", optarg) == 0)
445 dirbehave = DIR_READ;
446 else {
428 errno = EINVAL;
429 err(2, NULL);
430 }
431 break;
432 case 'E':
433 grepbehave = GREP_EXTENDED;
434 break;
435 case 'e':

--- 25 unchanged lines hidden (view full) ---

461 iflag = true;
462 cflags |= REG_ICASE;
463 break;
464 case 'J':
465 filebehave = FILE_BZIP;
466 break;
467 case 'L':
468 lflag = false;
447 errno = EINVAL;
448 err(2, NULL);
449 }
450 break;
451 case 'E':
452 grepbehave = GREP_EXTENDED;
453 break;
454 case 'e':

--- 25 unchanged lines hidden (view full) ---

480 iflag = true;
481 cflags |= REG_ICASE;
482 break;
483 case 'J':
484 filebehave = FILE_BZIP;
485 break;
486 case 'L':
487 lflag = false;
469 Lflag = qflag = true;
488 Lflag = true;
470 break;
471 case 'l':
472 Lflag = false;
489 break;
490 case 'l':
491 Lflag = false;
473 lflag = qflag = true;
492 lflag = true;
474 break;
475 case 'm':
476 mflag = true;
477 errno = 0;
478 mcount = strtoull(optarg, &ep, 10);
479 if (((errno == ERANGE) && (mcount == ULLONG_MAX)) ||
480 ((errno == EINVAL) && (mcount == 0)))
481 err(2, NULL);

--- 13 unchanged lines hidden (view full) ---

495 break;
496 case 'p':
497 linkbehave = LINK_SKIP;
498 break;
499 case 'q':
500 qflag = true;
501 break;
502 case 'S':
493 break;
494 case 'm':
495 mflag = true;
496 errno = 0;
497 mcount = strtoull(optarg, &ep, 10);
498 if (((errno == ERANGE) && (mcount == ULLONG_MAX)) ||
499 ((errno == EINVAL) && (mcount == 0)))
500 err(2, NULL);

--- 13 unchanged lines hidden (view full) ---

514 break;
515 case 'p':
516 linkbehave = LINK_SKIP;
517 break;
518 case 'q':
519 qflag = true;
520 break;
521 case 'S':
503 linkbehave = LINK_GREP;
522 linkbehave = LINK_READ;
504 break;
505 case 'R':
506 case 'r':
507 dirbehave = DIR_RECURSE;
508 Hflag = true;
509 break;
510 case 's':
511 sflag = true;

--- 16 unchanged lines hidden (view full) ---

528 break;
529 case 'x':
530 xflag = true;
531 break;
532 case 'Z':
533 filebehave = FILE_GZIP;
534 break;
535 case BIN_OPT:
523 break;
524 case 'R':
525 case 'r':
526 dirbehave = DIR_RECURSE;
527 Hflag = true;
528 break;
529 case 's':
530 sflag = true;

--- 16 unchanged lines hidden (view full) ---

547 break;
548 case 'x':
549 xflag = true;
550 break;
551 case 'Z':
552 filebehave = FILE_GZIP;
553 break;
554 case BIN_OPT:
536 if (strcmp("binary", optarg) == 0)
555 if (strcasecmp("binary", optarg) == 0)
537 binbehave = BINFILE_BIN;
556 binbehave = BINFILE_BIN;
538 else if (strcmp("without-match", optarg) == 0)
557 else if (strcasecmp("without-match", optarg) == 0)
539 binbehave = BINFILE_SKIP;
558 binbehave = BINFILE_SKIP;
540 else if (strcmp("text", optarg) == 0)
559 else if (strcasecmp("text", optarg) == 0)
541 binbehave = BINFILE_TEXT;
542 else
543 errx(2, "%s", getstr(8));
544 break;
545 case COLOR_OPT:
560 binbehave = BINFILE_TEXT;
561 else
562 errx(2, "%s", getstr(8));
563 break;
564 case COLOR_OPT:
546 if (optarg == NULL || strcmp("auto", optarg) == 0 ||
547 strcmp("always", optarg) == 0 ) {
548 color = getenv("GREP_COLOR");
549 if (color == NULL) {
550 color = grep_malloc(sizeof(char) * 6);
551 strcpy(color, "01;31");
552 }
553 } else if (strcmp("never", optarg) == 0)
554 color = NULL;
555 else
565 color = NULL;
566 if (optarg == NULL || strcasecmp("auto", optarg) == 0 ||
567 strcasecmp("tty", optarg) == 0 ||
568 strcasecmp("if-tty", optarg) == 0) {
569 char *term;
570
571 term = getenv("TERM");
572 if (isatty(STDOUT_FILENO) && term != NULL &&
573 strcasecmp(term, "dumb") != 0)
574 color = init_color("01;31");
575 } else if (strcasecmp("always", optarg) == 0 ||
576 strcasecmp("yes", optarg) == 0 ||
577 strcasecmp("force", optarg) == 0) {
578 color = init_color("01;31");
579 } else if (strcasecmp("never", optarg) != 0 &&
580 strcasecmp("none", optarg) != 0 &&
581 strcasecmp("no", optarg) != 0)
556 errx(2, "%s", getstr(3));
557 break;
558 case LABEL_OPT:
559 label = optarg;
560 break;
561 case LINEBUF_OPT:
562 lbflag = true;
563 break;

--- 104 unchanged lines hidden ---
582 errx(2, "%s", getstr(3));
583 break;
584 case LABEL_OPT:
585 label = optarg;
586 break;
587 case LINEBUF_OPT:
588 lbflag = true;
589 break;

--- 104 unchanged lines hidden ---