sbase/grep.c

165 lines
2.8 KiB
C
Raw Normal View History

2011-05-23 01:36:34 +00:00
/* See LICENSE file for copyright and license details. */
#include <regex.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
2011-05-24 00:13:34 +00:00
#include <unistd.h>
2011-05-25 19:40:47 +00:00
#include "text.h"
2011-06-18 05:42:24 +00:00
#include "util.h"
enum { Match = 0, NoMatch = 1, Error = 2 };
2011-05-23 01:36:34 +00:00
2013-09-27 15:26:22 +00:00
static void addpattern(const char *);
2014-05-11 22:31:07 +00:00
static bool grep(FILE *, const char *, int);
2011-05-23 01:36:34 +00:00
2013-09-27 15:26:22 +00:00
static bool eflag = false;
2011-05-23 01:36:34 +00:00
static bool vflag = false;
static bool many;
static char mode = 0;
2013-09-27 15:26:22 +00:00
static struct plist {
char *pattern;
struct plist *next;
} *phead;
static void
usage(void)
{
enprintf(Error, "usage: %s [-Ecilnqv] [-e pattern] pattern [files...]\n", argv0);
}
2011-05-23 01:36:34 +00:00
int
main(int argc, char *argv[])
{
2013-09-27 15:26:22 +00:00
bool match = false;
struct plist *pnode, *tmp;
2014-05-11 22:31:07 +00:00
int i, flags = REG_NOSUB;
2011-05-23 01:36:34 +00:00
FILE *fp;
2012-05-31 18:38:25 +00:00
ARGBEGIN {
case 'E':
flags |= REG_EXTENDED;
break;
case 'c':
2013-09-27 15:26:22 +00:00
case 'e':
addpattern(EARGF(usage()));
eflag = true;
break;
2012-05-31 18:38:25 +00:00
case 'l':
case 'n':
case 'q':
2012-06-09 17:49:02 +00:00
mode = ARGC();
2012-05-31 18:38:25 +00:00
break;
case 'i':
flags |= REG_ICASE;
break;
case 'v':
vflag = true;
break;
default:
usage();
} ARGEND;
2011-05-23 01:36:34 +00:00
2014-05-11 22:31:07 +00:00
/* no pattern */
2013-09-27 15:26:22 +00:00
if(argc == 0 && !eflag)
2014-05-11 22:31:07 +00:00
usage();
2012-05-31 18:38:25 +00:00
2013-09-27 15:26:22 +00:00
/* If -e is not specified treat it as if it were */
if(!eflag) {
addpattern(argv[0]);
argc--;
argv++;
}
2012-05-31 18:38:25 +00:00
many = (argc > 1);
2013-09-27 15:26:22 +00:00
if(argc == 0) {
2014-05-11 22:31:07 +00:00
match = grep(stdin, "<stdin>", flags);
2013-09-27 15:26:22 +00:00
} else {
for(i = 0; i < argc; i++) {
if(!(fp = fopen(argv[i], "r")))
enprintf(Error, "fopen %s:", argv[i]);
2014-05-11 22:31:07 +00:00
if(grep(fp, argv[i], flags))
2013-09-27 15:26:22 +00:00
match = true;
fclose(fp);
}
}
pnode = phead;
while(pnode) {
tmp = pnode->next;
free(pnode->pattern);
free(pnode);
pnode = tmp;
2011-05-23 01:36:34 +00:00
}
2011-06-18 05:42:24 +00:00
return match ? Match : NoMatch;
2011-05-23 01:36:34 +00:00
}
void
2013-09-27 15:26:22 +00:00
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
2014-05-11 22:31:07 +00:00
grep(FILE *fp, const char *str, int flags)
2011-05-23 01:36:34 +00:00
{
2014-05-11 23:39:56 +00:00
char err[BUFSIZ];
2011-05-25 19:40:47 +00:00
char *buf = NULL;
long n, c = 0;
2014-05-11 22:31:07 +00:00
int r;
static regex_t preg;
size_t size = 0, len;
2013-09-27 15:26:22 +00:00
struct plist *pnode;
bool match = false;
2011-05-23 01:36:34 +00:00
2011-05-25 19:40:47 +00:00
for(n = 1; afgets(&buf, &size, fp); n++) {
2013-09-27 15:26:22 +00:00
for(pnode = phead; pnode; pnode = pnode->next) {
2014-05-11 22:31:07 +00:00
if((r = regcomp(&preg, pnode->pattern, flags)) != 0) {
regerror(r, &preg, err, sizeof err);
enprintf(Error, "invalid pattern: %s\n", err);
}
2013-09-27 15:26:22 +00:00
if(buf[(len = strlen(buf))-1] == '\n')
buf[--len] = '\0';
2014-05-11 22:31:07 +00:00
if(regexec(&preg, buf, 0, NULL, 0) ^ vflag){
regfree(&preg);
2013-09-27 15:26:22 +00:00
continue;
2014-05-11 22:31:07 +00:00
}
2013-09-27 15:26:22 +00:00
switch(mode) {
case 'c':
c++;
break;
case 'l':
puts(str);
goto end;
case 'q':
exit(Match);
default:
if(many)
printf("%s:", str);
if(mode == 'n')
printf("%ld:", n);
printf("%s\n", buf);
break;
}
match = true;
2011-05-23 01:36:34 +00:00
}
}
if(mode == 'c')
2011-05-25 19:40:47 +00:00
printf("%ld\n", c);
end:
2011-06-18 05:42:24 +00:00
if(ferror(fp))
enprintf(Error, "%s: read error:", str);
2014-05-11 22:31:07 +00:00
regfree(&preg);
2011-05-25 19:40:47 +00:00
free(buf);
2013-09-27 15:26:22 +00:00
return match;
2011-05-23 01:36:34 +00:00
}