1
2/*
3 *  $Id: 808e536555f06924b450ab6b5a72c03b67c5b99a $
4 *  Time-stamp:      "2009-11-01 11:45:57 bkorb"
5 *
6 *  This file is part of AutoOpts, a companion to AutoGen.
7 *  AutoOpts is free software.
8 *  AutoOpts is copyright (c) 1992-2009 by Bruce Korb - all rights reserved
9 *
10 *  AutoOpts is available under any one of two licenses.  The license
11 *  in use must be one of these two and the choice is under the control
12 *  of the user of the license.
13 *
14 *   The GNU Lesser General Public License, version 3 or later
15 *      See the files "COPYING.lgplv3" and "COPYING.gplv3"
16 *
17 *   The Modified Berkeley Software Distribution License
18 *      See the file "COPYING.mbsd"
19 *
20 *  These files have the following md5sums:
21 *
22 *  43b91e8ca915626ed3818ffb1b71248b pkg/libopts/COPYING.gplv3
23 *  06a1a2e4760c90ea5e1dad8dfaac4d39 pkg/libopts/COPYING.lgplv3
24 *  66a5cedaf62c4b2637025f049f9b826f pkg/libopts/COPYING.mbsd
25 */
26
27static void
28optionReset( tOptions* pOpts, tOptDesc* pOD )
29{
30    pOD->fOptState &= OPTST_PERSISTENT_MASK;
31    pOD->fOptState |= OPTST_RESET;
32    if (pOD->pOptProc != NULL)
33        pOD->pOptProc(pOpts, pOD);
34    pOD->optArg.argString =
35        pOpts->originalOptArgArray[ pOD->optIndex ].argString;
36    pOD->optCookie = pOpts->originalOptArgCookie[ pOD->optIndex ];
37    pOD->fOptState &= OPTST_PERSISTENT_MASK;
38}
39
40
41static void
42optionResetEverything(tOptions * pOpts)
43{
44    tOptDesc * pOD = pOpts->pOptDesc;
45    int        ct  = pOpts->presetOptCt;
46
47    for (;;) {
48        optionReset(pOpts, pOD);
49
50        if (--ct <= 0)
51            break;
52        pOD++;
53    }
54}
55
56
57/*=export_func  optionResetOpt
58 * private:
59 *
60 * what:  Reset the value of an option
61 * arg:   + tOptions* + pOpts    + program options descriptor  +
62 * arg:   + tOptDesc* + pOptDesc + the descriptor for this arg +
63 *
64 * doc:
65 *  This code will cause another option to be reset to its initial state.
66 *  For example, --reset=foo will cause the --foo option to be reset.
67=*/
68void
69optionResetOpt( tOptions* pOpts, tOptDesc* pOD )
70{
71    static ag_bool reset_active = AG_FALSE;
72
73    tOptState opt_state = OPTSTATE_INITIALIZER(DEFINED);
74    char const * pzArg = pOD->optArg.argString;
75    tSuccess     succ;
76
77    if (reset_active)
78        return;
79
80    if (  (! HAS_originalOptArgArray(pOpts))
81       || (pOpts->originalOptArgCookie == NULL)) {
82        fputs(zResetNotConfig, stderr);
83        _exit(EX_SOFTWARE);
84    }
85
86    if ((pzArg == NULL) || (*pzArg == NUL)) {
87        fputs(zNoResetArg, stderr);
88        pOpts->pUsageProc(pOpts, EXIT_FAILURE);
89        /* NOTREACHED */
90        assert(0 == 1);
91    }
92
93    reset_active = AG_TRUE;
94
95    if (pzArg[1] == NUL) {
96        if (*pzArg == '*') {
97            optionResetEverything(pOpts);
98            reset_active = AG_FALSE;
99            return;
100        }
101
102        succ = shortOptionFind(pOpts, (tAoUC)*pzArg, &opt_state);
103        if (! SUCCESSFUL(succ)) {
104            fprintf(stderr, zIllOptChr, pOpts->pzProgPath, *pzArg);
105            pOpts->pUsageProc(pOpts, EXIT_FAILURE);
106            /* NOTREACHED */
107            assert(0 == 1);
108        }
109    } else {
110        succ = longOptionFind(pOpts, (char *)pzArg, &opt_state);
111        if (! SUCCESSFUL(succ)) {
112            fprintf(stderr, zIllOptStr, pOpts->pzProgPath, pzArg);
113            pOpts->pUsageProc(pOpts, EXIT_FAILURE);
114            /* NOTREACHED */
115            assert(0 == 1);
116        }
117    }
118
119    /*
120     *  We've found the indicated option.  Turn off all non-persistent
121     *  flags because we're forcing the option back to its initialized state.
122     *  Call any callout procedure to handle whatever it needs to.
123     *  Finally, clear the reset flag, too.
124     */
125    optionReset(pOpts, opt_state.pOD);
126    reset_active = AG_FALSE;
127}
128/*
129 * Local Variables:
130 * mode: C
131 * c-file-style: "stroustrup"
132 * indent-tabs-mode: nil
133 * End:
134 * end of autoopts/reset.c */
135