xref: /csrg-svn/usr.bin/logger/logger.c (revision 34911)
124870Seric /*
224870Seric  * Copyright (c) 1983 Regents of the University of California.
334446Sbostic  * All rights reserved.
434446Sbostic  *
534446Sbostic  * Redistribution and use in source and binary forms are permitted
6*34911Sbostic  * provided that the above copyright notice and this paragraph are
7*34911Sbostic  * duplicated in all such forms and that any documentation,
8*34911Sbostic  * advertising materials, and other materials related to such
9*34911Sbostic  * distribution and use acknowledge that the software was developed
10*34911Sbostic  * by the University of California, Berkeley.  The name of the
11*34911Sbostic  * University may not be used to endorse or promote products derived
12*34911Sbostic  * from this software without specific prior written permission.
13*34911Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14*34911Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15*34911Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1624870Seric  */
1724870Seric 
1824870Seric #ifndef lint
1924870Seric char copyright[] =
2024870Seric "@(#) Copyright (c) 1983 Regents of the University of California.\n\
2124870Seric  All rights reserved.\n";
2234446Sbostic #endif /* not lint */
2324870Seric 
2424870Seric #ifndef lint
25*34911Sbostic static char sccsid[] = "@(#)logger.c	6.8 (Berkeley) 06/29/88";
2634446Sbostic #endif /* not lint */
2724870Seric 
2824870Seric #include <stdio.h>
2924870Seric #include <syslog.h>
3024870Seric #include <ctype.h>
3124870Seric 
3224870Seric /*
3324870Seric **  LOGGER -- read and log utility
3424870Seric **
3524870Seric **	This routine reads from an input and arranges to write the
3624870Seric **	result on the system log, along with a useful tag.
3724870Seric */
3824870Seric 
3924870Seric main(argc, argv)
4024870Seric 	int argc;
4124870Seric 	char **argv;
4224870Seric {
4334446Sbostic 	extern char *optarg;
4434446Sbostic 	extern int optind;
4524870Seric 	int pri = LOG_NOTICE;
4634446Sbostic 	int ch, logflags = 0;
4734446Sbostic 	char *tag, buf[200], *getlogin();
4824870Seric 
4934446Sbostic 	tag = NULL;
5034446Sbostic 	while ((ch = getopt(argc, argv, "f:ip:t:")) != EOF)
5134446Sbostic 		switch((char)ch) {
5234446Sbostic 		case 'f':		/* file to log */
5334446Sbostic 			if (freopen(optarg, "r", stdin) == NULL) {
5434446Sbostic 				fprintf("logger: ");
5534446Sbostic 				perror(optarg);
5634446Sbostic 				exit(1);
5724870Seric 			}
5824870Seric 			break;
5934446Sbostic 		case 'i':		/* log process id also */
6024870Seric 			logflags |= LOG_PID;
6124870Seric 			break;
6234446Sbostic 		case 'p':		/* priority */
6334446Sbostic 			pri = pencode(optarg);
6424870Seric 			break;
6534446Sbostic 		case 't':		/* tag */
6634446Sbostic 			tag = optarg;
6724870Seric 			break;
6834446Sbostic 		case '?':
6934446Sbostic 		default:
7034446Sbostic 			usage();
7124870Seric 		}
7234446Sbostic 	argc -= optind;
7334446Sbostic 	argv += optind;
7424870Seric 
7524870Seric 	/* setup for logging */
7634446Sbostic 	openlog(tag ? tag : getlogin(), logflags, 0);
7724870Seric 	(void) fclose(stdout);
7824870Seric 
7924870Seric 	/* log input line if appropriate */
8034446Sbostic 	if (argc > 0) {
8134446Sbostic 		register char *p, *endp;
8234446Sbostic 		int len;
8324870Seric 
8434446Sbostic 		for (p = buf, endp = buf + sizeof(buf) - 1;;) {
8534446Sbostic 			len = strlen(*argv);
8634512Skarels 			if (p + len < endp && p > buf) {
8734512Skarels 				*--p = '\0';
8834512Skarels 				syslog(pri, buf);
8934512Skarels 				p = buf;
9034512Skarels 			}
9134512Skarels 			if (len > sizeof(buf) - 1) {
9234512Skarels 				syslog(pri, *argv++);
9334512Skarels 				if (!--argc)
9434512Skarels 					break;
9534512Skarels 			} else {
9634446Sbostic 				bcopy(*argv++, p, len);
9734446Sbostic 				p += len;
9834446Sbostic 				if (!--argc)
9934446Sbostic 					break;
10034446Sbostic 				*p++ = ' ';
10134446Sbostic 				*--p = '\0';
10234446Sbostic 			}
10324870Seric 		}
10434446Sbostic 		if (p != buf) {
10534446Sbostic 			*p = '\0';
10634446Sbostic 			syslog(pri, buf);
10734446Sbostic 		}
10824870Seric 		exit(0);
10924870Seric 	}
11024870Seric 
11124870Seric 	/* main loop */
11234446Sbostic 	while (fgets(buf, sizeof(buf), stdin) != NULL)
11324870Seric 		syslog(pri, buf);
11424870Seric 
11524870Seric 	exit(0);
11624870Seric }
11724870Seric 
11824870Seric 
11924870Seric struct code {
12024870Seric 	char	*c_name;
12124870Seric 	int	c_val;
12224870Seric };
12324870Seric 
12424870Seric struct code	PriNames[] = {
12524870Seric 	"panic",	LOG_EMERG,
12624870Seric 	"emerg",	LOG_EMERG,
12724870Seric 	"alert",	LOG_ALERT,
12824870Seric 	"crit",		LOG_CRIT,
12924870Seric 	"err",		LOG_ERR,
13024870Seric 	"error",	LOG_ERR,
13124870Seric 	"warn",		LOG_WARNING,
13224870Seric 	"warning",	LOG_WARNING,
13324870Seric 	"notice",	LOG_NOTICE,
13424870Seric 	"info",		LOG_INFO,
13524870Seric 	"debug",	LOG_DEBUG,
13624870Seric 	NULL,		-1
13724870Seric };
13824870Seric 
13924870Seric struct code	FacNames[] = {
14024870Seric 	"kern",		LOG_KERN,
14124870Seric 	"user",		LOG_USER,
14224870Seric 	"mail",		LOG_MAIL,
14324947Seric 	"daemon",	LOG_DAEMON,
14424870Seric 	"auth",		LOG_AUTH,
14524870Seric 	"security",	LOG_AUTH,
14629918Seric 	"syslog",	LOG_SYSLOG,
14729918Seric 	"lpr",		LOG_LPR,
14829918Seric 	"news",		LOG_NEWS,
14934647Sbostic 	"uucp",		LOG_UUCP,
15024870Seric 	"local0",	LOG_LOCAL0,
15124870Seric 	"local1",	LOG_LOCAL1,
15224870Seric 	"local2",	LOG_LOCAL2,
15324870Seric 	"local3",	LOG_LOCAL3,
15424870Seric 	"local4",	LOG_LOCAL4,
15524870Seric 	"local5",	LOG_LOCAL5,
15624870Seric 	"local6",	LOG_LOCAL6,
15724870Seric 	"local7",	LOG_LOCAL7,
15824870Seric 	NULL,		-1
15924870Seric };
16024870Seric 
16124870Seric 
16224870Seric /*
16324870Seric  *  Decode a symbolic name to a numeric value
16424870Seric  */
16524870Seric 
16624870Seric pencode(s)
16724870Seric 	register char *s;
16824870Seric {
16934446Sbostic 	char *save;
17034446Sbostic 	int fac, lev;
17124870Seric 
17234446Sbostic 	for (save = s; *s && *s != '.'; ++s);
17334446Sbostic 	if (*s) {
17434446Sbostic 		*s = '\0';
17534446Sbostic 		fac = decode(save, FacNames);
17624870Seric 		if (fac < 0)
17734446Sbostic 			bailout("unknown facility name: ", save);
17834446Sbostic 		*s++ = '.';
17934446Sbostic 	}
18034446Sbostic 	else {
18124870Seric 		fac = 0;
18234446Sbostic 		s = save;
18334446Sbostic 	}
18434446Sbostic 	lev = decode(s, PriNames);
18524870Seric 	if (lev < 0)
18634446Sbostic 		bailout("unknown priority name: ", save);
18724870Seric 	return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK));
18824870Seric }
18924870Seric 
19024870Seric 
19124870Seric decode(name, codetab)
19224870Seric 	char *name;
19324870Seric 	struct code *codetab;
19424870Seric {
19524870Seric 	register struct code *c;
19624870Seric 
19724870Seric 	if (isdigit(*name))
19824870Seric 		return (atoi(name));
19924870Seric 
20024870Seric 	for (c = codetab; c->c_name; c++)
20134446Sbostic 		if (!strcasecmp(name, c->c_name))
20224870Seric 			return (c->c_val);
20324870Seric 
20424870Seric 	return (-1);
20524870Seric }
20624870Seric 
20734446Sbostic bailout(msg, arg)
20834446Sbostic 	char *msg, *arg;
20924870Seric {
21034446Sbostic 	fprintf(stderr, "logger: %s%s\n", msg, arg);
21124870Seric 	exit(1);
21224870Seric }
21334446Sbostic 
21434446Sbostic usage()
21534446Sbostic {
21634446Sbostic 	fputs("logger: [-i] [-f file] [-p pri] [-t tag] [ message ... ]\n",
21734446Sbostic 	    stderr);
21834446Sbostic 	exit(1);
21934446Sbostic }
220