Blame src/libopts/autoopts.c

Packit Service 4684c1
Packit Service 4684c1
/**
Packit Service 4684c1
 * \file autoopts.c
Packit Service 4684c1
 *
Packit Service 4684c1
 *  This file contains all of the routines that must be linked into
Packit Service 4684c1
 *  an executable to use the generated option processing.  The optional
Packit Service 4684c1
 *  routines are in separately compiled modules so that they will not
Packit Service 4684c1
 *  necessarily be linked in.
Packit Service 4684c1
 *
Packit Service 4684c1
 * @addtogroup autoopts
Packit Service 4684c1
 * @{
Packit Service 4684c1
 */
Packit Service 4684c1
/*
Packit Service 4684c1
 *  This file is part of AutoOpts, a companion to AutoGen.
Packit Service 4684c1
 *  AutoOpts is free software.
Packit Service 4684c1
 *  AutoOpts is Copyright (C) 1992-2018 by Bruce Korb - all rights reserved
Packit Service 4684c1
 *
Packit Service 4684c1
 *  AutoOpts is available under any one of two licenses.  The license
Packit Service 4684c1
 *  in use must be one of these two and the choice is under the control
Packit Service 4684c1
 *  of the user of the license.
Packit Service 4684c1
 *
Packit Service 4684c1
 *   The GNU Lesser General Public License, version 3 or later
Packit Service 4684c1
 *      See the files "COPYING.lgplv3" and "COPYING.gplv3"
Packit Service 4684c1
 *
Packit Service 4684c1
 *   The Modified Berkeley Software Distribution License
Packit Service 4684c1
 *      See the file "COPYING.mbsd"
Packit Service 4684c1
 *
Packit Service 4684c1
 *  These files have the following sha256 sums:
Packit Service 4684c1
 *
Packit Service 4684c1
 *  8584710e9b04216a394078dc156b781d0b47e1729104d666658aecef8ee32e95  COPYING.gplv3
Packit Service 4684c1
 *  4379e7444a0e2ce2b12dd6f5a52a27a4d02d39d247901d3285c88cf0d37f477b  COPYING.lgplv3
Packit Service 4684c1
 *  13aa749a5b0a454917a944ed8fffc530b784f5ead522b1aacaf4ec8aa55a6239  COPYING.mbsd
Packit Service 4684c1
 */
Packit Service 4684c1
Packit Service 4684c1
/**
Packit Service 4684c1
 * The number of tab characters to skip when printing continuation lines.
Packit Service 4684c1
 */
Packit Service 4684c1
  static unsigned int tab_skip_ct          = 0;
