xref: /freebsd/contrib/ntp/sntp/libopts/reset.c (revision 15c433351f54e7cd5bec8d36c8e89e6a7fa55b26)
1 
2 /**
3  * \file reset.c
4  *
5  *  Reset the option state to the compiled state.
6  *
7  * @addtogroup autoopts
8  * @{
9  */
10 /*
11  *  This file is part of AutoOpts, a companion to AutoGen.
12  *  AutoOpts is free software.
13  *  AutoOpts is Copyright (C) 1992-2015 by Bruce Korb - all rights reserved
14  *
15  *  AutoOpts is available under any one of two licenses.  The license
16  *  in use must be one of these two and the choice is under the control
17  *  of the user of the license.
18  *
19  *   The GNU Lesser General Public License, version 3 or later
20  *      See the files "COPYING.lgplv3" and "COPYING.gplv3"
21  *
22  *   The Modified Berkeley Software Distribution License
23  *      See the file "COPYING.mbsd"
24  *
25  *  These files have the following sha256 sums:
26  *
27  *  8584710e9b04216a394078dc156b781d0b47e1729104d666658aecef8ee32e95  COPYING.gplv3
28  *  4379e7444a0e2ce2b12dd6f5a52a27a4d02d39d247901d3285c88cf0d37f477b  COPYING.lgplv3
29  *  13aa749a5b0a454917a944ed8fffc530b784f5ead522b1aacaf4ec8aa55a6239  COPYING.mbsd
30  */
31 
32 static void
33 optionReset(tOptions * pOpts, tOptDesc * pOD)
34 {
35     pOD->fOptState &= OPTST_PERSISTENT_MASK;
36     pOD->fOptState |= OPTST_RESET;
37     if (pOD->pOptProc != NULL)
38         pOD->pOptProc(pOpts, pOD);
39     pOD->optArg.argString =
40         pOpts->originalOptArgArray[ pOD->optIndex ].argString;
41     pOD->optCookie = pOpts->originalOptArgCookie[ pOD->optIndex ];
42     pOD->fOptState &= OPTST_PERSISTENT_MASK;
43 }
44 
45 
46 static void
47 optionResetEverything(tOptions * pOpts)
48 {
49     tOptDesc * pOD = pOpts->pOptDesc;
50     int        ct  = pOpts->presetOptCt;
51 
52     for (;;) {
53         optionReset(pOpts, pOD);
54 
55         if (--ct <= 0)
56             break;
57         pOD++;
58     }
59 }
60 
61 
62 /*=export_func  optionResetOpt
63  * private:
64  *
65  * what:  Reset the value of an option
66  * arg:   + tOptions * + pOpts    + program options descriptor  +
67  * arg:   + tOptDesc * + pOptDesc + the descriptor for this arg +
68  *
69  * doc:
70  *  This code will cause another option to be reset to its initial state.
71  *  For example, --reset=foo will cause the --foo option to be reset.
72 =*/
73 void
74 optionResetOpt(tOptions * pOpts, tOptDesc * pOD)
75 {
76     static bool reset_active = false;
77 
78     tOptState opt_state = OPTSTATE_INITIALIZER(DEFINED);
79     char const * pzArg = pOD->optArg.argString;
80     tSuccess     succ;
81 
82     if (pOpts <= OPTPROC_EMIT_LIMIT)
83         return;
84 
85     if (reset_active)
86         return;
87 
88     if (  (! HAS_originalOptArgArray(pOpts))
89        || (pOpts->originalOptArgCookie == NULL))
90         ao_bug(zno_reset);
91 
92     if ((pzArg == NULL) || (*pzArg == NUL)) {
93         fprintf(stderr, zreset_arg, pOpts->pzProgName, pOD->pz_Name);
94         pOpts->pUsageProc(pOpts, EXIT_FAILURE);
95         /* NOTREACHED */
96         assert(0 == 1);
97     }
98 
99     reset_active = true;
100 
101     if (pzArg[1] == NUL) {
102         if (*pzArg == '*') {
103             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