[master] 932217f Add a lib/libvarnishtools library to hold common files for the tools.

Martin Blix Grydeland martin at varnish-cache.org
Thu Jun 13 12:41:24 CEST 2013


commit 932217f75cd96e534805d09730d236e50b395f81
Author: Martin Blix Grydeland <martin at varnish-software.com>
Date:   Fri May 24 16:04:03 2013 +0200

    Add a lib/libvarnishtools library to hold common files for the tools.
    
    Move vut.c and opt2rst.c there.

diff --git a/bin/varnishlog/Makefile.am b/bin/varnishlog/Makefile.am
index f8a63af..2380fb9 100644
--- a/bin/varnishlog/Makefile.am
+++ b/bin/varnishlog/Makefile.am
@@ -9,8 +9,7 @@ dist_man_MANS = varnishlog.1
 varnishlog_SOURCES = \
 	varnishlog.c \
 	varnishlog_options.h \
-	vut.c \
-	vut.h \
+	$(top_builddir)/lib/libvarnishtools/vut.c \
 	$(top_builddir)/lib/libvarnish/vas.c \
 	$(top_builddir)/lib/libvarnish/flopen.c \
 	$(top_builddir)/lib/libvarnish/version.c \
@@ -26,7 +25,7 @@ varnishlog_LDADD = \
 noinst_PROGRAMS = varnishlog_opt2rst
 varnishlog_opt2rst_CFLAGS = -DOPT2RST_INC="varnishlog_options.h"
 varnishlog_opt2rst_SOURCES = \
-	opt2rst.c
+	$(top_builddir)/lib/libvarnishtools/opt2rst.c
 
 BUILT_SOURCES = varnishlog_options.rst
 
