Expand PMF_FN_* macros.
[netbsd-mini2440.git] / dist / ntp / sntp / libopts / save.c
blob305264d078b5ffaaf3f88ca5e429b32eeaaa7c19
1 /* $NetBSD$ */
4 /*
5 * save.c Id: save.c,v 4.18 2007/04/15 19:01:18 bkorb Exp
6 * Time-stamp: "2007-04-15 11:11:10 bkorb"
8 * This module's routines will take the currently set options and
9 * store them into an ".rc" file for re-interpretation the next
10 * time the invoking program is run.
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.
56 tSCC zWarn[] = "%s WARNING: cannot save options - ";
58 /* = = = START-STATIC-FORWARD = = = */
59 /* static forward declarations maintained by :mkfwd */
60 static tCC*
61 findDirName( tOptions* pOpts, int* p_free );
63 static tCC*
64 findFileName( tOptions* pOpts, int* p_free_name );
66 static void
67 printEntry(
68 FILE * fp,
69 tOptDesc * p,
70 tCC* pzLA );
71 /* = = = END-STATIC-FORWARD = = = */
73 static tCC*
74 findDirName( tOptions* pOpts, int* p_free )
76 tCC* pzDir;
78 if (pOpts->specOptIdx.save_opts == 0)
79 return NULL;
81 pzDir = pOpts->pOptDesc[ pOpts->specOptIdx.save_opts ].optArg.argString;
82 if ((pzDir != NULL) && (*pzDir != NUL))
83 return pzDir;
86 * This function only works if there is a directory where
87 * we can stash the RC (INI) file.
90 tCC* const* papz = pOpts->papzHomeList;
91 if (papz == NULL)
92 return NULL;
94 while (papz[1] != NULL) papz++;
95 pzDir = *papz;
99 * IF it does not require deciphering an env value, then just copy it
101 if (*pzDir != '$')
102 return pzDir;
105 tCC* pzEndDir = strchr( ++pzDir, DIRCH );
106 char* pzFileName;
107 char* pzEnv;
109 if (pzEndDir != NULL) {
110 char z[ AO_NAME_SIZE ];
111 if ((pzEndDir - pzDir) > AO_NAME_LIMIT )
112 return NULL;
113 strncpy( z, pzDir, (size_t)(pzEndDir - pzDir) );
114 z[ (pzEndDir - pzDir) ] = NUL;
115 pzEnv = getenv( z );
116 } else {
119 * Make sure we can get the env value (after stripping off
120 * any trailing directory or file names)
122 pzEnv = getenv( pzDir );
125 if (pzEnv == NULL) {
126 fprintf( stderr, zWarn, pOpts->pzProgName );
127 fprintf( stderr, zNotDef, pzDir );
128 return NULL;
131 if (pzEndDir == NULL)
132 return pzEnv;
135 size_t sz = strlen( pzEnv ) + strlen( pzEndDir ) + 2;
136 pzFileName = (char*)AGALOC( sz, "dir name" );
139 if (pzFileName == NULL)
140 return NULL;
142 *p_free = 1;
144 * Glue together the full name into the allocated memory.
145 * FIXME: We lose track of this memory.
147 sprintf( pzFileName, "%s/%s", pzEnv, pzEndDir );
148 return pzFileName;
153 static tCC*
154 findFileName( tOptions* pOpts, int* p_free_name )
156 tCC* pzDir;
157 struct stat stBuf;
158 int free_dir_name = 0;
160 pzDir = findDirName( pOpts, &free_dir_name );
161 if (pzDir == NULL)
162 return NULL;
165 * See if we can find the specified directory. We use a once-only loop
166 * structure so we can bail out early.
168 if (stat( pzDir, &stBuf ) != 0) do {
171 * IF we could not, check to see if we got a full
172 * path to a file name that has not been created yet.
174 if (errno == ENOENT) {
175 char z[AG_PATH_MAX];
178 * Strip off the last component, stat the remaining string and
179 * that string must name a directory
181 char* pzDirCh = strrchr( pzDir, DIRCH );
182 if (pzDirCh == NULL) {
183 stBuf.st_mode = S_IFREG;
184 continue; /* bail out of error condition */
187 strncpy( z, pzDir, (size_t)(pzDirCh - pzDir));
188 z[ pzDirCh - pzDir ] = NUL;
190 if ( (stat( z, &stBuf ) == 0)
191 && S_ISDIR( stBuf.st_mode )) {
194 * We found the directory. Restore the file name and
195 * mark the full name as a regular file
197 stBuf.st_mode = S_IFREG;
198 continue; /* bail out of error condition */
203 * We got a bogus name.
205 fprintf( stderr, zWarn, pOpts->pzProgName );
206 fprintf( stderr, zNoStat, errno, strerror( errno ), pzDir );
207 if (free_dir_name)
208 AGFREE( (void*)pzDir );
209 return NULL;
210 } while (0);
213 * IF what we found was a directory,
214 * THEN tack on the config file name
216 if (S_ISDIR( stBuf.st_mode )) {
217 size_t sz = strlen( pzDir ) + strlen( pOpts->pzRcName ) + 2;
220 char* pzPath = (char*)AGALOC( sz, "file name" );
221 #ifdef HAVE_SNPRINTF
222 snprintf( pzPath, sz, "%s/%s", pzDir, pOpts->pzRcName );
223 #else
224 sprintf( pzPath, "%s/%s", pzDir, pOpts->pzRcName );
225 #endif
226 if (free_dir_name)
227 AGFREE( (void*)pzDir );
228 pzDir = pzPath;
229 free_dir_name = 1;
233 * IF we cannot stat the object for any reason other than
234 * it does not exist, then we bail out
236 if (stat( pzDir, &stBuf ) != 0) {
237 if (errno != ENOENT) {
238 fprintf( stderr, zWarn, pOpts->pzProgName );
239 fprintf( stderr, zNoStat, errno, strerror( errno ),
240 pzDir );
241 AGFREE( (void*)pzDir );
242 return NULL;
246 * It does not exist yet, but it will be a regular file
248 stBuf.st_mode = S_IFREG;
253 * Make sure that whatever we ultimately found, that it either is
254 * or will soon be a file.
256 if (! S_ISREG( stBuf.st_mode )) {
257 fprintf( stderr, zWarn, pOpts->pzProgName );
258 fprintf( stderr, zNotFile, pzDir );
259 if (free_dir_name)
260 AGFREE( (void*)pzDir );
261 return NULL;
265 * Get rid of the old file
267 unlink( pzDir );
268 *p_free_name = free_dir_name;
269 return pzDir;
273 static void
274 printEntry(
275 FILE * fp,
276 tOptDesc * p,
277 tCC* pzLA )
280 * There is an argument. Pad the name so values line up.
281 * Not disabled *OR* this got equivalenced to another opt,
282 * then use current option name.
283 * Otherwise, there must be a disablement name.
286 char const * pz;
287 if (! DISABLED_OPT(p) || (p->optEquivIndex != NO_EQUIVALENT))
288 pz = p->pz_Name;
289 else
290 pz = p->pz_DisableName;
292 fprintf(fp, "%-18s", pz);
295 * IF the option is numeric only,
296 * THEN the char pointer is really the number
298 if (OPTST_GET_ARGTYPE(p->fOptState) == OPARG_TYPE_NUMERIC)
299 fprintf( fp, " %d\n", (int)(t_word)pzLA );
302 * OTHERWISE, FOR each line of the value text, ...
304 else if (pzLA == NULL)
305 fputc( '\n', fp );
307 else {
308 fputc( ' ', fp ); fputc( ' ', fp );
309 for (;;) {
310 tCC* pzNl = strchr( pzLA, '\n' );
313 * IF this is the last line
314 * THEN bail and print it
316 if (pzNl == NULL)
317 break;
320 * Print the continuation and the text from the current line
322 (void)fwrite( pzLA, (size_t)(pzNl - pzLA), (size_t)1, fp );
323 pzLA = pzNl+1; /* advance the Last Arg pointer */
324 fputs( "\\\n", fp );
328 * Terminate the entry
330 fputs( pzLA, fp );
331 fputc( '\n', fp );
336 /*=export_func optionSaveFile
338 * what: saves the option state to a file
340 * arg: tOptions*, pOpts, program options descriptor
342 * doc:
344 * This routine will save the state of option processing to a file. The name
345 * of that file can be specified with the argument to the @code{--save-opts}
346 * option, or by appending the @code{rcfile} attribute to the last
347 * @code{homerc} attribute. If no @code{rcfile} attribute was specified, it
348 * will default to @code{.@i{programname}rc}. If you wish to specify another
349 * file, you should invoke the @code{SET_OPT_SAVE_OPTS( @i{filename} )} macro.
351 * err:
353 * If no @code{homerc} file was specified, this routine will silently return
354 * and do nothing. If the output file cannot be created or updated, a message
355 * will be printed to @code{stderr} and the routine will return.
357 void
358 optionSaveFile( tOptions* pOpts )
360 tOptDesc* pOD;
361 int ct;
362 FILE* fp;
365 int free_name = 0;
366 tCC* pzFName = findFileName( pOpts, &free_name );
367 if (pzFName == NULL)
368 return;
370 fp = fopen( pzFName, "w" FOPEN_BINARY_FLAG );
371 if (fp == NULL) {
372 fprintf( stderr, zWarn, pOpts->pzProgName );
373 fprintf( stderr, zNoCreat, errno, strerror( errno ), pzFName );
374 if (free_name)
375 AGFREE((void*) pzFName );
376 return;
379 if (free_name)
380 AGFREE( (void*)pzFName );
384 char const* pz = pOpts->pzUsageTitle;
385 fputs( "# ", fp );
386 do { fputc( *pz, fp ); } while (*(pz++) != '\n');
390 time_t timeVal = time( NULL );
391 char* pzTime = ctime( &timeVal );
393 fprintf( fp, zPresetFile, pzTime );
394 #ifdef HAVE_ALLOCATED_CTIME
396 * The return values for ctime(), localtime(), and gmtime()
397 * normally point to static data that is overwritten by each call.
398 * The test to detect allocated ctime, so we leak the memory.
400 AGFREE( (void*)pzTime );
401 #endif
405 * FOR each of the defined options, ...
407 ct = pOpts->presetOptCt;
408 pOD = pOpts->pOptDesc;
409 do {
410 int arg_state;
411 tOptDesc* p;
414 * IF the option has not been defined
415 * OR it does not take an initialization value
416 * OR it is equivalenced to another option
417 * THEN continue (ignore it)
419 if (UNUSED_OPT( pOD ))
420 continue;
422 if ((pOD->fOptState & (OPTST_NO_INIT|OPTST_DOCUMENT|OPTST_OMITTED))
423 != 0)
424 continue;
426 if ( (pOD->optEquivIndex != NO_EQUIVALENT)
427 && (pOD->optEquivIndex != pOD->optIndex))
428 continue;
431 * Set a temporary pointer to the real option description
432 * (i.e. account for equivalencing)
434 p = ((pOD->fOptState & OPTST_EQUIVALENCE) != 0)
435 ? (pOpts->pOptDesc + pOD->optActualIndex) : pOD;
438 * IF no arguments are allowed
439 * THEN just print the name and continue
441 if (OPTST_GET_ARGTYPE(pOD->fOptState) == OPARG_TYPE_NONE) {
442 char const * pznm =
443 (DISABLED_OPT( p )) ? p->pz_DisableName : p->pz_Name;
445 * If the option was disabled and the disablement name is NULL,
446 * then the disablement was caused by aliasing.
447 * Use the name as the string to emit.
449 if (pznm == NULL)
450 pznm = p->pz_Name;
452 fprintf(fp, "%s\n", pznm);
453 continue;
456 arg_state = OPTST_GET_ARGTYPE(p->fOptState);
457 switch (arg_state) {
458 case 0:
459 case OPARG_TYPE_NUMERIC:
460 printEntry( fp, p, (void*)(p->optArg.argInt));
461 break;
463 case OPARG_TYPE_STRING:
464 if (p->fOptState & OPTST_STACKED) {
465 tArgList* pAL = (tArgList*)p->optCookie;
466 int uct = pAL->useCt;
467 tCC** ppz = pAL->apzArgs;
470 * Disallow multiple copies of disabled options.
472 if (uct > 1)
473 p->fOptState &= ~OPTST_DISABLED;
475 while (uct-- > 0)
476 printEntry( fp, p, *(ppz++) );
477 } else {
478 printEntry( fp, p, p->optArg.argString );
480 break;
482 case OPARG_TYPE_ENUMERATION:
483 case OPARG_TYPE_MEMBERSHIP:
485 uintptr_t val = p->optArg.argEnum;
487 * This is a magic incantation that will convert the
488 * bit flag values back into a string suitable for printing.
490 (*(p->pOptProc))( (tOptions*)2UL, p );
491 printEntry( fp, p, (void*)(p->optArg.argString));
493 if ( (p->optArg.argString != NULL)
494 && (arg_state != OPARG_TYPE_ENUMERATION)) {
496 * set membership strings get allocated
498 AGFREE( (void*)p->optArg.argString );
499 p->fOptState &= ~OPTST_ALLOC_ARG;
502 p->optArg.argEnum = val;
503 break;
506 case OPARG_TYPE_BOOLEAN:
507 printEntry( fp, p, p->optArg.argBool ? "true" : "false" );
508 break;
510 default:
511 break; /* cannot handle - skip it */
513 } while ( (pOD++), (--ct > 0));
515 fclose( fp );
518 * Local Variables:
519 * mode: C
520 * c-file-style: "stroustrup"
521 * indent-tabs-mode: nil
522 * End:
523 * end of autoopts/save.c */