-
Notifications
You must be signed in to change notification settings - Fork 18
/
regexp.c
68 lines (61 loc) · 1.35 KB
/
regexp.c
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
/*
* Copyright (c) 2005 - 2011 Miek Gieben
* License: GPLv3(+), see LICENSE for details
*
* regexp helper functions
*/
#include "rdup.h"
#define PCRE2_CODE_UNIT_WIDTH 8
#include <pcre2.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;
int errcode;
PCRE2_SIZE erroff;
char delim;
gpointer d;
size_t l;
size_t s;
size_t re_length;
ssize_t j;
pcre2_code *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 = pcre2_compile((PCRE2_SPTR)buf, PCRE2_ZERO_TERMINATED, 0, &errcode, &erroff, NULL)) == NULL) {
/* error */
fclose(fp);
msg(_
("Corrupt regular expression line: %zd, column %d: %d"),
l, erroff, errcode);
g_free(buf);
return FALSE;
} else {
pcre2_pattern_info(P, PCRE2_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;
}