1181834Sroberto 2280849Scy/** 3280849Scy * \file autoopts.c 4181834Sroberto * 5181834Sroberto * This file contains all of the routines that must be linked into 6181834Sroberto * an executable to use the generated option processing. The optional 7181834Sroberto * routines are in separately compiled modules so that they will not 8181834Sroberto * necessarily be linked in. 9280849Scy * 10280849Scy * @addtogroup autoopts 11280849Scy * @{ 12181834Sroberto */ 13181834Sroberto/* 14280849Scy * This file is part of AutoOpts, a companion to AutoGen. 15280849Scy * AutoOpts is free software. 16285169Scy * AutoOpts is Copyright (C) 1992-2015 by Bruce Korb - all rights reserved 17181834Sroberto * 18280849Scy * AutoOpts is available under any one of two licenses. The license 19280849Scy * in use must be one of these two and the choice is under the control 20280849Scy * of the user of the license. 21181834Sroberto * 22280849Scy * The GNU Lesser General Public License, version 3 or later 23280849Scy * See the files "COPYING.lgplv3" and "COPYING.gplv3" 24181834Sroberto * 25280849Scy * The Modified Berkeley Software Distribution License 26280849Scy * See the file "COPYING.mbsd" 27181834Sroberto * 28280849Scy * These files have the following sha256 sums: 29181834Sroberto * 30280849Scy * 8584710e9b04216a394078dc156b781d0b47e1729104d666658aecef8ee32e95 COPYING.gplv3 31280849Scy * 4379e7444a0e2ce2b12dd6f5a52a27a4d02d39d247901d3285c88cf0d37f477b COPYING.lgplv3 32280849Scy * 13aa749a5b0a454917a944ed8fffc530b784f5ead522b1aacaf4ec8aa55a6239 COPYING.mbsd 33181834Sroberto */ 34181834Sroberto 35280849Scy/** 36280849Scy * The number of tab characters to skip when printing continuation lines. 37280849Scy */ 38280849Scystatic unsigned int tab_skip_ct = 0; 39181834Sroberto 40280849Scy#ifndef HAVE_PATHFIND 41280849Scy# define pathfind(_p, _n, _m) option_pathfind(_p, _n, _m) 42280849Scy# include "compat/pathfind.c" 43280849Scy#endif 44181834Sroberto 45280849Scy#ifndef HAVE_SNPRINTF 46280849Scy# define vsnprintf option_vsnprintf 47280849Scy# define snprintf option_snprintf 48280849Scy# include "compat/snprintf.c" 49280849Scy#endif 50181834Sroberto 51280849Scy#ifndef HAVE_STRDUP 52280849Scy# define strdup(_s) option_strdup(_s) 53280849Scy# include "compat/strdup.c" 54280849Scy#endif 55181834Sroberto 56280849Scy#ifndef HAVE_STRCHR 57280849Scy# define strrchr(_s, _c) option_strrchr(_s, _c) 58280849Scy# define strchr(_s, _c) option_strchr(_s, _c) 59280849Scy# include "compat/strchr.c" 60280849Scy#endif 61181834Sroberto 62181834SrobertoLOCAL void * 63280849Scyao_malloc(size_t sz) 64181834Sroberto{ 65181834Sroberto void * res = malloc(sz); 66181834Sroberto if (res == NULL) { 67280849Scy fprintf(stderr, zalloc_fail, (int)sz); 68280849Scy option_exits(EXIT_FAILURE); 69181834Sroberto } 70181834Sroberto return res; 71181834Sroberto} 72181834Sroberto#undef malloc 73280849Scy#define malloc(_s) ao_malloc(_s) 74181834Sroberto 75181834SrobertoLOCAL void * 76280849Scyao_realloc(void *p, size_t sz) 77181834Sroberto{ 78280849Scy void * res = (p == NULL) ? malloc(sz) : realloc(p, sz); 79181834Sroberto if (res == NULL) { 80280849Scy fprintf(stderr, zrealloc_fail, (int)sz, p); 81280849Scy option_exits(EXIT_FAILURE); 82181834Sroberto } 83181834Sroberto return res; 84181834Sroberto} 85181834Sroberto#undef realloc 86280849Scy#define realloc(_p,_s) ao_realloc(_p,_s) 87181834Sroberto 88181834SrobertoLOCAL char * 89280849Scyao_strdup(char const *str) 90181834Sroberto{ 91181834Sroberto char * res = strdup(str); 92181834Sroberto if (res == NULL) { 93280849Scy fprintf(stderr, zalloc_fail, (int)strlen(str)); 94280849Scy option_exits(EXIT_FAILURE); 95181834Sroberto } 96181834Sroberto return res; 97181834Sroberto} 98181834Sroberto#undef strdup 99280849Scy#define strdup(_p) ao_strdup(_p) 100181834Sroberto 101280849Scy/** 102280849Scy * handle an option. 103181834Sroberto * 104181834Sroberto * This routine handles equivalencing, sets the option state flags and 105181834Sroberto * invokes the handler procedure, if any. 106181834Sroberto */ 107181834SrobertoLOCAL tSuccess 108280849Scyhandle_opt(tOptions * opts, tOptState * o_st) 109181834Sroberto{ 110181834Sroberto /* 111181834Sroberto * Save a copy of the option procedure pointer. 112181834Sroberto * If this is an equivalence class option, we still want this proc. 113181834Sroberto */ 114280849Scy tOptDesc * od = o_st->pOD; 115280849Scy tOptProc * opt_proc = od->pOptProc; 116280849Scy if (od->fOptState & OPTST_ALLOC_ARG) 117280849Scy AGFREE(od->optArg.argString); 118181834Sroberto 119280849Scy od->optArg.argString = o_st->pzOptArg; 120181834Sroberto 121181834Sroberto /* 122181834Sroberto * IF we are presetting options, then we will ignore any un-presettable 123181834Sroberto * options. They are the ones either marked as such. 124181834Sroberto */ 125280849Scy if ( ((opts->fOptSet & OPTPROC_PRESETTING) != 0) 126280849Scy && ((od->fOptState & OPTST_NO_INIT) != 0) 127181834Sroberto ) 128181834Sroberto return PROBLEM; 129181834Sroberto 130181834Sroberto /* 131181834Sroberto * IF this is an equivalence class option, 132181834Sroberto * THEN 133181834Sroberto * Save the option value that got us to this option 134280849Scy * entry. (It may not be od->optChar[0], if this is an 135181834Sroberto * equivalence entry.) 136181834Sroberto * set the pointer to the equivalence class base 137181834Sroberto */ 138280849Scy if (od->optEquivIndex != NO_EQUIVALENT) { 139280849Scy tOptDesc * eqv_od = opts->pOptDesc + od->optEquivIndex; 140181834Sroberto 141181834Sroberto /* 142181834Sroberto * IF the current option state has not been defined (set on the 143181834Sroberto * command line), THEN we will allow continued resetting of 144181834Sroberto * the value. Once "defined", then it must not change. 145181834Sroberto */ 146280849Scy if ((od->fOptState & OPTST_DEFINED) != 0) { 147181834Sroberto /* 148181834Sroberto * The equivalenced-to option has been found on the command 149181834Sroberto * line before. Make sure new occurrences are the same type. 150181834Sroberto * 151181834Sroberto * IF this option has been previously equivalenced and 152181834Sroberto * it was not the same equivalenced-to option, 153181834Sroberto * THEN we have a usage problem. 154181834Sroberto */ 155280849Scy if (eqv_od->optActualIndex != od->optIndex) { 156280849Scy fprintf(stderr, zmultiway_bug, eqv_od->pz_Name, od->pz_Name, 157280849Scy (opts->pOptDesc + eqv_od->optActualIndex)->pz_Name); 158181834Sroberto return FAILURE; 159181834Sroberto } 160181834Sroberto } else { 161181834Sroberto /* 162181834Sroberto * Set the equivalenced-to actual option index to no-equivalent 163181834Sroberto * so that we set all the entries below. This option may either 164181834Sroberto * never have been selected before, or else it was selected by 165181834Sroberto * some sort of "presetting" mechanism. 166181834Sroberto */ 167280849Scy eqv_od->optActualIndex = NO_EQUIVALENT; 168181834Sroberto } 169181834Sroberto 170280849Scy if (eqv_od->optActualIndex != od->optIndex) { 171181834Sroberto /* 172181834Sroberto * First time through, copy over the state 173181834Sroberto * and add in the equivalence flag 174181834Sroberto */ 175280849Scy eqv_od->optActualValue = od->optValue; 176280849Scy eqv_od->optActualIndex = od->optIndex; 177280849Scy o_st->flags |= OPTST_EQUIVALENCE; 178181834Sroberto } 179181834Sroberto 180181834Sroberto /* 181181834Sroberto * Copy the most recent option argument. set membership state 182280849Scy * is kept in 'eqv_od->optCookie'. Do not overwrite. 183181834Sroberto */ 184280849Scy eqv_od->optArg.argString = od->optArg.argString; 185280849Scy od = eqv_od; 186181834Sroberto 187181834Sroberto } else { 188280849Scy od->optActualValue = od->optValue; 189280849Scy od->optActualIndex = od->optIndex; 190181834Sroberto } 191181834Sroberto 192280849Scy od->fOptState &= OPTST_PERSISTENT_MASK; 193280849Scy od->fOptState |= (o_st->flags & ~OPTST_PERSISTENT_MASK); 194181834Sroberto 195181834Sroberto /* 196181834Sroberto * Keep track of count only for DEFINED (command line) options. 197181834Sroberto * IF we have too many, build up an error message and bail. 198181834Sroberto */ 199280849Scy if ( (od->fOptState & OPTST_DEFINED) 200280849Scy && (++od->optOccCt > od->optMaxCt) ) 201280849Scy return too_many_occurrences(opts, od); 202181834Sroberto /* 203181834Sroberto * If provided a procedure to call, call it 204181834Sroberto */ 205280849Scy if (opt_proc != NULL) 206280849Scy (*opt_proc)(opts, od); 207181834Sroberto 208181834Sroberto return SUCCESS; 209181834Sroberto} 210181834Sroberto 211280849Scy/** 212181834Sroberto * Find the option descriptor and option argument (if any) for the 213181834Sroberto * next command line argument. DO NOT modify the descriptor. Put 214181834Sroberto * all the state in the state argument so that the option can be skipped 215181834Sroberto * without consequence (side effect). 216280849Scy * 217280849Scy * @param opts the program option descriptor 218280849Scy * @param o_st the state of the next found option 219181834Sroberto */ 220280849ScyLOCAL tSuccess 221280849Scynext_opt(tOptions * opts, tOptState * o_st) 222181834Sroberto{ 223280849Scy { 224280849Scy tSuccess res = find_opt(opts, o_st); 225280849Scy if (! SUCCESSFUL(res)) 226280849Scy return res; 227280849Scy } 228181834Sroberto 229280849Scy if ( ((o_st->flags & OPTST_DEFINED) != 0) 230280849Scy && ((o_st->pOD->fOptState & OPTST_NO_COMMAND) != 0)) { 231280849Scy fprintf(stderr, zNotCmdOpt, o_st->pOD->pz_Name); 232280849Scy return FAILURE; 233181834Sroberto } 234181834Sroberto 235280849Scy return get_opt_arg(opts, o_st); 236181834Sroberto} 237181834Sroberto 238280849Scy/** 239280849Scy * Process all the options from our current position onward. (This allows 240280849Scy * interspersed options and arguments for the few non-standard programs that 241280849Scy * require it.) Thus, do not rewind option indexes because some programs 242280849Scy * choose to re-invoke after a non-option. 243181834Sroberto * 244280849Scy * @param[in,out] opts program options descriptor 245280849Scy * @returns SUCCESS or FAILURE 246181834Sroberto */ 247181834SrobertoLOCAL tSuccess 248280849Scyregular_opts(tOptions * opts) 249181834Sroberto{ 250280849Scy /* assert: opts->fOptSet & OPTPROC_IMMEDIATE == 0 */ 251181834Sroberto for (;;) { 252280849Scy tOptState opt_st = OPTSTATE_INITIALIZER(DEFINED); 253181834Sroberto 254280849Scy switch (next_opt(opts, &opt_st)) { 255280849Scy case FAILURE: goto failed_option; 256181834Sroberto case PROBLEM: return SUCCESS; /* no more args */ 257181834Sroberto case SUCCESS: break; 258181834Sroberto } 259181834Sroberto 260181834Sroberto /* 261280849Scy * IF this is an immediate action option, 262181834Sroberto * THEN skip it (unless we are supposed to do it a second time). 263181834Sroberto */ 264280849Scy if (! DO_NORMALLY(opt_st.flags)) { 265280849Scy if (! DO_SECOND_TIME(opt_st.flags)) 266181834Sroberto continue; 267280849Scy opt_st.pOD->optOccCt--; /* don't count this repetition */ 268181834Sroberto } 269181834Sroberto 270280849Scy if (! SUCCESSFUL(handle_opt(opts, &opt_st))) 271181834Sroberto break; 272280849Scy } failed_option:; 273181834Sroberto 274280849Scy if ((opts->fOptSet & OPTPROC_ERRSTOP) != 0) 275280849Scy (*opts->pUsageProc)(opts, EXIT_FAILURE); 276181834Sroberto 277280849Scy return FAILURE; 278181834Sroberto} 279181834Sroberto 280181834Sroberto/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 281181834Sroberto * 282181834Sroberto * THESE ROUTINES ARE CALLABLE FROM THE GENERATED OPTION PROCESSING CODE 283181834Sroberto */ 284181834Sroberto/*=--subblock=arg=arg_type,arg_name,arg_desc =*/ 285181834Sroberto/*=* 286181834Sroberto * library: opts 287181834Sroberto * header: your-opts.h 288181834Sroberto * 289181834Sroberto * lib_description: 290181834Sroberto * 291181834Sroberto * These are the routines that libopts users may call directly from their 292181834Sroberto * code. There are several other routines that can be called by code 293181834Sroberto * generated by the libopts option templates, but they are not to be 294181834Sroberto * called from any other user code. The @file{options.h} header is 295181834Sroberto * fairly clear about this, too. 296181834Sroberto=*/ 297181834Sroberto 298181834Sroberto/*=export_func optionProcess 299181834Sroberto * 300181834Sroberto * what: this is the main option processing routine 301181834Sroberto * 302285169Scy * arg: + tOptions * + opts + program options descriptor + 303285169Scy * arg: + int + a_ct + program arg count + 304285169Scy * arg: + char ** + a_v + program arg vector + 305181834Sroberto * 306181834Sroberto * ret_type: int 307181834Sroberto * ret_desc: the count of the arguments processed 308181834Sroberto * 309181834Sroberto * doc: 310181834Sroberto * 311181834Sroberto * This is the main entry point for processing options. It is intended 312181834Sroberto * that this procedure be called once at the beginning of the execution of 313181834Sroberto * a program. Depending on options selected earlier, it is sometimes 314181834Sroberto * necessary to stop and restart option processing, or to select completely 315181834Sroberto * different sets of options. This can be done easily, but you generally 316181834Sroberto * do not want to do this. 317181834Sroberto * 318181834Sroberto * The number of arguments processed always includes the program name. 319181834Sroberto * If one of the arguments is "--", then it is counted and the processing 320181834Sroberto * stops. If an error was encountered and errors are to be tolerated, then 321181834Sroberto * the returned value is the index of the argument causing the error. 322181834Sroberto * A hyphen by itself ("-") will also cause processing to stop and will 323181834Sroberto * @emph{not} be counted among the processed arguments. A hyphen by itself 324181834Sroberto * is treated as an operand. Encountering an operand stops option 325181834Sroberto * processing. 326181834Sroberto * 327181834Sroberto * err: Errors will cause diagnostics to be printed. @code{exit(3)} may 328181834Sroberto * or may not be called. It depends upon whether or not the options 329181834Sroberto * were generated with the "allow-errors" attribute, or if the 330181834Sroberto * ERRSKIP_OPTERR or ERRSTOP_OPTERR macros were invoked. 331181834Sroberto=*/ 332181834Srobertoint 333280849ScyoptionProcess(tOptions * opts, int a_ct, char ** a_v) 334181834Sroberto{ 335280849Scy if (! SUCCESSFUL(validate_struct(opts, a_v[0]))) 336280849Scy ao_bug(zbad_data_msg); 337280849Scy 338181834Sroberto /* 339181834Sroberto * Establish the real program name, the program full path, 340181834Sroberto * and do all the presetting the first time thru only. 341181834Sroberto */ 342280849Scy if (! ao_initialize(opts, a_ct, a_v)) 343280849Scy return 0; 344181834Sroberto 345181834Sroberto /* 346181834Sroberto * IF we are (re)starting, 347181834Sroberto * THEN reset option location 348181834Sroberto */ 349280849Scy if (opts->curOptIdx <= 0) { 350280849Scy opts->curOptIdx = 1; 351280849Scy opts->pzCurOpt = NULL; 352181834Sroberto } 353181834Sroberto 354280849Scy if (! SUCCESSFUL(regular_opts(opts))) 355280849Scy return (int)opts->origArgCt; 356181834Sroberto 357181834Sroberto /* 358181834Sroberto * IF there were no errors 359181834Sroberto * AND we have RC/INI files 360181834Sroberto * AND there is a request to save the files 361181834Sroberto * THEN do that now before testing for conflicts. 362181834Sroberto * (conflicts are ignored in preset options) 363181834Sroberto */ 364280849Scy switch (opts->specOptIdx.save_opts) { 365280849Scy case 0: 366280849Scy case NO_EQUIVALENT: 367280849Scy break; 368280849Scy default: 369280849Scy { 370280849Scy tOptDesc * od = opts->pOptDesc + opts->specOptIdx.save_opts; 371181834Sroberto 372280849Scy if (SELECTED_OPT(od)) { 373280849Scy optionSaveFile(opts); 374280849Scy option_exits(EXIT_SUCCESS); 375181834Sroberto } 376181834Sroberto } 377280849Scy } 378181834Sroberto 379181834Sroberto /* 380181834Sroberto * IF we are checking for errors, 381181834Sroberto * THEN look for too few occurrences of required options 382181834Sroberto */ 383280849Scy if (((opts->fOptSet & OPTPROC_ERRSTOP) != 0) 384280849Scy && (! is_consistent(opts))) 385280849Scy (*opts->pUsageProc)(opts, EXIT_FAILURE); 386181834Sroberto 387280849Scy return (int)opts->curOptIdx; 388181834Sroberto} 389181834Sroberto 390280849Scy/** @} 391280849Scy * 392181834Sroberto * Local Variables: 393181834Sroberto * mode: C 394181834Sroberto * c-file-style: "stroustrup" 395181834Sroberto * indent-tabs-mode: nil 396181834Sroberto * End: 397181834Sroberto * end of autoopts/autoopts.c */ 398