Deleted Added
sdiff udiff text old ( 179000 ) new ( 219027 )
full compact
1/*-
2 * Copyright (c) 2005-2008 Poul-Henning Kamp
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
13 *
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
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD: head/usr.sbin/fifolog/fifolog_reader/fifolog_reader.c 179000 2008-05-14 23:29:02Z phk $
27 */
28
29#include <stdio.h>
30#include <assert.h>
31#include <unistd.h>
32#include <err.h>
33#include <time.h>
34#include <string.h>
35#include <stdlib.h>
36#include <sysexits.h>
37#include <regex.h>
38
39#include "libfifolog.h"
40
41static time_t opt_B;
42static time_t opt_E;
43static const char *opt_T;
44static const char *opt_o;
45static const char *opt_R;
46static regex_t R;
47
48static FILE *fo;
49
50static void
51Render(void *priv __unused, time_t now, unsigned flag __unused, const unsigned char *p, unsigned l __unused)
52{
53 static struct tm utc;
54 char buf[128];
55 int i;
56
57 if (now < opt_B || now > opt_E)
58 return;
59
60 if (opt_R != NULL && regexec(&R, (const char *)p, 0, NULL, 0))
61 return;
62
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);
68 assert(i > 0);
69 fprintf(fo, "%s %s\n", buf, p);
70 } else {
71 fprintf(fo, "%12ld %s\n", (long)now, p);
72 }
73}
74
75/*--------------------------------------------------------------------*/
76
77static void
78Usage(void)
79{
80 fprintf(stderr,
81 "Usage: fiforead [options] fifofile\n"
82 "\t-b <start time integer>\n"
83 "\t-B <start time>\n"
84 "\t-e <end time integer>\n"
85 "\t-E <end time>\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"
90 );
91 exit (EX_USAGE);
92}
93
94int
95main(int argc, char * const *argv)
96{
97 int ch, i;
98 off_t o;
99 struct fifolog_reader *fl;
100
101 time(&opt_E);
102 opt_o = "-";
103 while ((ch = getopt(argc, argv, "b:B:e:E:o:R:tT:")) != -1) {
104 switch (ch) {
105 case 'b':
106 opt_B = strtoul(optarg, NULL, 0);
107 break;
108 case 'B':
109 opt_B = get_date(optarg);
110 if (opt_B == -1)
111 errx(1, "Didn't understand \"%s\"", optarg);
112 break;
113 case 'e':
114 opt_E = strtoul(optarg, NULL, 0);
115 break;
116 case 'E':
117 opt_E = get_date(optarg);
118 if (opt_E == -1)
119 errx(1, "Didn't understand \"%s\"", optarg);
120 break;
121 case 'o':
122 opt_o = optarg;
123 break;
124 case 'R':
125 opt_R = optarg;
126 break;
127 case 't':
128 opt_T = "%Y%m%d%H%M%S";
129 break;
130 case 'T':
131 opt_T = optarg;
132 break;
133 default:
134 Usage();
135 }
136 }
137 argc -= optind;
138 argv += optind;
139
140 if (opt_R != NULL) {
141 i = regcomp(&R, opt_R, REG_NOSUB);
142 if (i != 0) {
143 char buf[BUFSIZ];
144 (void)regerror(i, &R, buf, sizeof buf);
145 fprintf(stderr, "-R argument: %s\n", buf);
146 exit (1);
147 }
148 }
149
150 if (argv[0] == NULL)
151 Usage();
152
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));
155 if (opt_B >= opt_E)
156 errx(1, "Begin time not before End time");
157
158 fl = fifolog_reader_open(argv[0]);
159
160 if (!strcmp(opt_o, "-"))
161 fo = stdout;
162 else {
163 fo = fopen(opt_o, "w");
164 if (fo == NULL)
165 err(1, "Cannot open: %s", argv[1]);
166 }
167
168 o = fifolog_reader_seek(fl, opt_B);
169 fifolog_reader_process(fl, o, Render, NULL, opt_E);
170 return (0);
171}