summaryrefslogtreecommitdiff
path: root/src/grijp.c
blob: 9767c46c19d0f41eaf1f2f89a2652a9b75edf3ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include <assert.h>
#include <errno.h>
#include <getopt.h>
#include <pwd.h>
#include <regex.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>

#include "util/debug.h"
#include "util/error.h"
#include "util/loop_args.h"
#include "util/versie.h"

#include "io/read_file.h"

char *gpats;

bool fixed, extended, icase;

static void usage(FILE *f) {
  fprintf(f,
      "Gebruik: grijp [-hV] [PATRONEN] [BESTAND]...\n"
      "\n"
      "TODO\n"
      "\n"
      "  -h          Toon deze hulptekst\n"
      "  -V          Toon versienummer\n");
}

// 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, "FihV")) != -1) {
    switch (opt) {
      case 'F':
        fixed = true;
        break;
      case 'i':
        icase = true;
        break;

      case 'h':
        usage(stdout);
        exit(0);

      case 'V':
        drukkedoos_print_versie(stdout);
        exit(0);

      case '?':
        fprintf(stderr, "grijp: Ongeldige optie: -%c\n", optopt);
        usage(stderr);
        exit(1);
    }
  }

  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++;
    }
  }

  *_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;

  ssize_t nread = 0;
  while ((errno = 0, nread = getline(&line, &linen, stream)) != -1) {
    for (size_t i = 0; i < npat; i++) {
      char *pat = patterns[i];
      if (strstr(line, pat) == NULL) continue;
      printf("%s", line);
    }
  }
  free(line);

  for (size_t i = 0; i < npat; i++) free(patterns[i]);
  free(patterns);

  return 0;
}

int entry_grijp(int argc, char **argv) {
  char **args = parse_options(argc, argv);
  if (*args == NULL) {
    fprintf(stderr, "grijp: geen patroon gegeven\n");
    return 1;
  }

  gpats = *args;
  args++;

  if (fixed) {
    return loop_args(args, process_fixed);
  } else {
    return loop_args(args, process_regex);
  }
}