5 * This file contains all of the routines that must be linked into
6 * an executable to use the generated option processing. The optional
7 * routines are in separately compiled modules so that they will not
8 * necessarily be linked in.
10 * @addtogroup autoopts
14 * This file is part of AutoOpts, a companion to AutoGen.
15 * AutoOpts is free software.
16 * AutoOpts is Copyright (C) 1992-2015 by Bruce Korb - all rights reserved
18 * AutoOpts is available under any one of two licenses. The license
19 * in use must be one of these two and the choice is under the control
20 * of the user of the license.
22 * The GNU Lesser General Public License, version 3 or later
23 * See the files "COPYING.lgplv3" and "COPYING.gplv3"
25 * The Modified Berkeley Software Distribution License
26 * See the file "COPYING.mbsd"
28 * These files have the following sha256 sums:
30 * 8584710e9b04216a394078dc156b781d0b47e1729104d666658aecef8ee32e95 COPYING.gplv3
31 * 4379e7444a0e2ce2b12dd6f5a52a27a4d02d39d247901d3285c88cf0d37f477b COPYING.lgplv3
32 * 13aa749a5b0a454917a944ed8fffc530b784f5ead522b1aacaf4ec8aa55a6239 COPYING.mbsd
36 * The number of tab characters to skip when printing continuation lines.
38 static unsigned int tab_skip_ct = 0;
41 # define pathfind(_p, _n, _m) option_pathfind(_p, _n, _m)
42 # include "compat/pathfind.c"
46 # define vsnprintf option_vsnprintf
47 # define snprintf option_snprintf
48 # include "compat/snprintf.c"
52 # define strdup(_s) option_strdup(_s)
53 # include "compat/strdup.c"
57 # define strrchr(_s, _c) option_strrchr(_s, _c)
58 # define strchr(_s, _c) option_strchr(_s, _c)
59 # include "compat/strchr.c"
65 void * res = malloc(sz);
67 fprintf(stderr, zalloc_fail, (int)sz);
68 option_exits(EXIT_FAILURE);
73 #define malloc(_s) ao_malloc(_s)
76 ao_realloc(void *p, size_t sz)
78 void * res = (p == NULL) ? malloc(sz) : realloc(p, sz);
80 fprintf(stderr, zrealloc_fail, (int)sz, p);
81 option_exits(EXIT_FAILURE);
86 #define realloc(_p,_s) ao_realloc(_p,_s)
89 ao_strdup(char const *str)
91 char * res = strdup(str);
93 fprintf(stderr, zalloc_fail, (int)strlen(str));
94 option_exits(EXIT_FAILURE);
99 #define strdup(_p) ao_strdup(_p)
104 * This routine handles equivalencing, sets the option state flags and
105 * invokes the handler procedure, if any.
108 handle_opt(tOptions * opts, tOptState * o_st)
111 * Save a copy of the option procedure pointer.
112 * If this is an equivalence class option, we still want this proc.
114 tOptDesc * od = o_st->pOD;
115 tOptProc * opt_proc = od->pOptProc;
116 if (od->fOptState & OPTST_ALLOC_ARG)
117 AGFREE(od->optArg.argString);
119 od->optArg.argString = o_st->pzOptArg;
122 * IF we are presetting options, then we will ignore any un-presettable
123 * options. They are the ones either marked as such.
125 if ( ((opts->fOptSet & OPTPROC_PRESETTING) != 0)
126 && ((od->fOptState & OPTST_NO_INIT) != 0)
131 * IF this is an equivalence class option,
133 * Save the option value that got us to this option
134 * entry. (It may not be od->optChar[0], if this is an
135 * equivalence entry.)
136 * set the pointer to the equivalence class base
138 if (od->optEquivIndex != NO_EQUIVALENT) {
139 tOptDesc * eqv_od = opts->pOptDesc + od->optEquivIndex;
142 * IF the current option state has not been defined (set on the
143 * command line), THEN we will allow continued resetting of
144 * the value. Once "defined", then it must not change.
146 if ((od->fOptState & OPTST_DEFINED) != 0) {
148 * The equivalenced-to option has been found on the command
149 * line before. Make sure new occurrences are the same type.
151 * IF this option has been previously equivalenced and
152 * it was not the same equivalenced-to option,
153 * THEN we have a usage problem.
155 if (eqv_od->optActualIndex != od->optIndex) {
156 fprintf(stderr, zmultiway_bug, eqv_od->pz_Name, od->pz_Name,
157 (opts->pOptDesc + eqv_od->optActualIndex)->pz_Name);
162 * Set the equivalenced-to actual option index to no-equivalent
163 * so that we set all the entries below. This option may either
164 * never have been selected before, or else it was selected by
165 * some sort of "presetting" mechanism.
167 eqv_od->optActualIndex = NO_EQUIVALENT;
170 if (eqv_od->optActualIndex != od->optIndex) {
172 * First time through, copy over the state
173 * and add in the equivalence flag
175 eqv_od->optActualValue = od->optValue;
176 eqv_od->optActualIndex = od->optIndex;
177 o_st->flags |= OPTST_EQUIVALENCE;
181 * Copy the most recent option argument. set membership state
182 * is kept in 'eqv_od->optCookie'. Do not overwrite.
184 eqv_od->optArg.argString = od->optArg.argString;
188 od->optActualValue = od->optValue;
189 od->optActualIndex = od->optIndex;
192 od->fOptState &= OPTST_PERSISTENT_MASK;
193 od->fOptState |= (o_st->flags & ~OPTST_PERSISTENT_MASK);
196 * Keep track of count only for DEFINED (command line) options.
197 * IF we have too many, build up an error message and bail.
199 if ( (od->fOptState & OPTST_DEFINED)
200 && (++od->optOccCt > od->optMaxCt) )
201 return too_many_occurrences(opts, od);
203 * If provided a procedure to call, call it
205 if (opt_proc != NULL)
206 (*opt_proc)(opts, od);
212 * Find the option descriptor and option argument (if any) for the
213 * next command line argument. DO NOT modify the descriptor. Put
214 * all the state in the state argument so that the option can be skipped
215 * without consequence (side effect).
217 * @param opts the program option descriptor
218 * @param o_st the state of the next found option
221 next_opt(tOptions * opts, tOptState * o_st)
224 tSuccess res = find_opt(opts, o_st);
225 if (! SUCCESSFUL(res))
229 if ( ((o_st->flags & OPTST_DEFINED) != 0)
230 && ((o_st->pOD->fOptState & OPTST_NO_COMMAND) != 0)) {
231 fprintf(stderr, zNotCmdOpt, o_st->pOD->pz_Name);
235 return get_opt_arg(opts, o_st);
239 * Process all the options from our current position onward. (This allows
240 * interspersed options and arguments for the few non-standard programs that
241 * require it.) Thus, do not rewind option indexes because some programs
242 * choose to re-invoke after a non-option.
244 * @param[in,out] opts program options descriptor
245 * @returns SUCCESS or FAILURE
248 regular_opts(tOptions * opts)
250 /* assert: opts->fOptSet & OPTPROC_IMMEDIATE == 0 */
252 tOptState opt_st = OPTSTATE_INITIALIZER(DEFINED);
254 switch (next_opt(opts, &opt_st)) {
255 case FAILURE: goto failed_option;
256 case PROBLEM: return SUCCESS; /* no more args */
261 * IF this is an immediate action option,
262 * THEN skip it (unless we are supposed to do it a second time).
264 if (! DO_NORMALLY(opt_st.flags)) {
265 if (! DO_SECOND_TIME(opt_st.flags))
267 opt_st.pOD->optOccCt--; /* don't count this repetition */
270 if (! SUCCESSFUL(handle_opt(opts, &opt_st)))
274 if ((opts->fOptSet & OPTPROC_ERRSTOP) != 0)
275 (*opts->pUsageProc)(opts, EXIT_FAILURE);
280 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
282 * THESE ROUTINES ARE CALLABLE FROM THE GENERATED OPTION PROCESSING CODE
284 /*=--subblock=arg=arg_type,arg_name,arg_desc =*/
287 * header: your-opts.h
291 * These are the routines that libopts users may call directly from their
292 * code. There are several other routines that can be called by code
293 * generated by the libopts option templates, but they are not to be
294 * called from any other user code. The @file{options.h} header is
295 * fairly clear about this, too.
298 /*=export_func optionProcess
300 * what: this is the main option processing routine
302 * arg: + tOptions * + opts + program options descriptor +
303 * arg: + int + a_ct + program arg count +
304 * arg: + char ** + a_v + program arg vector +
307 * ret_desc: the count of the arguments processed
311 * This is the main entry point for processing options. It is intended
312 * that this procedure be called once at the beginning of the execution of
313 * a program. Depending on options selected earlier, it is sometimes
314 * necessary to stop and restart option processing, or to select completely
315 * different sets of options. This can be done easily, but you generally
316 * do not want to do this.
318 * The number of arguments processed always includes the program name.
319 * If one of the arguments is "--", then it is counted and the processing
320 * stops. If an error was encountered and errors are to be tolerated, then
321 * the returned value is the index of the argument causing the error.
322 * A hyphen by itself ("-") will also cause processing to stop and will
323 * @emph{not} be counted among the processed arguments. A hyphen by itself
324 * is treated as an operand. Encountering an operand stops option
327 * err: Errors will cause diagnostics to be printed. @code{exit(3)} may
328 * or may not be called. It depends upon whether or not the options
329 * were generated with the "allow-errors" attribute, or if the
330 * ERRSKIP_OPTERR or ERRSTOP_OPTERR macros were invoked.
333 optionProcess(tOptions * opts, int a_ct, char ** a_v)
335 if (! SUCCESSFUL(validate_struct(opts, a_v[0])))
336 ao_bug(zbad_data_msg);
339 * Establish the real program name, the program full path,
340 * and do all the presetting the first time thru only.
342 if (! ao_initialize(opts, a_ct, a_v))
346 * IF we are (re)starting,
347 * THEN reset option location
349 if (opts->curOptIdx <= 0) {
351 opts->pzCurOpt = NULL;
354 if (! SUCCESSFUL(regular_opts(opts)))
355 return (int)opts->origArgCt;
358 * IF there were no errors
359 * AND we have RC/INI files
360 * AND there is a request to save the files
361 * THEN do that now before testing for conflicts.
362 * (conflicts are ignored in preset options)
364 switch (opts->specOptIdx.save_opts) {
370 tOptDesc * od = opts->pOptDesc + opts->specOptIdx.save_opts;
372 if (SELECTED_OPT(od)) {
373 optionSaveFile(opts);
374 option_exits(EXIT_SUCCESS);
380 * IF we are checking for errors,
381 * THEN look for too few occurrences of required options
383 if (((opts->fOptSet & OPTPROC_ERRSTOP) != 0)
384 && (! is_consistent(opts)))
385 (*opts->pUsageProc)(opts, EXIT_FAILURE);
387 return (int)opts->curOptIdx;
394 * c-file-style: "stroustrup"
395 * indent-tabs-mode: nil
397 * end of autoopts/autoopts.c */