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