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>
|
2012-05-21 20:05:03 -04:00
|
|
|
#include <string.h>
|
2011-05-23 20:13:34 -04:00
|
|
|
#include <unistd.h>
|
2014-11-13 12:29:30 -05:00
|
|
|
|
2014-11-16 07:37:43 -05:00
|
|
|
#include "queue.h"
|
2011-05-25 15:40:47 -04:00
|
|
|
#include "text.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 *);
|
2014-06-01 08:03:10 -04:00
|
|
|
static int grep(FILE *, const char *);
|
2011-05-22 21:36:34 -04:00
|
|
|
|
2014-11-13 15:24:47 -05:00
|
|
|
static int eflag = 0;
|
|
|
|
static int vflag = 0;
|
2014-11-16 05:45:10 -05:00
|
|
|
static int Hflag = 0;
|
2014-11-13 15:24:47 -05:00
|
|
|
static int many;
|
2011-05-22 21:36:34 -04:00
|
|
|
static char mode = 0;
|
|
|
|
|
2014-11-16 07:37:43 -05:00
|
|
|
struct pattern {
|
2013-09-27 11:26:22 -04:00
|
|
|
char *pattern;
|
2014-05-12 06:59:39 -04:00
|
|
|
regex_t preg;
|
2014-11-16 07:37:43 -05:00
|
|
|
TAILQ_ENTRY(pattern) entry;
|
|
|
|
};
|
|
|
|
|
|
|
|
static TAILQ_HEAD(phead, pattern) phead;
|
2013-09-27 11:26:22 -04:00
|
|
|
|
2014-04-22 09:44:16 -04:00
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
2014-11-16 05:45:10 -05:00
|
|
|
enprintf(Error, "usage: %s [-EHcilnqv] [-e pattern] pattern [files...]\n", argv0);
|
2014-04-22 09:44:16 -04:00
|
|
|
}
|
|
|
|
|
2011-05-22 21:36:34 -04:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
2014-11-16 07:37:43 -05:00
|
|
|
struct pattern *pnode, *tmp;
|
2014-06-01 08:03:10 -04:00
|
|
|
int i, n, m, flags = REG_NOSUB, match = NoMatch;
|
|
|
|
char buf[BUFSIZ];
|
2011-05-22 21:36:34 -04:00
|
|
|
FILE *fp;
|
|
|
|
|
2014-11-16 07:37:43 -05:00
|
|
|
TAILQ_INIT(&phead);
|
|
|
|
|
2012-05-31 14:38:25 -04:00
|
|
|
ARGBEGIN {
|
|
|
|
case 'E':
|
|
|
|
flags |= REG_EXTENDED;
|
|
|
|
break;
|
2014-11-16 05:45:10 -05:00
|
|
|
case 'H':
|
|
|
|
Hflag = 1;
|
|
|
|
break;
|
2013-09-27 11:26:22 -04:00
|
|
|
case 'e':
|
|
|
|
addpattern(EARGF(usage()));
|
2014-11-13 15:24:47 -05:00
|
|
|
eflag = 1;
|
2013-09-27 11:26:22 -04: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;
|
|
|
|
break;
|
|
|
|
case 'v':
|
2014-11-13 15:24:47 -05:00
|
|
|
vflag = 1;
|
2012-05-31 14:38:25 -04:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
} ARGEND;
|
2011-05-22 21:36:34 -04:00
|
|
|
|
2014-11-13 12:29:30 -05:00
|
|
|
if (argc == 0 && !eflag)
|
2014-05-12 06:59:39 -04:00
|
|
|
usage(); /* no pattern */
|
2012-05-31 14:38:25 -04:00
|
|
|
|
2013-09-27 11:26:22 -04:00
|
|
|
/* If -e is not specified treat it as if it were */
|
2014-11-13 12:29:30 -05:00
|
|
|
if (!eflag) {
|
2013-09-27 11:26:22 -04:00
|
|
|
addpattern(argv[0]);
|
|
|
|
argc--;
|
|
|
|
argv++;
|
|
|
|
}
|
|
|
|
|
2014-05-12 06:59:39 -04:00
|
|
|
/* Compile regex for all search patterns */
|
2014-11-16 07:37:43 -05:00
|
|
|
TAILQ_FOREACH(pnode, &phead, entry) {
|
2014-11-13 12:29:30 -05:00
|
|
|
if ((n = regcomp(&pnode->preg, pnode->pattern, flags)) != 0) {
|
2014-05-12 06:59:39 -04:00
|
|
|
regerror(n, &pnode->preg, buf, sizeof buf);
|
|
|
|
enprintf(Error, "invalid pattern: %s\n", buf);
|
|
|
|
}
|
|
|
|
}
|
2012-05-31 14:38:25 -04:00
|
|
|
many = (argc > 1);
|
2014-11-13 12:29:30 -05:00
|
|
|
if (argc == 0) {
|
2014-05-12 06:59:39 -04:00
|
|
|
match = grep(stdin, "<stdin>");
|
2013-09-27 11:26:22 -04:00
|
|
|
} else {
|
2014-11-13 12:29:30 -05:00
|
|
|
for (i = 0; i < argc; i++) {
|
|
|
|
if (!(fp = fopen(argv[i], "r"))) {
|
2014-06-01 08:03:10 -04:00
|
|
|
weprintf("fopen %s:", argv[i]);
|
|
|
|
match = Error;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
m = grep(fp, argv[i]);
|
2014-11-13 12:29:30 -05:00
|
|
|
if (m == Error || (match != Error && m == Match))
|
2014-06-01 08:03:10 -04:00
|
|
|
match = m;
|
2013-09-27 11:26:22 -04:00
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
}
|
2014-11-16 07:37:43 -05:00
|
|
|
TAILQ_FOREACH_SAFE(pnode, &phead, entry, tmp) {
|
|
|
|
TAILQ_REMOVE(&phead, pnode, entry);
|
2014-05-12 06:59:39 -04:00
|
|
|
regfree(&pnode->preg);
|
2013-09-27 11:26:22 -04:00
|
|
|
free(pnode->pattern);
|
|
|
|
free(pnode);
|
2011-05-22 21:36:34 -04:00
|
|
|
}
|
2014-06-01 08:03:10 -04:00
|
|
|
return match;
|
2011-05-22 21:36:34 -04:00
|
|
|
}
|
|
|
|
|
2014-06-01 08:03:10 -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;
|
2013-09-27 11:26:22 -04:00
|
|
|
|
2014-11-16 05:07:26 -05:00
|
|
|
pnode = emalloc(sizeof(*pnode));
|
|
|
|
pnode->pattern = estrdup(pattern);
|
2014-11-16 07:37:43 -05:00
|
|
|
TAILQ_INSERT_TAIL(&phead, pnode, entry);
|
2013-09-27 11:26:22 -04:00
|
|
|
}
|
|
|
|
|
2014-06-01 08:03:10 -04:00
|
|
|
static int
|
2014-05-12 06:59:39 -04:00
|
|
|
grep(FILE *fp, const char *str)
|
2011-05-22 21:36:34 -04:00
|
|
|
{
|
2011-05-25 15:40:47 -04:00
|
|
|
char *buf = NULL;
|
2014-06-01 08:03:10 -04:00
|
|
|
size_t len = 0, size = 0;
|
|
|
|
long c = 0, n;
|
2014-11-16 07:37:43 -05:00
|
|
|
struct pattern *pnode;
|
2014-06-01 08:03:10 -04:00
|
|
|
int match = NoMatch;
|
2011-05-22 21:36:34 -04:00
|
|
|
|
2014-11-13 12:29:30 -05:00
|
|
|
for (n = 1; (len = agetline(&buf, &size, fp)) != -1; n++) {
|
2014-11-01 23:08:12 -04:00
|
|
|
/* Remove the trailing newline if one is present. */
|
|
|
|
if (len && buf[len - 1] == '\n')
|
|
|
|
buf[len - 1] = '\0';
|
2014-11-16 07:37:43 -05:00
|
|
|
TAILQ_FOREACH(pnode, &phead, entry) {
|
2014-11-13 12:29:30 -05:00
|
|
|
if (regexec(&pnode->preg, buf, 0, NULL, 0) ^ vflag)
|
2013-09-27 11:26:22 -04:00
|
|
|
continue;
|
2014-11-13 12:29:30 -05:00
|
|
|
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 05:45:10 -05:00
|
|
|
if (many || Hflag)
|
2013-09-27 11:26:22 -04:00
|
|
|
printf("%s:", str);
|
2014-11-13 12:29:30 -05:00
|
|
|
if (mode == 'n')
|
2013-09-27 11:26:22 -04:00
|
|
|
printf("%ld:", n);
|
2014-11-01 23:08:12 -04:00
|
|
|
puts(buf);
|
2013-09-27 11:26:22 -04:00
|
|
|
break;
|
|
|
|
}
|
2014-06-01 08:03:10 -04:00
|
|
|
match = Match;
|
2011-05-22 21:36:34 -04:00
|
|
|
}
|
|
|
|
}
|
2014-11-13 12:29:30 -05:00
|
|
|
if (mode == 'c')
|
2011-05-25 15:40:47 -04:00
|
|
|
printf("%ld\n", c);
|
|
|
|
end:
|
2014-11-13 12:29:30 -05:00
|
|
|
if (ferror(fp)) {
|
2014-06-01 08:03:10 -04:00
|
|
|
weprintf("%s: read error:", str);
|
|
|
|
match = Error;
|
|
|
|
}
|
2011-05-25 15:40:47 -04:00
|
|
|
free(buf);
|
2013-09-27 11:26:22 -04:00
|
|
|
return match;
|
2011-05-22 21:36:34 -04:00
|
|
|
}
|