Skip to content

Commit

Permalink
port to PCRE2
Browse files Browse the repository at this point in the history
  • Loading branch information
jovanlanik committed Feb 12, 2022
1 parent 211c137 commit a693557
Show file tree
Hide file tree
Showing 6 changed files with 228 additions and 232 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

.deps
.libs
.cache

*.diff
*.orig
Expand All @@ -28,6 +29,7 @@ Makefile
Makefile.in
missing
stamp-h1
compile_commands.json

/metalog.lsm
/metalog.spec
Expand Down
322 changes: 160 additions & 162 deletions INSTALL

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ AC_CHECK_FUNCS_ONCE(m4_flatten([
setprogname
]))

dnl PCRE
dnl PCRE2

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

dnl Options

Expand Down
15 changes: 9 additions & 6 deletions src/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@
#define errf(fmt, args...) _err(warnf, fmt, ## args)
#define errp(fmt, args...) _err(warnp, fmt , ## args)

static pcre *wpcre_compile(const char *pattern, int options)
static pcre2_code *wpcre2_compile(const char *pattern, uint32_t options)
{
const char *errptr;
int erroffset;
pcre *ret = pcre_compile(pattern, options, &errptr, &erroffset, NULL);
if (!ret)
warn("Invalid regex [%s] at %d: %s", pattern, erroffset, errptr);
int errcode;
PCRE2_SIZE erroffset;
pcre2_code *ret = pcre2_compile((PCRE2_SPTR)pattern, PCRE2_ZERO_TERMINATED, options, &errcode, &erroffset, NULL);
if (!ret) {
PCRE2_UCHAR buffer[256];
pcre2_get_error_message(errcode, buffer, sizeof(buffer));
warn("Invalid regex [%s] at %zu: %s", pattern, erroffset, buffer);
}
return ret;
}

