124870Seric /*
2*62070Sbostic * Copyright (c) 1983, 1993
3*62070Sbostic * The Regents of the University of California. All rights reserved.
434446Sbostic *
542739Sbostic * %sccs.include.redist.c%
624870Seric */
724870Seric
824870Seric #ifndef lint
9*62070Sbostic static char copyright[] =
10*62070Sbostic "@(#) Copyright (c) 1983, 1993\n\
11*62070Sbostic The Regents of the University of California. All rights reserved.\n";
1234446Sbostic #endif /* not lint */
1324870Seric
1424870Seric #ifndef lint
15*62070Sbostic static char sccsid[] = "@(#)logger.c 8.1 (Berkeley) 06/06/93";
1634446Sbostic #endif /* not lint */
1724870Seric
1854368Sbostic #include <errno.h>
1954368Sbostic #include <unistd.h>
2054368Sbostic #include <stdlib.h>
2124870Seric #include <stdio.h>
2224870Seric #include <ctype.h>
2354368Sbostic #include <string.h>
2424870Seric
2554368Sbostic #define SYSLOG_NAMES
2654368Sbostic #include <syslog.h>
2754368Sbostic
2854368Sbostic int decode __P((char *, CODE *));
2954368Sbostic int pencode __P((char *));
3054368Sbostic void usage __P((void));
3154368Sbostic
3224870Seric /*
3354368Sbostic * logger -- read and log utility
3454368Sbostic *
3554368Sbostic * Reads from an input and arranges to write the result on the system
3654368Sbostic * log.
3754368Sbostic */
3854368Sbostic int
main(argc,argv)3924870Seric main(argc, argv)
4024870Seric int argc;
4154368Sbostic char *argv[];
4224870Seric {
4354368Sbostic int ch, logflags, pri;
4454368Sbostic char *tag, buf[1024];
4524870Seric
4634446Sbostic tag = NULL;
4754368Sbostic pri = LOG_NOTICE;
4854368Sbostic logflags = 0;
4936791Sbostic while ((ch = getopt(argc, argv, "f:ip:st:")) != EOF)
5034446Sbostic switch((char)ch) {
5134446Sbostic case 'f': /* file to log */
5234446Sbostic if (freopen(optarg, "r", stdin) == NULL) {
5340791Sbostic (void)fprintf(stderr, "logger: %s: %s.\n",
5440791Sbostic optarg, strerror(errno));
5534446Sbostic exit(1);
5624870Seric }
5724870Seric break;
5834446Sbostic case 'i': /* log process id also */
5924870Seric logflags |= LOG_PID;
6024870Seric break;
6134446Sbostic case 'p': /* priority */
6234446Sbostic pri = pencode(optarg);
6324870Seric break;
6436791Sbostic case 's': /* log to standard error */
6536791Sbostic logflags |= LOG_PERROR;
6636791Sbostic break;
6734446Sbostic case 't': /* tag */
6834446Sbostic tag = optarg;
6924870Seric break;
7034446Sbostic case '?':
7134446Sbostic default:
7234446Sbostic usage();
7324870Seric }
7434446Sbostic argc -= optind;
7534446Sbostic argv += optind;
7624870Seric
7724870Seric /* setup for logging */
7834446Sbostic openlog(tag ? tag : getlogin(), logflags, 0);
7924870Seric (void) fclose(stdout);
8024870Seric
8124870Seric /* log input line if appropriate */
8234446Sbostic if (argc > 0) {
8334446Sbostic register char *p, *endp;
8434446Sbostic int len;
8524870Seric
8636438Sbostic for (p = buf, endp = buf + sizeof(buf) - 2; *argv;) {
8734446Sbostic len = strlen(*argv);
8836438Sbostic if (p + len > endp && p > buf) {
8935308Sbostic syslog(pri, "%s", buf);
9034512Skarels p = buf;
9134512Skarels }
9236438Sbostic if (len > sizeof(buf) - 1)
9335308Sbostic syslog(pri, "%s", *argv++);
9436438Sbostic else {
9536438Sbostic if (p != buf)
9636438Sbostic *p++ = ' ';
9734446Sbostic bcopy(*argv++, p, len);
9836438Sbostic *(p += len) = '\0';
9934446Sbostic }
10024870Seric }
10136438Sbostic if (p != buf)
10235308Sbostic syslog(pri, "%s", buf);
10354368Sbostic } else
10454368Sbostic while (fgets(buf, sizeof(buf), stdin) != NULL)
10554368Sbostic syslog(pri, "%s", buf);
10624870Seric exit(0);
10724870Seric }
10824870Seric
10924870Seric /*
11024870Seric * Decode a symbolic name to a numeric value
11124870Seric */
11254368Sbostic int
pencode(s)11324870Seric pencode(s)
11424870Seric register char *s;
11524870Seric {
11634446Sbostic char *save;
11734446Sbostic int fac, lev;
11824870Seric
11934446Sbostic for (save = s; *s && *s != '.'; ++s);
12034446Sbostic if (*s) {
12134446Sbostic *s = '\0';
12239527Sbostic fac = decode(save, facilitynames);
12346834Sbostic if (fac < 0) {
12446834Sbostic (void)fprintf(stderr,
12546834Sbostic "logger: unknown facility name: %s.\n", save);
12646834Sbostic exit(1);
12746834Sbostic }
12834446Sbostic *s++ = '.';
12934446Sbostic }
13034446Sbostic else {
13124870Seric fac = 0;
13234446Sbostic s = save;
13334446Sbostic }
13439527Sbostic lev = decode(s, prioritynames);
13546834Sbostic if (lev < 0) {
13646834Sbostic (void)fprintf(stderr,
13746834Sbostic "logger: unknown priority name: %s.\n", save);
13846834Sbostic exit(1);
13946834Sbostic }
14024870Seric return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK));
14124870Seric }
14224870Seric
14354368Sbostic int
decode(name,codetab)14424870Seric decode(name, codetab)
14524870Seric char *name;
14639527Sbostic CODE *codetab;
14724870Seric {
14839527Sbostic register CODE *c;
14924870Seric
15024870Seric if (isdigit(*name))
15124870Seric return (atoi(name));
15224870Seric
15324870Seric for (c = codetab; c->c_name; c++)
15434446Sbostic if (!strcasecmp(name, c->c_name))
15524870Seric return (c->c_val);
15624870Seric
15724870Seric return (-1);
15824870Seric }
15924870Seric
16054368Sbostic void
usage()16134446Sbostic usage()
16234446Sbostic {
16346834Sbostic (void)fprintf(stderr,
16454368Sbostic "logger: [-is] [-f file] [-p pri] [-t tag] [ message ... ]\n");
16534446Sbostic exit(1);
16634446Sbostic }
167