Packit Service 4684c1
Packit Service 4684c1
#ifndef HAVE_PATHFIND
Packit Service 4684c1
#  define  pathfind(_p, _n, _m) option_pathfind(_p, _n, _m)
Packit Service 4684c1
#  include "compat/pathfind.c"
Packit Service 4684c1
#endif
Packit Service 4684c1
Packit Service 4684c1
#ifndef HAVE_SNPRINTF
Packit Service 4684c1
#  define vsnprintf       option_vsnprintf
Packit Service 4684c1
#  define snprintf        option_snprintf
Packit Service 4684c1
#  include "compat/snprintf.c"
Packit Service 4684c1
#endif
Packit Service 4684c1
Packit Service 4684c1
#ifndef HAVE_STRDUP
Packit Service 4684c1
#  define  strdup(_s)     option_strdup(_s)
Packit Service 4684c1
#  include "compat/strdup.c"
Packit Service 4684c1
#endif
Packit Service 4684c1
Packit Service 4684c1
#ifndef HAVE_STRCHR
Packit Service 4684c1
#  define strrchr(_s, _c) option_strrchr(_s, _c)
Packit Service 4684c1
#  define strchr(_s, _c)  option_strchr(_s, _c)
Packit Service 4684c1
#  include "compat/strchr.c"
Packit Service 4684c1
#endif
Packit Service 4684c1
Packit Service 4684c1
static void *
Packit Service 4684c1
ao_malloc(size_t sz)
Packit Service 4684c1
{
Packit Service 4684c1
    void * res = malloc(sz);
Packit Service 4684c1
    if (res == NULL) {
Packit Service 4684c1
        fprintf(stderr, zalloc_fail, (int)sz);
Packit Service 4684c1
        option_exits(EXIT_FAILURE);
Packit Service 4684c1
    }
Packit Service 4684c1
    return res;
Packit Service 4684c1
}
Packit Service 4684c1
Packit Service 4684c1
static void *
Packit Service 4684c1
ao_realloc(void *p, size_t sz)
Packit Service 4684c1
{
Packit Service 4684c1
    void * res = (p == NULL) ? malloc(sz) : realloc(p, sz);
Packit Service 4684c1
    if (res == NULL) {
Packit Service 4684c1
        fprintf(stderr, zrealloc_fail, (int)sz, p);
Packit Service 4684c1
        option_exits(EXIT_FAILURE);
Packit Service 4684c1
    }
Packit Service 4684c1
    return res;
Packit Service 4684c1
}
Packit Service 4684c1
Packit Service 4684c1
static char *
Packit Service 4684c1
ao_strdup(char const *str)
Packit Service 4684c1
{
Packit Service 4684c1
    char * res = strdup(str);
Packit Service 4684c1
    if (res == NULL) {
Packit Service 4684c1
        fprintf(stderr, zalloc_fail, (int)strlen(str));
Packit Service 4684c1
        option_exits(EXIT_FAILURE);
Packit Service 4684c1
    }
Packit Service 4684c1
    return res;
Packit Service 4684c1
}
Packit Service 4684c1
Packit Service 4684c1
/**
Packit Service 4684c1
 *  handle an option.
Packit Service 4684c1
 *
Packit Service 4684c1
 *  This routine handles equivalencing, sets the option state flags and
Packit Service 4684c1
 *  invokes the handler procedure, if any.
Packit Service 4684c1
 */