Expand Down
103 changes: 50 additions & 53 deletions src/metalog.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ static int doLog(const char * fmt, ...);
*/
static int parseLine(char * const line, ConfigBlock **cur_block,
const ConfigBlock * const default_block,
pcre * const re_newblock, pcre * const re_newstmt,
pcre * const re_comment, pcre * const re_null)
pcre2_code * const re_newblock, pcre2_code * const re_newstmt,
pcre2_code * const re_comment, pcre2_code * const re_null)
{
int ovector[16];
pcre *new_regex;
const char *keyword;
const char *value;
pcre2_match_data *match_data = pcre2_match_data_create(16, NULL);
pcre2_code *new_regex;
char *keyword;
char *value;
int line_size;
int stcount;

Expand All @@ -41,14 +41,14 @@ static int parseLine(char * const line, ConfigBlock **cur_block,
}
line[--line_size] = 0;
}
if (pcre_exec(re_null, NULL, line, line_size,
0, 0, ovector, ARRAY_SIZE(ovector)) >= 0 ||
pcre_exec(re_comment, NULL, line, line_size,
0, 0, ovector, ARRAY_SIZE(ovector)) >= 0) {
if (pcre2_match(re_null, (PCRE2_SPTR)line, (PCRE2_SIZE)line_size,
0, 0, match_data, NULL) >= 0 ||
pcre2_match(re_comment, (PCRE2_SPTR)line, (PCRE2_SIZE)line_size,
0, 0, match_data, NULL) >= 0) {
return 0;
}
if (pcre_exec(re_newblock, NULL, line, line_size,
0, 0, ovector, ARRAY_SIZE(ovector)) >= 0) {
if (pcre2_match(re_newblock, (PCRE2_SPTR)line, (PCRE2_SIZE)line_size,
0, 0, match_data, NULL) >= 0) {
ConfigBlock *previous_block = *cur_block;

if ((*cur_block = wmalloc(sizeof **cur_block)) == NULL)
Expand All @@ -62,10 +62,11 @@ static int parseLine(char * const line, ConfigBlock **cur_block,
return 0;
}
if ((stcount =
pcre_exec(re_newstmt, NULL, line, line_size,
0, 0, ovector, ARRAY_SIZE(ovector))) >= 3) {
pcre_get_substring(line, ovector, stcount, 1, &keyword);
pcre_get_substring(line, ovector, stcount, 2, &value);
pcre2_match(re_newstmt, (PCRE2_SPTR)line, (PCRE2_SIZE)line_size,
0, 0, match_data, NULL)) >= 3) {
PCRE2_SIZE len;
pcre2_substring_get_bynumber(match_data, 1, (PCRE2_UCHAR **)&keyword, &len);
pcre2_substring_get_bynumber(match_data, 2, (PCRE2_UCHAR **)&value, &len);
if (strcasecmp(keyword, "minimum") == 0) {
(*cur_block)->minimum = atoi(value);
} else if (strcasecmp(keyword, "maximum") == 0) {
Expand Down Expand Up @@ -112,7 +113,7 @@ static int parseLine(char * const line, ConfigBlock **cur_block,
return -3;
}
(*cur_block)->regexeswithsign = new_regexeswithsign;
if ((new_regex = wpcre_compile(regex, PCRE_CASELESS)) == NULL) {
if ((new_regex = wpcre2_compile(regex, PCRE2_CASELESS)) == NULL) {
free(regex);
return -5;
}
Expand All @@ -127,9 +128,7 @@ static int parseLine(char * const line, ConfigBlock **cur_block,
} else {
this_regex->sign = REGEX_SIGN_POSITIVE;
}
this_regex->regex.pcre = new_regex;
this_regex->regex.pcre_extra =
pcre_study(new_regex, 0, &errptr);
this_regex->regex = new_regex;
}
(*cur_block)->nb_regexes++;
} else if (strcasecmp(keyword, "program_regex") == 0 ||
Expand All @@ -148,7 +147,7 @@ static int parseLine(char * const line, ConfigBlock **cur_block,
return -3;
}
(*cur_block)->program_regexeswithsign = new_regexeswithsign;
if ((new_regex = wpcre_compile(regex, PCRE_CASELESS)) == NULL) {
if ((new_regex = wpcre2_compile(regex, PCRE2_CASELESS)) == NULL) {
free(regex);
return -5;
}
Expand All @@ -164,9 +163,7 @@ static int parseLine(char * const line, ConfigBlock **cur_block,
} else {
this_regex->sign = REGEX_SIGN_POSITIVE;
}
this_regex->regex.pcre = new_regex;
this_regex->regex.pcre_extra =
pcre_study(new_regex, 0, &errptr);
this_regex->regex = new_regex;
}
(*cur_block)->program_nb_regexes++;
} else if (strcasecmp(keyword, "maxsize") == 0) {
Expand Down Expand Up @@ -336,18 +333,21 @@ static int parseLine(char * const line, ConfigBlock **cur_block,
}
} else
err("Unknown keyword '%s'! line: %s", keyword, line);
pcre2_substring_free((PCRE2_UCHAR *)keyword);
pcre2_substring_free((PCRE2_UCHAR *)value);
}
pcre2_match_data_free(match_data);
return 0;
}

static int configParser(const char * const file)
{
char line[LINE_MAX];
FILE *fp;
pcre *re_newblock;
pcre *re_newstmt;
pcre *re_comment;
pcre *re_null;
pcre2_code *re_newblock;
pcre2_code *re_newstmt;
pcre2_code *re_comment;
pcre2_code *re_null;
int retcode = 0;
ConfigBlock default_block = {
DEFAULT_MINIMUM, /* minimum */
Expand Down Expand Up @@ -381,10 +381,10 @@ static int configParser(const char * const file)
warnp("Can't open the configuration file");
return -2;
}
re_newblock = wpcre_compile(":\\s*$", 0);
re_newstmt = wpcre_compile("^\\s*(.+?)\\s*=\\s*\"?([^\"]*)\"?\\s*$", 0);
re_comment = wpcre_compile("^\\s*#", 0);
re_null = wpcre_compile("^\\s*$", 0);
re_newblock = wpcre2_compile(":\\s*$", 0);
re_newstmt = wpcre2_compile("^\\s*(.+?)\\s*=\\s*\"?([^\"]*)\"?\\s*$", 0);
re_comment = wpcre2_compile("^\\s*#", 0);
re_null = wpcre2_compile("^\\s*$", 0);
if (!re_newblock || !re_newstmt || !re_comment || !re_null) {
retcode = -1;
goto rtn;
Expand All @@ -398,16 +398,16 @@ static int configParser(const char * const file)
}
rtn:
if (re_newblock != NULL) {
pcre_free(re_newblock);
pcre2_code_free(re_newblock);
}
if (re_newstmt != NULL) {
pcre_free(re_newstmt);
pcre2_code_free(re_newstmt);
}
if (re_comment != NULL) {
pcre_free(re_comment);
pcre2_code_free(re_comment);
}
if (re_null != NULL) {
pcre_free(re_null);
pcre2_code_free(re_null);
}
fclose(fp);

Expand Down Expand Up @@ -1146,7 +1146,7 @@ static int log_stdout(const char * const date,
static int processLogLine(const int logcode,
const char * const prg, char * const info)
{
int ovector[16];
pcre2_match_data *match_data = pcre2_match_data_create(16, NULL);
ConfigBlock *block = config_blocks;
RegexWithSign *this_regex;
const int facility = LOG_FAC(logcode);
Expand Down Expand Up @@ -1184,17 +1184,15 @@ static int processLogLine(const int logcode,
this_regex = block->program_regexeswithsign;
do {
if (this_regex->sign == REGEX_SIGN_POSITIVE) {
if (pcre_exec(this_regex->regex.pcre,
this_regex->regex.pcre_extra,
prg, prg_len, 0, 0, ovector,
ARRAY_SIZE(ovector)) >= 0) {
if (pcre2_match(this_regex->regex,
(PCRE2_SPTR)prg, (PCRE2_SIZE)prg_len,
0, 0, match_data, NULL) >= 0) {
regex_result = 1;
}
} else {
if (pcre_exec(this_regex->regex.pcre,
this_regex->regex.pcre_extra,
prg, prg_len, 0, 0, ovector,
ARRAY_SIZE(ovector)) < 0) {
if (pcre2_match(this_regex->regex,
(PCRE2_SPTR)prg, (PCRE2_SIZE)prg_len,
0, 0, match_data, NULL) < 0) {
regex_result = 1;
}
}
Expand All @@ -1213,17 +1211,15 @@ static int processLogLine(const int logcode,
this_regex = block->regexeswithsign;
do {
if (this_regex->sign == REGEX_SIGN_POSITIVE) {
if (pcre_exec(this_regex->regex.pcre,
this_regex->regex.pcre_extra,
info, info_len, 0, 0, ovector,
ARRAY_SIZE(ovector)) >= 0) {
if (pcre2_match(this_regex->regex,
(PCRE2_SPTR)info, (PCRE2_SIZE)info_len,
0, 0, match_data, NULL) >= 0) {
regex_result = 1;
}
} else {
if (pcre_exec(this_regex->regex.pcre,
this_regex->regex.pcre_extra,
info, info_len, 0, 0, ovector,
ARRAY_SIZE(ovector)) < 0) {
if (pcre2_match(this_regex->regex,
(PCRE2_SPTR)info, (PCRE2_SIZE)info_len,
0, 0, match_data, NULL) < 0) {
regex_result = 1;
}
}
Expand Down Expand Up @@ -1274,6 +1270,7 @@ static int processLogLine(const int logcode,
block = block->next_block;
}

pcre2_match_data_free(match_data);
return 0;
}

Expand Down
10 changes: 3 additions & 7 deletions src/metalog.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@
#ifdef HAVE_SYS_KLOG_H
# include <sys/klog.h>
#endif
#include <pcre.h>
#define PCRE2_CODE_UNIT_WIDTH 8
#include <pcre2.h>

#include "helpers.h"

Expand All @@ -60,11 +61,6 @@
# define KLOG_FILE "/dev/klog"
#endif

typedef struct PCREInfo_ {
pcre *pcre;
pcre_extra *pcre_extra;
} PCREInfo;

typedef struct DuplicateTracker_ {
char *previous_prg;
char *previous_info;
Expand Down Expand Up @@ -118,7 +114,7 @@ typedef enum RegexSign_ {
} RegexSign;

typedef struct RegexWithSign_ {
PCREInfo regex;
pcre2_code *regex;
RegexSign sign;
} RegexWithSign;

Expand Down

0 comments on commit a693557

Please sign in to comment.