Blame src/libopts/restore.c

Packit Service 4684c1
Packit Service 4684c1
/*
Packit Service 4684c1
 * \file restore.c
Packit Service 4684c1
 *
Packit Service 4684c1
 *  This module's routines will save the current option state to memory
Packit Service 4684c1
 *  and restore it.  If saved prior to the initial optionProcess call,
Packit Service 4684c1
 *  then the initial state will be restored.
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
 *  optionFixupSavedOpts  Really, it just wipes out option state for
Packit Service 4684c1
 *  options that are troublesome to copy.  viz., stacked strings and
Packit Service 4684c1
 *  hierarcicaly valued option args.  We do duplicate string args that
Packit Service 4684c1
 *  have been marked as allocated though.
Packit Service 4684c1
 */
Packit Service 4684c1
static void
Packit Service 4684c1
fixupSavedOptionArgs(tOptions * pOpts)
Packit Service 4684c1
{
Packit Service 4684c1
    tOptions * p   = pOpts->pSavedState;
Packit Service 4684c1
    tOptDesc * pOD = pOpts->pOptDesc;
Packit Service 4684c1
    int        ct  = pOpts->optCt;
Packit Service 4684c1
Packit Service 4684c1
    /*
Packit Service 4684c1
     *  Make sure that allocated stuff is only referenced in the
Packit Service 4684c1
     *  archived copy of the data.
Packit Service 4684c1
     */
Packit Service 4684c1
    for (; ct-- > 0; pOD++)  {
Packit Service 4684c1
        switch (OPTST_GET_ARGTYPE(pOD->fOptState)) {
Packit Service 4684c1
        case OPARG_TYPE_STRING:
Packit Service 4684c1
            if (pOD->fOptState & OPTST_STACKED) {
Packit Service 4684c1
                tOptDesc * q = p->pOptDesc + (pOD - pOpts->pOptDesc);
Packit Service 4684c1
                q->optCookie = NULL;
Packit Service 4684c1
            }
Packit Service 4684c1
            if (pOD->fOptState & OPTST_ALLOC_ARG) {
Packit Service 4684c1
                tOptDesc * q = p->pOptDesc + (pOD - pOpts->pOptDesc);
Packit Service 4684c1
                AGDUPSTR(q->optArg.argString, pOD->optArg.argString, "arg");
Packit Service 4684c1
            }
Packit Service 4684c1
            break;
Packit Service 4684c1
Packit Service 4684c1
        case OPARG_TYPE_HIERARCHY:
Packit Service 4684c1
        {
Packit Service 4684c1
            tOptDesc * q = p->pOptDesc + (pOD - pOpts->pOptDesc);
Packit Service 4684c1
            q->optCookie = NULL;
Packit Service 4684c1
        }
Packit Service 4684c1
        }
Packit Service 4684c1
    }
Packit Service 4684c1
}
Packit Service 4684c1
Packit Service 4684c1
/*=export_func optionSaveState
Packit Service 4684c1
 *
Packit Service 4684c1
 * what:  saves the option state to memory
Packit Service 4684c1
 * arg:   tOptions *, pOpts, program options descriptor
Packit Service 4684c1
 *
Packit Service 4684c1
 * doc:
Packit Service 4684c1
 *
Packit Service 4684c1
 *  This routine will allocate enough memory to save the current option
Packit Service 4684c1
 *  processing state.  If this routine has been called before, that memory
Packit Service 4684c1
 *  will be reused.  You may only save one copy of the option state.  This
Packit Service 4684c1
 *  routine may be called before optionProcess(3AO).  If you do call it
Packit Service 4684c1
 *  before the first call to optionProcess, then you may also change the
Packit Service 4684c1
 *  contents of argc/argv after you call optionRestore(3AO)
Packit Service 4684c1
 *
Packit Service 4684c1
 *  In fact, more strongly put: it is safest to only use this function
Packit Service 4684c1
 *  before having processed any options.  In particular, the saving and
Packit Service 4684c1
 *  restoring of stacked string arguments and hierarchical values is
Packit Service 4684c1
 *  disabled.  The values are not saved.
Packit Service 4684c1
 *
Packit Service 4684c1
 * err:   If it fails to allocate the memory,
Packit Service 4684c1
 *        it will print a message to stderr and exit.
Packit Service 4684c1
 *        Otherwise, it will always succeed.
Packit Service 4684c1
=*/
Packit Service 4684c1
void
Packit Service 4684c1
optionSaveState(tOptions * pOpts)
Packit Service 4684c1
{
Packit Service 4684c1
    tOptions * p = (tOptions *)pOpts->pSavedState;
Packit Service 4684c1
Packit Service 4684c1
    if (p == NULL) {
Packit Service 4684c1
        size_t sz = sizeof(*pOpts)
Packit Service 4684c1
            + ((size_t)pOpts->optCt * sizeof(tOptDesc));
Packit Service 4684c1
        p = AGALOC(sz, "saved option state");
Packit Service 4684c1
Packit Service 4684c1
        pOpts->pSavedState = p;
Packit Service 4684c1
    }
Packit Service 4684c1
Packit Service 4684c1
    memcpy(p, pOpts, sizeof(*p));
Packit Service 4684c1
    memcpy(p + 1, pOpts->pOptDesc, (size_t)p->optCt * sizeof(tOptDesc));
Packit Service 4684c1
Packit Service 4684c1
    fixupSavedOptionArgs(pOpts);
Packit Service 4684c1
}
Packit Service 4684c1
Packit Service 4684c1
Packit Service 4684c1
/*=export_func optionRestore
Packit Service 4684c1
 *
Packit Service 4684c1
 * what:  restore option state from memory copy
Packit Service 4684c1
 * arg:   tOptions *, pOpts, program options descriptor
Packit Service 4684c1
 *
Packit Service 4684c1
 * doc:  Copy back the option state from saved memory.
Packit Service 4684c1
 *       The allocated memory is left intact, so this routine can be
Packit Service 4684c1
 *       called repeatedly without having to call optionSaveState again.
Packit Service 4684c1
 *       If you are restoring a state that was saved before the first call
Packit Service 4684c1
 *       to optionProcess(3AO), then you may change the contents of the
Packit Service 4684c1
 *       argc/argv parameters to optionProcess.
Packit Service 4684c1
 *
Packit Service 4684c1
 * err:  If you have not called @code{optionSaveState} before, a diagnostic is
Packit Service 4684c1
 *       printed to @code{stderr} and exit is called.
Packit Service 4684c1
=*/
Packit Service 4684c1
void
Packit Service 4684c1
optionRestore(tOptions * pOpts)
Packit Service 4684c1
{
Packit Service 4684c1
    tOptions * p = (tOptions *)pOpts->pSavedState;
Packit Service 4684c1
Packit Service 4684c1
    if (p == NULL) {
Packit Service 4684c1
        char const * pzName = pOpts->pzProgName;
Packit Service 4684c1
        if (pzName == NULL) {
Packit Service 4684c1
            pzName = pOpts->pzPROGNAME;
Packit Service 4684c1
            if (pzName == NULL)
Packit Service 4684c1
                pzName = zNil;
Packit Service 4684c1
        }
Packit Service 4684c1
        fprintf(stderr, zNoState, pzName);
Packit Service 4684c1
        option_exits(EXIT_FAILURE);
Packit Service 4684c1
    }
Packit Service 4684c1
Packit Service 4684c1
    pOpts->pSavedState = NULL;
Packit Service 4684c1
    optionFree(pOpts);
Packit Service 4684c1
Packit Service 4684c1
    memcpy(pOpts, p, sizeof(*p));
Packit Service 4684c1
    memcpy(pOpts->pOptDesc, p+1, (size_t)p->optCt * sizeof(tOptDesc));
Packit Service 4684c1
    pOpts->pSavedState = p;
Packit Service 4684c1
Packit Service 4684c1
    fixupSavedOptionArgs(pOpts);
Packit Service 4684c1
}
Packit Service 4684c1
Packit Service 4684c1
/* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
Packit Service 4684c1
Packit Service 4684c1
/*=export_func optionFree
Packit Service 4684c1
 *
Packit Service 4684c1
 * what:  free allocated option processing memory
Packit Service 4684c1
 * arg:   tOptions *, pOpts, program options descriptor
Packit Service 4684c1
 *
Packit Service 4684c1
 * doc:   AutoOpts sometimes allocates memory and puts pointers to it in the
Packit Service 4684c1
 *        option state structures.  This routine deallocates all such memory.
Packit Service 4684c1
 *
Packit Service 4684c1
 * err:   As long as memory has not been corrupted,
Packit Service 4684c1
 *        this routine is always successful.
Packit Service 4684c1
=*/
Packit Service 4684c1
void
Packit Service 4684c1
optionFree(tOptions * pOpts)
Packit Service 4684c1
{
Packit Service 4684c1
 free_saved_state:
Packit Service 4684c1
    {
Packit Service 4684c1
        tOptDesc * p = pOpts->pOptDesc;
Packit Service 4684c1
        int ct = pOpts->optCt;
Packit Service 4684c1
        do  {
Packit Service 4684c1
            if (p->fOptState & OPTST_ALLOC_ARG) {
Packit Service 4684c1
                AGFREE(p->optArg.argString);
Packit Service 4684c1
                p->optArg.argString = NULL;
Packit Service 4684c1
                p->fOptState &= ~OPTST_ALLOC_ARG;
Packit Service 4684c1
            }
Packit Service 4684c1
Packit Service 4684c1
            switch (OPTST_GET_ARGTYPE(p->fOptState)) {
Packit Service 4684c1
            case OPARG_TYPE_STRING:
Packit Service 4684c1
#ifdef WITH_LIBREGEX
Packit Service 4684c1
                if (  (p->fOptState & OPTST_STACKED)
Packit Service 4684c1
                   && (p->optCookie != NULL)) {
Packit Service 4684c1
                    p->optArg.argString = ".*";
Packit Service 4684c1
                    optionUnstackArg(pOpts, p);
Packit Service 4684c1
                }
Packit Service 4684c1
#else
Packit Service 4684c1
                /* leak memory */;
Packit Service 4684c1
#endif
Packit Service 4684c1
                break;
Packit Service 4684c1
Packit Service 4684c1
            case OPARG_TYPE_HIERARCHY:
Packit Service 4684c1
                if (p->optCookie != NULL)
Packit Service 4684c1
                    unload_arg_list(p->optCookie);
Packit Service 4684c1
                break;
Packit Service 4684c1
            }
Packit Service 4684c1
Packit Service 4684c1
            p->optCookie = NULL;
Packit Service 4684c1
        } while (p++, --ct > 0);
Packit Service 4684c1
    }
Packit Service 4684c1
    if (pOpts->pSavedState != NULL) {
Packit Service 4684c1
        tOptions * p = (tOptions *)pOpts->pSavedState;
Packit Service 4684c1
        memcpy(pOpts, p, sizeof(*p));
Packit Service 4684c1
        memcpy(pOpts->pOptDesc, p+1, (size_t)p->optCt * sizeof(tOptDesc));
Packit Service 4684c1
        AGFREE(pOpts->pSavedState);
Packit Service 4684c1
        pOpts->pSavedState = NULL;
Packit Service 4684c1
        goto free_saved_state;
Packit Service 4684c1
    }
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/restore.c */