1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START 3*7c478bd9Sstevel@tonic-gate * 4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*7c478bd9Sstevel@tonic-gate * with the License. 8*7c478bd9Sstevel@tonic-gate * 9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 12*7c478bd9Sstevel@tonic-gate * and limitations under the License. 13*7c478bd9Sstevel@tonic-gate * 14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*7c478bd9Sstevel@tonic-gate * 20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END 21*7c478bd9Sstevel@tonic-gate */ 22*7c478bd9Sstevel@tonic-gate /* 23*7c478bd9Sstevel@tonic-gate * Copyright (c) 2001 by Sun Microsystems, Inc. 24*7c478bd9Sstevel@tonic-gate * All rights reserved. 25*7c478bd9Sstevel@tonic-gate * 26*7c478bd9Sstevel@tonic-gate * logadm/opts.c -- options handling routines 27*7c478bd9Sstevel@tonic-gate */ 28*7c478bd9Sstevel@tonic-gate 29*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 30*7c478bd9Sstevel@tonic-gate 31*7c478bd9Sstevel@tonic-gate #include <stdio.h> 32*7c478bd9Sstevel@tonic-gate #include <libintl.h> 33*7c478bd9Sstevel@tonic-gate #include <stdlib.h> 34*7c478bd9Sstevel@tonic-gate #include <ctype.h> 35*7c478bd9Sstevel@tonic-gate #include <strings.h> 36*7c478bd9Sstevel@tonic-gate #include <time.h> 37*7c478bd9Sstevel@tonic-gate #include <sys/types.h> 38*7c478bd9Sstevel@tonic-gate #include <sys/stat.h> 39*7c478bd9Sstevel@tonic-gate #include <errno.h> 40*7c478bd9Sstevel@tonic-gate #include "err.h" 41*7c478bd9Sstevel@tonic-gate #include "lut.h" 42*7c478bd9Sstevel@tonic-gate #include "fn.h" 43*7c478bd9Sstevel@tonic-gate #include "opts.h" 44*7c478bd9Sstevel@tonic-gate 45*7c478bd9Sstevel@tonic-gate /* forward declarations for private functions */ 46*7c478bd9Sstevel@tonic-gate static struct optinfo *opt_info(int c); 47*7c478bd9Sstevel@tonic-gate static void opts_setcmdarg(struct opts *opts, const char *cmdarg); 48*7c478bd9Sstevel@tonic-gate 49*7c478bd9Sstevel@tonic-gate /* info created by opts_parse(), private to this module */ 50*7c478bd9Sstevel@tonic-gate struct opts { 51*7c478bd9Sstevel@tonic-gate struct lut *op_raw; /* the raw text for the options */ 52*7c478bd9Sstevel@tonic-gate struct lut *op_ints; /* the int values for the options */ 53*7c478bd9Sstevel@tonic-gate struct fn_list *op_cmdargs; /* the op_cmdargs */ 54*7c478bd9Sstevel@tonic-gate }; 55*7c478bd9Sstevel@tonic-gate 56*7c478bd9Sstevel@tonic-gate static struct lut *Info; /* table driving parsing */ 57*7c478bd9Sstevel@tonic-gate 58*7c478bd9Sstevel@tonic-gate /* 59*7c478bd9Sstevel@tonic-gate * opts_init -- set current options parsing table 60*7c478bd9Sstevel@tonic-gate */ 61*7c478bd9Sstevel@tonic-gate void 62*7c478bd9Sstevel@tonic-gate opts_init(struct optinfo *table, int numentries) 63*7c478bd9Sstevel@tonic-gate { 64*7c478bd9Sstevel@tonic-gate while (numentries-- > 0) { 65*7c478bd9Sstevel@tonic-gate Info = lut_add(Info, table->oi_o, table); 66*7c478bd9Sstevel@tonic-gate table++; 67*7c478bd9Sstevel@tonic-gate } 68*7c478bd9Sstevel@tonic-gate } 69*7c478bd9Sstevel@tonic-gate 70*7c478bd9Sstevel@tonic-gate /* 71*7c478bd9Sstevel@tonic-gate * opt_info -- fetch the optinfo struct for the given option 72*7c478bd9Sstevel@tonic-gate */ 73*7c478bd9Sstevel@tonic-gate static struct optinfo * 74*7c478bd9Sstevel@tonic-gate opt_info(int c) 75*7c478bd9Sstevel@tonic-gate { 76*7c478bd9Sstevel@tonic-gate char lhs[2]; 77*7c478bd9Sstevel@tonic-gate lhs[0] = c; 78*7c478bd9Sstevel@tonic-gate lhs[1] = '\0'; 79*7c478bd9Sstevel@tonic-gate return ((struct optinfo *)lut_lookup(Info, lhs)); 80*7c478bd9Sstevel@tonic-gate } 81*7c478bd9Sstevel@tonic-gate 82*7c478bd9Sstevel@tonic-gate /* 83*7c478bd9Sstevel@tonic-gate * opts_parse -- parse an argv-style list of options 84*7c478bd9Sstevel@tonic-gate * 85*7c478bd9Sstevel@tonic-gate * prints a message to stderr and calls err(EF_FILE|EF_JMP, ...) on error 86*7c478bd9Sstevel@tonic-gate */ 87*7c478bd9Sstevel@tonic-gate struct opts * 88*7c478bd9Sstevel@tonic-gate opts_parse(char **argv, int flags) 89*7c478bd9Sstevel@tonic-gate { 90*7c478bd9Sstevel@tonic-gate struct opts *ret = MALLOC(sizeof (*ret)); 91*7c478bd9Sstevel@tonic-gate int dashdash = 0; 92*7c478bd9Sstevel@tonic-gate char *ptr; 93*7c478bd9Sstevel@tonic-gate 94*7c478bd9Sstevel@tonic-gate ret->op_raw = ret->op_ints = NULL; 95*7c478bd9Sstevel@tonic-gate ret->op_cmdargs = fn_list_new(NULL); 96*7c478bd9Sstevel@tonic-gate 97*7c478bd9Sstevel@tonic-gate /* no words to process, just return empty opts struct */ 98*7c478bd9Sstevel@tonic-gate if (argv == NULL) 99*7c478bd9Sstevel@tonic-gate return (ret); 100*7c478bd9Sstevel@tonic-gate 101*7c478bd9Sstevel@tonic-gate /* foreach word... */ 102*7c478bd9Sstevel@tonic-gate for (; (ptr = *argv) != NULL; argv++) { 103*7c478bd9Sstevel@tonic-gate if (dashdash || *ptr != '-') { 104*7c478bd9Sstevel@tonic-gate /* found a cmdarg */ 105*7c478bd9Sstevel@tonic-gate opts_setcmdarg(ret, ptr); 106*7c478bd9Sstevel@tonic-gate continue; 107*7c478bd9Sstevel@tonic-gate } 108*7c478bd9Sstevel@tonic-gate if (*++ptr == '\0') 109*7c478bd9Sstevel@tonic-gate err(EF_FILE|EF_JMP, "Illegal option: dash by itself"); 110*7c478bd9Sstevel@tonic-gate if (*ptr == '-') { 111*7c478bd9Sstevel@tonic-gate /* (here's where support for --longname would go) */ 112*7c478bd9Sstevel@tonic-gate if (*(ptr + 1) != '\0') 113*7c478bd9Sstevel@tonic-gate err(EF_FILE|EF_JMP, "Illegal option: -%s", ptr); 114*7c478bd9Sstevel@tonic-gate dashdash++; 115*7c478bd9Sstevel@tonic-gate continue; 116*7c478bd9Sstevel@tonic-gate } 117*7c478bd9Sstevel@tonic-gate for (; *ptr; ptr++) { 118*7c478bd9Sstevel@tonic-gate struct optinfo *info = opt_info(*ptr); 119*7c478bd9Sstevel@tonic-gate 120*7c478bd9Sstevel@tonic-gate /* see if option was in our parsing table */ 121*7c478bd9Sstevel@tonic-gate if (info == NULL) 122*7c478bd9Sstevel@tonic-gate err(EF_FILE|EF_JMP, "Illegal option: %c", *ptr); 123*7c478bd9Sstevel@tonic-gate 124*7c478bd9Sstevel@tonic-gate /* see if context allows this option */ 125*7c478bd9Sstevel@tonic-gate if ((flags & OPTF_CLI) && 126*7c478bd9Sstevel@tonic-gate (info->oi_flags & OPTF_CLI) == 0) 127*7c478bd9Sstevel@tonic-gate err(EF_FILE|EF_JMP, 128*7c478bd9Sstevel@tonic-gate "Option '%c' not allowed on " 129*7c478bd9Sstevel@tonic-gate "command line", *ptr); 130*7c478bd9Sstevel@tonic-gate 131*7c478bd9Sstevel@tonic-gate if ((flags & OPTF_CONF) && 132*7c478bd9Sstevel@tonic-gate (info->oi_flags & OPTF_CONF) == 0) 133*7c478bd9Sstevel@tonic-gate err(EF_FILE|EF_JMP, 134*7c478bd9Sstevel@tonic-gate "Option '%c' not allowed in " 135*7c478bd9Sstevel@tonic-gate "configuration file", *ptr); 136*7c478bd9Sstevel@tonic-gate 137*7c478bd9Sstevel@tonic-gate /* for boolean options, we have all the info we need */ 138*7c478bd9Sstevel@tonic-gate if (info->oi_t == OPTTYPE_BOOLEAN) { 139*7c478bd9Sstevel@tonic-gate (void) opts_set(ret, info->oi_o, ""); 140*7c478bd9Sstevel@tonic-gate continue; 141*7c478bd9Sstevel@tonic-gate } 142*7c478bd9Sstevel@tonic-gate 143*7c478bd9Sstevel@tonic-gate /* option expects argument */ 144*7c478bd9Sstevel@tonic-gate if (*++ptr == '\0' && 145*7c478bd9Sstevel@tonic-gate ((ptr = *++argv) == NULL || *ptr == '-')) 146*7c478bd9Sstevel@tonic-gate err(EF_FILE|EF_JMP, 147*7c478bd9Sstevel@tonic-gate "Option '%c' requires an argument", 148*7c478bd9Sstevel@tonic-gate info->oi_o[0]); 149*7c478bd9Sstevel@tonic-gate opts_set(ret, info->oi_o, ptr); 150*7c478bd9Sstevel@tonic-gate break; 151*7c478bd9Sstevel@tonic-gate } 152*7c478bd9Sstevel@tonic-gate } 153*7c478bd9Sstevel@tonic-gate 154*7c478bd9Sstevel@tonic-gate return (ret); 155*7c478bd9Sstevel@tonic-gate } 156*7c478bd9Sstevel@tonic-gate 157*7c478bd9Sstevel@tonic-gate /* 158*7c478bd9Sstevel@tonic-gate * opts_free -- free a struct opts previously allocated by opts_parse() 159*7c478bd9Sstevel@tonic-gate */ 160*7c478bd9Sstevel@tonic-gate void 161*7c478bd9Sstevel@tonic-gate opts_free(struct opts *opts) 162*7c478bd9Sstevel@tonic-gate { 163*7c478bd9Sstevel@tonic-gate if (opts) { 164*7c478bd9Sstevel@tonic-gate lut_free(opts->op_raw, NULL); 165*7c478bd9Sstevel@tonic-gate lut_free(opts->op_ints, NULL); 166*7c478bd9Sstevel@tonic-gate fn_list_free(opts->op_cmdargs); 167*7c478bd9Sstevel@tonic-gate FREE(opts); 168*7c478bd9Sstevel@tonic-gate } 169*7c478bd9Sstevel@tonic-gate } 170*7c478bd9Sstevel@tonic-gate 171*7c478bd9Sstevel@tonic-gate /* 172*7c478bd9Sstevel@tonic-gate * opts_set -- set an option 173*7c478bd9Sstevel@tonic-gate */ 174*7c478bd9Sstevel@tonic-gate void 175*7c478bd9Sstevel@tonic-gate opts_set(struct opts *opts, const char *o, const char *optarg) 176*7c478bd9Sstevel@tonic-gate { 177*7c478bd9Sstevel@tonic-gate struct optinfo *info = opt_info(*o); 178*7c478bd9Sstevel@tonic-gate 179*7c478bd9Sstevel@tonic-gate opts->op_raw = lut_add(opts->op_raw, o, (void *)optarg); 180*7c478bd9Sstevel@tonic-gate 181*7c478bd9Sstevel@tonic-gate if (info->oi_parser) 182*7c478bd9Sstevel@tonic-gate opts->op_ints = lut_add(opts->op_ints, o, (void *) 183*7c478bd9Sstevel@tonic-gate (*info->oi_parser)(o, optarg)); 184*7c478bd9Sstevel@tonic-gate } 185*7c478bd9Sstevel@tonic-gate 186*7c478bd9Sstevel@tonic-gate /* 187*7c478bd9Sstevel@tonic-gate * opts_setcmdarg -- add a cmdarg to the list of op_cmdargs 188*7c478bd9Sstevel@tonic-gate */ 189*7c478bd9Sstevel@tonic-gate static void 190*7c478bd9Sstevel@tonic-gate opts_setcmdarg(struct opts *opts, const char *cmdarg) 191*7c478bd9Sstevel@tonic-gate { 192*7c478bd9Sstevel@tonic-gate fn_list_adds(opts->op_cmdargs, cmdarg); 193*7c478bd9Sstevel@tonic-gate } 194*7c478bd9Sstevel@tonic-gate 195*7c478bd9Sstevel@tonic-gate /* 196*7c478bd9Sstevel@tonic-gate * opts_count -- return count of the options in *options that are set 197*7c478bd9Sstevel@tonic-gate */ 198*7c478bd9Sstevel@tonic-gate int 199*7c478bd9Sstevel@tonic-gate opts_count(struct opts *opts, const char *options) 200*7c478bd9Sstevel@tonic-gate { 201*7c478bd9Sstevel@tonic-gate int count = 0; 202*7c478bd9Sstevel@tonic-gate 203*7c478bd9Sstevel@tonic-gate for (; *options; options++) { 204*7c478bd9Sstevel@tonic-gate char lhs[2]; 205*7c478bd9Sstevel@tonic-gate lhs[0] = *options; 206*7c478bd9Sstevel@tonic-gate lhs[1] = '\0'; 207*7c478bd9Sstevel@tonic-gate if (lut_lookup(opts->op_raw, lhs)) 208*7c478bd9Sstevel@tonic-gate count++; 209*7c478bd9Sstevel@tonic-gate } 210*7c478bd9Sstevel@tonic-gate return (count); 211*7c478bd9Sstevel@tonic-gate } 212*7c478bd9Sstevel@tonic-gate 213*7c478bd9Sstevel@tonic-gate /* 214*7c478bd9Sstevel@tonic-gate * opts_optarg -- return the optarg for the given option, NULL if not set 215*7c478bd9Sstevel@tonic-gate */ 216*7c478bd9Sstevel@tonic-gate const char * 217*7c478bd9Sstevel@tonic-gate opts_optarg(struct opts *opts, const char *o) 218*7c478bd9Sstevel@tonic-gate { 219*7c478bd9Sstevel@tonic-gate return ((char *)lut_lookup(opts->op_raw, o)); 220*7c478bd9Sstevel@tonic-gate } 221*7c478bd9Sstevel@tonic-gate 222*7c478bd9Sstevel@tonic-gate /* 223*7c478bd9Sstevel@tonic-gate * opts_optarg_int -- return the int value for the given option 224*7c478bd9Sstevel@tonic-gate */ 225*7c478bd9Sstevel@tonic-gate int 226*7c478bd9Sstevel@tonic-gate opts_optarg_int(struct opts *opts, const char *o) 227*7c478bd9Sstevel@tonic-gate { 228*7c478bd9Sstevel@tonic-gate return ((int)lut_lookup(opts->op_ints, o)); 229*7c478bd9Sstevel@tonic-gate } 230*7c478bd9Sstevel@tonic-gate 231*7c478bd9Sstevel@tonic-gate /* 232*7c478bd9Sstevel@tonic-gate * opts_cmdargs -- return list of op_cmdargs 233*7c478bd9Sstevel@tonic-gate */ 234*7c478bd9Sstevel@tonic-gate struct fn_list * 235*7c478bd9Sstevel@tonic-gate opts_cmdargs(struct opts *opts) 236*7c478bd9Sstevel@tonic-gate { 237*7c478bd9Sstevel@tonic-gate return (opts->op_cmdargs); 238*7c478bd9Sstevel@tonic-gate } 239*7c478bd9Sstevel@tonic-gate 240*7c478bd9Sstevel@tonic-gate static void 241*7c478bd9Sstevel@tonic-gate merger(const char *lhs, void *rhs, void *arg) 242*7c478bd9Sstevel@tonic-gate { 243*7c478bd9Sstevel@tonic-gate struct lut **destlutp = (struct lut **)arg; 244*7c478bd9Sstevel@tonic-gate 245*7c478bd9Sstevel@tonic-gate *destlutp = lut_add(*destlutp, lhs, rhs); 246*7c478bd9Sstevel@tonic-gate } 247*7c478bd9Sstevel@tonic-gate 248*7c478bd9Sstevel@tonic-gate /* 249*7c478bd9Sstevel@tonic-gate * opts_merge -- merge two option lists together 250*7c478bd9Sstevel@tonic-gate */ 251*7c478bd9Sstevel@tonic-gate struct opts * 252*7c478bd9Sstevel@tonic-gate opts_merge(struct opts *back, struct opts *front) 253*7c478bd9Sstevel@tonic-gate { 254*7c478bd9Sstevel@tonic-gate struct opts *ret = MALLOC(sizeof (struct opts)); 255*7c478bd9Sstevel@tonic-gate 256*7c478bd9Sstevel@tonic-gate ret->op_raw = lut_dup(back->op_raw); 257*7c478bd9Sstevel@tonic-gate lut_walk(front->op_raw, merger, &(ret->op_raw)); 258*7c478bd9Sstevel@tonic-gate 259*7c478bd9Sstevel@tonic-gate ret->op_ints = lut_dup(back->op_ints); 260*7c478bd9Sstevel@tonic-gate lut_walk(front->op_ints, merger, &(ret->op_ints)); 261*7c478bd9Sstevel@tonic-gate 262*7c478bd9Sstevel@tonic-gate ret->op_cmdargs = fn_list_dup(back->op_cmdargs); 263*7c478bd9Sstevel@tonic-gate 264*7c478bd9Sstevel@tonic-gate return (ret); 265*7c478bd9Sstevel@tonic-gate } 266*7c478bd9Sstevel@tonic-gate 267*7c478bd9Sstevel@tonic-gate /* 268*7c478bd9Sstevel@tonic-gate * opts_parse_ctime -- parse a ctime format optarg 269*7c478bd9Sstevel@tonic-gate */ 270*7c478bd9Sstevel@tonic-gate int 271*7c478bd9Sstevel@tonic-gate opts_parse_ctime(const char *o, const char *optarg) 272*7c478bd9Sstevel@tonic-gate { 273*7c478bd9Sstevel@tonic-gate struct tm tm; 274*7c478bd9Sstevel@tonic-gate int ret; 275*7c478bd9Sstevel@tonic-gate 276*7c478bd9Sstevel@tonic-gate if (strptime(optarg, "%a %b %e %T %Z %Y", &tm) == NULL && 277*7c478bd9Sstevel@tonic-gate strptime(optarg, "%c", &tm) == NULL) 278*7c478bd9Sstevel@tonic-gate err(EF_FILE|EF_JMP, 279*7c478bd9Sstevel@tonic-gate "Option '%c' requires ctime-style time", *o); 280*7c478bd9Sstevel@tonic-gate errno = 0; 281*7c478bd9Sstevel@tonic-gate if ((ret = (int)mktime(&tm)) == -1 && errno) 282*7c478bd9Sstevel@tonic-gate err(EF_FILE|EF_SYS|EF_JMP, "Option '%c' Illegal time", *o); 283*7c478bd9Sstevel@tonic-gate 284*7c478bd9Sstevel@tonic-gate return (ret); 285*7c478bd9Sstevel@tonic-gate } 286*7c478bd9Sstevel@tonic-gate 287*7c478bd9Sstevel@tonic-gate /* 288*7c478bd9Sstevel@tonic-gate * opts_parse_atopi -- parse a positive integer format optarg 289*7c478bd9Sstevel@tonic-gate */ 290*7c478bd9Sstevel@tonic-gate int 291*7c478bd9Sstevel@tonic-gate opts_parse_atopi(const char *o, const char *optarg) 292*7c478bd9Sstevel@tonic-gate { 293*7c478bd9Sstevel@tonic-gate int ret = atoi(optarg); 294*7c478bd9Sstevel@tonic-gate 295*7c478bd9Sstevel@tonic-gate while (isdigit(*optarg)) 296*7c478bd9Sstevel@tonic-gate optarg++; 297*7c478bd9Sstevel@tonic-gate 298*7c478bd9Sstevel@tonic-gate if (*optarg) 299*7c478bd9Sstevel@tonic-gate err(EF_FILE|EF_JMP, 300*7c478bd9Sstevel@tonic-gate "Option '%c' requires non-negative number", *o); 301*7c478bd9Sstevel@tonic-gate 302*7c478bd9Sstevel@tonic-gate return (ret); 303*7c478bd9Sstevel@tonic-gate } 304*7c478bd9Sstevel@tonic-gate 305*7c478bd9Sstevel@tonic-gate /* 306*7c478bd9Sstevel@tonic-gate * opts_parse_atopi -- parse a size format optarg into bytes 307*7c478bd9Sstevel@tonic-gate */ 308*7c478bd9Sstevel@tonic-gate int 309*7c478bd9Sstevel@tonic-gate opts_parse_bytes(const char *o, const char *optarg) 310*7c478bd9Sstevel@tonic-gate { 311*7c478bd9Sstevel@tonic-gate int ret = atoi(optarg); 312*7c478bd9Sstevel@tonic-gate while (isdigit(*optarg)) 313*7c478bd9Sstevel@tonic-gate optarg++; 314*7c478bd9Sstevel@tonic-gate 315*7c478bd9Sstevel@tonic-gate switch (*optarg) { 316*7c478bd9Sstevel@tonic-gate case 'g': 317*7c478bd9Sstevel@tonic-gate case 'G': 318*7c478bd9Sstevel@tonic-gate ret *= 1024; 319*7c478bd9Sstevel@tonic-gate /*FALLTHROUGH*/ 320*7c478bd9Sstevel@tonic-gate case 'm': 321*7c478bd9Sstevel@tonic-gate case 'M': 322*7c478bd9Sstevel@tonic-gate ret *= 1024; 323*7c478bd9Sstevel@tonic-gate /*FALLTHROUGH*/ 324*7c478bd9Sstevel@tonic-gate case 'k': 325*7c478bd9Sstevel@tonic-gate case 'K': 326*7c478bd9Sstevel@tonic-gate ret *= 1024; 327*7c478bd9Sstevel@tonic-gate /*FALLTHROUGH*/ 328*7c478bd9Sstevel@tonic-gate case 'b': 329*7c478bd9Sstevel@tonic-gate case 'B': 330*7c478bd9Sstevel@tonic-gate if (optarg[1] == '\0') 331*7c478bd9Sstevel@tonic-gate return (ret); 332*7c478bd9Sstevel@tonic-gate } 333*7c478bd9Sstevel@tonic-gate 334*7c478bd9Sstevel@tonic-gate err(EF_FILE|EF_JMP, 335*7c478bd9Sstevel@tonic-gate "Option '%c' requires number with suffix from [bkmg]", *o); 336*7c478bd9Sstevel@tonic-gate /*NOTREACHED*/ 337*7c478bd9Sstevel@tonic-gate } 338*7c478bd9Sstevel@tonic-gate 339*7c478bd9Sstevel@tonic-gate /* 340*7c478bd9Sstevel@tonic-gate * opts_parse_seconds -- parse a time format optarg into seconds 341*7c478bd9Sstevel@tonic-gate */ 342*7c478bd9Sstevel@tonic-gate int 343*7c478bd9Sstevel@tonic-gate opts_parse_seconds(const char *o, const char *optarg) 344*7c478bd9Sstevel@tonic-gate { 345*7c478bd9Sstevel@tonic-gate int ret; 346*7c478bd9Sstevel@tonic-gate 347*7c478bd9Sstevel@tonic-gate if (strcasecmp(optarg, "now") == 0) 348*7c478bd9Sstevel@tonic-gate return (OPTP_NOW); 349*7c478bd9Sstevel@tonic-gate 350*7c478bd9Sstevel@tonic-gate if (strcasecmp(optarg, "never") == 0) 351*7c478bd9Sstevel@tonic-gate return (OPTP_NEVER); 352*7c478bd9Sstevel@tonic-gate 353*7c478bd9Sstevel@tonic-gate ret = atoi(optarg); 354*7c478bd9Sstevel@tonic-gate while (isdigit(*optarg)) 355*7c478bd9Sstevel@tonic-gate optarg++; 356*7c478bd9Sstevel@tonic-gate 357*7c478bd9Sstevel@tonic-gate if (optarg[1] == '\0') 358*7c478bd9Sstevel@tonic-gate switch (*optarg) { 359*7c478bd9Sstevel@tonic-gate case 'h': 360*7c478bd9Sstevel@tonic-gate case 'H': 361*7c478bd9Sstevel@tonic-gate ret *= 60 * 60; 362*7c478bd9Sstevel@tonic-gate return (ret); 363*7c478bd9Sstevel@tonic-gate case 'd': 364*7c478bd9Sstevel@tonic-gate case 'D': 365*7c478bd9Sstevel@tonic-gate ret *= 60 * 60 * 24; 366*7c478bd9Sstevel@tonic-gate return (ret); 367*7c478bd9Sstevel@tonic-gate case 'w': 368*7c478bd9Sstevel@tonic-gate case 'W': 369*7c478bd9Sstevel@tonic-gate ret *= 60 * 60 * 24 * 7; 370*7c478bd9Sstevel@tonic-gate return (ret); 371*7c478bd9Sstevel@tonic-gate case 'm': 372*7c478bd9Sstevel@tonic-gate case 'M': 373*7c478bd9Sstevel@tonic-gate ret *= 60 * 60 * 24 * 30; 374*7c478bd9Sstevel@tonic-gate return (ret); 375*7c478bd9Sstevel@tonic-gate case 'y': 376*7c478bd9Sstevel@tonic-gate case 'Y': 377*7c478bd9Sstevel@tonic-gate ret *= 60 * 60 * 24 * 365; 378*7c478bd9Sstevel@tonic-gate return (ret); 379*7c478bd9Sstevel@tonic-gate } 380*7c478bd9Sstevel@tonic-gate 381*7c478bd9Sstevel@tonic-gate err(EF_FILE|EF_JMP, 382*7c478bd9Sstevel@tonic-gate "Option '%c' requires number with suffix from [hdwmy]", *o); 383*7c478bd9Sstevel@tonic-gate /*NOTREACHED*/ 384*7c478bd9Sstevel@tonic-gate } 385*7c478bd9Sstevel@tonic-gate 386*7c478bd9Sstevel@tonic-gate /* info passed between opts_print() and printer() */ 387*7c478bd9Sstevel@tonic-gate static struct printerinfo { 388*7c478bd9Sstevel@tonic-gate FILE *stream; 389*7c478bd9Sstevel@tonic-gate int isswitch; 390*7c478bd9Sstevel@tonic-gate char *exclude; 391*7c478bd9Sstevel@tonic-gate }; 392*7c478bd9Sstevel@tonic-gate 393*7c478bd9Sstevel@tonic-gate /* helper function for opts_print() */ 394*7c478bd9Sstevel@tonic-gate static void 395*7c478bd9Sstevel@tonic-gate printer(const char *lhs, void *rhs, void *arg) 396*7c478bd9Sstevel@tonic-gate { 397*7c478bd9Sstevel@tonic-gate struct printerinfo *pip = (struct printerinfo *)arg; 398*7c478bd9Sstevel@tonic-gate char *s = (char *)rhs; 399*7c478bd9Sstevel@tonic-gate 400*7c478bd9Sstevel@tonic-gate if (pip->isswitch) { 401*7c478bd9Sstevel@tonic-gate char *ep = pip->exclude; 402*7c478bd9Sstevel@tonic-gate while (ep && *ep) 403*7c478bd9Sstevel@tonic-gate if (*ep++ == *lhs) 404*7c478bd9Sstevel@tonic-gate return; 405*7c478bd9Sstevel@tonic-gate } 406*7c478bd9Sstevel@tonic-gate 407*7c478bd9Sstevel@tonic-gate (void) fprintf(pip->stream, " %s%s", (pip->isswitch) ? "-" : "", lhs); 408*7c478bd9Sstevel@tonic-gate if (s && *s) { 409*7c478bd9Sstevel@tonic-gate (void) fprintf(pip->stream, " "); 410*7c478bd9Sstevel@tonic-gate opts_printword(s, pip->stream); 411*7c478bd9Sstevel@tonic-gate } 412*7c478bd9Sstevel@tonic-gate } 413*7c478bd9Sstevel@tonic-gate 414*7c478bd9Sstevel@tonic-gate /* 415*7c478bd9Sstevel@tonic-gate * opts_printword -- print a word, quoting as necessary 416*7c478bd9Sstevel@tonic-gate */ 417*7c478bd9Sstevel@tonic-gate void 418*7c478bd9Sstevel@tonic-gate opts_printword(const char *word, FILE *stream) 419*7c478bd9Sstevel@tonic-gate { 420*7c478bd9Sstevel@tonic-gate char *q = ""; 421*7c478bd9Sstevel@tonic-gate 422*7c478bd9Sstevel@tonic-gate if (strchr(word, ' ') || strchr(word, '\t') || 423*7c478bd9Sstevel@tonic-gate strchr(word, '$') || strchr(word, '[') || 424*7c478bd9Sstevel@tonic-gate strchr(word, '?') || strchr(word, '{') || 425*7c478bd9Sstevel@tonic-gate strchr(word, '`') || strchr(word, ';')) { 426*7c478bd9Sstevel@tonic-gate if (strchr(word, '\'')) 427*7c478bd9Sstevel@tonic-gate q = "\""; 428*7c478bd9Sstevel@tonic-gate else if (strchr(word, '"')) 429*7c478bd9Sstevel@tonic-gate err(EF_FILE|EF_JMP, "Can't protect quotes in <%s>", 430*7c478bd9Sstevel@tonic-gate word); 431*7c478bd9Sstevel@tonic-gate else 432*7c478bd9Sstevel@tonic-gate q = "'"; 433*7c478bd9Sstevel@tonic-gate (void) fprintf(stream, "%s%s%s", q, word, q); 434*7c478bd9Sstevel@tonic-gate } else 435*7c478bd9Sstevel@tonic-gate (void) fprintf(stream, "%s", word); 436*7c478bd9Sstevel@tonic-gate } 437*7c478bd9Sstevel@tonic-gate 438*7c478bd9Sstevel@tonic-gate /* 439*7c478bd9Sstevel@tonic-gate * opts_print -- print options to stream, leaving out those in "exclude" 440*7c478bd9Sstevel@tonic-gate */ 441*7c478bd9Sstevel@tonic-gate void 442*7c478bd9Sstevel@tonic-gate opts_print(struct opts *opts, FILE *stream, char *exclude) 443*7c478bd9Sstevel@tonic-gate { 444*7c478bd9Sstevel@tonic-gate struct printerinfo pi; 445*7c478bd9Sstevel@tonic-gate struct fn *fnp; 446*7c478bd9Sstevel@tonic-gate 447*7c478bd9Sstevel@tonic-gate pi.stream = stream; 448*7c478bd9Sstevel@tonic-gate pi.isswitch = 1; 449*7c478bd9Sstevel@tonic-gate pi.exclude = exclude; 450*7c478bd9Sstevel@tonic-gate 451*7c478bd9Sstevel@tonic-gate lut_walk(opts->op_raw, printer, &pi); 452*7c478bd9Sstevel@tonic-gate 453*7c478bd9Sstevel@tonic-gate fn_list_rewind(opts->op_cmdargs); 454*7c478bd9Sstevel@tonic-gate while ((fnp = fn_list_next(opts->op_cmdargs)) != NULL) { 455*7c478bd9Sstevel@tonic-gate (void) fprintf(stream, " "); 456*7c478bd9Sstevel@tonic-gate opts_printword(fn_s(fnp), stream); 457*7c478bd9Sstevel@tonic-gate } 458*7c478bd9Sstevel@tonic-gate } 459*7c478bd9Sstevel@tonic-gate 460*7c478bd9Sstevel@tonic-gate #ifdef TESTMODULE 461*7c478bd9Sstevel@tonic-gate 462*7c478bd9Sstevel@tonic-gate /* table that drives argument parsing */ 463*7c478bd9Sstevel@tonic-gate static struct optinfo Opttable[] = { 464*7c478bd9Sstevel@tonic-gate { "a", OPTTYPE_BOOLEAN, NULL, OPTF_CLI }, 465*7c478bd9Sstevel@tonic-gate { "b", OPTTYPE_STRING, NULL, OPTF_CLI }, 466*7c478bd9Sstevel@tonic-gate { "c", OPTTYPE_INT, opts_parse_seconds, OPTF_CLI|OPTF_CONF }, 467*7c478bd9Sstevel@tonic-gate { "d", OPTTYPE_INT, opts_parse_ctime, OPTF_CLI|OPTF_CONF }, 468*7c478bd9Sstevel@tonic-gate { "e", OPTTYPE_INT, opts_parse_bytes, OPTF_CLI|OPTF_CONF }, 469*7c478bd9Sstevel@tonic-gate { "f", OPTTYPE_INT, opts_parse_atopi, OPTF_CLI|OPTF_CONF }, 470*7c478bd9Sstevel@tonic-gate }; 471*7c478bd9Sstevel@tonic-gate 472*7c478bd9Sstevel@tonic-gate /* 473*7c478bd9Sstevel@tonic-gate * test main for opts module, usage: a.out options... 474*7c478bd9Sstevel@tonic-gate */ 475*7c478bd9Sstevel@tonic-gate main(int argc, char *argv[]) 476*7c478bd9Sstevel@tonic-gate { 477*7c478bd9Sstevel@tonic-gate struct opts *opts; 478*7c478bd9Sstevel@tonic-gate 479*7c478bd9Sstevel@tonic-gate err_init(argv[0]); 480*7c478bd9Sstevel@tonic-gate setbuf(stdout, NULL); 481*7c478bd9Sstevel@tonic-gate 482*7c478bd9Sstevel@tonic-gate opts_init(Opttable, sizeof (Opttable) / sizeof (struct optinfo)); 483*7c478bd9Sstevel@tonic-gate 484*7c478bd9Sstevel@tonic-gate argv++; 485*7c478bd9Sstevel@tonic-gate 486*7c478bd9Sstevel@tonic-gate if (SETJMP) 487*7c478bd9Sstevel@tonic-gate err(0, "opts parsing failed"); 488*7c478bd9Sstevel@tonic-gate else 489*7c478bd9Sstevel@tonic-gate opts = opts_parse(argv, OPTF_CLI); 490*7c478bd9Sstevel@tonic-gate 491*7c478bd9Sstevel@tonic-gate printf("options:"); 492*7c478bd9Sstevel@tonic-gate opts_print(opts, stdout, NULL); 493*7c478bd9Sstevel@tonic-gate printf("\n"); 494*7c478bd9Sstevel@tonic-gate 495*7c478bd9Sstevel@tonic-gate err_done(0); 496*7c478bd9Sstevel@tonic-gate } 497*7c478bd9Sstevel@tonic-gate 498*7c478bd9Sstevel@tonic-gate #endif /* TESTMODULE */ 499