sbase/grep.c

172 lines
3.0 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 <stdio.h>
#include <stdlib.h>
#include <string.h>
2011-05-24 00:13:34 +00:00
#include <unistd.h>
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 *);
static int grep(FILE *, const char *);
2011-05-23 01:36:34 +00:00
static int eflag = 0;
static int vflag = 0;
2014-11-16 19:03:25 +00:00
static int hflag = 0;
2014-11-16 10:45:10 +00:00
static int Hflag = 0;
static int many;
2011-05-23 01:36:34 +00:00
static char mode = 0;
2014-11-16 12:37:43 +00:00
struct pattern {
2013-09-27 15:26:22 +00:00
char *pattern;
regex_t preg;
SLIST_ENTRY(pattern) entry;
2014-11-16 12:37:43 +00:00
};
static SLIST_HEAD(phead, pattern) phead;
2013-09-27 15:26:22 +00:00
static void
usage(void)
{
2014-11-16 10:45:10 +00:00
enprintf(Error, "usage: %s [-EHcilnqv] [-e pattern] pattern [files...]\n", argv0);
}
2011-05-23 01:36:34 +00:00
int
main(int argc, char *argv[])
{
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;
SLIST_INIT(&phead);
2014-11-16 12:37:43 +00:00
2012-05-31 18:38:25 +00:00
ARGBEGIN {
case 'E':
flags |= REG_EXTENDED;
break;
2014-11-16 10:45:10 +00:00
case 'H':
Hflag = 1;
break;
2013-09-27 15:26:22 +00:00
case 'e':
addpattern(EARGF(usage()));
eflag = 1;
2013-09-27 15:26:22 +00:00
break;
2014-11-16 19:03:25 +00:00
case 'h':
hflag = 1;
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;
break;
case 'v':
vflag = 1;
2012-05-31 18:38:25 +00:00
break;
default:
usage();
} ARGEND;
2011-05-23 01:36:34 +00:00
if (argc == 0 && !eflag)
usage(); /* no pattern */
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) {
2013-09-27 15:26:22 +00:00
addpattern(argv[0]);
argc--;
argv++;
}
/* Compile regex for all search patterns */
SLIST_FOREACH(pnode, &phead, entry)
2014-11-16 14:17:46 +00:00
enregcomp(Error, &pnode->preg, pnode->pattern, flags);
2012-05-31 18:38:25 +00:00
many = (argc > 1);
if (argc == 0) {
match = grep(stdin, "<stdin>");
2013-09-27 15:26:22 +00:00
} else {
for (i = 0; i < argc; i++) {
if (!(fp = fopen(argv[i], "r"))) {
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 15:26:22 +00:00
fclose(fp);
}
}
while (!SLIST_EMPTY(&phead)) {
pnode = SLIST_FIRST(&phead);
SLIST_REMOVE_HEAD(&phead, entry);
regfree(&pnode->preg);
2013-09-27 15:26:22 +00:00
free(pnode->pattern);
free(pnode);
2011-05-23 01:36:34 +00:00
}
return match;
2011-05-23 01:36:34 +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;
2013-09-27 15:26:22 +00:00
pnode = emalloc(sizeof(*pnode));
pnode->pattern = estrdup(pattern);
SLIST_INSERT_HEAD(&phead, pnode, entry);
2013-09-27 15:26:22 +00:00
}
static int
grep(FILE *fp, const char *str)
2011-05-23 01:36:34 +00:00
{
2011-05-25 19:40:47 +00:00
char *buf = NULL;
size_t len = 0, size = 0;
long c = 0, n;
2014-11-16 12:37:43 +00:00
struct pattern *pnode;
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++) {
/* Remove the trailing newline if one is present. */
if (len && buf[len - 1] == '\n')
buf[len - 1] = '\0';
SLIST_FOREACH(pnode, &phead, entry) {
if (regexec(&pnode->preg, buf, 0, NULL, 0) ^ vflag)
2013-09-27 15:26:22 +00:00
continue;
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);
if (mode == 'n')
2013-09-27 15:26:22 +00:00
printf("%ld:", n);
puts(buf);
2013-09-27 15:26:22 +00:00
break;
}
match = Match;
2011-05-23 01:36:34 +00:00
}
}
if (mode == 'c')
2011-05-25 19:40:47 +00:00
printf("%ld\n", c);
end:
if (ferror(fp)) {
weprintf("%s: read error:", str);
match = Error;
}
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
}