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 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 */ |
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 */ |
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 |
139static inline const char *init_color(const char *); 140 |
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 |
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': |
429 if (strcasecmp(optarg, "skip") == 0) |
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 } |
437 break; 438 case 'd': |
439 if (strcasecmp("recurse", optarg) == 0) { |
440 Hflag = true; 441 dirbehave = DIR_RECURSE; |
442 } else if (strcasecmp("skip", optarg) == 0) |
443 dirbehave = DIR_SKIP; |
444 else if (strcasecmp("read", optarg) == 0) 445 dirbehave = DIR_READ; 446 else { |
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; |
488 Lflag = true; |
489 break; 490 case 'l': 491 Lflag = false; |
492 lflag = true; |
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': |
522 linkbehave = LINK_READ; |
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: |
555 if (strcasecmp("binary", optarg) == 0) |
556 binbehave = BINFILE_BIN; |
557 else if (strcasecmp("without-match", optarg) == 0) |
558 binbehave = BINFILE_SKIP; |
559 else if (strcasecmp("text", optarg) == 0) |
560 binbehave = BINFILE_TEXT; 561 else 562 errx(2, "%s", getstr(8)); 563 break; 564 case COLOR_OPT: |
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) |
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 --- |