xref: /titanic_50/usr/src/cmd/logadm/opts.c (revision 78eb75caca75144af27b7903ffed3fb549faab2f)
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  */
22*78eb75caSchin 
237c478bd9Sstevel@tonic-gate /*
24*78eb75caSchin  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
25*78eb75caSchin  * Use is subject to license terms.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
297c478bd9Sstevel@tonic-gate 
30*78eb75caSchin /*
31*78eb75caSchin  * logadm/opts.c -- options handling routines
32*78eb75caSchin  */
33*78eb75caSchin 
347c478bd9Sstevel@tonic-gate #include <stdio.h>
357c478bd9Sstevel@tonic-gate #include <libintl.h>
367c478bd9Sstevel@tonic-gate #include <stdlib.h>
377c478bd9Sstevel@tonic-gate #include <ctype.h>
387c478bd9Sstevel@tonic-gate #include <strings.h>
397c478bd9Sstevel@tonic-gate #include <time.h>
407c478bd9Sstevel@tonic-gate #include <sys/types.h>
417c478bd9Sstevel@tonic-gate #include <sys/stat.h>
427c478bd9Sstevel@tonic-gate #include <errno.h>
437c478bd9Sstevel@tonic-gate #include "err.h"
447c478bd9Sstevel@tonic-gate #include "lut.h"
457c478bd9Sstevel@tonic-gate #include "fn.h"
467c478bd9Sstevel@tonic-gate #include "opts.h"
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate /* forward declarations for private functions */
497c478bd9Sstevel@tonic-gate static struct optinfo *opt_info(int c);
507c478bd9Sstevel@tonic-gate static void opts_setcmdarg(struct opts *opts, const char *cmdarg);
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate /* info created by opts_parse(), private to this module */
537c478bd9Sstevel@tonic-gate struct opts {
547c478bd9Sstevel@tonic-gate 	struct lut *op_raw;		/* the raw text for the options */
557c478bd9Sstevel@tonic-gate 	struct lut *op_ints;		/* the int values for the options */
567c478bd9Sstevel@tonic-gate 	struct fn_list *op_cmdargs;	/* the op_cmdargs */
577c478bd9Sstevel@tonic-gate };
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate static struct lut *Info;		/* table driving parsing */
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate /*
627c478bd9Sstevel@tonic-gate  * opts_init -- set current options parsing table
637c478bd9Sstevel@tonic-gate  */
647c478bd9Sstevel@tonic-gate void
657c478bd9Sstevel@tonic-gate opts_init(struct optinfo *table, int numentries)
667c478bd9Sstevel@tonic-gate {
677c478bd9Sstevel@tonic-gate 	while (numentries-- > 0) {
687c478bd9Sstevel@tonic-gate 		Info = lut_add(Info, table->oi_o, table);
697c478bd9Sstevel@tonic-gate 		table++;
707c478bd9Sstevel@tonic-gate 	}
717c478bd9Sstevel@tonic-gate }
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate /*
747c478bd9Sstevel@tonic-gate  * opt_info -- fetch the optinfo struct for the given option
757c478bd9Sstevel@tonic-gate  */
767c478bd9Sstevel@tonic-gate static struct optinfo *
777c478bd9Sstevel@tonic-gate opt_info(int c)
787c478bd9Sstevel@tonic-gate {
797c478bd9Sstevel@tonic-gate 	char lhs[2];
807c478bd9Sstevel@tonic-gate 	lhs[0] = c;
817c478bd9Sstevel@tonic-gate 	lhs[1] = '\0';
827c478bd9Sstevel@tonic-gate 	return ((struct optinfo *)lut_lookup(Info, lhs));
837c478bd9Sstevel@tonic-gate }
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate /*
867c478bd9Sstevel@tonic-gate  * opts_parse -- parse an argv-style list of options
877c478bd9Sstevel@tonic-gate  *
887c478bd9Sstevel@tonic-gate  * prints a message to stderr and calls err(EF_FILE|EF_JMP, ...) on error
897c478bd9Sstevel@tonic-gate  */
907c478bd9Sstevel@tonic-gate struct opts *
917c478bd9Sstevel@tonic-gate opts_parse(char **argv, int flags)
927c478bd9Sstevel@tonic-gate {
937c478bd9Sstevel@tonic-gate 	struct opts *ret = MALLOC(sizeof (*ret));
947c478bd9Sstevel@tonic-gate 	int dashdash = 0;
957c478bd9Sstevel@tonic-gate 	char *ptr;
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate 	ret->op_raw = ret->op_ints = NULL;
987c478bd9Sstevel@tonic-gate 	ret->op_cmdargs = fn_list_new(NULL);
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 	/* no words to process, just return empty opts struct */
1017c478bd9Sstevel@tonic-gate 	if (argv == NULL)
1027c478bd9Sstevel@tonic-gate 		return (ret);
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate 	/* foreach word... */
1057c478bd9Sstevel@tonic-gate 	for (; (ptr = *argv) != NULL; argv++) {
1067c478bd9Sstevel@tonic-gate 		if (dashdash || *ptr != '-') {
1077c478bd9Sstevel@tonic-gate 			/* found a cmdarg */
1087c478bd9Sstevel@tonic-gate 			opts_setcmdarg(ret, ptr);
1097c478bd9Sstevel@tonic-gate 			continue;
1107c478bd9Sstevel@tonic-gate 		}
1117c478bd9Sstevel@tonic-gate 		if (*++ptr == '\0')
1127c478bd9Sstevel@tonic-gate 			err(EF_FILE|EF_JMP, "Illegal option: dash by itself");
1137c478bd9Sstevel@tonic-gate 		if (*ptr == '-') {
1147c478bd9Sstevel@tonic-gate 			/* (here's where support for --longname would go) */
1157c478bd9Sstevel@tonic-gate 			if (*(ptr + 1) != '\0')
1167c478bd9Sstevel@tonic-gate 				err(EF_FILE|EF_JMP, "Illegal option: -%s", ptr);
1177c478bd9Sstevel@tonic-gate 			dashdash++;
1187c478bd9Sstevel@tonic-gate 			continue;
1197c478bd9Sstevel@tonic-gate 		}
1207c478bd9Sstevel@tonic-gate 		for (; *ptr; ptr++) {
1217c478bd9Sstevel@tonic-gate 			struct optinfo *info = opt_info(*ptr);
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate 			/* see if option was in our parsing table */
1247c478bd9Sstevel@tonic-gate 			if (info == NULL)
1257c478bd9Sstevel@tonic-gate 				err(EF_FILE|EF_JMP, "Illegal option: %c", *ptr);
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 			/* see if context allows this option */
1287c478bd9Sstevel@tonic-gate 			if ((flags & OPTF_CLI) &&
1297c478bd9Sstevel@tonic-gate 			    (info->oi_flags & OPTF_CLI) == 0)
1307c478bd9Sstevel@tonic-gate 				err(EF_FILE|EF_JMP,
1317c478bd9Sstevel@tonic-gate 				    "Option '%c' not allowed on "
1327c478bd9Sstevel@tonic-gate 				    "command line", *ptr);
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 			if ((flags & OPTF_CONF) &&
1357c478bd9Sstevel@tonic-gate 			    (info->oi_flags & OPTF_CONF) == 0)
1367c478bd9Sstevel@tonic-gate 				err(EF_FILE|EF_JMP,
1377c478bd9Sstevel@tonic-gate 				    "Option '%c' not allowed in "
1387c478bd9Sstevel@tonic-gate 				    "configuration file", *ptr);
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 			/* for boolean options, we have all the info we need */
1417c478bd9Sstevel@tonic-gate 			if (info->oi_t == OPTTYPE_BOOLEAN) {
1427c478bd9Sstevel@tonic-gate 				(void) opts_set(ret, info->oi_o, "");
1437c478bd9Sstevel@tonic-gate 				continue;
1447c478bd9Sstevel@tonic-gate 			}
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 			/* option expects argument */
1477c478bd9Sstevel@tonic-gate 			if (*++ptr == '\0' &&
1487c478bd9Sstevel@tonic-gate 			    ((ptr = *++argv) == NULL || *ptr == '-'))
1497c478bd9Sstevel@tonic-gate 				err(EF_FILE|EF_JMP,
1507c478bd9Sstevel@tonic-gate 				    "Option '%c' requires an argument",
1517c478bd9Sstevel@tonic-gate 				    info->oi_o[0]);
1527c478bd9Sstevel@tonic-gate 			opts_set(ret, info->oi_o, ptr);
1537c478bd9Sstevel@tonic-gate 			break;
1547c478bd9Sstevel@tonic-gate 		}
1557c478bd9Sstevel@tonic-gate 	}
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate 	return (ret);
1587c478bd9Sstevel@tonic-gate }
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate /*
1617c478bd9Sstevel@tonic-gate  * opts_free -- free a struct opts previously allocated by opts_parse()
1627c478bd9Sstevel@tonic-gate  */
1637c478bd9Sstevel@tonic-gate void
1647c478bd9Sstevel@tonic-gate opts_free(struct opts *opts)
1657c478bd9Sstevel@tonic-gate {
1667c478bd9Sstevel@tonic-gate 	if (opts) {
1677c478bd9Sstevel@tonic-gate 		lut_free(opts->op_raw, NULL);
1687c478bd9Sstevel@tonic-gate 		lut_free(opts->op_ints, NULL);
1697c478bd9Sstevel@tonic-gate 		fn_list_free(opts->op_cmdargs);
1707c478bd9Sstevel@tonic-gate 		FREE(opts);
1717c478bd9Sstevel@tonic-gate 	}
1727c478bd9Sstevel@tonic-gate }
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate /*
1757c478bd9Sstevel@tonic-gate  * opts_set -- set an option
1767c478bd9Sstevel@tonic-gate  */
1777c478bd9Sstevel@tonic-gate void
1787c478bd9Sstevel@tonic-gate opts_set(struct opts *opts, const char *o, const char *optarg)
1797c478bd9Sstevel@tonic-gate {
1807c478bd9Sstevel@tonic-gate 	struct optinfo *info = opt_info(*o);
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 	opts->op_raw = lut_add(opts->op_raw, o, (void *)optarg);
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 	if (info->oi_parser)
1857c478bd9Sstevel@tonic-gate 		opts->op_ints = lut_add(opts->op_ints, o, (void *)
1867c478bd9Sstevel@tonic-gate 		    (*info->oi_parser)(o, optarg));
1877c478bd9Sstevel@tonic-gate }
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate /*
1907c478bd9Sstevel@tonic-gate  * opts_setcmdarg -- add a cmdarg to the list of op_cmdargs
1917c478bd9Sstevel@tonic-gate  */
1927c478bd9Sstevel@tonic-gate static void
1937c478bd9Sstevel@tonic-gate opts_setcmdarg(struct opts *opts, const char *cmdarg)
1947c478bd9Sstevel@tonic-gate {
1957c478bd9Sstevel@tonic-gate 	fn_list_adds(opts->op_cmdargs, cmdarg);
1967c478bd9Sstevel@tonic-gate }
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate /*
1997c478bd9Sstevel@tonic-gate  * opts_count -- return count of the options in *options that are set
2007c478bd9Sstevel@tonic-gate  */
2017c478bd9Sstevel@tonic-gate int
2027c478bd9Sstevel@tonic-gate opts_count(struct opts *opts, const char *options)
2037c478bd9Sstevel@tonic-gate {
2047c478bd9Sstevel@tonic-gate 	int count = 0;
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 	for (; *options; options++) {
2077c478bd9Sstevel@tonic-gate 		char lhs[2];
2087c478bd9Sstevel@tonic-gate 		lhs[0] = *options;
2097c478bd9Sstevel@tonic-gate 		lhs[1] = '\0';
2107c478bd9Sstevel@tonic-gate 		if (lut_lookup(opts->op_raw, lhs))
2117c478bd9Sstevel@tonic-gate 			count++;
2127c478bd9Sstevel@tonic-gate 	}
2137c478bd9Sstevel@tonic-gate 	return (count);
2147c478bd9Sstevel@tonic-gate }
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate /*
2177c478bd9Sstevel@tonic-gate  * opts_optarg -- return the optarg for the given option, NULL if not set
2187c478bd9Sstevel@tonic-gate  */
2197c478bd9Sstevel@tonic-gate const char *
2207c478bd9Sstevel@tonic-gate opts_optarg(struct opts *opts, const char *o)
2217c478bd9Sstevel@tonic-gate {
2227c478bd9Sstevel@tonic-gate 	return ((char *)lut_lookup(opts->op_raw, o));
2237c478bd9Sstevel@tonic-gate }
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate /*
2267c478bd9Sstevel@tonic-gate  * opts_optarg_int -- return the int value for the given option
2277c478bd9Sstevel@tonic-gate  */
2287c478bd9Sstevel@tonic-gate int
2297c478bd9Sstevel@tonic-gate opts_optarg_int(struct opts *opts, const char *o)
2307c478bd9Sstevel@tonic-gate {
2317c478bd9Sstevel@tonic-gate 	return ((int)lut_lookup(opts->op_ints, o));
2327c478bd9Sstevel@tonic-gate }
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate /*
2357c478bd9Sstevel@tonic-gate  * opts_cmdargs -- return list of op_cmdargs
2367c478bd9Sstevel@tonic-gate  */
2377c478bd9Sstevel@tonic-gate struct fn_list *
2387c478bd9Sstevel@tonic-gate opts_cmdargs(struct opts *opts)
2397c478bd9Sstevel@tonic-gate {
2407c478bd9Sstevel@tonic-gate 	return (opts->op_cmdargs);
2417c478bd9Sstevel@tonic-gate }
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate static void
2447c478bd9Sstevel@tonic-gate merger(const char *lhs, void *rhs, void *arg)
2457c478bd9Sstevel@tonic-gate {
2467c478bd9Sstevel@tonic-gate 	struct lut **destlutp = (struct lut **)arg;
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 	*destlutp = lut_add(*destlutp, lhs, rhs);
2497c478bd9Sstevel@tonic-gate }
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate /*
2527c478bd9Sstevel@tonic-gate  * opts_merge -- merge two option lists together
2537c478bd9Sstevel@tonic-gate  */
2547c478bd9Sstevel@tonic-gate struct opts *
2557c478bd9Sstevel@tonic-gate opts_merge(struct opts *back, struct opts *front)
2567c478bd9Sstevel@tonic-gate {
2577c478bd9Sstevel@tonic-gate 	struct opts *ret = MALLOC(sizeof (struct opts));
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate 	ret->op_raw = lut_dup(back->op_raw);
2607c478bd9Sstevel@tonic-gate 	lut_walk(front->op_raw, merger, &(ret->op_raw));
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate 	ret->op_ints = lut_dup(back->op_ints);
2637c478bd9Sstevel@tonic-gate 	lut_walk(front->op_ints, merger, &(ret->op_ints));
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 	ret->op_cmdargs = fn_list_dup(back->op_cmdargs);
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 	return (ret);
2687c478bd9Sstevel@tonic-gate }
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate /*
2717c478bd9Sstevel@tonic-gate  * opts_parse_ctime -- parse a ctime format optarg
2727c478bd9Sstevel@tonic-gate  */
2737c478bd9Sstevel@tonic-gate int
2747c478bd9Sstevel@tonic-gate opts_parse_ctime(const char *o, const char *optarg)
2757c478bd9Sstevel@tonic-gate {
2767c478bd9Sstevel@tonic-gate 	struct tm tm;
2777c478bd9Sstevel@tonic-gate 	int ret;
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate 	if (strptime(optarg, "%a %b %e %T %Z %Y", &tm) == NULL &&
2807c478bd9Sstevel@tonic-gate 	    strptime(optarg, "%c", &tm) == NULL)
2817c478bd9Sstevel@tonic-gate 		err(EF_FILE|EF_JMP,
2827c478bd9Sstevel@tonic-gate 		    "Option '%c' requires ctime-style time", *o);
2837c478bd9Sstevel@tonic-gate 	errno = 0;
2847c478bd9Sstevel@tonic-gate 	if ((ret = (int)mktime(&tm)) == -1 && errno)
2857c478bd9Sstevel@tonic-gate 		err(EF_FILE|EF_SYS|EF_JMP, "Option '%c' Illegal time", *o);
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 	return (ret);
2887c478bd9Sstevel@tonic-gate }
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate /*
2917c478bd9Sstevel@tonic-gate  * opts_parse_atopi -- parse a positive integer format optarg
2927c478bd9Sstevel@tonic-gate  */
2937c478bd9Sstevel@tonic-gate int
2947c478bd9Sstevel@tonic-gate opts_parse_atopi(const char *o, const char *optarg)
2957c478bd9Sstevel@tonic-gate {
2967c478bd9Sstevel@tonic-gate 	int ret = atoi(optarg);
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 	while (isdigit(*optarg))
2997c478bd9Sstevel@tonic-gate 		optarg++;
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 	if (*optarg)
3027c478bd9Sstevel@tonic-gate 		err(EF_FILE|EF_JMP,
3037c478bd9Sstevel@tonic-gate 		    "Option '%c' requires non-negative number", *o);
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate 	return (ret);
3067c478bd9Sstevel@tonic-gate }
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate /*
3097c478bd9Sstevel@tonic-gate  * opts_parse_atopi -- parse a size format optarg into bytes
3107c478bd9Sstevel@tonic-gate  */
3117c478bd9Sstevel@tonic-gate int
3127c478bd9Sstevel@tonic-gate opts_parse_bytes(const char *o, const char *optarg)
3137c478bd9Sstevel@tonic-gate {
3147c478bd9Sstevel@tonic-gate 	int ret = atoi(optarg);
3157c478bd9Sstevel@tonic-gate 	while (isdigit(*optarg))
3167c478bd9Sstevel@tonic-gate 		optarg++;
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate 	switch (*optarg) {
3197c478bd9Sstevel@tonic-gate 	case 'g':
3207c478bd9Sstevel@tonic-gate 	case 'G':
3217c478bd9Sstevel@tonic-gate 		ret *= 1024;
3227c478bd9Sstevel@tonic-gate 		/*FALLTHROUGH*/
3237c478bd9Sstevel@tonic-gate 	case 'm':
3247c478bd9Sstevel@tonic-gate 	case 'M':
3257c478bd9Sstevel@tonic-gate 		ret *= 1024;
3267c478bd9Sstevel@tonic-gate 		/*FALLTHROUGH*/
3277c478bd9Sstevel@tonic-gate 	case 'k':
3287c478bd9Sstevel@tonic-gate 	case 'K':
3297c478bd9Sstevel@tonic-gate 		ret *= 1024;
3307c478bd9Sstevel@tonic-gate 		/*FALLTHROUGH*/
3317c478bd9Sstevel@tonic-gate 	case 'b':
3327c478bd9Sstevel@tonic-gate 	case 'B':
3337c478bd9Sstevel@tonic-gate 		if (optarg[1] == '\0')
3347c478bd9Sstevel@tonic-gate 			return (ret);
3357c478bd9Sstevel@tonic-gate 	}
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate 	err(EF_FILE|EF_JMP,
3387c478bd9Sstevel@tonic-gate 	    "Option '%c' requires number with suffix from [bkmg]", *o);
3397c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
340*78eb75caSchin 	return (0);
3417c478bd9Sstevel@tonic-gate }
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate /*
3447c478bd9Sstevel@tonic-gate  * opts_parse_seconds -- parse a time format optarg into seconds
3457c478bd9Sstevel@tonic-gate  */
3467c478bd9Sstevel@tonic-gate int
3477c478bd9Sstevel@tonic-gate opts_parse_seconds(const char *o, const char *optarg)
3487c478bd9Sstevel@tonic-gate {
3497c478bd9Sstevel@tonic-gate 	int ret;
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 	if (strcasecmp(optarg, "now") == 0)
3527c478bd9Sstevel@tonic-gate 		return (OPTP_NOW);
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate 	if (strcasecmp(optarg, "never") == 0)
3557c478bd9Sstevel@tonic-gate 		return (OPTP_NEVER);
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate 	ret = atoi(optarg);
3587c478bd9Sstevel@tonic-gate 	while (isdigit(*optarg))
3597c478bd9Sstevel@tonic-gate 		optarg++;
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate 	if (optarg[1] == '\0')
3627c478bd9Sstevel@tonic-gate 		switch (*optarg) {
3637c478bd9Sstevel@tonic-gate 		case 'h':
3647c478bd9Sstevel@tonic-gate 		case 'H':
3657c478bd9Sstevel@tonic-gate 			ret *= 60 * 60;
3667c478bd9Sstevel@tonic-gate 			return (ret);
3677c478bd9Sstevel@tonic-gate 		case 'd':
3687c478bd9Sstevel@tonic-gate 		case 'D':
3697c478bd9Sstevel@tonic-gate 			ret *= 60 * 60 * 24;
3707c478bd9Sstevel@tonic-gate 			return (ret);
3717c478bd9Sstevel@tonic-gate 		case 'w':
3727c478bd9Sstevel@tonic-gate 		case 'W':
3737c478bd9Sstevel@tonic-gate 			ret *= 60 * 60 * 24 * 7;
3747c478bd9Sstevel@tonic-gate 			return (ret);
3757c478bd9Sstevel@tonic-gate 		case 'm':
3767c478bd9Sstevel@tonic-gate 		case 'M':
3777c478bd9Sstevel@tonic-gate 			ret *= 60 * 60 * 24 * 30;
3787c478bd9Sstevel@tonic-gate 			return (ret);
3797c478bd9Sstevel@tonic-gate 		case 'y':
3807c478bd9Sstevel@tonic-gate 		case 'Y':
3817c478bd9Sstevel@tonic-gate 			ret *= 60 * 60 * 24 * 365;
3827c478bd9Sstevel@tonic-gate 			return (ret);
3837c478bd9Sstevel@tonic-gate 		}
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate 	err(EF_FILE|EF_JMP,
3867c478bd9Sstevel@tonic-gate 	    "Option '%c' requires number with suffix from [hdwmy]", *o);
3877c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
388*78eb75caSchin 	return (0);
3897c478bd9Sstevel@tonic-gate }
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate /* info passed between opts_print() and printer() */
392*78eb75caSchin struct printerinfo {
3937c478bd9Sstevel@tonic-gate 	FILE *stream;
3947c478bd9Sstevel@tonic-gate 	int isswitch;
3957c478bd9Sstevel@tonic-gate 	char *exclude;
3967c478bd9Sstevel@tonic-gate };
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate /* helper function for opts_print() */
3997c478bd9Sstevel@tonic-gate static void
4007c478bd9Sstevel@tonic-gate printer(const char *lhs, void *rhs, void *arg)
4017c478bd9Sstevel@tonic-gate {
4027c478bd9Sstevel@tonic-gate 	struct printerinfo *pip = (struct printerinfo *)arg;
4037c478bd9Sstevel@tonic-gate 	char *s = (char *)rhs;
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate 	if (pip->isswitch) {
4067c478bd9Sstevel@tonic-gate 		char *ep = pip->exclude;
4077c478bd9Sstevel@tonic-gate 		while (ep && *ep)
4087c478bd9Sstevel@tonic-gate 			if (*ep++ == *lhs)
4097c478bd9Sstevel@tonic-gate 				return;
4107c478bd9Sstevel@tonic-gate 	}
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate 	(void) fprintf(pip->stream, " %s%s", (pip->isswitch) ? "-" : "", lhs);
4137c478bd9Sstevel@tonic-gate 	if (s && *s) {
4147c478bd9Sstevel@tonic-gate 		(void) fprintf(pip->stream, " ");
4157c478bd9Sstevel@tonic-gate 		opts_printword(s, pip->stream);
4167c478bd9Sstevel@tonic-gate 	}
4177c478bd9Sstevel@tonic-gate }
4187c478bd9Sstevel@tonic-gate 
4197c478bd9Sstevel@tonic-gate /*
4207c478bd9Sstevel@tonic-gate  * opts_printword -- print a word, quoting as necessary
4217c478bd9Sstevel@tonic-gate  */
4227c478bd9Sstevel@tonic-gate void
4237c478bd9Sstevel@tonic-gate opts_printword(const char *word, FILE *stream)
4247c478bd9Sstevel@tonic-gate {
4257c478bd9Sstevel@tonic-gate 	char *q = "";
4267c478bd9Sstevel@tonic-gate 
4277c478bd9Sstevel@tonic-gate 	if (strchr(word, ' ') || strchr(word, '\t') ||
4287c478bd9Sstevel@tonic-gate 	    strchr(word, '$') || strchr(word, '[') ||
4297c478bd9Sstevel@tonic-gate 	    strchr(word, '?') || strchr(word, '{') ||
4307c478bd9Sstevel@tonic-gate 	    strchr(word, '`') || strchr(word, ';')) {
4317c478bd9Sstevel@tonic-gate 		if (strchr(word, '\''))
4327c478bd9Sstevel@tonic-gate 			q = "\"";
4337c478bd9Sstevel@tonic-gate 		else if (strchr(word, '"'))
4347c478bd9Sstevel@tonic-gate 			err(EF_FILE|EF_JMP, "Can't protect quotes in <%s>",
4357c478bd9Sstevel@tonic-gate 			    word);
4367c478bd9Sstevel@tonic-gate 		else
4377c478bd9Sstevel@tonic-gate 			q = "'";
4387c478bd9Sstevel@tonic-gate 		(void) fprintf(stream, "%s%s%s", q, word, q);
4397c478bd9Sstevel@tonic-gate 	} else
4407c478bd9Sstevel@tonic-gate 		(void) fprintf(stream, "%s", word);
4417c478bd9Sstevel@tonic-gate }
4427c478bd9Sstevel@tonic-gate 
4437c478bd9Sstevel@tonic-gate /*
4447c478bd9Sstevel@tonic-gate  * opts_print -- print options to stream, leaving out those in "exclude"
4457c478bd9Sstevel@tonic-gate  */
4467c478bd9Sstevel@tonic-gate void
4477c478bd9Sstevel@tonic-gate opts_print(struct opts *opts, FILE *stream, char *exclude)
4487c478bd9Sstevel@tonic-gate {
4497c478bd9Sstevel@tonic-gate 	struct printerinfo pi;
4507c478bd9Sstevel@tonic-gate 	struct fn *fnp;
4517c478bd9Sstevel@tonic-gate 
4527c478bd9Sstevel@tonic-gate 	pi.stream = stream;
4537c478bd9Sstevel@tonic-gate 	pi.isswitch = 1;
4547c478bd9Sstevel@tonic-gate 	pi.exclude = exclude;
4557c478bd9Sstevel@tonic-gate 
4567c478bd9Sstevel@tonic-gate 	lut_walk(opts->op_raw, printer, &pi);
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate 	fn_list_rewind(opts->op_cmdargs);
4597c478bd9Sstevel@tonic-gate 	while ((fnp = fn_list_next(opts->op_cmdargs)) != NULL) {
4607c478bd9Sstevel@tonic-gate 		(void) fprintf(stream, " ");
4617c478bd9Sstevel@tonic-gate 		opts_printword(fn_s(fnp), stream);
4627c478bd9Sstevel@tonic-gate 	}
4637c478bd9Sstevel@tonic-gate }
4647c478bd9Sstevel@tonic-gate 
4657c478bd9Sstevel@tonic-gate #ifdef	TESTMODULE
4667c478bd9Sstevel@tonic-gate 
4677c478bd9Sstevel@tonic-gate /* table that drives argument parsing */
4687c478bd9Sstevel@tonic-gate static struct optinfo Opttable[] = {
4697c478bd9Sstevel@tonic-gate 	{ "a", OPTTYPE_BOOLEAN,	NULL,			OPTF_CLI },
4707c478bd9Sstevel@tonic-gate 	{ "b", OPTTYPE_STRING,	NULL,			OPTF_CLI },
4717c478bd9Sstevel@tonic-gate 	{ "c", OPTTYPE_INT,	opts_parse_seconds,	OPTF_CLI|OPTF_CONF },
4727c478bd9Sstevel@tonic-gate 	{ "d", OPTTYPE_INT,	opts_parse_ctime,	OPTF_CLI|OPTF_CONF },
4737c478bd9Sstevel@tonic-gate 	{ "e", OPTTYPE_INT,	opts_parse_bytes,	OPTF_CLI|OPTF_CONF },
4747c478bd9Sstevel@tonic-gate 	{ "f", OPTTYPE_INT,	opts_parse_atopi,	OPTF_CLI|OPTF_CONF },
4757c478bd9Sstevel@tonic-gate };
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate /*
4787c478bd9Sstevel@tonic-gate  * test main for opts module, usage: a.out options...
4797c478bd9Sstevel@tonic-gate  */
4807c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
4817c478bd9Sstevel@tonic-gate {
4827c478bd9Sstevel@tonic-gate 	struct opts *opts;
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate 	err_init(argv[0]);
4857c478bd9Sstevel@tonic-gate 	setbuf(stdout, NULL);
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate 	opts_init(Opttable, sizeof (Opttable) / sizeof (struct optinfo));
4887c478bd9Sstevel@tonic-gate 
4897c478bd9Sstevel@tonic-gate 	argv++;
4907c478bd9Sstevel@tonic-gate 
4917c478bd9Sstevel@tonic-gate 	if (SETJMP)
4927c478bd9Sstevel@tonic-gate 		err(0, "opts parsing failed");
4937c478bd9Sstevel@tonic-gate 	else
4947c478bd9Sstevel@tonic-gate 		opts = opts_parse(argv, OPTF_CLI);
4957c478bd9Sstevel@tonic-gate 
4967c478bd9Sstevel@tonic-gate 	printf("options:");
4977c478bd9Sstevel@tonic-gate 	opts_print(opts, stdout, NULL);
4987c478bd9Sstevel@tonic-gate 	printf("\n");
4997c478bd9Sstevel@tonic-gate 
5007c478bd9Sstevel@tonic-gate 	err_done(0);
5017c478bd9Sstevel@tonic-gate }
5027c478bd9Sstevel@tonic-gate 
5037c478bd9Sstevel@tonic-gate #endif	/* TESTMODULE */
504