/src/ntp-dev/sntp/libopts/reset.c
Line | Count | Source (jump to first uncovered line) |
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 | 0 | { |
35 | 0 | pOD->fOptState &= OPTST_PERSISTENT_MASK; |
36 | 0 | pOD->fOptState |= OPTST_RESET; |
37 | 0 | if (pOD->pOptProc != NULL) |
38 | 0 | pOD->pOptProc(pOpts, pOD); |
39 | 0 | pOD->optArg.argString = |
40 | 0 | pOpts->originalOptArgArray[ pOD->optIndex ].argString; |
41 | 0 | pOD->optCookie = pOpts->originalOptArgCookie[ pOD->optIndex ]; |
42 | 0 | pOD->fOptState &= OPTST_PERSISTENT_MASK; |
43 | 0 | } |
44 | | |
45 | | |
46 | | static void |
47 | | optionResetEverything(tOptions * pOpts) |
48 | 0 | { |
49 | 0 | tOptDesc * pOD = pOpts->pOptDesc; |
50 | 0 | int ct = pOpts->presetOptCt; |
51 | |
|
52 | 0 | for (;;) { |
53 | 0 | optionReset(pOpts, pOD); |
54 | |
|
55 | 0 | if (--ct <= 0) |
56 | 0 | break; |
57 | 0 | pOD++; |
58 | 0 | } |
59 | 0 | } |
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 | 0 | { |
76 | 0 | static bool reset_active = false; |
77 | |
|
78 | 0 | tOptState opt_state = OPTSTATE_INITIALIZER(DEFINED); |
79 | 0 | char const * pzArg = pOD->optArg.argString; |
80 | 0 | tSuccess succ; |
81 | |
|
82 | 0 | if (pOpts <= OPTPROC_EMIT_LIMIT) |
83 | 0 | return; |
84 | | |
85 | 0 | if (reset_active) |
86 | 0 | return; |
87 | | |
88 | 0 | if ( (! HAS_originalOptArgArray(pOpts)) |
89 | 0 | || (pOpts->originalOptArgCookie == NULL)) |
90 | 0 | ao_bug(zno_reset); |
91 | |
|
92 | 0 | if ((pzArg == NULL) || (*pzArg == NUL)) { |
93 | 0 | fprintf(stderr, zreset_arg, pOpts->pzProgName, pOD->pz_Name); |
94 | 0 | pOpts->pUsageProc(pOpts, EXIT_FAILURE); |
95 | | /* NOTREACHED */ |
96 | 0 | assert(0 == 1); |
97 | 0 | } |
98 | | |
99 | 0 | reset_active = true; |
100 | |
|
101 | 0 | if (pzArg[1] == NUL) { |
102 | 0 | if (*pzArg == '*') { |
103 | 0 | optionResetEverything(pOpts); |
104 | 0 | reset_active = false; |
105 | 0 | return; |
106 | 0 | } |
107 | | |
108 | 0 | succ = opt_find_short(pOpts, (uint8_t)*pzArg, &opt_state); |
109 | 0 | if (! SUCCESSFUL(succ)) { |
110 | 0 | fprintf(stderr, zIllOptChr, pOpts->pzProgPath, *pzArg); |
111 | 0 | pOpts->pUsageProc(pOpts, EXIT_FAILURE); |
112 | | /* NOTREACHED */ |
113 | 0 | assert(0 == 1); |
114 | 0 | } |
115 | 0 | } else { |
116 | 0 | succ = opt_find_long(pOpts, (char *)pzArg, &opt_state); |
117 | 0 | if (! SUCCESSFUL(succ)) { |
118 | 0 | fprintf(stderr, zIllOptStr, pOpts->pzProgPath, pzArg); |
119 | 0 | pOpts->pUsageProc(pOpts, EXIT_FAILURE); |
120 | | /* NOTREACHED */ |
121 | 0 | assert(0 == 1); |
122 | 0 | } |
123 | 0 | } |
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 | 0 | optionReset(pOpts, opt_state.pOD); |
132 | 0 | reset_active = false; |
133 | 0 | } |
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 */ |