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