1 /* $NetBSD: save.c,v 1.4 2007/06/24 19:39:06 kardel Exp $ */
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 */
61 findDirName( tOptions
* pOpts
, int* p_free
);
64 findFileName( tOptions
* pOpts
, int* p_free_name
);
71 /* = = = END-STATIC-FORWARD = = = */
74 findDirName( tOptions
* pOpts
, int* p_free
)
78 if (pOpts
->specOptIdx
.save_opts
== 0)
81 pzDir
= pOpts
->pOptDesc
[ pOpts
->specOptIdx
.save_opts
].optArg
.argString
;
82 if ((pzDir
!= NULL
) && (*pzDir
!= NUL
))
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
;
94 while (papz
[1] != NULL
) papz
++;
99 * IF it does not require deciphering an env value, then just copy it
105 tCC
* pzEndDir
= strchr( ++pzDir
, DIRCH
);
109 if (pzEndDir
!= NULL
) {
110 char z
[ AO_NAME_SIZE
];
111 if ((pzEndDir
- pzDir
) > AO_NAME_LIMIT
)
113 strncpy( z
, pzDir
, (size_t)(pzEndDir
- pzDir
) );
114 z
[ (pzEndDir
- pzDir
) ] = NUL
;
119 * Make sure we can get the env value (after stripping off
120 * any trailing directory or file names)
122 pzEnv
= getenv( pzDir
);
126 fprintf( stderr
, zWarn
, pOpts
->pzProgName
);
127 fprintf( stderr
, zNotDef
, pzDir
);
131 if (pzEndDir
== NULL
)
135 size_t sz
= strlen( pzEnv
) + strlen( pzEndDir
) + 2;
136 pzFileName
= (char*)AGALOC( sz
, "dir name" );
139 if (pzFileName
== NULL
)
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
);
154 findFileName( tOptions
* pOpts
, int* p_free_name
)
158 int free_dir_name
= 0;
160 pzDir
= findDirName( pOpts
, &free_dir_name
);
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
) {
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
);
208 AGFREE( (void*)pzDir
);
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" );
222 snprintf( pzPath
, sz
, "%s/%s", pzDir
, pOpts
->pzRcName
);
224 sprintf( pzPath
, "%s/%s", pzDir
, pOpts
->pzRcName
);
227 AGFREE( (void*)pzDir
);
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
),
241 AGFREE( (void*)pzDir
);
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
);
260 AGFREE( (void*)pzDir
);
265 * Get rid of the old file
268 *p_free_name
= free_dir_name
;
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.
287 if (! DISABLED_OPT(p
) || (p
->optEquivIndex
!= NO_EQUIVALENT
))
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
, " %ld\n", (long)pzLA
);
302 * OTHERWISE, FOR each line of the value text, ...
304 else if (pzLA
== NULL
)
308 fputc( ' ', fp
); fputc( ' ', fp
);
310 tCC
* pzNl
= strchr( pzLA
, '\n' );
313 * IF this is the last line
314 * THEN bail and print it
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 */
328 * Terminate the entry
336 /*=export_func optionSaveFile
338 * what: saves the option state to a file
340 * arg: tOptions*, pOpts, program options descriptor
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.
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.
358 optionSaveFile( tOptions
* pOpts
)
366 tCC
* pzFName
= findFileName( pOpts
, &free_name
);
370 fp
= fopen( pzFName
, "w" FOPEN_BINARY_FLAG
);
372 fprintf( stderr
, zWarn
, pOpts
->pzProgName
);
373 fprintf( stderr
, zNoCreat
, errno
, strerror( errno
), pzFName
);
375 AGFREE((void*) pzFName
);
380 AGFREE( (void*)pzFName
);
384 char const* pz
= pOpts
->pzUsageTitle
;
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
);
405 * FOR each of the defined options, ...
407 ct
= pOpts
->presetOptCt
;
408 pOD
= pOpts
->pOptDesc
;
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
))
422 if ((pOD
->fOptState
& (OPTST_NO_INIT
|OPTST_DOCUMENT
|OPTST_OMITTED
))
426 if ( (pOD
->optEquivIndex
!= NO_EQUIVALENT
)
427 && (pOD
->optEquivIndex
!= pOD
->optIndex
))
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
) {
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.
452 fprintf(fp
, "%s\n", pznm
);
456 arg_state
= OPTST_GET_ARGTYPE(p
->fOptState
);
459 case OPARG_TYPE_NUMERIC
:
460 printEntry( fp
, p
, (void*)(p
->optArg
.argInt
));
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.
473 p
->fOptState
&= ~OPTST_DISABLED
;
476 printEntry( fp
, p
, *(ppz
++) );
478 printEntry( fp
, p
, p
->optArg
.argString
);
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
;
506 case OPARG_TYPE_BOOLEAN
:
507 printEntry( fp
, p
, p
->optArg
.argBool
? "true" : "false" );
511 break; /* cannot handle - skip it */
513 } while ( (pOD
++), (--ct
> 0));
520 * c-file-style: "stroustrup"
521 * indent-tabs-mode: nil
523 * end of autoopts/save.c */