Packit Service 4684c1
static tSuccess
Packit Service 4684c1
handle_opt(tOptions * opts, tOptState * o_st)
Packit Service 4684c1
{
Packit Service 4684c1
    /*
Packit Service 4684c1
     *  Save a copy of the option procedure pointer.
Packit Service 4684c1
     *  If this is an equivalence class option, we still want this proc.
Packit Service 4684c1
     */
Packit Service 4684c1
    tOptDesc *  od = o_st->pOD;
Packit Service 4684c1
    tOptProc *  opt_proc = od->pOptProc;
Packit Service 4684c1
    if (od->fOptState & OPTST_ALLOC_ARG)
Packit Service 4684c1
        AGFREE(od->optArg.argString);
Packit Service 4684c1
Packit Service 4684c1
    od->optArg.argString = o_st->pzOptArg;
Packit Service 4684c1
Packit Service 4684c1
    /*
Packit Service 4684c1
     *  IF we are presetting options, then we will ignore any un-presettable
Packit Service 4684c1
     *  options.  They are the ones either marked as such.
Packit Service 4684c1
     */
Packit Service 4684c1
    if (  ((opts->fOptSet & OPTPROC_PRESETTING) != 0)
Packit Service 4684c1
       && ((od->fOptState & OPTST_NO_INIT) != 0)
Packit Service 4684c1
       )
Packit Service 4684c1
        return PROBLEM;
Packit Service 4684c1
Packit Service 4684c1
    /*
Packit Service 4684c1
     *  IF this is an equivalence class option,
Packit Service 4684c1
     *  THEN
Packit Service 4684c1
     *      Save the option value that got us to this option
Packit Service 4684c1
     *      entry.  (It may not be od->optChar[0], if this is an
Packit Service 4684c1
     *      equivalence entry.)
Packit Service 4684c1
     *      set the pointer to the equivalence class base
Packit Service 4684c1
     */
Packit Service 4684c1
    if (od->optEquivIndex != NO_EQUIVALENT) {
Packit Service 4684c1
        tOptDesc * eqv_od = opts->pOptDesc + od->optEquivIndex;
Packit Service 4684c1
Packit Service 4684c1
        /*
Packit Service 4684c1
         * IF the current option state has not been defined (set on the
Packit Service 4684c1
         *    command line), THEN we will allow continued resetting of
Packit Service 4684c1
         *    the value.  Once "defined", then it must not change.
Packit Service 4684c1
         */
Packit Service 4684c1
        if ((od->fOptState & OPTST_DEFINED) != 0) {
Packit Service 4684c1
            /*
Packit Service 4684c1
             *  The equivalenced-to option has been found on the command
Packit Service 4684c1
             *  line before.  Make sure new occurrences are the same type.
Packit Service 4684c1
             *
Packit Service 4684c1
             *  IF this option has been previously equivalenced and
Packit Service 4684c1
             *     it was not the same equivalenced-to option,
Packit Service 4684c1
             *  THEN we have a usage problem.
Packit Service 4684c1
             */
Packit Service 4684c1
            if (eqv_od->optActualIndex != od->optIndex) {
Packit Service 4684c1
                fprintf(stderr, zmultiway_bug, eqv_od->pz_Name, od->pz_Name,
Packit Service 4684c1
                        (opts->pOptDesc + eqv_od->optActualIndex)->pz_Name);
Packit Service 4684c1
                return FAILURE;
Packit Service 4684c1
            }
Packit Service 4684c1
        } else {
Packit Service 4684c1
            /*
Packit Service 4684c1
             *  Set the equivalenced-to actual option index to no-equivalent
Packit Service 4684c1
             *  so that we set all the entries below.  This option may either
Packit Service 4684c1
             *  never have been selected before, or else it was selected by
Packit Service 4684c1
             *  some sort of "presetting" mechanism.
Packit Service 4684c1
             */
Packit Service 4684c1
            eqv_od->optActualIndex = NO_EQUIVALENT;
Packit Service 4684c1
        }
Packit Service 4684c1
Packit Service 4684c1
        if (eqv_od->optActualIndex != od->optIndex) {
Packit Service 4684c1
            /*
Packit Service 4684c1
             *  First time through, copy over the state
Packit Service 4684c1
             *  and add in the equivalence flag
Packit Service 4684c1
             */
Packit Service 4684c1
            eqv_od->optActualValue = od->optValue;
Packit Service 4684c1
            eqv_od->optActualIndex = od->optIndex;
Packit Service 4684c1
            o_st->flags |= OPTST_EQUIVALENCE;
Packit Service 4684c1
        }
Packit Service 4684c1
Packit Service 4684c1
        /*
Packit Service 4684c1
         *  Copy the most recent option argument.  set membership state
Packit Service 4684c1
         *  is kept in 'eqv_od->optCookie'.  Do not overwrite.
Packit Service 4684c1
         */
Packit Service 4684c1
        eqv_od->optArg.argString = od->optArg.argString;
Packit Service 4684c1
        od = eqv_od;
Packit Service 4684c1
Packit Service 4684c1
    } else {
Packit Service 4684c1
        od->optActualValue = od->optValue;
Packit Service 4684c1
        od->optActualIndex = od->optIndex;
Packit Service 4684c1
    }
Packit Service 4684c1
Packit Service 4684c1
    od->fOptState &= OPTST_PERSISTENT_MASK;
Packit Service 4684c1
    od->fOptState |= (o_st->flags & ~OPTST_PERSISTENT_MASK);
Packit Service 4684c1
Packit Service 4684c1
    /*
Packit Service 4684c1
     *  Keep track of count only for DEFINED (command line) options.
Packit Service 4684c1
     *  IF we have too many, build up an error message and bail.
Packit Service 4684c1
     */
Packit Service 4684c1
    if (  (od->fOptState & OPTST_DEFINED)
Packit Service 4684c1
       && (++od->optOccCt > od->optMaxCt)  )
Packit Service 4684c1
        return too_many_occurrences(opts, od);
Packit Service 4684c1
    /*
Packit Service 4684c1
     *  If provided a procedure to call, call it
Packit Service 4684c1
     */
Packit Service 4684c1
    if (opt_proc != NULL)
Packit Service 4684c1
        (*opt_proc)(opts, od);
Packit Service 4684c1
Packit Service 4684c1
    return SUCCESS;
Packit Service 4684c1
}
Packit Service 4684c1
Packit Service 4684c1
/**
Packit Service 4684c1
 *  Find the option descriptor and option argument (if any) for the
Packit Service 4684c1
 *  next command line argument.  DO NOT modify the descriptor.  Put
Packit Service 4684c1
 *  all the state in the state argument so that the option can be skipped
Packit Service 4684c1
 *  without consequence (side effect).
Packit Service 4684c1
 *
Packit Service 4684c1
 * @param opts the program option descriptor
Packit Service 4684c1
 * @param o_st  the state of the next found option
Packit Service 4684c1
 */
