2 * Copyright (c) 2005-2008 Poul-Henning Kamp
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 #include "libfifolog.h"
43 static const char *opt_T;
44 static const char *opt_o;
45 static const char *opt_R;
51 Render(void *priv __unused, time_t now, unsigned flag __unused, const unsigned char *p, unsigned l __unused)
57 if (now < opt_B || now > opt_E)
60 if (opt_R != NULL && regexec(&R, (const char *)p, 0, NULL, 0))
63 if (opt_T != NULL && *opt_T == '\0') {
64 fprintf(fo, "%s\n", p);
65 } else if (opt_T != NULL) {
66 (void)gmtime_r(&now, &utc);
67 i = strftime(buf, sizeof buf, opt_T, &utc);
69 fprintf(fo, "%s %s\n", buf, p);
71 fprintf(fo, "%12ld %s\n", (long)now, p);
75 /*--------------------------------------------------------------------*/
81 "Usage: fiforead [options] fifofile\n"
82 "\t-b <start time integer>\n"
84 "\t-e <end time integer>\n"
86 "\t-o <output file>\n"
87 "\t-R <regexp> # match regexp\n"
88 "\t-t # format timestamps as %%Y%%m%%d%%H%%M%%S\n"
89 "\t-T <timestamp format>\n"
95 main(int argc, char * const *argv)
99 struct fifolog_reader *fl;
103 while ((ch = getopt(argc, argv, "b:B:e:E:o:R:tT:")) != -1) {
106 opt_B = strtoul(optarg, NULL, 0);
109 opt_B = get_date(optarg);
111 errx(1, "Didn't understand \"%s\"", optarg);
114 opt_E = strtoul(optarg, NULL, 0);
117 opt_E = get_date(optarg);
119 errx(1, "Didn't understand \"%s\"", optarg);
128 opt_T = "%Y%m%d%H%M%S";
141 i = regcomp(&R, opt_R, REG_NOSUB);
144 (void)regerror(i, &R, buf, sizeof buf);
145 fprintf(stderr, "-R argument: %s\n", buf);
153 fprintf(stderr, "From\t%jd %s", (intmax_t)opt_B, ctime(&opt_B));
154 fprintf(stderr, "To\t%jd %s", (intmax_t)opt_E, ctime(&opt_E));
156 errx(1, "Begin time not before End time");
158 fl = fifolog_reader_open(argv[0]);
160 if (!strcmp(opt_o, "-"))
163 fo = fopen(opt_o, "w");
165 err(1, "Cannot open: %s", argv[1]);
168 o = fifolog_reader_seek(fl, opt_B);
169 fifolog_reader_process(fl, o, Render, NULL, opt_E);