Blame src/libopts/restore.c

Packit aea12f
Packit aea12f
/*
Packit aea12f
 * \file restore.c
Packit aea12f
 *
Packit aea12f
 *  This module's routines will save the current option state to memory
Packit aea12f
 *  and restore it.  If saved prior to the initial optionProcess call,
Packit aea12f
 *  then the initial state will be restored.
Packit aea12f
 *
Packit aea12f
 * @addtogroup autoopts
Packit aea12f
 * @{
Packit aea12f
 */
Packit aea12f
/*
Packit aea12f
 *  This file is part of AutoOpts, a companion to AutoGen.
Packit aea12f
 *  AutoOpts is free software.
Packit Service 991b93
 *  AutoOpts is Copyright (C) 1992-2018 by Bruce Korb - all rights reserved
Packit aea12f
 *
Packit aea12f
 *  AutoOpts is available under any one of two licenses.  The license
Packit aea12f
 *  in use must be one of these two and the choice is under the control
Packit aea12f
 *  of the user of the license.
Packit aea12f
 *
Packit aea12f
 *   The GNU Lesser General Public License, version 3 or later
Packit aea12f
 *      See the files "COPYING.lgplv3" and "COPYING.gplv3"
Packit aea12f
 *
Packit aea12f
 *   The Modified Berkeley Software Distribution License
Packit aea12f
 *      See the file "COPYING.mbsd"
Packit aea12f
 *
Packit aea12f
 *  These files have the following sha256 sums:
Packit aea12f
 *
Packit aea12f
 *  8584710e9b04216a394078dc156b781d0b47e1729104d666658aecef8ee32e95  COPYING.gplv3
Packit aea12f
 *  4379e7444a0e2ce2b12dd6f5a52a27a4d02d39d247901d3285c88cf0d37f477b  COPYING.lgplv3
Packit aea12f
 *  13aa749a5b0a454917a944ed8fffc530b784f5ead522b1aacaf4ec8aa55a6239  COPYING.mbsd
Packit aea12f
 */
Packit aea12f
Packit aea12f
/*
Packit aea12f
 *  optionFixupSavedOpts  Really, it just wipes out option state for
Packit aea12f
 *  options that are troublesome to copy.  viz., stacked strings and
Packit aea12f
 *  hierarcicaly valued option args.  We do duplicate string args that
Packit aea12f
 *  have been marked as allocated though.
Packit aea12f
 */
