]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - usr.sbin/fifolog/fifolog_writer/fifolog_writer.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / usr.sbin / fifolog / fifolog_writer / fifolog_writer.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 <stdlib.h>
31 #include <err.h>
32 #include <unistd.h>
33 #include <ctype.h>
34 #include <assert.h>
35 #include <poll.h>
36 #include <string.h>
37 #include <zlib.h>
38
39 #include "libfifolog.h"
40
41 int
42 main(int argc, char * const *argv)
43 {
44         struct fifolog_writer *f;
45         const char *es;
46         struct pollfd pfd[1];
47         char buf[BUFSIZ], *p;
48         int i, c;
49         unsigned w_opt = 10;
50         unsigned s_opt = 60;
51         unsigned z_opt = Z_BEST_COMPRESSION;
52
53         while ((c = getopt(argc, argv, "w:s:z:")) != -1) {
54                 switch(c) {
55                 case 'w':
56                         w_opt = strtoul(optarg, NULL, 0);
57                         break;
58                 case 's':
59                         s_opt = strtoul(optarg, NULL, 0);
60                         break;
61                 case 'z':
62                         z_opt = strtoul(optarg, NULL, 0);
63                         break;
64                 default:
65                         errx(1, "Usage");
66                 }
67         }
68         argc -= optind;
69         argv += optind;
70         if (argc != 1) 
71                 errx(1, "Usage");
72
73         if (z_opt > 9)
74                 errx(1, "Usage");
75
76         if (w_opt > s_opt)
77                 errx(1, "Usage");
78
79         f = fifolog_write_new();
80         assert(f != NULL);
81
82         es = fifolog_write_open(f, argv[0], w_opt, s_opt, z_opt);
83         if (es)
84                 err(1, "Error: %s", es);
85
86         while (1) {
87                 pfd[0].fd = 0;
88                 pfd[0].events = POLLIN;
89                 i = poll(pfd, 1, 1000);
90                 if (i == 1) {
91                         if (fgets(buf, sizeof buf, stdin) == NULL)
92                                 break;
93                         p = strchr(buf, '\0');
94                         assert(p != NULL);
95                         while (p > buf && isspace(p[-1]))
96                                 p--;
97                         *p = '\0';
98                         if (*buf != '\0')
99                                 fifolog_write_bytes_poll(f, 0, 0, buf, 0);
100                 } else if (i == 0)
101                         (void)fifolog_write_poll(f, 0);
102         }
103         (void)fifolog_write_flush(f);
104         return (0);
105 }