Implement -e support for grep
This commit is contained in:
parent
82bff35228
commit
f526ad099f
9
grep.1
9
grep.1
|
@ -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
|
||||||
|
|
79
grep.c
79
grep.c
|
@ -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,38 +57,77 @@ 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 */
|
||||||
|
if(!eflag) {
|
||||||
|
addpattern(argv[0]);
|
||||||
|
argc--;
|
||||||
|
argv++;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Compile regex for all search patterns */
|
||||||
|
for(pnode = phead; pnode; pnode = pnode->next) {
|
||||||
|
if((n = regcomp(&pnode->preg, pnode->pattern, flags)) != 0) {
|
||||||
char buf[BUFSIZ];
|
char buf[BUFSIZ];
|
||||||
|
|
||||||
regerror(n, &preg, buf, sizeof buf);
|
regerror(n, &pnode->preg, buf, sizeof buf);
|
||||||
enprintf(Error, "invalid pattern: %s\n", 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 {
|
||||||
|
for(i = 0; i < argc; i++) {
|
||||||
if(!(fp = fopen(argv[i], "r")))
|
if(!(fp = fopen(argv[i], "r")))
|
||||||
enprintf(Error, "fopen %s:", argv[i]);
|
enprintf(Error, "fopen %s:", argv[i]);
|
||||||
grep(fp, argv[i], &preg);
|
if(grep(fp, argv[i]))
|
||||||
|
match = true;
|
||||||
fclose(fp);
|
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++) {
|
||||||
|
for(pnode = phead; pnode; pnode = pnode->next) {
|
||||||
if(buf[(len = strlen(buf))-1] == '\n')
|
if(buf[(len = strlen(buf))-1] == '\n')
|
||||||
buf[--len] = '\0';
|
buf[--len] = '\0';
|
||||||
if(regexec(preg, buf, 0, NULL, 0) ^ vflag)
|
if(regexec(&pnode->preg, buf, 0, NULL, 0) ^ vflag)
|
||||||
continue;
|
continue;
|
||||||
switch(mode) {
|
switch(mode) {
|
||||||
case 'c':
|
case 'c':
|
||||||
|
@ -97,16 +148,18 @@ grep(FILE *fp, const char *str, regex_t *preg)
|
||||||
}
|
}
|
||||||
match = true;
|
match = true;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
if(mode == 'c')
|
if(mode == 'c')
|
||||||
printf("%ld\n", c);
|
printf("%ld\n", c);
|
||||||
end:
|
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);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user