- 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)
This commit is contained in:
Gabor Kovesdan
2010-07-25 08:42:18 +00:00
parent 5343524a9e
commit 2711628675
3 changed files with 60 additions and 32 deletions
+50 -24
View File
@@ -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:
+5 -4
View File
@@ -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, Lflag,
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;
+5 -4
View File
@@ -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)