aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSergey Poznyakoff <gray@gnu.org.ua>2013-10-14 15:28:51 +0300
committerSergey Poznyakoff <gray@gnu.org.ua>2013-10-14 17:54:41 +0300
commitf035194d7d1b6cc0846ad7a5d86e0d6fa9463c67 (patch)
tree494d180cee9a078727c8d4965caa65cb2052f33d
parent58f7dbc0658b3d73816a1bc91b75c2bdac733510 (diff)
downloadvmod-binlog-f035194d7d1b6cc0846ad7a5d86e0d6fa9463c67.tar.gz
vmod-binlog-f035194d7d1b6cc0846ad7a5d86e0d6fa9463c67.tar.bz2
New utility binlogsel
* configure.ac: Check for yacc. * src/.gitignore: Update. * src/Makefile.am (libbinlog_a_SOURCES): Add new files. Build binlogsel. * src/binlogcat.c: Use xmalloc. * src/binlogsel.c: New file. * src/parse-datetime.h: New file. * src/parse-datetime.y: New file. * src/xalloc.c: New file. * src/xalloc.h: New file.
-rw-r--r--configure.ac2
-rw-r--r--src/.gitignore2
-rw-r--r--src/Makefile.am9
-rw-r--r--src/binlogcat.c17
-rw-r--r--src/binlogsel.c474
-rw-r--r--src/parse-datetime.h3
-rw-r--r--src/parse-datetime.y1598
-rw-r--r--src/xalloc.c53
-rw-r--r--src/xalloc.h3
9 files changed, 2145 insertions, 16 deletions
diff --git a/configure.ac b/configure.ac
index ea7c061..2bb8f38 100644
--- a/configure.ac
+++ b/configure.ac
@@ -32,7 +32,7 @@ if test "x$ac_cv_prog_cc_c99" = xno; then
32 AC_MSG_ERROR([could not find a C99 compatible compiler]) 32 AC_MSG_ERROR([could not find a C99 compatible compiler])
33fi 33fi
34AC_PROG_CPP 34AC_PROG_CPP
35 35AC_PROG_YACC
36AC_PROG_INSTALL 36AC_PROG_INSTALL
37AC_PROG_LIBTOOL 37AC_PROG_LIBTOOL
38AC_PROG_MAKE_SET 38AC_PROG_MAKE_SET
diff --git a/src/.gitignore b/src/.gitignore
index a75fe6d..8f5a6ab 100644
--- a/src/.gitignore
+++ b/src/.gitignore
@@ -1,3 +1,5 @@
1binlogcat 1binlogcat
2binlogsel
3parse-datetime.c
2vcc_if.c 4vcc_if.c
3vcc_if.h 5vcc_if.h
diff --git a/src/Makefile.am b/src/Makefile.am
index 4e31e89..565444a 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -17,13 +17,16 @@
17AM_CPPFLAGS = -I$(VARNISHSRC)/include -I$(VARNISHSRC) 17AM_CPPFLAGS = -I$(VARNISHSRC)/include -I$(VARNISHSRC)
18 18
19noinst_LIBRARIES = libbinlog.a 19noinst_LIBRARIES = libbinlog.a
20libbinlog_a_SOURCES = pack.c err.c 20libbinlog_a_SOURCES = pack.c err.c parse-datetime.y xalloc.c
21libbinlog_a_CFLAGS = $(AM_CFLAGS) 21libbinlog_a_CFLAGS = $(AM_CFLAGS)
22 22
23bin_PROGRAMS = binlogcat 23bin_PROGRAMS = binlogcat binlogsel
24binlogcat_SOURCES = binlogcat.c 24binlogcat_SOURCES = binlogcat.c
25binlogcat_LDADD = ./libbinlog.a 25binlogcat_LDADD = ./libbinlog.a
26 26
27binlogsel_SOURCES = binlogsel.c
28binlogsel_LDADD = ./libbinlog.a
29
27vmoddir = $(VMODDIR) 30vmoddir = $(VMODDIR)
28vmod_LTLIBRARIES = libvmod_binlog.la 31vmod_LTLIBRARIES = libvmod_binlog.la
29 32
@@ -36,7 +39,7 @@ libvmod_binlog_la_SOURCES = \
36 vmod-binlog.h\ 39 vmod-binlog.h\
37 vcc_if.c vcc_if.h 40 vcc_if.c vcc_if.h
38 41
39noinst_HEADERS = pack.h err.h 42noinst_HEADERS = pack.h err.h parse-datetime.h xalloc.h
40 43
41BUILT_SOURCES = vcc_if.c vcc_if.h 44BUILT_SOURCES = vcc_if.c vcc_if.h
42 45
diff --git a/src/binlogcat.c b/src/binlogcat.c
index 505bbc2..656d8f2 100644
--- a/src/binlogcat.c
+++ b/src/binlogcat.c
@@ -27,6 +27,7 @@
27#include "vmod-binlog.h" 27#include "vmod-binlog.h"
28#include "pack.h" 28#include "pack.h"
29#include "err.h" 29#include "err.h"
30#include "xalloc.h"
30 31
31char *timefmt = "%c"; 32char *timefmt = "%c";
32int number_option; 33int number_option;
@@ -75,11 +76,7 @@ catlog(const char *fname)
75 } 76 }
76 77
77 size = header.hdrsize - sizeof(header); 78 size = header.hdrsize - sizeof(header);
78 dataspec = malloc(size); 79 dataspec = xmalloc(size);
79 if (!dataspec) {
80 error("not enough memory");
81 abort();
82 }
83 80
84 if (fread(dataspec, size, 1, fp) != 1) { 81 if (fread(dataspec, size, 1, fp) != 1) {
85 error("error reading header of %s: %s", 82 error("error reading header of %s: %s",
@@ -94,7 +91,7 @@ catlog(const char *fname)
94 inst = packcomp(dataspec, &p); 91 inst = packcomp(dataspec, &p);
95 if (!inst) { 92 if (!inst) {
96 if (errno == EINVAL) { 93 if (errno == EINVAL) {
97 error("%s: bad dataspec near %s", dataspec, p); 94 error("%s: %s: bad dataspec near %s", fname, dataspec, p);
98 exit(1); 95 exit(1);
99 } 96 }
100 97
@@ -103,11 +100,7 @@ catlog(const char *fname)
103 } 100 }
104 free(dataspec); 101 free(dataspec);
105 102
106 rec = malloc(header.recsize); 103 rec = xmalloc(header.recsize);
107 if (!rec) {
108 error("not enough memory");
109 exit(1);
110 }
111 env = packenv_create(header.recsize - 104 env = packenv_create(header.recsize -
112 offsetof(struct binlog_record,data)); 105 offsetof(struct binlog_record,data));
113 env->fp = stdout; 106 env->fp = stdout;
@@ -147,7 +140,7 @@ catlog(const char *fname)
147void 140void
148help() 141help()
149{ 142{
150 printf("usage: %s [-dhnv] [t FORMAT] [FILE...]\n", progname); 143 printf("usage: %s [-dhnv] [-t FORMAT] [FILE...]\n", progname);
151} 144}
152 145
153int 146int
diff --git a/src/binlogsel.c b/src/binlogsel.c
new file mode 100644
index 0000000..62299e5
--- /dev/null
+++ b/src/binlogsel.c
@@ -0,0 +1,474 @@
1/* This file is part of vmod-binlog
2 Copyright (C) 2013 Sergey Poznyakoff
3
4 Vmod-binlog is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3, or (at your option)
7 any later version.
8
9 Vmod-binlog is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with vmod-binlog. If not, see <http://www.gnu.org/licenses/>.
16*/
17
18#include <config.h>
19#include <unistd.h>
20#include <sys/stat.h>
21#include <sys/mman.h>
22#include <fcntl.h>
23#include <stddef.h>
24#include <stdio.h>
25#include <stdlib.h>
26#include <stdarg.h>
27#include <errno.h>
28#include <time.h>
29#include <string.h>
30#include <glob.h>
31#include "vmod-binlog.h"
32#include "pack.h"
33#include "err.h"
34#include "xalloc.h"
35#include "parse-datetime.h"
36
37char *timefmt = "%c";
38int number_option;
39int verbose_option;
40int timediff_option;
41char *pattern;
42
43#define FROM_TIME 0x01
44#define TO_TIME 0x02
45int timemask;
46time_t from_time, to_time;
47
48void
49help()
50{
51 printf("usage: %s [-dhnv] [-t FORMAT] [-F FROMTIME] [-T TOTIME] [-p PATTERN] [-D DIR] [FILE...]\n", progname);
52}
53
54/* Convert strftime-like pattern into globbing pattern */
55void
56convpattern(const char *dir)
57{
58 char *p, *q;
59 char *newpat;
60 size_t size = strlen(pattern) + 1;
61
62 if (dir)
63 size += strlen(dir) + 1;
64
65 newpat = xmalloc(size);
66
67 p = newpat;
68 if (dir) {
69 strcpy(p, dir);
70 p += strlen(dir);
71 *p++ = '/';
72 }
73
74 for (q = pattern; *q; ) {
75 if (*q == '%') {
76 *p++ = '*';
77 q += 2;
78 } else
79 *p++ = *q++;
80 }
81 *p = 0;
82 pattern = newpat;
83}
84
85#define getrec(base, recsize, n) \
86 ((struct binlog_record*)((char *)(base) + (n) * (recsize)))
87
88int
89searchts(void *base, size_t recsize, size_t from, size_t to, time_t ts,
90 size_t *ret)
91{
92 struct binlog_record *rec;
93 size_t middle;
94
95 rec = getrec(base, recsize, from);
96
97 if (ts < rec->ts) {
98 *ret = from;
99 return -1;
100 }
101
102 if (to == from) {