reset.c revision 294904
1276479Sdim
2249259Sdim/**
3249259Sdim * \file reset.c
4249259Sdim *
5249259Sdim *  Reset the option state to the compiled state.
6249259Sdim *
7249259Sdim * @addtogroup autoopts
8249259Sdim * @{
9249259Sdim */
10276479Sdim/*
11249259Sdim *  This file is part of AutoOpts, a companion to AutoGen.
12249259Sdim *  AutoOpts is free software.
13249259Sdim *  AutoOpts is Copyright (C) 1992-2015 by Bruce Korb - all rights reserved
14280031Sdim *
15280031Sdim *  AutoOpts is available under any one of two licenses.  The license
16249259Sdim *  in use must be one of these two and the choice is under the control
17249259Sdim *  of the user of the license.
18249259Sdim *
19249259Sdim *   The GNU Lesser General Public License, version 3 or later
20249259Sdim *      See the files "COPYING.lgplv3" and "COPYING.gplv3"
21249259Sdim *
22276479Sdim *   The Modified Berkeley Software Distribution License
23276479Sdim *      See the file "COPYING.mbsd"
24276479Sdim *
25288943Sdim *  These files have the following sha256 sums:
26249259Sdim *
27249259Sdim *  8584710e9b04216a394078dc156b781d0b47e1729104d666658aecef8ee32e95  COPYING.gplv3
28276479Sdim *  4379e7444a0e2ce2b12dd6f5a52a27a4d02d39d247901d3285c88cf0d37f477b  COPYING.lgplv3
29288943Sdim *  13aa749a5b0a454917a944ed8fffc530b784f5ead522b1aacaf4ec8aa55a6239  COPYING.mbsd
30249259Sdim */
31276479Sdim
32288943Sdimstatic void
33249259SdimoptionReset(tOptions * pOpts, tOptDesc * pOD)
34276479Sdim{
35249259Sdim    pOD->fOptState &= OPTST_PERSISTENT_MASK;
36276479Sdim    pOD->fOptState |= OPTST_RESET;
37288943Sdim    if (pOD->pOptProc != NULL)
38288943Sdim        pOD->pOptProc(pOpts, pOD);
39288943Sdim    pOD->optArg.argString =
40249259Sdim        pOpts->originalOptArgArray[ pOD->optIndex ].argString;
41276479Sdim    pOD->optCookie = pOpts->originalOptArgCookie[ pOD->optIndex ];
42276479Sdim    pOD->fOptState &= OPTST_PERSISTENT_MASK;
43276479Sdim}
44276479Sdim
45276479Sdim
46276479Sdimstatic void
47249259SdimoptionResetEverything(tOptions * pOpts)
48276479Sdim{
49276479Sdim    tOptDesc * pOD = pOpts->pOptDesc;
50276479Sdim    int        ct  = pOpts->presetOptCt;
51276479Sdim
52276479Sdim    for (;;) {
53276479Sdim        optionReset(pOpts, pOD);
54276479Sdim
55276479Sdim        if (--ct <= 0)
56276479Sdim            break;
57276479Sdim        pOD++;
58276479Sdim    }
59276479Sdim}
60288943Sdim
61288943Sdim
62276479Sdim/*=export_func  optionResetOpt
63276479Sdim * private:
64249259Sdim *
65276479Sdim * what:  Reset the value of an option
66276479Sdim * arg:   + tOptions * + pOpts    + program options descriptor  +
67276479Sdim * arg:   + tOptDesc * + pOptDesc + the descriptor for this arg +
68276479Sdim *
69249259Sdim * doc:
70276479Sdim *  This code will cause another option to be reset to its initial state.
71276479Sdim *  For example, --reset=foo will cause the --foo option to be reset.
72276479Sdim=*/
73249259Sdimvoid
74276479SdimoptionResetOpt(tOptions * pOpts, tOptDesc * pOD)
75288943Sdim{
76276479Sdim    static bool reset_active = false;
77276479Sdim
78276479Sdim    tOptState opt_state = OPTSTATE_INITIALIZER(DEFINED);
79276479Sdim    char const * pzArg = pOD->optArg.argString;
80276479Sdim    tSuccess     succ;
81276479Sdim
82276479Sdim    if (pOpts <= OPTPROC_EMIT_LIMIT)
83276479Sdim        return;
84276479Sdim
85276479Sdim    if (reset_active)
86249259Sdim        return;
87276479Sdim
88276479Sdim    if (  (! HAS_originalOptArgArray(pOpts))
89276479Sdim       || (pOpts->originalOptArgCookie == NULL))
90249259Sdim        ao_bug(zno_reset);
91276479Sdim
92276479Sdim    if ((pzArg == NULL) || (*pzArg == NUL)) {
93249259Sdim        fprintf(stderr, zreset_arg, pOpts->pzProgName, pOD->pz_Name);
94276479Sdim        pOpts->pUsageProc(pOpts, EXIT_FAILURE);
95276479Sdim        /* NOTREACHED */
96288943Sdim        assert(0 == 1);
97288943Sdim    }
98288943Sdim
99249259Sdim    reset_active = true;
100249259Sdim
101249259Sdim    if (pzArg[1] == NUL) {
102249259Sdim        if (*pzArg == '*') {
103280031Sdim            optionResetEverything(pOpts);
104            reset_active = false;
105            return;
106        }
107
108        succ = opt_find_short(pOpts, (uint8_t)*pzArg, &opt_state);
109        if (! SUCCESSFUL(succ)) {
110            fprintf(stderr, zIllOptChr, pOpts->pzProgPath, *pzArg);
111            pOpts->pUsageProc(pOpts, EXIT_FAILURE);
112            /* NOTREACHED */
113            assert(0 == 1);
114        }
115    } else {
116        succ = opt_find_long(pOpts, pzArg, &opt_state);
117        if (! SUCCESSFUL(succ)) {
118            fprintf(stderr, zIllOptStr, pOpts->pzProgPath, pzArg);
119            pOpts->pUsageProc(pOpts, EXIT_FAILURE);
120            /* NOTREACHED */
121            assert(0 == 1);
122        }
123    }
124
125    /*
126     *  We've found the indicated option.  Turn off all non-persistent
127     *  flags because we're forcing the option back to its initialized state.
128     *  Call any callout procedure to handle whatever it needs to.
129     *  Finally, clear the reset flag, too.
130     */
131    optionReset(pOpts, opt_state.pOD);
132    reset_active = false;
133}
134/** @}
135 *
136 * Local Variables:
137 * mode: C
138 * c-file-style: "stroustrup"
139 * indent-tabs-mode: nil
140 * End:
141 * end of autoopts/reset.c */
142