Packit Service 4684c1
static tSuccess
Packit Service 4684c1
next_opt(tOptions * opts, tOptState * o_st)
Packit Service 4684c1
{
Packit Service 4684c1
    {
Packit Service 4684c1
        tSuccess res = find_opt(opts, o_st);
Packit Service 4684c1
        if (! SUCCESSFUL(res))
Packit Service 4684c1
            return res;
Packit Service 4684c1
    }
Packit Service 4684c1
Packit Service 4684c1
    if (  ((o_st->flags & OPTST_DEFINED) != 0)
Packit Service 4684c1
       && ((o_st->pOD->fOptState & OPTST_NO_COMMAND) != 0)) {
Packit Service 4684c1
        fprintf(stderr, zNotCmdOpt, o_st->pOD->pz_Name);
Packit Service 4684c1
        return FAILURE;
Packit Service 4684c1
    }
Packit Service 4684c1
Packit Service 4684c1
    return get_opt_arg(opts, o_st);
Packit Service 4684c1
}
Packit Service 4684c1
Packit Service 4684c1
/**
Packit Service 4684c1
 * Process all the options from our current position onward.  (This allows
Packit Service 4684c1
 * interspersed options and arguments for the few non-standard programs that
Packit Service 4684c1
 * require it.)  Thus, do not rewind option indexes because some programs
Packit Service 4684c1
 * choose to re-invoke after a non-option.
Packit Service 4684c1
 *
Packit Service 4684c1
 *  @param[in,out] opts   program options descriptor
Packit Service 4684c1
 *  @returns SUCCESS or FAILURE
Packit Service 4684c1
 */
Packit Service 4684c1
static tSuccess
Packit Service 4684c1
regular_opts(tOptions * opts)
Packit Service 4684c1
{
Packit Service 4684c1
    /* assert:  opts->fOptSet & OPTPROC_IMMEDIATE == 0 */
Packit Service 4684c1
    for (;;) {
Packit Service 4684c1
        tOptState opt_st = OPTSTATE_INITIALIZER(DEFINED);
Packit Service 4684c1
Packit Service 4684c1
        switch (next_opt(opts, &opt_st)) {
Packit Service 4684c1
        case FAILURE: goto   failed_option;
Packit Service 4684c1
        case PROBLEM: return SUCCESS; /* no more args */
Packit Service 4684c1
        case SUCCESS: break;
Packit Service 4684c1
        }
Packit Service 4684c1
Packit Service 4684c1
        /*
Packit Service 4684c1
         *  IF this is an immediate action option,
Packit Service 4684c1
         *  THEN skip it (unless we are supposed to do it a second time).
Packit Service 4684c1
         */
Packit Service 4684c1
        if (! DO_NORMALLY(opt_st.flags)) {
Packit Service 4684c1
            if (! DO_SECOND_TIME(opt_st.flags))
Packit Service 4684c1
                continue;
Packit Service 4684c1
            opt_st.pOD->optOccCt--; /* don't count this repetition */
Packit Service 4684c1
        }
Packit Service 4684c1
Packit Service 4684c1
        if (! SUCCESSFUL(handle_opt(opts, &opt_st)))
Packit Service 4684c1
            break;
Packit Service 4684c1
    } failed_option:;
Packit Service 4684c1
Packit Service 4684c1
    if ((opts->fOptSet & OPTPROC_ERRSTOP) != 0)
Packit Service 4684c1
        (*opts->pUsageProc)(opts, EXIT_FAILURE);
Packit Service 4684c1
Packit Service 4684c1
    return FAILURE;
Packit Service 4684c1
}
Packit Service 4684c1
Packit Service 4684c1
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Packit Service 4684c1
 *
