[master] a200cd2 Move the sockaddr emitting function to vcc_utils.c (ne: vcc_string.c) where the rest of the compiler can get at it.

Poul-Henning Kamp phk at varnish-cache.org
Tue Aug 6 16:05:18 CEST 2013


commit a200cd2469ef31bcd1bf32e760be0e10e3f9fb6e
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date:   Tue Aug 6 14:04:35 2013 +0000

    Move the sockaddr emitting function to vcc_utils.c (ne: vcc_string.c)
    where the rest of the compiler can get at it.

diff --git a/lib/libvcl/Makefile.am b/lib/libvcl/Makefile.am
index ebe0526..91bc0d3 100644
--- a/lib/libvcl/Makefile.am
+++ b/lib/libvcl/Makefile.am
@@ -22,7 +22,7 @@ libvcl_la_SOURCES = \
 	vcc_fixed_token.c \
 	vcc_obj.c \
 	vcc_storage.c \
-	vcc_string.c \
+	vcc_utils.c \
 	vcc_symb.c \
 	vcc_token.c \
 	vcc_var.c \
diff --git a/lib/libvcl/vcc_backend.c b/lib/libvcl/vcc_backend.c
index 9bab3ce..1c69ae8 100644
--- a/lib/libvcl/vcc_backend.c
+++ b/lib/libvcl/vcc_backend.c
@@ -61,7 +61,6 @@
 #include "vcc_compile.h"
 
 #include "vss.h"
