From 50949b36ea354cff13e3c67e77c7fc3f88fd760d Mon Sep 17 00:00:00 2001 From: phk Date: Sun, 1 Apr 2007 09:34:28 +0000 Subject: [PATCH] Move backend parsing into a separate file. Eliminate a bunch of of unnecessary #includes. git-svn-id: svn+ssh://projects.linpro.no/svn/varnish/trunk@1300 d4fa192b-c00b-0410-8231-f00ffab90ce4 --- varnish-cache/lib/libvcl/Makefile.am | 1 + varnish-cache/lib/libvcl/flint.lnt | 2 +- varnish-cache/lib/libvcl/vcc_acl.c | 14 -- varnish-cache/lib/libvcl/vcc_action.c | 16 --- varnish-cache/lib/libvcl/vcc_backend.c | 186 +++++++++++++++++++++++++ varnish-cache/lib/libvcl/vcc_compile.c | 9 +- varnish-cache/lib/libvcl/vcc_compile.h | 3 + varnish-cache/lib/libvcl/vcc_parse.c | 160 +-------------------- varnish-cache/lib/libvcl/vcc_token.c | 3 - 9 files changed, 194 insertions(+), 200 deletions(-) create mode 100644 varnish-cache/lib/libvcl/vcc_backend.c diff --git a/varnish-cache/lib/libvcl/Makefile.am b/varnish-cache/lib/libvcl/Makefile.am index 263156f6..ecf71698 100644 --- a/varnish-cache/lib/libvcl/Makefile.am +++ b/varnish-cache/lib/libvcl/Makefile.am @@ -11,6 +11,7 @@ libvcl_la_SOURCES = \ \ vcc_acl.c \ vcc_action.c \ + vcc_backend.c \ vcc_compile.c \ vcc_parse.c \ vcc_fixed_token.c \ diff --git a/varnish-cache/lib/libvcl/flint.lnt b/varnish-cache/lib/libvcl/flint.lnt index 94b2c7b2..35910f42 100644 --- a/varnish-cache/lib/libvcl/flint.lnt +++ b/varnish-cache/lib/libvcl/flint.lnt @@ -42,7 +42,7 @@ -e785 // Too few initializers for aggregate --e766 // Header file '../../include/libvarnish.h' not used in module +// -e766 // Header file '../../include/libvarnish.h' not used in module -e773 // Expression-like macro 'VCL_FARGS' not parenthesized diff --git a/varnish-cache/lib/libvcl/vcc_acl.c b/varnish-cache/lib/libvcl/vcc_acl.c index b743ce5a..5c2c02d8 100644 --- a/varnish-cache/lib/libvcl/vcc_acl.c +++ b/varnish-cache/lib/libvcl/vcc_acl.c @@ -29,27 +29,13 @@ * $Id$ */ -#include -#include -#include - -#include -#include -#include -#include #include -#include -#include -#include -#include #include "vsb.h" #include "vcc_priv.h" #include "vcc_compile.h" -#include "libvcl.h" - void vcc_Cond_Ip(struct var *vp, struct tokenlist *tl) { diff --git a/varnish-cache/lib/libvcl/vcc_action.c b/varnish-cache/lib/libvcl/vcc_action.c index d3a5a008..bf1acc67 100644 --- a/varnish-cache/lib/libvcl/vcc_action.c +++ b/varnish-cache/lib/libvcl/vcc_action.c @@ -29,30 +29,14 @@ * $Id$ */ -#include -#include -#include - #include -#include -#include -#include -#include #include -#include -#include -#include -#include -#include -#include "compat/asprintf.h" #include "vsb.h" #include "vcc_priv.h" #include "vcc_compile.h" -#include "vrt.h" -#include "libvcl.h" /*--------------------------------------------------------------------*/ diff --git a/varnish-cache/lib/libvcl/vcc_backend.c b/varnish-cache/lib/libvcl/vcc_backend.c new file mode 100644 index 00000000..b73385cf --- /dev/null +++ b/varnish-cache/lib/libvcl/vcc_backend.c @@ -0,0 +1,186 @@ +/*- + * Copyright (c) 2006 Verdens Gang AS + * Copyright (c) 2006 Linpro AS + * All rights reserved. + * + * Author: Poul-Henning Kamp + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $Id$ + */ + +#include +#include + +#include +#include +#include +#include + +#include "vsb.h" + +#include "vcc_priv.h" +#include "vcc_compile.h" + + +static const char * +CheckHostPort(const char *host, const char *port) +{ + struct addrinfo *res, hint; + int error; + + memset(&hint, 0, sizeof hint); + hint.ai_family = PF_UNSPEC; + hint.ai_socktype = SOCK_STREAM; + error = getaddrinfo(host, port, &hint, &res); + if (error) + return (gai_strerror(error)); + freeaddrinfo(res); + return (NULL); +} + +void +vcc_ParseBackend(struct tokenlist *tl) +{ + unsigned a; + struct var *vp; + struct token *t_be = NULL; + struct token *t_host = NULL; + struct token *t_port = NULL; + const char *ep; + + vcc_NextToken(tl); + ExpectErr(tl, ID); + t_be = tl->t; + vcc_AddDef(tl, tl->t, R_BACKEND); + if (tl->nbackend == 0) + vcc_AddRef(tl, tl->t, R_BACKEND); + Fh(tl, 1, "#define VGC_backend_%.*s (VCL_conf.backend[%d])\n", + PF(tl->t), tl->nbackend); + Fc(tl, 0, "\n"); + Fc(tl, 0, "static void\n"); + Fc(tl, 1, "VGC_init_backend_%.*s (void)\n", PF(tl->t)); + Fc(tl, 1, "{\n"); + Fc(tl, 1, "\tstruct backend *backend = VGC_backend_%.*s;\n", PF(tl->t)); + Fc(tl, 1, "\n"); + Fc(tl, 1, "\tVRT_set_backend_name(backend, \"%.*s\");\n", PF(tl->t)); + vcc_NextToken(tl); + ExpectErr(tl, '{'); + vcc_NextToken(tl); + while (1) { + if (tl->t->tok == '}') + break; + ExpectErr(tl, ID); + if (!vcc_IdIs(tl->t, "set")) { + vsb_printf(tl->sb, + "Expected 'set', found "); + vcc_ErrToken(tl, tl->t); + vsb_printf(tl->sb, " at\n"); + vcc_ErrWhere(tl, tl->t); + return; + } + vcc_NextToken(tl); + ExpectErr(tl, VAR); + vp = FindVar(tl, tl->t, vcc_be_vars); + ERRCHK(tl); + assert(vp != NULL); + vcc_NextToken(tl); + ExpectErr(tl, '='); + vcc_NextToken(tl); + switch (vp->fmt) { + case HOSTNAME: + ExpectErr(tl, CSTR); + t_host = tl->t; + Fc(tl, 1, "\t%s ", vp->lname); + EncToken(tl->fc, t_host); + Fc(tl, 0, ");\n"); + vcc_NextToken(tl); + break; + case PORTNAME: + ExpectErr(tl, CSTR); + t_port = tl->t; + Fc(tl, 1, "\t%s ", vp->lname); + EncToken(tl->fc, t_port); + Fc(tl, 0, ");\n"); + vcc_NextToken(tl); + break; +#if 0 + case INT: + case SIZE: + case RATE: + case FLOAT: +#endif + case TIME: + Fc(tl, 1, "\t%s ", vp->lname); + a = tl->t->tok; + if (a == T_MUL || a == T_DIV) + Fc(tl, 0, "%g", vcc_DoubleVal(tl)); + else if (vp->fmt == TIME) + vcc_TimeVal(tl); + else if (vp->fmt == SIZE) + vcc_SizeVal(tl); + else if (vp->fmt == RATE) + vcc_RateVal(tl); + else + Fc(tl, 0, "%g", vcc_DoubleVal(tl)); + Fc(tl, 0, ");\n"); + break; + default: + vsb_printf(tl->sb, + "Assignments not possible for '%s'\n", vp->name); + vcc_ErrWhere(tl, tl->t); + return; + } + ExpectErr(tl, ';'); + vcc_NextToken(tl); + } + ExpectErr(tl, '}'); + if (t_host == NULL) { + vsb_printf(tl->sb, "Backend '%.*s' has no hostname\n", + PF(t_be)); + vcc_ErrWhere(tl, tl->t); + return; + } + ep = CheckHostPort(t_host->dec, "80"); + if (ep != NULL) { + vsb_printf(tl->sb, "Backend '%.*s': %s\n", PF(t_be), ep); + vcc_ErrWhere(tl, t_host); + return; + } + if (t_port != NULL) { + ep = CheckHostPort(t_host->dec, t_port->dec); + if (ep != NULL) { + vsb_printf(tl->sb, + "Backend '%.*s': %s\n", PF(t_be), ep); + vcc_ErrWhere(tl, t_port); + return; + } + } + + vcc_NextToken(tl); + Fc(tl, 1, "}\n"); + Fc(tl, 0, "\n"); + Fi(tl, 0, "\tVGC_init_backend_%.*s();\n", PF(t_be)); + Ff(tl, 0, "\tVRT_fini_backend(VGC_backend_%.*s);\n", PF(t_be)); + tl->nbackend++; +} diff --git a/varnish-cache/lib/libvcl/vcc_compile.c b/varnish-cache/lib/libvcl/vcc_compile.c index 425b1257..7505e397 100644 --- a/varnish-cache/lib/libvcl/vcc_compile.c +++ b/varnish-cache/lib/libvcl/vcc_compile.c @@ -60,15 +60,10 @@ * and all the rest... */ -#include -#include -#include - #include #include #include #include -#include #include #include #include @@ -76,13 +71,13 @@ #include #include -#include "compat/asprintf.h" +#include + #include "vsb.h" #include "vcc_priv.h" #include "vcc_compile.h" -#include "vrt.h" #include "libvcl.h" struct method method_tab[] = { diff --git a/varnish-cache/lib/libvcl/vcc_compile.h b/varnish-cache/lib/libvcl/vcc_compile.h index feb5d3e9..4a54a047 100644 --- a/varnish-cache/lib/libvcl/vcc_compile.h +++ b/varnish-cache/lib/libvcl/vcc_compile.h @@ -135,6 +135,9 @@ void vcc_Cond_Ip(struct var *vp, struct tokenlist *tl); /* vcc_action.c */ void vcc_ParseAction(struct tokenlist *tl); +/* vcc_backend.c */ +void vcc_ParseBackend(struct tokenlist *tl); + /* vcc_compile.c */ extern struct method method_tab[]; void Fh(struct tokenlist *tl, int indent, const char *fmt, ...); diff --git a/varnish-cache/lib/libvcl/vcc_parse.c b/varnish-cache/lib/libvcl/vcc_parse.c index b01052b6..6b485b8e 100644 --- a/varnish-cache/lib/libvcl/vcc_parse.c +++ b/varnish-cache/lib/libvcl/vcc_parse.c @@ -29,30 +29,16 @@ * $Id$ */ -#include -#include -#include - #include -#include -#include -#include -#include #include -#include -#include #include -#include -#include -#include "compat/asprintf.h" #include "vsb.h" #include "vcc_priv.h" #include "vcc_compile.h" #include "vrt.h" -#include "libvcl.h" /*--------------------------------------------------------------------*/ @@ -518,150 +504,6 @@ Compound(struct tokenlist *tl) /*--------------------------------------------------------------------*/ -static const char * -CheckHostPort(const char *host, const char *port) -{ - struct addrinfo *res, hint; - int error; - - memset(&hint, 0, sizeof hint); - hint.ai_family = PF_UNSPEC; - hint.ai_socktype = SOCK_STREAM; - error = getaddrinfo(host, port, &hint, &res); - if (error) - return (gai_strerror(error)); - freeaddrinfo(res); - return (NULL); -} - -static void -Backend(struct tokenlist *tl) -{ - unsigned a; - struct var *vp; - struct token *t_be = NULL; - struct token *t_host = NULL; - struct token *t_port = NULL; - const char *ep; - - vcc_NextToken(tl); - ExpectErr(tl, ID); - t_be = tl->t; - vcc_AddDef(tl, tl->t, R_BACKEND); - if (tl->nbackend == 0) - vcc_AddRef(tl, tl->t, R_BACKEND); - Fh(tl, 1, "#define VGC_backend_%.*s (VCL_conf.backend[%d])\n", - PF(tl->t), tl->nbackend); - Fc(tl, 0, "\n"); - Fc(tl, 0, "static void\n"); - Fc(tl, 1, "VGC_init_backend_%.*s (void)\n", PF(tl->t)); - Fc(tl, 1, "{\n"); - Fc(tl, 1, "\tstruct backend *backend = VGC_backend_%.*s;\n", PF(tl->t)); - Fc(tl, 1, "\n"); - Fc(tl, 1, "\tVRT_set_backend_name(backend, \"%.*s\");\n", PF(tl->t)); - vcc_NextToken(tl); - ExpectErr(tl, '{'); - vcc_NextToken(tl); - while (1) { - if (tl->t->tok == '}') - break; - ExpectErr(tl, ID); - if (!vcc_IdIs(tl->t, "set")) { - vsb_printf(tl->sb, - "Expected 'set', found "); - vcc_ErrToken(tl, tl->t); - vsb_printf(tl->sb, " at\n"); - vcc_ErrWhere(tl, tl->t); - return; - } - vcc_NextToken(tl); - ExpectErr(tl, VAR); - vp = FindVar(tl, tl->t, vcc_be_vars); - ERRCHK(tl); - assert(vp != NULL); - vcc_NextToken(tl); - ExpectErr(tl, '='); - vcc_NextToken(tl); - switch (vp->fmt) { - case HOSTNAME: - ExpectErr(tl, CSTR); - t_host = tl->t; - Fc(tl, 1, "\t%s ", vp->lname); - EncToken(tl->fc, t_host); - Fc(tl, 0, ");\n"); - vcc_NextToken(tl); - break; - case PORTNAME: - ExpectErr(tl, CSTR); - t_port = tl->t; - Fc(tl, 1, "\t%s ", vp->lname); - EncToken(tl->fc, t_port); - Fc(tl, 0, ");\n"); - vcc_NextToken(tl); - break; -#if 0 - case INT: - case SIZE: - case RATE: - case FLOAT: -#endif - case TIME: - Fc(tl, 1, "\t%s ", vp->lname); - a = tl->t->tok; - if (a == T_MUL || a == T_DIV) - Fc(tl, 0, "%g", vcc_DoubleVal(tl)); - else if (vp->fmt == TIME) - vcc_TimeVal(tl); - else if (vp->fmt == SIZE) - vcc_SizeVal(tl); - else if (vp->fmt == RATE) - vcc_RateVal(tl); - else - Fc(tl, 0, "%g", vcc_DoubleVal(tl)); - Fc(tl, 0, ");\n"); - break; - default: - vsb_printf(tl->sb, - "Assignments not possible for '%s'\n", vp->name); - vcc_ErrWhere(tl, tl->t); - return; - } - ExpectErr(tl, ';'); - vcc_NextToken(tl); - } - ExpectErr(tl, '}'); - if (t_host == NULL) { - vsb_printf(tl->sb, "Backend '%.*s' has no hostname\n", - PF(t_be)); - vcc_ErrWhere(tl, tl->t); - return; - } - ep = CheckHostPort(t_host->dec, "80"); - if (ep != NULL) { - vsb_printf(tl->sb, "Backend '%.*s': %s\n", PF(t_be), ep); - vcc_ErrWhere(tl, t_host); - return; - } - if (t_port != NULL) { - ep = CheckHostPort(t_host->dec, t_port->dec); - if (ep != NULL) { - vsb_printf(tl->sb, - "Backend '%.*s': %s\n", PF(t_be), ep); - vcc_ErrWhere(tl, t_port); - return; - } - } - - vcc_NextToken(tl); - Fc(tl, 1, "}\n"); - Fc(tl, 0, "\n"); - Fi(tl, 0, "\tVGC_init_backend_%.*s();\n", PF(t_be)); - Ff(tl, 0, "\tVRT_fini_backend(VGC_backend_%.*s);\n", PF(t_be)); - tl->nbackend++; -} - -/*--------------------------------------------------------------------*/ - static void Function(struct tokenlist *tl) { @@ -720,7 +562,7 @@ vcc_Parse(struct tokenlist *tl) Function(tl); break; case T_BACKEND: - Backend(tl); + vcc_ParseBackend(tl); break; case EOI: break; diff --git a/varnish-cache/lib/libvcl/vcc_token.c b/varnish-cache/lib/libvcl/vcc_token.c index ca47c8e2..00ff0429 100644 --- a/varnish-cache/lib/libvcl/vcc_token.c +++ b/varnish-cache/lib/libvcl/vcc_token.c @@ -39,11 +39,8 @@ #include "libvarnish.h" #include "vcc_priv.h" -#include "vcl_returns.h" #include "vcc_compile.h" -#include "libvcl.h" - /*--------------------------------------------------------------------*/ void -- 2.39.5