Skip to content

Commit eeb04a2

Browse files
authored
Merge pull request #16 from jovanlanik/pcre2
Port to PCRE2
2 parents 4f4ad32 + 95832b6 commit eeb04a2

File tree

8 files changed

+233
-236
lines changed

8 files changed

+233
-236
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
.deps
88
.libs
9+
.cache
910

1011
*.diff
1112
*.orig
@@ -28,6 +29,7 @@ Makefile
2829
Makefile.in
2930
missing
3031
stamp-h1
32+
compile_commands.json
3133

3234
/metalog.lsm
3335
/metalog.spec

AUTHORS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,6 @@
1212

1313
* Mike Frysinger <[email protected]>
1414
Keeping the bitrot away.
15+
16+
* Jovan Lanik <[email protected]>
17+
Port to PCRE2, implemented perms and --group

INSTALL

Lines changed: 160 additions & 162 deletions
Large diffs are not rendered by default.

README

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ http://metalog.sourceforge.net/
2020
------------------------ COMPILATION ------------------------
2121

2222

23-
In order to compile Metalog, you should have the PCRE library on your
23+
In order to compile Metalog, you should have the PCRE2 library on your
2424
system. That library is used to process Perl-compatible regular expressions.
2525

26-
libpcre comes with many distributions, but you can also download it from :
26+
libpcre2 comes with many distributions, but you can also download it from :
2727

2828
http://www.pcre.org/
2929

configure.ac

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,11 @@ AC_CHECK_FUNCS_ONCE(m4_flatten([
6969
setprogname
7070
]))
7171

72-
dnl PCRE
72+
dnl PCRE2
7373

74-
PKG_CHECK_MODULES([PCRE], libpcre)
75-
CFLAGS="$CFLAGS $PCRE_CFLAGS"
76-
LIBS="$LIBS $PCRE_LIBS"
74+
PKG_CHECK_MODULES([PCRE2], [libpcre2-8])
75+
CFLAGS="$CFLAGS $PCRE2_CFLAGS"
76+
LIBS="$LIBS $PCRE2_LIBS"
7777

7878
dnl Options
7979

src/helpers.h

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,16 @@
1616
#define errf(fmt, args...) _err(warnf, fmt, ## args)
1717
#define errp(fmt, args...) _err(warnp, fmt , ## args)
1818

19-
static pcre *wpcre_compile(const char *pattern, int options)
19+
static pcre2_code *wpcre2_compile(const char *pattern, uint32_t options)
2020
{
21-
const char *errptr;
22-
int erroffset;
23-
pcre *ret = pcre_compile(pattern, options, &errptr, &erroffset, NULL);
24-
if (!ret)
25-
warn("Invalid regex [%s] at %d: %s", pattern, erroffset, errptr);
21+
int errcode;
22+
PCRE2_SIZE erroffset;
23+
pcre2_code *ret = pcre2_compile((PCRE2_SPTR)pattern, PCRE2_ZERO_TERMINATED, options, &errcode, &erroffset, NULL);
24+
if (!ret) {
25+
PCRE2_UCHAR buffer[256];
26+
pcre2_get_error_message(errcode, buffer, sizeof(buffer));
27+
warn("Invalid regex [%s] at %zu: %s", pattern, erroffset, buffer);
28+
}
2629
return ret;
2730
}
2831

src/metalog.c

