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
|
/*
* Copyright (c) 2005 - 2011 Miek Gieben
* See LICENSE for the license
*
* regexp helper functions
*/
#include "rdup.h"
#include <pcre.h>
GSList *pregex_list = NULL;
/**
* Read the filename and create the compiled regexp
* in a linked list
*/
gboolean
regexp_init(char *file)
{
FILE *fp;
char *buf;
const char *errbuf;
int erroff;
char delim;
gpointer d;
size_t l;
size_t s;
size_t re_length;
ssize_t j;
pcre *P;
if ((fp = fopen(file, "r")) == NULL) {
msg(_("Could not open '%s\': %s"), file, strerror(errno));
exit(EXIT_FAILURE);
}
/* read the regexp */
buf = g_malloc(BUFSIZE + 1);
s = BUFSIZE;
delim = '\n';
l = 1;
while ((j = rdup_getdelim(&buf, &s, delim, fp)) != -1) {
if (buf[0] == '#' || buf[0] == '\n')
continue;
/* buf[j - 1] holds the delimeter */
buf[j - 1] = '\0';
if ((P = pcre_compile(buf, 0, &errbuf, &erroff, NULL)) == NULL) {
/* error */
fclose(fp);
msg(_("Corrupt regular expression line: %zd, column %d: %s"), l, erroff, errbuf);
g_free(buf);
return FALSE;
} else {
pcre_fullinfo(P, NULL, PCRE_INFO_SIZE, &re_length);
d = g_malloc(re_length);
d = memcpy(d, P, re_length);
pregex_list = g_slist_append(pregex_list, d);
}
l++;
}
fclose(fp);
g_free(buf);
return TRUE;
}
|