MINOR: regex: Change the struct containing regex

This change permits to remove the typedef. The original regex structs
are set in haproxy's struct.
This commit is contained in:
Thierry FOURNIER 2013-12-06 20:36:20 +01:00 committed by Willy Tarreau
parent 9645d42d74
commit 799c042daa
4 changed files with 19 additions and 18 deletions

View File

@ -29,24 +29,25 @@
#ifdef USE_PCRE #ifdef USE_PCRE
#include <pcre.h> #include <pcre.h>
#include <pcreposix.h> #include <pcreposix.h>
#else /* no PCRE */
#include <regex.h>
#endif
struct my_regex {
#ifdef USE_PCRE
#ifdef USE_PCRE_JIT #ifdef USE_PCRE_JIT
#ifndef PCRE_CONFIG_JIT #ifndef PCRE_CONFIG_JIT
#error "The PCRE lib doesn't support JIT. Change your lib, or remove the option USE_PCRE_JIT." #error "The PCRE lib doesn't support JIT. Change your lib, or remove the option USE_PCRE_JIT."
#endif #endif
struct jit_regex { pcre *reg;
pcre *reg; pcre_extra *extra;
pcre_extra *extra;
};
typedef struct jit_regex regex;
#else /* no PCRE_JIT */ #else /* no PCRE_JIT */
typedef regex_t regex; regex_t regex;
#endif #endif
#else /* no PCRE */ #else /* no PCRE */
#include <regex.h> regex_t regex;
typedef regex_t regex;
#endif #endif
};
/* what to do when a header matches a regex */ /* what to do when a header matches a regex */
#define ACT_ALLOW 0 /* allow the request */ #define ACT_ALLOW 0 /* allow the request */
@ -77,7 +78,7 @@ extern regmatch_t pmatch[MAX_MATCH];
* *
* The function return 1 is succes case, else return 0 and err is filled. * The function return 1 is succes case, else return 0 and err is filled.
*/ */
int regex_comp(const char *str, regex *regex, int cs, int cap, char **err); int regex_comp(const char *str, struct my_regex *regex, int cs, int cap, char **err);
int exp_replace(char *dst, char *src, const char *str, const regmatch_t *matches); int exp_replace(char *dst, char *src, const char *str, const regmatch_t *matches);
const char *check_replace_string(const char *str); const char *check_replace_string(const char *str);
const char *chain_regex(struct hdr_exp **head, const regex_t *preg, const char *chain_regex(struct hdr_exp **head, const regex_t *preg,
@ -87,25 +88,25 @@ const char *chain_regex(struct hdr_exp **head, const regex_t *preg,
* be writable because the function will temporarily force a zero past the * be writable because the function will temporarily force a zero past the
* last character. * last character.
*/ */
static inline int regex_exec(const regex *preg, char *subject, int length) { static inline int regex_exec(const struct my_regex *preg, char *subject, int length) {
#ifdef USE_PCRE_JIT #ifdef USE_PCRE_JIT
return pcre_exec(preg->reg, preg->extra, subject, length, 0, 0, NULL, 0); return pcre_exec(preg->reg, preg->extra, subject, length, 0, 0, NULL, 0);
#else #else
int match; int match;
char old_char = subject[length]; char old_char = subject[length];
subject[length] = 0; subject[length] = 0;
match = regexec(preg, subject, 0, NULL, 0); match = regexec(&preg->regex, subject, 0, NULL, 0);
subject[length] = old_char; subject[length] = old_char;
return match; return match;
#endif #endif
} }
static inline void regex_free(regex *preg) { static inline void regex_free(struct my_regex *preg) {
#ifdef USE_PCRE_JIT #ifdef USE_PCRE_JIT
pcre_free_study(preg->extra); pcre_free_study(preg->extra);
pcre_free(preg->reg); pcre_free(preg->reg);
#else #else
regfree(preg); regfree(&preg->regex);
#endif #endif
} }

View File

@ -135,7 +135,7 @@ struct pattern {
union { union {
void *ptr; /* any data */ void *ptr; /* any data */
char *str; /* any string */ char *str; /* any string */
regex *reg; /* a compiled regex */ struct my_regex *reg; /* a compiled regex */
} ptr; /* indirect values, allocated */ } ptr; /* indirect values, allocated */
void(*freeptrbuf)(void *ptr); /* a destructor able to free objects from the ptr */ void(*freeptrbuf)(void *ptr); /* a destructor able to free objects from the ptr */
int len; /* data length when required */ int len; /* data length when required */

View File

@ -476,7 +476,7 @@ static void pat_free_reg(void *ptr)
/* Parse a regex. It is allocated. */ /* Parse a regex. It is allocated. */
int pat_parse_reg(const char **text, struct pattern *pattern, int *opaque, char **err) int pat_parse_reg(const char **text, struct pattern *pattern, int *opaque, char **err)
{ {
regex *preg; struct my_regex *preg;
preg = calloc(1, sizeof(*preg)); preg = calloc(1, sizeof(*preg));

View File

@ -122,7 +122,7 @@ const char *chain_regex(struct hdr_exp **head, const regex_t *preg,
return NULL; return NULL;
} }
int regex_comp(const char *str, regex *regex, int cs, int cap, char **err) int regex_comp(const char *str, struct my_regex *regex, int cs, int cap, char **err)
{ {
#ifdef USE_PCRE_JIT #ifdef USE_PCRE_JIT
int flags = 0; int flags = 0;
@ -154,7 +154,7 @@ int regex_comp(const char *str, regex *regex, int cs, int cap, char **err)
if (!cap) if (!cap)
flags |= REG_NOSUB; flags |= REG_NOSUB;
if (regcomp(regex, str, flags) != 0) { if (regcomp(&regex->regex, str, flags) != 0) {
memprintf(err, "regex '%s' is invalid", str); memprintf(err, "regex '%s' is invalid", str);
return 0; return 0;
} }