rmdel is already linked
[unix-history] / usr / src / usr.bin / logger / logger.c
CommitLineData
bede97d4
EA
1/*
2 * Copyright (c) 1983 Regents of the University of California.
3 * All rights reserved. The Berkeley software License Agreement
4 * specifies the terms and conditions for redistribution.
5 */
6
7#ifndef lint
8char copyright[] =
9"@(#) Copyright (c) 1983 Regents of the University of California.\n\
10 All rights reserved.\n";
11#endif not lint
12
13#ifndef lint
28743301 14static char sccsid[] = "@(#)logger.c 6.4 (Berkeley) %G%";
bede97d4
EA
15#endif not lint
16
17#include <stdio.h>
18#include <syslog.h>
19#include <ctype.h>
20
21/*
22** LOGGER -- read and log utility
23**
24** This routine reads from an input and arranges to write the
25** result on the system log, along with a useful tag.
26*/
27
28main(argc, argv)
29 int argc;
30 char **argv;
31{
32 char buf[200];
33 char *tag;
34 register char *p;
35 int pri = LOG_NOTICE;
36 int logflags = 0;
37 extern char *getlogin();
38
39 /* initialize */
40 tag = getlogin();
41
42 /* crack arguments */
43 while (--argc > 0)
44 {
45 p = *++argv;
46 if (*p != '-')
47 break;
48
49 switch (*++p)
50 {
51 case '\0': /* dummy */
52 /* this can be used to give null parameters */
53 break;
54
55 case 't': /* tag */
56 if (argc > 1 && argv[1][0] != '-')
57 {
58 argc--;
59 tag = *++argv;
60 }
61 else
62 tag = NULL;
63 break;
64
65 case 'p': /* priority */
66 if (argc > 1 && argv[1][0] != '-')
67 {
68 argc--;
69 pri = pencode(*++argv);
70 }
71 break;
72
73 case 'i': /* log process id also */
74 logflags |= LOG_PID;
75 break;
76
77 case 'f': /* file to log */
78 if (argc > 1 && argv[1][0] != '-')
79 {
80 argc--;
81 if (freopen(*++argv, "r", stdin) == NULL)
82 {
83 fprintf("logger: ");
84 perror(*argv);
85 exit(1);
86 }
87 }
88 break;
89
90 default:
91 fprintf(stderr, "logger: unknown flag -%s\n", p);
92 break;
93 }
94 }
95
96 /* setup for logging */
97 openlog(tag, logflags, 0);
98 (void) fclose(stdout);
99
100 /* log input line if appropriate */
101 if (argc > 0)
102 {
103 char buf[120];
104
105 buf[0] = '\0';
106 while (argc-- > 0)
107 {
108 strcat(buf, " ");
109 strcat(buf, *argv++);
110 }
111 syslog(pri, buf + 1);
112 exit(0);
113 }
114
115 /* main loop */
116 while (fgets(buf, sizeof buf, stdin) != NULL)
117 syslog(pri, buf);
118
119 exit(0);
120}
121
122
123struct code {
124 char *c_name;
125 int c_val;
126};
127
128struct code PriNames[] = {
129 "panic", LOG_EMERG,
130 "emerg", LOG_EMERG,
131 "alert", LOG_ALERT,
132 "crit", LOG_CRIT,
133 "err", LOG_ERR,
134 "error", LOG_ERR,
135 "warn", LOG_WARNING,
136 "warning", LOG_WARNING,
137 "notice", LOG_NOTICE,
138 "info", LOG_INFO,
139 "debug", LOG_DEBUG,
140 NULL, -1
141};
142
143struct code FacNames[] = {
144 "kern", LOG_KERN,
145 "user", LOG_USER,
146 "mail", LOG_MAIL,
623d5539 147 "daemon", LOG_DAEMON,
bede97d4
EA
148 "auth", LOG_AUTH,
149 "security", LOG_AUTH,
3f980dd2
EA
150 "syslog", LOG_SYSLOG,
151 "lpr", LOG_LPR,
152 "news", LOG_NEWS,
bede97d4
EA
153 "local0", LOG_LOCAL0,
154 "local1", LOG_LOCAL1,
155 "local2", LOG_LOCAL2,
156 "local3", LOG_LOCAL3,
157 "local4", LOG_LOCAL4,
158 "local5", LOG_LOCAL5,
159 "local6", LOG_LOCAL6,
160 "local7", LOG_LOCAL7,
161 NULL, -1
162};
163
164
165/*
166 * Decode a symbolic name to a numeric value
167 */
168
169pencode(s)
170 register char *s;
171{
172 register char *p;
173 int lev;
174 int fac;
175 char buf[100];
176
177 for (p = buf; *s && *s != '.'; )
178 *p++ = *s++;
179 *p = '\0';
180 if (*s++) {
181 fac = decode(buf, FacNames);
182 if (fac < 0)
183 bailout("unknown facility name: ", buf);
184 for (p = buf; *p++ = *s++; )
185 continue;
186 } else
187 fac = 0;
188 lev = decode(buf, PriNames);
189 if (lev < 0)
190 bailout("unknown priority name: ", buf);
191
192 return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK));
193}
194
195
196decode(name, codetab)
197 char *name;
198 struct code *codetab;
199{
200 register struct code *c;
201 register char *p;
202 char buf[40];
203
204 if (isdigit(*name))
205 return (atoi(name));
206
207 (void) strcpy(buf, name);
208 for (p = buf; *p; p++)
209 if (isupper(*p))
210 *p = tolower(*p);
211 for (c = codetab; c->c_name; c++)
212 if (!strcmp(buf, c->c_name))
213 return (c->c_val);
214
215 return (-1);
216}
217
218bailout(a, b)
219 char *a, *b;
220{
221 fprintf(stderr, "logger: %s%s\n", a, b);
222 exit(1);
223}