2011-05-23 01:36:34 +00:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
|
|
|
#include <regex.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2011-05-24 00:13:34 +00:00
|
|
|
#include <unistd.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
|
|
|
|
|
|
|
static void grep(FILE *, const char *, regex_t *);
|
|
|
|
|
|
|
|
static bool vflag = false;
|
|
|
|
static bool many;
|
|
|
|
static bool match = false;
|
|
|
|
static char mode = 0;
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
2011-05-24 00:13:34 +00:00
|
|
|
char c;
|
2011-05-29 19:38:15 +00:00
|
|
|
int flags = REG_NOSUB;
|
2011-05-23 01:36:34 +00:00
|
|
|
regex_t preg;
|
|
|
|
FILE *fp;
|
|
|
|
|
2011-06-04 10:57:31 +00:00
|
|
|
while((c = getopt(argc, argv, "Ecilnqv")) != -1)
|
2011-05-24 00:13:34 +00:00
|
|
|
switch(c) {
|
2011-06-04 10:57:31 +00:00
|
|
|
case 'E':
|
2011-06-04 11:00:23 +00:00
|
|
|
flags |= REG_EXTENDED;
|
2011-06-04 10:57:31 +00:00
|
|
|
break;
|
2011-05-24 00:13:34 +00:00
|
|
|
case 'c':
|
|
|
|
case 'l':
|
|
|
|
case 'n':
|
|
|
|
case 'q':
|
|
|
|
mode = c;
|
|
|
|
break;
|
|
|
|
case 'i':
|
2011-06-04 11:00:23 +00:00
|
|
|
flags |= REG_ICASE;
|
2011-05-24 00:13:34 +00:00
|
|
|
break;
|
|
|
|
case 'v':
|
2011-05-23 01:36:34 +00:00
|
|
|
vflag = true;
|
|
|
|
break;
|
2011-05-24 00:13:34 +00:00
|
|
|
default:
|
2011-06-18 05:42:24 +00:00
|
|
|
exit(Error);
|
2011-05-24 00:13:34 +00:00
|
|
|
}
|
2011-06-18 05:42:24 +00:00
|
|
|
if(optind == argc)
|
|
|
|
enprintf(Error, "usage: %s [-Ecilnqv] pattern [files...]\n", argv[0]);
|
2011-05-24 00:13:34 +00:00
|
|
|
regcomp(&preg, argv[optind++], flags);
|
2011-05-23 01:36:34 +00:00
|
|
|
|
2011-05-24 00:13:34 +00:00
|
|
|
many = (argc > optind+1);
|
|
|
|
if(optind == argc)
|
2011-05-23 01:36:34 +00:00
|
|
|
grep(stdin, "<stdin>", &preg);
|
2011-05-24 00:13:34 +00:00
|
|
|
else for(; optind < argc; optind++) {
|
2011-06-18 05:42:24 +00:00
|
|
|
if(!(fp = fopen(argv[optind], "r")))
|
|
|
|
enprintf(Error, "fopen %s:", argv[optind]);
|
2011-05-24 00:13:34 +00:00
|
|
|
grep(fp, argv[optind], &preg);
|
2011-05-23 01:36:34 +00:00
|
|
|
fclose(fp);
|
|
|
|
}
|
2011-06-18 05:42:24 +00:00
|
|
|
return match ? Match : NoMatch;
|
2011-05-23 01:36:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
grep(FILE *fp, const char *str, regex_t *preg)
|
|
|
|
{
|
2011-05-25 19:40:47 +00:00
|
|
|
char *buf = NULL;
|
|
|
|
long n, c = 0;
|
|
|
|
size_t size = 0;
|
2011-05-23 01:36:34 +00:00
|
|
|
|
2011-05-25 19:40:47 +00:00
|
|
|
for(n = 1; afgets(&buf, &size, fp); n++) {
|
2011-05-23 01:36:34 +00:00
|
|
|
if(regexec(preg, buf, 0, NULL, 0) ^ vflag)
|
|
|
|
continue;
|
2011-05-25 19:40:47 +00:00
|
|
|
switch(mode) {
|
|
|
|
case 'c':
|
2011-05-23 01:36:34 +00:00
|
|
|
c++;
|
|
|
|
break;
|
2011-05-25 19:40:47 +00:00
|
|
|
case 'l':
|
|
|
|
puts(str);
|
|
|
|
goto end;
|
|
|
|
case 'q':
|
2011-06-18 05:42:24 +00:00
|
|
|
exit(Match);
|
2011-05-25 19:40:47 +00:00
|
|
|
default:
|
2011-05-23 01:36:34 +00:00
|
|
|
if(many)
|
|
|
|
printf("%s:", str);
|
|
|
|
if(mode == 'n')
|
2011-05-25 19:40:47 +00:00
|
|
|
printf("%ld:", n);
|
2011-05-23 01:36:34 +00:00
|
|
|
fputs(buf, stdout);
|
2011-05-25 19:40:47 +00:00
|
|
|
break;
|
2011-05-23 01:36:34 +00:00
|
|
|
}
|
|
|
|
match = true;
|
|
|
|
}
|
|
|
|
if(mode == 'c')
|
2011-05-25 19:40:47 +00:00
|
|
|
printf("%ld\n", c);
|
|
|
|
end:
|
2011-06-18 05:42:24 +00:00
|
|
|
if(ferror(fp))
|
|
|
|
enprintf(Error, "%s: read error:", str);
|
2011-05-25 19:40:47 +00:00
|
|
|
free(buf);
|
2011-05-23 01:36:34 +00:00
|
|
|
}
|