Implement -e support for grep

This commit is contained in:
sin 2013-09-27 16:26:22 +01:00
parent 82bff35228
commit f526ad099f
2 changed files with 100 additions and 38 deletions

9
grep.1
View File

@ -4,6 +4,8 @@ grep \- search files for a pattern
.SH SYNOPSIS .SH SYNOPSIS
.B grep .B grep
.RB [ \-Ecilnqv ] .RB [ \-Ecilnqv ]
.RB [ \-e
.I pattern ]
.I pattern .I pattern
.RI [ file ...] .RI [ file ...]
.SH DESCRIPTION .SH DESCRIPTION
@ -25,6 +27,13 @@ matches using extended regex.
.B \-c .B \-c
prints only a count of matching lines. prints only a count of matching lines.
.TP .TP
.B \-e pattern
Specify a pattern used during the search of the input: an input
line is selected if it matches any of the specified patterns.
This option is most useful when multiple -e options are used to
specify multiple patterns, or when a pattern begins with a dash
(`-').
.TP
.B \-i .B \-i
matches lines case insensitively. matches lines case insensitively.
.TP .TP

129
grep.c
View File

@ -10,19 +10,27 @@
enum { Match = 0, NoMatch = 1, Error = 2 }; enum { Match = 0, NoMatch = 1, Error = 2 };
static void grep(FILE *, const char *, regex_t *); static void addpattern(const char *);
static bool grep(FILE *, const char *);
static void usage(void); static void usage(void);
static bool eflag = false;
static bool vflag = false; static bool vflag = false;
static bool many; static bool many;
static bool match = false;
static char mode = 0; static char mode = 0;
static struct plist {
char *pattern;
regex_t preg;
struct plist *next;
} *phead;
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
bool match = false;
struct plist *pnode, *tmp;
int i, n, flags = REG_NOSUB; int i, n, flags = REG_NOSUB;
regex_t preg;
FILE *fp; FILE *fp;
ARGBEGIN { ARGBEGIN {
@ -30,6 +38,10 @@ main(int argc, char *argv[])
flags |= REG_EXTENDED; flags |= REG_EXTENDED;
break; break;
case 'c': case 'c':
case 'e':
addpattern(EARGF(usage()));
eflag = true;
break;
case 'l': case 'l':
case 'n': case 'n':
case 'q': case 'q':
@ -45,57 +57,97 @@ main(int argc, char *argv[])
usage(); usage();
} ARGEND; } ARGEND;
if(argc == 0) if(argc == 0 && !eflag)
usage(); /* no pattern */ usage(); /* no pattern */
if((n = regcomp(&preg, argv[0], flags)) != 0) { /* If -e is not specified treat it as if it were */
char buf[BUFSIZ]; if(!eflag) {
addpattern(argv[0]);
argc--;
argv++;
}
regerror(n, &preg, buf, sizeof buf); /* Compile regex for all search patterns */
enprintf(Error, "invalid pattern: %s\n", buf); for(pnode = phead; pnode; pnode = pnode->next) {
if((n = regcomp(&pnode->preg, pnode->pattern, flags)) != 0) {
char buf[BUFSIZ];
regerror(n, &pnode->preg, buf, sizeof buf);
enprintf(Error, "invalid pattern: %s\n", buf);
}
} }
many = (argc > 1); many = (argc > 1);
if(argc == 1) if(argc == 0) {
grep(stdin, "<stdin>", &preg); match = grep(stdin, "<stdin>");
else for(i = 1; i < argc; i++) { } else {
if(!(fp = fopen(argv[i], "r"))) for(i = 0; i < argc; i++) {
enprintf(Error, "fopen %s:", argv[i]); if(!(fp = fopen(argv[i], "r")))
grep(fp, argv[i], &preg); enprintf(Error, "fopen %s:", argv[i]);
fclose(fp); if(grep(fp, argv[i]))
match = true;
fclose(fp);
}
}
pnode = phead;
while(pnode) {
tmp = pnode->next;
regfree(&pnode->preg);
free(pnode->pattern);
free(pnode);
pnode = tmp;
} }
return match ? Match : NoMatch; return match ? Match : NoMatch;
} }
void void
grep(FILE *fp, const char *str, regex_t *preg) addpattern(const char *pattern)
{
struct plist *pnode;
pnode = malloc(sizeof(*pnode));
if(!pnode)
eprintf("malloc:");
pnode->pattern = strdup(pattern);
if(!pnode->pattern)
eprintf("strdup:");
pnode->next = phead;
phead = pnode;
}
bool
grep(FILE *fp, const char *str)
{ {
char *buf = NULL; char *buf = NULL;
long n, c = 0; long n, c = 0;
size_t size = 0, len; size_t size = 0, len;
struct plist *pnode;
bool match = false;
for(n = 1; afgets(&buf, &size, fp); n++) { for(n = 1; afgets(&buf, &size, fp); n++) {
if(buf[(len = strlen(buf))-1] == '\n') for(pnode = phead; pnode; pnode = pnode->next) {
buf[--len] = '\0'; if(buf[(len = strlen(buf))-1] == '\n')
if(regexec(preg, buf, 0, NULL, 0) ^ vflag) buf[--len] = '\0';
continue; if(regexec(&pnode->preg, buf, 0, NULL, 0) ^ vflag)
switch(mode) { continue;
case 'c': switch(mode) {
c++; case 'c':
break; c++;
case 'l': break;
puts(str); case 'l':
goto end; puts(str);
case 'q': goto end;
exit(Match); case 'q':
default: exit(Match);
if(many) default:
printf("%s:", str); if(many)
if(mode == 'n') printf("%s:", str);
printf("%ld:", n); if(mode == 'n')
printf("%s\n", buf); printf("%ld:", n);
break; printf("%s\n", buf);
break;
}
match = true;
} }
match = true;
} }
if(mode == 'c') if(mode == 'c')
printf("%ld\n", c); printf("%ld\n", c);
@ -103,10 +155,11 @@ end:
if(ferror(fp)) if(ferror(fp))
enprintf(Error, "%s: read error:", str); enprintf(Error, "%s: read error:", str);
free(buf); free(buf);
return match;
} }
void void
usage(void) usage(void)
{ {
enprintf(Error, "usage: %s [-Ecilnqv] pattern [files...]\n", argv0); enprintf(Error, "usage: %s [-Ecilnqv] [-e pattern] pattern [files...]\n", argv0);
} }