diff options
Diffstat (limited to 'grep.c')
| -rw-r--r-- | grep.c | 170 |
1 files changed, 81 insertions, 89 deletions
@@ -26,13 +26,10 @@ #include <errno.h> #include <locale.h> -#include <limits.h> #include <regex.h> #include <stdio.h> #include <stdlib.h> #include <string.h> -#include <sys/stat.h> -#include <sys/types.h> #include <unistd.h> struct grep_list { @@ -41,14 +38,6 @@ struct grep_list { regex_t re; }; -#ifndef ARG_MAX -#define ARG_MAX _POSIX_ARG_MAX -#endif - -#define BASIC 0 -#define EXTENDED 1 -#define FIXED 2 - #define NORMAL 0 #define COUNT 1 #define LIST 2 @@ -61,95 +50,60 @@ struct grep_list { #define WHOLELINE 1 << 4 #define FILENAMES 1 << 5 -#if 0 -static int grep(int patterns, char **p, int type, int display, int flags, - char *path) +static int grep_match(struct grep_list *head, const char *buf) { - FILE *f = stdin; - char *buf = NULL; - int line = 0; - size_t len = 0; - ssize_t nread = 0; - regmatch_t pmatch[1]; - regex_t re; - int i; - int count = 0; - struct stat st; + for (struct grep_list *c = head; c != NULL; c = c->next) { + if (c->string) { + if (strstr(buf, c->string)) { + return 1; + } + continue; + } - stat(path, &st); - if (S_ISDIR(st.st_mode)) { - return 0; + printf("%s => ", buf); + int e; + if ((e = regexec(&(c->re), buf, 0, NULL, REG_NOSUB)) == 0) { + return 1; + } + printf("%d\n", e); } - if (strcmp("-", path)) { - f = fopen(path, "r"); - } + return 0; +} - if (f == NULL) { - if (!(flags & SUPPRESS)) { - perror(path); - } - return 0; - } +static int grep(struct grep_list *head, const char *path, int flags) +{ + (void)flags; - while ((nread = getline(&buf, &len, f)) != -1) { - line++; - for (i = 0; i < patterns; i++) { - // FIXME: fixed pattern - if (regcomp - (&re, p[i], - (type == - BASIC ? 0 : REG_EXTENDED) | (flags & IGNORECASE ? - REG_ICASE : 0)) != - 0) { - printf("Bad regex: %s\n", p[i]); - exit(2); - } else if (regexec(&re, buf, 1, pmatch, 0) == - (flags & INVERT ? REG_NOMATCH : 0)) { - if (!(flags & WHOLELINE) - || (flags & WHOLELINE - && pmatch[0].rm_so == 0 - && pmatch[0].rm_eo == nread - 1)) { - count++; - if (NORMAL == display) { - if (flags & FILENAMES) - printf("%s:", path); - if (flags & LINENUMBERS) - printf("%d:", line); - fwrite(buf, sizeof(char), nread, - stdout); - } - } + FILE *f = stdin; + if (path && strcmp(path, "-")) { + f = fopen(path, "r"); + if (f == NULL) { + if (/*!suppress ||*/ (errno != ENOENT && errno != EPERM)) { + fprintf(stderr, "grep: %s: %s\n", path, strerror(errno)); } - regfree(&re); - } - if (buf != NULL) { - free(buf); - buf = NULL; + return 0; } - len = 0; } - if (strcmp("-", path)) { - fclose(f); - } - - if (count > 0 && LIST == display) { - if (strcmp("-", path)) { - printf("%s\n", path); - } else { - printf("(standard input)\n"); + int found = 0; + char *buf = NULL; + size_t len = 0; + while (getline(&buf, &len, f) != -1) { + char *nl = strrchr(buf, '\n'); + if (nl) { + *nl = '\0'; } - } else if (COUNT == display) { - if (flags & FILENAMES) { - printf("%s:", path); + if (grep_match(head, buf)) { + printf("%s\n", buf); + found++; } - printf("%d\n", count); } - return count; + fclose(f); + + return found; } -#endif static struct grep_list * grep_add_list(struct grep_list *head, char *s) { @@ -202,10 +156,11 @@ int main(int argc, char *argv[]) { setlocale(LC_ALL, ""); - int c; int flags = REG_NOSUB; struct grep_list *head = NULL; + enum {REGEX, FIXED } type = REGEX; + int c; while ((c = getopt(argc, argv, "EFce:f:ilnqsvx")) != -1) { switch (c) { case 'E': @@ -213,7 +168,7 @@ int main(int argc, char *argv[]) break; case 'F': - //type = FIXED; // FIXME + type = FIXED; break; case 'c': @@ -269,7 +224,44 @@ int main(int argc, char *argv[]) head = grep_add_list(head, argv[optind++]); } - for (struct grep_list *c = head; c != NULL; c = c->next) { - printf("pattern '%s'\n", c->string); + if (type != FIXED) { + for (struct grep_list *c = head; c != NULL; c = c->next) { + int e = regcomp(&(c->re), c->string, flags); + if (e == 0) { + free(c->string); + c->string = NULL; + continue; + } + + char err[512]; + regerror(e, &(c->re), err, sizeof(err)); + fprintf(stderr, "grep: %s: %s\n", c->string, err); + return 2; + } } + + int found = 0; + do { + found += grep(head, argv[optind++], flags); + } while (optind < argc); + + printf("REG_BADBR=%d\n", REG_BADBR); + printf("REG_BADPAT=%d\n", REG_BADPAT); + printf("REG_BADRPT=%d\n", REG_BADRPT); + printf("REG_EBRACE=%d\n", REG_EBRACE); + printf("REG_EBRACK=%d\n", REG_EBRACK); + printf("REG_ECOLLATE=%d\n", REG_ECOLLATE); + printf("REG_ECTYPE=%d\n", REG_ECTYPE); + printf("REG_EESCAPE=%d\n", REG_EESCAPE); + printf("REG_EPAREN=%d\n", REG_EPAREN); + printf("REG_ERANGE=%d\n", REG_ERANGE); + printf("REG_ESPACE=%d\n", REG_ESPACE); + printf("REG_ESUBREG=%d\n", REG_ESUBREG); + printf("REG_NOMATCH=%d\n", REG_NOMATCH); + + if (found) { + return 0; + } + + return 1; } |
