summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/grijp.c86
1 files changed, 39 insertions, 47 deletions
diff --git a/src/grijp.c b/src/grijp.c
index 7d33f83..29a13b1 100644
--- a/src/grijp.c
+++ b/src/grijp.c
@@ -15,11 +15,14 @@
#include "io/read_file.h"
-char *gpats;
+static char *gpats;
-bool fixed = false;
-bool extended = false;
-bool icase = false;
+static bool fixed = false;
+static bool extended = false;
+static bool icase = false;
+
+static size_t npat = 0;
+static void **patterns = NULL; // can hold `char*` or `regex_t*`
static void usage(FILE *f) {
fprintf(f,
@@ -64,38 +67,25 @@ static char** parse_options(int argc, char **argv) {
return argv + optind;
}
-static char **get_patterns(size_t *_npat) {
- char **patterns;
- size_t npat = 0;
- {
- size_t cappat = 1 << 4;
- patterns = calloc(sizeof(char*), cappat);
-
- char *s = gpats;
- char *pat = NULL;
- while ((pat = strtok(s, "\n")) != NULL) {
- s = NULL;
-
- if (npat == cappat) {
- cappat *= 2;
- patterns = realloc(patterns, sizeof(char*)*cappat);
- }
- patterns[npat] = strdup(pat);
- npat++;
+static void parse_patterns() {
+ size_t cappat = 1 << 4;
+ patterns = calloc(sizeof(char*), cappat);
+
+ char *s = gpats;
+ char *pat = NULL;
+ while ((pat = strtok(s, "\n")) != NULL) {
+ s = NULL;
+
+ if (npat == cappat) {
+ cappat *= 2;
+ patterns = realloc(patterns, sizeof(char*)*cappat);
}
+ patterns[npat] = strdup(pat);
+ npat++;
}
-
- *_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);
+static void compile_regexps() {
for (size_t i = 0; i < npat; i++) {
regex_t *regexp = calloc(1, sizeof(regex_t));
@@ -105,13 +95,16 @@ static int process_regex(char *fname, bool isstdin) {
if (regcomp(regexp, patterns[i], flags) != 0) {
fprintf(stderr, "grijp: fout tijdens compileren van reguliere expressie\n");
- return 1;
+ exit(1);
}
free(patterns[i]);
- regexps[i] = regexp;
+ patterns[i] = regexp;
}
- free(patterns);
+}
+
+static int process_regex(char *fname, bool isstdin) {
+ FILE *stream = isstdin ? stdin : fopen(fname, "rb");
char *line = NULL;
size_t linen = 0;
@@ -119,7 +112,7 @@ static int process_regex(char *fname, bool isstdin) {
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];
+ regex_t *regexp = patterns[i];
regmatch_t pmatch;
if (regexec(regexp, line, 1, &pmatch, 0) == 0) {
@@ -129,9 +122,6 @@ static int process_regex(char *fname, bool isstdin) {
}
free(line);
- for (size_t i = 0; i < npat; i++) regfree(regexps[i]);
- free(regexps);
-
if (!isstdin) fclose(stream);
return 0;
}
@@ -139,9 +129,6 @@ static int process_regex(char *fname, bool isstdin) {
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;
@@ -155,9 +142,6 @@ static int process_fixed(char *fname, bool isstdin) {
}
free(line);
- for (size_t i = 0; i < npat; i++) free(patterns[i]);
- free(patterns);
-
if (!isstdin) fclose(stream);
return 0;
}
@@ -172,9 +156,17 @@ int entry_grijp(int argc, char **argv) {
gpats = *args;
args++;
+ parse_patterns();
+
+ int res = 0;
if (fixed) {
- return loop_args(args, process_fixed);
+ res = loop_args(args, process_fixed);
+ for (size_t i = 0; i < npat; i++) free(patterns[i]);
} else {
- return loop_args(args, process_regex);
+ compile_regexps();
+ res = loop_args(args, process_regex);
+ for (size_t i = 0; i < npat; i++) regfree(patterns[i]);
}
+ free(patterns);
+ return res;
}