Browse Source

* regexps.c: add f2b_regexlist_from_file()

master
Alex 'AdUser' Z 8 years ago
parent
commit
17850f6cfe
  1. 54
      src/regexps.c
  2. 1
      src/regexps.h

54
src/regexps.c

@ -1,5 +1,7 @@
/* this file should not be used directly, only with `#include "regexps.c"` */
#include "log.h"
f2b_regex_t *
f2b_regexlist_append(f2b_regex_t *list, f2b_regex_t *regex) {
assert(regex != NULL);
@ -18,6 +20,58 @@ f2b_regexlist_match(f2b_regex_t *list, const char *line, char *buf, size_t buf_s
return false;
}
f2b_regex_t *
f2b_regexlist_from_file(const char *path) {
f2b_regex_t *list = NULL, *regex = NULL;
FILE *f = NULL;
size_t linenum = 0;
char line[REGEX_LINE_MAX] = "";
char *p, *q;
if ((f = fopen(path, "r")) == NULL) {
f2b_log_msg(log_error, "can't open regex list '%s': %s", path, strerror(errno));
return NULL;
}
while (1) {
p = fgets(line, sizeof(line), f);
if (!p && (feof(f) || ferror(f)))
break;
linenum++;
/* strip leading spaces */
while (isblank(*p))
p++;
/* strip trailing spaces */
if ((q = strchr(p, '\r')) || (q = strchr(p, '\n'))) {
while(q > p && isspace(*q)) {
*q = '\0';
q--;
}
}
switch(*p) {
case '\r':
case '\n':
case '\0':
/* empty line */
break;
case ';':
case '#':
/* comment line */
break;
default:
/* TODO: icase */
if ((regex = f2b_regex_create(p, false)) == NULL) {
f2b_log_msg(log_warn, "can't create regex from pattern at %s:%s: %s", path, linenum, p);
continue;
}
list = f2b_regexlist_append(list, regex);
break;
}
}
return list;
}
f2b_regex_t *
f2b_regexlist_destroy(f2b_regex_t *list) {
f2b_regex_t *next;

1
src/regexps.h

@ -1,6 +1,7 @@
#ifndef F2B_REGEX_H_
#define F2B_REGEX_H_
#define REGEX_LINE_MAX 256
#define HOST_TOKEN "<HOST>"
typedef struct _regex f2b_regex_t;

Loading…
Cancel
Save