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