123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765 |
- /**
- * @file check.c
- *
- * @brief Hunt for options in the option descriptor list
- *
- * This file contains the routines that deal with processing quoted strings
- * into an internal format.
- *
- * @addtogroup autoopts
- * @{
- */
- /*
- * This file is part of AutoOpts, a companion to AutoGen.
- * AutoOpts is free software.
- * AutoOpts is Copyright (C) 1992-2018 by Bruce Korb - all rights reserved
- *
- * AutoOpts is available under any one of two licenses. The license
- * in use must be one of these two and the choice is under the control
- * of the user of the license.
- *
- * The GNU Lesser General Public License, version 3 or later
- * See the files "COPYING.lgplv3" and "COPYING.gplv3"
- *
- * The Modified Berkeley Software Distribution License
- * See the file "COPYING.mbsd"
- *
- * These files have the following sha256 sums:
- *
- * 8584710e9b04216a394078dc156b781d0b47e1729104d666658aecef8ee32e95 COPYING.gplv3
- * 4379e7444a0e2ce2b12dd6f5a52a27a4d02d39d247901d3285c88cf0d37f477b COPYING.lgplv3
- * 13aa749a5b0a454917a944ed8fffc530b784f5ead522b1aacaf4ec8aa55a6239 COPYING.mbsd
- */
- /**
- * find the name and name length we are looking for
- */
- static int
- parse_opt(char const ** nm_pp, char ** arg_pp, char * buf, size_t bufsz)
- {
- int res = 0;
- char const * p = *nm_pp;
- *arg_pp = NULL;
- for (;;) {
- switch (*(p++)) {
- case NUL: return res;
- case '=':
- memcpy(buf, *nm_pp, (size_t)res);
- buf[res] = NUL;
- *nm_pp = buf;
- *arg_pp = (char *)p;
- return res;
- default:
- if (++res >= (int)bufsz)
- return -1;
- }
- }
- }
- /**
- * print out the options that match the given name.
- *
- * @param pOpts option data
- * @param opt_name name of option to look for
- */
- static void
- opt_ambiguities(tOptions * opts, char const * name, int nm_len)
- {
- char const * const hyph =
- NAMED_OPTS(opts) ? "" : LONG_OPT_MARKER;
- tOptDesc * pOD = opts->pOptDesc;
- int idx = 0;
- fputs(zambig_list_msg, stderr);
- do {
- if (pOD->pz_Name == NULL)
- continue; /* doc option */
- if (strneqvcmp(name, pOD->pz_Name, nm_len) == 0)
- fprintf(stderr, zambig_file, hyph, pOD->pz_Name);
- else if ( (pOD->pz_DisableName != NULL)
- && (strneqvcmp(name, pOD->pz_DisableName, nm_len) == 0)
- )
- fprintf(stderr, zambig_file, hyph, pOD->pz_DisableName);
- } while (pOD++, (++idx < opts->optCt));
- }
- /**
- * Determine the number of options that match the name
- *
- * @param pOpts option data
- * @param opt_name name of option to look for
- * @param nm_len length of provided name
- * @param index pointer to int for option index
- * @param disable pointer to bool to mark disabled option
- * @return count of options that match
- */
- static int
- opt_match_ct(tOptions * opts, char const * name, int nm_len,
- int * ixp, bool * disable)
- {
- int matchCt = 0;
- int idx = 0;
- int idxLim = opts->optCt;
- tOptDesc * pOD = opts->pOptDesc;
- do {
- /*
- * If option disabled or a doc option, skip to next
- */
- if (pOD->pz_Name == NULL)
- continue;
- if ( SKIP_OPT(pOD)
- && (pOD->fOptState != (OPTST_OMITTED | OPTST_NO_INIT)))
- continue;
- if (strneqvcmp(name, pOD->pz_Name, nm_len) == 0) {
- /*
- * IF we have a complete match
- * THEN it takes priority over any already located partial
- */
- if (pOD->pz_Name[ nm_len ] == NUL) {
- *ixp = idx;
- return 1;
- }
- }
- /*
- * IF there is a disable name
- * *AND* the option name matches the disable name
- * THEN ...
- */
- else if ( (pOD->pz_DisableName != NULL)
- && (strneqvcmp(name, pOD->pz_DisableName, nm_len) == 0)
- ) {
- *disable = true;
- /*
- * IF we have a complete match
- * THEN it takes priority over any already located partial
- */
- if (pOD->pz_DisableName[ nm_len ] == NUL) {
- *ixp = idx;
- return 1;
- }
- }
- else
- continue; /* does not match any option */
- /*
- * We found a full or partial match, either regular or disabling.
- * Remember the index for later.
- */
- *ixp = idx;
- ++matchCt;
- } while (pOD++, (++idx < idxLim));
- return matchCt;
- }
- /**
- * Set the option to the indicated option number.
- *
- * @param opts option data
- * @param arg option argument (if glued to name)
- * @param idx option index
- * @param disable mark disabled option
- * @param st state about current option
- */
- static tSuccess
- opt_set(tOptions * opts, char * arg, int idx, bool disable, tOptState * st)
- {
- tOptDesc * pOD = opts->pOptDesc + idx;
- if (SKIP_OPT(pOD)) {
- if ((opts->fOptSet & OPTPROC_ERRSTOP) == 0)
- return FAILURE;
- fprintf(stderr, zDisabledErr, opts->pzProgName, pOD->pz_Name);
- if (pOD->pzText != NULL)
- fprintf(stderr, SET_OFF_FMT, pOD->pzText);
- fputc(NL, stderr);
- (*opts->pUsageProc)(opts, EXIT_FAILURE);
- /* NOTREACHED */
- _exit(EXIT_FAILURE); /* to be certain */
- }
- /*
- * IF we found a disablement name,
- * THEN set the bit in the callers' flag word
- */
- if (disable)
- st->flags |= OPTST_DISABLED;
- st->pOD = pOD;
- st->pzOptArg = arg;
- st->optType = TOPT_LONG;
- return SUCCESS;
- }
- /**
- * An option was not found. Check for default option and set it
- * if there is one. Otherwise, handle the error.
- *
- * @param opts option data
- * @param name name of option to look for
- * @param arg option argument
- * @param st state about current option
- *
- * @return success status
- */
- static tSuccess
- opt_unknown(tOptions * opts, char const * name, char * arg, tOptState * st)
- {
- /*
- * IF there is no equal sign
- * *AND* we are using named arguments
- * *AND* there is a default named option,
- * THEN return that option.
- */
- if ( (arg == NULL)
- && NAMED_OPTS(opts)
- && (opts->specOptIdx.default_opt != NO_EQUIVALENT)) {
- st->pOD = opts->pOptDesc + opts->specOptIdx.default_opt;
- st->pzOptArg = name;
- st->optType = TOPT_DEFAULT;
- return SUCCESS;
- }
- if ((opts->fOptSet & OPTPROC_ERRSTOP) != 0) {
- fprintf(stderr, zIllOptStr, opts->pzProgPath, name);
- (*opts->pUsageProc)(opts, EXIT_FAILURE);
- /* NOTREACHED */
- _exit(EXIT_FAILURE); /* to be certain */
- }
- return FAILURE;
- }
- /**
- * Several options match the provided name.
- *
- * @param opts option data
- * @param name name of option to look for
- * @param match_ct number of matching options
- *
- * @return success status (always FAILURE, if it returns)
- */
- static tSuccess
- opt_ambiguous(tOptions * opts, char const * name, int match_ct)
- {
- if ((opts->fOptSet & OPTPROC_ERRSTOP) != 0) {
- fprintf(stderr, zambig_opt_fmt, opts->pzProgPath, name, match_ct);
- if (match_ct <= 4)
- opt_ambiguities(opts, name, (int)strlen(name));
- (*opts->pUsageProc)(opts, EXIT_FAILURE);
- /* NOTREACHED */
- _exit(EXIT_FAILURE); /* to be certain */
- }
- return FAILURE;
- }
- /*=export_func optionVendorOption
- * private:
- *
- * what: Process a vendor option
- * arg: + tOptions * + pOpts + program options descriptor +
- * arg: + tOptDesc * + pOptDesc + the descriptor for this arg +
- *
- * doc:
- * For POSIX specified utilities, the options are constrained to the options,
- * @xref{config attributes, Program Configuration}. AutoOpts clients should
- * never specify this directly. It gets referenced when the option
- * definitions contain a "vendor-opt" attribute.
- =*/
- void
- optionVendorOption(tOptions * pOpts, tOptDesc * pOD)
- {
- tOptState opt_st = OPTSTATE_INITIALIZER(PRESET);
- char const * vopt_str = pOD->optArg.argString;
- if (pOpts <= OPTPROC_EMIT_LIMIT)
- return;
- if ((pOD->fOptState & OPTST_RESET) != 0)
- return;
- if ((pOD->fOptState & OPTPROC_IMMEDIATE) == 0)
- opt_st.flags = OPTST_DEFINED;
- if ( ((pOpts->fOptSet & OPTPROC_VENDOR_OPT) == 0)
- || ! SUCCESSFUL(opt_find_long(pOpts, vopt_str, &opt_st))
- || ! SUCCESSFUL(get_opt_arg(pOpts, &opt_st)) )
- {
- fprintf(stderr, zIllVendOptStr, pOpts->pzProgName, vopt_str);
- (*pOpts->pUsageProc)(pOpts, EXIT_FAILURE);
- /* NOTREACHED */
- _exit(EXIT_FAILURE); /* to be certain */
- }
- /*
- * See if we are in immediate handling state.
- */
- if (pOpts->fOptSet & OPTPROC_IMMEDIATE) {
- /*
- * See if the enclosed option is okay with that state.
- */
- if (DO_IMMEDIATELY(opt_st.flags))
- (void)handle_opt(pOpts, &opt_st);
- } else {
- /*
- * non-immediate direction.
- * See if the enclosed option is okay with that state.
- */
- if (DO_NORMALLY(opt_st.flags) || DO_SECOND_TIME(opt_st.flags))
- (void)handle_opt(pOpts, &opt_st);
- }
- }
- /**
- * Find the option descriptor by full name.
- *
- * @param opts option data
- * @param opt_name name of option to look for
- * @param state state about current option
- *
- * @return success status
- */
- static tSuccess
- opt_find_long(tOptions * opts, char const * opt_name, tOptState * state)
- {
- char name_buf[128];
- char * opt_arg;
- int nm_len = parse_opt(&opt_name, &opt_arg, name_buf, sizeof(name_buf));
- int idx = 0;
- bool disable = false;
- int ct;
- if (nm_len <= 1) {
- if ((opts->fOptSet & OPTPROC_ERRSTOP) == 0)
- return FAILURE;
-
- fprintf(stderr, zInvalOptName, opts->pzProgName, opt_name);
- (*opts->pUsageProc)(opts, EXIT_FAILURE);
- /* NOTREACHED */
- _exit(EXIT_FAILURE); /* to be certain */
- }
- ct = opt_match_ct(opts, opt_name, nm_len, &idx, &disable);
- /*
- * See if we found one match, no matches or multiple matches.
- */
- switch (ct) {
- case 1: return opt_set(opts, opt_arg, idx, disable, state);
- case 0: return opt_unknown(opts, opt_name, opt_arg, state);
- default: return opt_ambiguous(opts, opt_name, ct);
- }
- }
- /**
- * Find the short option descriptor for the current option
- *
- * @param pOpts option data
- * @param optValue option flag character
- * @param pOptState state about current option
- */
- static tSuccess
- opt_find_short(tOptions * pOpts, uint_t optValue, tOptState * pOptState)
- {
- tOptDesc * pRes = pOpts->pOptDesc;
- int ct = pOpts->optCt;
- /*
- * Search the option list
- */
- do {
- if (optValue != pRes->optValue)
- continue;
- if (SKIP_OPT(pRes)) {
- if ( (pRes->fOptState == (OPTST_OMITTED | OPTST_NO_INIT))
- && (pRes->pz_Name != NULL)) {
- if ((pOpts->fOptSet & OPTPROC_ERRSTOP) == 0)
- return FAILURE;
-
- fprintf(stderr, zDisabledErr, pOpts->pzProgPath, pRes->pz_Name);
- if (pRes->pzText != NULL)
- fprintf(stderr, SET_OFF_FMT, pRes->pzText);
- fputc(NL, stderr);
- (*pOpts->pUsageProc)(pOpts, EXIT_FAILURE);
- /* NOTREACHED */
- _exit(EXIT_FAILURE); /* to be certain */
- }
- goto short_opt_error;
- }
- pOptState->pOD = pRes;
- pOptState->optType = TOPT_SHORT;
- return SUCCESS;
- } while (pRes++, --ct > 0);
- /*
- * IF the character value is a digit
- * AND there is a special number option ("-n")
- * THEN the result is the "option" itself and the
- * option is the specially marked "number" option.
- */
- if ( IS_DEC_DIGIT_CHAR(optValue)
- && (pOpts->specOptIdx.number_option != NO_EQUIVALENT) ) {
- pOptState->pOD = \
- pRes = pOpts->pOptDesc + pOpts->specOptIdx.number_option;
- (pOpts->pzCurOpt)--;
- pOptState->optType = TOPT_SHORT;
- return SUCCESS;
- }
- short_opt_error:
- /*
- * IF we are to stop on errors (the default, actually)
- * THEN call the usage procedure.
- */
- if ((pOpts->fOptSet & OPTPROC_ERRSTOP) != 0) {
- fprintf(stderr, zIllOptChr, pOpts->pzProgPath, optValue);
- (*pOpts->pUsageProc)(pOpts, EXIT_FAILURE);
- /* NOTREACHED */
- _exit(EXIT_FAILURE); /* to be certain */
- }
- return FAILURE;
- }
- /**
- * Process option with a required argument. Long options can either have a
- * separate command line argument, or an argument attached by the '='
- * character. Figure out which.
- *
- * @param[in,out] opts the program option descriptor
- * @param[in,out] o_st the option processing state
- * @returns SUCCESS or FAILURE
- */
- static tSuccess
- get_opt_arg_must(tOptions * opts, tOptState * o_st)
- {
- switch (o_st->optType) {
- case TOPT_SHORT:
- /*
- * See if an arg string follows the flag character
- */
- if (*++(opts->pzCurOpt) == NUL)
- opts->pzCurOpt = opts->origArgVect[ opts->curOptIdx++ ];
- o_st->pzOptArg = opts->pzCurOpt;
- break;
- case TOPT_LONG:
- /*
- * See if an arg string has already been assigned (glued on
- * with an `=' character)
- */
- if (o_st->pzOptArg == NULL)
- o_st->pzOptArg = opts->origArgVect[ opts->curOptIdx++ ];
- break;
- default:
- #ifdef DEBUG
- fputs("AutoOpts lib error: option type not selected\n", stderr);
- option_exits(EXIT_FAILURE);
- #endif
- case TOPT_DEFAULT:
- /*
- * The option was selected by default. The current token is
- * the option argument.
- */
- break;
- }
- /*
- * Make sure we did not overflow the argument list.
- */
- if (opts->curOptIdx > opts->origArgCt) {
- fprintf(stderr, zMisArg, opts->pzProgPath, o_st->pOD->pz_Name);
- return FAILURE;
- }
- opts->pzCurOpt = NULL; /* next time advance to next arg */
- return SUCCESS;
- }
- /**
- * Process an option with an optional argument. For short options, it looks
- * at the character after the option character, or it consumes the next full
- * argument. For long options, it looks for an '=' character attachment to
- * the long option name before deciding to take the next command line
- * argument.
- *
- * @param pOpts the option descriptor
- * @param o_st a structure for managing the current processing state
- * @returns SUCCESS or does not return
- */
- static tSuccess
- get_opt_arg_may(tOptions * pOpts, tOptState * o_st)
- {
- /*
- * An option argument is optional.
- */
- switch (o_st->optType) {
- case TOPT_SHORT:
- if (*++pOpts->pzCurOpt != NUL)
- o_st->pzOptArg = pOpts->pzCurOpt;
- else {
- char * pzLA = pOpts->origArgVect[ pOpts->curOptIdx ];
- /*
- * BECAUSE it is optional, we must make sure
- * we did not find another flag and that there
- * is such an argument.
- */
- if ((pzLA == NULL) || (*pzLA == '-'))
- o_st->pzOptArg = NULL;
- else {
- pOpts->curOptIdx++; /* argument found */
- o_st->pzOptArg = pzLA;
- }
- }
- break;
- case TOPT_LONG:
- /*
- * Look for an argument if we don't already have one (glued on
- * with a `=' character) *AND* we are not in named argument mode
- */
- if ( (o_st->pzOptArg == NULL)
- && (! NAMED_OPTS(pOpts))) {
- char * pzLA = pOpts->origArgVect[ pOpts->curOptIdx ];
- /*
- * BECAUSE it is optional, we must make sure
- * we did not find another flag and that there
- * is such an argument.
- */
- if ((pzLA == NULL) || (*pzLA == '-'))
- o_st->pzOptArg = NULL;
- else {
- pOpts->curOptIdx++; /* argument found */
- o_st->pzOptArg = pzLA;
- }
- }
- break;
- default:
- case TOPT_DEFAULT:
- ao_bug(zbad_default_msg);
- }
- /*
- * After an option with an optional argument, we will
- * *always* start with the next option because if there
- * were any characters following the option name/flag,
- * they would be interpreted as the argument.
- */
- pOpts->pzCurOpt = NULL;
- return SUCCESS;
- }
- /**
- * Process option that does not have an argument.
- *
- * @param[in,out] opts the program option descriptor
- * @param[in,out] o_st the option processing state
- * @returns SUCCESS or FAILURE
- */
- static tSuccess
- get_opt_arg_none(tOptions * pOpts, tOptState * o_st)
- {
- /*
- * No option argument. Make sure next time around we find
- * the correct option flag character for short options
- */
- if (o_st->optType == TOPT_SHORT)
- (pOpts->pzCurOpt)++;
- /*
- * It is a long option. Make sure there was no ``=xxx'' argument
- */
- else if (o_st->pzOptArg != NULL) {
- fprintf(stderr, zNoArg, pOpts->pzProgPath, o_st->pOD->pz_Name);
- return FAILURE;
- }
- /*
- * It is a long option. Advance to next command line argument.
- */
- else
- pOpts->pzCurOpt = NULL;
- return SUCCESS;
- }
- /**
- * Process option. Figure out whether or not to look for an option argument.
- *
- * @param[in,out] opts the program option descriptor
- * @param[in,out] o_st the option processing state
- * @returns SUCCESS or FAILURE
- */
- static tSuccess
- get_opt_arg(tOptions * opts, tOptState * o_st)
- {
- o_st->flags |= (o_st->pOD->fOptState & OPTST_PERSISTENT_MASK);
- /*
- * Disabled options and options specified to not have arguments
- * are handled with the "none" procedure. Otherwise, check the
- * optional flag and call either the "may" or "must" function.
- */
- if ((o_st->flags & OPTST_DISABLED) != 0)
- return get_opt_arg_none(opts, o_st);
- switch (OPTST_GET_ARGTYPE(o_st->flags)) {
- case OPARG_TYPE_STATIC:
- {
- /*
- * Propagate the static arg
- */
- tSuccess res = get_opt_arg_none(opts, o_st);
- o_st->pzOptArg = o_st->pOD->optArg.argString;
- return res;
- }
- case OPARG_TYPE_NONE:
- return get_opt_arg_none(opts, o_st);
- }
- if (o_st->flags & OPTST_ARG_OPTIONAL)
- return get_opt_arg_may( opts, o_st);
-
- return get_opt_arg_must(opts, o_st);
- }
- /**
- * Find the option descriptor for the current option.
- *
- * @param[in,out] opts the program option descriptor
- * @param[in,out] o_st the option processing state
- * @returns SUCCESS or FAILURE
- */
- static tSuccess
- find_opt(tOptions * opts, tOptState * o_st)
- {
- /*
- * IF we are continuing a short option list (e.g. -xyz...)
- * THEN continue a single flag option.
- * OTHERWISE see if there is room to advance and then do so.
- */
- if ((opts->pzCurOpt != NULL) && (*opts->pzCurOpt != NUL))
- return opt_find_short(opts, (uint8_t)*(opts->pzCurOpt), o_st);
- if (opts->curOptIdx >= opts->origArgCt)
- return PROBLEM; /* NORMAL COMPLETION */
- opts->pzCurOpt = opts->origArgVect[ opts->curOptIdx ];
- /*
- * IF all arguments must be named options, ...
- */
- if (NAMED_OPTS(opts)) {
- char * pz = opts->pzCurOpt;
- int def;
- tSuccess res;
- uint16_t * def_opt;
- opts->curOptIdx++;
- if (*pz != '-')
- return opt_find_long(opts, pz, o_st);
- /*
- * The name is prefixed with one or more hyphens. Strip them off
- * and disable the "default_opt" setting. Use heavy recasting to
- * strip off the "const" quality of the "default_opt" field.
- */
- while (*(++pz) == '-') ;
- def_opt = VOIDP(&(opts->specOptIdx.default_opt));
- def = *def_opt;
- *def_opt = NO_EQUIVALENT;
- res = opt_find_long(opts, pz, o_st);
- *def_opt = (uint16_t)def;
- return res;
- }
- /*
- * Note the kind of flag/option marker
- */
- if (*((opts->pzCurOpt)++) != '-')
- return PROBLEM; /* NORMAL COMPLETION - this + rest are operands */
- /*
- * Special hack for a hyphen by itself
- */
- if (*(opts->pzCurOpt) == NUL)
- return PROBLEM; /* NORMAL COMPLETION - this + rest are operands */
- /*
- * The current argument is to be processed as an option argument
- */
- opts->curOptIdx++;
- /*
- * We have an option marker.
- * Test the next character for long option indication
- */
- if (opts->pzCurOpt[0] == '-') {
- if (*++(opts->pzCurOpt) == NUL)
- /*
- * NORMAL COMPLETION - NOT this arg, but rest are operands
- */
- return PROBLEM;
- /*
- * We do not allow the hyphen to be used as a flag value.
- * Therefore, if long options are not to be accepted, we punt.
- */
- if ((opts->fOptSet & OPTPROC_LONGOPT) == 0) {
- fprintf(stderr, zIllOptStr, opts->pzProgPath, opts->pzCurOpt-2);
- return FAILURE;
- }
- return opt_find_long(opts, opts->pzCurOpt, o_st);
- }
- /*
- * If short options are not allowed, then do long
- * option processing. Otherwise the character must be a
- * short (i.e. single character) option.
- */
- if ((opts->fOptSet & OPTPROC_SHORTOPT) != 0)
- return opt_find_short(opts, (uint8_t)*(opts->pzCurOpt), o_st);
- return opt_find_long(opts, opts->pzCurOpt, o_st);
- }
- /** @}
- *
- * Local Variables:
- * mode: C
- * c-file-style: "stroustrup"
- * indent-tabs-mode: nil
- * End:
- * end of autoopts/find.c */
|