Expand PMF_FN_* macros.
[netbsd-mini2440.git] / dist / ntp / sntp / libopts / restore.c
blobe6e9898be9f842a81f585e84ac5d51222d364b41
1 /* $NetBSD$ */
4 /*
5 * restore.c Id: restore.c,v 4.10 2007/02/04 17:44:12 bkorb Exp
6 * Time-stamp: "2007-01-13 14:13:17 bkorb"
8 * This module's routines will save the current option state to memory
9 * and restore it. If saved prior to the initial optionProcess call,
10 * then the initial state will be restored.
14 * Automated Options copyright 1992-2007 Bruce Korb
16 * Automated Options is free software.
17 * You may redistribute it and/or modify it under the terms of the
18 * GNU General Public License, as published by the Free Software
19 * Foundation; either version 2, or (at your option) any later version.
21 * Automated Options is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with Automated Options. See the file "COPYING". If not,
28 * write to: The Free Software Foundation, Inc.,
29 * 51 Franklin Street, Fifth Floor,
30 * Boston, MA 02110-1301, USA.
32 * As a special exception, Bruce Korb gives permission for additional
33 * uses of the text contained in his release of AutoOpts.
35 * The exception is that, if you link the AutoOpts library with other
36 * files to produce an executable, this does not by itself cause the
37 * resulting executable to be covered by the GNU General Public License.
38 * Your use of that executable is in no way restricted on account of
39 * linking the AutoOpts library code into it.
41 * This exception does not however invalidate any other reasons why
42 * the executable file might be covered by the GNU General Public License.
44 * This exception applies only to the code released by Bruce Korb under
45 * the name AutoOpts. If you copy code from other sources under the
46 * General Public License into a copy of AutoOpts, as the General Public
47 * License permits, the exception does not apply to the code that you add
48 * in this way. To avoid misleading anyone as to the status of such
49 * modified files, you must delete this exception notice from them.
51 * If you write modifications of your own for AutoOpts, it is your choice
52 * whether to permit this exception to apply to your modifications.
53 * If you do not wish that, delete this exception notice.
57 * optionFixupSavedOpts Really, it just wipes out option state for
58 * options that are troublesome to copy. viz., stacked strings and
59 * hierarcicaly valued option args. We do duplicate string args that
60 * have been marked as allocated though.
62 static void
63 fixupSavedOptionArgs(tOptions* pOpts)
65 tOptions* p = pOpts->pSavedState;
66 tOptDesc* pOD = pOpts->pOptDesc;
67 int ct = pOpts->optCt;
70 * Make sure that allocated stuff is only referenced in the
71 * archived copy of the data.
73 for (; ct-- > 0; pOD++) {
74 switch (OPTST_GET_ARGTYPE(pOD->fOptState)) {
75 case OPARG_TYPE_STRING:
76 if (pOD->fOptState & OPTST_STACKED) {
77 tOptDesc* q = p->pOptDesc + (pOD - pOpts->pOptDesc);
78 q->optCookie = NULL;
80 if (pOD->fOptState & OPTST_ALLOC_ARG) {
81 tOptDesc* q = p->pOptDesc + (pOD - pOpts->pOptDesc);
82 AGDUPSTR(q->optArg.argString, pOD->optArg.argString, "arg");
84 break;
86 case OPARG_TYPE_HIERARCHY:
88 tOptDesc* q = p->pOptDesc + (pOD - pOpts->pOptDesc);
89 q->optCookie = NULL;
95 /*=export_func optionSaveState
97 * what: saves the option state to memory
98 * arg: tOptions*, pOpts, program options descriptor
100 * doc:
102 * This routine will allocate enough memory to save the current option
103 * processing state. If this routine has been called before, that memory
104 * will be reused. You may only save one copy of the option state. This
105 * routine may be called before optionProcess(3AO). If you do call it
106 * before the first call to optionProcess, then you may also change the
107 * contents of argc/argv after you call optionRestore(3AO)
109 * In fact, more strongly put: it is safest to only use this function
110 * before having processed any options. In particular, the saving and
111 * restoring of stacked string arguments and hierarchical values is
112 * disabled. The values are not saved.
114 * err: If it fails to allocate the memory,
115 * it will print a message to stderr and exit.
116 * Otherwise, it will always succeed.
118 void
119 optionSaveState(tOptions* pOpts)
121 tOptions* p = (tOptions*)pOpts->pSavedState;
123 if (p == NULL) {
124 size_t sz = sizeof( *pOpts ) + (pOpts->optCt * sizeof( tOptDesc ));
125 p = AGALOC( sz, "saved option state" );
126 if (p == NULL) {
127 tCC* pzName = pOpts->pzProgName;
128 if (pzName == NULL) {
129 pzName = pOpts->pzPROGNAME;
130 if (pzName == NULL)
131 pzName = zNil;
133 fprintf( stderr, zCantSave, pzName, sz );
134 exit( EXIT_FAILURE );
137 pOpts->pSavedState = p;
140 memcpy( p, pOpts, sizeof( *p ));
141 memcpy( p + 1, pOpts->pOptDesc, p->optCt * sizeof( tOptDesc ));
143 fixupSavedOptionArgs(pOpts);
147 /*=export_func optionRestore
149 * what: restore option state from memory copy
150 * arg: tOptions*, pOpts, program options descriptor
152 * doc: Copy back the option state from saved memory.
153 * The allocated memory is left intact, so this routine can be
154 * called repeatedly without having to call optionSaveState again.
155 * If you are restoring a state that was saved before the first call
156 * to optionProcess(3AO), then you may change the contents of the
157 * argc/argv parameters to optionProcess.
159 * err: If you have not called @code{optionSaveState} before, a diagnostic is
160 * printed to @code{stderr} and exit is called.
162 void
163 optionRestore( tOptions* pOpts )
165 tOptions* p = (tOptions*)pOpts->pSavedState;
167 if (p == NULL) {
168 tCC* pzName = pOpts->pzProgName;
169 if (pzName == NULL) {
170 pzName = pOpts->pzPROGNAME;
171 if (pzName == NULL)
172 pzName = zNil;
174 fprintf( stderr, zNoState, pzName );
175 exit( EXIT_FAILURE );
178 pOpts->pSavedState = NULL;
179 optionFree(pOpts);
181 memcpy( pOpts, p, sizeof( *p ));
182 memcpy( pOpts->pOptDesc, p+1, p->optCt * sizeof( tOptDesc ));
183 pOpts->pSavedState = p;
185 fixupSavedOptionArgs(pOpts);
188 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
190 /*=export_func optionFree
192 * what: free allocated option processing memory
193 * arg: tOptions*, pOpts, program options descriptor
195 * doc: AutoOpts sometimes allocates memory and puts pointers to it in the
196 * option state structures. This routine deallocates all such memory.
198 * err: As long as memory has not been corrupted,
199 * this routine is always successful.
201 void
202 optionFree( tOptions* pOpts )
204 free_saved_state:
206 tOptDesc* p = pOpts->pOptDesc;
207 int ct = pOpts->optCt;
208 do {
209 if (p->fOptState & OPTST_ALLOC_ARG) {
210 AGFREE(p->optArg.argString);
211 p->optArg.argString = NULL;
212 p->fOptState &= ~OPTST_ALLOC_ARG;
215 switch (OPTST_GET_ARGTYPE(p->fOptState)) {
216 case OPARG_TYPE_STRING:
217 #ifdef WITH_LIBREGEX
218 if ( (p->fOptState & OPTST_STACKED)
219 && (p->optCookie != NULL)) {
220 p->optArg.argString = ".*";
221 optionUnstackArg(pOpts, p);
223 #else
224 /* leak memory */;
225 #endif
226 break;
228 case OPARG_TYPE_HIERARCHY:
229 if (p->optCookie != NULL)
230 unloadNestedArglist(p->optCookie);
231 break;
234 p->optCookie = NULL;
235 } while (p++, --ct > 0);
237 if (pOpts->pSavedState != NULL) {
238 tOptions * p = (tOptions*)pOpts->pSavedState;
239 memcpy( pOpts, p, sizeof( *p ));
240 memcpy( pOpts->pOptDesc, p+1, p->optCt * sizeof( tOptDesc ));
241 AGFREE( pOpts->pSavedState );
242 pOpts->pSavedState = NULL;
243 goto free_saved_state;
247 * Local Variables:
248 * mode: C
249 * c-file-style: "stroustrup"
250 * indent-tabs-mode: nil
251 * End:
252 * end of autoopts/restore.c */