xref: /csrg-svn/usr.bin/logger/logger.c (revision 24870)
1*24870Seric /*
2*24870Seric  * Copyright (c) 1983 Regents of the University of California.
3*24870Seric  * All rights reserved.  The Berkeley software License Agreement
4*24870Seric  * specifies the terms and conditions for redistribution.
5*24870Seric  */
6*24870Seric 
7*24870Seric #ifndef lint
8*24870Seric char copyright[] =
9*24870Seric "@(#) Copyright (c) 1983 Regents of the University of California.\n\
10*24870Seric  All rights reserved.\n";
11*24870Seric #endif not lint
12*24870Seric 
13*24870Seric #ifndef lint
14*24870Seric static char sccsid[] = "@(#)logger.c	6.1 (Berkeley) 09/17/85";
15*24870Seric #endif not lint
16*24870Seric 
17*24870Seric #include <stdio.h>
18*24870Seric #include <syslog.h>
19*24870Seric #include <ctype.h>
20*24870Seric 
21*24870Seric /*
22*24870Seric **  LOGGER -- read and log utility
23*24870Seric **
24*24870Seric **	This routine reads from an input and arranges to write the
25*24870Seric **	result on the system log, along with a useful tag.
26*24870Seric */
27*24870Seric 
28*24870Seric main(argc, argv)
29*24870Seric 	int argc;
30*24870Seric 	char **argv;
31*24870Seric {
32*24870Seric 	char buf[200];
33*24870Seric 	char *tag;
34*24870Seric 	register char *p;
35*24870Seric 	int pri = LOG_NOTICE;
36*24870Seric 	int logflags = 0;
37*24870Seric 	extern char *getlogin();
38*24870Seric 
39*24870Seric 	/* initialize */
40*24870Seric 	tag = getlogin();
41*24870Seric 
42*24870Seric 	/* crack arguments */
43*24870Seric 	while (--argc > 0)
44*24870Seric 	{
45*24870Seric 		p = *++argv;
46*24870Seric 		if (*p != '-')
47*24870Seric 			break;
48*24870Seric 
49*24870Seric 		switch (*++p)
50*24870Seric 		{
51*24870Seric 		  case '\0':		/* dummy */
52*24870Seric 			/* this can be used to give null parameters */
53*24870Seric 			break;
54*24870Seric 
55*24870Seric 		  case 't':		/* tag */
56*24870Seric 			if (argc > 1 && argv[1][0] != '-')
57*24870Seric 			{
58*24870Seric 				argc--;
59*24870Seric 				tag = *++argv;
60*24870Seric 			}
61*24870Seric 			else
62*24870Seric 				tag = NULL;
63*24870Seric 			break;
64*24870Seric 
65*24870Seric 		  case 'p':		/* priority */
66*24870Seric 			if (argc > 1 && argv[1][0] != '-')
67*24870Seric 			{
68*24870Seric 				argc--;
69*24870Seric 				pri = pencode(*++argv);
70*24870Seric 			}
71*24870Seric 			break;
72*24870Seric 
73*24870Seric 		  case 'i':		/* log process id also */
74*24870Seric 			logflags |= LOG_PID;
75*24870Seric 			break;
76*24870Seric 
77*24870Seric 		  case 'f':		/* file to log */
78*24870Seric 			if (argc > 1 && argv[1][0] != '-')
79*24870Seric 			{
80*24870Seric 				argc--;
81*24870Seric 				if (freopen(*++argv, "r", stdin) == NULL)
82*24870Seric 				{
83*24870Seric 					fprintf("logger: ");
84*24870Seric 					perror(*argv);
85*24870Seric 					exit(1);
86*24870Seric 				}
87*24870Seric 			}
88*24870Seric 			break;
89*24870Seric 
90*24870Seric 		  default:
91*24870Seric 			fprintf(stderr, "logger: unknown flag -%s\n", p);
92*24870Seric 			break;
93*24870Seric 		}
94*24870Seric 	}
95*24870Seric 
96*24870Seric 	/* setup for logging */
97*24870Seric 	openlog(tag, logflags, 0);
98*24870Seric 	(void) fclose(stdout);
99*24870Seric 
100*24870Seric 	/* log input line if appropriate */
101*24870Seric 	if (argc > 0)
102*24870Seric 	{
103*24870Seric 		char buf[120];
104*24870Seric 
105*24870Seric 		buf[0] = '\0';
106*24870Seric 		while (argc-- > 0)
107*24870Seric 		{
108*24870Seric 			strcat(buf, " ");
109*24870Seric 			strcat(buf, *argv++);
110*24870Seric 		}
111*24870Seric 		syslog(pri, buf + 1);
112*24870Seric 		exit(0);
113*24870Seric 	}
114*24870Seric 
115*24870Seric 	/* main loop */
116*24870Seric 	while (fgets(buf, sizeof buf, stdin) != NULL)
117*24870Seric 		syslog(pri, buf);
118*24870Seric 
119*24870Seric 	exit(0);
120*24870Seric }
121*24870Seric 
122*24870Seric 
123*24870Seric struct code {
124*24870Seric 	char	*c_name;
125*24870Seric 	int	c_val;
126*24870Seric };
127*24870Seric 
128*24870Seric struct code	PriNames[] = {
129*24870Seric 	"panic",	LOG_EMERG,
130*24870Seric 	"emerg",	LOG_EMERG,
131*24870Seric 	"alert",	LOG_ALERT,
132*24870Seric 	"crit",		LOG_CRIT,
133*24870Seric 	"err",		LOG_ERR,
134*24870Seric 	"error",	LOG_ERR,
135*24870Seric 	"warn",		LOG_WARNING,
136*24870Seric 	"warning",	LOG_WARNING,
137*24870Seric 	"notice",	LOG_NOTICE,
138*24870Seric 	"info",		LOG_INFO,
139*24870Seric 	"debug",	LOG_DEBUG,
140*24870Seric 	NULL,		-1
141*24870Seric };
142*24870Seric 
143*24870Seric struct code	FacNames[] = {
144*24870Seric 	"kern",		LOG_KERN,
145*24870Seric 	"user",		LOG_USER,
146*24870Seric 	"mail",		LOG_MAIL,
147*24870Seric 	"auth",		LOG_AUTH,
148*24870Seric 	"security",	LOG_AUTH,
149*24870Seric 	"local0",	LOG_LOCAL0,
150*24870Seric 	"local1",	LOG_LOCAL1,
151*24870Seric 	"local2",	LOG_LOCAL2,
152*24870Seric 	"local3",	LOG_LOCAL3,
153*24870Seric 	"local4",	LOG_LOCAL4,
154*24870Seric 	"local5",	LOG_LOCAL5,
155*24870Seric 	"local6",	LOG_LOCAL6,
156*24870Seric 	"local7",	LOG_LOCAL7,
157*24870Seric 	NULL,		-1
158*24870Seric };
159*24870Seric 
160*24870Seric 
161*24870Seric /*
162*24870Seric  *  Decode a symbolic name to a numeric value
163*24870Seric  */
164*24870Seric 
165*24870Seric pencode(s)
166*24870Seric 	register char *s;
167*24870Seric {
168*24870Seric 	register char *p;
169*24870Seric 	int lev;
170*24870Seric 	int fac;
171*24870Seric 	char buf[100];
172*24870Seric 
173*24870Seric 	for (p = buf; *s && *s != '.'; )
174*24870Seric 		*p++ = *s++;
175*24870Seric 	*p = '\0';
176*24870Seric 	if (*s++) {
177*24870Seric 		fac = decode(buf, FacNames);
178*24870Seric 		if (fac < 0)
179*24870Seric 			bailout("unknown facility name: ", buf);
180*24870Seric 		for (p = buf; *p++ = *s++; )
181*24870Seric 			continue;
182*24870Seric 	} else
183*24870Seric 		fac = 0;
184*24870Seric 	lev = decode(buf, PriNames);
185*24870Seric 	if (lev < 0)
186*24870Seric 		bailout("unknown priority name: ", buf);
187*24870Seric 
188*24870Seric 	return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK));
189*24870Seric }
190*24870Seric 
191*24870Seric 
192*24870Seric decode(name, codetab)
193*24870Seric 	char *name;
194*24870Seric 	struct code *codetab;
195*24870Seric {
196*24870Seric 	register struct code *c;
197*24870Seric 	register char *p;
198*24870Seric 	char buf[40];
199*24870Seric 
200*24870Seric 	if (isdigit(*name))
201*24870Seric 		return (atoi(name));
202*24870Seric 
203*24870Seric 	(void) strcpy(buf, name);
204*24870Seric 	for (p = buf; *p; p++)
205*24870Seric 		if (isupper(*p))
206*24870Seric 			*p = tolower(*p);
207*24870Seric 	for (c = codetab; c->c_name; c++)
208*24870Seric 		if (!strcmp(buf, c->c_name))
209*24870Seric 			return (c->c_val);
210*24870Seric 
211*24870Seric 	return (-1);
212*24870Seric }
213*24870Seric 
214*24870Seric bailout(a, b)
215*24870Seric 	char *a, *b;
216*24870Seric {
217*24870Seric 	fprintf(stderr, "logger: %s%s\n", a, b);
218*24870Seric 	exit(1);
219*24870Seric }
220