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