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