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