xref: /freebsd/usr.sbin/fifolog/fifolog_reader/fifolog_reader.c (revision d876124d6ae9d56da5b4ff4c6015efd1d0c9222a)
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$
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 <regex.h>
37 
38 #include "libfifolog.h"
39 
40 static time_t opt_B;
41 static time_t opt_E;
42 static const char *opt_T;
43 static const char *opt_o;
44 static const char *opt_R;
45 static regex_t R;
46 
47 static FILE *fo;
48 
49 static void
50 Render(void *priv __unused, time_t now, unsigned flag __unused, const unsigned char *p, unsigned l __unused)
51 {
52 	static struct tm utc;
53 	char buf[128];
54 	int i;
55 
56 	if (now < opt_B || now > opt_E)
57 		return;
58 
59 	if (opt_R != NULL && regexec(&R, (const char *)p, 0, NULL, 0))
60 		return;
61 
62 	if (opt_T != NULL && *opt_T == '\0') {
63 		fprintf(fo, "%s\n", p);
64 	} else if (opt_T != NULL) {
65 		(void)gmtime_r(&now, &utc);
66 		i = strftime(buf, sizeof buf, opt_T, &utc);
67 		assert(i > 0);
68 		fprintf(fo, "%s %s\n", buf, p);
69 	} else {
70 		fprintf(fo, "%12ld %s\n", (long)now, p);
71 	}
72 }
73 
74 /*--------------------------------------------------------------------*/
75 
76 static void
77 Usage(void)
78 {
79 	fprintf(stderr,
80 		"Usage: fiforead [options] fifofile\n"
81 		"\t-b <start time integer>\n"
82 		"\t-B <start time>\n"
83 		"\t-e <end time integer>\n"
84 		"\t-E <end time>\n"
85 		"\t-o <output file>\n"
86 		"\t-R <regexp> # match regexp\n"
87 		"\t-t # format timestamps as %%Y%%m%%d%%H%%M%%S\n"
88 		"\t-T <timestamp format>\n"
89 	);
90 	exit (2);
91 }
92 
93 int
94 main(int argc, char * const *argv)
95 {
96 	int ch, i;
97 	off_t o;
98 	struct fifolog_reader *fl;
99 	const char *progname;
100 
101 	progname=argv[0];
102 
103 	time(&opt_E);
104 	opt_o = "-";
105 	while ((ch = getopt(argc, argv, "b:B:e:E:o:R:tT:")) != -1) {
106 		switch (ch) {
107 		case 'b':
108 			opt_B = strtoul(optarg, NULL, 0);
109 			break;
110 		case 'B':
111 			opt_B = get_date(optarg);
112 			if (opt_B == -1)
113 				errx(1, "Didn't understand \"%s\"", optarg);
114 			break;
115 		case 'e':
116 			opt_E = strtoul(optarg, NULL, 0);
117 			break;
118 		case 'E':
119 			opt_E = get_date(optarg);
120 			if (opt_E == -1)
121 				errx(1, "Didn't understand \"%s\"", optarg);
122 			break;
123 		case 'o':
124 			opt_o = optarg;
125 			break;
126 		case 'R':
127 			opt_R = optarg;
128 			break;
129 		case 't':
130 			opt_T = "%Y%m%d%H%M%S";
131 			break;
132 		case 'T':
133 			opt_T = optarg;
134 			break;
135 		default:
136 			Usage();
137 		}
138 	}
139 	argc -= optind;
140 	argv += optind;
141 
142 	if (opt_R != NULL) {
143 		i = regcomp(&R, opt_R, REG_NOSUB);
144 		if (i != 0) {
145 			char buf[BUFSIZ];
146 			(void)regerror(i, &R, buf, sizeof buf);
147 			fprintf(stderr, "-R argument: %s\n", buf);
148 			exit (1);
149 		}
150 	}
151 
152 	fprintf(stderr, "From\t%jd %s", (intmax_t)opt_B, ctime(&opt_B));
153 	fprintf(stderr, "To\t%jd %s", (intmax_t)opt_E, ctime(&opt_E));
154 	if (opt_B >= opt_E)
155 		errx(1, "Begin time not before End time");
156 
157 	if (argv[0] == NULL)
158 		errx(1, "Usage: %s [options] fifolog", progname);
159 	fl = fifolog_reader_open(argv[0]);
160 
161 	if (!strcmp(opt_o, "-"))
162 		fo = stdout;
163 	else {
164 		fo = fopen(opt_o, "w");
165 		if (fo == NULL)
166 			err(1, "Cannot open: %s", argv[1]);
167 	}
168 
169 	o = fifolog_reader_seek(fl, opt_B);
170 	fifolog_reader_process(fl, o, Render, NULL, opt_E);
171 	return (0);
172 }
173