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*34512Skarels static char sccsid[] = "@(#)logger.c 6.6 (Berkeley) 05/27/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); 81*34512Skarels if (p + len < endp && p > buf) { 82*34512Skarels *--p = '\0'; 83*34512Skarels syslog(pri, buf); 84*34512Skarels p = buf; 85*34512Skarels } 86*34512Skarels if (len > sizeof(buf) - 1) { 87*34512Skarels syslog(pri, *argv++); 88*34512Skarels if (!--argc) 89*34512Skarels break; 90*34512Skarels } 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, 14424870Seric "local0", LOG_LOCAL0, 14524870Seric "local1", LOG_LOCAL1, 14624870Seric "local2", LOG_LOCAL2, 14724870Seric "local3", LOG_LOCAL3, 14824870Seric "local4", LOG_LOCAL4, 14924870Seric "local5", LOG_LOCAL5, 15024870Seric "local6", LOG_LOCAL6, 15124870Seric "local7", LOG_LOCAL7, 15224870Seric NULL, -1 15324870Seric }; 15424870Seric 15524870Seric 15624870Seric /* 15724870Seric * Decode a symbolic name to a numeric value 15824870Seric */ 15924870Seric 16024870Seric pencode(s) 16124870Seric register char *s; 16224870Seric { 16334446Sbostic char *save; 16434446Sbostic int fac, lev; 16524870Seric 16634446Sbostic for (save = s; *s && *s != '.'; ++s); 16734446Sbostic if (*s) { 16834446Sbostic *s = '\0'; 16934446Sbostic fac = decode(save, FacNames); 17024870Seric if (fac < 0) 17134446Sbostic bailout("unknown facility name: ", save); 17234446Sbostic *s++ = '.'; 17334446Sbostic } 17434446Sbostic else { 17524870Seric fac = 0; 17634446Sbostic s = save; 17734446Sbostic } 17834446Sbostic lev = decode(s, PriNames); 17924870Seric if (lev < 0) 18034446Sbostic bailout("unknown priority name: ", save); 18124870Seric return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK)); 18224870Seric } 18324870Seric 18424870Seric 18524870Seric decode(name, codetab) 18624870Seric char *name; 18724870Seric struct code *codetab; 18824870Seric { 18924870Seric register struct code *c; 19024870Seric 19124870Seric if (isdigit(*name)) 19224870Seric return (atoi(name)); 19324870Seric 19424870Seric for (c = codetab; c->c_name; c++) 19534446Sbostic if (!strcasecmp(name, c->c_name)) 19624870Seric return (c->c_val); 19724870Seric 19824870Seric return (-1); 19924870Seric } 20024870Seric 20134446Sbostic bailout(msg, arg) 20234446Sbostic char *msg, *arg; 20324870Seric { 20434446Sbostic fprintf(stderr, "logger: %s%s\n", msg, arg); 20524870Seric exit(1); 20624870Seric } 20734446Sbostic 20834446Sbostic usage() 20934446Sbostic { 21034446Sbostic fputs("logger: [-i] [-f file] [-p pri] [-t tag] [ message ... ]\n", 21134446Sbostic stderr); 21234446Sbostic exit(1); 21334446Sbostic } 214