Commit 520cd8af authored by Dridi Boukelmoune's avatar Dridi Boukelmoune

vre: Replace VRE_exec() with a simpler VRE_match()

With the introduction of VRE_sub() many VRE_exec() use cases went away:

- VRE_NOTEMPTY flag
- start offset
- ovector for capture groups

The subject string length is now optional and zero can be passed to signal
that the subject string is null-terminated.

There are no options for VRE_match() but an option argument is kept in
case we start exposing some in the future.
parent 0e3667c5
......@@ -70,7 +70,7 @@ VRT_re_match(VRT_CTX, const char *s, VCL_REGEX re)
if (s == NULL)
s = "";
AN(re);
i = VRE_exec(re, s, strlen(s), 0, 0, NULL, 0, &cache_param->vre_limits);
i = VRE_match(re, s, 0, 0, &cache_param->vre_limits);
if (i >= 0)
return (1);
if (i < VRE_ERROR_NOMATCH )
......
......@@ -323,7 +323,7 @@ cmd_haproxy_cli_expect(CMD_ARGS)
vtc_fatal(hc->vl, "CLI regexp error: '%s' (@%d) (%s)",
error, erroroffset, spec);
i = VRE_exec(vre, hc->rxbuf, strlen(hc->rxbuf), 0, 0, NULL, 0, 0);
i = VRE_match(vre, hc->rxbuf, 0, 0, NULL);
VRE_free(&vre);
......
......@@ -394,8 +394,7 @@ logexp_match(const struct logexp *le, struct logexp_test *test,
if (test->vre &&
test->tag >= 0 &&
test->tag == tag &&
VRE_ERROR_NOMATCH == VRE_exec(test->vre, data,
len, 0, 0, NULL, 0, NULL))
VRE_ERROR_NOMATCH == VRE_match(test->vre, data, len, 0, NULL))
ok = 0;
alt = (test->skip_max == LE_ALT);
......
......@@ -193,10 +193,8 @@ cmd_shell_engine(struct vtclog *vl, int ok, const char *cmd,
else
vtc_log(vl, 4, "shell_expect found");
} else if (vre != NULL) {
if (VRE_exec(vre, VSB_data(vsb), VSB_len(vsb), 0, 0,
NULL, 0, NULL) < 1)
vtc_fatal(vl,
"shell_match failed: (\"%s\")", re);
if (VRE_match(vre, VSB_data(vsb), VSB_len(vsb), 0, NULL) < 1)
vtc_fatal(vl, "shell_match failed: (\"%s\")", re);
else
vtc_log(vl, 4, "shell_match succeeded");
VRE_free(&vre);
......
......@@ -103,7 +103,7 @@ vtc_expect(struct vtclog *vl,
if (vre == NULL)
vtc_fatal(vl, "REGEXP error: %s (@%d) (%s)",
error, erroroffset, rhs);
i = VRE_exec(vre, lhs, strlen(lhs), 0, 0, NULL, 0, 0);
i = VRE_match(vre, lhs, 0, 0, NULL);
retval = (i >= 0 && *cmp == '~') || (i < 0 && *cmp == '!');
VRE_free(&vre);
} else if (!strcmp(cmp, "==")) {
......
......@@ -411,7 +411,7 @@ cmd_syslog_expect(CMD_ARGS)
vtc_fatal(s->vl, "REGEXP error: '%s' (@%d) (%s)",
error, erroroffset, spec);
i = VRE_exec(vre, s->rxbuf, strlen(s->rxbuf), 0, 0, NULL, 0, 0);
i = VRE_match(vre, s->rxbuf, 0, 0, NULL);
VRE_free(&vre);
......
......@@ -755,7 +755,7 @@ varnish_cli(struct varnish *v, const char *cli, unsigned exp, const char *re)
if (exp != 0 && exp != (unsigned)u)
vtc_fatal(v->vl, "FAIL CLI response %u expected %u", u, exp);
if (vre != NULL) {
err = VRE_exec(vre, resp, strlen(resp), 0, 0, NULL, 0, NULL);
err = VRE_match(vre, resp, 0, 0, NULL);
if (err < 1)
vtc_fatal(v->vl, "Expect failed (%d)", err);
VRE_free(&vre);
......
......@@ -53,12 +53,10 @@ typedef struct vre vre_t;
/* And those to PCRE options */
extern const unsigned VRE_has_jit;
extern const unsigned VRE_CASELESS;
extern const unsigned VRE_NOTEMPTY;
vre_t *VRE_compile(const char *, unsigned, const char **, int *);
int VRE_exec(const vre_t *code, const char *subject, int length,
int startoffset, int options, int *ovector, int ovecsize,
const volatile struct vre_limits *lim);
int VRE_match(const vre_t *code, const char *subject, size_t length,
int options, const volatile struct vre_limits *lim);
int VRE_sub(const vre_t *code, const char *subject, const char *replacement,
struct vsb *vsb, const volatile struct vre_limits *lim, int all);
void VRE_free(vre_t **);
......
......@@ -69,15 +69,14 @@ struct vre {
* here.
*/
const unsigned VRE_CASELESS = PCRE_CASELESS;
const unsigned VRE_NOTEMPTY = PCRE_NOTEMPTY;
/*
* Even though we only have one for each case so far, keep track of masks
* to differentiate between compile and exec options and enfore the hard
* to differentiate between compile and match options and enfore the hard
* VRE linkage.
*/
#define VRE_MASK_COMPILE PCRE_CASELESS
#define VRE_MASK_EXEC PCRE_NOTEMPTY
#define VRE_MASK_MATCH 0
vre_t *
VRE_compile(const char *pattern, unsigned options,
......@@ -115,18 +114,14 @@ VRE_compile(const char *pattern, unsigned options,
return (v);
}
int
VRE_exec(const vre_t *code, const char *subject, int length,
static int
vre_exec(const vre_t *code, const char *subject, int length,
int startoffset, int options, int *ovector, int ovecsize,
const volatile struct vre_limits *lim)
{
CHECK_OBJ_NOTNULL(code, VRE_MAGIC);
int ov[30];
if (ovector == NULL) {
ovector = ov;
ovecsize = sizeof(ov)/sizeof(ov[0]);
}
CHECK_OBJ_NOTNULL(code, VRE_MAGIC);
AN(ovector);
if (lim != NULL) {
/* XXX: not reentrant */
......@@ -139,11 +134,27 @@ VRE_exec(const vre_t *code, const char *subject, int length,
code->re_extra->flags &= ~PCRE_EXTRA_MATCH_LIMIT_RECURSION;
}
AZ(options & (~VRE_MASK_EXEC));
return (pcre_exec(code->re, code->re_extra, subject, length,
startoffset, options, ovector, ovecsize));
}
int
VRE_match(const vre_t *code, const char *subject, size_t length,
int options, const volatile struct vre_limits *lim)
{
int ovector[30];
CHECK_OBJ_NOTNULL(code, VRE_MAGIC);
AN(subject);
AZ(options & (~VRE_MASK_MATCH));
if (length == 0)
length = strlen(subject);
return (vre_exec(code, subject, length, 0, options,
ovector, 30, lim));
}
int
VRE_sub(const vre_t *code, const char *subject, const char *replacement,
struct vsb *vsb, const volatile struct vre_limits *lim, int all)
......@@ -152,7 +163,6 @@ VRE_sub(const vre_t *code, const char *subject, const char *replacement,
int i, l;
const char *s;
unsigned x;
int options = 0;
int offset = 0;
size_t len;
......@@ -163,7 +173,7 @@ VRE_sub(const vre_t *code, const char *subject, const char *replacement,
memset(ovector, 0, sizeof(ovector));
len = strlen(subject);
i = VRE_exec(code, subject, len, 0, options, ovector, 30, lim);
i = vre_exec(code, subject, len, 0, 0, ovector, 30, lim);
if (i <= VRE_ERROR_NOMATCH)
return (i);
......@@ -189,9 +199,8 @@ VRE_sub(const vre_t *code, const char *subject, const char *replacement,
if (!all)
break;
memset(ovector, 0, sizeof(ovector));
options |= VRE_NOTEMPTY;
i = VRE_exec(code, subject, len, offset, options, ovector, 30,
lim);
i = vre_exec(code, subject, len, offset, PCRE_NOTEMPTY,
ovector, 30, lim);
if (i < VRE_ERROR_NOMATCH )
return (i);
} while (i != VRE_ERROR_NOMATCH);
......
......@@ -173,7 +173,7 @@ vsl_match_IX(struct VSL_data *vsl, const vslf_list *list,
CHECK_OBJ_NOTNULL(vslf, VSLF_MAGIC);
if (vslf->tags != NULL && !vbit_test(vslf->tags, tag))
continue;
if (VRE_exec(vslf->vre, cdata, len, 0, 0, NULL, 0, NULL) >= 0)
if (VRE_match(vslf->vre, cdata, len, 0, NULL) >= 0)
return (1);
}
return (0);
......
......@@ -264,13 +264,13 @@ vslq_test_rec(const struct vex *vex, const struct VSLC_ptr *rec)
return (0);
case '~': /* ~ */
assert(rhs->type == VEX_REGEX && rhs->val_regex != NULL);
i = VRE_exec(rhs->val_regex, b, e - b, 0, 0, NULL, 0, NULL);
i = VRE_match(rhs->val_regex, b, e - b, 0, NULL);
if (i != VRE_ERROR_NOMATCH)
return (1);
return (0);
case T_NOMATCH: /* !~ */
assert(rhs->type == VEX_REGEX && rhs->val_regex != NULL);
i = VRE_exec(rhs->val_regex, b, e - b, 0, 0, NULL, 0, NULL);
i = VRE_match(rhs->val_regex, b, e - b, 0, NULL);
if (i == VRE_ERROR_NOMATCH)
return (1);
return (0);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment