summaryrefslogtreecommitdiffstats
path: root/usr.sbin/fifolog/fifolog_reader/fifolog_reader.c
blob: 6c4dc4150e2d9164d5c435d05e143b20edea26d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*-
 * Copyright (c) 2005-2008 Poul-Henning Kamp
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * $FreeBSD$
 */

#include <stdio.h>
#include <assert.h>
#include <unistd.h>
#include <err.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>
#include <regex.h>

#include "libfifolog.h"

static time_t opt_B;
static time_t opt_E;
static const char *opt_T;
static const char *opt_o;
static const char *opt_R;
static regex_t R;

static FILE *fo;

static void
Render(void *priv __unused, time_t now, unsigned flag __unused, const unsigned char *p, unsigned l __unused)
{
	static struct tm utc;
	char buf[128];
	int i;

	if (now < opt_B || now > opt_E)
		return;

	if (opt_R != NULL && regexec(&R, (const char *)p, 0, NULL, 0))
		return;

	if (opt_T != NULL) {
		(void)gmtime_r(&now, &utc);
		i = strftime(buf, sizeof buf, opt_T, &utc);
		assert(i > 0);
		fprintf(fo, "%s %s\n", buf, p);
	} else {
		fprintf(fo, "%12ld %s\n", (long)now, p);
	}
}

/*--------------------------------------------------------------------*/

static void
Usage(void)
{
	fprintf(stderr,
		"Usage: fiforead [options] fifofile\n"
		"\t-b <start time integer>\n"
		"\t-B <start time>\n"
		"\t-e <end time integer>\n"
		"\t-E <end time>\n"
		"\t-o <output file>\n"
		"\t-R <regexp> # match regexp\n"
		"\t-t # format timestamps as %%Y%%m%%d%%H%%M%%S\n"
		"\t-T <timestamp format>\n"
	);
	exit (2);
}

int
main(int argc, char * const *argv)
{
	int ch, i;
	off_t o;
	struct fifolog_reader *fl;
	const char *progname;

	progname=argv[0];

	time(&opt_E);
	opt_o = "-";
	while ((ch = getopt(argc, argv, "b:B:e:E:o:R:tT:")) != -1) {
		switch (ch) {
		case 'b':
			opt_B = strtoul(optarg, NULL, 0);
			break;
		case 'B':
			opt_B = get_date(optarg);
			if (opt_B == -1)
				errx(1, "Didn't understand \"%s\"", optarg);
			break;
		case 'e':
			opt_E = strtoul(optarg, NULL, 0);
			break;
		case 'E':
			opt_E = get_date(optarg);
			if (opt_E == -1)
				errx(1, "Didn't understand \"%s\"", optarg);
			break;
		case 'o':
			opt_o = optarg;
			break;
		case 'R':
			opt_R = optarg;
			break;
		case 't':
			opt_T = "%Y%m%d%H%M%S";
			break;
		case 'T':
			opt_T = optarg;
			break;
		default:
			Usage();
		}
	}
	argc -= optind;
	argv += optind;

	if (opt_R != NULL) {
		i = regcomp(&R, opt_R, REG_NOSUB);
		if (i != 0) {
			char buf[BUFSIZ];
			(void)regerror(i, &R, buf, sizeof buf);
			fprintf(stderr, "-R argument: %s\n", buf);
			exit (1);
		}
	}

	fprintf(stderr, "From\t%jd %s", (intmax_t)opt_B, ctime(&opt_B));
	fprintf(stderr, "To\t%jd %s", (intmax_t)opt_E, ctime(&opt_E));
	if (opt_B >= opt_E)
		errx(1, "Begin time not before End time");

	if (argv[0] == NULL)
		errx(1, "Usage: %s [options] fifolog", progname);
	fl = fifolog_reader_open(argv[0]);
	
	if (!strcmp(opt_o, "-"))
		fo = stdout;
	else {
		fo = fopen(opt_o, "w");
		if (fo == NULL)
			err(1, "Cannot open: %s", argv[1]);
	}

	o = fifolog_reader_seek(fl, opt_B);
	fifolog_reader_process(fl, o, Render, NULL, opt_E);
	return (0);
}
OpenPOWER on IntegriCloud