sbase/grep.c

271 lines
5.0 KiB
C
Raw Normal View History

2011-05-22 21:36:34 -04:00
/* See LICENSE file for copyright and license details. */
#include <regex.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
2014-11-16 07:37:43 -05:00
#include "queue.h"
2011-06-18 01:42:24 -04:00
#include "util.h"
enum { Match = 0, NoMatch = 1, Error = 2 };
2011-05-22 21:36:34 -04:00
2013-09-27 11:26:22 -04:00
static void addpattern(const char *);
static void addpatternfile(FILE *);
static int grep(FILE *, const char *);
2011-05-22 21:36:34 -04:00
static int Eflag;
2014-11-20 09:35:23 -05:00
static int Fflag;
2014-11-20 09:14:26 -05:00
static int Hflag;
static int eflag;
2014-11-20 11:57:49 -05:00
static int fflag;
2014-11-20 09:14:26 -05:00
static int hflag;
2014-11-20 12:38:31 -05:00
static int iflag;
2014-11-20 09:14:26 -05:00
static int sflag;
static int vflag;
static int wflag;
2014-11-20 09:47:26 -05:00
static int xflag;
static int many;
2014-11-20 12:26:47 -05:00
static int mode;
2011-05-22 21:36:34 -04:00
2014-11-16 07:37:43 -05:00
struct pattern {
2013-09-27 11:26:22 -04:00
char *pattern;
regex_t preg;
SLIST_ENTRY(pattern) entry;
2014-11-16 07:37:43 -05:00
};
static SLIST_HEAD(phead, pattern) phead;
2013-09-27 11:26:22 -04:00
static void
usage(void)
{
enprintf(Error, "usage: %s [-EFHchilnqsvwx] [-e pattern] [-f file] [pattern] [file ...]\n", argv0);
}
2011-05-22 21:36:34 -04:00
int
main(int argc, char *argv[])
{
struct pattern *pnode;
2014-11-16 09:17:46 -05:00
int i, m, flags = REG_NOSUB, match = NoMatch;
2011-05-22 21:36:34 -04:00
FILE *fp;
char *arg;
2011-05-22 21:36:34 -04:00
SLIST_INIT(&phead);
2014-11-16 07:37:43 -05:00
2012-05-31 14:38:25 -04:00
ARGBEGIN {
case 'E':
Eflag = 1;
2012-05-31 14:38:25 -04:00
flags |= REG_EXTENDED;
break;
2014-11-20 09:35:23 -05:00
case 'F':
Fflag = 1;
break;
2014-11-16 05:45:10 -05:00
case 'H':
Hflag = 1;
hflag = 0;
2014-11-16 05:45:10 -05:00
break;
2013-09-27 11:26:22 -04:00
case 'e':
arg = EARGF(usage());
fp = fmemopen(arg, strlen(arg) + 1, "r");
addpatternfile(fp);
fclose(fp);
eflag = 1;
2013-09-27 11:26:22 -04:00
break;
2014-11-20 11:57:49 -05:00
case 'f':
arg = EARGF(usage());
fp = fopen(arg, "r");
if (!fp)
enprintf(Error, "fopen %s:", arg);
addpatternfile(fp);
fclose(fp);
2014-11-20 11:57:49 -05:00
fflag = 1;
break;
2014-11-16 14:03:25 -05:00
case 'h':
hflag = 1;
Hflag = 0;
2014-11-16 14:03:25 -05:00
break;
2014-11-01 23:08:13 -04:00
case 'c':
2012-05-31 14:38:25 -04:00
case 'l':
case 'n':
case 'q':
2012-06-09 13:49:02 -04:00
mode = ARGC();
2012-05-31 14:38:25 -04:00
break;
case 'i':
flags |= REG_ICASE;
2014-11-20 12:38:31 -05:00
iflag = 1;
2012-05-31 14:38:25 -04:00
break;
2014-11-20 09:14:26 -05:00
case 's':
sflag = 1;
break;
2012-05-31 14:38:25 -04:00
case 'v':
vflag = 1;
2012-05-31 14:38:25 -04:00
break;
case 'w':
wflag = 1;
break;
2014-11-20 09:47:26 -05:00
case 'x':
xflag = 1;
break;
2012-05-31 14:38:25 -04:00
default:
usage();
} ARGEND;
2011-05-22 21:36:34 -04:00
2014-11-20 11:57:49 -05:00
if (argc == 0 && !eflag && !fflag)
usage(); /* no pattern */
2012-05-31 14:38:25 -04:00
2014-11-20 11:57:49 -05:00
/* just add literal pattern to list */
if (!eflag && !fflag) {
fp = fmemopen(argv[0], strlen(argv[0]) + 1, "r");
addpatternfile(fp);
fclose(fp);
2013-09-27 11:26:22 -04:00
argc--;
argv++;
}
2014-11-20 09:35:23 -05:00
if (!Fflag)
/* Compile regex for all search patterns */
SLIST_FOREACH(pnode, &phead, entry)
enregcomp(Error, &pnode->preg, pnode->pattern, flags);
2012-05-31 14:38:25 -04:00
many = (argc > 1);
if (argc == 0) {
match = grep(stdin, "<stdin>");
2013-09-27 11:26:22 -04:00
} else {
for (i = 0; i < argc; i++) {
if (!(fp = fopen(argv[i], "r"))) {
2014-11-20 09:14:26 -05:00
if (!sflag)
weprintf("fopen %s:", argv[i]);
match = Error;
continue;
}
m = grep(fp, argv[i]);
if (m == Error || (match != Error && m == Match))
match = m;
2013-09-27 11:26:22 -04:00
fclose(fp);
}
}
return match;
2011-05-22 21:36:34 -04:00
}
static void
2013-09-27 11:26:22 -04:00
addpattern(const char *pattern)
{
2014-11-16 07:37:43 -05:00
struct pattern *pnode;
2014-11-20 09:47:26 -05:00
char *tmp;
int bol, eol;
size_t len;
2013-09-27 11:26:22 -04:00
/* a null BRE/ERE matches every line */
if (!Fflag)
if (pattern[0] == '\0')
pattern = ".";
2014-11-20 09:47:26 -05:00
if (!Fflag && xflag) {
2015-02-10 20:08:17 -05:00
tmp = enmalloc(Error, strlen(pattern) + 3);
2014-11-20 09:47:26 -05:00
snprintf(tmp, strlen(pattern) + 3, "%s%s%s",
pattern[0] == '^' ? "" : "^",
pattern,
pattern[strlen(pattern) - 1] == '$' ? "" : "$");
} else if (!Fflag && wflag) {
len = strlen(pattern) + 5 + (Eflag ? 2 : 4);
2015-02-10 20:08:17 -05:00
tmp = enmalloc(Error, len);
bol = eol = 0;
if (pattern[0] == '^')
bol = 1;
if (pattern[strlen(pattern) - 1] == '$')
eol = 1;
snprintf(tmp, len, "%s\\<%s%.*s%s\\>%s",
bol ? "^" : "",
Eflag ? "(" : "\\(",
(int)strlen(pattern) - bol - eol, pattern + bol,
Eflag ? ")" : "\\)",
eol ? "$" : "");
2014-11-20 09:47:26 -05:00
} else {
2015-02-10 20:08:17 -05:00
tmp = enstrdup(Error, pattern);
}
2015-02-10 20:08:17 -05:00
pnode = enmalloc(Error, sizeof(*pnode));
pnode->pattern = tmp;
SLIST_INSERT_HEAD(&phead, pnode, entry);
2013-09-27 11:26:22 -04:00
}
2014-11-20 11:57:49 -05:00
static void
addpatternfile(FILE *fp)
2014-11-20 11:57:49 -05:00
{
2014-12-16 15:20:41 -05:00
static char *buf = NULL;
static size_t size = 0;
2015-01-31 09:19:42 -05:00
ssize_t len = 0;
2014-11-20 11:57:49 -05:00
while ((len = getline(&buf, &size, fp)) != -1) {
2015-01-31 09:19:42 -05:00
if (len > 0 && buf[len - 1] == '\n')
2014-11-20 11:57:49 -05:00
buf[len - 1] = '\0';
addpattern(buf);
}
2014-11-21 06:43:53 -05:00
if (ferror(fp))
enprintf(Error, "read error:");
2014-11-20 11:57:49 -05:00
}
static int
grep(FILE *fp, const char *str)
2011-05-22 21:36:34 -04:00
{
2014-12-16 15:20:41 -05:00
static char *buf = NULL;
static size_t size = 0;
2015-01-31 09:19:42 -05:00
ssize_t len = 0;
long c = 0, n;
2014-11-16 07:37:43 -05:00
struct pattern *pnode;
int match = NoMatch;
2011-05-22 21:36:34 -04:00
2014-11-18 15:49:30 -05:00
for (n = 1; (len = getline(&buf, &size, fp)) != -1; n++) {
/* Remove the trailing newline if one is present. */
if (len && buf[len - 1] == '\n')
buf[len - 1] = '\0';
SLIST_FOREACH(pnode, &phead, entry) {
2014-11-20 09:35:23 -05:00
if (!Fflag) {
if (regexec(&pnode->preg, buf[0] == '\0' ? "\n" : buf, 0, NULL, 0) ^ vflag)
2014-11-20 09:35:23 -05:00
continue;
} else {
2014-11-20 12:38:31 -05:00
if (!xflag) {
if ((iflag ? strcasestr : strstr)(buf, pnode->pattern))
match = Match;
else
match = NoMatch;
} else {
if (!(iflag ? strcasecmp : strcmp)(buf, pnode->pattern))
match = Match;
else
match = NoMatch;
}
if (match ^ vflag)
2014-11-20 09:35:23 -05:00
continue;
}
switch (mode) {
2013-09-27 11:26:22 -04:00
case 'c':
c++;
break;
case 'l':
puts(str);
goto end;
case 'q':
exit(Match);
default:
2014-11-16 14:03:25 -05:00
if (!hflag && (many || Hflag))
2013-09-27 11:26:22 -04:00
printf("%s:", str);
if (mode == 'n')
2013-09-27 11:26:22 -04:00
printf("%ld:", n);
puts(buf);
2013-09-27 11:26:22 -04:00
break;
}
match = Match;
2014-11-20 12:02:48 -05:00
break;
2011-05-22 21:36:34 -04:00
}
}
if (mode == 'c')
2011-05-25 15:40:47 -04:00
printf("%ld\n", c);
end:
if (ferror(fp)) {
weprintf("%s: read error:", str);
match = Error;
}
2013-09-27 11:26:22 -04:00
return match;
2011-05-22 21:36:34 -04:00
}