1 /* $NetBSD: logger.c,v 1.13 2008/11/13 15:51:07 christos Exp $ */
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
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
32 #include <sys/cdefs.h>
34 __COPYRIGHT("@(#) Copyright (c) 1983, 1993\
35 The Regents of the University of California. All rights reserved.");
40 static char sccsid
[] = "@(#)logger.c 8.1 (Berkeley) 6/6/93";
42 __RCSID("$NetBSD: logger.c,v 1.13 2008/11/13 15:51:07 christos Exp $");
56 int decode(const char *, const CODE
*);
58 int main(int, char **);
62 * logger -- read and log utility
64 * Reads from an input and arranges to write the result on the system
68 main(int argc
, char *argv
[])
70 int ch
, logflags
, pri
;
73 const char *msgid
= "-";
79 while ((ch
= getopt(argc
, argv
, "d:f:im:p:st:")) != -1)
81 case 'd': /* structured data field */
84 case 'f': /* file to log */
85 if (freopen(optarg
, "r", stdin
) == NULL
)
86 err(EXIT_FAILURE
, "%s", optarg
);
88 case 'i': /* log process id also */
91 case 'm': /* msgid field */
94 case 'p': /* priority */
95 pri
= pencode(optarg
);
97 case 's': /* log to standard error */
98 logflags
|= LOG_PERROR
;
110 /* setup for logging */
111 openlog(tag
!= NULL
? tag
: getlogin(), logflags
, 0);
112 (void)fclose(stdout
);
114 /* log input line if appropriate */
119 for (p
= buf
, endp
= buf
+ sizeof(buf
) - 2; *argv
!= NULL
;) {
121 if (p
+ len
> endp
&& p
> buf
) {
122 syslogp(pri
, msgid
, sd
, "%s", buf
);
125 if (len
> sizeof(buf
) - 1)
126 syslogp(pri
, msgid
, sd
, "%s", *argv
++);
130 memmove(p
, *argv
++, len
);
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
);
148 * Decode a symbolic name to a numeric value
156 for (save
= s
; *s
!= '\0' && *s
!= '.'; ++s
)
160 fac
= decode(save
, facilitynames
);
162 errx(EXIT_FAILURE
, "unknown facility name: %s", save
);
168 lev
= decode(s
, prioritynames
);
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
)
179 if (isdigit((unsigned char)*name
))
182 for (c
= codetab
; c
->c_name
!= NULL
; c
++)
183 if (strcasecmp(name
, c
->c_name
) == 0)
193 (void)fprintf(stderr
,
194 "%s: [-is] [-f file] [-p pri] [-t tag] "
195 "[-m msgid] [-d SD] [ message ... ]\n",