xref: /original-bsd/usr.bin/logger/logger.c (revision 5bfa3c83)
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 are permitted
6  * provided that the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * advertising materials, and other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  */
17 
18 #ifndef lint
19 char copyright[] =
20 "@(#) Copyright (c) 1983 Regents of the University of California.\n\
21  All rights reserved.\n";
22 #endif /* not lint */
23 
24 #ifndef lint
25 static char sccsid[] = "@(#)logger.c	6.12 (Berkeley) 11/13/89";
26 #endif /* not lint */
27 
28 #include <stdio.h>
29 #include <syslog.h>
30 #include <ctype.h>
31 
32 /*
33 **  LOGGER -- read and log utility
34 **
35 **	This routine reads from an input and arranges to write the
36 **	result on the system log, along with a useful tag.
37 */
38 
39 main(argc, argv)
40 	int argc;
41 	char **argv;
42 {
43 	extern char *optarg;
44 	extern int optind;
45 	int pri = LOG_NOTICE;
46 	int ch, logflags = 0;
47 	char *tag, buf[1024], *getlogin();
48 
49 	tag = NULL;
50 	while ((ch = getopt(argc, argv, "f:ip:st:")) != EOF)
51 		switch((char)ch) {
52 		case 'f':		/* file to log */
53 			if (freopen(optarg, "r", stdin) == NULL) {
54 				fprintf("logger: ");
55 				perror(optarg);
56 				exit(1);
57 			}
58 			break;
59 		case 'i':		/* log process id also */
60 			logflags |= LOG_PID;
61 			break;
62 		case 'p':		/* priority */
63 			pri = pencode(optarg);
64 			break;
65 		case 's':		/* log to standard error */
66 			logflags |= LOG_PERROR;
67 			break;
68 		case 't':		/* tag */
69 			tag = optarg;
70 			break;
71 		case '?':
72 		default:
73 			usage();
74 		}
75 	argc -= optind;
76 	argv += optind;
77 
78 	/* setup for logging */
79 	openlog(tag ? tag : getlogin(), logflags, 0);
80 	(void) fclose(stdout);
81 
82 	/* log input line if appropriate */
83 	if (argc > 0) {
84 		register char *p, *endp;
85 		int len;
86 
87 		for (p = buf, endp = buf + sizeof(buf) - 2; *argv;) {
88 			len = strlen(*argv);
89 			if (p + len > endp && p > buf) {
90 				syslog(pri, "%s", buf);
91 				p = buf;
92 			}
93 			if (len > sizeof(buf) - 1)
94 				syslog(pri, "%s", *argv++);
95 			else {
96 				if (p != buf)
97 					*p++ = ' ';
98 				bcopy(*argv++, p, len);
99 				*(p += len) = '\0';
100 			}
101 		}
102 		if (p != buf)
103 			syslog(pri, "%s", buf);
104 		exit(0);
105 	}
106 
107 	/* main loop */
108 	while (fgets(buf, sizeof(buf), stdin) != NULL)
109 		syslog(pri, "%s", buf);
110 
111 	exit(0);
112 }
113 
114 #define	SYSLOG_NAMES
115 #include <syslog.h>
116 
117 /*
118  *  Decode a symbolic name to a numeric value
119  */
120 pencode(s)
121 	register char *s;
122 {
123 	char *save;
124 	int fac, lev;
125 
126 	for (save = s; *s && *s != '.'; ++s);
127 	if (*s) {
128 		*s = '\0';
129 		fac = decode(save, facilitynames);
130 		if (fac < 0)
131 			bailout("unknown facility name: ", save);
132 		*s++ = '.';
133 	}
134 	else {
135 		fac = 0;
136 		s = save;
137 	}
138 	lev = decode(s, prioritynames);
139 	if (lev < 0)
140 		bailout("unknown priority name: ", save);
141 	return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK));
142 }
143 
144 
145 decode(name, codetab)
146 	char *name;
147 	CODE *codetab;
148 {
149 	register CODE *c;
150 
151 	if (isdigit(*name))
152 		return (atoi(name));
153 
154 	for (c = codetab; c->c_name; c++)
155 		if (!strcasecmp(name, c->c_name))
156 			return (c->c_val);
157 
158 	return (-1);
159 }
160 
161 bailout(msg, arg)
162 	char *msg, *arg;
163 {
164 	fprintf(stderr, "logger: %s%s\n", msg, arg);
165 	exit(1);
166 }
167 
168 usage()
169 {
170 	fputs("logger: [-i] [-f file] [-p pri] [-t tag] [ message ... ]\n",
171 	    stderr);
172 	exit(1);
173 }
174