Esempio n. 1
0
/**
 * AutoOpts initialization
 *
 * @param[in,out] opts  the structure to initialize
 * @param[in]     a_ct  program argument count
 * @param[in]     a_v   program argument vector
 */
LOCAL bool
ao_initialize(tOptions * opts, int a_ct, char ** a_v)
{
    if ((opts->fOptSet & OPTPROC_INITDONE) != 0)
        return true;

    opts->origArgCt   = (unsigned int)a_ct;
    opts->origArgVect = a_v;
    opts->fOptSet    |= OPTPROC_INITDONE;

    if (HAS_pzPkgDataDir(opts))
        program_pkgdatadir = opts->pzPkgDataDir;

    if (! SUCCESSFUL(do_presets(opts)))
        return false;

    /*
     *  IF option name conversion was suppressed but it is not suppressed
     *  for the command line, then it's time to translate option names.
     *  Usage text will not get retranslated.
     */
    if (  ((opts->fOptSet & OPTPROC_TRANSLATE) != 0)
       && (opts->pTransProc != NULL)
       && ((opts->fOptSet & OPTPROC_NO_XLAT_MASK) == OPTPROC_NXLAT_OPT_CFG)
       )  {
        opts->fOptSet &= ~OPTPROC_NXLAT_OPT_CFG;
        (*opts->pTransProc)();
    }

    if ((opts->fOptSet & OPTPROC_REORDER) != 0)
        optionSort(opts);

    opts->curOptIdx   = 1;
    opts->pzCurOpt    = NULL;
    return true;
}
Esempio n. 2
0
/*=export_func optionProcess
 *
 * what: this is the main option processing routine
 *
 * arg:  + tOptions* + pOpts + program options descriptor +
 * arg:  + int       + argc  + program arg count  +
 * arg:  + char**    + argv  + program arg vector +
 *
 * ret_type:  int
 * ret_desc:  the count of the arguments processed
 *
 * doc:
 *
 * This is the main entry point for processing options.  It is intended
 * that this procedure be called once at the beginning of the execution of
 * a program.  Depending on options selected earlier, it is sometimes
 * necessary to stop and restart option processing, or to select completely
 * different sets of options.  This can be done easily, but you generally
 * do not want to do this.
 *
 * The number of arguments processed always includes the program name.
 * If one of the arguments is "--", then it is counted and the processing
 * stops.  If an error was encountered and errors are to be tolerated, then
 * the returned value is the index of the argument causing the error.
 * A hyphen by itself ("-") will also cause processing to stop and will
 * @emph{not} be counted among the processed arguments.  A hyphen by itself
 * is treated as an operand.  Encountering an operand stops option
 * processing.
 *
 * err:  Errors will cause diagnostics to be printed.  @code{exit(3)} may
 *       or may not be called.  It depends upon whether or not the options
 *       were generated with the "allow-errors" attribute, or if the
 *       ERRSKIP_OPTERR or ERRSTOP_OPTERR macros were invoked.
=*/
int
optionProcess(tOptions * pOpts, int argCt, char ** argVect)
{
    if (! SUCCESSFUL(validateOptionsStruct(pOpts, argVect[0])))
        exit(EX_SOFTWARE);

    /*
     *  Establish the real program name, the program full path,
     *  and do all the presetting the first time thru only.
     */
    if ((pOpts->fOptSet & OPTPROC_INITDONE) == 0) {
        pOpts->origArgCt   = argCt;
        pOpts->origArgVect = argVect;
        pOpts->fOptSet    |= OPTPROC_INITDONE;
        if (HAS_pzPkgDataDir(pOpts))
            program_pkgdatadir = pOpts->pzPkgDataDir;

        if (! SUCCESSFUL(doPresets(pOpts)))
            return 0;

        /*
         *  IF option name conversion was suppressed but it is not suppressed
         *  for the command line, then it's time to translate option names.
         *  Usage text will not get retranslated.
         */
        if (  ((pOpts->fOptSet & OPTPROC_TRANSLATE) != 0)
           && (pOpts->pTransProc != NULL)
           && ((pOpts->fOptSet & OPTPROC_NO_XLAT_MASK)
              == OPTPROC_NXLAT_OPT_CFG)  )  {

            pOpts->fOptSet &= ~OPTPROC_NXLAT_OPT_CFG;
            (*pOpts->pTransProc)();
        }

        if ((pOpts->fOptSet & OPTPROC_REORDER) != 0)
            optionSort(pOpts);

        pOpts->curOptIdx   = 1;
        pOpts->pzCurOpt    = NULL;
    }

    /*
     *  IF we are (re)starting,
     *  THEN reset option location
     */
    else if (pOpts->curOptIdx <= 0) {
        pOpts->curOptIdx = 1;
        pOpts->pzCurOpt  = NULL;
    }

    if (! SUCCESSFUL(doRegularOpts(pOpts)))
        return pOpts->origArgCt;

    /*
     *  IF    there were no errors
     *    AND we have RC/INI files
     *    AND there is a request to save the files
     *  THEN do that now before testing for conflicts.
     *       (conflicts are ignored in preset options)
     */
    if (  (pOpts->specOptIdx.save_opts != NO_EQUIVALENT)
       && (pOpts->specOptIdx.save_opts != 0)) {
        tOptDesc*  pOD = pOpts->pOptDesc + pOpts->specOptIdx.save_opts;

        if (SELECTED_OPT(pOD)) {
            optionSaveFile(pOpts);
            exit(EXIT_SUCCESS);
        }
    }

    /*
     *  IF we are checking for errors,
     *  THEN look for too few occurrences of required options
     */
    if ((pOpts->fOptSet & OPTPROC_ERRSTOP) != 0) {
        if (checkConsistency(pOpts) != 0)
            (*pOpts->pUsageProc)(pOpts, EXIT_FAILURE);
    }

    return pOpts->curOptIdx;
}