diff --git a/bin/varnishlog/opt2rst.c b/bin/varnishlog/opt2rst.c
deleted file mode 100644
index ab854ef..0000000
--- a/bin/varnishlog/opt2rst.c
+++ /dev/null
@@ -1,89 +0,0 @@
-/*-
- * Copyright (c) 2006 Verdens Gang AS
- * Copyright (c) 2006-2013 Varnish Software AS
- * All rights reserved.
- *
- * Author: Martin Blix Grydeland <martin at varnish-software.com>
- *
- * 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 <stdio.h>
-#include <string.h>
-
-#ifndef OPT2RST_INC
-#error "OPT2RST_INC undefined"
-#endif
-
-#define STRINGIFY(x) #x
-#define TOSTRING(x) STRINGIFY(x)
-
-#define VOPT_SYNOPSIS
-#define VOPT_FULL
-#define VOPT_INC TOSTRING(OPT2RST_INC)
-#include "vapi/voptget.h"
-
-static void
-print_nobrackets(const char *s)
-{
-	for (; *s; s++) {
-		if (strchr("[]", *s))
-			continue;
-		printf("%c", *s);
-	}
-}
-
-static void
-print_tabbed(const char *string, int tabs)
-{
-	int i;
-	const char *c;
-
-	for (c = string; *c; c++) {
-		if (c == string || *(c - 1) == '\n')
-			for (i = 0; i < tabs; i++)
-				printf("\t");
-		printf("%c", *c);
-	}
-}
-
-static void
-print_opt(const struct vopt_full *opt)
-{
-	print_nobrackets(opt->synopsis);
-	printf("\n\n");
-	print_tabbed(opt->ldesc, 1);
-	printf("\n\n");
-}
-
-int
-main(int argc, char * const *argv)
-{
-	int i;
-
-	(void)argc;
-	(void)argv;
-	for (i = 0; i < sizeof vopt_full / sizeof vopt_full[0]; i++)
-		print_opt(&vopt_full[i]);
-
-	return (0);
-}
diff --git a/bin/varnishlog/vut.c b/bin/varnishlog/vut.c
deleted file mode 100644
index 29ff56b..0000000
--- a/bin/varnishlog/vut.c
+++ /dev/null
@@ -1,273 +0,0 @@
-/*-
- * Copyright (c) 2006 Verdens Gang AS
- * Copyright (c) 2006-2013 Varnish Software AS
- * All rights reserved.
- *
- * Author: Martin Blix Grydeland <martin at varnish-software.com>
- *
- * 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.
- *
- * Common functions for the utilities
- */
-
-#include <stdint.h>
-#include <stdarg.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <stdio.h>
-#include <errno.h>
-#include <string.h>
-
-#include "compat/daemon.h"
-#include "vapi/vsm.h"
-#include "vapi/vsc.h"
-#include "vapi/vsl.h"
-#include "vtim.h"
-#include "vas.h"
-#include "miniobj.h"
-
-#include "vut.h"
-
-struct VUT VUT;
-
-void
-VUT_Error(int status, const char *fmt, ...)
-{
-	va_list ap;
-
-	AN(fmt);
-	va_start(ap, fmt);
-	vfprintf(stderr, fmt, ap);
-	va_end(ap);
-	fprintf(stderr, "\n");
-
-	if (status)
-		exit(status);
-}
-
-int
-VUT_g_Arg(const char *arg)
-{
-
-	VUT.g_arg = VSLQ_Name2Grouping(arg, -1);
-	if (VUT.g_arg == -2)
-		VUT_Error(1, "Ambigous grouping type: %s", arg);
-	else if (VUT.g_arg < 0)
-		VUT_Error(1, "Unknown grouping type: %s", arg);
-	return (1);
-}
-
-int
-VUT_Arg(int opt, const char *arg)
-{
-	switch (opt) {
-	case 'a':
-		/* Binary file append */
-		VUT.a_opt = 1;
-		return (1);
-	case 'n':
-		/* Varnish instance */
-		if (VUT.vsm == NULL)
-			VUT.vsm = VSM_New();
-		AN(VUT.vsm);
-		if (VSM_n_Arg(VUT.vsm, arg) <= 0)
-			VUT_Error(1, "%s", VSM_Error(VUT.vsm));
-		return (1);
-	case 'd':
-		/* Head */
-		VUT.d_opt = 1;
-		return (1);
-	case 'g':
-		/* Grouping */
-		return (VUT_g_Arg(arg));
-	case 'r':
-		/* Binary file input */
-		REPLACE(VUT.r_arg, arg);
-		return (1);
-	case 'u':
-		/* Unbuffered binary output */
-		VUT.u_opt = 1;
-		return (1);
-	case 'w':
-		/* Binary file output */
-		REPLACE(VUT.w_arg, arg);
-		return (1);
-	default:
-		AN(VUT.vsl);
-		return (VSL_Arg(VUT.vsl, opt, arg));
-	}
-}
-
-void
-VUT_Init(void)
-{
-	VUT.g_arg = VSL_g_vxid;
-	AZ(VUT.vsl);
-	VUT.vsl = VSL_New();
-	AN(VUT.vsl);
-}
-
-void
-VUT_Setup(void)
-{
-	struct VSL_cursor *c;
-
-	AN(VUT.vsl);
-
-	/* Input */
-	if (VUT.r_arg && VUT.vsm)
-		VUT_Error(1, "Can't have both -n and -r options");
-	if (VUT.r_arg)
-		c = VSL_CursorFile(VUT.vsl, VUT.r_arg);
-	else {
-		if (VUT.vsm == NULL)
-			/* Default uses VSM with n=hostname */
-			VUT.vsm = VSM_New();
-		AN(VUT.vsm);
-		if (VSM_Open(VUT.vsm))
-			VUT_Error(1, "Can't open VSM file (%s)",
-			    VSM_Error(VUT.vsm));
-		c = VSL_CursorVSM(VUT.vsl, VUT.vsm, !VUT.d_opt);
-	}
-	if (c == NULL)
-		VUT_Error(1, "Can't open log (%s)", VSL_Error(VUT.vsl));
-
-	/* Output */
-	if (VUT.w_arg) {
-		VUT.fo = VSL_WriteOpen(VUT.vsl, VUT.w_arg, VUT.a_opt,
-		    VUT.u_opt);
-		if (VUT.fo == NULL)
-			VUT_Error(1, "Can't open output file (%s)",
-			    VSL_Error(VUT.vsl));
-	}
-
-	/* Create query */
-	VUT.vslq = VSLQ_New(VUT.vsl, &c, VUT.g_arg, VUT.query);
-	if (VUT.vslq == NULL)
-		VUT_Error(1, "Query parse error (%s)", VSL_Error(VUT.vsl));
-	AZ(c);
-}
-
-void
-VUT_Fini(void)
-{
-	free(VUT.r_arg);
-
-	if (VUT.vslq)
-		VSLQ_Delete(&VUT.vslq);
-	if (VUT.vsl)
-		VSL_Delete(VUT.vsl);
-	if (VUT.vsm)
-		VSM_Delete(VUT.vsm);
-
-	memset(&VUT, 0, sizeof VUT);
-}
-
-int
-VUT_Main(VSLQ_dispatch_f *func, void *priv)
-{
-	struct VSL_cursor *c;
-	int i;
-
-	if (func == NULL) {
-		if (VUT.w_arg)
-			func = VSL_WriteTransactions;
-		else
-			func = VSL_PrintTransactions;
-		priv = VUT.fo;
-	}
-
-	while (1) {
-		while (VUT.vslq == NULL) {
-			AZ(VUT.r_arg);
-			AN(VUT.vsm);
-			VTIM_sleep(0.1);
-			if (VSM_Open(VUT.vsm)) {
-				VSM_ResetError(VUT.vsm);
-				continue;
-			}
-			c = VSL_CursorVSM(VUT.vsl, VUT.vsm, 1);
-			if (c == NULL) {
-				VSL_ResetError(VUT.vsl);
-				continue;
-			}
-			VUT.vslq = VSLQ_New(VUT.vsl, &c, VUT.g_arg, VUT.query);
-			AN(VUT.vslq);
-			AZ(c);
-		}
-
-		i = VSLQ_Dispatch(VUT.vslq, func, priv);
-		if (i == 0) {
-			/* Nothing to do but wait */
-			if (VUT.fo)
-				fflush(VUT.fo);
-			VTIM_sleep(0.01);
-			continue;
-		}
-		if (i == -1) {
-			/* EOF */
-			break;
-		}
-
-		if (VUT.vsm == NULL)
-			break;
-
-		/* XXX: Make continuation optional */
-
-		VSLQ_Flush(VUT.vslq, func, priv);
-		VSLQ_Delete(&VUT.vslq);
-		AZ(VUT.vslq);
-
-		if (i == -2) {
-			/* Abandoned */
-			VUT_Error(0, "Log abandoned - reopening");
-			VSM_Close(VUT.vsm);
-		} else if (i < -2) {
-			/* Overrun */
-			VUT_Error(0, "Log overrun");
-		}
-
-		/* Reconnect VSM */
-		while (VUT.vslq == NULL) {
-			AZ(VUT.r_arg);
-			AN(VUT.vsm);
-			VTIM_sleep(0.1);
-			if (VSM_Open(VUT.vsm)) {
-				VSM_ResetError(VUT.vsm);
-				continue;
-			}
-			c = VSL_CursorVSM(VUT.vsl, VUT.vsm, 1);
-			if (c == NULL) {
-				VSL_ResetError(VUT.vsl);
-				continue;
-			}
-			VUT.vslq = VSLQ_New(VUT.vsl, &c, VUT.g_arg, VUT.query);
-			AN(VUT.vslq);
-			AZ(c);
-		}
-	}
-
-	if (VUT.vslq != NULL)
-		VSLQ_Flush(VUT.vslq, func, priv);
-
-	return (i);
-}
diff --git a/bin/varnishlog/vut.h b/bin/varnishlog/vut.h
deleted file mode 100644
index dcb5a77..0000000
--- a/bin/varnishlog/vut.h
+++ /dev/null
@@ -1,66 +0,0 @@
-/*-
- * Copyright (c) 2006 Verdens Gang AS
- * Copyright (c) 2006-2013 Varnish Software AS
- * All rights reserved.
- *
- * Author: Martin Blix Grydeland <martin at varnish-software.com>
- *
- * 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.
- *
- * Common functions for the utilities
- */
-
-#include "vdef.h"
-
-struct VUT {
-	/* Options */
-	int		a_opt;
-	int		d_opt;
-	int		g_arg;
-	char		*r_arg;
-	int		u_opt;
-	char		*w_arg;
-	const char	*query;
-
-	/* State */
-	struct VSL_data	*vsl;
-	struct VSM_data	*vsm;
-	struct VSLQ	*vslq;
-	FILE		*fo;
-};
-
-extern struct VUT VUT;
-
-void VUT_Error(int status, const char *fmt, ...)
-	__printflike(2, 3);
-
-int VUT_g_Arg(const char *arg);
-
-int VUT_Arg(int opt, const char *arg);
-
-void VUT_Setup(void);
-
-void VUT_Init(void);
-
-void VUT_Fini(void);
-
-int VUT_Main(VSLQ_dispatch_f *func, void *priv);
diff --git a/configure.ac b/configure.ac
index 3c9add4..f2efa37 100644
--- a/configure.ac
+++ b/configure.ac
@@ -588,6 +588,7 @@ AC_CONFIG_FILES([
     lib/Makefile
     lib/libvarnish/Makefile
     lib/libvarnishapi/Makefile
+    lib/libvarnishtools/Makefile
     lib/libvarnishcompat/Makefile
     lib/libvcl/Makefile
     lib/libvgz/Makefile
diff --git a/include/Makefile.am b/include/Makefile.am
index 1ad0935..bc55f85 100644
--- a/include/Makefile.am
+++ b/include/Makefile.am
@@ -61,7 +61,8 @@ nobase_noinst_HEADERS = \
 	vss.h \
 	vtcp.h \
 	vtim.h \
-	vtree.h
+	vtree.h \
+	vut.h
 
 # Headers for use with vmods
 pkgdataincludedir = $(pkgdatadir)/include
diff --git a/include/vut.h b/include/vut.h
new file mode 100644
index 0000000..dcb5a77
--- /dev/null
+++ b/include/vut.h
@@ -0,0 +1,66 @@
+/*-
+ * Copyright (c) 2006 Verdens Gang AS
+ * Copyright (c) 2006-2013 Varnish Software AS
+ * All rights reserved.
+ *
+ * Author: Martin Blix Grydeland <martin at varnish-software.com>
+ *
+ * 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.
+ *
+ * Common functions for the utilities
+ */
+
+#include "vdef.h"
+
+struct VUT {
+	/* Options */
+	int		a_opt;
+	int		d_opt;
+	int		g_arg;
+	char		*r_arg;
+	int		u_opt;
+	char		*w_arg;
+	const char	*query;
+
+	/* State */
+	struct VSL_data	*vsl;
+	struct VSM_data	*vsm;
+	struct VSLQ	*vslq;
+	FILE		*fo;
+};
+
+extern struct VUT VUT;
+
+void VUT_Error(int status, const char *fmt, ...)
+	__printflike(2, 3);
+
+int VUT_g_Arg(const char *arg);
+
+int VUT_Arg(int opt, const char *arg);
+
+void VUT_Setup(void);
+
+void VUT_Init(void);
+
+void VUT_Fini(void);
+
+int VUT_Main(VSLQ_dispatch_f *func, void *priv);
diff --git a/lib/Makefile.am b/lib/Makefile.am
index 77c5ac5..eb67436 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -4,6 +4,7 @@ SUBDIRS = \
 	libvarnishcompat \
 	libvarnish \
 	libvarnishapi \
+	libvarnishtools \
 	libvcl \
 	libvgz \
 	libvmod_debug \
@@ -14,6 +15,7 @@ DIST_SUBDIRS = 	\
 	libvarnishcompat \
 	libvarnish \
 	libvarnishapi \
+	libvarnishtools \
 	libvcl \
 	libvgz \
 	libvmod_debug \
diff --git a/lib/libvarnishtools/Makefile.am b/lib/libvarnishtools/Makefile.am
new file mode 100644
index 0000000..1a14212
--- /dev/null
+++ b/lib/libvarnishtools/Makefile.am
@@ -0,0 +1,5 @@
+#
+
+EXTRA_DIST = \
+	vut.c \
+	opt2rst.c
diff --git a/lib/libvarnishtools/opt2rst.c b/lib/libvarnishtools/opt2rst.c
new file mode 100644
index 0000000..ab854ef
--- /dev/null
+++ b/lib/libvarnishtools/opt2rst.c
@@ -0,0 +1,89 @@
+/*-
+ * Copyright (c) 2006 Verdens Gang AS
+ * Copyright (c) 2006-2013 Varnish Software AS
+ * All rights reserved.
+ *
+ * Author: Martin Blix Grydeland <martin at varnish-software.com>
+ *
+ * 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 <stdio.h>
+#include <string.h>
+
+#ifndef OPT2RST_INC
+#error "OPT2RST_INC undefined"
+#endif
+
+#define STRINGIFY(x) #x
+#define TOSTRING(x) STRINGIFY(x)
+
+#define VOPT_SYNOPSIS
+#define VOPT_FULL
+#define VOPT_INC TOSTRING(OPT2RST_INC)
+#include "vapi/voptget.h"
+
+static void
+print_nobrackets(const char *s)
+{
+	for (; *s; s++) {
+		if (strchr("[]", *s))
+			continue;
+		printf("%c", *s);
+	}
+}
+
+static void
+print_tabbed(const char *string, int tabs)
+{
+	int i;
+	const char *c;
+
+	for (c = string; *c; c++) {
+		if (c == string || *(c - 1) == '\n')
+			for (i = 0; i < tabs; i++)
+				printf("\t");
+		printf("%c", *c);
+	}
+}
+
+static void
+print_opt(const struct vopt_full *opt)
+{
+	print_nobrackets(opt->synopsis);
+	printf("\n\n");
+	print_tabbed(opt->ldesc, 1);
+	printf("\n\n");
+}
+
+int
+main(int argc, char * const *argv)
+{
+	int i;
+
+	(void)argc;
+	(void)argv;
+	for (i = 0; i < sizeof vopt_full / sizeof vopt_full[0]; i++)
+		print_opt(&vopt_full[i]);
+
+	return (0);
+}
diff --git a/lib/libvarnishtools/vut.c b/lib/libvarnishtools/vut.c
new file mode 100644
index 0000000..29ff56b
--- /dev/null
+++ b/lib/libvarnishtools/vut.c
@@ -0,0 +1,273 @@
+/*-
+ * Copyright (c) 2006 Verdens Gang AS
+ * Copyright (c) 2006-2013 Varnish Software AS
+ * All rights reserved.
+ *
+ * Author: Martin Blix Grydeland <martin at varnish-software.com>
+ *
+ * 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.
+ *
+ * Common functions for the utilities
+ */
+
+#include <stdint.h>
+#include <stdarg.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <errno.h>
+#include <string.h>
+
+#include "compat/daemon.h"
+#include "vapi/vsm.h"
+#include "vapi/vsc.h"
+#include "vapi/vsl.h"
+#include "vtim.h"
+#include "vas.h"
+#include "miniobj.h"
+
+#include "vut.h"
+
+struct VUT VUT;
+
+void
+VUT_Error(int status, const char *fmt, ...)
+{
+	va_list ap;
+
+	AN(fmt);
+	va_start(ap, fmt);
+	vfprintf(stderr, fmt, ap);
+	va_end(ap);
+	fprintf(stderr, "\n");
+
+	if (status)
+		exit(status);
+}
+
+int
+VUT_g_Arg(const char *arg)
+{
+
+	VUT.g_arg = VSLQ_Name2Grouping(arg, -1);
+	if (VUT.g_arg == -2)
+		VUT_Error(1, "Ambigous grouping type: %s", arg);
+	else if (VUT.g_arg < 0)
+		VUT_Error(1, "Unknown grouping type: %s", arg);
+	return (1);
+}
+
+int
+VUT_Arg(int opt, const char *arg)
+{
+	switch (opt) {
+	case 'a':
+		/* Binary file append */
+		VUT.a_opt = 1;
+		return (1);
+	case 'n':
+		/* Varnish instance */
+		if (VUT.vsm == NULL)
+			VUT.vsm = VSM_New();
+		AN(VUT.vsm);
+		if (VSM_n_Arg(VUT.vsm, arg) <= 0)
+			VUT_Error(1, "%s", VSM_Error(VUT.vsm));
+		return (1);
+	case 'd':
+		/* Head */
+		VUT.d_opt = 1;
+		return (1);
+	case 'g':
+		/* Grouping */
+		return (VUT_g_Arg(arg));
+	case 'r':
+		/* Binary file input */
+		REPLACE(VUT.r_arg, arg);
+		return (1);
+	case 'u':
+		/* Unbuffered binary output */
+		VUT.u_opt = 1;
+		return (1);
+	case 'w':
+		/* Binary file output */
+		REPLACE(VUT.w_arg, arg);
+		return (1);
+	default:
+		AN(VUT.vsl);
+		return (VSL_Arg(VUT.vsl, opt, arg));
+	}
+}
+
+void
+VUT_Init(void)
+{
+	VUT.g_arg = VSL_g_vxid;
+	AZ(VUT.vsl);
+	VUT.vsl = VSL_New();
+	AN(VUT.vsl);
+}
+
+void
+VUT_Setup(void)
+{
+	struct VSL_cursor *c;
+
+	AN(VUT.vsl);
+
+	/* Input */
+	if (VUT.r_arg && VUT.vsm)
+		VUT_Error(1, "Can't have both -n and -r options");
+	if (VUT.r_arg)
+		c = VSL_CursorFile(VUT.vsl, VUT.r_arg);
+	else {
+		if (VUT.vsm == NULL)
+			/* Default uses VSM with n=hostname */
+			VUT.vsm = VSM_New();
+		AN(VUT.vsm);
+		if (VSM_Open(VUT.vsm))
+			VUT_Error(1, "Can't open VSM file (%s)",
+			    VSM_Error(VUT.vsm));
+		c = VSL_CursorVSM(VUT.vsl, VUT.vsm, !VUT.d_opt);
+	}
+	if (c == NULL)
+		VUT_Error(1, "Can't open log (%s)", VSL_Error(VUT.vsl));
+
+	/* Output */
+	if (VUT.w_arg) {
+		VUT.fo = VSL_WriteOpen(VUT.vsl, VUT.w_arg, VUT.a_opt,
+		    VUT.u_opt);
+		if (VUT.fo == NULL)
+			VUT_Error(1, "Can't open output file (%s)",
+			    VSL_Error(VUT.vsl));
+	}
+
+	/* Create query */
+	VUT.vslq = VSLQ_New(VUT.vsl, &c, VUT.g_arg, VUT.query);
+	if (VUT.vslq == NULL)
+		VUT_Error(1, "Query parse error (%s)", VSL_Error(VUT.vsl));
+	AZ(c);
+}
+
+void
+VUT_Fini(void)
+{
+	free(VUT.r_arg);
+
+	if (VUT.vslq)
+		VSLQ_Delete(&VUT.vslq);
+	if (VUT.vsl)
+		VSL_Delete(VUT.vsl);
+	if (VUT.vsm)
+		VSM_Delete(VUT.vsm);
+
+	memset(&VUT, 0, sizeof VUT);
+}
+
+int
+VUT_Main(VSLQ_dispatch_f *func, void *priv)
+{
+	struct VSL_cursor *c;
+	int i;
+
+	if (func == NULL) {
+		if (VUT.w_arg)
+			func = VSL_WriteTransactions;
+		else
+			func = VSL_PrintTransactions;
+		priv = VUT.fo;
+	}
+
+	while (1) {
+		while (VUT.vslq == NULL) {
+			AZ(VUT.r_arg);
+			AN(VUT.vsm);
+			VTIM_sleep(0.1);
+			if (VSM_Open(VUT.vsm)) {
+				VSM_ResetError(VUT.vsm);
+				continue;
+			}
+			c = VSL_CursorVSM(VUT.vsl, VUT.vsm, 1);
+			if (c == NULL) {
+				VSL_ResetError(VUT.vsl);
+				continue;
+			}
+			VUT.vslq = VSLQ_New(VUT.vsl, &c, VUT.g_arg, VUT.query);
+			AN(VUT.vslq);
+			AZ(c);
+		}
+
+		i = VSLQ_Dispatch(VUT.vslq, func, priv);
+		if (i == 0) {
+			/* Nothing to do but wait */
+			if (VUT.fo)
+				fflush(VUT.fo);
+			VTIM_sleep(0.01);
+			continue;
+		}
+		if (i == -1) {
+			/* EOF */
+			break;
+		}
+
+		if (VUT.vsm == NULL)
+			break;
+
+		/* XXX: Make continuation optional */
+
+		VSLQ_Flush(VUT.vslq, func, priv);
+		VSLQ_Delete(&VUT.vslq);
+		AZ(VUT.vslq);
+
+		if (i == -2) {
+			/* Abandoned */
+			VUT_Error(0, "Log abandoned - reopening");
+			VSM_Close(VUT.vsm);
+		} else if (i < -2) {
+			/* Overrun */
+			VUT_Error(0, "Log overrun");
+		}
+
+		/* Reconnect VSM */
+		while (VUT.vslq == NULL) {
+			AZ(VUT.r_arg);
+			AN(VUT.vsm);
+			VTIM_sleep(0.1);
+			if (VSM_Open(VUT.vsm)) {
+				VSM_ResetError(VUT.vsm);
+				continue;
+			}
+			c = VSL_CursorVSM(VUT.vsl, VUT.vsm, 1);
+			if (c == NULL) {
+				VSL_ResetError(VUT.vsl);
+				continue;
+			}
+			VUT.vslq = VSLQ_New(VUT.vsl, &c, VUT.g_arg, VUT.query);
+			AN(VUT.vslq);
+			AZ(c);
+		}
+	}
+
+	if (VUT.vslq != NULL)
+		VSLQ_Flush(VUT.vslq, func, priv);
+
+	return (i);
+}



More information about the varnish-commit mailing list