133965Sjdp
2218822Sdim/**
378828Sobrien * \file reset.c
433965Sjdp *
533965Sjdp *  Reset the option state to the compiled state.
633965Sjdp *
733965Sjdp * @addtogroup autoopts
833965Sjdp * @{
933965Sjdp */
1033965Sjdp/*
1133965Sjdp *  This file is part of AutoOpts, a companion to AutoGen.
1233965Sjdp *  AutoOpts is free software.
1333965Sjdp *  AutoOpts is Copyright (C) 1992-2015 by Bruce Korb - all rights reserved
1433965Sjdp *
1533965Sjdp *  AutoOpts is available under any one of two licenses.  The license
1633965Sjdp *  in use must be one of these two and the choice is under the control
1733965Sjdp *  of the user of the license.
1833965Sjdp *
1933965Sjdp *   The GNU Lesser General Public License, version 3 or later
2033965Sjdp *      See the files "COPYING.lgplv3" and "COPYING.gplv3"
2133965Sjdp *
2233965Sjdp *   The Modified Berkeley Software Distribution License
23218822Sdim *      See the file "COPYING.mbsd"
2433965Sjdp *
2533965Sjdp *  These files have the following sha256 sums:
2633965Sjdp *
2733965Sjdp *  8584710e9b04216a394078dc156b781d0b47e1729104d666658aecef8ee32e95  COPYING.gplv3
2833965Sjdp *  4379e7444a0e2ce2b12dd6f5a52a27a4d02d39d247901d3285c88cf0d37f477b  COPYING.lgplv3
2978828Sobrien *  13aa749a5b0a454917a944ed8fffc530b784f5ead522b1aacaf4ec8aa55a6239  COPYING.mbsd
3033965Sjdp */
3160484Sobrien
3233965Sjdpstatic void
3333965SjdpoptionReset(tOptions * pOpts, tOptDesc * pOD)
3433965Sjdp{
3533965Sjdp    pOD->fOptState &= OPTST_PERSISTENT_MASK;
3633965Sjdp    pOD->fOptState |= OPTST_RESET;
3733965Sjdp    if (pOD->pOptProc != NULL)
3833965Sjdp        pOD->pOptProc(pOpts, pOD);
3933965Sjdp    pOD->optArg.argString =
4033965Sjdp        pOpts->originalOptArgArray[ pOD->optIndex ].argString;
4133965Sjdp    pOD->optCookie = pOpts->originalOptArgCookie[ pOD->optIndex ];
4233965Sjdp    pOD->fOptState &= OPTST_PERSISTENT_MASK;
4333965Sjdp}
4433965Sjdp
4533965Sjdp
4633965Sjdpstatic void
4733965SjdpoptionResetEverything(tOptions * pOpts)
4833965Sjdp{
4933965Sjdp    tOptDesc * pOD = pOpts->pOptDesc;
5033965Sjdp    int        ct  = pOpts->presetOptCt;
5133965Sjdp
5233965Sjdp    for (;;) {
5333965Sjdp        optionReset(pOpts, pOD);
5433965Sjdp
5533965Sjdp        if (--ct <= 0)
5633965Sjdp            break;
5733965Sjdp        pOD++;
5833965Sjdp    }
5933965Sjdp}
6033965Sjdp
6133965Sjdp
6233965Sjdp/*=export_func  optionResetOpt
6333965Sjdp * private:
6433965Sjdp *
6533965Sjdp * what:  Reset the value of an option
6633965Sjdp * arg:   + tOptions * + pOpts    + program options descriptor  +
6733965Sjdp * arg:   + tOptDesc * + pOptDesc + the descriptor for this arg +
6833965Sjdp *
6933965Sjdp * doc:
7033965Sjdp *  This code will cause another option to be reset to its initial state.
7133965Sjdp *  For example, --reset=foo will cause the --foo option to be reset.
7233965Sjdp=*/
7333965Sjdpvoid
7433965SjdpoptionResetOpt(tOptions * pOpts, tOptDesc * pOD)
7533965Sjdp{
7633965Sjdp    static bool reset_active = false;
7733965Sjdp
7833965Sjdp    tOptState opt_state = OPTSTATE_INITIALIZER(DEFINED);
7933965Sjdp    char const * pzArg = pOD->optArg.argString;
8033965Sjdp    tSuccess     succ;
8133965Sjdp
8233965Sjdp    if (pOpts <= OPTPROC_EMIT_LIMIT)
8333965Sjdp        return;
8433965Sjdp
8533965Sjdp    if (reset_active)
8633965Sjdp        return;
8733965Sjdp
8833965Sjdp    if (  (! HAS_originalOptArgArray(pOpts))
8933965Sjdp       || (pOpts->originalOptArgCookie == NULL))
9033965Sjdp        ao_bug(zno_reset);
9133965Sjdp
9233965Sjdp    if ((pzArg == NULL) || (*pzArg == NUL)) {
9333965Sjdp        fprintf(stderr, zreset_arg, pOpts->pzProgName, pOD->pz_Name);
9433965Sjdp        pOpts->pUsageProc(pOpts, EXIT_FAILURE);
9533965Sjdp        /* NOTREACHED */
9633965Sjdp        assert(0 == 1);
9733965Sjdp    }
9833965Sjdp
9933965Sjdp    reset_active = true;
10033965Sjdp
10133965Sjdp    if (pzArg[1] == NUL) {
10233965Sjdp        if (*pzArg == '*') {
10333965Sjdp            optionResetEverything(pOpts);
10433965Sjdp            reset_active = false;
10533965Sjdp            return;
10633965Sjdp        }
10733965Sjdp
10833965Sjdp        succ = opt_find_short(pOpts, (uint8_t)*pzArg, &opt_state);
10933965Sjdp        if (! SUCCESSFUL(succ)) {
11033965Sjdp            fprintf(stderr, zIllOptChr, pOpts->pzProgPath, *pzArg);
11133965Sjdp            pOpts->pUsageProc(pOpts, EXIT_FAILURE);
11233965Sjdp            /* NOTREACHED */
11333965Sjdp            assert(0 == 1);
11433965Sjdp        }
11533965Sjdp    } else {
11633965Sjdp        succ = opt_find_long(pOpts, pzArg, &opt_state);
11733965Sjdp        if (! SUCCESSFUL(succ)) {
11833965Sjdp            fprintf(stderr, zIllOptStr, pOpts->pzProgPath, pzArg);
11933965Sjdp            pOpts->pUsageProc(pOpts, EXIT_FAILURE);
12033965Sjdp            /* NOTREACHED */
12133965Sjdp            assert(0 == 1);
12233965Sjdp        }
12333965Sjdp    }
12433965Sjdp
12533965Sjdp    /*
12633965Sjdp     *  We've found the indicated option.  Turn off all non-persistent
12733965Sjdp     *  flags because we're forcing the option back to its initialized state.
12833965Sjdp     *  Call any callout procedure to handle whatever it needs to.
12933965Sjdp     *  Finally, clear the reset flag, too.
13033965Sjdp     */
13133965Sjdp    optionReset(pOpts, opt_state.pOD);
13233965Sjdp    reset_active = false;
13333965Sjdp}
13433965Sjdp/** @}
13533965Sjdp *
13633965Sjdp * Local Variables:
13733965Sjdp * mode: C
13833965Sjdp * c-file-style: "stroustrup"
13933965Sjdp * indent-tabs-mode: nil
14033965Sjdp * End:
14133965Sjdp * end of autoopts/reset.c */
14233965Sjdp