xref: /onnv-gate/usr/src/cmd/logadm/kw.c (revision 2397:34388daecb5f)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51511Sdp  * Common Development and Distribution License (the "License").
61511Sdp  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
221511Sdp  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  *
250Sstevel@tonic-gate  * logadm/kw.c -- manage keywords table
260Sstevel@tonic-gate  *
270Sstevel@tonic-gate  * this module expands things like $file.$n in "templates".
280Sstevel@tonic-gate  * calling kw_init() sets the current "filename" used for
290Sstevel@tonic-gate  * $file, $dirname, and $basename.
300Sstevel@tonic-gate  *
310Sstevel@tonic-gate  * any time-based expansions, like $secs, or all the strftime()
320Sstevel@tonic-gate  * percent sequences, are based on the exact same point in time.
330Sstevel@tonic-gate  * so calling kw_expand() on something like "file-%T" will return
340Sstevel@tonic-gate  * the same thing when called multiple times during the same logadm run.
350Sstevel@tonic-gate  */
360Sstevel@tonic-gate 
370Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
380Sstevel@tonic-gate 
390Sstevel@tonic-gate #include <stdio.h>
400Sstevel@tonic-gate #include <libintl.h>
410Sstevel@tonic-gate #include <stdlib.h>
420Sstevel@tonic-gate #include <sys/types.h>
430Sstevel@tonic-gate #include <sys/stat.h>
440Sstevel@tonic-gate #include <sys/utsname.h>
450Sstevel@tonic-gate #include <sys/systeminfo.h>
460Sstevel@tonic-gate #include <strings.h>
470Sstevel@tonic-gate #include <time.h>
480Sstevel@tonic-gate #include <ctype.h>
491511Sdp #include <zone.h>
500Sstevel@tonic-gate #include "err.h"
510Sstevel@tonic-gate #include "lut.h"
520Sstevel@tonic-gate #include "fn.h"
530Sstevel@tonic-gate #include "kw.h"
540Sstevel@tonic-gate 
550Sstevel@tonic-gate /* forward declarations for functions used internally by this module */
560Sstevel@tonic-gate static void kw_printer(const char *lhs, void *rhs, void *arg);
570Sstevel@tonic-gate 
580Sstevel@tonic-gate /*
590Sstevel@tonic-gate  * absurdly long length to hold sprintf of a %d,
600Sstevel@tonic-gate  * or strftime() expansion of a *single* percent sequence
610Sstevel@tonic-gate  */
620Sstevel@tonic-gate #define	MAXDIGITS 100
630Sstevel@tonic-gate 
640Sstevel@tonic-gate static struct lut *Keywords;	/* lookup table for keywords */
650Sstevel@tonic-gate 
660Sstevel@tonic-gate extern time_t Now;		/* time used for keyword expansions */
670Sstevel@tonic-gate 
680Sstevel@tonic-gate /*
690Sstevel@tonic-gate  * kw_init -- initialize keywords based on given filename
700Sstevel@tonic-gate  */
710Sstevel@tonic-gate void
kw_init(struct fn * fnp,struct fn * nfnp)720Sstevel@tonic-gate kw_init(struct fn *fnp, struct fn *nfnp)
730Sstevel@tonic-gate {
740Sstevel@tonic-gate 	static char *fullpath;
750Sstevel@tonic-gate 	static char *nfullpath;
760Sstevel@tonic-gate 	static char *splitpath;
770Sstevel@tonic-gate 	static char secs[MAXDIGITS];
780Sstevel@tonic-gate 	static struct utsname un;
790Sstevel@tonic-gate 	static char platform[SYS_NMLN];
800Sstevel@tonic-gate 	static char isa[SYS_NMLN];
810Sstevel@tonic-gate 	static char domain[256];
820Sstevel@tonic-gate 	static char *home;
830Sstevel@tonic-gate 	static char *user;
840Sstevel@tonic-gate 	static char *logname;
851511Sdp 	static char zonename[ZONENAME_MAX];
861511Sdp 	static zoneid_t zoneid;
870Sstevel@tonic-gate 	static int initialized;
880Sstevel@tonic-gate 	char *ptr;
890Sstevel@tonic-gate 
900Sstevel@tonic-gate 	/* make a copy of the string for $file */
910Sstevel@tonic-gate 	if (fullpath)
920Sstevel@tonic-gate 		FREE(fullpath);
930Sstevel@tonic-gate 	fullpath = STRDUP(fn_s(fnp));
940Sstevel@tonic-gate 	Keywords = lut_add(Keywords, "file", fullpath);
950Sstevel@tonic-gate 
960Sstevel@tonic-gate 	/* make a copy of the string for $nfile */
970Sstevel@tonic-gate 	if (nfullpath)
980Sstevel@tonic-gate 		FREE(nfullpath);
990Sstevel@tonic-gate 	if (nfnp == NULL) {
1000Sstevel@tonic-gate 		nfullpath = NULL;
1010Sstevel@tonic-gate 		Keywords = lut_add(Keywords, "nfile", "");
1020Sstevel@tonic-gate 	} else {
1030Sstevel@tonic-gate 		nfullpath = STRDUP(fn_s(nfnp));
1040Sstevel@tonic-gate 		Keywords = lut_add(Keywords, "nfile", nfullpath);
1050Sstevel@tonic-gate 	}
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate 	/* make a copy of the string for $dirname/$basename */
1080Sstevel@tonic-gate 	if (splitpath)
1090Sstevel@tonic-gate 		FREE(splitpath);
1100Sstevel@tonic-gate 	splitpath = STRDUP(fn_s(fnp));
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate 	if ((ptr = strrchr(splitpath, '/')) == NULL) {
1130Sstevel@tonic-gate 		Keywords = lut_add(Keywords, "basename", splitpath);
1140Sstevel@tonic-gate 		Keywords = lut_add(Keywords, "dirname", ".");
1150Sstevel@tonic-gate 	} else {
1160Sstevel@tonic-gate 		*ptr++ = '\0';
1170Sstevel@tonic-gate 		Keywords = lut_add(Keywords, "basename", ptr);
1180Sstevel@tonic-gate 		Keywords = lut_add(Keywords, "dirname", splitpath);
1190Sstevel@tonic-gate 	}
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate 	if (initialized)
1220Sstevel@tonic-gate 		return;		/* rest of the keywords don't change */
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate 	(void) snprintf(secs, MAXDIGITS, "%d", (int)Now);
1250Sstevel@tonic-gate 	Keywords = lut_add(Keywords, "secs", secs);
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate 	if (uname(&un) < 0)
1280Sstevel@tonic-gate 		err(EF_SYS, "uname");
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate 	Keywords = lut_add(Keywords, "nodename", un.nodename);
1310Sstevel@tonic-gate 	Keywords = lut_add(Keywords, "release", un.release);
1320Sstevel@tonic-gate 	Keywords = lut_add(Keywords, "machine", un.machine);
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate 	if (sysinfo(SI_ARCHITECTURE, isa, sizeof (isa)) == -1)
1350Sstevel@tonic-gate 		err(EF_WARN|EF_SYS, "sysinfo(SI_ARCHITECTURE) failed.");
1360Sstevel@tonic-gate 	else
1370Sstevel@tonic-gate 		Keywords = lut_add(Keywords, "isa", isa);
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate 	if (sysinfo(SI_PLATFORM, platform, sizeof (platform)) == -1)
1400Sstevel@tonic-gate 		err(EF_WARN|EF_SYS, "sysinfo(SI_PLATFORM) failed.");
1410Sstevel@tonic-gate 	else
1420Sstevel@tonic-gate 		Keywords = lut_add(Keywords, "platform", platform);
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate 	if (sysinfo(SI_SRPC_DOMAIN, domain, sizeof (domain)) == -1)
1450Sstevel@tonic-gate 		err(EF_WARN|EF_SYS, "sysinfo(SI_SRPC_DOMAIN) failed.");
1460Sstevel@tonic-gate 	else
1470Sstevel@tonic-gate 		Keywords = lut_add(Keywords, "domain", domain);
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate 	if ((home = getenv("HOME")) != NULL)
1500Sstevel@tonic-gate 		Keywords = lut_add(Keywords, "home", STRDUP(home));
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate 	if ((user = getenv("USER")) != NULL)
1530Sstevel@tonic-gate 		Keywords = lut_add(Keywords, "user", STRDUP(user));
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate 	if ((logname = getenv("LOGNAME")) != NULL)
1560Sstevel@tonic-gate 		Keywords = lut_add(Keywords, "logname", STRDUP(logname));
1570Sstevel@tonic-gate 
1581511Sdp 	zoneid = getzoneid();
1591511Sdp 	if ((getzonenamebyid(zoneid, zonename, sizeof (zonename))) == -1)
1601511Sdp 		err(EF_WARN|EF_SYS, "getzonenamebyid() failed.");
1611511Sdp 	else
1621511Sdp 		Keywords = lut_add(Keywords, "zonename", STRDUP(zonename));
1631511Sdp 
1640Sstevel@tonic-gate 	initialized = 1;
1650Sstevel@tonic-gate }
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate /* helper function for kw_print() */
1680Sstevel@tonic-gate static void
kw_printer(const char * lhs,void * rhs,void * arg)1690Sstevel@tonic-gate kw_printer(const char *lhs, void *rhs, void *arg)
1700Sstevel@tonic-gate {
1710Sstevel@tonic-gate 	FILE *stream = (FILE *)arg;
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate 	(void) fprintf(stream, "%20.20s %s\n", lhs, (char *)rhs);
1740Sstevel@tonic-gate }
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate /*
1770Sstevel@tonic-gate  * kw_print -- spew the entire keywords table to stream
1780Sstevel@tonic-gate  *
1790Sstevel@tonic-gate  * this routine is used to dump the keywords table for debugging.
1800Sstevel@tonic-gate  */
1810Sstevel@tonic-gate void
kw_print(FILE * stream)1820Sstevel@tonic-gate kw_print(FILE *stream)
1830Sstevel@tonic-gate {
1840Sstevel@tonic-gate 	lut_walk(Keywords, kw_printer, stream);
1850Sstevel@tonic-gate }
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate /*
1880Sstevel@tonic-gate  * kw_expand -- expand src into dst with given n value for $n (or $N)
1890Sstevel@tonic-gate  *
1900Sstevel@tonic-gate  * n == -1 means expand src into a reglob
1910Sstevel@tonic-gate  * if gz is true, include ".gz" extension
1920Sstevel@tonic-gate  *
1930Sstevel@tonic-gate  * returns true if template contains $n or $N (implying rotation of files)
1940Sstevel@tonic-gate  */
1950Sstevel@tonic-gate boolean_t
kw_expand(struct fn * src,struct fn * dst,int n,boolean_t gz)1960Sstevel@tonic-gate kw_expand(struct fn *src, struct fn *dst, int n, boolean_t gz)
1970Sstevel@tonic-gate {
1980Sstevel@tonic-gate 	int c;
1990Sstevel@tonic-gate 	char buf[MAXDIGITS];
2000Sstevel@tonic-gate 	boolean_t hasn = B_FALSE;
2010Sstevel@tonic-gate 	struct fn *kw = fn_new(NULL);
2020Sstevel@tonic-gate 	char *ptr;
203954Sgm149974 	struct tm *gmt_tm = localtime(&Now);
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate 	while ((c = fn_getc(src)) != '\0')
2060Sstevel@tonic-gate 		switch (c) {
2070Sstevel@tonic-gate 		case '.':
2080Sstevel@tonic-gate 		case '(':
2090Sstevel@tonic-gate 		case ')':
2100Sstevel@tonic-gate 		case '^':
2110Sstevel@tonic-gate 		case '+':
2120Sstevel@tonic-gate 		case '{':
2130Sstevel@tonic-gate 		case '}':
2140Sstevel@tonic-gate 			/* when building an re, escape with a backslash */
2150Sstevel@tonic-gate 			if (n < 0)
2160Sstevel@tonic-gate 				fn_putc(dst, '\\');
2170Sstevel@tonic-gate 			fn_putc(dst, c);
2180Sstevel@tonic-gate 			break;
2190Sstevel@tonic-gate 		case '?':
2200Sstevel@tonic-gate 			/* when building an re, change '?' to a single dot */
2210Sstevel@tonic-gate 			if (n < 0)
2220Sstevel@tonic-gate 				fn_putc(dst, '.');
2230Sstevel@tonic-gate 			break;
2240Sstevel@tonic-gate 		case '*':
2250Sstevel@tonic-gate 			/* when building an re, change '*' to ".*" */
2260Sstevel@tonic-gate 			if (n < 0)
2270Sstevel@tonic-gate 				fn_putc(dst, '.');
2280Sstevel@tonic-gate 			fn_putc(dst, '*');
2290Sstevel@tonic-gate 			break;
2300Sstevel@tonic-gate 		case '$':
2310Sstevel@tonic-gate 			/* '$' marks the start of a keyword */
2320Sstevel@tonic-gate 			switch (c = fn_getc(src)) {
2330Sstevel@tonic-gate 			case '$':
2340Sstevel@tonic-gate 				/* double '$' stands for a single '$' */
2350Sstevel@tonic-gate 				if (n < 0)
2360Sstevel@tonic-gate 					fn_putc(dst, '\\');
2370Sstevel@tonic-gate 				fn_putc(dst, '$');
2380Sstevel@tonic-gate 				break;
2390Sstevel@tonic-gate 			case '#':
2400Sstevel@tonic-gate 				/*
2410Sstevel@tonic-gate 				 * $# expands to nothing, but forces an end
2420Sstevel@tonic-gate 				 * of keyword, allow juxtaposition of a
2430Sstevel@tonic-gate 				 * keyword with lower-case characters
2440Sstevel@tonic-gate 				 */
2450Sstevel@tonic-gate 				break;
2460Sstevel@tonic-gate 			case 'n':
2470Sstevel@tonic-gate 			case 'N':
2480Sstevel@tonic-gate 				if (c == 'N' || !islower(fn_peekc(src))) {
2490Sstevel@tonic-gate 					/*
2500Sstevel@tonic-gate 					 * we've found $n or $N, if we're
2510Sstevel@tonic-gate 					 * building an re, build one that
2520Sstevel@tonic-gate 					 * matches a number, otherwise
2530Sstevel@tonic-gate 					 * expand the keyword to the n
2540Sstevel@tonic-gate 					 * passed in to this function
2550Sstevel@tonic-gate 					 */
2560Sstevel@tonic-gate 					hasn = B_TRUE;
2570Sstevel@tonic-gate 					if (n < 0)
2580Sstevel@tonic-gate 						fn_puts(dst, "([0-9]+)$0");
2590Sstevel@tonic-gate 					else {
2600Sstevel@tonic-gate 						(void) snprintf(buf,
2610Sstevel@tonic-gate 						    MAXDIGITS, "%d",
2620Sstevel@tonic-gate 						    (c == 'n') ? n : n + 1);
2630Sstevel@tonic-gate 						fn_puts(dst, buf);
2640Sstevel@tonic-gate 					}
2650Sstevel@tonic-gate 					break;
2660Sstevel@tonic-gate 				}
2670Sstevel@tonic-gate 				/*FALLTHROUGH*/
2680Sstevel@tonic-gate 			default:
2690Sstevel@tonic-gate 				/* gather up the keyword name */
2700Sstevel@tonic-gate 				fn_renew(kw, NULL);
2710Sstevel@tonic-gate 				fn_putc(kw, c);
2720Sstevel@tonic-gate 				while (islower(fn_peekc(src)))
2730Sstevel@tonic-gate 					fn_putc(kw, fn_getc(src));
2740Sstevel@tonic-gate 
2750Sstevel@tonic-gate 				/* lookup keyword */
2760Sstevel@tonic-gate 				if ((ptr = (char *)lut_lookup(Keywords,
2770Sstevel@tonic-gate 				    fn_s(kw))) == NULL) {
2780Sstevel@tonic-gate 					/* nope, copy it unexpanded */
2790Sstevel@tonic-gate 					if (n < 0)
2800Sstevel@tonic-gate 						fn_putc(dst, '\\');
2810Sstevel@tonic-gate 					fn_putc(dst, '$');
2820Sstevel@tonic-gate 					fn_putfn(dst, kw);
2830Sstevel@tonic-gate 				} else
2840Sstevel@tonic-gate 					fn_puts(dst, ptr);
2850Sstevel@tonic-gate 			}
2860Sstevel@tonic-gate 			break;
2870Sstevel@tonic-gate 		case '%':
2880Sstevel@tonic-gate 			/*
2890Sstevel@tonic-gate 			 * % sequence for strftime(), if we're building
2900Sstevel@tonic-gate 			 * an re, we take our best guess at the re for
2910Sstevel@tonic-gate 			 * this sequence, otherwise we pass it to strftime()
2920Sstevel@tonic-gate 			 */
2930Sstevel@tonic-gate 			if (n < 0) {
2940Sstevel@tonic-gate 				/*
2950Sstevel@tonic-gate 				 * the regex for a percent sequence is
2960Sstevel@tonic-gate 				 * usually just ".*" unless it is one
2970Sstevel@tonic-gate 				 * of the common cases we know about
2980Sstevel@tonic-gate 				 * that are numeric.  in those  cases, we
2990Sstevel@tonic-gate 				 * tighten up the regex to just match digits.
3000Sstevel@tonic-gate 				 *
3010Sstevel@tonic-gate 				 * while it is gross that we embed knowledge
3020Sstevel@tonic-gate 				 * of strftime() sequences here, they are
3030Sstevel@tonic-gate 				 * specified in a standard so aren't
3040Sstevel@tonic-gate 				 * expected to change often, and it *really*
3050Sstevel@tonic-gate 				 * cuts down on the possibility that we'll
3060Sstevel@tonic-gate 				 * expire a file that isn't an old log file.
3070Sstevel@tonic-gate 				 */
3080Sstevel@tonic-gate 				if ((c = fn_getc(src)) == 'E' || c == 'O') {
3090Sstevel@tonic-gate 					c = fn_getc(src);
3100Sstevel@tonic-gate 					fn_puts(dst, ".*");
3110Sstevel@tonic-gate 				} else
3120Sstevel@tonic-gate 					switch (c) {
3130Sstevel@tonic-gate 					case 'd':
3140Sstevel@tonic-gate 					case 'g':
3150Sstevel@tonic-gate 					case 'G':
3160Sstevel@tonic-gate 					case 'H':
3170Sstevel@tonic-gate 					case 'I':
3180Sstevel@tonic-gate 					case 'j':
3190Sstevel@tonic-gate 					case 'm':
3200Sstevel@tonic-gate 					case 'M':
3210Sstevel@tonic-gate 					case 'S':
3220Sstevel@tonic-gate 					case 'u':
3230Sstevel@tonic-gate 					case 'U':
3240Sstevel@tonic-gate 					case 'V':
3250Sstevel@tonic-gate 					case 'w':
3260Sstevel@tonic-gate 					case 'W':
3270Sstevel@tonic-gate 					case 'y':
3280Sstevel@tonic-gate 					case 'Y':
3290Sstevel@tonic-gate 						/* pure numeric cases */
3300Sstevel@tonic-gate 						fn_puts(dst, "[0-9]+");
3310Sstevel@tonic-gate 						break;
3320Sstevel@tonic-gate 					case 'e':
3330Sstevel@tonic-gate 					case 'k':
3340Sstevel@tonic-gate 					case 'l':
3350Sstevel@tonic-gate 						/* possible space then num */
3360Sstevel@tonic-gate 						fn_puts(dst, " *[0-9]+");
3370Sstevel@tonic-gate 						break;
3380Sstevel@tonic-gate 					case 'D':	/* %m/%d/%y */
3390Sstevel@tonic-gate 						/* adds slashes! */
3400Sstevel@tonic-gate 						fn_puts(dst,
3410Sstevel@tonic-gate 						    "[0-9]+/[0-9]+/[0-9]+");
3420Sstevel@tonic-gate 						break;
3430Sstevel@tonic-gate 					case 'R':	/* %H:%M */
3440Sstevel@tonic-gate 						fn_puts(dst, "[0-9]+:[0-9]+");
3450Sstevel@tonic-gate 						break;
3460Sstevel@tonic-gate 					case 'T':	/* %H:%M:%S */
3470Sstevel@tonic-gate 						fn_puts(dst,
3480Sstevel@tonic-gate 						    "[0-9]+:[0-9]+:[0-9]+");
3490Sstevel@tonic-gate 						break;
3500Sstevel@tonic-gate 					default:
3510Sstevel@tonic-gate 						fn_puts(dst, ".*");
3520Sstevel@tonic-gate 					}
3530Sstevel@tonic-gate 			} else {
3540Sstevel@tonic-gate 				char tbuf[4];
3550Sstevel@tonic-gate 
3560Sstevel@tonic-gate 				/* copy % sequence to tbuf */
3570Sstevel@tonic-gate 				tbuf[0] = '%';
3580Sstevel@tonic-gate 				tbuf[1] = fn_getc(src);
3590Sstevel@tonic-gate 				if (tbuf[1] == 'E' || tbuf[1] == 'O') {
3600Sstevel@tonic-gate 					/* "extended" sequence */
3610Sstevel@tonic-gate 					tbuf[2] = fn_getc(src);
3620Sstevel@tonic-gate 					tbuf[3] = '\0';
3630Sstevel@tonic-gate 				} else
3640Sstevel@tonic-gate 					tbuf[2] = '\0';
3650Sstevel@tonic-gate 
3660Sstevel@tonic-gate 				if (strftime(buf, MAXDIGITS, tbuf, gmt_tm) == 0)
3670Sstevel@tonic-gate 					/* just copy %x */
3680Sstevel@tonic-gate 					fn_puts(dst, tbuf);
3690Sstevel@tonic-gate 				else
3700Sstevel@tonic-gate 					fn_puts(dst, buf);
3710Sstevel@tonic-gate 			}
3720Sstevel@tonic-gate 			break;
3730Sstevel@tonic-gate 		default:
3740Sstevel@tonic-gate 			/* nothing special, just copy it */
3750Sstevel@tonic-gate 			fn_putc(dst, c);
3760Sstevel@tonic-gate 		}
3770Sstevel@tonic-gate 
3780Sstevel@tonic-gate 	if (gz) {
3790Sstevel@tonic-gate 		if (n < 0)
3800Sstevel@tonic-gate 			fn_puts(dst, "(\\.gz){0,1}");
3810Sstevel@tonic-gate 		else
3820Sstevel@tonic-gate 			fn_puts(dst, ".gz");
3830Sstevel@tonic-gate 	}
3840Sstevel@tonic-gate 
3850Sstevel@tonic-gate 	fn_free(kw);
3860Sstevel@tonic-gate 	return (hasn);
3870Sstevel@tonic-gate }
3880Sstevel@tonic-gate 
3890Sstevel@tonic-gate #ifdef	TESTMODULE
3900Sstevel@tonic-gate 
3910Sstevel@tonic-gate time_t Now;
3920Sstevel@tonic-gate 
3930Sstevel@tonic-gate /*
3940Sstevel@tonic-gate  * test main for kw module, usage: a.out fname [template...]
3950Sstevel@tonic-gate  */
396*2397Sbasabi int
main(int argc,char * argv[])3970Sstevel@tonic-gate main(int argc, char *argv[])
3980Sstevel@tonic-gate {
3990Sstevel@tonic-gate 	int i;
4000Sstevel@tonic-gate 	struct fn *src = fn_new(NULL);
4010Sstevel@tonic-gate 	struct fn *dst = fn_new(NULL);
4020Sstevel@tonic-gate 
4030Sstevel@tonic-gate 	err_init(argv[0]);
4040Sstevel@tonic-gate 	setbuf(stdout, NULL);
4050Sstevel@tonic-gate 
4060Sstevel@tonic-gate 	Now = time(0);
4070Sstevel@tonic-gate 
4080Sstevel@tonic-gate 	if (argc < 2)
4090Sstevel@tonic-gate 		err(0, "first arg must be fname");
4100Sstevel@tonic-gate 
4110Sstevel@tonic-gate 	kw_init(fn_new(argv[1]), NULL);
4120Sstevel@tonic-gate 
4130Sstevel@tonic-gate 	kw_print(stdout);
4140Sstevel@tonic-gate 
4150Sstevel@tonic-gate 	for (i = 2; i < argc; i++) {
4160Sstevel@tonic-gate 		int n;
4170Sstevel@tonic-gate 
4180Sstevel@tonic-gate 		for (n = -1; n < 2; n++) {
4190Sstevel@tonic-gate 			fn_renew(src, argv[i]);
4200Sstevel@tonic-gate 			fn_renew(dst, NULL);
4210Sstevel@tonic-gate 			printf("expand<%s> n %d hasn %d ",
4220Sstevel@tonic-gate 			    argv[i], n, kw_expand(src, dst, n, B_FALSE));
4230Sstevel@tonic-gate 			printf("result <%s>\n", fn_s(dst));
4240Sstevel@tonic-gate 		}
4250Sstevel@tonic-gate 	}
4260Sstevel@tonic-gate 
4270Sstevel@tonic-gate 	err_done(0);
428*2397Sbasabi 	/* NOTREACHED */
429*2397Sbasabi 	return (0);
4300Sstevel@tonic-gate }
4310Sstevel@tonic-gate 
4320Sstevel@tonic-gate #endif	/* TESTMODULE */
433