Packit Service 4684c1
 *  THESE ROUTINES ARE CALLABLE FROM THE GENERATED OPTION PROCESSING CODE
Packit Service 4684c1
 */
Packit Service 4684c1
/*=--subblock=arg=arg_type,arg_name,arg_desc =*/
Packit Service 4684c1
/*=*
Packit Service 4684c1
 * library:  opts
Packit Service 4684c1
 * header:   your-opts.h
Packit Service 4684c1
 *
Packit Service 4684c1
 * lib_description:
Packit Service 4684c1
 *
Packit Service 4684c1
 *  These are the routines that libopts users may call directly from their
Packit Service 4684c1
 *  code.  There are several other routines that can be called by code
Packit Service 4684c1
 *  generated by the libopts option templates, but they are not to be
Packit Service 4684c1
 *  called from any other user code.  The @file{options.h} header is
Packit Service 4684c1
 *  fairly clear about this, too.
Packit Service 4684c1
=*/
Packit Service 4684c1
Packit Service 4684c1
/*=export_func optionProcess
Packit Service 4684c1
 *
Packit Service 4684c1
 * what: this is the main option processing routine
Packit Service 4684c1
 *
Packit Service 4684c1
 * arg:  + tOptions * + opts  + program options descriptor +
Packit Service 4684c1
 * arg:  + int        + a_ct  + program arg count  +
Packit Service 4684c1
 * arg:  + char **    + a_v   + program arg vector +
Packit Service 4684c1
 *
Packit Service 4684c1
 * ret_type:  int
Packit Service 4684c1
 * ret_desc:  the count of the arguments processed
Packit Service 4684c1
 *
Packit Service 4684c1
 * doc:
Packit Service 4684c1
 *
Packit Service 4684c1
 * This is the main entry point for processing options.  It is intended
Packit Service 4684c1
 * that this procedure be called once at the beginning of the execution of
Packit Service 4684c1
 * a program.  Depending on options selected earlier, it is sometimes
Packit Service 4684c1
 * necessary to stop and restart option processing, or to select completely
Packit Service 4684c1
 * different sets of options.  This can be done easily, but you generally
Packit Service 4684c1
 * do not want to do this.
Packit Service 4684c1
 *
Packit Service 4684c1
 * The number of arguments processed always includes the program name.
Packit Service 4684c1
 * If one of the arguments is "--", then it is counted and the processing
Packit Service 4684c1
 * stops.  If an error was encountered and errors are to be tolerated, then
Packit Service 4684c1
 * the returned value is the index of the argument causing the error.
Packit Service 4684c1
 * A hyphen by itself ("-") will also cause processing to stop and will
Packit Service 4684c1
 * @emph{not} be counted among the processed arguments.  A hyphen by itself
Packit Service 4684c1
 * is treated as an operand.  Encountering an operand stops option
Packit Service 4684c1
 * processing.
Packit Service 4684c1
 *
Packit Service 4684c1
 * err:  Errors will cause diagnostics to be printed.  @code{exit(3)} may
Packit Service 4684c1
 *       or may not be called.  It depends upon whether or not the options
Packit Service 4684c1
 *       were generated with the "allow-errors" attribute, or if the
Packit Service 4684c1
 *       ERRSKIP_OPTERR or ERRSTOP_OPTERR macros were invoked.
Packit Service 4684c1
=*/
Packit Service 4684c1
int
Packit Service 4684c1
optionProcess(tOptions * opts, int a_ct, char ** a_v)
Packit Service 4684c1
{
Packit Service 4684c1
    if (! SUCCESSFUL(validate_struct(opts, a_v[0])))
Packit Service 4684c1
        ao_bug(zbad_data_msg);
Packit Service 4684c1
    
Packit Service 4684c1
    /*
Packit Service 4684c1
     *  Establish the real program name, the program full path,
Packit Service 4684c1
     *  and do all the presetting the first time thru only.
Packit Service 4684c1
     */
Packit Service 4684c1
    if (! ao_initialize(opts, a_ct, a_v))
Packit Service 4684c1
        return 0;
Packit Service 4684c1
Packit Service 4684c1
    /*
Packit Service 4684c1
     *  IF we are (re)starting,
Packit Service 4684c1
     *  THEN reset option location
Packit Service 4684c1
     */
Packit Service 4684c1
    if (opts->curOptIdx <= 0) {
Packit Service 4684c1
        opts->curOptIdx = 1;
Packit Service 4684c1
        opts->pzCurOpt  = NULL;
Packit Service 4684c1
    }
Packit Service 4684c1
Packit Service 4684c1
    if (! SUCCESSFUL(regular_opts(opts)))
Packit Service 4684c1
        return (int)opts->origArgCt;
Packit Service 4684c1
Packit Service 4684c1
    /*
Packit Service 4684c1
     *  IF    there were no errors
Packit Service 4684c1
     *    AND we have RC/INI files
Packit Service 4684c1
     *    AND there is a request to save the files
Packit Service 4684c1
     *  THEN do that now before testing for conflicts.
Packit Service 4684c1
     *       (conflicts are ignored in preset options)
Packit Service 4684c1
     */
Packit Service 4684c1
    switch (opts->specOptIdx.save_opts) {
Packit Service 4684c1
    case 0:
Packit Service 4684c1
    case NO_EQUIVALENT:
Packit Service 4684c1
        break;
Packit Service 4684c1
    default:
Packit Service 4684c1
    {
Packit Service 4684c1
        tOptDesc * od = opts->pOptDesc + opts->specOptIdx.save_opts;
Packit Service 4684c1
Packit Service 4684c1
        if (SELECTED_OPT(od)) {
Packit Service 4684c1
            optionSaveFile(opts);
Packit Service 4684c1
            option_exits(EXIT_SUCCESS);
Packit Service 4684c1
        }
Packit Service 4684c1
    }
Packit Service 4684c1
    }
Packit Service 4684c1
Packit Service 4684c1
    /*
Packit Service 4684c1
     *  IF we are checking for errors,
Packit Service 4684c1
     *  THEN look for too few occurrences of required options
Packit Service 4684c1
     */
Packit Service 4684c1
    if (((opts->fOptSet & OPTPROC_ERRSTOP) != 0)
Packit Service 4684c1
       && (! is_consistent(opts)))
Packit Service 4684c1
        (*opts->pUsageProc)(opts, EXIT_FAILURE);
Packit Service 4684c1
Packit Service 4684c1
    return (int)opts->curOptIdx;
Packit Service 4684c1
}
Packit Service 4684c1
Packit Service 4684c1
/** @}
Packit Service 4684c1
 *
Packit Service 4684c1
 * Local Variables:
Packit Service 4684c1
 * mode: C
Packit Service 4684c1
 * c-file-style: "stroustrup"
Packit Service 4684c1
 * indent-tabs-mode: nil
Packit Service 4684c1
 * End:
Packit Service 4684c1
 * end of autoopts/autoopts.c */