diff options
author | Lieuwe Rooijakkers <lieuwerooijakkers@gmail.com> | 2024-08-07 23:03:14 +0200 |
---|---|---|
committer | Lieuwe Rooijakkers <lieuwerooijakkers@gmail.com> | 2024-08-07 23:03:14 +0200 |
commit | a0962d7311c057229a25ae5f30fc3e9645d6ff24 (patch) | |
tree | fc366196484985d22e0e5a2c04fe5f26378c0bfd /src | |
parent | 568f84f8ab3b62c1e6d2982beada18590a8db01b (diff) |
regex grijp
Diffstat (limited to 'src')
-rw-r--r-- | src/grijp.c | 79 |
1 files changed, 74 insertions, 5 deletions
diff --git a/src/grijp.c b/src/grijp.c index 0c406f4..9767c46 100644 --- a/src/grijp.c +++ b/src/grijp.c @@ -2,6 +2,7 @@ #include <errno.h> #include <getopt.h> #include <pwd.h> +#include <regex.h> #include <stdlib.h> #include <string.h> #include <sys/types.h> @@ -16,6 +17,8 @@ char *gpats; +bool fixed, extended, icase; + static void usage(FILE *f) { fprintf(f, "Gebruik: grijp [-hV] [PATRONEN] [BESTAND]...\n" @@ -29,8 +32,15 @@ static void usage(FILE *f) { // Returns pointer to argument array containing patterns and then the file names static char** parse_options(int argc, char **argv) { int opt; - while ((opt = getopt(argc, argv, "hV")) != -1) { + while ((opt = getopt(argc, argv, "FihV")) != -1) { switch (opt) { + case 'F': + fixed = true; + break; + case 'i': + icase = true; + break; + case 'h': usage(stdout); exit(0); @@ -49,9 +59,7 @@ static char** parse_options(int argc, char **argv) { return argv + optind; } -static int process(char *fname, bool isstdin) { - FILE *stream = isstdin ? stdin : fopen(fname, "rb"); - +static char **get_patterns(size_t *_npat) { char **patterns; size_t npat = 0; { @@ -72,6 +80,62 @@ static int process(char *fname, bool isstdin) { } } + *_npat = npat; + return patterns; +} + +static int process_regex(char *fname, bool isstdin) { + FILE *stream = isstdin ? stdin : fopen(fname, "rb"); + + size_t npat = 0; + char **patterns = get_patterns(&npat); + + regex_t **regexps = calloc(sizeof(regex_t*), npat); + for (size_t i = 0; i < npat; i++) { + regex_t *regexp = calloc(1, sizeof(regex_t)); + + int flags = 0; + if (extended) flags |= REG_EXTENDED; + if (icase) flags |= REG_ICASE; + + if (regcomp(regexp, patterns[i], flags) != 0) { + // TODO: error + return 1; + } + + free(patterns[i]); + regexps[i] = regexp; + } + free(patterns); + + char *line = NULL; + size_t linen = 0; + + ssize_t nread = 0; + while ((errno = 0, nread = getline(&line, &linen, stream)) != -1) { + for (size_t i = 0; i < npat; i++) { + regex_t *regexp = regexps[i]; + + regmatch_t pmatch; + if (regexec(regexp, line, 1, &pmatch, 0) == 0) { + printf("%s", line); + } + } + } + free(line); + + for (size_t i = 0; i < npat; i++) regfree(regexps[i]); + free(regexps); + + return 0; +} + +static int process_fixed(char *fname, bool isstdin) { + FILE *stream = isstdin ? stdin : fopen(fname, "rb"); + + size_t npat = 0; + char **patterns = get_patterns(&npat); + char *line = NULL; size_t linen = 0; @@ -100,5 +164,10 @@ int entry_grijp(int argc, char **argv) { gpats = *args; args++; - return loop_args(args, process); + + if (fixed) { + return loop_args(args, process_fixed); + } else { + return loop_args(args, process_regex); + } } |