From: phk Date: Mon, 27 Mar 2006 14:10:43 +0000 (+0000) Subject: Add VCL_CompileFile() function. X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=803054c171bbe7ef4d0af627bbb8308a5ff35116;p=varnish Add VCL_CompileFile() function. git-svn-id: svn+ssh://projects.linpro.no/svn/varnish/trunk@81 d4fa192b-c00b-0410-8231-f00ffab90ce4 --- diff --git a/varnish-cache/lib/libvcl/vcl_compile.c b/varnish-cache/lib/libvcl/vcl_compile.c index 9c7df521..1ea98388 100644 --- a/varnish-cache/lib/libvcl/vcl_compile.c +++ b/varnish-cache/lib/libvcl/vcl_compile.c @@ -36,9 +36,13 @@ #include #include #include +#include +#include +#include #include #include #include +#include #include #include "vcl_priv.h" @@ -1455,56 +1459,38 @@ done: /*--------------------------------------------------------------------*/ -void -VCL_InitCompile(void) +char * +VCL_CompileFile(struct sbuf *sb, const char *fn) { - struct var *v; - - vcl_init_tnames(); - for (v = vars; v->name != NULL; v++) - v->len = strlen(v->name); + char *f, *r; + int fd, i; + struct stat st; + + fd = open(fn, O_RDONLY); + if (fd < 0) { + sbuf_printf(sb, "Cannot open file '%s': %s", + fn, strerror(errno)); + return (NULL); + } + assert(0 == fstat(fd, &st)); + f = malloc(st.st_size + 1); + assert(f != NULL); + i = read(fd, f, st.st_size); + assert(i == st.st_size); + f[i] = '\0'; + r = VCL_Compile(sb, f, NULL); + free(f); + return (r); } -#if 0 /*--------------------------------------------------------------------*/ -#include - -#define MYSPACE (128 * 1024) - -int -main(int argc, char **argv) +void +VCL_InitCompile(void) { - char *p; - size_t z; - FILE *fi; - struct sbuf *sb; - - setbuf(stdout, NULL); - { struct var *v; - for (v = vars; v->name != NULL; v++) { + vcl_init_tnames(); + for (v = vars; v->name != NULL; v++) v->len = strlen(v->name); - } - } - if (argc != 2) - err(1, "Usage: %s file", argv[0]); - fi = fopen(argv[1], "r"); - if (fi == NULL) - err(1, "Cannot open %s", argv[1]); - p = malloc(MYSPACE); - assert(p != NULL); - - z = fread(p, 1, MYSPACE - 1, fi); - if (z == 0) - err(1, "Nothing read from %s", argv[1]); - p[z] = '\0'; - sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND); - Compile(sb, p, p + z); - sbuf_finish(sb); - if (sbuf_len(sb)) - printf("<%s>\n", sbuf_data(sb)); - return (0); } -#endif