xref: /illumos-gate/usr/src/cmd/logadm/opts.c (revision 509030e0)
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
5404720a7Sbasabi  * Common Development and Distribution License (the "License").
6404720a7Sbasabi  * 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 /*
23e9a193fcSJohn.Zolnowsky@Sun.COM  * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
2678eb75caSchin /*
2778eb75caSchin  * logadm/opts.c -- options handling routines
2878eb75caSchin  */
2978eb75caSchin 
307c478bd9Sstevel@tonic-gate #include <stdio.h>
317c478bd9Sstevel@tonic-gate #include <libintl.h>
327c478bd9Sstevel@tonic-gate #include <stdlib.h>
337c478bd9Sstevel@tonic-gate #include <ctype.h>
347c478bd9Sstevel@tonic-gate #include <strings.h>
357c478bd9Sstevel@tonic-gate #include <time.h>
367c478bd9Sstevel@tonic-gate #include <sys/types.h>
377c478bd9Sstevel@tonic-gate #include <sys/stat.h>
387c478bd9Sstevel@tonic-gate #include <errno.h>
397c478bd9Sstevel@tonic-gate #include "err.h"
407c478bd9Sstevel@tonic-gate #include "lut.h"
417c478bd9Sstevel@tonic-gate #include "fn.h"
427c478bd9Sstevel@tonic-gate #include "opts.h"
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate /* forward declarations for private functions */
457c478bd9Sstevel@tonic-gate static struct optinfo *opt_info(int c);
467c478bd9Sstevel@tonic-gate static void opts_setcmdarg(struct opts *opts, const char *cmdarg);
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate /* info created by opts_parse(), private to this module */
497c478bd9Sstevel@tonic-gate struct opts {
507c478bd9Sstevel@tonic-gate 	struct lut *op_raw;		/* the raw text for the options */
517c478bd9Sstevel@tonic-gate 	struct lut *op_ints;		/* the int values for the options */
527c478bd9Sstevel@tonic-gate 	struct fn_list *op_cmdargs;	/* the op_cmdargs */
537c478bd9Sstevel@tonic-gate };
547c478bd9Sstevel@tonic-gate 
55e9a193fcSJohn.Zolnowsky@Sun.COM static off_t opts_parse_ctime(const char *o, const char *optarg);
56e9a193fcSJohn.Zolnowsky@Sun.COM static off_t opts_parse_bytes(const char *o, const char *optarg);
57e9a193fcSJohn.Zolnowsky@Sun.COM static off_t opts_parse_atopi(const char *o, const char *optarg);
58e9a193fcSJohn.Zolnowsky@Sun.COM static off_t opts_parse_seconds(const char *o, const char *optarg);
59e9a193fcSJohn.Zolnowsky@Sun.COM 
607c478bd9Sstevel@tonic-gate static struct lut *Info;		/* table driving parsing */
617c478bd9Sstevel@tonic-gate 
62e9a193fcSJohn.Zolnowsky@Sun.COM /* table that drives argument parsing */
63e9a193fcSJohn.Zolnowsky@Sun.COM struct optinfo Opttable[] = {
64e9a193fcSJohn.Zolnowsky@Sun.COM 	{ "e", OPTTYPE_STRING,	NULL,			OPTF_CLI|OPTF_CONF },
65e9a193fcSJohn.Zolnowsky@Sun.COM 	{ "F", OPTTYPE_STRING,	NULL,			OPTF_CLI },
66e9a193fcSJohn.Zolnowsky@Sun.COM 	{ "f", OPTTYPE_STRING,	NULL,			OPTF_CLI },
67e9a193fcSJohn.Zolnowsky@Sun.COM 	{ "h", OPTTYPE_BOOLEAN,	NULL,			OPTF_CLI },
68e9a193fcSJohn.Zolnowsky@Sun.COM 	{ "l", OPTTYPE_BOOLEAN, NULL,			OPTF_CLI|OPTF_CONF },
69e9a193fcSJohn.Zolnowsky@Sun.COM 	{ "N", OPTTYPE_BOOLEAN,	NULL,			OPTF_CLI|OPTF_CONF },
70e9a193fcSJohn.Zolnowsky@Sun.COM 	{ "n", OPTTYPE_BOOLEAN,	NULL,			OPTF_CLI },
71e9a193fcSJohn.Zolnowsky@Sun.COM 	{ "r", OPTTYPE_BOOLEAN,	NULL,			OPTF_CLI },
72e9a193fcSJohn.Zolnowsky@Sun.COM 	{ "V", OPTTYPE_BOOLEAN,	NULL,			OPTF_CLI },
73e9a193fcSJohn.Zolnowsky@Sun.COM 	{ "v", OPTTYPE_BOOLEAN,	NULL,			OPTF_CLI },
74e9a193fcSJohn.Zolnowsky@Sun.COM 	{ "w", OPTTYPE_STRING,	NULL,			OPTF_CLI },
75e9a193fcSJohn.Zolnowsky@Sun.COM 	{ "p", OPTTYPE_INT,	opts_parse_seconds,	OPTF_CLI|OPTF_CONF },
76e9a193fcSJohn.Zolnowsky@Sun.COM 	{ "P", OPTTYPE_INT,	opts_parse_ctime,	OPTF_CLI|OPTF_CONF },
77e9a193fcSJohn.Zolnowsky@Sun.COM 	{ "s", OPTTYPE_INT,	opts_parse_bytes,	OPTF_CLI|OPTF_CONF },
78e9a193fcSJohn.Zolnowsky@Sun.COM 	{ "a", OPTTYPE_STRING,	NULL,			OPTF_CLI|OPTF_CONF },
79e9a193fcSJohn.Zolnowsky@Sun.COM 	{ "b", OPTTYPE_STRING,	NULL,			OPTF_CLI|OPTF_CONF },
80e9a193fcSJohn.Zolnowsky@Sun.COM 	{ "c", OPTTYPE_BOOLEAN, NULL,			OPTF_CLI|OPTF_CONF },
81e9a193fcSJohn.Zolnowsky@Sun.COM 	{ "g", OPTTYPE_STRING,	NULL,			OPTF_CLI|OPTF_CONF },
82e9a193fcSJohn.Zolnowsky@Sun.COM 	{ "m", OPTTYPE_INT,	opts_parse_atopi,	OPTF_CLI|OPTF_CONF },
83e9a193fcSJohn.Zolnowsky@Sun.COM 	{ "M", OPTTYPE_STRING,	NULL,			OPTF_CLI|OPTF_CONF },
84e9a193fcSJohn.Zolnowsky@Sun.COM 	{ "o", OPTTYPE_STRING,	NULL,			OPTF_CLI|OPTF_CONF },
85e9a193fcSJohn.Zolnowsky@Sun.COM 	{ "R", OPTTYPE_STRING,	NULL,			OPTF_CLI|OPTF_CONF },
86e9a193fcSJohn.Zolnowsky@Sun.COM 	{ "t", OPTTYPE_STRING,	NULL,			OPTF_CLI|OPTF_CONF },
87e9a193fcSJohn.Zolnowsky@Sun.COM 	{ "z", OPTTYPE_INT,	opts_parse_atopi,	OPTF_CLI|OPTF_CONF },
88e9a193fcSJohn.Zolnowsky@Sun.COM 	{ "A", OPTTYPE_INT,	opts_parse_seconds,	OPTF_CLI|OPTF_CONF },
89e9a193fcSJohn.Zolnowsky@Sun.COM 	{ "C", OPTTYPE_INT,	opts_parse_atopi,	OPTF_CLI|OPTF_CONF },
90e9a193fcSJohn.Zolnowsky@Sun.COM 	{ "E", OPTTYPE_STRING,	NULL,			OPTF_CLI|OPTF_CONF },
91e9a193fcSJohn.Zolnowsky@Sun.COM 	{ "S", OPTTYPE_INT,	opts_parse_bytes,	OPTF_CLI|OPTF_CONF },
92e9a193fcSJohn.Zolnowsky@Sun.COM 	{ "T", OPTTYPE_STRING,	NULL,			OPTF_CLI|OPTF_CONF },
93e9a193fcSJohn.Zolnowsky@Sun.COM };
94e9a193fcSJohn.Zolnowsky@Sun.COM 
95e9a193fcSJohn.Zolnowsky@Sun.COM int Opttable_cnt = sizeof (Opttable) / sizeof (struct optinfo);
96e9a193fcSJohn.Zolnowsky@Sun.COM 
977c478bd9Sstevel@tonic-gate /*
987c478bd9Sstevel@tonic-gate  * opts_init -- set current options parsing table
997c478bd9Sstevel@tonic-gate  */
1007c478bd9Sstevel@tonic-gate void
opts_init(struct optinfo * table,int numentries)1017c478bd9Sstevel@tonic-gate opts_init(struct optinfo *table, int numentries)
1027c478bd9Sstevel@tonic-gate {
1037c478bd9Sstevel@tonic-gate 	while (numentries-- > 0) {
1047c478bd9Sstevel@tonic-gate 		Info = lut_add(Info, table->oi_o, table);
1057c478bd9Sstevel@tonic-gate 		table++;
1067c478bd9Sstevel@tonic-gate 	}
1077c478bd9Sstevel@tonic-gate }
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate /*
1107c478bd9Sstevel@tonic-gate  * opt_info -- fetch the optinfo struct for the given option
1117c478bd9Sstevel@tonic-gate  */
1127c478bd9Sstevel@tonic-gate static struct optinfo *
opt_info(int c)1137c478bd9Sstevel@tonic-gate opt_info(int c)
1147c478bd9Sstevel@tonic-gate {
1157c478bd9Sstevel@tonic-gate 	char lhs[2];
1167c478bd9Sstevel@tonic-gate 	lhs[0] = c;
1177c478bd9Sstevel@tonic-gate 	lhs[1] = '\0';
1187c478bd9Sstevel@tonic-gate 	return ((struct optinfo *)lut_lookup(Info, lhs));
1197c478bd9Sstevel@tonic-gate }
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate /*
1227c478bd9Sstevel@tonic-gate  * opts_parse -- parse an argv-style list of options
1237c478bd9Sstevel@tonic-gate  *
1247c478bd9Sstevel@tonic-gate  * prints a message to stderr and calls err(EF_FILE|EF_JMP, ...) on error
1257c478bd9Sstevel@tonic-gate  */
1267c478bd9Sstevel@tonic-gate struct opts *
opts_parse(struct opts * opts,char ** argv,int flags)127e9a193fcSJohn.Zolnowsky@Sun.COM opts_parse(struct opts *opts, char **argv, int flags)
1287c478bd9Sstevel@tonic-gate {
1297c478bd9Sstevel@tonic-gate 	int dashdash = 0;
1307c478bd9Sstevel@tonic-gate 	char *ptr;
1317c478bd9Sstevel@tonic-gate 
132e9a193fcSJohn.Zolnowsky@Sun.COM 	if (opts == NULL) {
133e9a193fcSJohn.Zolnowsky@Sun.COM 		opts = MALLOC(sizeof (*opts));
134e9a193fcSJohn.Zolnowsky@Sun.COM 		opts->op_raw = opts->op_ints = NULL;
135e9a193fcSJohn.Zolnowsky@Sun.COM 		opts->op_cmdargs = fn_list_new(NULL);
136e9a193fcSJohn.Zolnowsky@Sun.COM 	}
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 	/* no words to process, just return empty opts struct */
1397c478bd9Sstevel@tonic-gate 	if (argv == NULL)
140e9a193fcSJohn.Zolnowsky@Sun.COM 		return (opts);
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate 	/* foreach word... */
1437c478bd9Sstevel@tonic-gate 	for (; (ptr = *argv) != NULL; argv++) {
1447c478bd9Sstevel@tonic-gate 		if (dashdash || *ptr != '-') {
1457c478bd9Sstevel@tonic-gate 			/* found a cmdarg */
146e9a193fcSJohn.Zolnowsky@Sun.COM 			opts_setcmdarg(opts, ptr);
1477c478bd9Sstevel@tonic-gate 			continue;
1487c478bd9Sstevel@tonic-gate 		}
1497c478bd9Sstevel@tonic-gate 		if (*++ptr == '\0')
1507c478bd9Sstevel@tonic-gate 			err(EF_FILE|EF_JMP, "Illegal option: dash by itself");
1517c478bd9Sstevel@tonic-gate 		if (*ptr == '-') {
1527c478bd9Sstevel@tonic-gate 			/* (here's where support for --longname would go) */
1537c478bd9Sstevel@tonic-gate 			if (*(ptr + 1) != '\0')
1547c478bd9Sstevel@tonic-gate 				err(EF_FILE|EF_JMP, "Illegal option: -%s", ptr);
1557c478bd9Sstevel@tonic-gate 			dashdash++;
1567c478bd9Sstevel@tonic-gate 			continue;
1577c478bd9Sstevel@tonic-gate 		}
1587c478bd9Sstevel@tonic-gate 		for (; *ptr; ptr++) {
1597c478bd9Sstevel@tonic-gate 			struct optinfo *info = opt_info(*ptr);
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 			/* see if option was in our parsing table */
1627c478bd9Sstevel@tonic-gate 			if (info == NULL)
1637c478bd9Sstevel@tonic-gate 				err(EF_FILE|EF_JMP, "Illegal option: %c", *ptr);
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 			/* see if context allows this option */
1667c478bd9Sstevel@tonic-gate 			if ((flags & OPTF_CLI) &&
1677c478bd9Sstevel@tonic-gate 			    (info->oi_flags & OPTF_CLI) == 0)
1687c478bd9Sstevel@tonic-gate 				err(EF_FILE|EF_JMP,
1697c478bd9Sstevel@tonic-gate 				    "Option '%c' not allowed on "
1707c478bd9Sstevel@tonic-gate 				    "command line", *ptr);
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate 			if ((flags & OPTF_CONF) &&
1737c478bd9Sstevel@tonic-gate 			    (info->oi_flags & OPTF_CONF) == 0)
1747c478bd9Sstevel@tonic-gate 				err(EF_FILE|EF_JMP,
1757c478bd9Sstevel@tonic-gate 				    "Option '%c' not allowed in "
1767c478bd9Sstevel@tonic-gate 				    "configuration file", *ptr);
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate 			/* for boolean options, we have all the info we need */
1797c478bd9Sstevel@tonic-gate 			if (info->oi_t == OPTTYPE_BOOLEAN) {
180e9a193fcSJohn.Zolnowsky@Sun.COM 				(void) opts_set(opts, info->oi_o, "");
1817c478bd9Sstevel@tonic-gate 				continue;
1827c478bd9Sstevel@tonic-gate 			}
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 			/* option expects argument */
1857c478bd9Sstevel@tonic-gate 			if (*++ptr == '\0' &&
1867c478bd9Sstevel@tonic-gate 			    ((ptr = *++argv) == NULL || *ptr == '-'))
1877c478bd9Sstevel@tonic-gate 				err(EF_FILE|EF_JMP,
1887c478bd9Sstevel@tonic-gate 				    "Option '%c' requires an argument",
1897c478bd9Sstevel@tonic-gate 				    info->oi_o[0]);
190e9a193fcSJohn.Zolnowsky@Sun.COM 			opts_set(opts, info->oi_o, ptr);
1917c478bd9Sstevel@tonic-gate 			break;
1927c478bd9Sstevel@tonic-gate 		}
1937c478bd9Sstevel@tonic-gate 	}
1947c478bd9Sstevel@tonic-gate 
195e9a193fcSJohn.Zolnowsky@Sun.COM 	return (opts);
1967c478bd9Sstevel@tonic-gate }
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate /*
1997c478bd9Sstevel@tonic-gate  * opts_free -- free a struct opts previously allocated by opts_parse()
2007c478bd9Sstevel@tonic-gate  */
2017c478bd9Sstevel@tonic-gate void
opts_free(struct opts * opts)2027c478bd9Sstevel@tonic-gate opts_free(struct opts *opts)
2037c478bd9Sstevel@tonic-gate {
2047c478bd9Sstevel@tonic-gate 	if (opts) {
2057c478bd9Sstevel@tonic-gate 		lut_free(opts->op_raw, NULL);
2067c478bd9Sstevel@tonic-gate 		lut_free(opts->op_ints, NULL);
2077c478bd9Sstevel@tonic-gate 		fn_list_free(opts->op_cmdargs);
2087c478bd9Sstevel@tonic-gate 		FREE(opts);
2097c478bd9Sstevel@tonic-gate 	}
2107c478bd9Sstevel@tonic-gate }
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate /*
2137c478bd9Sstevel@tonic-gate  * opts_set -- set an option
2147c478bd9Sstevel@tonic-gate  */
2157c478bd9Sstevel@tonic-gate void
opts_set(struct opts * opts,const char * o,const char * optarg)2167c478bd9Sstevel@tonic-gate opts_set(struct opts *opts, const char *o, const char *optarg)
2177c478bd9Sstevel@tonic-gate {
218404720a7Sbasabi 	off_t *rval;
2197c478bd9Sstevel@tonic-gate 	struct optinfo *info = opt_info(*o);
2207c478bd9Sstevel@tonic-gate 
221404720a7Sbasabi 	rval = MALLOC(sizeof (off_t));
2227c478bd9Sstevel@tonic-gate 	opts->op_raw = lut_add(opts->op_raw, o, (void *)optarg);
2237c478bd9Sstevel@tonic-gate 
224404720a7Sbasabi 	if (info->oi_parser) {
225404720a7Sbasabi 		*rval = (*info->oi_parser)(o, optarg);
226404720a7Sbasabi 		opts->op_ints = lut_add(opts->op_ints, o, (void *)rval);
227404720a7Sbasabi 	}
2287c478bd9Sstevel@tonic-gate }
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate /*
2317c478bd9Sstevel@tonic-gate  * opts_setcmdarg -- add a cmdarg to the list of op_cmdargs
2327c478bd9Sstevel@tonic-gate  */
2337c478bd9Sstevel@tonic-gate static void
opts_setcmdarg(struct opts * opts,const char * cmdarg)2347c478bd9Sstevel@tonic-gate opts_setcmdarg(struct opts *opts, const char *cmdarg)
2357c478bd9Sstevel@tonic-gate {
2367c478bd9Sstevel@tonic-gate 	fn_list_adds(opts->op_cmdargs, cmdarg);
2377c478bd9Sstevel@tonic-gate }
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate /*
2407c478bd9Sstevel@tonic-gate  * opts_count -- return count of the options in *options that are set
2417c478bd9Sstevel@tonic-gate  */
2427c478bd9Sstevel@tonic-gate int
opts_count(struct opts * opts,const char * options)2437c478bd9Sstevel@tonic-gate opts_count(struct opts *opts, const char *options)
2447c478bd9Sstevel@tonic-gate {
2457c478bd9Sstevel@tonic-gate 	int count = 0;
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate 	for (; *options; options++) {
2487c478bd9Sstevel@tonic-gate 		char lhs[2];
2497c478bd9Sstevel@tonic-gate 		lhs[0] = *options;
2507c478bd9Sstevel@tonic-gate 		lhs[1] = '\0';
2517c478bd9Sstevel@tonic-gate 		if (lut_lookup(opts->op_raw, lhs))
2527c478bd9Sstevel@tonic-gate 			count++;
2537c478bd9Sstevel@tonic-gate 	}
2547c478bd9Sstevel@tonic-gate 	return (count);
2557c478bd9Sstevel@tonic-gate }
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate /*
2587c478bd9Sstevel@tonic-gate  * opts_optarg -- return the optarg for the given option, NULL if not set
2597c478bd9Sstevel@tonic-gate  */
2607c478bd9Sstevel@tonic-gate const char *
opts_optarg(struct opts * opts,const char * o)2617c478bd9Sstevel@tonic-gate opts_optarg(struct opts *opts, const char *o)
2627c478bd9Sstevel@tonic-gate {
2637c478bd9Sstevel@tonic-gate 	return ((char *)lut_lookup(opts->op_raw, o));
2647c478bd9Sstevel@tonic-gate }
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate /*
2677c478bd9Sstevel@tonic-gate  * opts_optarg_int -- return the int value for the given option
2687c478bd9Sstevel@tonic-gate  */
269404720a7Sbasabi off_t
opts_optarg_int(struct opts * opts,const char * o)2707c478bd9Sstevel@tonic-gate opts_optarg_int(struct opts *opts, const char *o)
2717c478bd9Sstevel@tonic-gate {
272404720a7Sbasabi 	off_t	*ret;
273404720a7Sbasabi 
274404720a7Sbasabi 	ret = (off_t *)lut_lookup(opts->op_ints, o);
275404720a7Sbasabi 	if (ret != NULL)
276404720a7Sbasabi 		return (*ret);
277404720a7Sbasabi 	return (0);
2787c478bd9Sstevel@tonic-gate }
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate /*
2817c478bd9Sstevel@tonic-gate  * opts_cmdargs -- return list of op_cmdargs
2827c478bd9Sstevel@tonic-gate  */
2837c478bd9Sstevel@tonic-gate struct fn_list *
opts_cmdargs(struct opts * opts)2847c478bd9Sstevel@tonic-gate opts_cmdargs(struct opts *opts)
2857c478bd9Sstevel@tonic-gate {
2867c478bd9Sstevel@tonic-gate 	return (opts->op_cmdargs);
2877c478bd9Sstevel@tonic-gate }
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate static void
merger(const char * lhs,void * rhs,void * arg)2907c478bd9Sstevel@tonic-gate merger(const char *lhs, void *rhs, void *arg)
2917c478bd9Sstevel@tonic-gate {
2927c478bd9Sstevel@tonic-gate 	struct lut **destlutp = (struct lut **)arg;
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate 	*destlutp = lut_add(*destlutp, lhs, rhs);
2957c478bd9Sstevel@tonic-gate }
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate /*
2987c478bd9Sstevel@tonic-gate  * opts_merge -- merge two option lists together
2997c478bd9Sstevel@tonic-gate  */
3007c478bd9Sstevel@tonic-gate struct opts *
opts_merge(struct opts * back,struct opts * front)3017c478bd9Sstevel@tonic-gate opts_merge(struct opts *back, struct opts *front)
3027c478bd9Sstevel@tonic-gate {
3037c478bd9Sstevel@tonic-gate 	struct opts *ret = MALLOC(sizeof (struct opts));
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate 	ret->op_raw = lut_dup(back->op_raw);
3067c478bd9Sstevel@tonic-gate 	lut_walk(front->op_raw, merger, &(ret->op_raw));
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate 	ret->op_ints = lut_dup(back->op_ints);
3097c478bd9Sstevel@tonic-gate 	lut_walk(front->op_ints, merger, &(ret->op_ints));
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 	ret->op_cmdargs = fn_list_dup(back->op_cmdargs);
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate 	return (ret);
3147c478bd9Sstevel@tonic-gate }
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate /*
3177c478bd9Sstevel@tonic-gate  * opts_parse_ctime -- parse a ctime format optarg
3187c478bd9Sstevel@tonic-gate  */
319e9a193fcSJohn.Zolnowsky@Sun.COM static off_t
opts_parse_ctime(const char * o,const char * optarg)3207c478bd9Sstevel@tonic-gate opts_parse_ctime(const char *o, const char *optarg)
3217c478bd9Sstevel@tonic-gate {
3227c478bd9Sstevel@tonic-gate 	struct tm tm;
323404720a7Sbasabi 	off_t ret;
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate 	if (strptime(optarg, "%a %b %e %T %Z %Y", &tm) == NULL &&
3267c478bd9Sstevel@tonic-gate 	    strptime(optarg, "%c", &tm) == NULL)
3277c478bd9Sstevel@tonic-gate 		err(EF_FILE|EF_JMP,
3287c478bd9Sstevel@tonic-gate 		    "Option '%c' requires ctime-style time", *o);
3297c478bd9Sstevel@tonic-gate 	errno = 0;
330404720a7Sbasabi 	if ((ret = (off_t)mktime(&tm)) == -1 && errno)
3317c478bd9Sstevel@tonic-gate 		err(EF_FILE|EF_SYS|EF_JMP, "Option '%c' Illegal time", *o);
3327c478bd9Sstevel@tonic-gate 
3337c478bd9Sstevel@tonic-gate 	return (ret);
3347c478bd9Sstevel@tonic-gate }
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate /*
3377c478bd9Sstevel@tonic-gate  * opts_parse_atopi -- parse a positive integer format optarg
3387c478bd9Sstevel@tonic-gate  */
339e9a193fcSJohn.Zolnowsky@Sun.COM static off_t
opts_parse_atopi(const char * o,const char * optarg)3407c478bd9Sstevel@tonic-gate opts_parse_atopi(const char *o, const char *optarg)
3417c478bd9Sstevel@tonic-gate {
342404720a7Sbasabi 	off_t ret = atoll(optarg);
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate 	while (isdigit(*optarg))
3457c478bd9Sstevel@tonic-gate 		optarg++;
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate 	if (*optarg)
3487c478bd9Sstevel@tonic-gate 		err(EF_FILE|EF_JMP,
3497c478bd9Sstevel@tonic-gate 		    "Option '%c' requires non-negative number", *o);
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 	return (ret);
3527c478bd9Sstevel@tonic-gate }
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate /*
3557c478bd9Sstevel@tonic-gate  * opts_parse_atopi -- parse a size format optarg into bytes
3567c478bd9Sstevel@tonic-gate  */
357e9a193fcSJohn.Zolnowsky@Sun.COM static off_t
opts_parse_bytes(const char * o,const char * optarg)3587c478bd9Sstevel@tonic-gate opts_parse_bytes(const char *o, const char *optarg)
3597c478bd9Sstevel@tonic-gate {
360404720a7Sbasabi 	off_t ret = atoll(optarg);
3617c478bd9Sstevel@tonic-gate 	while (isdigit(*optarg))
3627c478bd9Sstevel@tonic-gate 		optarg++;
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate 	switch (*optarg) {
3657c478bd9Sstevel@tonic-gate 	case 'g':
3667c478bd9Sstevel@tonic-gate 	case 'G':
3677c478bd9Sstevel@tonic-gate 		ret *= 1024;
3687c478bd9Sstevel@tonic-gate 		/*FALLTHROUGH*/
3697c478bd9Sstevel@tonic-gate 	case 'm':
3707c478bd9Sstevel@tonic-gate 	case 'M':
3717c478bd9Sstevel@tonic-gate 		ret *= 1024;
3727c478bd9Sstevel@tonic-gate 		/*FALLTHROUGH*/
3737c478bd9Sstevel@tonic-gate 	case 'k':
3747c478bd9Sstevel@tonic-gate 	case 'K':
3757c478bd9Sstevel@tonic-gate 		ret *= 1024;
3767c478bd9Sstevel@tonic-gate 		/*FALLTHROUGH*/
3777c478bd9Sstevel@tonic-gate 	case 'b':
3787c478bd9Sstevel@tonic-gate 	case 'B':
3797c478bd9Sstevel@tonic-gate 		if (optarg[1] == '\0')
3807c478bd9Sstevel@tonic-gate 			return (ret);
3817c478bd9Sstevel@tonic-gate 	}
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate 	err(EF_FILE|EF_JMP,
3847c478bd9Sstevel@tonic-gate 	    "Option '%c' requires number with suffix from [bkmg]", *o);
3857c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
38678eb75caSchin 	return (0);
3877c478bd9Sstevel@tonic-gate }
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate /*
3907c478bd9Sstevel@tonic-gate  * opts_parse_seconds -- parse a time format optarg into seconds
3917c478bd9Sstevel@tonic-gate  */
392e9a193fcSJohn.Zolnowsky@Sun.COM static off_t
opts_parse_seconds(const char * o,const char * optarg)3937c478bd9Sstevel@tonic-gate opts_parse_seconds(const char *o, const char *optarg)
3947c478bd9Sstevel@tonic-gate {
395404720a7Sbasabi 	off_t ret;
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate 	if (strcasecmp(optarg, "now") == 0)
3987c478bd9Sstevel@tonic-gate 		return (OPTP_NOW);
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate 	if (strcasecmp(optarg, "never") == 0)
4017c478bd9Sstevel@tonic-gate 		return (OPTP_NEVER);
4027c478bd9Sstevel@tonic-gate 
403404720a7Sbasabi 	ret = atoll(optarg);
4047c478bd9Sstevel@tonic-gate 	while (isdigit(*optarg))
4057c478bd9Sstevel@tonic-gate 		optarg++;
4067c478bd9Sstevel@tonic-gate 
4077c478bd9Sstevel@tonic-gate 	if (optarg[1] == '\0')
4087c478bd9Sstevel@tonic-gate 		switch (*optarg) {
4097c478bd9Sstevel@tonic-gate 		case 'h':
4107c478bd9Sstevel@tonic-gate 		case 'H':
4117c478bd9Sstevel@tonic-gate 			ret *= 60 * 60;
4127c478bd9Sstevel@tonic-gate 			return (ret);
4137c478bd9Sstevel@tonic-gate 		case 'd':
4147c478bd9Sstevel@tonic-gate 		case 'D':
4157c478bd9Sstevel@tonic-gate 			ret *= 60 * 60 * 24;
4167c478bd9Sstevel@tonic-gate 			return (ret);
4177c478bd9Sstevel@tonic-gate 		case 'w':
4187c478bd9Sstevel@tonic-gate 		case 'W':
4197c478bd9Sstevel@tonic-gate 			ret *= 60 * 60 * 24 * 7;
4207c478bd9Sstevel@tonic-gate 			return (ret);
4217c478bd9Sstevel@tonic-gate 		case 'm':
4227c478bd9Sstevel@tonic-gate 		case 'M':
4237c478bd9Sstevel@tonic-gate 			ret *= 60 * 60 * 24 * 30;
4247c478bd9Sstevel@tonic-gate 			return (ret);
4257c478bd9Sstevel@tonic-gate 		case 'y':
4267c478bd9Sstevel@tonic-gate 		case 'Y':
4277c478bd9Sstevel@tonic-gate 			ret *= 60 * 60 * 24 * 365;
4287c478bd9Sstevel@tonic-gate 			return (ret);
4297c478bd9Sstevel@tonic-gate 		}
4307c478bd9Sstevel@tonic-gate 
4317c478bd9Sstevel@tonic-gate 	err(EF_FILE|EF_JMP,
4327c478bd9Sstevel@tonic-gate 	    "Option '%c' requires number with suffix from [hdwmy]", *o);
4337c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
43478eb75caSchin 	return (0);
4357c478bd9Sstevel@tonic-gate }
4367c478bd9Sstevel@tonic-gate 
4377c478bd9Sstevel@tonic-gate /* info passed between opts_print() and printer() */
43878eb75caSchin struct printerinfo {
4397c478bd9Sstevel@tonic-gate 	FILE *stream;
4407c478bd9Sstevel@tonic-gate 	int isswitch;
4417c478bd9Sstevel@tonic-gate 	char *exclude;
4427c478bd9Sstevel@tonic-gate };
4437c478bd9Sstevel@tonic-gate 
4447c478bd9Sstevel@tonic-gate /* helper function for opts_print() */
4457c478bd9Sstevel@tonic-gate static void
printer(const char * lhs,void * rhs,void * arg)4467c478bd9Sstevel@tonic-gate printer(const char *lhs, void *rhs, void *arg)
4477c478bd9Sstevel@tonic-gate {
4487c478bd9Sstevel@tonic-gate 	struct printerinfo *pip = (struct printerinfo *)arg;
4497c478bd9Sstevel@tonic-gate 	char *s = (char *)rhs;
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate 	if (pip->isswitch) {
4527c478bd9Sstevel@tonic-gate 		char *ep = pip->exclude;
4537c478bd9Sstevel@tonic-gate 		while (ep && *ep)
4547c478bd9Sstevel@tonic-gate 			if (*ep++ == *lhs)
4557c478bd9Sstevel@tonic-gate 				return;
4567c478bd9Sstevel@tonic-gate 	}
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate 	(void) fprintf(pip->stream, " %s%s", (pip->isswitch) ? "-" : "", lhs);
4597c478bd9Sstevel@tonic-gate 	if (s && *s) {
4607c478bd9Sstevel@tonic-gate 		(void) fprintf(pip->stream, " ");
4617c478bd9Sstevel@tonic-gate 		opts_printword(s, pip->stream);
4627c478bd9Sstevel@tonic-gate 	}
4637c478bd9Sstevel@tonic-gate }
4647c478bd9Sstevel@tonic-gate 
4657c478bd9Sstevel@tonic-gate /*
4667c478bd9Sstevel@tonic-gate  * opts_printword -- print a word, quoting as necessary
4677c478bd9Sstevel@tonic-gate  */
4687c478bd9Sstevel@tonic-gate void
opts_printword(const char * word,FILE * stream)4697c478bd9Sstevel@tonic-gate opts_printword(const char *word, FILE *stream)
4707c478bd9Sstevel@tonic-gate {
4717c478bd9Sstevel@tonic-gate 	char *q = "";
4727c478bd9Sstevel@tonic-gate 
473b493790cSbasabi 	if (word != NULL) {
474b493790cSbasabi 		if (strchr(word, ' ') || strchr(word, '\t') ||
475b493790cSbasabi 		    strchr(word, '$') || strchr(word, '[') ||
476b493790cSbasabi 		    strchr(word, '?') || strchr(word, '{') ||
477b493790cSbasabi 		    strchr(word, '`') || strchr(word, ';')) {
478e9a193fcSJohn.Zolnowsky@Sun.COM 			if (strchr(word, '\'') == NULL)
479e9a193fcSJohn.Zolnowsky@Sun.COM 				q = "'";
480e9a193fcSJohn.Zolnowsky@Sun.COM 			else if (strchr(word, '"') == NULL)
481b493790cSbasabi 				q = "\"";
482e9a193fcSJohn.Zolnowsky@Sun.COM 			else
483b493790cSbasabi 				err(EF_FILE|EF_JMP,
484b493790cSbasabi 				    "Can't protect quotes in <%s>", word);
485b493790cSbasabi 			(void) fprintf(stream, "%s%s%s", q, word, q);
486b493790cSbasabi 		} else
487b493790cSbasabi 			(void) fprintf(stream, "%s", word);
488b493790cSbasabi 	}
4897c478bd9Sstevel@tonic-gate }
4907c478bd9Sstevel@tonic-gate 
4917c478bd9Sstevel@tonic-gate /*
4927c478bd9Sstevel@tonic-gate  * opts_print -- print options to stream, leaving out those in "exclude"
4937c478bd9Sstevel@tonic-gate  */
4947c478bd9Sstevel@tonic-gate void
opts_print(struct opts * opts,FILE * stream,char * exclude)4957c478bd9Sstevel@tonic-gate opts_print(struct opts *opts, FILE *stream, char *exclude)
4967c478bd9Sstevel@tonic-gate {
4977c478bd9Sstevel@tonic-gate 	struct printerinfo pi;
4987c478bd9Sstevel@tonic-gate 	struct fn *fnp;
4997c478bd9Sstevel@tonic-gate 
5007c478bd9Sstevel@tonic-gate 	pi.stream = stream;
5017c478bd9Sstevel@tonic-gate 	pi.isswitch = 1;
5027c478bd9Sstevel@tonic-gate 	pi.exclude = exclude;
5037c478bd9Sstevel@tonic-gate 
5047c478bd9Sstevel@tonic-gate 	lut_walk(opts->op_raw, printer, &pi);
5057c478bd9Sstevel@tonic-gate 
5067c478bd9Sstevel@tonic-gate 	fn_list_rewind(opts->op_cmdargs);
5077c478bd9Sstevel@tonic-gate 	while ((fnp = fn_list_next(opts->op_cmdargs)) != NULL) {
5087c478bd9Sstevel@tonic-gate 		(void) fprintf(stream, " ");
5097c478bd9Sstevel@tonic-gate 		opts_printword(fn_s(fnp), stream);
5107c478bd9Sstevel@tonic-gate 	}
5117c478bd9Sstevel@tonic-gate }
5127c478bd9Sstevel@tonic-gate 
5137c478bd9Sstevel@tonic-gate #ifdef	TESTMODULE
5147c478bd9Sstevel@tonic-gate 
5157c478bd9Sstevel@tonic-gate /* table that drives argument parsing */
516e9a193fcSJohn.Zolnowsky@Sun.COM static struct optinfo Testopttable[] = {
5177c478bd9Sstevel@tonic-gate 	{ "a", OPTTYPE_BOOLEAN,	NULL,			OPTF_CLI },
5187c478bd9Sstevel@tonic-gate 	{ "b", OPTTYPE_STRING,	NULL,			OPTF_CLI },
5197c478bd9Sstevel@tonic-gate 	{ "c", OPTTYPE_INT,	opts_parse_seconds,	OPTF_CLI|OPTF_CONF },
5207c478bd9Sstevel@tonic-gate 	{ "d", OPTTYPE_INT,	opts_parse_ctime,	OPTF_CLI|OPTF_CONF },
5217c478bd9Sstevel@tonic-gate 	{ "e", OPTTYPE_INT,	opts_parse_bytes,	OPTF_CLI|OPTF_CONF },
5227c478bd9Sstevel@tonic-gate 	{ "f", OPTTYPE_INT,	opts_parse_atopi,	OPTF_CLI|OPTF_CONF },
5237c478bd9Sstevel@tonic-gate };
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate /*
5267c478bd9Sstevel@tonic-gate  * test main for opts module, usage: a.out options...
5277c478bd9Sstevel@tonic-gate  */
528b493790cSbasabi int
main(int argc,char * argv[])5297c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
5307c478bd9Sstevel@tonic-gate {
531*509030e0SToomas Soome 	struct opts *opts = NULL;
5327c478bd9Sstevel@tonic-gate 
5337c478bd9Sstevel@tonic-gate 	err_init(argv[0]);
5347c478bd9Sstevel@tonic-gate 	setbuf(stdout, NULL);
5357c478bd9Sstevel@tonic-gate 
536e9a193fcSJohn.Zolnowsky@Sun.COM 	opts_init(Testopttable,
537e9a193fcSJohn.Zolnowsky@Sun.COM 	    sizeof (Testopttable) / sizeof (struct optinfo));
5387c478bd9Sstevel@tonic-gate 
5397c478bd9Sstevel@tonic-gate 	argv++;
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate 	if (SETJMP)
5427c478bd9Sstevel@tonic-gate 		err(0, "opts parsing failed");
5437c478bd9Sstevel@tonic-gate 	else
544e9a193fcSJohn.Zolnowsky@Sun.COM 		opts = opts_parse(NULL, argv, OPTF_CLI);
5457c478bd9Sstevel@tonic-gate 
5467c478bd9Sstevel@tonic-gate 	printf("options:");
5477c478bd9Sstevel@tonic-gate 	opts_print(opts, stdout, NULL);
5487c478bd9Sstevel@tonic-gate 	printf("\n");
5497c478bd9Sstevel@tonic-gate 
5507c478bd9Sstevel@tonic-gate 	err_done(0);
551b493790cSbasabi 	/* NOTREACHED */
552b493790cSbasabi 	return (0);
5537c478bd9Sstevel@tonic-gate }
5547c478bd9Sstevel@tonic-gate 
5557c478bd9Sstevel@tonic-gate #endif	/* TESTMODULE */
556