xref: /freebsd/usr.sbin/fifolog/fifolog_writer/fifolog_writer.c (revision 830940567b49bb0c08dfaed40418999e76616909)
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 <stdlib.h>
31 #include <sysexits.h>
32 #include <err.h>
33 #include <unistd.h>
34 #include <ctype.h>
35 #include <assert.h>
36 #include <poll.h>
37 #include <string.h>
38 #include <zlib.h>
39 
40 #include "libfifolog.h"
41 
42 static void
43 usage(void)
44 {
45 	fprintf(stderr, "fifolog_writer [-w write-rate] [-s sync-rate] "
46 	    "[-z compression] file\n");
47 	exit(EX_USAGE);
48 }
49 
50 int
51 main(int argc, char * const *argv)
52 {
53 	struct fifolog_writer *f;
54 	const char *es;
55 	struct pollfd pfd[1];
56 	char buf[BUFSIZ], *p;
57 	int i, c;
58 	unsigned w_opt = 10;
59 	unsigned s_opt = 60;
60 	unsigned z_opt = Z_BEST_COMPRESSION;
61 
62 	while ((c = getopt(argc, argv, "w:s:z:")) != -1) {
63 		switch(c) {
64 		case 'w':
65 			w_opt = strtoul(optarg, NULL, 0);
66 			break;
67 		case 's':
68 			s_opt = strtoul(optarg, NULL, 0);
69 			break;
70 		case 'z':
71 			z_opt = strtoul(optarg, NULL, 0);
72 			break;
73 		default:
74 			usage();
75 		}
76 	}
77 	argc -= optind;
78 	argv += optind;
79 	if (argc != 1)
80 		usage();
81 
82 	if (z_opt > 9)
83 		usage();
84 
85 	if (w_opt > s_opt)
86 		usage();
87 
88 	f = fifolog_write_new();
89 	assert(f != NULL);
90 
91 	es = fifolog_write_open(f, argv[0], w_opt, s_opt, z_opt);
92 	if (es)
93 		err(1, "Error: %s", es);
94 
95 	while (1) {
96 		pfd[0].fd = 0;
97 		pfd[0].events = POLLIN;
98 		i = poll(pfd, 1, 1000);
99 		if (i == 1) {
100 			if (fgets(buf, sizeof buf, stdin) == NULL)
101 				break;
102 			p = strchr(buf, '\0');
103 			assert(p != NULL);
104 			while (p > buf && isspace(p[-1]))
105 				p--;
106 			*p = '\0';
107 			if (*buf != '\0')
108 				fifolog_write_bytes_poll(f, 0, 0, buf, 0);
109 		} else if (i == 0)
110 			(void)fifolog_write_poll(f, 0);
111 	}
112 	(void)fifolog_write_flush(f);
113 	return (0);
114 }
115