-#include "vsa.h"
 
 struct host {
 	VTAILQ_ENTRY(host)      list;
@@ -69,42 +68,6 @@ struct host {
 	char			*vgcname;
 };
 
-/*
- * The IPv6 crew royally screwed up the entire idea behind
- * struct sockaddr, and combined with various other incomptency
- * in the OS business, that means that there is no sane or even
- * remotely portable way to initialize a sockaddr at compile time.
- *
- * In our case it is slightly more tricky than that, because we don't
- * even want to #include the struct sockaddr* definitions.
- *
- * Instead we make sure the sockaddr is sane (for our values of sane)
- * and dump it in binary, using a 64 bit integertype, hoping that this
- * will ensure good enough alignment.
- */
-
-static int
-emit_sockaddr(struct vcc *tl, const void *sa, unsigned sal)
-{
-	unsigned n = (sal + 7) / 8, len;
-	uint64_t b[n];
-
-	assert(VSA_Sane(sa));
-	AN(sa);
-	AN(sal);
-	assert(sal < 256);
-	assert(sizeof(unsigned long long) == 8);
-	Fh(tl, 0, "\nstatic const unsigned long long");
-	Fh(tl, 0, " sockaddr_%u[%d] = {\n", tl->unique, n);
-	memcpy(b, sa, sal);
-	for (len = 0; len <n; len++) {
-		Fh(tl, 0, "%s    0x%016jx",
-		    len ? ",\n" : "",
-		    (uintmax_t)b[len]);
-	}
-	Fh(tl, 0, "\n};\n");
-	return (tl->unique++);
-}
 
 /*--------------------------------------------------------------------
  * Struct sockaddr is not really designed to be a compile time
@@ -124,9 +87,10 @@ Emit_Sockaddr(struct vcc *tl, const struct token *t_host, const char *port)
 {
 	struct foo_proto protos[3], *pp;
 	struct addrinfo *res, *res0, *res1, hint;
-	int error, retval, x;
+	int error, retval;
 	char hbuf[NI_MAXHOST];
 	char *hop, *pop;
+	const char *sa;
 
 	AN(t_host->dec);
 
@@ -204,8 +168,8 @@ Emit_Sockaddr(struct vcc *tl, const struct token *t_host, const char *port)
 		pp->l =  res->ai_addrlen;
 		memcpy(&pp->sa, res->ai_addr, pp->l);
 
-		x = emit_sockaddr(tl, res->ai_addr, res->ai_addrlen);
-		Fb(tl, 0, "\t.%s_sockaddr = sockaddr_%u,\n", pp->name, x);
+		sa = vcc_sockaddr(tl, res->ai_addr, res->ai_addrlen);
+		Fb(tl, 0, "\t.%s_sockaddr = %s,\n", pp->name, sa);
 		error = getnameinfo(res->ai_addr,
 		    res->ai_addrlen, hbuf, sizeof hbuf,
 		    NULL, 0, NI_NUMERICHOST);
diff --git a/lib/libvcl/vcc_compile.h b/lib/libvcl/vcc_compile.h
index feb993a..11bd782 100644
--- a/lib/libvcl/vcc_compile.h
+++ b/lib/libvcl/vcc_compile.h
@@ -286,7 +286,8 @@ void vcc_Parse(struct vcc *tl);
 sym_wildcard_t vcc_Stv_Wildcard;
 
 /* vcc_string.c */
-char *vcc_regexp(struct vcc *tl);
+const char *vcc_regexp(struct vcc *tl);
+const char *vcc_sockaddr(struct vcc *tl, const void *sa, unsigned sal);
 
 /* vcc_symb.c */
 struct symbol *VCC_AddSymbolStr(struct vcc *tl, const char *name, enum symkind);
diff --git a/lib/libvcl/vcc_expr.c b/lib/libvcl/vcc_expr.c
index 88d7ccb..95eceba 100644
--- a/lib/libvcl/vcc_expr.c
+++ b/lib/libvcl/vcc_expr.c
@@ -441,7 +441,7 @@ vcc_Eval_Regsub(struct vcc *tl, struct expr **e, const struct symbol *sym)
 {
 	struct expr *e2;
 	int all = sym->eval_priv == NULL ? 0 : 1;
-	char *p;
+	const char *p;
 	char buf[128];
 
 	vcc_delete_expr(*e);
@@ -963,7 +963,7 @@ vcc_expr_cmp(struct vcc *tl, struct expr **e, enum var_type fmt)
 	struct expr *e2;
 	const struct cmps *cp;
 	char buf[256];
-	char *re;
+	const char *re;
 	const char *not;
 	struct token *tk;
 
diff --git a/lib/libvcl/vcc_string.c b/lib/libvcl/vcc_string.c
deleted file mode 100644
index 4e4762e..0000000
--- a/lib/libvcl/vcc_string.c
+++ /dev/null
@@ -1,72 +0,0 @@
-/*-
- * Copyright (c) 2006 Verdens Gang AS
- * Copyright (c) 2006-2011 Varnish Software AS
- * All rights reserved.
- *
- * Author: Poul-Henning Kamp <phk at phk.freebsd.dk>
- *
- * 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 THE 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.
- */
-
-#include "config.h"
-
-#include <stdio.h>
-#include <string.h>
-
-#include "vcc_compile.h"
-
-#include "vre.h"
-#include "vrt.h"
-
-/*--------------------------------------------------------------------*/
-
-char *
-vcc_regexp(struct vcc *tl)
-{
-	char buf[BUFSIZ], *p;
-	vre_t *t;
-	const char *error;
-	int erroroffset;
-
-	Expect(tl, CSTR);
-	if (tl->err)
-		return (NULL);
-	memset(&t, 0, sizeof t);
-	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);
-	sprintf(buf, "VGC_re_%u", tl->unique++);
-	p = TlAlloc(tl, strlen(buf) + 1);
-	strcpy(p, buf);
-
-	Fh(tl, 0, "static void *%s;\n", buf);
-	Fi(tl, 0, "\tVRT_re_init(&%s, ",buf);
-	EncToken(tl->fi, tl->t);
-	Fi(tl, 0, ");\n");
-	Ff(tl, 0, "\tVRT_re_fini(%s);\n", buf);
-	return (p);
-}
diff --git a/lib/libvcl/vcc_utils.c b/lib/libvcl/vcc_utils.c
new file mode 100644
index 0000000..bfb3c05
--- /dev/null
+++ b/lib/libvcl/vcc_utils.c
@@ -0,0 +1,116 @@
+/*-
+ * Copyright (c) 2006 Verdens Gang AS
+ * Copyright (c) 2006-2011 Varnish Software AS
+ * All rights reserved.
+ *
+ * Author: Poul-Henning Kamp <phk at phk.freebsd.dk>
+ *
+ * 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 THE 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.
+ */
+
+#include "config.h"
+
+#include <stdio.h>
+#include <string.h>
+#include <sys/socket.h>
+
+#include "vcc_compile.h"
+
+#include "vre.h"
+#include "vrt.h"
+#include "vsa.h"
+
+/*--------------------------------------------------------------------*/
+
+const char *
+vcc_regexp(struct vcc *tl)
+{
+	char buf[BUFSIZ], *p;
+	vre_t *t;
+	const char *error;
+	int erroroffset;
+
+	Expect(tl, CSTR);
+	if (tl->err)
+		return (NULL);
+	memset(&t, 0, sizeof t);
+	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);
+	sprintf(buf, "VGC_re_%u", tl->unique++);
+	p = TlAlloc(tl, strlen(buf) + 1);
+	strcpy(p, buf);
+
+	Fh(tl, 0, "static void *%s;\n", buf);
+	Fi(tl, 0, "\tVRT_re_init(&%s, ",buf);
+	EncToken(tl->fi, tl->t);
+	Fi(tl, 0, ");\n");
+	Ff(tl, 0, "\tVRT_re_fini(%s);\n", buf);
+	return (p);
+}
+
+/*
+ * The IPv6 crew royally screwed up the entire idea behind
+ * struct sockaddr, and combined with various other incomptency
+ * in the OS business, that means that there is no sane or even
+ * remotely portable way to initialize a sockaddr at compile time.
+ *
+ * In our case it is slightly more tricky than that, because we don't
+ * even want to #include the struct sockaddr* definitions.
+ *
+ * Instead we make sure the sockaddr is sane (for our values of sane)
+ * and dump it in binary, using a 64 bit integertype, hoping that this
+ * will ensure good enough alignment.
+ */
+
+const char *
+vcc_sockaddr(struct vcc *tl, const void *sa, unsigned sal)
+{
+	unsigned n = (sal + 7) / 8, len;
+	uint64_t b[n];
+	char *p;
+
+	assert(VSA_Sane(sa));
+	AN(sa);
+	AN(sal);
+	assert(sal < 256);
+	assert(sizeof(unsigned long long) == 8);
+
+	p = TlAlloc(tl, 20);
+	sprintf(p, "sockaddr_%u", tl->unique++);
+
+	Fh(tl, 0, "\nstatic const unsigned long long");
+	Fh(tl, 0, " %s[%d] = {\n", p, n);
+	memcpy(b, sa, sal);
+	for (len = 0; len <n; len++) {
+		Fh(tl, 0, "%s    0x%016jx",
+		    len ? ",\n" : "",
+		    (uintmax_t)b[len]);
+	}
+	Fh(tl, 0, "\n};\n");
+	return (p);
+}



More information about the varnish-commit mailing list