xref: /netbsd-src/usr.bin/logger/logger.c (revision 811e6386f8c5e4a3521c7003da29ec8673e344fa)
1 /*
2  * Copyright (c) 1983 Regents of the University of California.
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  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #ifndef lint
35 char copyright[] =
36 "@(#) Copyright (c) 1983 Regents of the University of California.\n\
37  All rights reserved.\n";
38 #endif /* not lint */
39 
40 #ifndef lint
41 static char sccsid[] = "@(#)logger.c	6.15 (Berkeley) 3/1/91";
42 #endif /* not lint */
43 
44 #include <stdio.h>
45 #define	SYSLOG_NAMES
46 #include <syslog.h>
47 #include <ctype.h>
48 
49 /*
50 **  LOGGER -- read and log utility
51 **
52 **	This routine reads from an input and arranges to write the
53 **	result on the system log, along with a useful tag.
54 */
55 
56 main(argc, argv)
57 	int argc;
58 	char **argv;
59 {
60 	extern char *optarg;
61 	extern int errno, optind;
62 	int pri = LOG_NOTICE;
63 	int ch, logflags = 0;
64 	char *tag, buf[1024], *getlogin(), *strerror();
65 
66 	tag = NULL;
67 	while ((ch = getopt(argc, argv, "f:ip:st:")) != EOF)
68 		switch((char)ch) {
69 		case 'f':		/* file to log */
70 			if (freopen(optarg, "r", stdin) == NULL) {
71 				(void)fprintf(stderr, "logger: %s: %s.\n",
72 				    optarg, strerror(errno));
73 				exit(1);
74 			}
75 			break;
76 		case 'i':		/* log process id also */
77 			logflags |= LOG_PID;
78 			break;
79 		case 'p':		/* priority */
80 			pri = pencode(optarg);
81 			break;
82 		case 's':		/* log to standard error */
83 			logflags |= LOG_PERROR;
84 			break;
85 		case 't':		/* tag */
86 			tag = optarg;
87 			break;
88 		case '?':
89 		default:
90 			usage();
91 		}
92 	argc -= optind;
93 	argv += optind;
94 
95 	/* setup for logging */
96 	openlog(tag ? tag : getlogin(), logflags, 0);
97 	(void) fclose(stdout);
98 
99 	/* log input line if appropriate */
100 	if (argc > 0) {
101 		register char *p, *endp;
102 		int len;
103 
104 		for (p = buf, endp = buf + sizeof(buf) - 2; *argv;) {
105 			len = strlen(*argv);
106 			if (p + len > endp && p > buf) {
107 				syslog(pri, "%s", buf);
108 				p = buf;
109 			}
110 			if (len > sizeof(buf) - 1)
111 				syslog(pri, "%s", *argv++);
112 			else {
113 				if (p != buf)
114 					*p++ = ' ';
115 				bcopy(*argv++, p, len);
116 				*(p += len) = '\0';
117 			}
118 		}
119 		if (p != buf)
120 			syslog(pri, "%s", buf);
121 		exit(0);
122 	}
123 
124 	/* main loop */
125 	while (fgets(buf, sizeof(buf), stdin) != NULL)
126 		syslog(pri, "%s", buf);
127 
128 	exit(0);
129 }
130 
131 /*
132  *  Decode a symbolic name to a numeric value
133  */
134 pencode(s)
135 	register char *s;
136 {
137 	char *save;
138 	int fac, lev;
139 
140 	for (save = s; *s && *s != '.'; ++s);
141 	if (*s) {
142 		*s = '\0';
143 		fac = decode(save, facilitynames);
144 		if (fac < 0) {
145 			(void)fprintf(stderr,
146 			    "logger: unknown facility name: %s.\n", save);
147 			exit(1);
148 		}
149 		*s++ = '.';
150 	}
151 	else {
152 		fac = 0;
153 		s = save;
154 	}
155 	lev = decode(s, prioritynames);
156 	if (lev < 0) {
157 		(void)fprintf(stderr,
158 		    "logger: unknown priority name: %s.\n", save);
159 		exit(1);
160 	}
161 	return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK));
162 }
163 
164 decode(name, codetab)
165 	char *name;
166 	CODE *codetab;
167 {
168 	register CODE *c;
169 
170 	if (isdigit(*name))
171 		return (atoi(name));
172 
173 	for (c = codetab; c->c_name; c++)
174 		if (!strcasecmp(name, c->c_name))
175 			return (c->c_val);
176 
177 	return (-1);
178 }
179 
180 usage()
181 {
182 	(void)fprintf(stderr,
183 	    "logger: [-i] [-f file] [-p pri] [-t tag] [ message ... ]\n");
184 	exit(1);
185 }
186