Files
sbase/grep.c
T

291 lines
5.3 KiB
C
Raw Normal View History

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