1 /*- 2 * Copyright (c) 2006 Verdens Gang AS 3 * Copyright (c) 2006-2015 Varnish Software AS 4 * All rights reserved. 5 * 6 * Author: Martin Blix Grydeland <martin@varnish-software.com> 7 * 8 * SPDX-License-Identifier: BSD-2-Clause 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 * 31 */ 32 33 #include "vxp_tokens.h" 34 35 #define isword(c) (isalpha(c) || isdigit(c) || (c) == '_' || (c) == '-' || \ 36 (c) == '+' || (c) == '.' || (c) == '*') 37 38 #define PF(t) (int)((t)->e - (t)->b), (t)->b 39 40 /* From vex_fixed_token.c */ 41 unsigned vxp_fixed_token(const char *p, const char **q); 42 extern const char * const vxp_tnames[256]; 43 44 struct membit { 45 VTAILQ_ENTRY(membit) list; 46 void *ptr; 47 }; 48 49 struct token { 50 unsigned tok; 51 const char *b; 52 const char *e; 53 VTAILQ_ENTRY(token) list; 54 char *dec; 55 }; 56 57 struct vxp { 58 unsigned magic; 59 #define VXP_MAGIC 0x59C7F6AC 60 61 const char *b; 62 const char *e; 63 64 VTAILQ_HEAD(, token) tokens; 65 VTAILQ_HEAD(, membit) membits; 66 struct token *t; 67 68 unsigned vex_options; 69 unsigned vre_options; 70 71 struct vsb *sb; 72 int err; 73 }; 74 75 struct vex_lhs { 76 /* Left-hand-side of a vex expression. Stores the information 77 about which records and what parts of those records the 78 expression should be applied to */ 79 unsigned magic; 80 #define VEX_LHS_MAGIC 0x1AD3D78D 81 struct vbitmap *tags; 82 char *prefix; 83 int prefixlen; 84 int field; 85 int level; 86 int level_pm; 87 unsigned taglist; 88 unsigned vxid; 89 }; 90 91 enum vex_rhs_e { 92 VEX__UNSET, 93 VEX_INT, 94 VEX_FLOAT, 95 VEX_STRING, 96 VEX_REGEX, 97 }; 98 99 struct vex_rhs { 100 /* Right-hand-side of a vex expression. Stores the value that the 101 records from LHS should be matched against */ 102 unsigned magic; 103 #define VEX_RHS_MAGIC 0x3F109965 104 enum vex_rhs_e type; 105 long long val_int; 106 double val_float; 107 char *val_string; 108 size_t val_stringlen; 109 vre_t *val_regex; 110 }; 111 112 struct vex { 113 unsigned magic; 114 #define VEX_MAGIC 0xC7DB792D 115 unsigned tok; 116 unsigned options; 117 struct vex *a, *b; 118 struct vex_lhs *lhs; 119 struct vex_rhs *rhs; 120 }; 121 122 /* VXP internals */ 123 124 #define ERRCHK(tl) do { if ((tl)->err) return; } while (0) 125 #define ExpectErr(a, b) \ 126 do { vxp__Expect(a, b); ERRCHK(a); } while (0) 127 #define SkipToken(a, b) \ 128 do { vxp__Expect(a, b); ERRCHK(a); vxp_NextToken(a); } while (0) 129 130 void vxp__Expect(struct vxp *vxp, unsigned tok); 131 void vxp_ErrWhere(struct vxp *vxp, const struct token *t, int tokoff); 132 void vxp_NextToken(struct vxp *vxp); 133 void * vxp_Alloc(struct vxp *vxp, unsigned len); 134 void vxp_Lexer(struct vxp *vxp); 135 struct vex * vxp_Parse(struct vxp *vxp); 136 137 /* API internal interface */ 138 #define VEX_OPT_CASELESS (1 << 0) 139 struct vex * vex_New(const char *query, struct vsb *sb, unsigned options); 140 void vex_Free(struct vex **pvex); 141 142 /* Debug routines */ 143 #ifdef VXP_DEBUG 144 void vxp_PrintTokens(const struct vxp *vxp); 145 void vex_PrintTree(const struct vex *vex); 146 #endif /* VXP_DEBUG */ 147