Fix markup. Fix backslashes to surive roff.
[netbsd-mini2440.git] / usr.bin / logger / logger.c
blob635214bcdc810ac282142b01ab0842f563cdc581
1 /* $NetBSD: logger.c,v 1.13 2008/11/13 15:51:07 christos Exp $ */
3 /*
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 #ifndef lint
34 __COPYRIGHT("@(#) Copyright (c) 1983, 1993\
35 The Regents of the University of California. All rights reserved.");
36 #endif /* not lint */
38 #ifndef lint
39 #if 0
40 static char sccsid[] = "@(#)logger.c 8.1 (Berkeley) 6/6/93";
41 #endif
42 __RCSID("$NetBSD: logger.c,v 1.13 2008/11/13 15:51:07 christos Exp $");
43 #endif /* not lint */
45 #include <errno.h>
46 #include <unistd.h>
47 #include <stdlib.h>
48 #include <stdio.h>
49 #include <ctype.h>
50 #include <string.h>
51 #include <err.h>
53 #define SYSLOG_NAMES
54 #include <syslog.h>
56 int decode(const char *, const CODE *);
57 int pencode(char *);
58 int main(int, char **);
59 void usage(void);
62 * logger -- read and log utility
64 * Reads from an input and arranges to write the result on the system
65 * log.
67 int
68 main(int argc, char *argv[])
70 int ch, logflags, pri;
71 const char *tag;
72 const char *sd = "-";
73 const char *msgid = "-";
74 char buf[1024];
76 tag = NULL;
77 pri = LOG_NOTICE;
78 logflags = 0;
79 while ((ch = getopt(argc, argv, "d:f:im:p:st:")) != -1)
80 switch((char)ch) {
81 case 'd': /* structured data field */
82 sd = optarg;
83 break;
84 case 'f': /* file to log */
85 if (freopen(optarg, "r", stdin) == NULL)
86 err(EXIT_FAILURE, "%s", optarg);
87 break;
88 case 'i': /* log process id also */
89 logflags |= LOG_PID;
90 break;
91 case 'm': /* msgid field */
92 msgid = optarg;
93 break;
94 case 'p': /* priority */
95 pri = pencode(optarg);
96 break;
97 case 's': /* log to standard error */
98 logflags |= LOG_PERROR;
99 break;
100 case 't': /* tag */
101 tag = optarg;
102 break;
103 case '?':
104 default:
105 usage();
107 argc -= optind;
108 argv += optind;
110 /* setup for logging */
111 openlog(tag != NULL ? tag : getlogin(), logflags, 0);
112 (void)fclose(stdout);
114 /* log input line if appropriate */
115 if (argc > 0) {
116 char *p, *endp;
117 size_t len;
119 for (p = buf, endp = buf + sizeof(buf) - 2; *argv != NULL;) {
120 len = strlen(*argv);
121 if (p + len > endp && p > buf) {
122 syslogp(pri, msgid, sd, "%s", buf);
123 p = buf;
125 if (len > sizeof(buf) - 1)
126 syslogp(pri, msgid, sd, "%s", *argv++);
127 else {
128 if (p != buf)
129 *p++ = ' ';
130 memmove(p, *argv++, len);
131 *(p += len) = '\0';
134 if (p != buf)
135 syslogp(pri, msgid, sd, "%s", buf);
136 } else /* TODO: allow syslog-protocol messages from file/stdin
137 * but that will require parsing the line to split
138 * it into three fields.
140 while (fgets(buf, sizeof(buf), stdin) != NULL)
141 syslogp(pri, msgid, sd, "%s", buf);
143 exit(EXIT_SUCCESS);
144 /* NOTREACHED */
148 * Decode a symbolic name to a numeric value
151 pencode(char *s)
153 char *save;
154 int fac, lev;
156 for (save = s; *s != '\0' && *s != '.'; ++s)
158 if (*s != '\0') {
159 *s = '\0';
160 fac = decode(save, facilitynames);
161 if (fac < 0)
162 errx(EXIT_FAILURE, "unknown facility name: %s", save);
163 *s++ = '.';
164 } else {
165 fac = 0;
166 s = save;
168 lev = decode(s, prioritynames);
169 if (lev < 0)
170 errx(EXIT_FAILURE, "unknown priority name: %s", s);
171 return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK));
175 decode(const char *name, const CODE *codetab)
177 const CODE *c;
179 if (isdigit((unsigned char)*name))
180 return (atoi(name));
182 for (c = codetab; c->c_name != NULL; c++)
183 if (strcasecmp(name, c->c_name) == 0)
184 return (c->c_val);
186 return (-1);
189 void
190 usage(void)
193 (void)fprintf(stderr,
194 "%s: [-is] [-f file] [-p pri] [-t tag] "
195 "[-m msgid] [-d SD] [ message ... ]\n",
196 getprogname());
197 exit(EXIT_FAILURE);