vre_t * VRE_compile(const char *pattern, int options, const char **errptr, int *erroffset) { vre_t *v; *errptr = NULL; *erroffset = 0; ALLOC_OBJ(v, VRE_MAGIC); if (v == NULL) { *errptr = "Out of memory for VRE"; return (NULL); } v->re = pcre_compile(pattern, options, errptr, erroffset, NULL); if (v->re == NULL) { VRE_free(&v); return (NULL); } v->re_extra = pcre_study(v->re, VRE_STUDY_JIT_COMPILE, errptr); if (*errptr != NULL) { VRE_free(&v); return (NULL); } if (v->re_extra == NULL) { /* allocate our own */ v->re_extra = calloc(1, sizeof(pcre_extra)); v->my_extra = 1; if (v->re_extra == NULL) { *errptr = "Out of memory for pcre_extra"; VRE_free(&v); return (NULL); } } return (v); }
const char * vcc_regexp(struct vcc *tl) { char buf[BUFSIZ], *p; vre_t *t; const char *error; int erroroffset; struct inifin *ifp; Expect(tl, CSTR); if (tl->err) return (NULL); t = VRE_compile(tl->t->dec, 0, &error, &erroroffset); if (t == NULL) { VSB_printf(tl->sb, "Regexp compilation error:\n\n%s\n\n", error); vcc_ErrWhere(tl, tl->t); return (NULL); } VRE_free(&t); bprintf(buf, "VGC_re_%u", tl->unique++); p = TlAlloc(tl, strlen(buf) + 1); strcpy(p, buf); Fh(tl, 0, "static void *%s;\n", buf); ifp = New_IniFin(tl); VSB_printf(ifp->ini, "\tVRT_re_init(&%s, ",buf); EncToken(ifp->ini, tl->t); VSB_printf(ifp->ini, ");"); VSB_printf(ifp->fin, "\t\tVRT_re_fini(%s);", buf); vcc_NextToken(tl); return (p); }
void vex_Free(struct vex **pvex) { if ((*pvex)->lhs != NULL) { if ((*pvex)->lhs->tags != NULL) vbit_destroy((*pvex)->lhs->tags); if ((*pvex)->lhs->prefix != NULL) free((*pvex)->lhs->prefix); FREE_OBJ((*pvex)->lhs); } if ((*pvex)->rhs != NULL) { if ((*pvex)->rhs->val_string) free((*pvex)->rhs->val_string); if ((*pvex)->rhs->val_regex) VRE_free(&(*pvex)->rhs->val_regex); FREE_OBJ((*pvex)->rhs); } if ((*pvex)->a != NULL) { vex_Free(&(*pvex)->a); AZ((*pvex)->a); } if ((*pvex)->b != NULL) { vex_Free(&(*pvex)->b); AZ((*pvex)->b); } FREE_OBJ(*pvex); *pvex = NULL; }
static void cmd_http_expect(CMD_ARGS) { struct http *hp; const char *lhs; char *cmp; const char *rhs; vre_t *vre; const char *error; int erroroffset; int i; (void)cmd; (void)vl; CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC); assert(!strcmp(av[0], "expect")); av++; AN(av[0]); AN(av[1]); AN(av[2]); AZ(av[3]); lhs = cmd_var_resolve(hp, av[0]); cmp = av[1]; rhs = cmd_var_resolve(hp, av[2]); if (!strcmp(cmp, "==")) { if (strcmp(lhs, rhs)) vtc_log(hp->vl, 0, "EXPECT %s (%s) %s %s (%s) failed", av[0], lhs, av[1], av[2], rhs); else vtc_log(hp->vl, 4, "EXPECT %s (%s) %s %s (%s) match", av[0], lhs, av[1], av[2], rhs); } else if (!strcmp(cmp, "!=")) { if (!strcmp(lhs, rhs)) vtc_log(hp->vl, 0, "EXPECT %s (%s) %s %s (%s) failed", av[0], lhs, av[1], av[2], rhs); else vtc_log(hp->vl, 4, "EXPECT %s (%s) %s %s (%s) match", av[0], lhs, av[1], av[2], rhs); } else if (!strcmp(cmp, "~") || !strcmp(cmp, "!~")) { vre = VRE_compile(rhs, 0, &error, &erroroffset); if (vre == NULL) vtc_log(hp->vl, 0, "REGEXP error: %s (@%d) (%s)", error, erroroffset, rhs); i = VRE_exec(vre, lhs, strlen(lhs), 0, 0, NULL, 0, 0); if ((i >= 0 && *cmp == '~') || (i < 0 && *cmp == '!')) vtc_log(hp->vl, 4, "EXPECT %s (%s) %s \"%s\" match", av[0], lhs, cmp, rhs); else vtc_log(hp->vl, 0, "EXPECT %s (%s) %s \"%s\" failed", av[0], lhs, cmp, rhs); VRE_free(&vre); } else { vtc_log(hp->vl, 0, "EXPECT %s (%s) %s %s (%s) test not implemented", av[0], lhs, av[1], av[2], rhs); } }
void VRT_re_fini(void *rep) { vre_t *vv; vv = rep; if (rep != NULL) VRE_free(&vv); }
void vtc_expect(struct vtclog *vl, const char *olhs, const char *lhs, const char *cmp, const char *orhs, const char *rhs) { vre_t *vre; const char *error; int erroroffset; int i, j, retval = -1; double fl, fr; j = lhs == NULL || rhs == NULL; if (lhs == NULL) lhs = "<undef>"; if (rhs == NULL) rhs = "<undef>"; if (!strcmp(cmp, "~") || !strcmp(cmp, "!~")) { vre = VRE_compile(rhs, 0, &error, &erroroffset); 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); retval = (i >= 0 && *cmp == '~') || (i < 0 && *cmp == '!'); VRE_free(&vre); } else if (!strcmp(cmp, "==")) { retval = strcmp(lhs, rhs) == 0; } else if (!strcmp(cmp, "!=")) { retval = strcmp(lhs, rhs) != 0; } else if (j) { // fail inequality comparisons if either side is undef'ed retval = 0; } else { fl = VNUM(lhs); fr = VNUM(rhs); if (!strcmp(cmp, "<")) retval = isless(fl, fr); else if (!strcmp(cmp, ">")) retval = isgreater(fl, fr); else if (!strcmp(cmp, "<=")) retval = islessequal(fl, fr); else if (!strcmp(cmp, ">=")) retval = isgreaterequal(fl, fr); } if (retval == -1) vtc_fatal(vl, "EXPECT %s (%s) %s %s (%s) test not implemented", olhs, lhs, cmp, orhs, rhs); else if (retval == 0) vtc_fatal(vl, "EXPECT %s (%s) %s \"%s\" failed", olhs, lhs, cmp, rhs); else vtc_log(vl, 4, "EXPECT %s (%s) %s \"%s\" match", olhs, lhs, cmp, rhs); }
static void vsl_IX_free(vslf_list *list) { struct vslf *vslf; while (!VTAILQ_EMPTY(list)) { vslf = VTAILQ_FIRST(list); CHECK_OBJ_NOTNULL(vslf, VSLF_MAGIC); VTAILQ_REMOVE(list, vslf, list); AN(vslf->vre); VRE_free(&vslf->vre); AZ(vslf->vre); } }
static void vsl_IX_free(vslf_list *filters) { struct vslf *vslf; while (!VTAILQ_EMPTY(filters)) { vslf = VTAILQ_FIRST(filters); CHECK_OBJ_NOTNULL(vslf, VSLF_MAGIC); VTAILQ_REMOVE(filters, vslf, list); if (vslf->tags) vbit_destroy(vslf->tags); AN(vslf->vre); VRE_free(&vslf->vre); AZ(vslf->vre); } }
void vslq_deletequery(struct vslq_query **pquery) { struct vslq_query *query; AN(pquery); query = *pquery; *pquery = NULL; CHECK_OBJ_NOTNULL(query, VSLQ_QUERY_MAGIC); AN(query->regex); VRE_free(&query->regex); AZ(query->regex); FREE_OBJ(query); }
vre_t * VRE_compile(const char *pattern, int options, const char **errptr, int *erroffset) { vre_t *v; *errptr = NULL; *erroffset = 0; ALLOC_OBJ(v, VRE_MAGIC); if (v == NULL) return (NULL); v->re = pcre_compile(pattern, options, errptr, erroffset, NULL); if (v->re == NULL) { VRE_free(&v); return (NULL); } return (v); }
static void cmd_http_expect(CMD_ARGS) { struct http *hp; const char *lhs, *clhs; char *cmp; const char *rhs, *crhs; vre_t *vre; const char *error; int erroroffset; int i, retval = -1; (void)cmd; (void)vl; CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC); AZ(strcmp(av[0], "expect")); av++; AN(av[0]); AN(av[1]); AN(av[2]); AZ(av[3]); lhs = cmd_var_resolve(hp, av[0]); cmp = av[1]; rhs = cmd_var_resolve(hp, av[2]); clhs = lhs ? lhs : "<undef>"; crhs = rhs ? rhs : "<undef>"; if (!strcmp(cmp, "~") || !strcmp(cmp, "!~")) { vre = VRE_compile(crhs, 0, &error, &erroroffset); if (vre == NULL) vtc_log(hp->vl, 0, "REGEXP error: %s (@%d) (%s)", error, erroroffset, crhs); i = VRE_exec(vre, clhs, strlen(clhs), 0, 0, NULL, 0, 0); retval = (i >= 0 && *cmp == '~') || (i < 0 && *cmp == '!'); VRE_free(&vre); } else if (!strcmp(cmp, "==")) { retval = strcmp(clhs, crhs) == 0; } else if (!strcmp(cmp, "!=")) { retval = strcmp(clhs, crhs) != 0; } else if (lhs == NULL || rhs == NULL) { // fail inequality comparisons if either side is undef'ed retval = 0; } else if (!strcmp(cmp, "<")) { retval = isless(VNUM(lhs), VNUM(rhs)); } else if (!strcmp(cmp, ">")) { retval = isgreater(VNUM(lhs), VNUM(rhs)); } else if (!strcmp(cmp, "<=")) { retval = islessequal(VNUM(lhs), VNUM(rhs)); } else if (!strcmp(cmp, ">=")) { retval = isgreaterequal(VNUM(lhs), VNUM(rhs)); } if (retval == -1) vtc_log(hp->vl, 0, "EXPECT %s (%s) %s %s (%s) test not implemented", av[0], clhs, av[1], av[2], crhs); else vtc_log(hp->vl, retval ? 4 : 0, "EXPECT %s (%s) %s \"%s\" %s", av[0], clhs, cmp, crhs, retval ? "match" : "failed"); }