3 * save.c $Id: save.c,v 4.18 2007/04/15 19:01:18 bkorb Exp $
4 * Time-stamp: "2007-04-15 11:11:10 bkorb"
6 * This module's routines will take the currently set options and
7 * store them into an ".rc" file for re-interpretation the next
8 * time the invoking program is run.
12 * Automated Options copyright 1992-2007 Bruce Korb
14 * Automated Options is free software.
15 * You may redistribute it and/or modify it under the terms of the
16 * GNU General Public License, as published by the Free Software
17 * Foundation; either version 2, or (at your option) any later version.
19 * Automated Options is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with Automated Options. See the file "COPYING". If not,
26 * write to: The Free Software Foundation, Inc.,
27 * 51 Franklin Street, Fifth Floor,
28 * Boston, MA 02110-1301, USA.
30 * As a special exception, Bruce Korb gives permission for additional
31 * uses of the text contained in his release of AutoOpts.
33 * The exception is that, if you link the AutoOpts library with other
34 * files to produce an executable, this does not by itself cause the
35 * resulting executable to be covered by the GNU General Public License.
36 * Your use of that executable is in no way restricted on account of
37 * linking the AutoOpts library code into it.
39 * This exception does not however invalidate any other reasons why
40 * the executable file might be covered by the GNU General Public License.
42 * This exception applies only to the code released by Bruce Korb under
43 * the name AutoOpts. If you copy code from other sources under the
44 * General Public License into a copy of AutoOpts, as the General Public
45 * License permits, the exception does not apply to the code that you add
46 * in this way. To avoid misleading anyone as to the status of such
47 * modified files, you must delete this exception notice from them.
49 * If you write modifications of your own for AutoOpts, it is your choice
50 * whether to permit this exception to apply to your modifications.
51 * If you do not wish that, delete this exception notice.
54 tSCC zWarn
[] = "%s WARNING: cannot save options - ";
56 /* = = = START-STATIC-FORWARD = = = */
57 /* static forward declarations maintained by :mkfwd */
59 findDirName( tOptions
* pOpts
, int* p_free
);
62 findFileName( tOptions
* pOpts
, int* p_free_name
);
69 /* = = = END-STATIC-FORWARD = = = */
72 findDirName( tOptions
* pOpts
, int* p_free
)
76 if (pOpts
->specOptIdx
.save_opts
== 0)
79 pzDir
= pOpts
->pOptDesc
[ pOpts
->specOptIdx
.save_opts
].optArg
.argString
;
80 if ((pzDir
!= NULL
) && (*pzDir
!= NUL
))
84 * This function only works if there is a directory where
85 * we can stash the RC (INI) file.
88 tCC
* const* papz
= pOpts
->papzHomeList
;
92 while (papz
[1] != NULL
) papz
++;
97 * IF it does not require deciphering an env value, then just copy it
103 tCC
* pzEndDir
= strchr( ++pzDir
, DIRCH
);
107 if (pzEndDir
!= NULL
) {
108 char z
[ AO_NAME_SIZE
];
109 if ((pzEndDir
- pzDir
) > AO_NAME_LIMIT
)
111 strncpy( z
, pzDir
, (size_t)(pzEndDir
- pzDir
) );
112 z
[ (pzEndDir
- pzDir
) ] = NUL
;
117 * Make sure we can get the env value (after stripping off
118 * any trailing directory or file names)
120 pzEnv
= getenv( pzDir
);
124 fprintf( stderr
, zWarn
, pOpts
->pzProgName
);
125 fprintf( stderr
, zNotDef
, pzDir
);
129 if (pzEndDir
== NULL
)
133 size_t sz
= strlen( pzEnv
) + strlen( pzEndDir
) + 2;
134 pzFileName
= (char*)AGALOC( sz
, "dir name" );
137 if (pzFileName
== NULL
)
142 * Glue together the full name into the allocated memory.
143 * FIXME: We lose track of this memory.
145 sprintf( pzFileName
, "%s/%s", pzEnv
, pzEndDir
);
152 findFileName( tOptions
* pOpts
, int* p_free_name
)
156 int free_dir_name
= 0;
158 pzDir
= findDirName( pOpts
, &free_dir_name
);
163 * See if we can find the specified directory. We use a once-only loop
164 * structure so we can bail out early.
166 if (stat( pzDir
, &stBuf
) != 0) do {
169 * IF we could not, check to see if we got a full
170 * path to a file name that has not been created yet.
172 if (errno
== ENOENT
) {
176 * Strip off the last component, stat the remaining string and
177 * that string must name a directory
179 char* pzDirCh
= strrchr( pzDir
, DIRCH
);
180 if (pzDirCh
== NULL
) {
181 stBuf
.st_mode
= S_IFREG
;
182 continue; /* bail out of error condition */
185 strncpy( z
, pzDir
, (size_t)(pzDirCh
- pzDir
));
186 z
[ pzDirCh
- pzDir
] = NUL
;
188 if ( (stat( z
, &stBuf
) == 0)
189 && S_ISDIR( stBuf
.st_mode
)) {
192 * We found the directory. Restore the file name and
193 * mark the full name as a regular file
195 stBuf
.st_mode
= S_IFREG
;
196 continue; /* bail out of error condition */
201 * We got a bogus name.
203 fprintf( stderr
, zWarn
, pOpts
->pzProgName
);
204 fprintf( stderr
, zNoStat
, errno
, strerror( errno
), pzDir
);
206 AGFREE( (void*)pzDir
);
211 * IF what we found was a directory,
212 * THEN tack on the config file name
214 if (S_ISDIR( stBuf
.st_mode
)) {
215 size_t sz
= strlen( pzDir
) + strlen( pOpts
->pzRcName
) + 2;
218 char* pzPath
= (char*)AGALOC( sz
, "file name" );
220 snprintf( pzPath
, sz
, "%s/%s", pzDir
, pOpts
->pzRcName
);
222 sprintf( pzPath
, "%s/%s", pzDir
, pOpts
->pzRcName
);
225 AGFREE( (void*)pzDir
);
231 * IF we cannot stat the object for any reason other than
232 * it does not exist, then we bail out
234 if (stat( pzDir
, &stBuf
) != 0) {
235 if (errno
!= ENOENT
) {
236 fprintf( stderr
, zWarn
, pOpts
->pzProgName
);
237 fprintf( stderr
, zNoStat
, errno
, strerror( errno
),
239 AGFREE( (void*)pzDir
);
244 * It does not exist yet, but it will be a regular file
246 stBuf
.st_mode
= S_IFREG
;
251 * Make sure that whatever we ultimately found, that it either is
252 * or will soon be a file.
254 if (! S_ISREG( stBuf
.st_mode
)) {
255 fprintf( stderr
, zWarn
, pOpts
->pzProgName
);
256 fprintf( stderr
, zNotFile
, pzDir
);
258 AGFREE( (void*)pzDir
);
263 * Get rid of the old file
266 *p_free_name
= free_dir_name
;
278 * There is an argument. Pad the name so values line up.
279 * Not disabled *OR* this got equivalenced to another opt,
280 * then use current option name.
281 * Otherwise, there must be a disablement name.
285 if (! DISABLED_OPT(p
) || (p
->optEquivIndex
!= NO_EQUIVALENT
))
288 pz
= p
->pz_DisableName
;
290 fprintf(fp
, "%-18s", pz
);
293 * IF the option is numeric only,
294 * THEN the char pointer is really the number
296 if (OPTST_GET_ARGTYPE(p
->fOptState
) == OPARG_TYPE_NUMERIC
)
297 fprintf( fp
, " %d\n", (int)(t_word
)pzLA
);
300 * OTHERWISE, FOR each line of the value text, ...
302 else if (pzLA
== NULL
)
306 fputc( ' ', fp
); fputc( ' ', fp
);
308 tCC
* pzNl
= strchr( pzLA
, '\n' );
311 * IF this is the last line
312 * THEN bail and print it
318 * Print the continuation and the text from the current line
320 (void)fwrite( pzLA
, (size_t)(pzNl
- pzLA
), (size_t)1, fp
);
321 pzLA
= pzNl
+1; /* advance the Last Arg pointer */
326 * Terminate the entry
334 /*=export_func optionSaveFile
336 * what: saves the option state to a file
338 * arg: tOptions*, pOpts, program options descriptor
342 * This routine will save the state of option processing to a file. The name
343 * of that file can be specified with the argument to the @code{--save-opts}
344 * option, or by appending the @code{rcfile} attribute to the last
345 * @code{homerc} attribute. If no @code{rcfile} attribute was specified, it
346 * will default to @code{.@i{programname}rc}. If you wish to specify another
347 * file, you should invoke the @code{SET_OPT_SAVE_OPTS( @i{filename} )} macro.
351 * If no @code{homerc} file was specified, this routine will silently return
352 * and do nothing. If the output file cannot be created or updated, a message
353 * will be printed to @code{stderr} and the routine will return.
356 optionSaveFile( tOptions
* pOpts
)
364 tCC
* pzFName
= findFileName( pOpts
, &free_name
);
368 fp
= fopen( pzFName
, "w" FOPEN_BINARY_FLAG
);
370 fprintf( stderr
, zWarn
, pOpts
->pzProgName
);
371 fprintf( stderr
, zNoCreat
, errno
, strerror( errno
), pzFName
);
373 AGFREE((void*) pzFName
);
378 AGFREE( (void*)pzFName
);
382 char const* pz
= pOpts
->pzUsageTitle
;
384 do { fputc( *pz
, fp
); } while (*(pz
++) != '\n');
388 time_t timeVal
= time( NULL
);
389 char* pzTime
= ctime( &timeVal
);
391 fprintf( fp
, zPresetFile
, pzTime
);
392 #ifdef HAVE_ALLOCATED_CTIME
394 * The return values for ctime(), localtime(), and gmtime()
395 * normally point to static data that is overwritten by each call.
396 * The test to detect allocated ctime, so we leak the memory.
398 AGFREE( (void*)pzTime
);
403 * FOR each of the defined options, ...
405 ct
= pOpts
->presetOptCt
;
406 pOD
= pOpts
->pOptDesc
;
412 * IF the option has not been defined
413 * OR it does not take an initialization value
414 * OR it is equivalenced to another option
415 * THEN continue (ignore it)
417 if (UNUSED_OPT( pOD
))
420 if ((pOD
->fOptState
& (OPTST_NO_INIT
|OPTST_DOCUMENT
|OPTST_OMITTED
))
424 if ( (pOD
->optEquivIndex
!= NO_EQUIVALENT
)
425 && (pOD
->optEquivIndex
!= pOD
->optIndex
))
429 * Set a temporary pointer to the real option description
430 * (i.e. account for equivalencing)
432 p
= ((pOD
->fOptState
& OPTST_EQUIVALENCE
) != 0)
433 ? (pOpts
->pOptDesc
+ pOD
->optActualIndex
) : pOD
;
436 * IF no arguments are allowed
437 * THEN just print the name and continue
439 if (OPTST_GET_ARGTYPE(pOD
->fOptState
) == OPARG_TYPE_NONE
) {
441 (DISABLED_OPT( p
)) ? p
->pz_DisableName
: p
->pz_Name
;
443 * If the option was disabled and the disablement name is NULL,
444 * then the disablement was caused by aliasing.
445 * Use the name as the string to emit.
450 fprintf(fp
, "%s\n", pznm
);
454 arg_state
= OPTST_GET_ARGTYPE(p
->fOptState
);
457 case OPARG_TYPE_NUMERIC
:
458 printEntry( fp
, p
, (void*)(p
->optArg
.argInt
));
461 case OPARG_TYPE_STRING
:
462 if (p
->fOptState
& OPTST_STACKED
) {
463 tArgList
* pAL
= (tArgList
*)p
->optCookie
;
464 int uct
= pAL
->useCt
;
465 tCC
** ppz
= pAL
->apzArgs
;
468 * Disallow multiple copies of disabled options.
471 p
->fOptState
&= ~OPTST_DISABLED
;
474 printEntry( fp
, p
, *(ppz
++) );
476 printEntry( fp
, p
, p
->optArg
.argString
);
480 case OPARG_TYPE_ENUMERATION
:
481 case OPARG_TYPE_MEMBERSHIP
:
483 uintptr_t val
= p
->optArg
.argEnum
;
485 * This is a magic incantation that will convert the
486 * bit flag values back into a string suitable for printing.
488 (*(p
->pOptProc
))( (tOptions
*)2UL, p
);
489 printEntry( fp
, p
, (void*)(p
->optArg
.argString
));
491 if ( (p
->optArg
.argString
!= NULL
)
492 && (arg_state
!= OPARG_TYPE_ENUMERATION
)) {
494 * set membership strings get allocated
496 AGFREE( (void*)p
->optArg
.argString
);
497 p
->fOptState
&= ~OPTST_ALLOC_ARG
;
500 p
->optArg
.argEnum
= val
;
504 case OPARG_TYPE_BOOLEAN
:
505 printEntry( fp
, p
, p
->optArg
.argBool
? "true" : "false" );
509 break; /* cannot handle - skip it */
511 } while ( (pOD
++), (--ct
> 0));
518 * c-file-style: "stroustrup"
519 * indent-tabs-mode: nil
521 * end of autoopts/save.c */