]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - usr.sbin/fifolog/fifolog_reader/fifolog_reader.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / usr.sbin / fifolog / fifolog_reader / fifolog_reader.c
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) {
63                 (void)gmtime_r(&now, &utc);
64                 i = strftime(buf, sizeof buf, opt_T, &utc);
65                 assert(i > 0);
66                 fprintf(fo, "%s %s\n", buf, p);
67         } else {
68                 fprintf(fo, "%12ld %s\n", (long)now, p);
69         }
70 }
71
72 /*--------------------------------------------------------------------*/
73
74 static void
75 Usage(void)
76 {
77         fprintf(stderr,
78                 "Usage: fiforead [options] fifofile\n"
79                 "\t-b <start time integer>\n"
80                 "\t-B <start time>\n"
81                 "\t-e <end time integer>\n"
82                 "\t-E <end time>\n"
83                 "\t-o <output file>\n"
84                 "\t-R <regexp> # match regexp\n"
85                 "\t-t # format timestamps as %%Y%%m%%d%%H%%M%%S\n"
86                 "\t-T <timestamp format>\n"
87         );
88         exit (2);
89 }
90
91 int
92 main(int argc, char * const *argv)
93 {
94         int ch, i;
95         off_t o;
96         struct fifolog_reader *fl;
97         const char *progname;
98
99         progname=argv[0];
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         fprintf(stderr, "From\t%jd %s", (intmax_t)opt_B, ctime(&opt_B));
151         fprintf(stderr, "To\t%jd %s", (intmax_t)opt_E, ctime(&opt_E));
152         if (opt_B >= opt_E)
153                 errx(1, "Begin time not before End time");
154
155         if (argv[0] == NULL)
156                 errx(1, "Usage: %s [options] fifolog", progname);
157         fl = fifolog_reader_open(argv[0]);
158         
159         if (!strcmp(opt_o, "-"))
160                 fo = stdout;
161         else {
162                 fo = fopen(opt_o, "w");
163                 if (fo == NULL)
164                         err(1, "Cannot open: %s", argv[1]);
165         }
166
167         o = fifolog_reader_seek(fl, opt_B);
168         fifolog_reader_process(fl, o, Render, NULL, opt_E);
169         return (0);
170 }