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