Packit aea12f
static void
Packit aea12f
fixupSavedOptionArgs(tOptions * pOpts)
Packit aea12f
{
Packit aea12f
    tOptions * p   = pOpts->pSavedState;
Packit aea12f
    tOptDesc * pOD = pOpts->pOptDesc;
Packit aea12f
    int        ct  = pOpts->optCt;
Packit aea12f
Packit aea12f
    /*
Packit aea12f
     *  Make sure that allocated stuff is only referenced in the
Packit aea12f
     *  archived copy of the data.
Packit aea12f
     */
Packit aea12f
    for (; ct-- > 0; pOD++)  {
Packit aea12f
        switch (OPTST_GET_ARGTYPE(pOD->fOptState)) {
Packit aea12f
        case OPARG_TYPE_STRING:
Packit aea12f
            if (pOD->fOptState & OPTST_STACKED) {
Packit aea12f
                tOptDesc * q = p->pOptDesc + (pOD - pOpts->pOptDesc);
Packit aea12f
                q->optCookie = NULL;
Packit aea12f
            }
Packit aea12f
            if (pOD->fOptState & OPTST_ALLOC_ARG) {
Packit aea12f
                tOptDesc * q = p->pOptDesc + (pOD - pOpts->pOptDesc);
Packit aea12f
                AGDUPSTR(q->optArg.argString, pOD->optArg.argString, "arg");
Packit aea12f
            }
Packit aea12f
            break;
Packit aea12f
Packit aea12f
        case OPARG_TYPE_HIERARCHY:
Packit aea12f
        {
Packit aea12f
            tOptDesc * q = p->pOptDesc + (pOD - pOpts->pOptDesc);
Packit aea12f
            q->optCookie = NULL;
Packit aea12f
        }
Packit aea12f
        }
Packit aea12f
    }
Packit aea12f
}
Packit aea12f
Packit aea12f
/*=export_func optionSaveState
Packit aea12f
 *
Packit aea12f
 * what:  saves the option state to memory
Packit aea12f
 * arg:   tOptions *, pOpts, program options descriptor
Packit aea12f
 *
Packit aea12f
 * doc:
Packit aea12f
 *
Packit aea12f
 *  This routine will allocate enough memory to save the current option
Packit aea12f
 *  processing state.  If this routine has been called before, that memory
Packit aea12f
 *  will be reused.  You may only save one copy of the option state.  This
Packit aea12f
 *  routine may be called before optionProcess(3AO).  If you do call it
Packit aea12f
 *  before the first call to optionProcess, then you may also change the
Packit aea12f
 *  contents of argc/argv after you call optionRestore(3AO)
Packit aea12f
 *
Packit aea12f
 *  In fact, more strongly put: it is safest to only use this function
Packit aea12f
 *  before having processed any options.  In particular, the saving and
Packit aea12f
 *  restoring of stacked string arguments and hierarchical values is
Packit aea12f
 *  disabled.  The values are not saved.
Packit aea12f
 *
Packit aea12f
 * err:   If it fails to allocate the memory,
Packit aea12f
 *        it will print a message to stderr and exit.
Packit aea12f
 *        Otherwise, it will always succeed.
Packit aea12f
=*/
Packit aea12f
void
Packit aea12f
optionSaveState(tOptions * pOpts)
Packit aea12f
{
Packit aea12f
    tOptions * p = (tOptions *)pOpts->pSavedState;
Packit aea12f
Packit aea12f
    if (p == NULL) {
Packit aea12f
        size_t sz = sizeof(*pOpts)
Packit aea12f
            + ((size_t)pOpts->optCt * sizeof(tOptDesc));
Packit aea12f
        p = AGALOC(sz, "saved option state");
Packit aea12f
Packit aea12f
        pOpts->pSavedState = p;
Packit aea12f
    }
Packit aea12f
Packit aea12f
    memcpy(p, pOpts, sizeof(*p));
Packit aea12f
    memcpy(p + 1, pOpts->pOptDesc, (size_t)p->optCt * sizeof(tOptDesc));
Packit aea12f
Packit aea12f
    fixupSavedOptionArgs(pOpts);
Packit aea12f
}
Packit aea12f
Packit aea12f
Packit aea12f
/*=export_func optionRestore
Packit aea12f
 *
Packit aea12f
 * what:  restore option state from memory copy
Packit aea12f
 * arg:   tOptions *, pOpts, program options descriptor
Packit aea12f
 *
Packit aea12f
 * doc:  Copy back the option state from saved memory.
Packit aea12f
 *       The allocated memory is left intact, so this routine can be
Packit aea12f
 *       called repeatedly without having to call optionSaveState again.
Packit aea12f
 *       If you are restoring a state that was saved before the first call
Packit aea12f
 *       to optionProcess(3AO), then you may change the contents of the
Packit aea12f
 *       argc/argv parameters to optionProcess.
Packit aea12f
 *
Packit aea12f
 * err:  If you have not called @code{optionSaveState} before, a diagnostic is
Packit aea12f
 *       printed to @code{stderr} and exit is called.
Packit aea12f
=*/
Packit aea12f
void
Packit aea12f
optionRestore(tOptions * pOpts)
Packit aea12f
{
Packit aea12f
    tOptions * p = (tOptions *)pOpts->pSavedState;
Packit aea12f
Packit aea12f
    if (p == NULL) {
Packit aea12f
        char const * pzName = pOpts->pzProgName;
Packit aea12f
        if (pzName == NULL) {
Packit aea12f
            pzName = pOpts->pzPROGNAME;
Packit aea12f
            if (pzName == NULL)
Packit aea12f
                pzName = zNil;
Packit aea12f
        }
Packit aea12f
        fprintf(stderr, zNoState, pzName);
Packit aea12f
        option_exits(EXIT_FAILURE);
Packit aea12f
    }
Packit aea12f
Packit aea12f
    pOpts->pSavedState = NULL;
Packit aea12f
    optionFree(pOpts);
Packit aea12f
Packit aea12f
    memcpy(pOpts, p, sizeof(*p));
Packit aea12f
    memcpy(pOpts->pOptDesc, p+1, (size_t)p->optCt * sizeof(tOptDesc));
Packit aea12f
    pOpts->pSavedState = p;
Packit aea12f
Packit aea12f
    fixupSavedOptionArgs(pOpts);
Packit aea12f
}
Packit aea12f
Packit aea12f
/* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
Packit aea12f
Packit aea12f
/*=export_func optionFree
Packit aea12f
 *
Packit aea12f
 * what:  free allocated option processing memory
Packit aea12f
 * arg:   tOptions *, pOpts, program options descriptor
Packit aea12f
 *
Packit aea12f
 * doc:   AutoOpts sometimes allocates memory and puts pointers to it in the
Packit aea12f
 *        option state structures.  This routine deallocates all such memory.
Packit aea12f
 *
Packit aea12f
 * err:   As long as memory has not been corrupted,
Packit aea12f
 *        this routine is always successful.
Packit aea12f
=*/
Packit aea12f
void
Packit aea12f
optionFree(tOptions * pOpts)
Packit aea12f
{
Packit aea12f
 free_saved_state:
Packit aea12f
    {
Packit aea12f
        tOptDesc * p = pOpts->pOptDesc;
Packit aea12f
        int ct = pOpts->optCt;
Packit aea12f
        do  {
Packit aea12f
            if (p->fOptState & OPTST_ALLOC_ARG) {
Packit aea12f
                AGFREE(p->optArg.argString);
Packit aea12f
                p->optArg.argString = NULL;
Packit aea12f
                p->fOptState &= ~OPTST_ALLOC_ARG;
Packit aea12f
            }
Packit aea12f
Packit aea12f
            switch (OPTST_GET_ARGTYPE(p->fOptState)) {
Packit aea12f
            case OPARG_TYPE_STRING:
Packit aea12f
#ifdef WITH_LIBREGEX
Packit aea12f
                if (  (p->fOptState & OPTST_STACKED)
Packit aea12f
                   && (p->optCookie != NULL)) {
Packit aea12f
                    p->optArg.argString = ".*";
Packit aea12f
                    optionUnstackArg(pOpts, p);
Packit aea12f
                }
Packit aea12f
#else
Packit aea12f
                /* leak memory */;
Packit aea12f
#endif
Packit aea12f
                break;
Packit aea12f
Packit aea12f
            case OPARG_TYPE_HIERARCHY:
Packit aea12f
                if (p->optCookie != NULL)
Packit aea12f
                    unload_arg_list(p->optCookie);
Packit aea12f
                break;
Packit aea12f
            }
Packit aea12f
Packit aea12f
            p->optCookie = NULL;
Packit aea12f
        } while (p++, --ct > 0);
Packit aea12f
    }
Packit aea12f
    if (pOpts->pSavedState != NULL) {
Packit aea12f
        tOptions * p = (tOptions *)pOpts->pSavedState;
Packit aea12f
        memcpy(pOpts, p, sizeof(*p));
Packit aea12f
        memcpy(pOpts->pOptDesc, p+1, (size_t)p->optCt * sizeof(tOptDesc));
Packit aea12f
        AGFREE(pOpts->pSavedState);
Packit aea12f
        pOpts->pSavedState = NULL;
Packit aea12f
        goto free_saved_state;
Packit aea12f
    }
Packit aea12f
}
Packit aea12f
Packit aea12f
/** @}
Packit aea12f
 *
Packit aea12f
 * Local Variables:
Packit aea12f
 * mode: C
Packit aea12f
 * c-file-style: "stroustrup"
Packit aea12f
 * indent-tabs-mode: nil
Packit aea12f
 * End:
Packit aea12f
 * end of autoopts/restore.c */