8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / cmd / logadm / kw.c
blobdcd102e947a1b1265439e91bc47a1df2b2eaba73
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
25 * logadm/kw.c -- manage keywords table
27 * this module expands things like $file.$n in "templates".
28 * calling kw_init() sets the current "filename" used for
29 * $file, $dirname, and $basename.
31 * any time-based expansions, like $secs, or all the strftime()
32 * percent sequences, are based on the exact same point in time.
33 * so calling kw_expand() on something like "file-%T" will return
34 * the same thing when called multiple times during the same logadm run.
37 #pragma ident "%Z%%M% %I% %E% SMI"
39 #include <stdio.h>
40 #include <libintl.h>
41 #include <stdlib.h>
42 #include <sys/types.h>
43 #include <sys/stat.h>
44 #include <sys/utsname.h>
45 #include <sys/systeminfo.h>
46 #include <strings.h>
47 #include <time.h>
48 #include <ctype.h>
49 #include <zone.h>
50 #include "err.h"
51 #include "lut.h"
52 #include "fn.h"
53 #include "kw.h"
55 /* forward declarations for functions used internally by this module */
56 static void kw_printer(const char *lhs, void *rhs, void *arg);
59 * absurdly long length to hold sprintf of a %d,
60 * or strftime() expansion of a *single* percent sequence
62 #define MAXDIGITS 100
64 static struct lut *Keywords; /* lookup table for keywords */
66 extern time_t Now; /* time used for keyword expansions */
69 * kw_init -- initialize keywords based on given filename
71 void
72 kw_init(struct fn *fnp, struct fn *nfnp)
74 static char *fullpath;
75 static char *nfullpath;
76 static char *splitpath;
77 static char secs[MAXDIGITS];
78 static struct utsname un;
79 static char platform[SYS_NMLN];
80 static char isa[SYS_NMLN];
81 static char domain[256];
82 static char *home;
83 static char *user;
84 static char *logname;
85 static char zonename[ZONENAME_MAX];
86 static zoneid_t zoneid;
87 static int initialized;
88 char *ptr;
90 /* make a copy of the string for $file */
91 if (fullpath)
92 FREE(fullpath);
93 fullpath = STRDUP(fn_s(fnp));
94 Keywords = lut_add(Keywords, "file", fullpath);
96 /* make a copy of the string for $nfile */
97 if (nfullpath)
98 FREE(nfullpath);
99 if (nfnp == NULL) {
100 nfullpath = NULL;
101 Keywords = lut_add(Keywords, "nfile", "");
102 } else {
103 nfullpath = STRDUP(fn_s(nfnp));
104 Keywords = lut_add(Keywords, "nfile", nfullpath);
107 /* make a copy of the string for $dirname/$basename */
108 if (splitpath)
109 FREE(splitpath);
110 splitpath = STRDUP(fn_s(fnp));
112 if ((ptr = strrchr(splitpath, '/')) == NULL) {
113 Keywords = lut_add(Keywords, "basename", splitpath);
114 Keywords = lut_add(Keywords, "dirname", ".");
115 } else {
116 *ptr++ = '\0';
117 Keywords = lut_add(Keywords, "basename", ptr);
118 Keywords = lut_add(Keywords, "dirname", splitpath);
121 if (initialized)
122 return; /* rest of the keywords don't change */
124 (void) snprintf(secs, MAXDIGITS, "%d", (int)Now);
125 Keywords = lut_add(Keywords, "secs", secs);
127 if (uname(&un) < 0)
128 err(EF_SYS, "uname");
130 Keywords = lut_add(Keywords, "nodename", un.nodename);
131 Keywords = lut_add(Keywords, "release", un.release);
132 Keywords = lut_add(Keywords, "machine", un.machine);
134 if (sysinfo(SI_ARCHITECTURE, isa, sizeof (isa)) == -1)
135 err(EF_WARN|EF_SYS, "sysinfo(SI_ARCHITECTURE) failed.");
136 else
137 Keywords = lut_add(Keywords, "isa", isa);
139 if (sysinfo(SI_PLATFORM, platform, sizeof (platform)) == -1)
140 err(EF_WARN|EF_SYS, "sysinfo(SI_PLATFORM) failed.");
141 else
142 Keywords = lut_add(Keywords, "platform", platform);
144 if (sysinfo(SI_SRPC_DOMAIN, domain, sizeof (domain)) == -1)
145 err(EF_WARN|EF_SYS, "sysinfo(SI_SRPC_DOMAIN) failed.");
146 else
147 Keywords = lut_add(Keywords, "domain", domain);
149 if ((home = getenv("HOME")) != NULL)
150 Keywords = lut_add(Keywords, "home", STRDUP(home));
152 if ((user = getenv("USER")) != NULL)
153 Keywords = lut_add(Keywords, "user", STRDUP(user));
155 if ((logname = getenv("LOGNAME")) != NULL)
156 Keywords = lut_add(Keywords, "logname", STRDUP(logname));
158 zoneid = getzoneid();
159 if ((getzonenamebyid(zoneid, zonename, sizeof (zonename))) == -1)
160 err(EF_WARN|EF_SYS, "getzonenamebyid() failed.");
161 else
162 Keywords = lut_add(Keywords, "zonename", STRDUP(zonename));
164 initialized = 1;
167 /* helper function for kw_print() */
168 static void
169 kw_printer(const char *lhs, void *rhs, void *arg)
171 FILE *stream = (FILE *)arg;
173 (void) fprintf(stream, "%20.20s %s\n", lhs, (char *)rhs);
177 * kw_print -- spew the entire keywords table to stream
179 * this routine is used to dump the keywords table for debugging.
181 void
182 kw_print(FILE *stream)
184 lut_walk(Keywords, kw_printer, stream);
188 * kw_expand -- expand src into dst with given n value for $n (or $N)
190 * n == -1 means expand src into a reglob
191 * if gz is true, include ".gz" extension
193 * returns true if template contains $n or $N (implying rotation of files)
195 boolean_t
196 kw_expand(struct fn *src, struct fn *dst, int n, boolean_t gz)
198 int c;
199 char buf[MAXDIGITS];
200 boolean_t hasn = B_FALSE;
201 struct fn *kw = fn_new(NULL);
202 char *ptr;
203 struct tm *gmt_tm = localtime(&Now);
205 while ((c = fn_getc(src)) != '\0')
206 switch (c) {
207 case '.':
208 case '(':
209 case ')':
210 case '^':
211 case '+':
212 case '{':
213 case '}':
214 /* when building an re, escape with a backslash */
215 if (n < 0)
216 fn_putc(dst, '\\');
217 fn_putc(dst, c);
218 break;
219 case '?':
220 /* when building an re, change '?' to a single dot */
221 if (n < 0)
222 fn_putc(dst, '.');
223 break;
224 case '*':
225 /* when building an re, change '*' to ".*" */
226 if (n < 0)
227 fn_putc(dst, '.');
228 fn_putc(dst, '*');
229 break;
230 case '$':
231 /* '$' marks the start of a keyword */
232 switch (c = fn_getc(src)) {
233 case '$':
234 /* double '$' stands for a single '$' */
235 if (n < 0)
236 fn_putc(dst, '\\');
237 fn_putc(dst, '$');
238 break;
239 case '#':
241 * $# expands to nothing, but forces an end
242 * of keyword, allow juxtaposition of a
243 * keyword with lower-case characters
245 break;
246 case 'n':
247 case 'N':
248 if (c == 'N' || !islower(fn_peekc(src))) {
250 * we've found $n or $N, if we're
251 * building an re, build one that
252 * matches a number, otherwise
253 * expand the keyword to the n
254 * passed in to this function
256 hasn = B_TRUE;
257 if (n < 0)
258 fn_puts(dst, "([0-9]+)$0");
259 else {
260 (void) snprintf(buf,
261 MAXDIGITS, "%d",
262 (c == 'n') ? n : n + 1);
263 fn_puts(dst, buf);
265 break;
267 /*FALLTHROUGH*/
268 default:
269 /* gather up the keyword name */
270 fn_renew(kw, NULL);
271 fn_putc(kw, c);
272 while (islower(fn_peekc(src)))
273 fn_putc(kw, fn_getc(src));
275 /* lookup keyword */
276 if ((ptr = (char *)lut_lookup(Keywords,
277 fn_s(kw))) == NULL) {
278 /* nope, copy it unexpanded */
279 if (n < 0)
280 fn_putc(dst, '\\');
281 fn_putc(dst, '$');
282 fn_putfn(dst, kw);
283 } else
284 fn_puts(dst, ptr);
286 break;
287 case '%':
289 * % sequence for strftime(), if we're building
290 * an re, we take our best guess at the re for
291 * this sequence, otherwise we pass it to strftime()
293 if (n < 0) {
295 * the regex for a percent sequence is
296 * usually just ".*" unless it is one
297 * of the common cases we know about
298 * that are numeric. in those cases, we
299 * tighten up the regex to just match digits.
301 * while it is gross that we embed knowledge
302 * of strftime() sequences here, they are
303 * specified in a standard so aren't
304 * expected to change often, and it *really*
305 * cuts down on the possibility that we'll
306 * expire a file that isn't an old log file.
308 if ((c = fn_getc(src)) == 'E' || c == 'O') {
309 c = fn_getc(src);
310 fn_puts(dst, ".*");
311 } else
312 switch (c) {
313 case 'd':
314 case 'g':
315 case 'G':
316 case 'H':
317 case 'I':
318 case 'j':
319 case 'm':
320 case 'M':
321 case 'S':
322 case 'u':
323 case 'U':
324 case 'V':
325 case 'w':
326 case 'W':
327 case 'y':
328 case 'Y':
329 /* pure numeric cases */
330 fn_puts(dst, "[0-9]+");
331 break;
332 case 'e':
333 case 'k':
334 case 'l':
335 /* possible space then num */
336 fn_puts(dst, " *[0-9]+");
337 break;
338 case 'D': /* %m/%d/%y */
339 /* adds slashes! */
340 fn_puts(dst,
341 "[0-9]+/[0-9]+/[0-9]+");
342 break;
343 case 'R': /* %H:%M */
344 fn_puts(dst, "[0-9]+:[0-9]+");
345 break;
346 case 'T': /* %H:%M:%S */
347 fn_puts(dst,
348 "[0-9]+:[0-9]+:[0-9]+");
349 break;
350 default:
351 fn_puts(dst, ".*");
353 } else {
354 char tbuf[4];
356 /* copy % sequence to tbuf */
357 tbuf[0] = '%';
358 tbuf[1] = fn_getc(src);
359 if (tbuf[1] == 'E' || tbuf[1] == 'O') {
360 /* "extended" sequence */
361 tbuf[2] = fn_getc(src);
362 tbuf[3] = '\0';
363 } else
364 tbuf[2] = '\0';
366 if (strftime(buf, MAXDIGITS, tbuf, gmt_tm) == 0)
367 /* just copy %x */
368 fn_puts(dst, tbuf);
369 else
370 fn_puts(dst, buf);
372 break;
373 default:
374 /* nothing special, just copy it */
375 fn_putc(dst, c);
378 if (gz) {
379 if (n < 0)
380 fn_puts(dst, "(\\.gz){0,1}");
381 else
382 fn_puts(dst, ".gz");
385 fn_free(kw);
386 return (hasn);
389 #ifdef TESTMODULE
391 time_t Now;
394 * test main for kw module, usage: a.out fname [template...]
397 main(int argc, char *argv[])
399 int i;
400 struct fn *src = fn_new(NULL);
401 struct fn *dst = fn_new(NULL);
403 err_init(argv[0]);
404 setbuf(stdout, NULL);
406 Now = time(0);
408 if (argc < 2)
409 err(0, "first arg must be fname");
411 kw_init(fn_new(argv[1]), NULL);
413 kw_print(stdout);
415 for (i = 2; i < argc; i++) {
416 int n;
418 for (n = -1; n < 2; n++) {
419 fn_renew(src, argv[i]);
420 fn_renew(dst, NULL);
421 printf("expand<%s> n %d hasn %d ",
422 argv[i], n, kw_expand(src, dst, n, B_FALSE));
423 printf("result <%s>\n", fn_s(dst));
427 err_done(0);
428 /* NOTREACHED */
429 return (0);
432 #endif /* TESTMODULE */