xref: /freebsd/usr.bin/logger/logger.c (revision a04667ab172598d17ccf33d73ba13f427982527a)
19b50d902SRodney W. Grimes /*
29b50d902SRodney W. Grimes  * Copyright (c) 1983, 1993
39b50d902SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
49b50d902SRodney W. Grimes  *
59b50d902SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
69b50d902SRodney W. Grimes  * modification, are permitted provided that the following conditions
79b50d902SRodney W. Grimes  * are met:
89b50d902SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
99b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
109b50d902SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
119b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
129b50d902SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
139b50d902SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
149b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
159b50d902SRodney W. Grimes  *    without specific prior written permission.
169b50d902SRodney W. Grimes  *
179b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
189b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
199b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
209b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
219b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
229b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
239b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
249b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
259b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
269b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
279b50d902SRodney W. Grimes  * SUCH DAMAGE.
289b50d902SRodney W. Grimes  */
299b50d902SRodney W. Grimes 
309b50d902SRodney W. Grimes #ifndef lint
31df071556SPhilippe Charnier static const char copyright[] =
329b50d902SRodney W. Grimes "@(#) Copyright (c) 1983, 1993\n\
339b50d902SRodney W. Grimes 	The Regents of the University of California.  All rights reserved.\n";
349b50d902SRodney W. Grimes #endif /* not lint */
359b50d902SRodney W. Grimes 
36df071556SPhilippe Charnier #if 0
37865059c8SPhilippe Charnier #ifndef lint
389b50d902SRodney W. Grimes static char sccsid[] = "@(#)logger.c	8.1 (Berkeley) 6/6/93";
399b50d902SRodney W. Grimes #endif /* not lint */
40865059c8SPhilippe Charnier #endif
41865059c8SPhilippe Charnier 
429bd5ae85SDavid Malone #include <sys/cdefs.h>
439bd5ae85SDavid Malone __FBSDID("$FreeBSD$");
449bd5ae85SDavid Malone 
45b0fe2da8SDavid Malone #include <sys/types.h>
46b0fe2da8SDavid Malone #include <sys/socket.h>
47b0fe2da8SDavid Malone #include <netinet/in.h>
48b0fe2da8SDavid Malone 
499b50d902SRodney W. Grimes #include <ctype.h>
50df071556SPhilippe Charnier #include <err.h>
51b0fe2da8SDavid Malone #include <netdb.h>
52df071556SPhilippe Charnier #include <stdio.h>
53df071556SPhilippe Charnier #include <stdlib.h>
549b50d902SRodney W. Grimes #include <string.h>
55df071556SPhilippe Charnier #include <unistd.h>
569b50d902SRodney W. Grimes 
579b50d902SRodney W. Grimes #define	SYSLOG_NAMES
589b50d902SRodney W. Grimes #include <syslog.h>
599b50d902SRodney W. Grimes 
60*a04667abSHiroki Sato #define	sstosa(ss)	((struct sockaddr *)(void *)ss)
619b50d902SRodney W. Grimes 
620b5f90afSHajimu UMEMOTO struct socks {
63*a04667abSHiroki Sato 	int sk_sock;
64*a04667abSHiroki Sato 	int sk_addrlen;
65*a04667abSHiroki Sato 	struct sockaddr_storage sk_addr;
660b5f90afSHajimu UMEMOTO };
670b5f90afSHajimu UMEMOTO 
68*a04667abSHiroki Sato static int	decode(char *, const CODE *);
69*a04667abSHiroki Sato static int	pencode(char *);
70*a04667abSHiroki Sato static ssize_t	socksetup(const char *, const char *, const char *,
71*a04667abSHiroki Sato 		    struct socks **);
72*a04667abSHiroki Sato static void	logmessage(int, const char *, struct socks *, ssize_t,
73*a04667abSHiroki Sato 			   const char *);
74*a04667abSHiroki Sato static void	usage(void);
75*a04667abSHiroki Sato 
760b5f90afSHajimu UMEMOTO #ifdef INET6
77481bce6cSEd Schouten static int family = PF_UNSPEC;	/* protocol family (IPv4, IPv6 or both) */
780b5f90afSHajimu UMEMOTO #else
79481bce6cSEd Schouten static int family = PF_INET;	/* protocol family (IPv4 only) */
800b5f90afSHajimu UMEMOTO #endif
81481bce6cSEd Schouten static int send_to_all = 0;	/* send message to all IPv4/IPv6 addresses */
820b5f90afSHajimu UMEMOTO 
839b50d902SRodney W. Grimes /*
849b50d902SRodney W. Grimes  * logger -- read and log utility
859b50d902SRodney W. Grimes  *
869b50d902SRodney W. Grimes  *	Reads from an input and arranges to write the result on the system
879b50d902SRodney W. Grimes  *	log.
889b50d902SRodney W. Grimes  */
899b50d902SRodney W. Grimes int
90f4ac32deSDavid Malone main(int argc, char *argv[])
919b50d902SRodney W. Grimes {
92*a04667abSHiroki Sato 	struct socks *socks;
93*a04667abSHiroki Sato 	ssize_t nsock;
949b50d902SRodney W. Grimes 	int ch, logflags, pri;
956f62d863SDavid Malone 	char *tag, *host, buf[1024];
96*a04667abSHiroki Sato 	const char *svcname, *src;
979b50d902SRodney W. Grimes 
989b50d902SRodney W. Grimes 	tag = NULL;
99b0fe2da8SDavid Malone 	host = NULL;
1006b04b7f6SBruce M Simpson 	svcname = "syslog";
101*a04667abSHiroki Sato 	src = NULL;
102ca122bf7SRuslan Ermilov 	pri = LOG_USER | LOG_NOTICE;
1039b50d902SRodney W. Grimes 	logflags = 0;
10440244c28SPoul-Henning Kamp 	unsetenv("TZ");
105*a04667abSHiroki Sato 	while ((ch = getopt(argc, argv, "46Af:h:iP:p:S:st:")) != -1)
1069b50d902SRodney W. Grimes 		switch((char)ch) {
1070b5f90afSHajimu UMEMOTO 		case '4':
1080b5f90afSHajimu UMEMOTO 			family = PF_INET;
1090b5f90afSHajimu UMEMOTO 			break;
1100b5f90afSHajimu UMEMOTO #ifdef INET6
1110b5f90afSHajimu UMEMOTO 		case '6':
1120b5f90afSHajimu UMEMOTO 			family = PF_INET6;
1130b5f90afSHajimu UMEMOTO 			break;
1140b5f90afSHajimu UMEMOTO #endif
1150b5f90afSHajimu UMEMOTO 		case 'A':
1160b5f90afSHajimu UMEMOTO 			send_to_all++;
1170b5f90afSHajimu UMEMOTO 			break;
1189b50d902SRodney W. Grimes 		case 'f':		/* file to log */
119df071556SPhilippe Charnier 			if (freopen(optarg, "r", stdin) == NULL)
120df071556SPhilippe Charnier 				err(1, "%s", optarg);
1214145bb53SKirk McKusick 			setvbuf(stdin, 0, _IONBF, 0);
1229b50d902SRodney W. Grimes 			break;
123b0fe2da8SDavid Malone 		case 'h':		/* hostname to deliver to */
124b0fe2da8SDavid Malone 			host = optarg;
125b0fe2da8SDavid Malone 			break;
1269b50d902SRodney W. Grimes 		case 'i':		/* log process id also */
1279b50d902SRodney W. Grimes 			logflags |= LOG_PID;
1289b50d902SRodney W. Grimes 			break;
1296b04b7f6SBruce M Simpson 		case 'P':		/* service name or port number */
1306b04b7f6SBruce M Simpson 			svcname = optarg;
1316b04b7f6SBruce M Simpson 			break;
1329b50d902SRodney W. Grimes 		case 'p':		/* priority */
1339b50d902SRodney W. Grimes 			pri = pencode(optarg);
1349b50d902SRodney W. Grimes 			break;
1359b50d902SRodney W. Grimes 		case 's':		/* log to standard error */
1369b50d902SRodney W. Grimes 			logflags |= LOG_PERROR;
1379b50d902SRodney W. Grimes 			break;
138*a04667abSHiroki Sato 		case 'S':		/* source address */
139*a04667abSHiroki Sato 			src = optarg;
140*a04667abSHiroki Sato 			break;
1419b50d902SRodney W. Grimes 		case 't':		/* tag */
1429b50d902SRodney W. Grimes 			tag = optarg;
1439b50d902SRodney W. Grimes 			break;
1449b50d902SRodney W. Grimes 		case '?':
1459b50d902SRodney W. Grimes 		default:
1469b50d902SRodney W. Grimes 			usage();
1479b50d902SRodney W. Grimes 		}
1489b50d902SRodney W. Grimes 	argc -= optind;
1499b50d902SRodney W. Grimes 	argv += optind;
1509b50d902SRodney W. Grimes 
151*a04667abSHiroki Sato 	if (host) {
152*a04667abSHiroki Sato 		nsock = socksetup(src, host, svcname, &socks);
153*a04667abSHiroki Sato 		if (nsock <= 0)
154*a04667abSHiroki Sato 			errx(1, "socket");
155*a04667abSHiroki Sato 	} else {
156*a04667abSHiroki Sato 		if (src)
157*a04667abSHiroki Sato 			errx(1, "-h option is missing.");
158*a04667abSHiroki Sato 		nsock = 0;
159*a04667abSHiroki Sato 	}
160*a04667abSHiroki Sato 
16181280940SEdwin Groothuis 	if (tag == NULL)
16281280940SEdwin Groothuis 		tag = getlogin();
1639b50d902SRodney W. Grimes 	/* setup for logging */
16481280940SEdwin Groothuis 	if (host == NULL)
16581280940SEdwin Groothuis 		openlog(tag, logflags, 0);
1669b50d902SRodney W. Grimes 	(void) fclose(stdout);
1679b50d902SRodney W. Grimes 
1689b50d902SRodney W. Grimes 	/* log input line if appropriate */
1699b50d902SRodney W. Grimes 	if (argc > 0) {
170f4ac32deSDavid Malone 		char *p, *endp;
1719bd5ae85SDavid Malone 		size_t len;
1729b50d902SRodney W. Grimes 
1739b50d902SRodney W. Grimes 		for (p = buf, endp = buf + sizeof(buf) - 2; *argv;) {
1749b50d902SRodney W. Grimes 			len = strlen(*argv);
1759b50d902SRodney W. Grimes 			if (p + len > endp && p > buf) {
176*a04667abSHiroki Sato 				logmessage(pri, tag, socks, nsock, buf);
1779b50d902SRodney W. Grimes 				p = buf;
1789b50d902SRodney W. Grimes 			}
1799b50d902SRodney W. Grimes 			if (len > sizeof(buf) - 1)
180*a04667abSHiroki Sato 				logmessage(pri, tag, socks, nsock, *argv++);
1819b50d902SRodney W. Grimes 			else {
1829b50d902SRodney W. Grimes 				if (p != buf)
1839b50d902SRodney W. Grimes 					*p++ = ' ';
1849b50d902SRodney W. Grimes 				bcopy(*argv++, p, len);
1859b50d902SRodney W. Grimes 				*(p += len) = '\0';
1869b50d902SRodney W. Grimes 			}
1879b50d902SRodney W. Grimes 		}
1889b50d902SRodney W. Grimes 		if (p != buf)
189*a04667abSHiroki Sato 			logmessage(pri, tag, socks, nsock, buf);
1909b50d902SRodney W. Grimes 	} else
1919b50d902SRodney W. Grimes 		while (fgets(buf, sizeof(buf), stdin) != NULL)
192*a04667abSHiroki Sato 			logmessage(pri, tag, socks, nsock, buf);
1939b50d902SRodney W. Grimes 	exit(0);
1949b50d902SRodney W. Grimes }
1959b50d902SRodney W. Grimes 
196*a04667abSHiroki Sato static ssize_t
197*a04667abSHiroki Sato socksetup(const char *src, const char *dst, const char *svcname,
198*a04667abSHiroki Sato 	struct socks **socks)
199*a04667abSHiroki Sato {
200*a04667abSHiroki Sato 	struct addrinfo hints, *res, *res0;
201*a04667abSHiroki Sato 	struct sockaddr_storage *ss_src[AF_MAX];
202*a04667abSHiroki Sato 	struct socks *sk;
203*a04667abSHiroki Sato 	ssize_t nsock = 0;
204*a04667abSHiroki Sato 	int error, maxs;
205*a04667abSHiroki Sato 
206*a04667abSHiroki Sato 	memset(&ss_src[0], 0, sizeof(ss_src));
207*a04667abSHiroki Sato 	if (src) {
208*a04667abSHiroki Sato 		char *p, *p0, *hs, *hbuf, *sbuf;
209*a04667abSHiroki Sato 
210*a04667abSHiroki Sato 		hbuf = sbuf = NULL;
211*a04667abSHiroki Sato 		p0 = p = strdup(src);
212*a04667abSHiroki Sato 		if (p0 == NULL)
213*a04667abSHiroki Sato 			err(1, "strdup failed");
214*a04667abSHiroki Sato 		hs = p0;	/* point to search ":" */
215*a04667abSHiroki Sato #ifdef INET6
216*a04667abSHiroki Sato 		/* -S option supports IPv6 addr in "[2001:db8::1]:service". */
217*a04667abSHiroki Sato 		if (*p0 == '[') {
218*a04667abSHiroki Sato 			p = strchr(p0, ']');
219*a04667abSHiroki Sato 			if (p == NULL)
220*a04667abSHiroki Sato 				errx(1, "\"]\" not found in src addr");
221*a04667abSHiroki Sato 			*p = '\0';
222*a04667abSHiroki Sato 			/* hs points just after ']' (':' or '\0'). */
223*a04667abSHiroki Sato 			hs = p + 1;
224*a04667abSHiroki Sato 			/*
225*a04667abSHiroki Sato 			 * p points just after '[' while it points hs
226*a04667abSHiroki Sato 			 * in the case of [].
227*a04667abSHiroki Sato 			 */
228*a04667abSHiroki Sato 			p = ((p0 + 1) == (hs - 1)) ? hs : p0 + 1;
229*a04667abSHiroki Sato 		}
230*a04667abSHiroki Sato #endif
231*a04667abSHiroki Sato 		if (*p != '\0') {
232*a04667abSHiroki Sato 			/* (p == hs) means ":514" or "[]:514". */
233*a04667abSHiroki Sato 			hbuf = (p == hs && *p == ':') ? NULL : p;
234*a04667abSHiroki Sato 			p = strchr(hs, ':');
235*a04667abSHiroki Sato 			if (p != NULL) {
236*a04667abSHiroki Sato 				*p = '\0';
237*a04667abSHiroki Sato 				sbuf = (*(p + 1) != '\0') ? p + 1 : NULL;
238*a04667abSHiroki Sato 			}
239*a04667abSHiroki Sato 		}
240*a04667abSHiroki Sato 		hints = (struct addrinfo){
241*a04667abSHiroki Sato 			.ai_family = family,
242*a04667abSHiroki Sato 			.ai_socktype = SOCK_DGRAM,
243*a04667abSHiroki Sato 			.ai_flags = AI_PASSIVE
244*a04667abSHiroki Sato 		};
245*a04667abSHiroki Sato 		error = getaddrinfo(hbuf, sbuf, &hints, &res0);
246*a04667abSHiroki Sato 		if (error)
247*a04667abSHiroki Sato 			errx(1, "%s: %s", gai_strerror(error), src);
248*a04667abSHiroki Sato 		for (res = res0; res; res = res->ai_next) {
249*a04667abSHiroki Sato 			switch (res->ai_family) {
250*a04667abSHiroki Sato 			case AF_INET:
251*a04667abSHiroki Sato #ifdef INET6
252*a04667abSHiroki Sato 			case AF_INET6:
253*a04667abSHiroki Sato #endif
254*a04667abSHiroki Sato 				if (ss_src[res->ai_family] != NULL)
255*a04667abSHiroki Sato 					continue;
256*a04667abSHiroki Sato 				ss_src[res->ai_family] =
257*a04667abSHiroki Sato 				    malloc(sizeof(struct sockaddr_storage));
258*a04667abSHiroki Sato 				if (ss_src[res->ai_family] == NULL)
259*a04667abSHiroki Sato 					err(1, "malloc failed");
260*a04667abSHiroki Sato 				memcpy(ss_src[res->ai_family], res->ai_addr,
261*a04667abSHiroki Sato 				    res->ai_addrlen);
262*a04667abSHiroki Sato 			}
263*a04667abSHiroki Sato 		}
264*a04667abSHiroki Sato 		freeaddrinfo(res0);
265*a04667abSHiroki Sato 		free(p0);
266*a04667abSHiroki Sato 	}
267*a04667abSHiroki Sato 
268*a04667abSHiroki Sato 	/* resolve hostname */
269*a04667abSHiroki Sato 	hints = (struct addrinfo){
270*a04667abSHiroki Sato 		.ai_family = family,
271*a04667abSHiroki Sato 		.ai_socktype = SOCK_DGRAM
272*a04667abSHiroki Sato 	};
273*a04667abSHiroki Sato 	error = getaddrinfo(dst, svcname, &hints, &res0);
274*a04667abSHiroki Sato 	if (error == EAI_SERVICE) {
275*a04667abSHiroki Sato 		warnx("%s/udp: unknown service", svcname);
276*a04667abSHiroki Sato 		error = getaddrinfo(dst, "514", &hints, &res);
277*a04667abSHiroki Sato 	}
278*a04667abSHiroki Sato 	if (error)
279*a04667abSHiroki Sato 		errx(1, "%s: %s", gai_strerror(error), dst);
280*a04667abSHiroki Sato 	/* count max number of sockets we may open */
281*a04667abSHiroki Sato 	maxs = 0;
282*a04667abSHiroki Sato 	for (res = res0; res; res = res->ai_next)
283*a04667abSHiroki Sato 		maxs++;
284*a04667abSHiroki Sato 	sk = calloc(maxs, sizeof(*sk));
285*a04667abSHiroki Sato 	if (sk == NULL)
286*a04667abSHiroki Sato 		errx(1, "couldn't allocate memory for sockets");
287*a04667abSHiroki Sato 	for (res = res0; res; res = res->ai_next) {
288*a04667abSHiroki Sato 		int s;
289*a04667abSHiroki Sato 
290*a04667abSHiroki Sato 		s = socket(res->ai_family, res->ai_socktype,
291*a04667abSHiroki Sato 		    res->ai_protocol);
292*a04667abSHiroki Sato 		if (s < 0)
293*a04667abSHiroki Sato 			continue;
294*a04667abSHiroki Sato 		if (src && ss_src[res->ai_family] == NULL)
295*a04667abSHiroki Sato 			errx(1, "address family mismatch");
296*a04667abSHiroki Sato 
297*a04667abSHiroki Sato 		if (ss_src[res->ai_family]) {
298*a04667abSHiroki Sato 			error = bind(s, sstosa(ss_src[res->ai_family]),
299*a04667abSHiroki Sato 				    ss_src[res->ai_family]->ss_len);
300*a04667abSHiroki Sato 			if (error < 0)
301*a04667abSHiroki Sato 				err(1, "bind");
302*a04667abSHiroki Sato 		}
303*a04667abSHiroki Sato 		sk[nsock] = (struct socks){
304*a04667abSHiroki Sato 			.sk_addrlen = res->ai_addrlen,
305*a04667abSHiroki Sato 			.sk_sock = s
306*a04667abSHiroki Sato 		};
307*a04667abSHiroki Sato 		memcpy(&sk[nsock].sk_addr, res->ai_addr, res->ai_addrlen);
308*a04667abSHiroki Sato 		nsock++;
309*a04667abSHiroki Sato 	}
310*a04667abSHiroki Sato 	freeaddrinfo(res0);
311*a04667abSHiroki Sato 
312*a04667abSHiroki Sato 	*socks = sk;
313*a04667abSHiroki Sato 	return (nsock);
314*a04667abSHiroki Sato }
315*a04667abSHiroki Sato 
3169b50d902SRodney W. Grimes /*
317b0fe2da8SDavid Malone  *  Send the message to syslog, either on the local host, or on a remote host
318b0fe2da8SDavid Malone  */
319481bce6cSEd Schouten static void
320*a04667abSHiroki Sato logmessage(int pri, const char *tag, struct socks *sk, ssize_t nsock,
32181280940SEdwin Groothuis 	const char *buf)
322b0fe2da8SDavid Malone {
323b0fe2da8SDavid Malone 	char *line;
324*a04667abSHiroki Sato 	int len, i, lsent;
325b0fe2da8SDavid Malone 
326*a04667abSHiroki Sato 	if (nsock == 0) {
327b0fe2da8SDavid Malone 		syslog(pri, "%s", buf);
328b0fe2da8SDavid Malone 		return;
329b0fe2da8SDavid Malone 	}
33081280940SEdwin Groothuis 	if ((len = asprintf(&line, "<%d>%s: %s", pri, tag, buf)) == -1)
331b0fe2da8SDavid Malone 		errx(1, "asprintf");
332b0fe2da8SDavid Malone 
333865059c8SPhilippe Charnier 	lsent = -1;
334*a04667abSHiroki Sato 	for (i = 0; i < nsock; i++) {
335*a04667abSHiroki Sato 		lsent = sendto(sk[i].sk_sock, line, len, 0,
336*a04667abSHiroki Sato 			       sstosa(&sk[i].sk_addr), sk[i].sk_addrlen);
3370b5f90afSHajimu UMEMOTO 		if (lsent == len && !send_to_all)
3380b5f90afSHajimu UMEMOTO 			break;
3390b5f90afSHajimu UMEMOTO 	}
3409bd5ae85SDavid Malone 	if (lsent != len) {
34157c1a0b6SBill Fenner 		if (lsent == -1)
34257c1a0b6SBill Fenner 			warn("sendto");
34357c1a0b6SBill Fenner 		else
34457c1a0b6SBill Fenner 			warnx("sendto: short send - %d bytes", lsent);
3459bd5ae85SDavid Malone 	}
346b0fe2da8SDavid Malone 
347b0fe2da8SDavid Malone 	free(line);
348b0fe2da8SDavid Malone }
349b0fe2da8SDavid Malone 
350b0fe2da8SDavid Malone /*
3519b50d902SRodney W. Grimes  *  Decode a symbolic name to a numeric value
3529b50d902SRodney W. Grimes  */
353481bce6cSEd Schouten static int
354f4ac32deSDavid Malone pencode(char *s)
3559b50d902SRodney W. Grimes {
3569b50d902SRodney W. Grimes 	char *save;
3579b50d902SRodney W. Grimes 	int fac, lev;
3589b50d902SRodney W. Grimes 
3599b50d902SRodney W. Grimes 	for (save = s; *s && *s != '.'; ++s);
3609b50d902SRodney W. Grimes 	if (*s) {
3619b50d902SRodney W. Grimes 		*s = '\0';
3629b50d902SRodney W. Grimes 		fac = decode(save, facilitynames);
363df071556SPhilippe Charnier 		if (fac < 0)
364df071556SPhilippe Charnier 			errx(1, "unknown facility name: %s", save);
3659b50d902SRodney W. Grimes 		*s++ = '.';
3669b50d902SRodney W. Grimes 	}
3679b50d902SRodney W. Grimes 	else {
3689b50d902SRodney W. Grimes 		fac = 0;
3699b50d902SRodney W. Grimes 		s = save;
3709b50d902SRodney W. Grimes 	}
3719b50d902SRodney W. Grimes 	lev = decode(s, prioritynames);
372df071556SPhilippe Charnier 	if (lev < 0)
373df071556SPhilippe Charnier 		errx(1, "unknown priority name: %s", save);
3749b50d902SRodney W. Grimes 	return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK));
3759b50d902SRodney W. Grimes }
3769b50d902SRodney W. Grimes 
377481bce6cSEd Schouten static int
37839893d56SEd Schouten decode(char *name, const CODE *codetab)
3799b50d902SRodney W. Grimes {
38039893d56SEd Schouten 	const CODE *c;
3819b50d902SRodney W. Grimes 
3829b50d902SRodney W. Grimes 	if (isdigit(*name))
3839b50d902SRodney W. Grimes 		return (atoi(name));
3849b50d902SRodney W. Grimes 
3859b50d902SRodney W. Grimes 	for (c = codetab; c->c_name; c++)
3869b50d902SRodney W. Grimes 		if (!strcasecmp(name, c->c_name))
3879b50d902SRodney W. Grimes 			return (c->c_val);
3889b50d902SRodney W. Grimes 
3899b50d902SRodney W. Grimes 	return (-1);
3909b50d902SRodney W. Grimes }
3919b50d902SRodney W. Grimes 
392df071556SPhilippe Charnier static void
393f4ac32deSDavid Malone usage(void)
3949b50d902SRodney W. Grimes {
395b0fe2da8SDavid Malone 	(void)fprintf(stderr, "usage: %s\n",
3966b04b7f6SBruce M Simpson 	    "logger [-46Ais] [-f file] [-h host] [-P port] [-p pri] [-t tag]\n"
397*a04667abSHiroki Sato 	    "              [-S addr:port] [message ...]"
398b0fe2da8SDavid Malone 	    );
3999b50d902SRodney W. Grimes 	exit(1);
4009b50d902SRodney W. Grimes }
401