4 * @brief Hunt for options in the option descriptor list
6 * This file contains the routines that deal with processing quoted strings
7 * into an internal format.
13 * This file is part of AutoOpts, a companion to AutoGen.
14 * AutoOpts is free software.
15 * AutoOpts is Copyright (C) 1992-2015 by Bruce Korb - all rights reserved
17 * AutoOpts is available under any one of two licenses. The license
18 * in use must be one of these two and the choice is under the control
19 * of the user of the license.
21 * The GNU Lesser General Public License, version 3 or later
22 * See the files "COPYING.lgplv3" and "COPYING.gplv3"
24 * The Modified Berkeley Software Distribution License
25 * See the file "COPYING.mbsd"
27 * These files have the following sha256 sums:
29 * 8584710e9b04216a394078dc156b781d0b47e1729104d666658aecef8ee32e95 COPYING.gplv3
30 * 4379e7444a0e2ce2b12dd6f5a52a27a4d02d39d247901d3285c88cf0d37f477b COPYING.lgplv3
31 * 13aa749a5b0a454917a944ed8fffc530b784f5ead522b1aacaf4ec8aa55a6239 COPYING.mbsd
34 /* = = = START-STATIC-FORWARD = = = */
36 parse_opt(char const ** nm_pp, char ** arg_pp, char * buf, size_t bufsz);
39 opt_ambiguities(tOptions * opts, char const * name, int nm_len);
42 opt_match_ct(tOptions * opts, char const * name, int nm_len,
43 int * ixp, bool * disable);
46 opt_set(tOptions * opts, char * arg, int idx, bool disable, tOptState * st);
49 opt_unknown(tOptions * opts, char const * name, char * arg, tOptState * st);
52 opt_ambiguous(tOptions * opts, char const * name, int match_ct);
55 get_opt_arg_must(tOptions * opts, tOptState * o_st);
58 get_opt_arg_may(tOptions * pOpts, tOptState * o_st);
61 get_opt_arg_none(tOptions * pOpts, tOptState * o_st);
62 /* = = = END-STATIC-FORWARD = = = */
65 * find the name and name length we are looking for
68 parse_opt(char const ** nm_pp, char ** arg_pp, char * buf, size_t bufsz)
71 char const * p = *nm_pp;
79 memcpy(buf, *nm_pp, (size_t)res);
87 if (++res >= (int)bufsz)
94 * print out the options that match the given name.
96 * @param pOpts option data
97 * @param opt_name name of option to look for
100 opt_ambiguities(tOptions * opts, char const * name, int nm_len)
102 char const * const hyph =
103 NAMED_OPTS(opts) ? "" : LONG_OPT_MARKER;
105 tOptDesc * pOD = opts->pOptDesc;
108 fputs(zambig_list_msg, stderr);
110 if (pOD->pz_Name == NULL)
111 continue; /* doc option */
113 if (strneqvcmp(name, pOD->pz_Name, nm_len) == 0)
114 fprintf(stderr, zambig_file, hyph, pOD->pz_Name);
116 else if ( (pOD->pz_DisableName != NULL)
117 && (strneqvcmp(name, pOD->pz_DisableName, nm_len) == 0)
119 fprintf(stderr, zambig_file, hyph, pOD->pz_DisableName);
120 } while (pOD++, (++idx < opts->optCt));
124 * Determine the number of options that match the name
126 * @param pOpts option data
127 * @param opt_name name of option to look for
128 * @param nm_len length of provided name
129 * @param index pointer to int for option index
130 * @param disable pointer to bool to mark disabled option
131 * @return count of options that match
134 opt_match_ct(tOptions * opts, char const * name, int nm_len,
135 int * ixp, bool * disable)
139 int idxLim = opts->optCt;
140 tOptDesc * pOD = opts->pOptDesc;
144 * If option disabled or a doc option, skip to next
146 if (pOD->pz_Name == NULL)
150 && (pOD->fOptState != (OPTST_OMITTED | OPTST_NO_INIT)))
153 if (strneqvcmp(name, pOD->pz_Name, nm_len) == 0) {
155 * IF we have a complete match
156 * THEN it takes priority over any already located partial
158 if (pOD->pz_Name[ nm_len ] == NUL) {
165 * IF there is a disable name
166 * *AND* the option name matches the disable name
169 else if ( (pOD->pz_DisableName != NULL)
170 && (strneqvcmp(name, pOD->pz_DisableName, nm_len) == 0)
175 * IF we have a complete match
176 * THEN it takes priority over any already located partial
178 if (pOD->pz_DisableName[ nm_len ] == NUL) {
185 continue; /* does not match any option */
188 * We found a full or partial match, either regular or disabling.
189 * Remember the index for later.
194 } while (pOD++, (++idx < idxLim));
200 * Set the option to the indicated option number.
202 * @param opts option data
203 * @param arg option argument (if glued to name)
204 * @param idx option index
205 * @param disable mark disabled option
206 * @param st state about current option
209 opt_set(tOptions * opts, char * arg, int idx, bool disable, tOptState * st)
211 tOptDesc * pOD = opts->pOptDesc + idx;
214 if ((opts->fOptSet & OPTPROC_ERRSTOP) == 0)
217 fprintf(stderr, zDisabledErr, opts->pzProgName, pOD->pz_Name);
218 if (pOD->pzText != NULL)
219 fprintf(stderr, SET_OFF_FMT, pOD->pzText);
221 (*opts->pUsageProc)(opts, EXIT_FAILURE);
223 _exit(EXIT_FAILURE); /* to be certain */
227 * IF we found a disablement name,
228 * THEN set the bit in the callers' flag word
231 st->flags |= OPTST_DISABLED;
235 st->optType = TOPT_LONG;
241 * An option was not found. Check for default option and set it
242 * if there is one. Otherwise, handle the error.
244 * @param opts option data
245 * @param name name of option to look for
246 * @param arg option argument
247 * @param st state about current option
249 * @return success status
252 opt_unknown(tOptions * opts, char const * name, char * arg, tOptState * st)
255 * IF there is no equal sign
256 * *AND* we are using named arguments
257 * *AND* there is a default named option,
258 * THEN return that option.
262 && (opts->specOptIdx.default_opt != NO_EQUIVALENT)) {
264 st->pOD = opts->pOptDesc + opts->specOptIdx.default_opt;
266 st->optType = TOPT_DEFAULT;
270 if ((opts->fOptSet & OPTPROC_ERRSTOP) != 0) {
271 fprintf(stderr, zIllOptStr, opts->pzProgPath, name);
272 (*opts->pUsageProc)(opts, EXIT_FAILURE);
274 _exit(EXIT_FAILURE); /* to be certain */
281 * Several options match the provided name.
283 * @param opts option data
284 * @param name name of option to look for
285 * @param match_ct number of matching options
287 * @return success status (always FAILURE, if it returns)
290 opt_ambiguous(tOptions * opts, char const * name, int match_ct)
292 if ((opts->fOptSet & OPTPROC_ERRSTOP) != 0) {
293 fprintf(stderr, zambig_opt_fmt, opts->pzProgPath, name, match_ct);
295 opt_ambiguities(opts, name, (int)strlen(name));
296 (*opts->pUsageProc)(opts, EXIT_FAILURE);
298 _exit(EXIT_FAILURE); /* to be certain */
303 /*=export_func optionVendorOption
306 * what: Process a vendor option
307 * arg: + tOptions * + pOpts + program options descriptor +
308 * arg: + tOptDesc * + pOptDesc + the descriptor for this arg +
311 * For POSIX specified utilities, the options are constrained to the options,
312 * @xref{config attributes, Program Configuration}. AutoOpts clients should
313 * never specify this directly. It gets referenced when the option
314 * definitions contain a "vendor-opt" attribute.
317 optionVendorOption(tOptions * pOpts, tOptDesc * pOD)
319 tOptState opt_st = OPTSTATE_INITIALIZER(PRESET);
320 char const * vopt_str = pOD->optArg.argString;
322 if (pOpts <= OPTPROC_EMIT_LIMIT)
325 if ((pOD->fOptState & OPTST_RESET) != 0)
328 if ((pOD->fOptState & OPTPROC_IMMEDIATE) == 0)
329 opt_st.flags = OPTST_DEFINED;
331 if ( ((pOpts->fOptSet & OPTPROC_VENDOR_OPT) == 0)
332 || ! SUCCESSFUL(opt_find_long(pOpts, vopt_str, &opt_st))
333 || ! SUCCESSFUL(get_opt_arg(pOpts, &opt_st)) )
335 fprintf(stderr, zIllVendOptStr, pOpts->pzProgName, vopt_str);
336 (*pOpts->pUsageProc)(pOpts, EXIT_FAILURE);
338 _exit(EXIT_FAILURE); /* to be certain */
342 * See if we are in immediate handling state.
344 if (pOpts->fOptSet & OPTPROC_IMMEDIATE) {
346 * See if the enclosed option is okay with that state.
348 if (DO_IMMEDIATELY(opt_st.flags))
349 (void)handle_opt(pOpts, &opt_st);
353 * non-immediate direction.
354 * See if the enclosed option is okay with that state.
356 if (DO_NORMALLY(opt_st.flags) || DO_SECOND_TIME(opt_st.flags))
357 (void)handle_opt(pOpts, &opt_st);
362 * Find the option descriptor by full name.
364 * @param opts option data
365 * @param opt_name name of option to look for
366 * @param state state about current option
368 * @return success status
371 opt_find_long(tOptions * opts, char const * opt_name, tOptState * state)
375 int nm_len = parse_opt(&opt_name, &opt_arg, name_buf, sizeof(name_buf));
378 bool disable = false;
382 if ((opts->fOptSet & OPTPROC_ERRSTOP) == 0)
385 fprintf(stderr, zInvalOptName, opts->pzProgName, opt_name);
386 (*opts->pUsageProc)(opts, EXIT_FAILURE);
388 _exit(EXIT_FAILURE); /* to be certain */
391 ct = opt_match_ct(opts, opt_name, nm_len, &idx, &disable);
394 * See if we found one match, no matches or multiple matches.
397 case 1: return opt_set(opts, opt_arg, idx, disable, state);
398 case 0: return opt_unknown(opts, opt_name, opt_arg, state);
399 default: return opt_ambiguous(opts, opt_name, ct);
405 * Find the short option descriptor for the current option
407 * @param pOpts option data
408 * @param optValue option flag character
409 * @param pOptState state about current option
412 opt_find_short(tOptions * pOpts, uint_t optValue, tOptState * pOptState)
414 tOptDesc * pRes = pOpts->pOptDesc;
415 int ct = pOpts->optCt;
418 * Search the option list
421 if (optValue != pRes->optValue)
424 if (SKIP_OPT(pRes)) {
425 if ( (pRes->fOptState == (OPTST_OMITTED | OPTST_NO_INIT))
426 && (pRes->pz_Name != NULL)) {
427 if ((pOpts->fOptSet & OPTPROC_ERRSTOP) == 0)
430 fprintf(stderr, zDisabledErr, pOpts->pzProgPath, pRes->pz_Name);
431 if (pRes->pzText != NULL)
432 fprintf(stderr, SET_OFF_FMT, pRes->pzText);
434 (*pOpts->pUsageProc)(pOpts, EXIT_FAILURE);
436 _exit(EXIT_FAILURE); /* to be certain */
438 goto short_opt_error;
441 pOptState->pOD = pRes;
442 pOptState->optType = TOPT_SHORT;
445 } while (pRes++, --ct > 0);
448 * IF the character value is a digit
449 * AND there is a special number option ("-n")
450 * THEN the result is the "option" itself and the
451 * option is the specially marked "number" option.
453 if ( IS_DEC_DIGIT_CHAR(optValue)
454 && (pOpts->specOptIdx.number_option != NO_EQUIVALENT) ) {
456 pRes = pOpts->pOptDesc + pOpts->specOptIdx.number_option;
458 pOptState->optType = TOPT_SHORT;
465 * IF we are to stop on errors (the default, actually)
466 * THEN call the usage procedure.
468 if ((pOpts->fOptSet & OPTPROC_ERRSTOP) != 0) {
469 fprintf(stderr, zIllOptChr, pOpts->pzProgPath, optValue);
470 (*pOpts->pUsageProc)(pOpts, EXIT_FAILURE);
472 _exit(EXIT_FAILURE); /* to be certain */
479 * Process option with a required argument. Long options can either have a
480 * separate command line argument, or an argument attached by the '='
481 * character. Figure out which.
483 * @param[in,out] opts the program option descriptor
484 * @param[in,out] o_st the option processing state
485 * @returns SUCCESS or FAILURE
488 get_opt_arg_must(tOptions * opts, tOptState * o_st)
490 switch (o_st->optType) {
493 * See if an arg string follows the flag character
495 if (*++(opts->pzCurOpt) == NUL)
496 opts->pzCurOpt = opts->origArgVect[ opts->curOptIdx++ ];
497 o_st->pzOptArg = opts->pzCurOpt;
502 * See if an arg string has already been assigned (glued on
503 * with an `=' character)
505 if (o_st->pzOptArg == NULL)
506 o_st->pzOptArg = opts->origArgVect[ opts->curOptIdx++ ];
511 fputs("AutoOpts lib error: option type not selected\n", stderr);
512 option_exits(EXIT_FAILURE);
517 * The option was selected by default. The current token is
518 * the option argument.
524 * Make sure we did not overflow the argument list.
526 if (opts->curOptIdx > opts->origArgCt) {
527 fprintf(stderr, zMisArg, opts->pzProgPath, o_st->pOD->pz_Name);
531 opts->pzCurOpt = NULL; /* next time advance to next arg */
536 * Process an option with an optional argument. For short options, it looks
537 * at the character after the option character, or it consumes the next full
538 * argument. For long options, it looks for an '=' character attachment to
539 * the long option name before deciding to take the next command line
542 * @param pOpts the option descriptor
543 * @param o_st a structure for managing the current processing state
544 * @returns SUCCESS or does not return
547 get_opt_arg_may(tOptions * pOpts, tOptState * o_st)
550 * An option argument is optional.
552 switch (o_st->optType) {
554 if (*++pOpts->pzCurOpt != NUL)
555 o_st->pzOptArg = pOpts->pzCurOpt;
557 char * pzLA = pOpts->origArgVect[ pOpts->curOptIdx ];
560 * BECAUSE it is optional, we must make sure
561 * we did not find another flag and that there
562 * is such an argument.
564 if ((pzLA == NULL) || (*pzLA == '-'))
565 o_st->pzOptArg = NULL;
567 pOpts->curOptIdx++; /* argument found */
568 o_st->pzOptArg = pzLA;
575 * Look for an argument if we don't already have one (glued on
576 * with a `=' character) *AND* we are not in named argument mode
578 if ( (o_st->pzOptArg == NULL)
579 && (! NAMED_OPTS(pOpts))) {
580 char * pzLA = pOpts->origArgVect[ pOpts->curOptIdx ];
583 * BECAUSE it is optional, we must make sure
584 * we did not find another flag and that there
585 * is such an argument.
587 if ((pzLA == NULL) || (*pzLA == '-'))
588 o_st->pzOptArg = NULL;
590 pOpts->curOptIdx++; /* argument found */
591 o_st->pzOptArg = pzLA;
598 ao_bug(zbad_default_msg);
602 * After an option with an optional argument, we will
603 * *always* start with the next option because if there
604 * were any characters following the option name/flag,
605 * they would be interpreted as the argument.
607 pOpts->pzCurOpt = NULL;
612 * Process option that does not have an argument.
614 * @param[in,out] opts the program option descriptor
615 * @param[in,out] o_st the option processing state
616 * @returns SUCCESS or FAILURE
619 get_opt_arg_none(tOptions * pOpts, tOptState * o_st)
622 * No option argument. Make sure next time around we find
623 * the correct option flag character for short options
625 if (o_st->optType == TOPT_SHORT)
629 * It is a long option. Make sure there was no ``=xxx'' argument
631 else if (o_st->pzOptArg != NULL) {
632 fprintf(stderr, zNoArg, pOpts->pzProgPath, o_st->pOD->pz_Name);
637 * It is a long option. Advance to next command line argument.
640 pOpts->pzCurOpt = NULL;
645 * Process option. Figure out whether or not to look for an option argument.
647 * @param[in,out] opts the program option descriptor
648 * @param[in,out] o_st the option processing state
649 * @returns SUCCESS or FAILURE
652 get_opt_arg(tOptions * opts, tOptState * o_st)
654 o_st->flags |= (o_st->pOD->fOptState & OPTST_PERSISTENT_MASK);
657 * Disabled options and options specified to not have arguments
658 * are handled with the "none" procedure. Otherwise, check the
659 * optional flag and call either the "may" or "must" function.
661 if ( ((o_st->flags & OPTST_DISABLED) != 0)
662 || (OPTST_GET_ARGTYPE(o_st->flags) == OPARG_TYPE_NONE))
663 return get_opt_arg_none(opts, o_st);
665 if (o_st->flags & OPTST_ARG_OPTIONAL)
666 return get_opt_arg_may( opts, o_st);
668 return get_opt_arg_must(opts, o_st);
672 * Find the option descriptor for the current option.
674 * @param[in,out] opts the program option descriptor
675 * @param[in,out] o_st the option processing state
676 * @returns SUCCESS or FAILURE
679 find_opt(tOptions * opts, tOptState * o_st)
682 * IF we are continuing a short option list (e.g. -xyz...)
683 * THEN continue a single flag option.
684 * OTHERWISE see if there is room to advance and then do so.
686 if ((opts->pzCurOpt != NULL) && (*opts->pzCurOpt != NUL))
687 return opt_find_short(opts, (uint8_t)*(opts->pzCurOpt), o_st);
689 if (opts->curOptIdx >= opts->origArgCt)
690 return PROBLEM; /* NORMAL COMPLETION */
692 opts->pzCurOpt = opts->origArgVect[ opts->curOptIdx ];
695 * IF all arguments must be named options, ...
697 if (NAMED_OPTS(opts)) {
698 char * pz = opts->pzCurOpt;
706 return opt_find_long(opts, pz, o_st);
709 * The name is prefixed with one or more hyphens. Strip them off
710 * and disable the "default_opt" setting. Use heavy recasting to
711 * strip off the "const" quality of the "default_opt" field.
713 while (*(++pz) == '-') ;
714 def_opt = VOIDP(&(opts->specOptIdx.default_opt));
716 *def_opt = NO_EQUIVALENT;
717 res = opt_find_long(opts, pz, o_st);
718 *def_opt = (uint16_t)def;
723 * Note the kind of flag/option marker
725 if (*((opts->pzCurOpt)++) != '-')
726 return PROBLEM; /* NORMAL COMPLETION - this + rest are operands */
729 * Special hack for a hyphen by itself
731 if (*(opts->pzCurOpt) == NUL)
732 return PROBLEM; /* NORMAL COMPLETION - this + rest are operands */
735 * The current argument is to be processed as an option argument
740 * We have an option marker.
741 * Test the next character for long option indication
743 if (opts->pzCurOpt[0] == '-') {
744 if (*++(opts->pzCurOpt) == NUL)
746 * NORMAL COMPLETION - NOT this arg, but rest are operands
751 * We do not allow the hyphen to be used as a flag value.
752 * Therefore, if long options are not to be accepted, we punt.
754 if ((opts->fOptSet & OPTPROC_LONGOPT) == 0) {
755 fprintf(stderr, zIllOptStr, opts->pzProgPath, opts->pzCurOpt-2);
759 return opt_find_long(opts, opts->pzCurOpt, o_st);
763 * If short options are not allowed, then do long
764 * option processing. Otherwise the character must be a
765 * short (i.e. single character) option.
767 if ((opts->fOptSet & OPTPROC_SHORTOPT) != 0)
768 return opt_find_short(opts, (uint8_t)*(opts->pzCurOpt), o_st);
770 return opt_find_long(opts, opts->pzCurOpt, o_st);
777 * c-file-style: "stroustrup"
778 * indent-tabs-mode: nil
780 * end of autoopts/find.c */