[master] a09e2e0 Add vut.[ch] to hold functions common to the utilities.
Martin Blix Grydeland
martin at varnish-cache.org
Thu Jun 13 12:41:23 CEST 2013
commit a09e2e0886cce266435b7b2e2ec9db61563acaa3
Author: Martin Blix Grydeland <martin at varnish-software.com>
Date: Thu May 16 18:14:01 2013 +0200
Add vut.[ch] to hold functions common to the utilities.
Move some parsing of options to these files.
diff --git a/bin/varnishlog/Makefile.am b/bin/varnishlog/Makefile.am
index b163259..77e04a8 100644
--- a/bin/varnishlog/Makefile.am
+++ b/bin/varnishlog/Makefile.am
@@ -8,6 +8,8 @@ dist_man_MANS = varnishlog.1
varnishlog_SOURCES = \
varnishlog.c \
+ vut.c \
+ vut.h \
$(top_builddir)/lib/libvarnish/vas.c \
$(top_builddir)/lib/libvarnish/flopen.c \
$(top_builddir)/lib/libvarnish/version.c \
diff --git a/bin/varnishlog/varnishlog.c b/bin/varnishlog/varnishlog.c
index ee4b2bc..1cf1217 100644
--- a/bin/varnishlog/varnishlog.c
+++ b/bin/varnishlog/varnishlog.c
@@ -47,26 +47,7 @@
#include "vpf.h"
#include "vsb.h"
#include "vtim.h"
-
-#include "compat/daemon.h"
-
-static void error(int status, const char *fmt, ...)
- __printflike(2, 3);
-
-static void
-error(int status, const char *fmt, ...)
-{
- va_list ap;
-
- AN(fmt);
- va_start(ap, fmt);
- vfprintf(stderr, fmt, ap); /* XXX: syslog on daemon */
- va_end(ap);
- fprintf(stderr, "\n");
-
- if (status)
- exit(status);
-}
+#include "vut.h"
static void
usage(void)
@@ -78,56 +59,46 @@ usage(void)
int
main(int argc, char * const *argv)
{
- char optchar;
- int d_opt = 0;
+ char opt;
+ struct VUT *vut;
struct VSL_data *vsl;
struct VSM_data *vsm;
struct VSL_cursor *c;
struct VSLQ *q;
- int grouping = VSL_g_vxid;
int i;
+ vut = VUT_New();
+ AN(vut);
vsl = VSL_New();
AN(vsl);
vsm = VSM_New();
AN(vsm);
- while ((optchar = getopt(argc, argv, "dg:n:r:v")) != -1) {
- switch (optchar) {
- case 'd':
- d_opt = 1;
- break;
- case 'g':
- /* Grouping mode */
- grouping = VSLQ_Name2Grouping(optarg, -1);
- if (grouping == -2)
- error(1, "Ambiguous grouping type: %s", optarg);
- else if (grouping < 0)
- error(1, "Unknown grouping type: %s", optarg);
- break;
+ while ((opt = getopt(argc, argv, "dg:n:r:v")) != -1) {
+ switch (opt) {
case 'n':
/* Instance name */
if (VSM_n_Arg(vsm, optarg) > 0)
break;
default:
- if (!VSL_Arg(vsl, optchar, optarg))
+ if (!VSL_Arg(vsl, opt, optarg) &&
+ !VUT_Arg(vut, opt, optarg))
usage();
}
}
- assert(grouping >= 0 && grouping <= VSL_g_session);
/* Create cursor */
if (VSM_Open(vsm))
- error(1, "VSM_Open: %s", VSM_Error(vsm));
- c = VSL_CursorVSM(vsl, vsm, !d_opt);
+ VUT_Error(1, "VSM_Open: %s", VSM_Error(vsm));
+ c = VSL_CursorVSM(vsl, vsm, !vut->d_opt);
if (c == NULL)
- error(1, "VSL_CursorVSM: %s", VSL_Error(vsl));
+ VUT_Error(1, "VSL_CursorVSM: %s", VSL_Error(vsl));
/* Create query */
- q = VSLQ_New(vsl, &c, grouping, argv[optind]);
+ q = VSLQ_New(vsl, &c, vut->g_arg, argv[optind]);
if (q == NULL)
- error(1, "VSLQ_New: %s", VSL_Error(vsl));
+ VUT_Error(1, "VSLQ_New: %s", VSL_Error(vsl));
AZ(c);
while (1) {
@@ -142,7 +113,7 @@ main(int argc, char * const *argv)
VSL_ResetError(vsl);
continue;
}
- q = VSLQ_New(vsl, &c, grouping, argv[optind]);
+ q = VSLQ_New(vsl, &c, vut->g_arg, argv[optind]);
AN(q);
AZ(c);
}
@@ -161,14 +132,14 @@ main(int argc, char * const *argv)
AZ(q);
if (i == -2) {
/* Abandoned */
- error(0, "Log abandoned - reopening");
+ VUT_Error(0, "Log abandoned - reopening");
VSM_Close(vsm);
} else if (i < -2) {
/* Overrun */
- error(0, "Log overrun");
+ VUT_Error(0, "Log overrun");
}
} else {
- error(1, "Unexpected: %d", i);
+ VUT_Error(1, "Unexpected: %d", i);
}
}
@@ -179,6 +150,7 @@ main(int argc, char * const *argv)
}
VSL_Delete(vsl);
VSM_Delete(vsm);
+ VUT_Delete(&vut);
exit(0);
}
diff --git a/bin/varnishlog/vut.c b/bin/varnishlog/vut.c
new file mode 100644
index 0000000..b6711ec
--- /dev/null
+++ b/bin/varnishlog/vut.c
@@ -0,0 +1,112 @@
+/*-
+ * 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 "vas.h"
+#include "miniobj.h"
+
+#include "vut.h"
+
+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);
+}
+
+struct VUT*
+VUT_New(void)
+{
+ struct VUT *vut;
+
+ vut = calloc(1, sizeof *vut);
+ AN(vut);
+ vut->g_arg = VSL_g_vxid;
+
+ return (vut);
+}
+
+void
+VUT_Delete(struct VUT **pvut)
+{
+ struct VUT *vut;
+
+ AN(pvut);
+ vut = *pvut;
+ *pvut = NULL;
+ AN(vut);
+
+ free(vut->r_arg);
+
+ free(vut);
+}
+
+int
+VUT_g_Arg(struct VUT *vut, 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(struct VUT *vut, int opt, const char *arg)
+{
+ switch (opt) {
+ case 'd': vut->d_opt = 1; return (1);
+ case 'g': return (VUT_g_Arg(vut, arg));
+ case 'r': REPLACE(vut->r_arg, arg); return (1);
+ default: return (0);
+ }
+}
diff --git a/bin/varnishlog/vut.h b/bin/varnishlog/vut.h
new file mode 100644
index 0000000..6d72755
--- /dev/null
+++ b/bin/varnishlog/vut.h
@@ -0,0 +1,49 @@
+/*-
+ * 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 {
+ int d_opt;
+ int g_arg;
+ char *r_arg;
+};
+
+void VUT_Error(int status, const char *fmt, ...)
+ __printflike(2, 3);
+
+struct VUT *VUT_New(void);
+
+void VUT_Delete(struct VUT **pvut);
+
+int VUT_g_Arg(struct VUT *vut, const char *arg);
+
+int VUT_Arg(struct VUT *vut, int opt, const char *arg);
More information about the varnish-commit
mailing list