xref: /csrg-svn/usr.bin/logger/logger.c (revision 34647)
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
634446Sbostic  * provided that this notice is preserved and that due credit is given
734446Sbostic  * to the University of California at Berkeley. The name of the University
834446Sbostic  * may not be used to endorse or promote products derived from this
934446Sbostic  * software without specific prior written permission. This software
1034446Sbostic  * is provided ``as is'' without express or implied warranty.
1124870Seric  */
1224870Seric 
1324870Seric #ifndef lint
1424870Seric char copyright[] =
1524870Seric "@(#) Copyright (c) 1983 Regents of the University of California.\n\
1624870Seric  All rights reserved.\n";
1734446Sbostic #endif /* not lint */
1824870Seric 
1924870Seric #ifndef lint
20*34647Sbostic static char sccsid[] = "@(#)logger.c	6.7 (Berkeley) 06/06/88";
2134446Sbostic #endif /* not lint */
2224870Seric 
2324870Seric #include <stdio.h>
2424870Seric #include <syslog.h>
2524870Seric #include <ctype.h>
2624870Seric 
2724870Seric /*
2824870Seric **  LOGGER -- read and log utility
2924870Seric **
3024870Seric **	This routine reads from an input and arranges to write the
3124870Seric **	result on the system log, along with a useful tag.
3224870Seric */
3324870Seric 
3424870Seric main(argc, argv)
3524870Seric 	int argc;
3624870Seric 	char **argv;
3724870Seric {
3834446Sbostic 	extern char *optarg;
3934446Sbostic 	extern int optind;
4024870Seric 	int pri = LOG_NOTICE;
4134446Sbostic 	int ch, logflags = 0;
4234446Sbostic 	char *tag, buf[200], *getlogin();
4324870Seric 
4434446Sbostic 	tag = NULL;
4534446Sbostic 	while ((ch = getopt(argc, argv, "f:ip:t:")) != EOF)
4634446Sbostic 		switch((char)ch) {
4734446Sbostic 		case 'f':		/* file to log */
4834446Sbostic 			if (freopen(optarg, "r", stdin) == NULL) {
4934446Sbostic 				fprintf("logger: ");
5034446Sbostic 				perror(optarg);
5134446Sbostic 				exit(1);
5224870Seric 			}
5324870Seric 			break;
5434446Sbostic 		case 'i':		/* log process id also */
5524870Seric 			logflags |= LOG_PID;
5624870Seric 			break;
5734446Sbostic 		case 'p':		/* priority */
5834446Sbostic 			pri = pencode(optarg);
5924870Seric 			break;
6034446Sbostic 		case 't':		/* tag */
6134446Sbostic 			tag = optarg;
6224870Seric 			break;
6334446Sbostic 		case '?':
6434446Sbostic 		default:
6534446Sbostic 			usage();
6624870Seric 		}
6734446Sbostic 	argc -= optind;
6834446Sbostic 	argv += optind;
6924870Seric 
7024870Seric 	/* setup for logging */
7134446Sbostic 	openlog(tag ? tag : getlogin(), logflags, 0);
7224870Seric 	(void) fclose(stdout);
7324870Seric 
7424870Seric 	/* log input line if appropriate */
7534446Sbostic 	if (argc > 0) {
7634446Sbostic 		register char *p, *endp;
7734446Sbostic 		int len;
7824870Seric 
7934446Sbostic 		for (p = buf, endp = buf + sizeof(buf) - 1;;) {
8034446Sbostic 			len = strlen(*argv);
8134512Skarels 			if (p + len < endp && p > buf) {
8234512Skarels 				*--p = '\0';
8334512Skarels 				syslog(pri, buf);
8434512Skarels 				p = buf;
8534512Skarels 			}
8634512Skarels 			if (len > sizeof(buf) - 1) {
8734512Skarels 				syslog(pri, *argv++);
8834512Skarels 				if (!--argc)
8934512Skarels 					break;
9034512Skarels 			} else {
9134446Sbostic 				bcopy(*argv++, p, len);
9234446Sbostic 				p += len;
9334446Sbostic 				if (!--argc)
9434446Sbostic 					break;
9534446Sbostic 				*p++ = ' ';
9634446Sbostic 				*--p = '\0';
9734446Sbostic 			}
9824870Seric 		}
9934446Sbostic 		if (p != buf) {
10034446Sbostic 			*p = '\0';
10134446Sbostic 			syslog(pri, buf);
10234446Sbostic 		}
10324870Seric 		exit(0);
10424870Seric 	}
10524870Seric 
10624870Seric 	/* main loop */
10734446Sbostic 	while (fgets(buf, sizeof(buf), stdin) != NULL)
10824870Seric 		syslog(pri, buf);
10924870Seric 
11024870Seric 	exit(0);
11124870Seric }
11224870Seric 
11324870Seric 
11424870Seric struct code {
11524870Seric 	char	*c_name;
11624870Seric 	int	c_val;
11724870Seric };
11824870Seric 
11924870Seric struct code	PriNames[] = {
12024870Seric 	"panic",	LOG_EMERG,
12124870Seric 	"emerg",	LOG_EMERG,
12224870Seric 	"alert",	LOG_ALERT,
12324870Seric 	"crit",		LOG_CRIT,
12424870Seric 	"err",		LOG_ERR,
12524870Seric 	"error",	LOG_ERR,
12624870Seric 	"warn",		LOG_WARNING,
12724870Seric 	"warning",	LOG_WARNING,
12824870Seric 	"notice",	LOG_NOTICE,
12924870Seric 	"info",		LOG_INFO,
13024870Seric 	"debug",	LOG_DEBUG,
13124870Seric 	NULL,		-1
13224870Seric };
13324870Seric 
13424870Seric struct code	FacNames[] = {
13524870Seric 	"kern",		LOG_KERN,
13624870Seric 	"user",		LOG_USER,
13724870Seric 	"mail",		LOG_MAIL,
13824947Seric 	"daemon",	LOG_DAEMON,
13924870Seric 	"auth",		LOG_AUTH,
14024870Seric 	"security",	LOG_AUTH,
14129918Seric 	"syslog",	LOG_SYSLOG,
14229918Seric 	"lpr",		LOG_LPR,
14329918Seric 	"news",		LOG_NEWS,
144*34647Sbostic 	"uucp",		LOG_UUCP,
14524870Seric 	"local0",	LOG_LOCAL0,
14624870Seric 	"local1",	LOG_LOCAL1,
14724870Seric 	"local2",	LOG_LOCAL2,
14824870Seric 	"local3",	LOG_LOCAL3,
14924870Seric 	"local4",	LOG_LOCAL4,
15024870Seric 	"local5",	LOG_LOCAL5,
15124870Seric 	"local6",	LOG_LOCAL6,
15224870Seric 	"local7",	LOG_LOCAL7,
15324870Seric 	NULL,		-1
15424870Seric };
15524870Seric 
15624870Seric 
15724870Seric /*
15824870Seric  *  Decode a symbolic name to a numeric value
15924870Seric  */
16024870Seric 
16124870Seric pencode(s)
16224870Seric 	register char *s;
16324870Seric {
16434446Sbostic 	char *save;
16534446Sbostic 	int fac, lev;
16624870Seric 
16734446Sbostic 	for (save = s; *s && *s != '.'; ++s);
16834446Sbostic 	if (*s) {
16934446Sbostic 		*s = '\0';
17034446Sbostic 		fac = decode(save, FacNames);
17124870Seric 		if (fac < 0)
17234446Sbostic 			bailout("unknown facility name: ", save);
17334446Sbostic 		*s++ = '.';
17434446Sbostic 	}
17534446Sbostic 	else {
17624870Seric 		fac = 0;
17734446Sbostic 		s = save;
17834446Sbostic 	}
17934446Sbostic 	lev = decode(s, PriNames);
18024870Seric 	if (lev < 0)
18134446Sbostic 		bailout("unknown priority name: ", save);
18224870Seric 	return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK));
18324870Seric }
18424870Seric 
18524870Seric 
18624870Seric decode(name, codetab)
18724870Seric 	char *name;
18824870Seric 	struct code *codetab;
18924870Seric {
19024870Seric 	register struct code *c;
19124870Seric 
19224870Seric 	if (isdigit(*name))
19324870Seric 		return (atoi(name));
19424870Seric 
19524870Seric 	for (c = codetab; c->c_name; c++)
19634446Sbostic 		if (!strcasecmp(name, c->c_name))
19724870Seric 			return (c->c_val);
19824870Seric 
19924870Seric 	return (-1);
20024870Seric }
20124870Seric 
20234446Sbostic bailout(msg, arg)
20334446Sbostic 	char *msg, *arg;
20424870Seric {
20534446Sbostic 	fprintf(stderr, "logger: %s%s\n", msg, arg);
20624870Seric 	exit(1);
20724870Seric }
20834446Sbostic 
20934446Sbostic usage()
21034446Sbostic {
21134446Sbostic 	fputs("logger: [-i] [-f file] [-p pri] [-t tag] [ message ... ]\n",
21234446Sbostic 	    stderr);
21334446Sbostic 	exit(1);
21434446Sbostic }
215