Lines changed: 50 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ static int doLog(const char * fmt, ...);
2222
*/
2323
static int parseLine(char * const line, ConfigBlock **cur_block,
2424
const ConfigBlock * const default_block,
25-
pcre * const re_newblock, pcre * const re_newstmt,
26-
pcre * const re_comment, pcre * const re_null)
25+
pcre2_code * const re_newblock, pcre2_code * const re_newstmt,
26+
pcre2_code * const re_comment, pcre2_code * const re_null)
2727
{
28-
int ovector[16];
29-
pcre *new_regex;
30-
const char *keyword;
31-
const char *value;
28+
pcre2_match_data *match_data = pcre2_match_data_create(16, NULL);
29+
pcre2_code *new_regex;
30+
char *keyword;
31+
char *value;
3232
int line_size;
3333
int stcount;
3434

@@ -41,14 +41,14 @@ static int parseLine(char * const line, ConfigBlock **cur_block,
4141
}
4242
line[--line_size] = 0;
4343
}
44-
if (pcre_exec(re_null, NULL, line, line_size,
45-
0, 0, ovector, ARRAY_SIZE(ovector)) >= 0 ||
46-
pcre_exec(re_comment, NULL, line, line_size,
47-
0, 0, ovector, ARRAY_SIZE(ovector)) >= 0) {
44+
if (pcre2_match(re_null, (PCRE2_SPTR)line, (PCRE2_SIZE)line_size,
45+
0, 0, match_data, NULL) >= 0 ||
46+
pcre2_match(re_comment, (PCRE2_SPTR)line, (PCRE2_SIZE)line_size,
47+
0, 0, match_data, NULL) >= 0) {
4848
return 0;
4949
}
50-
if (pcre_exec(re_newblock, NULL, line, line_size,
51-
0, 0, ovector, ARRAY_SIZE(ovector)) >= 0) {
50+
if (pcre2_match(re_newblock, (PCRE2_SPTR)line, (PCRE2_SIZE)line_size,
51+
0, 0, match_data, NULL) >= 0) {
5252
ConfigBlock *previous_block = *cur_block;
5353

5454
if ((*cur_block = wmalloc(sizeof **cur_block)) == NULL)
@@ -62,10 +62,11 @@ static int parseLine(char * const line, ConfigBlock **cur_block,
6262
return 0;
6363
}
6464
if ((stcount =
65-
pcre_exec(re_newstmt, NULL, line, line_size,
66-
0, 0, ovector, ARRAY_SIZE(ovector))) >= 3) {
67-
pcre_get_substring(line, ovector, stcount, 1, &keyword);
68-
pcre_get_substring(line, ovector, stcount, 2, &value);
65+
pcre2_match(re_newstmt, (PCRE2_SPTR)line, (PCRE2_SIZE)line_size,
66+
0, 0, match_data, NULL)) >= 3) {
67+
PCRE2_SIZE len;
68+
pcre2_substring_get_bynumber(match_data, 1, (PCRE2_UCHAR **)&keyword, &len);
69+
pcre2_substring_get_bynumber(match_data, 2, (PCRE2_UCHAR **)&value, &len);
6970
if (strcasecmp(keyword, "minimum") == 0) {
7071
(*cur_block)->minimum = atoi(value);
7172
} else if (strcasecmp(keyword, "maximum") == 0) {
@@ -112,12 +113,11 @@ static int parseLine(char * const line, ConfigBlock **cur_block,
112113
return -3;
113114
}
114115
(*cur_block)->regexeswithsign = new_regexeswithsign;
115-
if ((new_regex = wpcre_compile(regex, PCRE_CASELESS)) == NULL) {
116+
if ((new_regex = wpcre2_compile(regex, PCRE2_CASELESS)) == NULL) {
116117
free(regex);
117118
return -5;
118119
}
119120
else {
120-
const char *errptr;
121121
RegexWithSign * const this_regex =
122122
&((*cur_block)->regexeswithsign[(*cur_block)->nb_regexes]);
123123

@@ -127,9 +127,7 @@ static int parseLine(char * const line, ConfigBlock **cur_block,
127127
} else {
128128
this_regex->sign = REGEX_SIGN_POSITIVE;
129129
}
130-
this_regex->regex.pcre = new_regex;
131-
this_regex->regex.pcre_extra =
132-
pcre_study(new_regex, 0, &errptr);
130+
this_regex->regex = new_regex;
133131
}
134132
(*cur_block)->nb_regexes++;
135133
} else if (strcasecmp(keyword, "program_regex") == 0 ||
@@ -148,12 +146,11 @@ static int parseLine(char * const line, ConfigBlock **cur_block,
148146
return -3;
149147
}
150148
(*cur_block)->program_regexeswithsign = new_regexeswithsign;
151-
if ((new_regex = wpcre_compile(regex, PCRE_CASELESS)) == NULL) {
149+
if ((new_regex = wpcre2_compile(regex, PCRE2_CASELESS)) == NULL) {
152150
free(regex);
153151
return -5;
154152
}
155153
else {
156-
const char *errptr;
157154
free(regex);
158155
RegexWithSign * const this_regex =
159156
&((*cur_block)->program_regexeswithsign
@@ -164,9 +161,7 @@ static int parseLine(char * const line, ConfigBlock **cur_block,
164161
} else {
165162
this_regex->sign = REGEX_SIGN_POSITIVE;
166163
}
167-
this_regex->regex.pcre = new_regex;
168-
this_regex->regex.pcre_extra =
169-
pcre_study(new_regex, 0, &errptr);
164+
this_regex->regex = new_regex;
170165
}
171166
(*cur_block)->program_nb_regexes++;
172167
} else if (strcasecmp(keyword, "maxsize") == 0) {
@@ -336,18 +331,21 @@ static int parseLine(char * const line, ConfigBlock **cur_block,
336331
}
337332
} else
338333
err("Unknown keyword '%s'! line: %s", keyword, line);
334+
pcre2_substring_free((PCRE2_UCHAR *)keyword);
335+
pcre2_substring_free((PCRE2_UCHAR *)value);
339336
}
337+
pcre2_match_data_free(match_data);
340338
return 0;
341339
}
342340

343341
static int configParser(const char * const file)
344342
{
345343
char line[LINE_MAX];
346344
FILE *fp;
347-
pcre *re_newblock;
348-
pcre *re_newstmt;
349-
pcre *re_comment;
350-
pcre *re_null;
345+
pcre2_code *re_newblock;
346+
pcre2_code *re_newstmt;
347+
pcre2_code *re_comment;
348+
pcre2_code *re_null;
351349
int retcode = 0;
352350
ConfigBlock default_block = {
353351
DEFAULT_MINIMUM, /* minimum */
@@ -381,10 +379,10 @@ static int configParser(const char * const file)
381379
warnp("Can't open the configuration file");
382380
return -2;
383381
}
384-
re_newblock = wpcre_compile(":\\s*$", 0);
385-
re_newstmt = wpcre_compile("^\\s*(.+?)\\s*=\\s*\"?([^\"]*)\"?\\s*$", 0);
386-
re_comment = wpcre_compile("^\\s*#", 0);
387-
re_null = wpcre_compile("^\\s*$", 0);
382+
re_newblock = wpcre2_compile(":\\s*$", 0);
383+
re_newstmt = wpcre2_compile("^\\s*(.+?)\\s*=\\s*\"?([^\"]*)\"?\\s*$", 0);
384+
re_comment = wpcre2_compile("^\\s*#", 0);
385+
re_null = wpcre2_compile("^\\s*$", 0);
388386
if (!re_newblock || !re_newstmt || !re_comment || !re_null) {
389387
retcode = -1;
390388
goto rtn;
@@ -398,16 +396,16 @@ static int configParser(const char * const file)
398396
}
399397
rtn:
400398
if (re_newblock != NULL) {
401-
pcre_free(re_newblock);
399+
pcre2_code_free(re_newblock);
402400
}
403401
if (re_newstmt != NULL) {
404-
pcre_free(re_newstmt);
402+
pcre2_code_free(re_newstmt);
405403
}
406404
if (re_comment != NULL) {
407-
pcre_free(re_comment);
405+
pcre2_code_free(re_comment);
408406
}
409407
if (re_null != NULL) {
410-
pcre_free(re_null);
408+
pcre2_code_free(re_null);
411409
}
412410
fclose(fp);
413411

@@ -1146,7 +1144,7 @@ static int log_stdout(const char * const date,
11461144
static int processLogLine(const int logcode,
11471145
const char * const prg, char * const info)
11481146
{
1149-
int ovector[16];
1147+
pcre2_match_data *match_data = pcre2_match_data_create(16, NULL);
11501148
ConfigBlock *block = config_blocks;
11511149
RegexWithSign *this_regex;
11521150
const int facility = LOG_FAC(logcode);
@@ -1184,17 +1182,15 @@ static int processLogLine(const int logcode,
11841182
this_regex = block->program_regexeswithsign;
11851183
do {
11861184
if (this_regex->sign == REGEX_SIGN_POSITIVE) {
1187-
if (pcre_exec(this_regex->regex.pcre,
1188-
this_regex->regex.pcre_extra,
1189-
prg, prg_len, 0, 0, ovector,
1190-
ARRAY_SIZE(ovector)) >= 0) {
1185+
if (pcre2_match(this_regex->regex,
1186+
(PCRE2_SPTR)prg, (PCRE2_SIZE)prg_len,
1187+
0, 0, match_data, NULL) >= 0) {
11911188
regex_result = 1;
11921189
}
11931190
} else {
1194-
if (pcre_exec(this_regex->regex.pcre,
1195-
this_regex->regex.pcre_extra,
1196-
prg, prg_len, 0, 0, ovector,
1197-
ARRAY_SIZE(ovector)) < 0) {
1191+
if (pcre2_match(this_regex->regex,
1192+
(PCRE2_SPTR)prg, (PCRE2_SIZE)prg_len,
1193+
0, 0, match_data, NULL) < 0) {
11981194
regex_result = 1;
11991195
}
12001196
}
@@ -1213,17 +1209,15 @@ static int processLogLine(const int logcode,
12131209
this_regex = block->regexeswithsign;
12141210
do {
12151211
if (this_regex->sign == REGEX_SIGN_POSITIVE) {
1216-
if (pcre_exec(this_regex->regex.pcre,
1217-
this_regex->regex.pcre_extra,
1218-
info, info_len, 0, 0, ovector,
1219-
ARRAY_SIZE(ovector)) >= 0) {
1212+
if (pcre2_match(this_regex->regex,
1213+
(PCRE2_SPTR)info, (PCRE2_SIZE)info_len,
1214+
0, 0, match_data, NULL) >= 0) {
12201215
regex_result = 1;
12211216
}
12221217
} else {
1223-
if (pcre_exec(this_regex->regex.pcre,
1224-
this_regex->regex.pcre_extra,
1225-
info, info_len, 0, 0, ovector,
1226-
ARRAY_SIZE(ovector)) < 0) {
1218+
if (pcre2_match(this_regex->regex,
1219+
(PCRE2_SPTR)info, (PCRE2_SIZE)info_len,
1220+
0, 0, match_data, NULL) < 0) {
12271221
regex_result = 1;
12281222
}
12291223
}
@@ -1274,6 +1268,7 @@ static int processLogLine(const int logcode,
12741268
block = block->next_block;
12751269
}
12761270

1271+
pcre2_match_data_free(match_data);
12771272
return 0;
12781273
}
12791274

src/metalog.h

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@
4545
#ifdef HAVE_SYS_KLOG_H
4646
# include <sys/klog.h>
4747
#endif
48-
#include <pcre.h>
48+
#define PCRE2_CODE_UNIT_WIDTH 8
49+
#include <pcre2.h>
4950

5051
#include "helpers.h"
5152

@@ -60,11 +61,6 @@
6061
# define KLOG_FILE "/dev/klog"
6162
#endif
6263

63-
typedef struct PCREInfo_ {
64-
pcre *pcre;
65-
pcre_extra *pcre_extra;
66-
} PCREInfo;
67-
6864
typedef struct DuplicateTracker_ {
6965
char *previous_prg;
7066
char *previous_info;
@@ -118,7 +114,7 @@ typedef enum RegexSign_ {
118114
} RegexSign;
119115

120116
typedef struct RegexWithSign_ {
121-
PCREInfo regex;
117+
pcre2_code *regex;
122118
RegexSign sign;
123119
} RegexWithSign;
124120

0 commit comments

Comments
 (0)