diff options
author | Sergey Poznyakoff <gray@gnu.org.ua> | 2013-10-13 12:44:12 +0300 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org.ua> | 2013-10-13 12:48:04 +0300 |
commit | 23f262e8c6181465347c3bb47644f9d86fcb7432 (patch) | |
tree | cdf5ac3a861395ade00b71e7b8cdd641db67032c | |
parent | 510fc646f13b843d046323e3477edb6c1bc2258d (diff) | |
download | vmod-binlog-23f262e8c6181465347c3bb47644f9d86fcb7432.tar.gz vmod-binlog-23f262e8c6181465347c3bb47644f9d86fcb7432.tar.bz2 |
* src/Makefile.am: New convenience library libbinlog.a
* src/binlogcat.c: Use functions from libbinlog.a
* src/err.h: New file.
* src/err.c: New file.
* tests/.gitignore: Add binpack.c
* tests/Makefile.am: Build binpack.c
* tests/binpack.c: New file.
-rw-r--r-- | src/Makefile.am | 10 | ||||
-rw-r--r-- | src/binlogcat.c | 34 | ||||
-rw-r--r-- | src/err.c | 62 | ||||
-rw-r--r-- | src/err.h | 21 | ||||
-rw-r--r-- | tests/.gitignore | 1 | ||||
-rw-r--r-- | tests/Makefile.am | 7 | ||||
-rw-r--r-- | tests/binpack.c | 86 |
7 files changed, 187 insertions, 34 deletions
diff --git a/src/Makefile.am b/src/Makefile.am index 9ce519f..4e31e89 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -13,15 +13,19 @@ # # You should have received a copy of the GNU General Public License # along with vmod-binlog. If not, see <http://www.gnu.org/licenses/>. AM_CPPFLAGS = -I$(VARNISHSRC)/include -I$(VARNISHSRC) +noinst_LIBRARIES = libbinlog.a +libbinlog_a_SOURCES = pack.c err.c +libbinlog_a_CFLAGS = $(AM_CFLAGS) + bin_PROGRAMS = binlogcat -binlogcat_SOURCES = binlogcat.c pack.c -binlogcat_CFLAGS = $(AM_CFLAGS) +binlogcat_SOURCES = binlogcat.c +binlogcat_LDADD = ./libbinlog.a vmoddir = $(VMODDIR) vmod_LTLIBRARIES = libvmod_binlog.la libvmod_binlog_la_LDFLAGS = -module -export-dynamic -avoid-version libvmod_binlog_la_LIBADD= @@ -29,13 +33,13 @@ libvmod_binlog_la_LIBADD= libvmod_binlog_la_SOURCES = \ binlog.c\ pack.c\ vmod-binlog.h\ vcc_if.c vcc_if.h -noinst_HEADERS = pack.h +noinst_HEADERS = pack.h err.h BUILT_SOURCES = vcc_if.c vcc_if.h vcc_if.c vcc_if.h: $(VARNISHSRC)/lib/libvmod_std/vmod.py $(top_srcdir)/src/vmod.vcc @PYTHON@ $(VARNISHSRC)/lib/libvmod_std/vmod.py $(top_srcdir)/src/vmod.vcc diff --git a/src/binlogcat.c b/src/binlogcat.c index 8cdfaaa..bdeb992 100644 --- a/src/binlogcat.c +++ b/src/binlogcat.c @@ -23,48 +23,20 @@ #include <stdarg.h> #include <errno.h> #include <time.h> #include <string.h> #include "vmod-binlog.h" #include "pack.h" +#include "err.h" -char *progname; char *timefmt = "%c"; int number_option; int verbose_option; int timediff_option; void -verror(const char *fmt, va_list ap) -{ - fprintf(stderr, "%s: ", progname); - vfprintf(stderr, fmt, ap); - fputc('\n', stderr); -} - -void -error(const char *fmt, ...) -{ - va_list ap; - - va_start(ap, fmt); - verror(fmt, ap); - va_end(ap); -} - -void -packerror(const char *fmt, ...) -{ - va_list ap; - - va_start(ap, fmt); - verror(fmt, ap); - va_end(ap); -} - -void catlog(const char *fname) { FILE *fp; struct binlog_file_header header; struct binlog_record *rec; char timebuf[128]; @@ -95,13 +67,13 @@ catlog(const char *fname) if (memcmp(header.magic, BINLOG_MAGIC_STR, BINLOG_MAGIC_LEN)) { error("%s is not a binlog file", fname); exit(1); } if (header.version != BINLOG_VERSION) { - error("%s: unknown version", progname, fname); + error("%s: unknown version", fname); exit(1); } size = header.hdrsize - sizeof(header); dataspec = malloc(size); if (!dataspec) { @@ -173,15 +145,15 @@ help() printf("usage: %s [-dhnv] [t FORMAT] [FILE...]\n", progname); } int main(int argc, char **argv) { - progname = argv[0]; int c; + setprogname(argv[0]); while ((c = getopt(argc, argv, "dht:nv")) != EOF) switch (c) { case 'd': timediff_option = 1; timefmt = "%s"; break; diff --git a/src/err.c b/src/err.c new file mode 100644 index 0000000..73aaba7 --- /dev/null +++ b/src/err.c @@ -0,0 +1,62 @@ +/* This file is part of vmod-binlog + Copyright (C) 2013 Sergey Poznyakoff + + Vmod-binlog is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3, or (at your option) + any later version. + + Vmod-binlog is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with vmod-binlog. If not, see <http://www.gnu.org/licenses/>. +*/ +#include <config.h> +#include <stdio.h> +#include <stdarg.h> +#include <string.h> +#include "err.h" + +const char *progname; + +void +setprogname(const char *arg) +{ + char *p = strrchr(arg, '/'); + if (p) + progname = p + 1; + else + progname = arg; +} + +void +verror(const char *fmt, va_list ap) +{ + fprintf(stderr, "%s: ", progname); + vfprintf(stderr, fmt, ap); + fputc('\n', stderr); +} + +void +error(const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + verror(fmt, ap); + va_end(ap); +} + +void +packerror(const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + verror(fmt, ap); + va_end(ap); +} + diff --git a/src/err.h b/src/err.h new file mode 100644 index 0000000..69139fd --- /dev/null +++ b/src/err.h @@ -0,0 +1,21 @@ +/* This file is part of vmod-binlog + Copyright (C) 2013 Sergey Poznyakoff + + Vmod-binlog is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3, or (at your option) + any later version. + + Vmod-binlog is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with vmod-binlog. If not, see <http://www.gnu.org/licenses/>. +*/ +extern const char *progname; + +void setprogname(const char *); +void error(const char *fmt, ...); +void packerror(const char *fmt, ...); diff --git a/tests/.gitignore b/tests/.gitignore index 93f8f46..f689f0d 100644 --- a/tests/.gitignore +++ b/tests/.gitignore @@ -1,6 +1,7 @@ atconfig atlocal +binpack package.m4 testsuite testsuite.dir testsuite.log diff --git a/tests/Makefile.am b/tests/Makefile.am index 7f24aeb..4e3bc7b 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -62,10 +62,17 @@ check-local: atconfig atlocal $(TESTSUITE) $(SHELL) $(TESTSUITE) # Run the test suite on the *installed* tree. #installcheck-local: # $(SHELL) $(TESTSUITE) AUTOTEST_PATH=$(exec_prefix)/bin +check_PROGRAMS = binpack +binpack_SOURCES = binpack.c +binpack_LDADD = ../src/libbinlog.a + +binpack_CFLAGS = $(AM_CFLAGS) + +AM_CPPFLAGS = -I$(top_srcdir)/src diff --git a/tests/binpack.c b/tests/binpack.c new file mode 100644 index 0000000..b54ed80 --- /dev/null +++ b/tests/binpack.c @@ -0,0 +1,86 @@ +/* This file is part of vmod-binlog + Copyright (C) 2013 Sergey Poznyakoff + + Vmod-binlog is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3, or (at your option) + any later version. + + Vmod-binlog is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with vmod-binlog. If not, see <http://www.gnu.org/licenses/>. +*/ +#include <unistd.h> +#include <stdlib.h> +#include <stdio.h> +#include <stdarg.h> +#include "pack.h" +#include "err.h" + +int +main(int argc, char **argv) +{ + enum { mode_packin, mode_packout } mode = mode_packin; + struct packinst *pi; + struct packenv *env; + char *end; + int c; + + setprogname(argv[0]); + + while ((c = getopt(argc, argv, "d")) != EOF) { + switch (c) { + case 'd': + mode = mode_packout; + break; + default: + exit(1); + } + } + + argc -= optind; + argv += optind; + + if (argc == 0) { + error("not enough arguments"); + exit(1); + } + + pi = packcomp(argv[0], &end); + if (!pi) { + error("out of memory"); + abort(); + } + if (*end) { + error("compile error near %s", end); + exit(1); + } + env = packenv_create(packsize(pi)); + if (!env) { + error("out of memory"); + abort(); + } + + switch (mode) { + case mode_packin: + env->argv = argv + 1; + env->argc = argc - 1; + packin(pi, env); + fwrite(env->buf_base, env->buf_size, 1, stdout); + break; + + case mode_packout: + env->fp = stdout; + fread(env->buf_base, env->buf_size, 1, stdin); + packout(pi, env); + fputc('\n', stdout); + break; + } + + packenv_free(env); + packfree(pi); +} |