xref: /illumos-gate/usr/src/cmd/logadm/kw.c (revision 2a8bcb4e)
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 #include <stdio.h>
387c478bd9Sstevel@tonic-gate #include <libintl.h>
397c478bd9Sstevel@tonic-gate #include <stdlib.h>
407c478bd9Sstevel@tonic-gate #include <sys/types.h>
417c478bd9Sstevel@tonic-gate #include <sys/stat.h>
427c478bd9Sstevel@tonic-gate #include <sys/utsname.h>
437c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h>
447c478bd9Sstevel@tonic-gate #include <strings.h>
457c478bd9Sstevel@tonic-gate #include <time.h>
467c478bd9Sstevel@tonic-gate #include <ctype.h>
473010f05bSdp #include <zone.h>
487c478bd9Sstevel@tonic-gate #include "err.h"
497c478bd9Sstevel@tonic-gate #include "lut.h"
507c478bd9Sstevel@tonic-gate #include "fn.h"
517c478bd9Sstevel@tonic-gate #include "kw.h"
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate /* forward declarations for functions used internally by this module */
547c478bd9Sstevel@tonic-gate static void kw_printer(const char *lhs, void *rhs, void *arg);
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate /*
577c478bd9Sstevel@tonic-gate  * absurdly long length to hold sprintf of a %d,
587c478bd9Sstevel@tonic-gate  * or strftime() expansion of a *single* percent sequence
597c478bd9Sstevel@tonic-gate  */
607c478bd9Sstevel@tonic-gate #define	MAXDIGITS 100
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate static struct lut *Keywords;	/* lookup table for keywords */
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate extern time_t Now;		/* time used for keyword expansions */
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate /*
677c478bd9Sstevel@tonic-gate  * kw_init -- initialize keywords based on given filename
687c478bd9Sstevel@tonic-gate  */
697c478bd9Sstevel@tonic-gate void
kw_init(struct fn * fnp,struct fn * nfnp)707c478bd9Sstevel@tonic-gate kw_init(struct fn *fnp, struct fn *nfnp)
717c478bd9Sstevel@tonic-gate {
727c478bd9Sstevel@tonic-gate 	static char *fullpath;
737c478bd9Sstevel@tonic-gate 	static char *nfullpath;
747c478bd9Sstevel@tonic-gate 	static char *splitpath;
757c478bd9Sstevel@tonic-gate 	static char secs[MAXDIGITS];
767c478bd9Sstevel@tonic-gate 	static struct utsname un;
777c478bd9Sstevel@tonic-gate 	static char platform[SYS_NMLN];
787c478bd9Sstevel@tonic-gate 	static char isa[SYS_NMLN];
797c478bd9Sstevel@tonic-gate 	static char domain[256];
807c478bd9Sstevel@tonic-gate 	static char *home;
817c478bd9Sstevel@tonic-gate 	static char *user;
827c478bd9Sstevel@tonic-gate 	static char *logname;
833010f05bSdp 	static char zonename[ZONENAME_MAX];
843010f05bSdp 	static zoneid_t zoneid;
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 
1563010f05bSdp 	zoneid = getzoneid();
1573010f05bSdp 	if ((getzonenamebyid(zoneid, zonename, sizeof (zonename))) == -1)
1583010f05bSdp 		err(EF_WARN|EF_SYS, "getzonenamebyid() failed.");
1593010f05bSdp 	else
1603010f05bSdp 		Keywords = lut_add(Keywords, "zonename", STRDUP(zonename));
1613010f05bSdp 
1627c478bd9Sstevel@tonic-gate 	initialized = 1;
1637c478bd9Sstevel@tonic-gate }
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate /* helper function for kw_print() */
1667c478bd9Sstevel@tonic-gate static void
kw_printer(const char * lhs,void * rhs,void * arg)1677c478bd9Sstevel@tonic-gate kw_printer(const char *lhs, void *rhs, void *arg)
1687c478bd9Sstevel@tonic-gate {
1697c478bd9Sstevel@tonic-gate 	FILE *stream = (FILE *)arg;
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 	(void) fprintf(stream, "%20.20s %s\n", lhs, (char *)rhs);
1727c478bd9Sstevel@tonic-gate }
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate /*
1757c478bd9Sstevel@tonic-gate  * kw_print -- spew the entire keywords table to stream
1767c478bd9Sstevel@tonic-gate  *
1777c478bd9Sstevel@tonic-gate  * this routine is used to dump the keywords table for debugging.
1787c478bd9Sstevel@tonic-gate  */
1797c478bd9Sstevel@tonic-gate void
kw_print(FILE * stream)1807c478bd9Sstevel@tonic-gate kw_print(FILE *stream)
1817c478bd9Sstevel@tonic-gate {
1827c478bd9Sstevel@tonic-gate 	lut_walk(Keywords, kw_printer, stream);
1837c478bd9Sstevel@tonic-gate }
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate /*
1867c478bd9Sstevel@tonic-gate  * kw_expand -- expand src into dst with given n value for $n (or $N)
1877c478bd9Sstevel@tonic-gate  *
1887c478bd9Sstevel@tonic-gate  * n == -1 means expand src into a reglob
1897c478bd9Sstevel@tonic-gate  * if gz is true, include ".gz" extension
1907c478bd9Sstevel@tonic-gate  *
1917c478bd9Sstevel@tonic-gate  * returns true if template contains $n or $N (implying rotation of files)
1927c478bd9Sstevel@tonic-gate  */
1937c478bd9Sstevel@tonic-gate boolean_t
kw_expand(struct fn * src,struct fn * dst,int n,boolean_t gz)1947c478bd9Sstevel@tonic-gate kw_expand(struct fn *src, struct fn *dst, int n, boolean_t gz)
1957c478bd9Sstevel@tonic-gate {
1967c478bd9Sstevel@tonic-gate 	int c;
1977c478bd9Sstevel@tonic-gate 	char buf[MAXDIGITS];
1987c478bd9Sstevel@tonic-gate 	boolean_t hasn = B_FALSE;
1997c478bd9Sstevel@tonic-gate 	struct fn *kw = fn_new(NULL);
2007c478bd9Sstevel@tonic-gate 	char *ptr;
201636deb66Sgm 	struct tm *gmt_tm = localtime(&Now);
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 	while ((c = fn_getc(src)) != '\0')
2047c478bd9Sstevel@tonic-gate 		switch (c) {
2057c478bd9Sstevel@tonic-gate 		case '.':
2067c478bd9Sstevel@tonic-gate 		case '(':
2077c478bd9Sstevel@tonic-gate 		case ')':
2087c478bd9Sstevel@tonic-gate 		case '^':
2097c478bd9Sstevel@tonic-gate 		case '+':
2107c478bd9Sstevel@tonic-gate 		case '{':
2117c478bd9Sstevel@tonic-gate 		case '}':
2127c478bd9Sstevel@tonic-gate 			/* when building an re, escape with a backslash */
2137c478bd9Sstevel@tonic-gate 			if (n < 0)
2147c478bd9Sstevel@tonic-gate 				fn_putc(dst, '\\');
2157c478bd9Sstevel@tonic-gate 			fn_putc(dst, c);
2167c478bd9Sstevel@tonic-gate 			break;
2177c478bd9Sstevel@tonic-gate 		case '?':
2187c478bd9Sstevel@tonic-gate 			/* when building an re, change '?' to a single dot */
2197c478bd9Sstevel@tonic-gate 			if (n < 0)
2207c478bd9Sstevel@tonic-gate 				fn_putc(dst, '.');
2217c478bd9Sstevel@tonic-gate 			break;
2227c478bd9Sstevel@tonic-gate 		case '*':
2237c478bd9Sstevel@tonic-gate 			/* when building an re, change '*' to ".*" */
2247c478bd9Sstevel@tonic-gate 			if (n < 0)
2257c478bd9Sstevel@tonic-gate 				fn_putc(dst, '.');
2267c478bd9Sstevel@tonic-gate 			fn_putc(dst, '*');
2277c478bd9Sstevel@tonic-gate 			break;
2287c478bd9Sstevel@tonic-gate 		case '$':
2297c478bd9Sstevel@tonic-gate 			/* '$' marks the start of a keyword */
2307c478bd9Sstevel@tonic-gate 			switch (c = fn_getc(src)) {
2317c478bd9Sstevel@tonic-gate 			case '$':
2327c478bd9Sstevel@tonic-gate 				/* double '$' stands for a single '$' */
2337c478bd9Sstevel@tonic-gate 				if (n < 0)
2347c478bd9Sstevel@tonic-gate 					fn_putc(dst, '\\');
2357c478bd9Sstevel@tonic-gate 				fn_putc(dst, '$');
2367c478bd9Sstevel@tonic-gate 				break;
2377c478bd9Sstevel@tonic-gate 			case '#':
2387c478bd9Sstevel@tonic-gate 				/*
2397c478bd9Sstevel@tonic-gate 				 * $# expands to nothing, but forces an end
2407c478bd9Sstevel@tonic-gate 				 * of keyword, allow juxtaposition of a
2417c478bd9Sstevel@tonic-gate 				 * keyword with lower-case characters
2427c478bd9Sstevel@tonic-gate 				 */
2437c478bd9Sstevel@tonic-gate 				break;
2447c478bd9Sstevel@tonic-gate 			case 'n':
2457c478bd9Sstevel@tonic-gate 			case 'N':
2467c478bd9Sstevel@tonic-gate 				if (c == 'N' || !islower(fn_peekc(src))) {
2477c478bd9Sstevel@tonic-gate 					/*
2487c478bd9Sstevel@tonic-gate 					 * we've found $n or $N, if we're
2497c478bd9Sstevel@tonic-gate 					 * building an re, build one that
2507c478bd9Sstevel@tonic-gate 					 * matches a number, otherwise
2517c478bd9Sstevel@tonic-gate 					 * expand the keyword to the n
2527c478bd9Sstevel@tonic-gate 					 * passed in to this function
2537c478bd9Sstevel@tonic-gate 					 */
2547c478bd9Sstevel@tonic-gate 					hasn = B_TRUE;
2557c478bd9Sstevel@tonic-gate 					if (n < 0)
2567c478bd9Sstevel@tonic-gate 						fn_puts(dst, "([0-9]+)$0");
2577c478bd9Sstevel@tonic-gate 					else {
2587c478bd9Sstevel@tonic-gate 						(void) snprintf(buf,
2597c478bd9Sstevel@tonic-gate 						    MAXDIGITS, "%d",
2607c478bd9Sstevel@tonic-gate 						    (c == 'n') ? n : n + 1);
2617c478bd9Sstevel@tonic-gate 						fn_puts(dst, buf);
2627c478bd9Sstevel@tonic-gate 					}
2637c478bd9Sstevel@tonic-gate 					break;
2647c478bd9Sstevel@tonic-gate 				}
2657c478bd9Sstevel@tonic-gate 				/*FALLTHROUGH*/
2667c478bd9Sstevel@tonic-gate 			default:
2677c478bd9Sstevel@tonic-gate 				/* gather up the keyword name */
2687c478bd9Sstevel@tonic-gate 				fn_renew(kw, NULL);
2697c478bd9Sstevel@tonic-gate 				fn_putc(kw, c);
2707c478bd9Sstevel@tonic-gate 				while (islower(fn_peekc(src)))
2717c478bd9Sstevel@tonic-gate 					fn_putc(kw, fn_getc(src));
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate 				/* lookup keyword */
2747c478bd9Sstevel@tonic-gate 				if ((ptr = (char *)lut_lookup(Keywords,
2757c478bd9Sstevel@tonic-gate 				    fn_s(kw))) == NULL) {
2767c478bd9Sstevel@tonic-gate 					/* nope, copy it unexpanded */
2777c478bd9Sstevel@tonic-gate 					if (n < 0)
2787c478bd9Sstevel@tonic-gate 						fn_putc(dst, '\\');
2797c478bd9Sstevel@tonic-gate 					fn_putc(dst, '$');
2807c478bd9Sstevel@tonic-gate 					fn_putfn(dst, kw);
2817c478bd9Sstevel@tonic-gate 				} else
2827c478bd9Sstevel@tonic-gate 					fn_puts(dst, ptr);
2837c478bd9Sstevel@tonic-gate 			}
2847c478bd9Sstevel@tonic-gate 			break;
2857c478bd9Sstevel@tonic-gate 		case '%':
2867c478bd9Sstevel@tonic-gate 			/*
2877c478bd9Sstevel@tonic-gate 			 * % sequence for strftime(), if we're building
2887c478bd9Sstevel@tonic-gate 			 * an re, we take our best guess at the re for
2897c478bd9Sstevel@tonic-gate 			 * this sequence, otherwise we pass it to strftime()
2907c478bd9Sstevel@tonic-gate 			 */
2917c478bd9Sstevel@tonic-gate 			if (n < 0) {
2927c478bd9Sstevel@tonic-gate 				/*
2937c478bd9Sstevel@tonic-gate 				 * the regex for a percent sequence is
2947c478bd9Sstevel@tonic-gate 				 * usually just ".*" unless it is one
2957c478bd9Sstevel@tonic-gate 				 * of the common cases we know about
2967c478bd9Sstevel@tonic-gate 				 * that are numeric.  in those  cases, we
2977c478bd9Sstevel@tonic-gate 				 * tighten up the regex to just match digits.
2987c478bd9Sstevel@tonic-gate 				 *
2997c478bd9Sstevel@tonic-gate 				 * while it is gross that we embed knowledge
3007c478bd9Sstevel@tonic-gate 				 * of strftime() sequences here, they are
3017c478bd9Sstevel@tonic-gate 				 * specified in a standard so aren't
3027c478bd9Sstevel@tonic-gate 				 * expected to change often, and it *really*
3037c478bd9Sstevel@tonic-gate 				 * cuts down on the possibility that we'll
3047c478bd9Sstevel@tonic-gate 				 * expire a file that isn't an old log file.
3057c478bd9Sstevel@tonic-gate 				 */
3067c478bd9Sstevel@tonic-gate 				if ((c = fn_getc(src)) == 'E' || c == 'O') {
3077c478bd9Sstevel@tonic-gate 					c = fn_getc(src);
3087c478bd9Sstevel@tonic-gate 					fn_puts(dst, ".*");
3097c478bd9Sstevel@tonic-gate 				} else
3107c478bd9Sstevel@tonic-gate 					switch (c) {
3117c478bd9Sstevel@tonic-gate 					case 'd':
3127c478bd9Sstevel@tonic-gate 					case 'g':
3137c478bd9Sstevel@tonic-gate 					case 'G':
3147c478bd9Sstevel@tonic-gate 					case 'H':
3157c478bd9Sstevel@tonic-gate 					case 'I':
3167c478bd9Sstevel@tonic-gate 					case 'j':
3177c478bd9Sstevel@tonic-gate 					case 'm':
3187c478bd9Sstevel@tonic-gate 					case 'M':
3197c478bd9Sstevel@tonic-gate 					case 'S':
3207c478bd9Sstevel@tonic-gate 					case 'u':
3217c478bd9Sstevel@tonic-gate 					case 'U':
3227c478bd9Sstevel@tonic-gate 					case 'V':
3237c478bd9Sstevel@tonic-gate 					case 'w':
3247c478bd9Sstevel@tonic-gate 					case 'W':
3257c478bd9Sstevel@tonic-gate 					case 'y':
3267c478bd9Sstevel@tonic-gate 					case 'Y':
3277c478bd9Sstevel@tonic-gate 						/* pure numeric cases */
3287c478bd9Sstevel@tonic-gate 						fn_puts(dst, "[0-9]+");
3297c478bd9Sstevel@tonic-gate 						break;
3307c478bd9Sstevel@tonic-gate 					case 'e':
3317c478bd9Sstevel@tonic-gate 					case 'k':
3327c478bd9Sstevel@tonic-gate 					case 'l':
3337c478bd9Sstevel@tonic-gate 						/* possible space then num */
3347c478bd9Sstevel@tonic-gate 						fn_puts(dst, " *[0-9]+");
3357c478bd9Sstevel@tonic-gate 						break;
3367c478bd9Sstevel@tonic-gate 					case 'D':	/* %m/%d/%y */
3377c478bd9Sstevel@tonic-gate 						/* adds slashes! */
3387c478bd9Sstevel@tonic-gate 						fn_puts(dst,
3397c478bd9Sstevel@tonic-gate 						    "[0-9]+/[0-9]+/[0-9]+");
3407c478bd9Sstevel@tonic-gate 						break;
3417c478bd9Sstevel@tonic-gate 					case 'R':	/* %H:%M */
3427c478bd9Sstevel@tonic-gate 						fn_puts(dst, "[0-9]+:[0-9]+");
3437c478bd9Sstevel@tonic-gate 						break;
3447c478bd9Sstevel@tonic-gate 					case 'T':	/* %H:%M:%S */
3457c478bd9Sstevel@tonic-gate 						fn_puts(dst,
3467c478bd9Sstevel@tonic-gate 						    "[0-9]+:[0-9]+:[0-9]+");
3477c478bd9Sstevel@tonic-gate 						break;
3487c478bd9Sstevel@tonic-gate 					default:
3497c478bd9Sstevel@tonic-gate 						fn_puts(dst, ".*");
3507c478bd9Sstevel@tonic-gate 					}
3517c478bd9Sstevel@tonic-gate 			} else {
3527c478bd9Sstevel@tonic-gate 				char tbuf[4];
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate 				/* copy % sequence to tbuf */
3557c478bd9Sstevel@tonic-gate 				tbuf[0] = '%';
3567c478bd9Sstevel@tonic-gate 				tbuf[1] = fn_getc(src);
3577c478bd9Sstevel@tonic-gate 				if (tbuf[1] == 'E' || tbuf[1] == 'O') {
3587c478bd9Sstevel@tonic-gate 					/* "extended" sequence */
3597c478bd9Sstevel@tonic-gate 					tbuf[2] = fn_getc(src);
3607c478bd9Sstevel@tonic-gate 					tbuf[3] = '\0';
3617c478bd9Sstevel@tonic-gate 				} else
3627c478bd9Sstevel@tonic-gate 					tbuf[2] = '\0';
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate 				if (strftime(buf, MAXDIGITS, tbuf, gmt_tm) == 0)
3657c478bd9Sstevel@tonic-gate 					/* just copy %x */
3667c478bd9Sstevel@tonic-gate 					fn_puts(dst, tbuf);
3677c478bd9Sstevel@tonic-gate 				else
3687c478bd9Sstevel@tonic-gate 					fn_puts(dst, buf);
3697c478bd9Sstevel@tonic-gate 			}
3707c478bd9Sstevel@tonic-gate 			break;
3717c478bd9Sstevel@tonic-gate 		default:
3727c478bd9Sstevel@tonic-gate 			/* nothing special, just copy it */
3737c478bd9Sstevel@tonic-gate 			fn_putc(dst, c);
3747c478bd9Sstevel@tonic-gate 		}
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate 	if (gz) {
3777c478bd9Sstevel@tonic-gate 		if (n < 0)
3787c478bd9Sstevel@tonic-gate 			fn_puts(dst, "(\\.gz){0,1}");
3797c478bd9Sstevel@tonic-gate 		else
3807c478bd9Sstevel@tonic-gate 			fn_puts(dst, ".gz");
3817c478bd9Sstevel@tonic-gate 	}
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate 	fn_free(kw);
3847c478bd9Sstevel@tonic-gate 	return (hasn);
3857c478bd9Sstevel@tonic-gate }
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate #ifdef	TESTMODULE
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate time_t Now;
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate /*
3927c478bd9Sstevel@tonic-gate  * test main for kw module, usage: a.out fname [template...]
3937c478bd9Sstevel@tonic-gate  */
394*b493790cSbasabi int
main(int argc,char * argv[])3957c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
3967c478bd9Sstevel@tonic-gate {
3977c478bd9Sstevel@tonic-gate 	int i;
3987c478bd9Sstevel@tonic-gate 	struct fn *src = fn_new(NULL);
3997c478bd9Sstevel@tonic-gate 	struct fn *dst = fn_new(NULL);
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate 	err_init(argv[0]);
4027c478bd9Sstevel@tonic-gate 	setbuf(stdout, NULL);
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate 	Now = time(0);
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate 	if (argc < 2)
4077c478bd9Sstevel@tonic-gate 		err(0, "first arg must be fname");
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate 	kw_init(fn_new(argv[1]), NULL);
4107c478bd9Sstevel@tonic-gate 
4117c478bd9Sstevel@tonic-gate 	kw_print(stdout);
4127c478bd9Sstevel@tonic-gate 
4137c478bd9Sstevel@tonic-gate 	for (i = 2; i < argc; i++) {
4147c478bd9Sstevel@tonic-gate 		int n;
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate 		for (n = -1; n < 2; n++) {
4177c478bd9Sstevel@tonic-gate 			fn_renew(src, argv[i]);
4187c478bd9Sstevel@tonic-gate 			fn_renew(dst, NULL);
4197c478bd9Sstevel@tonic-gate 			printf("expand<%s> n %d hasn %d ",
4207c478bd9Sstevel@tonic-gate 			    argv[i], n, kw_expand(src, dst, n, B_FALSE));
4217c478bd9Sstevel@tonic-gate 			printf("result <%s>\n", fn_s(dst));
4227c478bd9Sstevel@tonic-gate 		}
4237c478bd9Sstevel@tonic-gate 	}
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate 	err_done(0);
426*b493790cSbasabi 	/* NOTREACHED */
427*b493790cSbasabi 	return (0);
4287c478bd9Sstevel@tonic-gate }
4297c478bd9Sstevel@tonic-gate 
4307c478bd9Sstevel@tonic-gate #endif	/* TESTMODULE */
431