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