xref: /titanic_50/usr/src/cmd/logadm/kw.c (revision 636deb6630259136adfafabed91fd84dfcaae63c)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
23*636deb66Sgm149974  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  *
267c478bd9Sstevel@tonic-gate  * logadm/kw.c -- manage keywords table
277c478bd9Sstevel@tonic-gate  *
287c478bd9Sstevel@tonic-gate  * this module expands things like $file.$n in "templates".
297c478bd9Sstevel@tonic-gate  * calling kw_init() sets the current "filename" used for
307c478bd9Sstevel@tonic-gate  * $file, $dirname, and $basename.
317c478bd9Sstevel@tonic-gate  *
327c478bd9Sstevel@tonic-gate  * any time-based expansions, like $secs, or all the strftime()
337c478bd9Sstevel@tonic-gate  * percent sequences, are based on the exact same point in time.
347c478bd9Sstevel@tonic-gate  * so calling kw_expand() on something like "file-%T" will return
357c478bd9Sstevel@tonic-gate  * the same thing when called multiple times during the same logadm run.
367c478bd9Sstevel@tonic-gate  */
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate #include <stdio.h>
417c478bd9Sstevel@tonic-gate #include <libintl.h>
427c478bd9Sstevel@tonic-gate #include <stdlib.h>
437c478bd9Sstevel@tonic-gate #include <sys/types.h>
447c478bd9Sstevel@tonic-gate #include <sys/stat.h>
457c478bd9Sstevel@tonic-gate #include <sys/utsname.h>
467c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h>
477c478bd9Sstevel@tonic-gate #include <strings.h>
487c478bd9Sstevel@tonic-gate #include <time.h>
497c478bd9Sstevel@tonic-gate #include <ctype.h>
507c478bd9Sstevel@tonic-gate #include "err.h"
517c478bd9Sstevel@tonic-gate #include "lut.h"
527c478bd9Sstevel@tonic-gate #include "fn.h"
537c478bd9Sstevel@tonic-gate #include "kw.h"
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate /* forward declarations for functions used internally by this module */
567c478bd9Sstevel@tonic-gate static void kw_printer(const char *lhs, void *rhs, void *arg);
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate /*
597c478bd9Sstevel@tonic-gate  * absurdly long length to hold sprintf of a %d,
607c478bd9Sstevel@tonic-gate  * or strftime() expansion of a *single* percent sequence
617c478bd9Sstevel@tonic-gate  */
627c478bd9Sstevel@tonic-gate #define	MAXDIGITS 100
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate static struct lut *Keywords;	/* lookup table for keywords */
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate extern time_t Now;		/* time used for keyword expansions */
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate /*
697c478bd9Sstevel@tonic-gate  * kw_init -- initialize keywords based on given filename
707c478bd9Sstevel@tonic-gate  */
717c478bd9Sstevel@tonic-gate void
727c478bd9Sstevel@tonic-gate kw_init(struct fn *fnp, struct fn *nfnp)
737c478bd9Sstevel@tonic-gate {
747c478bd9Sstevel@tonic-gate 	static char *fullpath;
757c478bd9Sstevel@tonic-gate 	static char *nfullpath;
767c478bd9Sstevel@tonic-gate 	static char *splitpath;
777c478bd9Sstevel@tonic-gate 	static char secs[MAXDIGITS];
787c478bd9Sstevel@tonic-gate 	static struct utsname un;
797c478bd9Sstevel@tonic-gate 	static char platform[SYS_NMLN];
807c478bd9Sstevel@tonic-gate 	static char isa[SYS_NMLN];
817c478bd9Sstevel@tonic-gate 	static char domain[256];
827c478bd9Sstevel@tonic-gate 	static char *home;
837c478bd9Sstevel@tonic-gate 	static char *user;
847c478bd9Sstevel@tonic-gate 	static char *logname;
857c478bd9Sstevel@tonic-gate 	static int initialized;
867c478bd9Sstevel@tonic-gate 	char *ptr;
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate 	/* make a copy of the string for $file */
897c478bd9Sstevel@tonic-gate 	if (fullpath)
907c478bd9Sstevel@tonic-gate 		FREE(fullpath);
917c478bd9Sstevel@tonic-gate 	fullpath = STRDUP(fn_s(fnp));
927c478bd9Sstevel@tonic-gate 	Keywords = lut_add(Keywords, "file", fullpath);
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate 	/* make a copy of the string for $nfile */
957c478bd9Sstevel@tonic-gate 	if (nfullpath)
967c478bd9Sstevel@tonic-gate 		FREE(nfullpath);
977c478bd9Sstevel@tonic-gate 	if (nfnp == NULL) {
987c478bd9Sstevel@tonic-gate 		nfullpath = NULL;
997c478bd9Sstevel@tonic-gate 		Keywords = lut_add(Keywords, "nfile", "");
1007c478bd9Sstevel@tonic-gate 	} else {
1017c478bd9Sstevel@tonic-gate 		nfullpath = STRDUP(fn_s(nfnp));
1027c478bd9Sstevel@tonic-gate 		Keywords = lut_add(Keywords, "nfile", nfullpath);
1037c478bd9Sstevel@tonic-gate 	}
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 	/* make a copy of the string for $dirname/$basename */
1067c478bd9Sstevel@tonic-gate 	if (splitpath)
1077c478bd9Sstevel@tonic-gate 		FREE(splitpath);
1087c478bd9Sstevel@tonic-gate 	splitpath = STRDUP(fn_s(fnp));
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 	if ((ptr = strrchr(splitpath, '/')) == NULL) {
1117c478bd9Sstevel@tonic-gate 		Keywords = lut_add(Keywords, "basename", splitpath);
1127c478bd9Sstevel@tonic-gate 		Keywords = lut_add(Keywords, "dirname", ".");
1137c478bd9Sstevel@tonic-gate 	} else {
1147c478bd9Sstevel@tonic-gate 		*ptr++ = '\0';
1157c478bd9Sstevel@tonic-gate 		Keywords = lut_add(Keywords, "basename", ptr);
1167c478bd9Sstevel@tonic-gate 		Keywords = lut_add(Keywords, "dirname", splitpath);
1177c478bd9Sstevel@tonic-gate 	}
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate 	if (initialized)
1207c478bd9Sstevel@tonic-gate 		return;		/* rest of the keywords don't change */
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate 	(void) snprintf(secs, MAXDIGITS, "%d", (int)Now);
1237c478bd9Sstevel@tonic-gate 	Keywords = lut_add(Keywords, "secs", secs);
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate 	if (uname(&un) < 0)
1267c478bd9Sstevel@tonic-gate 		err(EF_SYS, "uname");
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 	Keywords = lut_add(Keywords, "nodename", un.nodename);
1297c478bd9Sstevel@tonic-gate 	Keywords = lut_add(Keywords, "release", un.release);
1307c478bd9Sstevel@tonic-gate 	Keywords = lut_add(Keywords, "machine", un.machine);
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 	if (sysinfo(SI_ARCHITECTURE, isa, sizeof (isa)) == -1)
1337c478bd9Sstevel@tonic-gate 		err(EF_WARN|EF_SYS, "sysinfo(SI_ARCHITECTURE) failed.");
1347c478bd9Sstevel@tonic-gate 	else
1357c478bd9Sstevel@tonic-gate 		Keywords = lut_add(Keywords, "isa", isa);
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 	if (sysinfo(SI_PLATFORM, platform, sizeof (platform)) == -1)
1387c478bd9Sstevel@tonic-gate 		err(EF_WARN|EF_SYS, "sysinfo(SI_PLATFORM) failed.");
1397c478bd9Sstevel@tonic-gate 	else
1407c478bd9Sstevel@tonic-gate 		Keywords = lut_add(Keywords, "platform", platform);
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate 	if (sysinfo(SI_SRPC_DOMAIN, domain, sizeof (domain)) == -1)
1437c478bd9Sstevel@tonic-gate 		err(EF_WARN|EF_SYS, "sysinfo(SI_SRPC_DOMAIN) failed.");
1447c478bd9Sstevel@tonic-gate 	else
1457c478bd9Sstevel@tonic-gate 		Keywords = lut_add(Keywords, "domain", domain);
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 	if ((home = getenv("HOME")) != NULL)
1487c478bd9Sstevel@tonic-gate 		Keywords = lut_add(Keywords, "home", STRDUP(home));
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 	if ((user = getenv("USER")) != NULL)
1517c478bd9Sstevel@tonic-gate 		Keywords = lut_add(Keywords, "user", STRDUP(user));
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 	if ((logname = getenv("LOGNAME")) != NULL)
1547c478bd9Sstevel@tonic-gate 		Keywords = lut_add(Keywords, "logname", STRDUP(logname));
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 	initialized = 1;
1577c478bd9Sstevel@tonic-gate }
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate /* helper function for kw_print() */
1607c478bd9Sstevel@tonic-gate static void
1617c478bd9Sstevel@tonic-gate kw_printer(const char *lhs, void *rhs, void *arg)
1627c478bd9Sstevel@tonic-gate {
1637c478bd9Sstevel@tonic-gate 	FILE *stream = (FILE *)arg;
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 	(void) fprintf(stream, "%20.20s %s\n", lhs, (char *)rhs);
1667c478bd9Sstevel@tonic-gate }
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate /*
1697c478bd9Sstevel@tonic-gate  * kw_print -- spew the entire keywords table to stream
1707c478bd9Sstevel@tonic-gate  *
1717c478bd9Sstevel@tonic-gate  * this routine is used to dump the keywords table for debugging.
1727c478bd9Sstevel@tonic-gate  */
1737c478bd9Sstevel@tonic-gate void
1747c478bd9Sstevel@tonic-gate kw_print(FILE *stream)
1757c478bd9Sstevel@tonic-gate {
1767c478bd9Sstevel@tonic-gate 	lut_walk(Keywords, kw_printer, stream);
1777c478bd9Sstevel@tonic-gate }
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate /*
1807c478bd9Sstevel@tonic-gate  * kw_expand -- expand src into dst with given n value for $n (or $N)
1817c478bd9Sstevel@tonic-gate  *
1827c478bd9Sstevel@tonic-gate  * n == -1 means expand src into a reglob
1837c478bd9Sstevel@tonic-gate  * if gz is true, include ".gz" extension
1847c478bd9Sstevel@tonic-gate  *
1857c478bd9Sstevel@tonic-gate  * returns true if template contains $n or $N (implying rotation of files)
1867c478bd9Sstevel@tonic-gate  */
1877c478bd9Sstevel@tonic-gate boolean_t
1887c478bd9Sstevel@tonic-gate kw_expand(struct fn *src, struct fn *dst, int n, boolean_t gz)
1897c478bd9Sstevel@tonic-gate {
1907c478bd9Sstevel@tonic-gate 	int c;
1917c478bd9Sstevel@tonic-gate 	char buf[MAXDIGITS];
1927c478bd9Sstevel@tonic-gate 	boolean_t hasn = B_FALSE;
1937c478bd9Sstevel@tonic-gate 	struct fn *kw = fn_new(NULL);
1947c478bd9Sstevel@tonic-gate 	char *ptr;
195*636deb66Sgm149974 	struct tm *gmt_tm = localtime(&Now);
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 	while ((c = fn_getc(src)) != '\0')
1987c478bd9Sstevel@tonic-gate 		switch (c) {
1997c478bd9Sstevel@tonic-gate 		case '.':
2007c478bd9Sstevel@tonic-gate 		case '(':
2017c478bd9Sstevel@tonic-gate 		case ')':
2027c478bd9Sstevel@tonic-gate 		case '^':
2037c478bd9Sstevel@tonic-gate 		case '+':
2047c478bd9Sstevel@tonic-gate 		case '{':
2057c478bd9Sstevel@tonic-gate 		case '}':
2067c478bd9Sstevel@tonic-gate 			/* when building an re, escape with a backslash */
2077c478bd9Sstevel@tonic-gate 			if (n < 0)
2087c478bd9Sstevel@tonic-gate 				fn_putc(dst, '\\');
2097c478bd9Sstevel@tonic-gate 			fn_putc(dst, c);
2107c478bd9Sstevel@tonic-gate 			break;
2117c478bd9Sstevel@tonic-gate 		case '?':
2127c478bd9Sstevel@tonic-gate 			/* when building an re, change '?' to a single dot */
2137c478bd9Sstevel@tonic-gate 			if (n < 0)
2147c478bd9Sstevel@tonic-gate 				fn_putc(dst, '.');
2157c478bd9Sstevel@tonic-gate 			break;
2167c478bd9Sstevel@tonic-gate 		case '*':
2177c478bd9Sstevel@tonic-gate 			/* when building an re, change '*' to ".*" */
2187c478bd9Sstevel@tonic-gate 			if (n < 0)
2197c478bd9Sstevel@tonic-gate 				fn_putc(dst, '.');
2207c478bd9Sstevel@tonic-gate 			fn_putc(dst, '*');
2217c478bd9Sstevel@tonic-gate 			break;
2227c478bd9Sstevel@tonic-gate 		case '$':
2237c478bd9Sstevel@tonic-gate 			/* '$' marks the start of a keyword */
2247c478bd9Sstevel@tonic-gate 			switch (c = fn_getc(src)) {
2257c478bd9Sstevel@tonic-gate 			case '$':
2267c478bd9Sstevel@tonic-gate 				/* double '$' stands for a single '$' */
2277c478bd9Sstevel@tonic-gate 				if (n < 0)
2287c478bd9Sstevel@tonic-gate 					fn_putc(dst, '\\');
2297c478bd9Sstevel@tonic-gate 				fn_putc(dst, '$');
2307c478bd9Sstevel@tonic-gate 				break;
2317c478bd9Sstevel@tonic-gate 			case '#':
2327c478bd9Sstevel@tonic-gate 				/*
2337c478bd9Sstevel@tonic-gate 				 * $# expands to nothing, but forces an end
2347c478bd9Sstevel@tonic-gate 				 * of keyword, allow juxtaposition of a
2357c478bd9Sstevel@tonic-gate 				 * keyword with lower-case characters
2367c478bd9Sstevel@tonic-gate 				 */
2377c478bd9Sstevel@tonic-gate 				break;
2387c478bd9Sstevel@tonic-gate 			case 'n':
2397c478bd9Sstevel@tonic-gate 			case 'N':
2407c478bd9Sstevel@tonic-gate 				if (c == 'N' || !islower(fn_peekc(src))) {
2417c478bd9Sstevel@tonic-gate 					/*
2427c478bd9Sstevel@tonic-gate 					 * we've found $n or $N, if we're
2437c478bd9Sstevel@tonic-gate 					 * building an re, build one that
2447c478bd9Sstevel@tonic-gate 					 * matches a number, otherwise
2457c478bd9Sstevel@tonic-gate 					 * expand the keyword to the n
2467c478bd9Sstevel@tonic-gate 					 * passed in to this function
2477c478bd9Sstevel@tonic-gate 					 */
2487c478bd9Sstevel@tonic-gate 					hasn = B_TRUE;
2497c478bd9Sstevel@tonic-gate 					if (n < 0)
2507c478bd9Sstevel@tonic-gate 						fn_puts(dst, "([0-9]+)$0");
2517c478bd9Sstevel@tonic-gate 					else {
2527c478bd9Sstevel@tonic-gate 						(void) snprintf(buf,
2537c478bd9Sstevel@tonic-gate 						    MAXDIGITS, "%d",
2547c478bd9Sstevel@tonic-gate 						    (c == 'n') ? n : n + 1);
2557c478bd9Sstevel@tonic-gate 						fn_puts(dst, buf);
2567c478bd9Sstevel@tonic-gate 					}
2577c478bd9Sstevel@tonic-gate 					break;
2587c478bd9Sstevel@tonic-gate 				}
2597c478bd9Sstevel@tonic-gate 				/*FALLTHROUGH*/
2607c478bd9Sstevel@tonic-gate 			default:
2617c478bd9Sstevel@tonic-gate 				/* gather up the keyword name */
2627c478bd9Sstevel@tonic-gate 				fn_renew(kw, NULL);
2637c478bd9Sstevel@tonic-gate 				fn_putc(kw, c);
2647c478bd9Sstevel@tonic-gate 				while (islower(fn_peekc(src)))
2657c478bd9Sstevel@tonic-gate 					fn_putc(kw, fn_getc(src));
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 				/* lookup keyword */
2687c478bd9Sstevel@tonic-gate 				if ((ptr = (char *)lut_lookup(Keywords,
2697c478bd9Sstevel@tonic-gate 				    fn_s(kw))) == NULL) {
2707c478bd9Sstevel@tonic-gate 					/* nope, copy it unexpanded */
2717c478bd9Sstevel@tonic-gate 					if (n < 0)
2727c478bd9Sstevel@tonic-gate 						fn_putc(dst, '\\');
2737c478bd9Sstevel@tonic-gate 					fn_putc(dst, '$');
2747c478bd9Sstevel@tonic-gate 					fn_putfn(dst, kw);
2757c478bd9Sstevel@tonic-gate 				} else
2767c478bd9Sstevel@tonic-gate 					fn_puts(dst, ptr);
2777c478bd9Sstevel@tonic-gate 			}
2787c478bd9Sstevel@tonic-gate 			break;
2797c478bd9Sstevel@tonic-gate 		case '%':
2807c478bd9Sstevel@tonic-gate 			/*
2817c478bd9Sstevel@tonic-gate 			 * % sequence for strftime(), if we're building
2827c478bd9Sstevel@tonic-gate 			 * an re, we take our best guess at the re for
2837c478bd9Sstevel@tonic-gate 			 * this sequence, otherwise we pass it to strftime()
2847c478bd9Sstevel@tonic-gate 			 */
2857c478bd9Sstevel@tonic-gate 			if (n < 0) {
2867c478bd9Sstevel@tonic-gate 				/*
2877c478bd9Sstevel@tonic-gate 				 * the regex for a percent sequence is
2887c478bd9Sstevel@tonic-gate 				 * usually just ".*" unless it is one
2897c478bd9Sstevel@tonic-gate 				 * of the common cases we know about
2907c478bd9Sstevel@tonic-gate 				 * that are numeric.  in those  cases, we
2917c478bd9Sstevel@tonic-gate 				 * tighten up the regex to just match digits.
2927c478bd9Sstevel@tonic-gate 				 *
2937c478bd9Sstevel@tonic-gate 				 * while it is gross that we embed knowledge
2947c478bd9Sstevel@tonic-gate 				 * of strftime() sequences here, they are
2957c478bd9Sstevel@tonic-gate 				 * specified in a standard so aren't
2967c478bd9Sstevel@tonic-gate 				 * expected to change often, and it *really*
2977c478bd9Sstevel@tonic-gate 				 * cuts down on the possibility that we'll
2987c478bd9Sstevel@tonic-gate 				 * expire a file that isn't an old log file.
2997c478bd9Sstevel@tonic-gate 				 */
3007c478bd9Sstevel@tonic-gate 				if ((c = fn_getc(src)) == 'E' || c == 'O') {
3017c478bd9Sstevel@tonic-gate 					c = fn_getc(src);
3027c478bd9Sstevel@tonic-gate 					fn_puts(dst, ".*");
3037c478bd9Sstevel@tonic-gate 				} else
3047c478bd9Sstevel@tonic-gate 					switch (c) {
3057c478bd9Sstevel@tonic-gate 					case 'd':
3067c478bd9Sstevel@tonic-gate 					case 'g':
3077c478bd9Sstevel@tonic-gate 					case 'G':
3087c478bd9Sstevel@tonic-gate 					case 'H':
3097c478bd9Sstevel@tonic-gate 					case 'I':
3107c478bd9Sstevel@tonic-gate 					case 'j':
3117c478bd9Sstevel@tonic-gate 					case 'm':
3127c478bd9Sstevel@tonic-gate 					case 'M':
3137c478bd9Sstevel@tonic-gate 					case 'S':
3147c478bd9Sstevel@tonic-gate 					case 'u':
3157c478bd9Sstevel@tonic-gate 					case 'U':
3167c478bd9Sstevel@tonic-gate 					case 'V':
3177c478bd9Sstevel@tonic-gate 					case 'w':
3187c478bd9Sstevel@tonic-gate 					case 'W':
3197c478bd9Sstevel@tonic-gate 					case 'y':
3207c478bd9Sstevel@tonic-gate 					case 'Y':
3217c478bd9Sstevel@tonic-gate 						/* pure numeric cases */
3227c478bd9Sstevel@tonic-gate 						fn_puts(dst, "[0-9]+");
3237c478bd9Sstevel@tonic-gate 						break;
3247c478bd9Sstevel@tonic-gate 					case 'e':
3257c478bd9Sstevel@tonic-gate 					case 'k':
3267c478bd9Sstevel@tonic-gate 					case 'l':
3277c478bd9Sstevel@tonic-gate 						/* possible space then num */
3287c478bd9Sstevel@tonic-gate 						fn_puts(dst, " *[0-9]+");
3297c478bd9Sstevel@tonic-gate 						break;
3307c478bd9Sstevel@tonic-gate 					case 'D':	/* %m/%d/%y */
3317c478bd9Sstevel@tonic-gate 						/* adds slashes! */
3327c478bd9Sstevel@tonic-gate 						fn_puts(dst,
3337c478bd9Sstevel@tonic-gate 						    "[0-9]+/[0-9]+/[0-9]+");
3347c478bd9Sstevel@tonic-gate 						break;
3357c478bd9Sstevel@tonic-gate 					case 'R':	/* %H:%M */
3367c478bd9Sstevel@tonic-gate 						fn_puts(dst, "[0-9]+:[0-9]+");
3377c478bd9Sstevel@tonic-gate 						break;
3387c478bd9Sstevel@tonic-gate 					case 'T':	/* %H:%M:%S */
3397c478bd9Sstevel@tonic-gate 						fn_puts(dst,
3407c478bd9Sstevel@tonic-gate 						    "[0-9]+:[0-9]+:[0-9]+");
3417c478bd9Sstevel@tonic-gate 						break;
3427c478bd9Sstevel@tonic-gate 					default:
3437c478bd9Sstevel@tonic-gate 						fn_puts(dst, ".*");
3447c478bd9Sstevel@tonic-gate 					}
3457c478bd9Sstevel@tonic-gate 			} else {
3467c478bd9Sstevel@tonic-gate 				char tbuf[4];
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate 				/* copy % sequence to tbuf */
3497c478bd9Sstevel@tonic-gate 				tbuf[0] = '%';
3507c478bd9Sstevel@tonic-gate 				tbuf[1] = fn_getc(src);
3517c478bd9Sstevel@tonic-gate 				if (tbuf[1] == 'E' || tbuf[1] == 'O') {
3527c478bd9Sstevel@tonic-gate 					/* "extended" sequence */
3537c478bd9Sstevel@tonic-gate 					tbuf[2] = fn_getc(src);
3547c478bd9Sstevel@tonic-gate 					tbuf[3] = '\0';
3557c478bd9Sstevel@tonic-gate 				} else
3567c478bd9Sstevel@tonic-gate 					tbuf[2] = '\0';
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate 				if (strftime(buf, MAXDIGITS, tbuf, gmt_tm) == 0)
3597c478bd9Sstevel@tonic-gate 					/* just copy %x */
3607c478bd9Sstevel@tonic-gate 					fn_puts(dst, tbuf);
3617c478bd9Sstevel@tonic-gate 				else
3627c478bd9Sstevel@tonic-gate 					fn_puts(dst, buf);
3637c478bd9Sstevel@tonic-gate 			}
3647c478bd9Sstevel@tonic-gate 			break;
3657c478bd9Sstevel@tonic-gate 		default:
3667c478bd9Sstevel@tonic-gate 			/* nothing special, just copy it */
3677c478bd9Sstevel@tonic-gate 			fn_putc(dst, c);
3687c478bd9Sstevel@tonic-gate 		}
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate 	if (gz) {
3717c478bd9Sstevel@tonic-gate 		if (n < 0)
3727c478bd9Sstevel@tonic-gate 			fn_puts(dst, "(\\.gz){0,1}");
3737c478bd9Sstevel@tonic-gate 		else
3747c478bd9Sstevel@tonic-gate 			fn_puts(dst, ".gz");
3757c478bd9Sstevel@tonic-gate 	}
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate 	fn_free(kw);
3787c478bd9Sstevel@tonic-gate 	return (hasn);
3797c478bd9Sstevel@tonic-gate }
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate #ifdef	TESTMODULE
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate time_t Now;
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate /*
3867c478bd9Sstevel@tonic-gate  * test main for kw module, usage: a.out fname [template...]
3877c478bd9Sstevel@tonic-gate  */
3887c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
3897c478bd9Sstevel@tonic-gate {
3907c478bd9Sstevel@tonic-gate 	int i;
3917c478bd9Sstevel@tonic-gate 	struct fn *src = fn_new(NULL);
3927c478bd9Sstevel@tonic-gate 	struct fn *dst = fn_new(NULL);
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate 	err_init(argv[0]);
3957c478bd9Sstevel@tonic-gate 	setbuf(stdout, NULL);
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate 	Now = time(0);
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate 	if (argc < 2)
4007c478bd9Sstevel@tonic-gate 		err(0, "first arg must be fname");
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate 	kw_init(fn_new(argv[1]), NULL);
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate 	kw_print(stdout);
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate 	for (i = 2; i < argc; i++) {
4077c478bd9Sstevel@tonic-gate 		int n;
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate 		for (n = -1; n < 2; n++) {
4107c478bd9Sstevel@tonic-gate 			fn_renew(src, argv[i]);
4117c478bd9Sstevel@tonic-gate 			fn_renew(dst, NULL);
4127c478bd9Sstevel@tonic-gate 			printf("expand<%s> n %d hasn %d ",
4137c478bd9Sstevel@tonic-gate 			    argv[i], n, kw_expand(src, dst, n, B_FALSE));
4147c478bd9Sstevel@tonic-gate 			printf("result <%s>\n", fn_s(dst));
4157c478bd9Sstevel@tonic-gate 		}
4167c478bd9Sstevel@tonic-gate 	}
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate 	err_done(0);
4197c478bd9Sstevel@tonic-gate }
4207c478bd9Sstevel@tonic-gate 
4217c478bd9Sstevel@tonic-gate #endif	/* TESTMODULE */
422