1329dcbaphk/*-
27551d83pfg * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
37551d83pfg *
4329dcbaphk * Copyright (c) 2005-2008 Poul-Henning Kamp
5329dcbaphk * All rights reserved.
6329dcbaphk *
7329dcbaphk * Redistribution and use in source and binary forms, with or without
8329dcbaphk * modification, are permitted provided that the following conditions
9329dcbaphk * are met:
10329dcbaphk * 1. Redistributions of source code must retain the above copyright
11329dcbaphk *    notice, this list of conditions and the following disclaimer.
12329dcbaphk * 2. Redistributions in binary form must reproduce the above copyright
13329dcbaphk *    notice, this list of conditions and the following disclaimer in the
14329dcbaphk *    documentation and/or other materials provided with the distribution.
15329dcbaphk *
16329dcbaphk * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17329dcbaphk * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18329dcbaphk * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19329dcbaphk * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20329dcbaphk * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21329dcbaphk * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22329dcbaphk * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23329dcbaphk * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24329dcbaphk * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25329dcbaphk * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26329dcbaphk * SUCH DAMAGE.
27329dcbaphk *
28329dcbaphk * $FreeBSD$
29329dcbaphk */
30329dcbaphk
31329dcbaphk#include <stdio.h>
32329dcbaphk#include <stdlib.h>
33b6c607ephk#include <sysexits.h>
34329dcbaphk#include <err.h>
35329dcbaphk#include <unistd.h>
36329dcbaphk#include <ctype.h>
37329dcbaphk#include <assert.h>
38329dcbaphk#include <poll.h>
39329dcbaphk#include <string.h>
40329dcbaphk#include <zlib.h>
41329dcbaphk
42329dcbaphk#include "libfifolog.h"
43329dcbaphk
44b6c607ephkstatic void
45b6c607ephkusage(void)
46b6c607ephk{
47851c06bphk	fprintf(stderr,
48851c06bphk	    "Usage: fifolog_writer [-w write-rate] [-s sync-rate] "
49b6c607ephk	    "[-z compression] file\n");
50b6c607ephk	exit(EX_USAGE);
51b6c607ephk}
52b6c607ephk
53329dcbaphkint
54329dcbaphkmain(int argc, char * const *argv)
55329dcbaphk{
56329dcbaphk	struct fifolog_writer *f;
57329dcbaphk	const char *es;
58329dcbaphk	struct pollfd pfd[1];
59329dcbaphk	char buf[BUFSIZ], *p;
60329dcbaphk	int i, c;
61329dcbaphk	unsigned w_opt = 10;
62329dcbaphk	unsigned s_opt = 60;
63329dcbaphk	unsigned z_opt = Z_BEST_COMPRESSION;
64329dcbaphk
65329dcbaphk	while ((c = getopt(argc, argv, "w:s:z:")) != -1) {
66329dcbaphk		switch(c) {
67329dcbaphk		case 'w':
68329dcbaphk			w_opt = strtoul(optarg, NULL, 0);
69329dcbaphk			break;
70329dcbaphk		case 's':
71329dcbaphk			s_opt = strtoul(optarg, NULL, 0);
72329dcbaphk			break;
73329dcbaphk		case 'z':
74329dcbaphk			z_opt = strtoul(optarg, NULL, 0);
75329dcbaphk			break;
76329dcbaphk		default:
77b6c607ephk			usage();
78329dcbaphk		}
79329dcbaphk	}
80329dcbaphk	argc -= optind;
81329dcbaphk	argv += optind;
828642393phk	if (argc != 1)
83b6c607ephk		usage();
84329dcbaphk
85329dcbaphk	if (z_opt > 9)
86b6c607ephk		usage();
87329dcbaphk
88329dcbaphk	if (w_opt > s_opt)
89b6c607ephk		usage();
90329dcbaphk
91329dcbaphk	f = fifolog_write_new();
92329dcbaphk	assert(f != NULL);
93329dcbaphk
94329dcbaphk	es = fifolog_write_open(f, argv[0], w_opt, s_opt, z_opt);
95329dcbaphk	if (es)
96329dcbaphk		err(1, "Error: %s", es);
97329dcbaphk
98329dcbaphk	while (1) {
99329dcbaphk		pfd[0].fd = 0;
100329dcbaphk		pfd[0].events = POLLIN;
101329dcbaphk		i = poll(pfd, 1, 1000);
102329dcbaphk		if (i == 1) {
103329dcbaphk			if (fgets(buf, sizeof buf, stdin) == NULL)
104329dcbaphk				break;
105329dcbaphk			p = strchr(buf, '\0');
106329dcbaphk			assert(p != NULL);
107329dcbaphk			while (p > buf && isspace(p[-1]))
108329dcbaphk				p--;
109329dcbaphk			*p = '\0';
110329dcbaphk			if (*buf != '\0')
111851c06bphk				fifolog_write_record_poll(f, 0, 0, buf, 0);
112329dcbaphk		} else if (i == 0)
113851c06bphk			fifolog_write_poll(f, 0);
114329dcbaphk	}
115851c06bphk	fifolog_write_close(f);
116329dcbaphk	return (0);
117329dcbaphk}
118