xref: /netbsd-src/usr.bin/logger/logger.c (revision aaf4ece63a859a04e37cf3a7229b5fab0157cc06)
1 /*	$NetBSD: logger.c,v 1.9 2003/08/07 11:14:23 agc Exp $	*/
2 
3 /*
4  * Copyright (c) 1983, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
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.
18  *
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.
30  */
31 
32 #include <sys/cdefs.h>
33 #ifndef lint
34 __COPYRIGHT("@(#) Copyright (c) 1983, 1993\n\
35 	The Regents of the University of California.  All rights reserved.\n");
36 #endif /* not lint */
37 
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.9 2003/08/07 11:14:23 agc Exp $");
43 #endif /* not lint */
44 
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>
52 
53 #define	SYSLOG_NAMES
54 #include <syslog.h>
55 
56 int	decode(const char *, const CODE *);
57 int	pencode(char *);
58 int	main(int, char **);
59 void	usage(void);
60 
61 /*
62  * logger -- read and log utility
63  *
64  *	Reads from an input and arranges to write the result on the system
65  *	log.
66  */
67 int
68 main(int argc, char *argv[])
69 {
70 	int ch, logflags, pri;
71 	const char *tag;
72 	char buf[1024];
73 
74 	tag = NULL;
75 	pri = LOG_NOTICE;
76 	logflags = 0;
77 	while ((ch = getopt(argc, argv, "f:ip:st:")) != -1)
78 		switch((char)ch) {
79 		case 'f':		/* file to log */
80 			if (freopen(optarg, "r", stdin) == NULL)
81 				err(EXIT_FAILURE, "%s", optarg);
82 			break;
83 		case 'i':		/* log process id also */
84 			logflags |= LOG_PID;
85 			break;
86 		case 'p':		/* priority */
87 			pri = pencode(optarg);
88 			break;
89 		case 's':		/* log to standard error */
90 			logflags |= LOG_PERROR;
91 			break;
92 		case 't':		/* tag */
93 			tag = optarg;
94 			break;
95 		case '?':
96 		default:
97 			usage();
98 		}
99 	argc -= optind;
100 	argv += optind;
101 
102 	/* setup for logging */
103 	openlog(tag != NULL ? tag : getlogin(), logflags, 0);
104 	(void)fclose(stdout);
105 
106 	/* log input line if appropriate */
107 	if (argc > 0) {
108 		char *p, *endp;
109 		int len;
110 
111 		for (p = buf, endp = buf + sizeof(buf) - 2; *argv != NULL;) {
112 			len = strlen(*argv);
113 			if (p + len > endp && p > buf) {
114 				syslog(pri, "%s", buf);
115 				p = buf;
116 			}
117 			if (len > sizeof(buf) - 1)
118 				syslog(pri, "%s", *argv++);
119 			else {
120 				if (p != buf)
121 					*p++ = ' ';
122 				memmove(p, *argv++, len);
123 				*(p += len) = '\0';
124 			}
125 		}
126 		if (p != buf)
127 			syslog(pri, "%s", buf);
128 	} else
129 		while (fgets(buf, sizeof(buf), stdin) != NULL)
130 			syslog(pri, "%s", buf);
131 
132 	exit(EXIT_SUCCESS);
133 	/* NOTREACHED */
134 }
135 
136 /*
137  *  Decode a symbolic name to a numeric value
138  */
139 int
140 pencode(char *s)
141 {
142 	char *save;
143 	int fac, lev;
144 
145 	for (save = s; *s != '\0' && *s != '.'; ++s)
146 		;
147 	if (*s != '\0') {
148 		*s = '\0';
149 		fac = decode(save, facilitynames);
150 		if (fac < 0)
151 			errx(EXIT_FAILURE, "unknown facility name: %s", save);
152 		*s++ = '.';
153 	} else {
154 		fac = 0;
155 		s = save;
156 	}
157 	lev = decode(s, prioritynames);
158 	if (lev < 0)
159 		errx(EXIT_FAILURE, "unknown priority name: %s", s);
160 	return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK));
161 }
162 
163 int
164 decode(const char *name, const CODE *codetab)
165 {
166 	const CODE *c;
167 
168 	if (isdigit((unsigned char)*name))
169 		return (atoi(name));
170 
171 	for (c = codetab; c->c_name != NULL; c++)
172 		if (strcasecmp(name, c->c_name) == 0)
173 			return (c->c_val);
174 
175 	return (-1);
176 }
177 
178 void
179 usage(void)
180 {
181 
182 	(void)fprintf(stderr,
183 	    "%s: [-is] [-f file] [-p pri] [-t tag] [ message ... ]\n",
184 	    getprogname());
185 	exit(EXIT_FAILURE);
186 }
187