| | varnish-cache/lib/libvcc/vcc_types.c |
| 0 |
|
/*- |
| 1 |
|
* Copyright (c) 2006 Verdens Gang AS |
| 2 |
|
* Copyright (c) 2006-2011 Varnish Software AS |
| 3 |
|
* All rights reserved. |
| 4 |
|
* |
| 5 |
|
* Author: Poul-Henning Kamp <phk@phk.freebsd.dk> |
| 6 |
|
* |
| 7 |
|
* SPDX-License-Identifier: BSD-2-Clause |
| 8 |
1203960 |
* |
| 9 |
1187600 |
* Redistribution and use in source and binary forms, with or without |
| 10 |
1138200 |
* modification, are permitted provided that the following conditions |
| 11 |
1072840 |
* are met: |
| 12 |
1072440 |
* 1. Redistributions of source code must retain the above copyright |
| 13 |
961240 |
* notice, this list of conditions and the following disclaimer. |
| 14 |
938360 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 15 |
900160 |
* notice, this list of conditions and the following disclaimer in the |
| 16 |
824360 |
* documentation and/or other materials provided with the distribution. |
| 17 |
823240 |
* |
| 18 |
822000 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
| 19 |
822000 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 |
741200 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 |
716680 |
* ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE |
| 22 |
715320 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 23 |
672280 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 24 |
666120 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 25 |
662280 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 26 |
624400 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 27 |
385960 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 28 |
385960 |
* SUCH DAMAGE. |
| 29 |
374440 |
* |
| 30 |
349200 |
*/ |
| 31 |
349200 |
|
| 32 |
|
#include "config.h" |
| 33 |
|
|
| 34 |
|
#include <string.h> |
| 35 |
|
|
| 36 |
|
#include "vcc_compile.h" |
| 37 |
|
|
| 38 |
|
/* |
| 39 |
|
* A type attribute is information already existing, requiring no processing |
| 40 |
|
* or resource usage. |
| 41 |
|
* |
| 42 |
|
* A type method is a call and may do significant processing, change things, |
| 43 |
|
* eat workspace etc. |
| 44 |
|
* |
| 45 |
|
* XXX: type methods might move in a more comprehensive direction. |
| 46 |
|
*/ |
| 47 |
|
struct vcc_method { |
| 48 |
|
unsigned magic; |
| 49 |
|
#define VCC_METHOD_MAGIC 0x594108cd |
| 50 |
|
vcc_type_t type; |
| 51 |
|
const char *name; |
| 52 |
|
const char *impl; |
| 53 |
|
int func; |
| 54 |
|
}; |
| 55 |
|
|
| 56 |
|
const struct type ACL[1] = {{ |
| 57 |
|
.magic = TYPE_MAGIC, |
| 58 |
|
.name = "ACL", |
| 59 |
|
.global_pfx = "vrt_acl", |
| 60 |
|
.tostring = "((\v1)->name)", |
| 61 |
|
}}; |
| 62 |
|
|
| 63 |
|
static const struct vcc_method backend_methods[] = { |
| 64 |
|
{ VCC_METHOD_MAGIC, BACKEND, "resolve", |
| 65 |
|
"VRT_DirectorResolve(ctx, \v1)", 1 }, |
| 66 |
|
{ VCC_METHOD_MAGIC, NULL }, |
| 67 |
|
}; |
| 68 |
|
|
| 69 |
|
static struct symbol default_backend[1] = {{ |
| 70 |
|
.magic = SYMBOL_MAGIC, |
| 71 |
|
.name = "default", |
| 72 |
|
.lorev = VCL_LOW, |
| 73 |
|
.hirev = VCL_HIGH, |
| 74 |
|
.kind = SYM_BACKEND, |
| 75 |
|
.type = DEFAULT, |
| 76 |
|
.rname = "*(VCL_conf.default_director)", |
| 77 |
|
}}; |
| 78 |
|
|
| 79 |
|
const struct type BACKEND[1] = {{ |
| 80 |
|
.magic = TYPE_MAGIC, |
| 81 |
|
.name = "BACKEND", |
| 82 |
|
.methods = backend_methods, |
| 83 |
|
.global_pfx = "vgc_backend", |
| 84 |
|
.tostring = "VRT_BACKEND_string(\v1)", |
| 85 |
|
.default_sym = default_backend, |
| 86 |
|
}}; |
| 87 |
|
|
| 88 |
|
const struct type BLOB[1] = {{ |
| 89 |
|
.magic = TYPE_MAGIC, |
| 90 |
|
.name = "BLOB", |
| 91 |
|
.bodyform = 1, |
| 92 |
|
.tostring = "VRT_BLOB_string(ctx, \v1)", |
| 93 |
|
}}; |
| 94 |
|
|
| 95 |
|
const struct type BODY[1] = {{ |
| 96 |
|
.magic = TYPE_MAGIC, |
| 97 |
|
.name = "BODY", |
| 98 |
|
.noindent = 1, |
| 99 |
|
}}; |
| 100 |
|
|
| 101 |
|
const struct type BOOL[1] = {{ |
| 102 |
|
.magic = TYPE_MAGIC, |
| 103 |
|
.name = "BOOL", |
| 104 |
|
.tostring = "VRT_BOOL_string(\v1)", |
| 105 |
|
}}; |
| 106 |
|
|
| 107 |
|
const struct type BYTES[1] = {{ |
| 108 |
|
.magic = TYPE_MAGIC, |
| 109 |
|
.name = "BYTES", |
| 110 |
|
.tostring = "VRT_INT_string(ctx, \v1)", |
| 111 |
|
.multype = REAL, // XXX: wrong |
| 112 |
|
}}; |
| 113 |
|
|
| 114 |
|
const struct type DEFAULT[1] = {{ |
| 115 |
|
.magic = TYPE_MAGIC, |
| 116 |
|
.name = "DEFAULT", |
| 117 |
|
}}; |
| 118 |
|
|
| 119 |
|
const struct type DURATION[1] = {{ |
| 120 |
|
.magic = TYPE_MAGIC, |
| 121 |
|
.name = "DURATION", |
| 122 |
|
.tostring = "VRT_REAL_string(ctx, \v1)", // XXX 's' suff? |
| 123 |
|
.multype = REAL, |
| 124 |
|
}}; |
| 125 |
|
|
| 126 |
|
const struct type ENUM[1] = {{ |
| 127 |
|
.magic = TYPE_MAGIC, |
| 128 |
|
.name = "ENUM", |
| 129 |
|
.tostring = "", |
| 130 |
|
}}; |
| 131 |
|
|
| 132 |
|
const struct type HEADER[1] = {{ |
| 133 |
|
.magic = TYPE_MAGIC, |
| 134 |
|
.name = "HEADER", |
| 135 |
|
.tostring = "VRT_GetHdr(ctx, \v1)", |
| 136 |
|
}}; |
| 137 |
|
|
| 138 |
|
const struct type HTTP[1] = {{ |
| 139 |
|
.magic = TYPE_MAGIC, |
| 140 |
|
.name = "HTTP", |
| 141 |
|
}}; |
| 142 |
|
|
| 143 |
|
const struct type INSTANCE[1] = {{ |
| 144 |
|
.magic = TYPE_MAGIC, |
| 145 |
|
.name = "INSTANCE", |
| 146 |
|
.global_pfx = "vo", |
| 147 |
|
}}; |
| 148 |
|
|
| 149 |
|
const struct type INT[1] = {{ |
| 150 |
|
.magic = TYPE_MAGIC, |
| 151 |
|
.name = "INT", |
| 152 |
|
.multype = INT, |
| 153 |
|
.tostring = "VRT_INT_string(ctx, \v1)", |
| 154 |
|
}}; |
| 155 |
|
|
| 156 |
|
const struct type IP[1] = {{ |
| 157 |
|
.magic = TYPE_MAGIC, |
| 158 |
|
.name = "IP", |
| 159 |
|
.tostring = "VRT_IP_string(ctx, \v1)", |
| 160 |
|
}}; |
| 161 |
|
|
| 162 |
|
static struct symbol default_probe[1] = {{ |
| 163 |
|
.magic = SYMBOL_MAGIC, |
| 164 |
|
.name = "default", |
| 165 |
|
.lorev = VCL_LOW, |
| 166 |
|
.hirev = VCL_HIGH, |
| 167 |
|
.kind = SYM_PROBE, |
| 168 |
|
.type = DEFAULT, |
| 169 |
|
.rname = "vgc_probe_default", |
| 170 |
|
}}; |
| 171 |
|
|
| 172 |
|
const struct type PROBE[1] = {{ |
| 173 |
|
.magic = TYPE_MAGIC, |
| 174 |
|
.name = "PROBE", |
| 175 |
|
.global_pfx = "vgc_probe", |
| 176 |
|
.default_sym = default_probe, |
| 177 |
|
}}; |
| 178 |
|
|
| 179 |
|
const struct type REAL[1] = {{ |
| 180 |
|
.magic = TYPE_MAGIC, |
| 181 |
|
.name = "REAL", |
| 182 |
|
.tostring = "VRT_REAL_string(ctx, \v1)", |
| 183 |
|
.multype = REAL, |
| 184 |
|
}}; |
| 185 |
|
|
| 186 |
|
const struct type REGEX[1] = {{ |
| 187 |
|
.magic = TYPE_MAGIC, |
| 188 |
|
.name = "REGEX", |
| 189 |
|
}}; |
| 190 |
|
|
| 191 |
|
static const struct vcc_method stevedore_methods[] = { |
| 192 |
|
#define VRTSTVVAR(nm, vtype, ctype, dval) \ |
| 193 |
|
{ VCC_METHOD_MAGIC, vtype, #nm, "VRT_stevedore_" #nm "(\v1)", 0}, |
| 194 |
|
#include "tbl/vrt_stv_var.h" |
| 195 |
|
{ VCC_METHOD_MAGIC, NULL }, |
| 196 |
|
}; |
| 197 |
|
|
| 198 |
|
const struct type STEVEDORE[1] = {{ |
| 199 |
|
.magic = TYPE_MAGIC, |
| 200 |
|
.name = "STEVEDORE", |
| 201 |
|
.methods = stevedore_methods, |
| 202 |
|
.tostring = "VRT_STEVEDORE_string(\v1)", |
| 203 |
|
}}; |
| 204 |
|
|
| 205 |
|
const struct type STRING[1] = {{ |
| 206 |
|
.magic = TYPE_MAGIC, |
| 207 |
|
.name = "STRING", |
| 208 |
|
.stringform = 1, |
| 209 |
|
}}; |
| 210 |
|
|
| 211 |
|
const struct type STRANDS[1] = {{ |
| 212 |
|
.magic = TYPE_MAGIC, |
| 213 |
|
.name = "STRANDS", |
| 214 |
|
.stringform = 1, |
| 215 |
|
.tostring = "VRT_STRANDS_string(ctx,\v+\n\v1\v-\n)", |
| 216 |
|
}}; |
| 217 |
|
|
| 218 |
|
static const struct vcc_method strings_methods[] = { |
| 219 |
|
{ VCC_METHOD_MAGIC, STRING, "upper", |
| 220 |
|
"VRT_UpperLowerStrands(ctx, \vT, 1)", 1 }, |
| 221 |
|
{ VCC_METHOD_MAGIC, STRING, "lower", |
| 222 |
|
"VRT_UpperLowerStrands(ctx, \vT, 0)", 1 }, |
| 223 |
|
{ VCC_METHOD_MAGIC, NULL }, |
| 224 |
|
}; |
| 225 |
|
|
| 226 |
|
const struct type STRINGS[1] = {{ |
| 227 |
|
.magic = TYPE_MAGIC, |
| 228 |
|
.name = "STRINGS", |
| 229 |
|
.methods = strings_methods, |
| 230 |
|
.bodyform = 1, |
| 231 |
|
.tostring = "", |
| 232 |
|
}}; |
| 233 |
|
|
| 234 |
|
const struct type SUB[1] = {{ |
| 235 |
|
.magic = TYPE_MAGIC, |
| 236 |
|
.name = "SUB", |
| 237 |
|
.global_pfx = "VGC_function", |
| 238 |
|
}}; |
| 239 |
|
|
| 240 |
|
const struct type TIME[1] = {{ |
| 241 |
|
.magic = TYPE_MAGIC, |
| 242 |
|
.name = "TIME", |
| 243 |
|
.tostring = "VRT_TIME_string(ctx, \v1)", |
| 244 |
|
}}; |
| 245 |
|
|
| 246 |
|
const struct type VCL[1] = {{ |
| 247 |
|
.magic = TYPE_MAGIC, |
| 248 |
|
.name = "VCL", |
| 249 |
|
}}; |
| 250 |
|
|
| 251 |
|
const struct type VOID[1] = {{ |
| 252 |
|
.magic = TYPE_MAGIC, |
| 253 |
|
.name = "VOID", |
| 254 |
|
}}; |
| 255 |
|
|
| 256 |
|
vcc_type_t |
| 257 |
1203960 |
VCC_Type(const char *p) |
| 258 |
|
{ |
| 259 |
|
|
| 260 |
|
#define VCC_TYPE(UC, lc) if (!strcmp(p, #UC)) return (UC); |
| 261 |
|
#include "vcc_types.h" |
| 262 |
|
return (NULL); |
| 263 |
|
} |
| 264 |
|
|
| 265 |
|
static void |
| 266 |
2972880 |
vcc_type_init(struct vcc *tl, vcc_type_t type) |
| 267 |
|
{ |
| 268 |
|
const struct vcc_method *vm; |
| 269 |
|
struct symbol *sym; |
| 270 |
|
struct vsb *buf; |
| 271 |
|
|
| 272 |
|
/* NB: Don't bother even creating a type symbol if there are no |
| 273 |
|
* methods attached to it. |
| 274 |
|
*/ |
| 275 |
2972880 |
if (type->methods == NULL) |
| 276 |
2601270 |
return; |
| 277 |
|
|
| 278 |
371610 |
buf = VSB_new_auto(); |
| 279 |
371610 |
AN(buf); |
| 280 |
371610 |
AN(VCC_MkSym(tl, type->name, SYM_TYPE, SYM_NONE, VCL_LOW, VCL_HIGH)); |
| 281 |
|
|
| 282 |
1114830 |
for (vm = type->methods; vm->type != NULL; vm++) { |
| 283 |
743220 |
VSB_printf(buf, "%s.%s", type->name, vm->name); |
| 284 |
743220 |
AZ(VSB_finish(buf)); |
| 285 |
743220 |
sym = VCC_MkSym(tl, VSB_data(buf), SYM_TYPE, SYM_METHOD, |
| 286 |
|
VCL_LOW, VCL_HIGH); |
| 287 |
743220 |
VSB_clear(buf); |
| 288 |
743220 |
if (tl->err) |
| 289 |
0 |
break; |
| 290 |
743220 |
AN(sym); |
| 291 |
743220 |
sym->type = vm->type; |
| 292 |
743220 |
sym->eval = vcc_Eval_TypeMethod; |
| 293 |
743220 |
sym->eval_priv = vm; |
| 294 |
743220 |
} |
| 295 |
|
|
| 296 |
371610 |
VSB_destroy(&buf); |
| 297 |
2972880 |
} |
| 298 |
|
|
| 299 |
|
const char * |
| 300 |
115275 |
VCC_Type_EvalMethod(struct vcc *tl, const struct symbol *sym) |
| 301 |
|
{ |
| 302 |
|
const struct vcc_method *vm; |
| 303 |
|
|
| 304 |
115275 |
AN(sym); |
| 305 |
115275 |
AN(sym->kind == SYM_METHOD); |
| 306 |
115275 |
CAST_OBJ_NOTNULL(vm, sym->eval_priv, VCC_METHOD_MAGIC); |
| 307 |
|
|
| 308 |
115275 |
vcc_NextToken(tl); |
| 309 |
115275 |
if (vm->func) { |
| 310 |
114915 |
Expect(tl, '('); |
| 311 |
114915 |
if (tl->err) |
| 312 |
0 |
return (NULL); |
| 313 |
114915 |
vcc_NextToken(tl); |
| 314 |
114915 |
Expect(tl, ')'); |
| 315 |
114915 |
if (tl->err) |
| 316 |
0 |
return (NULL); |
| 317 |
114915 |
vcc_NextToken(tl); |
| 318 |
114915 |
} |
| 319 |
|
|
| 320 |
115275 |
return (vm->impl); |
| 321 |
115275 |
} |
| 322 |
|
|
| 323 |
|
void |
| 324 |
123870 |
vcc_Type_Init(struct vcc *tl) |
| 325 |
|
{ |
| 326 |
|
|
| 327 |
|
#define VCC_TYPE(UC, lc) vcc_type_init(tl, UC); |
| 328 |
|
#include "vcc_types.h" |
| 329 |
|
} |