xref: /original-bsd/usr.bin/logger/logger.c (revision fa348642)
1 /*
2  * Copyright (c) 1983 Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 char copyright[] =
10 "@(#) Copyright (c) 1983 Regents of the University of California.\n\
11  All rights reserved.\n";
12 #endif /* not lint */
13 
14 #ifndef lint
15 static char sccsid[] = "@(#)logger.c	6.15 (Berkeley) 03/01/91";
16 #endif /* not lint */
17 
18 #include <stdio.h>
19 #include <syslog.h>
20 #include <ctype.h>
21 
22 /*
23 **  LOGGER -- read and log utility
24 **
25 **	This routine reads from an input and arranges to write the
26 **	result on the system log, along with a useful tag.
27 */
28 
29 main(argc, argv)
30 	int argc;
31 	char **argv;
32 {
33 	extern char *optarg;
34 	extern int errno, optind;
35 	int pri = LOG_NOTICE;
36 	int ch, logflags = 0;
37 	char *tag, buf[1024], *getlogin(), *strerror();
38 
39 	tag = NULL;
40 	while ((ch = getopt(argc, argv, "f:ip:st:")) != EOF)
41 		switch((char)ch) {
42 		case 'f':		/* file to log */
43 			if (freopen(optarg, "r", stdin) == NULL) {
44 				(void)fprintf(stderr, "logger: %s: %s.\n",
45 				    optarg, strerror(errno));
46 				exit(1);
47 			}
48 			break;
49 		case 'i':		/* log process id also */
50 			logflags |= LOG_PID;
51 			break;
52 		case 'p':		/* priority */
53 			pri = pencode(optarg);
54 			break;
55 		case 's':		/* log to standard error */
56 			logflags |= LOG_PERROR;
57 			break;
58 		case 't':		/* tag */
59 			tag = optarg;
60 			break;
61 		case '?':
62 		default:
63 			usage();
64 		}
65 	argc -= optind;
66 	argv += optind;
67 
68 	/* setup for logging */
69 	openlog(tag ? tag : getlogin(), logflags, 0);
70 	(void) fclose(stdout);
71 
72 	/* log input line if appropriate */
73 	if (argc > 0) {
74 		register char *p, *endp;
75 		int len;
76 
77 		for (p = buf, endp = buf + sizeof(buf) - 2; *argv;) {
78 			len = strlen(*argv);
79 			if (p + len > endp && p > buf) {
80 				syslog(pri, "%s", buf);
81 				p = buf;
82 			}
83 			if (len > sizeof(buf) - 1)
84 				syslog(pri, "%s", *argv++);
85 			else {
86 				if (p != buf)
87 					*p++ = ' ';
88 				bcopy(*argv++, p, len);
89 				*(p += len) = '\0';
90 			}
91 		}
92 		if (p != buf)
93 			syslog(pri, "%s", buf);
94 		exit(0);
95 	}
96 
97 	/* main loop */
98 	while (fgets(buf, sizeof(buf), stdin) != NULL)
99 		syslog(pri, "%s", buf);
100 
101 	exit(0);
102 }
103 
104 #define	SYSLOG_NAMES
105 #include <syslog.h>
106 
107 /*
108  *  Decode a symbolic name to a numeric value
109  */
110 pencode(s)
111 	register char *s;
112 {
113 	char *save;
114 	int fac, lev;
115 
116 	for (save = s; *s && *s != '.'; ++s);
117 	if (*s) {
118 		*s = '\0';
119 		fac = decode(save, facilitynames);
120 		if (fac < 0) {
121 			(void)fprintf(stderr,
122 			    "logger: unknown facility name: %s.\n", save);
123 			exit(1);
124 		}
125 		*s++ = '.';
126 	}
127 	else {
128 		fac = 0;
129 		s = save;
130 	}
131 	lev = decode(s, prioritynames);
132 	if (lev < 0) {
133 		(void)fprintf(stderr,
134 		    "logger: unknown priority name: %s.\n", save);
135 		exit(1);
136 	}
137 	return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK));
138 }
139 
140 decode(name, codetab)
141 	char *name;
142 	CODE *codetab;
143 {
144 	register CODE *c;
145 
146 	if (isdigit(*name))
147 		return (atoi(name));
148 
149 	for (c = codetab; c->c_name; c++)
150 		if (!strcasecmp(name, c->c_name))
151 			return (c->c_val);
152 
153 	return (-1);
154 }
155 
156 usage()
157 {
158 	(void)fprintf(stderr,
159 	    "logger: [-i] [-f file] [-p pri] [-t tag] [ message ... ]\n");
160 	exit(1);
161 }
162