xref: /netbsd-src/usr.bin/logger/logger.c (revision d9158b13b5dfe46201430699a3f7a235ecf28df3)
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[] = "from: @(#)logger.c	6.15 (Berkeley) 3/1/91";*/
42 static char rcsid[] = "$Id: logger.c,v 1.3 1993/08/01 18:13:37 mycroft Exp $";
43 #endif /* not lint */
44 
45 #include <stdio.h>
46 #define	SYSLOG_NAMES
47 #include <syslog.h>
48 #include <ctype.h>
49 
50 /*
51 **  LOGGER -- read and log utility
52 **
53 **	This routine reads from an input and arranges to write the
54 **	result on the system log, along with a useful tag.
55 */
56 
57 main(argc, argv)
58 	int argc;
59 	char **argv;
60 {
61 	extern char *optarg;
62 	extern int errno, optind;
63 	int pri = LOG_NOTICE;
64 	int ch, logflags = 0;
65 	char *tag, buf[1024], *getlogin(), *strerror();
66 
67 	tag = NULL;
68 	while ((ch = getopt(argc, argv, "f:ip:st:")) != EOF)
69 		switch((char)ch) {
70 		case 'f':		/* file to log */
71 			if (freopen(optarg, "r", stdin) == NULL) {
72 				(void)fprintf(stderr, "logger: %s: %s.\n",
73 				    optarg, strerror(errno));
74 				exit(1);
75 			}
76 			break;
77 		case 'i':		/* log process id also */
78 			logflags |= LOG_PID;
79 			break;
80 		case 'p':		/* priority */
81 			pri = pencode(optarg);
82 			break;
83 		case 's':		/* log to standard error */
84 			logflags |= LOG_PERROR;
85 			break;
86 		case 't':		/* tag */
87 			tag = optarg;
88 			break;
89 		case '?':
90 		default:
91 			usage();
92 		}
93 	argc -= optind;
94 	argv += optind;
95 
96 	/* setup for logging */
97 	openlog(tag ? tag : getlogin(), logflags, 0);
98 	(void) fclose(stdout);
99 
100 	/* log input line if appropriate */
101 	if (argc > 0) {
102 		register char *p, *endp;
103 		int len;
104 
105 		for (p = buf, endp = buf + sizeof(buf) - 2; *argv;) {
106 			len = strlen(*argv);
107 			if (p + len > endp && p > buf) {
108 				syslog(pri, "%s", buf);
109 				p = buf;
110 			}
111 			if (len > sizeof(buf) - 1)
112 				syslog(pri, "%s", *argv++);
113 			else {
114 				if (p != buf)
115 					*p++ = ' ';
116 				bcopy(*argv++, p, len);
117 				*(p += len) = '\0';
118 			}
119 		}
120 		if (p != buf)
121 			syslog(pri, "%s", buf);
122 		exit(0);
123 	}
124 
125 	/* main loop */
126 	while (fgets(buf, sizeof(buf), stdin) != NULL)
127 		syslog(pri, "%s", buf);
128 
129 	exit(0);
130 }
131 
132 /*
133  *  Decode a symbolic name to a numeric value
134  */
135 pencode(s)
136 	register char *s;
137 {
138 	char *save;
139 	int fac, lev;
140 
141 	for (save = s; *s && *s != '.'; ++s);
142 	if (*s) {
143 		*s = '\0';
144 		fac = decode(save, facilitynames);
145 		if (fac < 0) {
146 			(void)fprintf(stderr,
147 			    "logger: unknown facility name: %s.\n", save);
148 			exit(1);
149 		}
150 		*s++ = '.';
151 	}
152 	else {
153 		fac = 0;
154 		s = save;
155 	}
156 	lev = decode(s, prioritynames);
157 	if (lev < 0) {
158 		(void)fprintf(stderr,
159 		    "logger: unknown priority name: %s.\n", save);
160 		exit(1);
161 	}
162 	return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK));
163 }
164 
165 decode(name, codetab)
166 	char *name;
167 	CODE *codetab;
168 {
169 	register CODE *c;
170 
171 	if (isdigit(*name))
172 		return (atoi(name));
173 
174 	for (c = codetab; c->c_name; c++)
175 		if (!strcasecmp(name, c->c_name))
176 			return (c->c_val);
177 
178 	return (-1);
179 }
180 
181 usage()
182 {
183 	(void)fprintf(stderr,
184 	    "logger: [-i] [-f file] [-p pri] [-t tag] [ message ... ]\n");
185 	exit(1);
186 }
187