summaryrefslogtreecommitdiff
path: root/usr/src/cmd
diff options
context:
space:
mode:
authorDamian Bogel <damian.bogel@gmail.com>2013-05-14 15:08:53 +0200
committerRichard Lowe <richlowe@richlowe.net>2013-09-13 15:16:32 -0400
commit41599e9fdccb44cc5f17828ab04b3147cefcc4e0 (patch)
treed9436585e9226721f83c7877855092e2230e17ad /usr/src/cmd
parentea76c26cbb46a2abd6ae5d4bdfaa26e32a405633 (diff)
downloadillumos-joyent-41599e9fdccb44cc5f17828ab04b3147cefcc4e0.tar.gz
3737 grep does not support -H option
3759 egrep(1) and fgrep(1) -s flag does not hide -c output Reviewed by: Albert Lee <trisk@nexenta.com> Reviewed by: Andy Stormont <andyjstormont@gmail.com> Approved by: Richard Lowe <richlowe@richlowe.net>
Diffstat (limited to 'usr/src/cmd')
-rw-r--r--usr/src/cmd/egrep/egrep.y58
-rw-r--r--usr/src/cmd/fgrep/fgrep.c81
-rw-r--r--usr/src/cmd/grep/grep.c35
-rw-r--r--usr/src/cmd/grep_xpg4/grep.c47
4 files changed, 139 insertions, 82 deletions
diff --git a/usr/src/cmd/egrep/egrep.y b/usr/src/cmd/egrep/egrep.y
index 95e071ebb9..8068dc447e 100644
--- a/usr/src/cmd/egrep/egrep.y
+++ b/usr/src/cmd/egrep/egrep.y
@@ -32,9 +32,9 @@
/* Copyright (c) 1987, 1988 Microsoft Corporation */
/* All Rights Reserved */
-%{
-#pragma ident "%Z%%M% %I% %E% SMI"
-%}
+/*
+ * Copyright 2013 Damian Bogel. All rights reserved.
+ */
/*
* egrep -- print lines containing (or not containing) a regular expression
@@ -61,6 +61,8 @@
#include <limits.h>
#include <locale.h>
+#define STDIN_FILENAME gettext("(standard input)")
+
#define BLKSIZE 512 /* size of reported disk blocks */
#define EBUFSIZ 8192
#define MAXLIN 350
@@ -110,11 +112,12 @@ int bflag;
int cflag;
int eflag;
int fflag;
+int Hflag;
int hflag;
int iflag;
int lflag;
int nflag;
-int sflag;
+int qflag;
int vflag;
int nfile;
long long blkno;
@@ -644,7 +647,7 @@ follow(int v)
}
}
-#define USAGE "[ -bchilnsv ] [ -e exp ] [ -f file ] [ strings ] [ file ] ..."
+#define USAGE "[ -bchHilnsqv ] [ -e exp ] [ -f file ] [ strings ] [ file ] ..."
int
main(int argc, char **argv)
@@ -660,7 +663,7 @@ main(int argc, char **argv)
#endif
(void) textdomain(TEXT_DOMAIN);
- while((c = getopt(argc, argv, "ybcie:f:hlnvs")) != -1)
+ while((c = getopt(argc, argv, "ybcie:f:Hhlnvsq")) != -1)
switch(c) {
case 'b':
@@ -686,8 +689,15 @@ main(int argc, char **argv)
}
continue;
+ case 'H':
+ if (!lflag) /* H is excluded by l as in GNU grep */
+ Hflag++;
+ hflag = 0; /* H excludes h */
+ continue;
+
case 'h':
hflag++;
+ Hflag = 0; /* h excludes H */
continue;
case 'y':
@@ -697,14 +707,16 @@ main(int argc, char **argv)
case 'l':
lflag++;
+ Hflag = 0; /* l excludes H */
continue;
case 'n':
nflag++;
continue;
- case 's':
- sflag++;
+ case 'q':
+ case 's': /* Solaris: legacy option */
+ qflag++;
continue;
case 'v':
@@ -799,16 +811,16 @@ execute(char *file)
return;
}
} else {
- file = "<stdin>";
f = stdin;
+ file = STDIN_FILENAME;
}
lnum = 1;
tln = 0;
if((count = read(fileno(f), buf, EBUFSIZ)) <= 0) {
fclose(f);
- if (cflag) {
- if (nfile>1 && !hflag)
+ if (cflag && !qflag) {
+ if (Hflag || (nfile > 1 && !hflag))
fprintf(stdout, "%s:", file);
fprintf(stdout, "%lld\n", tln);
}
@@ -902,19 +914,19 @@ execute(char *file)
break;
}
}
- if(succ) {
+ if (succ) {
nsucc = 1;
- if (cflag) tln++;
- else if (sflag)
- ; /* ugh */
- else if (lflag) {
- printf("%s\n", file);
+ if (lflag || qflag) {
+ if (!qflag)
+ (void) printf("%s\n", file);
fclose(f);
return;
}
- else {
- if (nfile > 1 && !hflag)
- printf(gettext("%s:"), file);
+ if (cflag) {
+ tln++;
+ } else {
+ if (Hflag || (nfile > 1 && !hflag))
+ printf("%s:", file);
if (bflag) {
nchars = blkno - (buf + count - ptrend) - 2;
if(nlflag)
@@ -944,9 +956,9 @@ execute(char *file)
clearg();
}
fclose(f);
- if (cflag) {
- if (nfile > 1 && !hflag)
- printf(gettext("%s:"), file);
+ if (cflag && !qflag) {
+ if (Hflag || (nfile > 1 && !hflag))
+ printf("%s:", file);
printf("%lld\n", tln);
}
}
diff --git a/usr/src/cmd/fgrep/fgrep.c b/usr/src/cmd/fgrep/fgrep.c
index 20be258cfa..31724cda40 100644
--- a/usr/src/cmd/fgrep/fgrep.c
+++ b/usr/src/cmd/fgrep/fgrep.c
@@ -30,7 +30,9 @@
/* Copyright (c) 1987, 1988 Microsoft Corporation */
/* All Rights Reserved */
-#pragma ident "%Z%%M% %I% %E% SMI"
+/*
+ * Copyright 2013 Damian Bogel. All rights reserved.
+ */
/*
* fgrep -- print all lines containing any of a set of keywords
@@ -107,6 +109,7 @@ wchar_t letter();
(a == b || iflag && (!MULTI_BYTE || ISASCII(a)) && (a ^ b) == ' ' && \
letter(a) == letter(b))
+#define STDIN_FILENAME gettext("(standard input)")
#define QSIZE 400
struct words {
@@ -119,8 +122,8 @@ struct words {
FILE *fptr;
long long lnum;
-int bflag, cflag, lflag, fflag, nflag, vflag, xflag, eflag, sflag;
-int hflag, iflag;
+int bflag, cflag, lflag, fflag, nflag, vflag, xflag, eflag, qflag;
+int Hflag, hflag, iflag;
int retcode = 0;
int nfile;
long long blkno;
@@ -150,14 +153,20 @@ main(int argc, char **argv)
#endif
(void) textdomain(TEXT_DOMAIN);
- while ((c = getopt(argc, argv, "hybcie:f:lnvxs")) != EOF)
+ while ((c = getopt(argc, argv, "Hhybcie:f:lnvxqs")) != EOF)
switch (c) {
- case 's':
- sflag++;
+ case 'q':
+ case 's': /* Solaris: legacy option */
+ qflag++;
+ continue;
+ case 'H':
+ Hflag++;
+ hflag = 0;
continue;
case 'h':
hflag++;
+ Hflag = 0;
continue;
case 'b':
bflag++;
@@ -183,17 +192,17 @@ main(int argc, char **argv)
wordf = fopen(optarg, "r");
if (wordf == NULL) {
(void) fprintf(stderr,
- gettext("fgrep: can't open %s\n"),
- optarg);
+ gettext("fgrep: can't open %s\n"),
+ optarg);
exit(2);
}
if (fstat(fileno(wordf), &file_stat) == 0) {
- input_size = file_stat.st_size;
+ input_size = file_stat.st_size;
} else {
(void) fprintf(stderr,
- gettext("fgrep: can't fstat %s\n"),
- optarg);
+ gettext("fgrep: can't fstat %s\n"),
+ optarg);
exit(2);
}
@@ -221,8 +230,8 @@ main(int argc, char **argv)
argc -= optind;
if (errflg || ((argc <= 0) && !fflag && !eflag)) {
- (void) printf(gettext("usage: fgrep [ -bchilnsvx ] "
- "[ -e exp ] [ -f file ] [ strings ] [ file ] ...\n"));
+ (void) printf(gettext("usage: fgrep [ -bcHhilnqsvx ] "
+ "[ -e exp ] [ -f file ] [ strings ] [ file ] ...\n"));
exit(2);
}
if (!eflag && !fflag) {
@@ -250,14 +259,14 @@ main(int argc, char **argv)
w = (struct words *)calloc(input_size, sizeof (struct words));
if (w == NULL) {
(void) fprintf(stderr,
- gettext("fgrep: could not allocate "
- "memory for wordlist\n"));
+ gettext("fgrep: could not allocate "
+ "memory for wordlist\n"));
exit(2);
}
getwidth(&WW);
if ((WIDTH1 == 0) && (WIDTH2 == 0) &&
- (WIDTH3 == 0)) {
+ (WIDTH3 == 0)) {
/*
* If non EUC-based locale,
* assume WIDTH1 is 1.
@@ -308,13 +317,13 @@ execute(char *file)
if (file) {
if ((fptr = fopen(file, "r")) == NULL) {
(void) fprintf(stderr,
- gettext("fgrep: can't open %s\n"), file);
+ gettext("fgrep: can't open %s\n"), file);
retcode = 2;
return;
}
} else {
- file = "<stdin>";
fptr = stdin;
+ file = STDIN_FILENAME;
}
ccount = 0;
failed = 0;
@@ -333,7 +342,7 @@ execute(char *file)
/* increase the buffer size */
fw_lBufsiz += BUFSIZ;
if ((buf = realloc(buf,
- fw_lBufsiz + BUFSIZ)) == NULL) {
+ fw_lBufsiz + BUFSIZ)) == NULL) {
exit(2); /* out of memory */
}
nlp = buf;
@@ -341,8 +350,8 @@ execute(char *file)
} else {
/* shift the buffer down */
(void) memmove(buf, nlp,
- &buf[fw_lBufsiz + BUFSIZ]
- - nlp);
+ &buf[fw_lBufsiz + BUFSIZ]
+ - nlp);
p -= nlp - buf;
nlp = buf;
}
@@ -350,11 +359,11 @@ execute(char *file)
}
if (p > &buf[fw_lBufsiz]) {
if ((ccount = fread(p, sizeof (char),
- &buf[fw_lBufsiz + BUFSIZ] - p, fptr))
- <= 0)
+ &buf[fw_lBufsiz + BUFSIZ] - p, fptr))
+ <= 0)
break;
} else if ((ccount = fread(p, sizeof (char),
- BUFSIZ, fptr)) <= 0)
+ BUFSIZ, fptr)) <= 0)
break;
blkno += (long long)ccount;
}
@@ -417,14 +426,16 @@ if (p > &buf[fw_lBufsiz]) {
goto nomatch;
succeed:
nsucc = 1;
- if (cflag)
- tln++;
- else if (lflag && !sflag) {
- (void) printf("%s\n", file);
+ if (lflag || qflag) {
+ if (!qflag)
+ (void) printf("%s\n", file);
(void) fclose(fptr);
return;
- } else if (!sflag) {
- if (nfile > 1 && !hflag)
+ }
+ if (cflag) {
+ tln++;
+ } else {
+ if (Hflag || (nfile > 1 && !hflag))
(void) printf("%s:", file);
if (bflag)
(void) printf("%lld:",
@@ -458,8 +469,8 @@ nomatch:
}
}
(void) fclose(fptr);
- if (cflag) {
- if ((nfile > 1) && !hflag)
+ if (cflag && !qflag) {
+ if (Hflag || (nfile > 1 && !hflag))
(void) printf("%s:", file);
(void) printf("%lld\n", tln);
}
@@ -481,7 +492,7 @@ getargc(void)
if ((b = getc(wordf)) == EOF)
return (EOF);
cw = ISASCII(c = (wchar_t)b) ? 1 :
- (ISSET2(c) ? WIDTH2 : (ISSET3(c) ? WIDTH3 : WIDTH1));
+ (ISSET2(c) ? WIDTH2 : (ISSET3(c) ? WIDTH3 : WIDTH1));
while (--cw) {
if ((b = getc(wordf)) == EOF)
return (EOF);
@@ -495,7 +506,7 @@ getargc(void)
{
cw = ISASCII(c = (unsigned char)*argptr++) ? 1 :
- (ISSET2(c) ? WIDTH2 : (ISSET3(c) ? WIDTH3 : WIDTH1));
+ (ISSET2(c) ? WIDTH2 : (ISSET3(c) ? WIDTH3 : WIDTH1));
while (--cw)
c = (c << 7) | ((*argptr++) & 0177);
@@ -614,7 +625,7 @@ cfail(void)
struct words *s;
s = w;
if ((queue = (struct words **)calloc(qsize, sizeof (struct words *)))
- == NULL) {
+ == NULL) {
perror("fgrep");
exit(2);
}
diff --git a/usr/src/cmd/grep/grep.c b/usr/src/cmd/grep/grep.c
index edc83be3e6..44c91f8271 100644
--- a/usr/src/cmd/grep/grep.c
+++ b/usr/src/cmd/grep/grep.c
@@ -33,6 +33,10 @@
/* Copyright 2012 Nexenta Systems, Inc. All rights reserved. */
/*
+ * Copyright 2013 Damian Bogel. All rights reserved.
+ */
+
+/*
* grep -- print lines matching (or not matching) a pattern
*
* status returns:
@@ -73,6 +77,8 @@ static const char *errstr[] = {
NULL
};
+#define STDIN_FILENAME gettext("(standard input)")
+
#define errmsg(msg, arg) (void) fprintf(stderr, gettext(msg), arg)
#define BLKSIZE 512
#define GBUFSIZ 8192
@@ -94,6 +100,7 @@ static int sflag;
static int iflag;
static int wflag;
static int hflag;
+static int Hflag;
static int qflag;
static int errflg;
static int nfile;
@@ -123,10 +130,17 @@ main(int argc, char **argv)
#endif
(void) textdomain(TEXT_DOMAIN);
- while ((c = getopt(argc, argv, "hqblcnRrsviyw")) != -1)
+ while ((c = getopt(argc, argv, "hHqblcnRrsviyw")) != -1)
switch (c) {
+ /* based on options order h or H is set as in GNU grep */
case 'h':
hflag++;
+ Hflag = 0; /* h excludes H */
+ break;
+ case 'H':
+ if (!lflag) /* H is excluded by l */
+ Hflag++;
+ hflag = 0; /* H excludes h */
break;
case 'q': /* POSIX: quiet: status only */
qflag++;
@@ -154,6 +168,7 @@ main(int argc, char **argv)
break;
case 'l':
lflag++;
+ Hflag = 0; /* l excludes H */
break;
case 'y':
case 'i':
@@ -167,7 +182,7 @@ main(int argc, char **argv)
}
if (errflg || (optind >= argc)) {
- errmsg("Usage: grep [-c|-l|-q] [-r|-R] -hbnsviw "
+ errmsg("Usage: grep [-c|-l|-q] [-r|-R] -hHbnsviw "
"pattern file . . .\n",
(char *)NULL);
exit(2);
@@ -299,9 +314,10 @@ execute(const char *file, int base)
}
}
- if (file == NULL)
+ if (file == NULL) {
temp = 0;
- else if ((temp = open(file + base, O_RDONLY)) == -1) {
+ file = STDIN_FILENAME;
+ } else if ((temp = open(file + base, O_RDONLY)) == -1) {
if (!sflag)
errmsg("grep: can't open %s\n", file);
nsucc = 2;
@@ -313,7 +329,7 @@ execute(const char *file, int base)
(void) close(temp);
if (cflag && !qflag) {
- if (nfile > 1 && !hflag && file)
+ if (Hflag || (nfile > 1 && !hflag))
(void) fprintf(stdout, "%s:", file);
if (!rflag)
(void) fprintf(stdout, "%lld\n", tln);
@@ -410,8 +426,8 @@ execute(const char *file, int base)
(void) close(temp);
if (cflag && !qflag) {
- if (!hflag && file && (nfile > 1 ||
- (rflag && outfn)))
+ if (Hflag || (!hflag && ((nfile > 1) ||
+ (rflag && outfn))))
(void) fprintf(stdout, "%s:", file);
(void) fprintf(stdout, "%lld\n", tln);
}
@@ -423,9 +439,6 @@ succeed(const char *f)
int nchars;
nsucc = (nsucc == 2) ? 2 : 1;
- if (f == NULL)
- f = "<stdin>";
-
if (qflag) {
/* no need to continue */
return (1);
@@ -441,7 +454,7 @@ succeed(const char *f)
return (1);
}
- if (!hflag && (nfile > 1 || (rflag && outfn))) {
+ if (Hflag || (!hflag && (nfile > 1 || (rflag && outfn)))) {
/* print filename */
(void) fprintf(stdout, "%s:", f);
}
diff --git a/usr/src/cmd/grep_xpg4/grep.c b/usr/src/cmd/grep_xpg4/grep.c
index 8bfd8d7ef9..647d91f760 100644
--- a/usr/src/cmd/grep_xpg4/grep.c
+++ b/usr/src/cmd/grep_xpg4/grep.c
@@ -36,6 +36,10 @@
/* Copyright 2012 Nexenta Systems, Inc. All rights reserved. */
+/*
+ * Copyright 2013 Damian Bogel. All rights reserved.
+ */
+
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
@@ -54,6 +58,8 @@
#include <ftw.h>
#include <sys/param.h>
+#define STDIN_FILENAME gettext("(standard input)")
+
#define BSIZE 512 /* Size of block for -b */
#define BUFSIZE 8192 /* Input buffer size */
#define MAX_DEPTH 1000 /* how deep to recurse */
@@ -80,6 +86,7 @@ static uchar_t egrep = 0; /* Invoked as egrep */
static uchar_t nvflag = 1; /* Print matching lines */
static uchar_t cflag; /* Count of matches */
static uchar_t iflag; /* Case insensitve matching */
+static uchar_t Hflag; /* Precede lines by file name */
static uchar_t hflag; /* Supress printing of filename */
static uchar_t lflag; /* Print file names of matches */
static uchar_t nflag; /* Precede lines by line number */
@@ -155,7 +162,7 @@ main(int argc, char **argv)
}
}
- while ((c = getopt(argc, argv, "vwchilnrbse:f:qxEFIR")) != EOF) {
+ while ((c = getopt(argc, argv, "vwchHilnrbse:f:qxEFIR")) != EOF) {
switch (c) {
case 'v': /* POSIX: negate matches */
nvflag = 0;
@@ -216,8 +223,16 @@ main(int argc, char **argv)
}
*(file_list + n_file - 1) = optarg;
break;
+
+ /* based on options order h or H is set as in GNU grep */
case 'h': /* Solaris: supress printing of file name */
hflag = 1;
+ Hflag = 0;
+ break;
+ /* Solaris: precede every matching with file name */
+ case 'H':
+ Hflag = 1;
+ hflag = 0;
break;
case 'q': /* POSIX: quiet: status only */
@@ -294,6 +309,12 @@ main(int argc, char **argv)
usage();
/*
+ * -l overrides -H like in GNU grep
+ */
+ if (lflag)
+ Hflag = 0;
+
+ /*
* -c, -l and -q flags are mutually exclusive
* We have -c override -l like in Solaris.
* -q overrides -l & -c programmatically in grep() function.
@@ -346,9 +367,9 @@ main(int argc, char **argv)
/* Process all files: stdin, or rest of arg list */
if (argc < 2) {
- matched = grep(0, gettext("(standard input)"));
+ matched = grep(0, STDIN_FILENAME);
} else {
- if (argc > 2 && hflag == 0)
+ if (Hflag || (argc > 2 && hflag == 0))
outfn = 1; /* Print filename on match line */
for (argv++; *argv != NULL; argv++) {
process_path(*argv);
@@ -1112,7 +1133,7 @@ L_next_line:
break;
}
if (!cflag) {
- if (outfn) {
+ if (Hflag || outfn) {
(void) printf("%s:", fn);
}
if (bflag) {
@@ -1139,7 +1160,7 @@ L_skip_line:
}
if (cflag) {
- if (outfn) {
+ if (Hflag || outfn) {
(void) printf("%s:", fn);
}
if (!qflag) {
@@ -1158,45 +1179,45 @@ usage(void)
if (egrep || fgrep) {
(void) fprintf(stderr, gettext("Usage:\t%s"), cmdname);
(void) fprintf(stderr,
- gettext(" [-c|-l|-q] [-r|-R] [-bhinsvx] "
+ gettext(" [-c|-l|-q] [-r|-R] [-bhHinsvx] "
"pattern_list [file ...]\n"));
(void) fprintf(stderr, "\t%s", cmdname);
(void) fprintf(stderr,
- gettext(" [-c|-l|-q] [-r|-R] [-bhinsvx] "
+ gettext(" [-c|-l|-q] [-r|-R] [-bhHinsvx] "
"[-e pattern_list]... "
"[-f pattern_file]... [file...]\n"));
} else {
(void) fprintf(stderr, gettext("Usage:\t%s"), cmdname);
(void) fprintf(stderr,
- gettext(" [-c|-l|-q] [-r|-R] [-bhinsvwx] "
+ gettext(" [-c|-l|-q] [-r|-R] [-bhHinsvwx] "
"pattern_list [file ...]\n"));
(void) fprintf(stderr, "\t%s", cmdname);
(void) fprintf(stderr,
- gettext(" [-c|-l|-q] [-r|-R] [-bhinsvwx] "
+ gettext(" [-c|-l|-q] [-r|-R] [-bhHinsvwx] "
"[-e pattern_list]... "
"[-f pattern_file]... [file...]\n"));
(void) fprintf(stderr, "\t%s", cmdname);
(void) fprintf(stderr,
- gettext(" -E [-c|-l|-q] [-r|-R] [-bhinsvx] "
+ gettext(" -E [-c|-l|-q] [-r|-R] [-bhHinsvx] "
"pattern_list [file ...]\n"));
(void) fprintf(stderr, "\t%s", cmdname);
(void) fprintf(stderr,
- gettext(" -E [-c|-l|-q] [-r|-R] [-bhinsvx] "
+ gettext(" -E [-c|-l|-q] [-r|-R] [-bhHinsvx] "
"[-e pattern_list]... "
"[-f pattern_file]... [file...]\n"));
(void) fprintf(stderr, "\t%s", cmdname);
(void) fprintf(stderr,
- gettext(" -F [-c|-l|-q] [-r|-R] [-bhinsvx] "
+ gettext(" -F [-c|-l|-q] [-r|-R] [-bhHinsvx] "
"pattern_list [file ...]\n"));
(void) fprintf(stderr, "\t%s", cmdname);
(void) fprintf(stderr,
- gettext(" -F [-c|-l|-q] [-bhinsvx] [-e pattern_list]... "
+ gettext(" -F [-c|-l|-q] [-bhHinsvx] [-e pattern_list]... "
"[-f pattern_file]... [file...]\n"));
}
exit(2);