reset.c revision 285612
1218887Sdim
2218887Sdim/**
3218887Sdim * \file reset.c
4218887Sdim *
5218887Sdim *  Reset the option state to the compiled state.
6218887Sdim *
7218887Sdim * @addtogroup autoopts
8218887Sdim * @{
9218887Sdim */
10218887Sdim/*
11218887Sdim *  This file is part of AutoOpts, a companion to AutoGen.
12218887Sdim *  AutoOpts is free software.
13218887Sdim *  AutoOpts is Copyright (C) 1992-2015 by Bruce Korb - all rights reserved
14218887Sdim *
15218887Sdim *  AutoOpts is available under any one of two licenses.  The license
16221345Sdim *  in use must be one of these two and the choice is under the control
17218887Sdim *  of the user of the license.
18218887Sdim *
19218887Sdim *   The GNU Lesser General Public License, version 3 or later
20218887Sdim *      See the files "COPYING.lgplv3" and "COPYING.gplv3"
21218887Sdim *
22218887Sdim *   The Modified Berkeley Software Distribution License
23218887Sdim *      See the file "COPYING.mbsd"
24218887Sdim *
25218887Sdim *  These files have the following sha256 sums:
26218887Sdim *
27218887Sdim *  8584710e9b04216a394078dc156b781d0b47e1729104d666658aecef8ee32e95  COPYING.gplv3
28218887Sdim *  4379e7444a0e2ce2b12dd6f5a52a27a4d02d39d247901d3285c88cf0d37f477b  COPYING.lgplv3
29218887Sdim *  13aa749a5b0a454917a944ed8fffc530b784f5ead522b1aacaf4ec8aa55a6239  COPYING.mbsd
30218887Sdim */
31218887Sdim
32218887Sdimstatic void
33218887SdimoptionReset(tOptions * pOpts, tOptDesc * pOD)
34218887Sdim{
35218887Sdim    pOD->fOptState &= OPTST_PERSISTENT_MASK;
36218887Sdim    pOD->fOptState |= OPTST_RESET;
37218887Sdim    if (pOD->pOptProc != NULL)
38218887Sdim        pOD->pOptProc(pOpts, pOD);
39218887Sdim    pOD->optArg.argString =
40218887Sdim        pOpts->originalOptArgArray[ pOD->optIndex ].argString;
41218887Sdim    pOD->optCookie = pOpts->originalOptArgCookie[ pOD->optIndex ];
42218887Sdim    pOD->fOptState &= OPTST_PERSISTENT_MASK;
43218887Sdim}
44218887Sdim
45218887Sdim
46218887Sdimstatic void
47218887SdimoptionResetEverything(tOptions * pOpts)
48218887Sdim{
49218887Sdim    tOptDesc * pOD = pOpts->pOptDesc;
50218887Sdim    int        ct  = pOpts->presetOptCt;
51218887Sdim
52218887Sdim    for (;;) {
53226633Sdim        optionReset(pOpts, pOD);
54218887Sdim
55218887Sdim        if (--ct <= 0)
56218887Sdim            break;
57218887Sdim        pOD++;
58218887Sdim    }
59218887Sdim}
60218887Sdim
61218887Sdim
62218887Sdim/*=export_func  optionResetOpt
63218887Sdim * private:
64218887Sdim *
65218887Sdim * what:  Reset the value of an option
66218887Sdim * arg:   + tOptions * + pOpts    + program options descriptor  +
67218887Sdim * arg:   + tOptDesc * + pOptDesc + the descriptor for this arg +
68218887Sdim *
69218887Sdim * doc:
70218887Sdim *  This code will cause another option to be reset to its initial state.
71226633Sdim *  For example, --reset=foo will cause the --foo option to be reset.
72218887Sdim=*/
73218887Sdimvoid
74218887SdimoptionResetOpt(tOptions * pOpts, tOptDesc * pOD)
75226633Sdim{
76218887Sdim    static bool reset_active = false;
77226633Sdim
78218887Sdim    tOptState opt_state = OPTSTATE_INITIALIZER(DEFINED);
79218887Sdim    char const * pzArg = pOD->optArg.argString;
80218887Sdim    tSuccess     succ;
81218887Sdim
82218887Sdim    if (pOpts <= OPTPROC_EMIT_LIMIT)
83218887Sdim        return;
84218887Sdim
85226633Sdim    if (reset_active)
86226633Sdim        return;
87226633Sdim
88218887Sdim    if (  (! HAS_originalOptArgArray(pOpts))
89218887Sdim       || (pOpts->originalOptArgCookie == NULL))
90218887Sdim        ao_bug(zno_reset);
91218887Sdim
92226633Sdim    if ((pzArg == NULL) || (*pzArg == NUL)) {
93226633Sdim        fprintf(stderr, zreset_arg, pOpts->pzProgName, pOD->pz_Name);
94218887Sdim        pOpts->pUsageProc(pOpts, EXIT_FAILURE);
95218887Sdim        /* NOTREACHED */
96218887Sdim        assert(0 == 1);
97218887Sdim    }
98218887Sdim
99218887Sdim    reset_active = true;
100218887Sdim
101218887Sdim    if (pzArg[1] == NUL) {
102218887Sdim        if (*pzArg == '*') {
103218887Sdim            optionResetEverything(pOpts);
104218887Sdim            reset_active = false;
105218887Sdim            return;
106218887Sdim        }
107226633Sdim
108226633Sdim        succ = opt_find_short(pOpts, (uint8_t)*pzArg, &opt_state);
109226633Sdim        if (! SUCCESSFUL(succ)) {
110218887Sdim            fprintf(stderr, zIllOptChr, pOpts->pzProgPath, *pzArg);
111218887Sdim            pOpts->pUsageProc(pOpts, EXIT_FAILURE);
112218887Sdim            /* NOTREACHED */
113226633Sdim            assert(0 == 1);
114218887Sdim        }
115218887Sdim    } else {
116218887Sdim        succ = opt_find_long(pOpts, (char *)pzArg, &opt_state);
117226633Sdim        if (! SUCCESSFUL(succ)) {
118226633Sdim            fprintf(stderr, zIllOptStr, pOpts->pzProgPath, pzArg);
119218887Sdim            pOpts->pUsageProc(pOpts, EXIT_FAILURE);
120218887Sdim            /* NOTREACHED */
121218887Sdim            assert(0 == 1);
122218887Sdim        }
123218887Sdim    }
124218887Sdim
125226633Sdim    /*
126218887Sdim     *  We've found the indicated option.  Turn off all non-persistent
127218887Sdim     *  flags because we're forcing the option back to its initialized state.
128218887Sdim     *  Call any callout procedure to handle whatever it needs to.
129218887Sdim     *  Finally, clear the reset flag, too.
130218887Sdim     */
131218887Sdim    optionReset(pOpts, opt_state.pOD);
132218887Sdim    reset_active = false;
133218887Sdim}
134218887Sdim/** @}
135226633Sdim *
136218887Sdim * Local Variables:
137218887Sdim * mode: C
138226633Sdim * c-file-style: "stroustrup"
139218887Sdim * indent-tabs-mode: nil
140226633Sdim * End:
141218887Sdim * end of autoopts/reset.c */
142218887Sdim