xref: /csrg-svn/usr.bin/logger/logger.c (revision 40791)
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
634911Sbostic  * provided that the above copyright notice and this paragraph are
734911Sbostic  * duplicated in all such forms and that any documentation,
834911Sbostic  * advertising materials, and other materials related to such
934911Sbostic  * distribution and use acknowledge that the software was developed
1034911Sbostic  * by the University of California, Berkeley.  The name of the
1134911Sbostic  * University may not be used to endorse or promote products derived
1234911Sbostic  * from this software without specific prior written permission.
1334911Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1434911Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1534911Sbostic  * 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*40791Sbostic static char sccsid[] = "@(#)logger.c	6.13 (Berkeley) 04/05/90";
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;
44*40791Sbostic 	extern int errno, optind;
4524870Seric 	int pri = LOG_NOTICE;
4634446Sbostic 	int ch, logflags = 0;
47*40791Sbostic 	char *tag, buf[1024], *getlogin(), *strerror();
4824870Seric 
4934446Sbostic 	tag = NULL;
5036791Sbostic 	while ((ch = getopt(argc, argv, "f:ip:st:")) != EOF)
5134446Sbostic 		switch((char)ch) {
5234446Sbostic 		case 'f':		/* file to log */
5334446Sbostic 			if (freopen(optarg, "r", stdin) == NULL) {
54*40791Sbostic 				(void)fprintf(stderr, "logger: %s: %s.\n",
55*40791Sbostic 				    optarg, strerror(errno));
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;
6536791Sbostic 		case 's':		/* log to standard error */
6636791Sbostic 			logflags |= LOG_PERROR;
6736791Sbostic 			break;
6834446Sbostic 		case 't':		/* tag */
6934446Sbostic 			tag = optarg;
7024870Seric 			break;
7134446Sbostic 		case '?':
7234446Sbostic 		default:
7334446Sbostic 			usage();
7424870Seric 		}
7534446Sbostic 	argc -= optind;
7634446Sbostic 	argv += optind;
7724870Seric 
7824870Seric 	/* setup for logging */
7934446Sbostic 	openlog(tag ? tag : getlogin(), logflags, 0);
8024870Seric 	(void) fclose(stdout);
8124870Seric 
8224870Seric 	/* log input line if appropriate */
8334446Sbostic 	if (argc > 0) {
8434446Sbostic 		register char *p, *endp;
8534446Sbostic 		int len;
8624870Seric 
8736438Sbostic 		for (p = buf, endp = buf + sizeof(buf) - 2; *argv;) {
8834446Sbostic 			len = strlen(*argv);
8936438Sbostic 			if (p + len > endp && p > buf) {
9035308Sbostic 				syslog(pri, "%s", buf);
9134512Skarels 				p = buf;
9234512Skarels 			}
9336438Sbostic 			if (len > sizeof(buf) - 1)
9435308Sbostic 				syslog(pri, "%s", *argv++);
9536438Sbostic 			else {
9636438Sbostic 				if (p != buf)
9736438Sbostic 					*p++ = ' ';
9834446Sbostic 				bcopy(*argv++, p, len);
9936438Sbostic 				*(p += len) = '\0';
10034446Sbostic 			}
10124870Seric 		}
10236438Sbostic 		if (p != buf)
10335308Sbostic 			syslog(pri, "%s", buf);
10424870Seric 		exit(0);
10524870Seric 	}
10624870Seric 
10724870Seric 	/* main loop */
10834446Sbostic 	while (fgets(buf, sizeof(buf), stdin) != NULL)
10935308Sbostic 		syslog(pri, "%s", buf);
11024870Seric 
11124870Seric 	exit(0);
11224870Seric }
11324870Seric 
11439527Sbostic #define	SYSLOG_NAMES
11539527Sbostic #include <syslog.h>
11624870Seric 
11724870Seric /*
11824870Seric  *  Decode a symbolic name to a numeric value
11924870Seric  */
12024870Seric pencode(s)
12124870Seric 	register char *s;
12224870Seric {
12334446Sbostic 	char *save;
12434446Sbostic 	int fac, lev;
12524870Seric 
12634446Sbostic 	for (save = s; *s && *s != '.'; ++s);
12734446Sbostic 	if (*s) {
12834446Sbostic 		*s = '\0';
12939527Sbostic 		fac = decode(save, facilitynames);
13024870Seric 		if (fac < 0)
13134446Sbostic 			bailout("unknown facility name: ", save);
13234446Sbostic 		*s++ = '.';
13334446Sbostic 	}
13434446Sbostic 	else {
13524870Seric 		fac = 0;
13634446Sbostic 		s = save;
13734446Sbostic 	}
13839527Sbostic 	lev = decode(s, prioritynames);
13924870Seric 	if (lev < 0)
14034446Sbostic 		bailout("unknown priority name: ", save);
14124870Seric 	return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK));
14224870Seric }
14324870Seric 
14424870Seric 
14524870Seric decode(name, codetab)
14624870Seric 	char *name;
14739527Sbostic 	CODE *codetab;
14824870Seric {
14939527Sbostic 	register CODE *c;
15024870Seric 
15124870Seric 	if (isdigit(*name))
15224870Seric 		return (atoi(name));
15324870Seric 
15424870Seric 	for (c = codetab; c->c_name; c++)
15534446Sbostic 		if (!strcasecmp(name, c->c_name))
15624870Seric 			return (c->c_val);
15724870Seric 
15824870Seric 	return (-1);
15924870Seric }
16024870Seric 
16134446Sbostic bailout(msg, arg)
16234446Sbostic 	char *msg, *arg;
16324870Seric {
16434446Sbostic 	fprintf(stderr, "logger: %s%s\n", msg, arg);
16524870Seric 	exit(1);
16624870Seric }
16734446Sbostic 
16834446Sbostic usage()
16934446Sbostic {
17034446Sbostic 	fputs("logger: [-i] [-f file] [-p pri] [-t tag] [ message ... ]\n",
17134446Sbostic 	    stderr);
17234446Sbostic 	exit(1);
17334446Sbostic }
174