From owner-svn-src-head@FreeBSD.ORG Sun Jul 25 08:42:19 2010 Return-Path: Delivered-To: svn-src-head@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 18046106567D; Sun, 25 Jul 2010 08:42:19 +0000 (UTC) (envelope-from gabor@FreeBSD.org) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:4f8:fff6::2c]) by mx1.freebsd.org (Postfix) with ESMTP id 289E08FC0C; Sun, 25 Jul 2010 08:42:19 +0000 (UTC) Received: from svn.freebsd.org (localhost [127.0.0.1]) by svn.freebsd.org (8.14.3/8.14.3) with ESMTP id o6P8gISU090612; Sun, 25 Jul 2010 08:42:18 GMT (envelope-from gabor@svn.freebsd.org) Received: (from gabor@localhost) by svn.freebsd.org (8.14.3/8.14.3/Submit) id o6P8gI2W090609; Sun, 25 Jul 2010 08:42:18 GMT (envelope-from gabor@svn.freebsd.org) Message-Id: <201007250842.o6P8gI2W090609@svn.freebsd.org> From: Gabor Kovesdan Date: Sun, 25 Jul 2010 08:42:18 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Cc: Subject: svn commit: r210461 - head/usr.bin/grep X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 25 Jul 2010 08:42:19 -0000 Author: gabor Date: Sun Jul 25 08:42:18 2010 New Revision: 210461 URL: http://svn.freebsd.org/changeset/base/210461 Log: - Fix --color behaviour to only output color sequences if stdout is a tty or if forced mode is specified [1] - While here, add some alternative names for the options and make then case-insensitive - Fix -q and -l behaviour [2] - Some small changes to make the code easier to review Submitted by: swell.k@gmail.com [1], dougb [2] Approved by: delphij (mentor) Modified: head/usr.bin/grep/grep.c head/usr.bin/grep/grep.h head/usr.bin/grep/util.c Modified: head/usr.bin/grep/grep.c ============================================================================== --- head/usr.bin/grep/grep.c Sun Jul 25 04:19:05 2010 (r210460) +++ head/usr.bin/grep/grep.c Sun Jul 25 08:42:18 2010 (r210461) @@ -114,13 +114,13 @@ bool lbflag; /* --line-buffered */ bool nullflag; /* --null */ bool exclflag; /* --exclude */ char *label; /* --label */ -char *color; /* --color */ +const char *color; /* --color */ int grepbehave = GREP_BASIC; /* -EFGP: type of the regex */ int binbehave = BINFILE_BIN; /* -aIU: handling of binary files */ int filebehave = FILE_STDIO; /* -JZ: normal, gzip or bzip2 file */ -int devbehave = DEV_GREP; /* -D: handling of devices */ -int dirbehave = DIR_GREP; /* -dRr: handling of directories */ -int linkbehave = LINK_GREP; /* -OpS: handling of symlinks */ +int devbehave = DEV_READ; /* -D: handling of devices */ +int dirbehave = DIR_READ; /* -dRr: handling of directories */ +int linkbehave = LINK_READ; /* -OpS: handling of symlinks */ enum { BIN_OPT = CHAR_MAX + 1, @@ -136,6 +136,8 @@ enum { R_DINCLUDE_OPT }; +static inline const char *init_color(const char *); + /* Housekeeping */ bool first = true; /* flag whether we are processing the first match */ bool prev; /* flag whether or not the previous line matched */ @@ -279,6 +281,15 @@ read_patterns(const char *fn) fclose(f); } +static inline const char * +init_color(const char *d) +{ + char *c; + + c = getenv("GREP_COLOR"); + return (c != NULL ? c : d); +} + int main(int argc, char *argv[]) { @@ -415,16 +426,24 @@ main(int argc, char *argv[]) cflag = true; break; case 'D': - if (strcmp(optarg, "skip") == 0) + if (strcasecmp(optarg, "skip") == 0) devbehave = DEV_SKIP; + else if (strcasecmp(optarg, "read") == 0) + devbehave = DEV_READ; + else { + errno = EINVAL; + err(2, NULL); + } break; case 'd': - if (strcmp("recurse", optarg) == 0) { + if (strcasecmp("recurse", optarg) == 0) { Hflag = true; dirbehave = DIR_RECURSE; - } else if (strcmp("skip", optarg) == 0) + } else if (strcasecmp("skip", optarg) == 0) dirbehave = DIR_SKIP; - else if (strcmp("read", optarg) != 0) { + else if (strcasecmp("read", optarg) == 0) + dirbehave = DIR_READ; + else { errno = EINVAL; err(2, NULL); } @@ -466,11 +485,11 @@ main(int argc, char *argv[]) break; case 'L': lflag = false; - Lflag = qflag = true; + Lflag = true; break; case 'l': Lflag = false; - lflag = qflag = true; + lflag = true; break; case 'm': mflag = true; @@ -500,7 +519,7 @@ main(int argc, char *argv[]) qflag = true; break; case 'S': - linkbehave = LINK_GREP; + linkbehave = LINK_READ; break; case 'R': case 'r': @@ -533,26 +552,33 @@ main(int argc, char *argv[]) filebehave = FILE_GZIP; break; case BIN_OPT: - if (strcmp("binary", optarg) == 0) + if (strcasecmp("binary", optarg) == 0) binbehave = BINFILE_BIN; - else if (strcmp("without-match", optarg) == 0) + else if (strcasecmp("without-match", optarg) == 0) binbehave = BINFILE_SKIP; - else if (strcmp("text", optarg) == 0) + else if (strcasecmp("text", optarg) == 0) binbehave = BINFILE_TEXT; else errx(2, "%s", getstr(8)); break; case COLOR_OPT: - if (optarg == NULL || strcmp("auto", optarg) == 0 || - strcmp("always", optarg) == 0 ) { - color = getenv("GREP_COLOR"); - if (color == NULL) { - color = grep_malloc(sizeof(char) * 6); - strcpy(color, "01;31"); - } - } else if (strcmp("never", optarg) == 0) - color = NULL; - else + color = NULL; + if (optarg == NULL || strcasecmp("auto", optarg) == 0 || + strcasecmp("tty", optarg) == 0 || + strcasecmp("if-tty", optarg) == 0) { + char *term; + + term = getenv("TERM"); + if (isatty(STDOUT_FILENO) && term != NULL && + strcasecmp(term, "dumb") != 0) + color = init_color("01;31"); + } else if (strcasecmp("always", optarg) == 0 || + strcasecmp("yes", optarg) == 0 || + strcasecmp("force", optarg) == 0) { + color = init_color("01;31"); + } else if (strcasecmp("never", optarg) != 0 && + strcasecmp("none", optarg) != 0 && + strcasecmp("no", optarg) != 0) errx(2, "%s", getstr(3)); break; case LABEL_OPT: Modified: head/usr.bin/grep/grep.h ============================================================================== --- head/usr.bin/grep/grep.h Sun Jul 25 04:19:05 2010 (r210460) +++ head/usr.bin/grep/grep.h Sun Jul 25 08:42:18 2010 (r210461) @@ -60,14 +60,14 @@ extern const char *errstr[]; #define FILE_GZIP 1 #define FILE_BZIP 2 -#define DIR_GREP 0 +#define DIR_READ 0 #define DIR_SKIP 1 #define DIR_RECURSE 2 -#define DEV_GREP 0 +#define DEV_READ 0 #define DEV_SKIP 1 -#define LINK_GREP 0 +#define LINK_READ 0 #define LINK_EXPLICIT 1 #define LINK_SKIP 2 @@ -120,7 +120,8 @@ extern bool Eflag, Fflag, Gflag, Hflag, qflag, sflag, vflag, wflag, xflag; extern bool exclflag, nullflag; extern unsigned long long Aflag, Bflag, mcount; -extern char *color, *label; +extern char *label; +extern const char *color; extern int binbehave, devbehave, dirbehave, filebehave, grepbehave, linkbehave; extern bool first, matchall, notfound, prev; Modified: head/usr.bin/grep/util.c ============================================================================== --- head/usr.bin/grep/util.c Sun Jul 25 04:19:05 2010 (r210460) +++ head/usr.bin/grep/util.c Sun Jul 25 08:42:18 2010 (r210461) @@ -226,9 +226,9 @@ procfile(const char *fn) printf("%s:", ln.file); printf("%u\n", c); } - if (lflag && c != 0) + if (lflag && !qflag && c != 0) printf("%s\n", fn); - if (Lflag && c == 0) + if (Lflag && !qflag && c == 0) printf("%s\n", fn); if (c && !cflag && !lflag && !Lflag && binbehave == BINFILE_BIN && f->binary && !qflag) @@ -320,7 +320,8 @@ procline(struct str *l, int nottext) if (m < MAX_LINE_MATCHES) matches[m++] = pmatch; /* matches - skip further patterns */ - break; + if ((color != NULL && !oflag) || qflag || lflag) + break; } } @@ -329,7 +330,7 @@ procline(struct str *l, int nottext) break; } /* One pass if we are not recording matches */ - if (!oflag && !color) + if ((color != NULL && !oflag) || qflag || lflag) break; if (st == (size_t)pmatch.rm_so)