5 * This module's routines will take the currently set options and
6 * store them into an ".rc" file for re-interpretation the next
7 * time the invoking program is run.
13 * This file is part of AutoOpts, a companion to AutoGen.
14 * AutoOpts is free software.
15 * AutoOpts is Copyright (C) 1992-2015 by Bruce Korb - all rights reserved
17 * AutoOpts is available under any one of two licenses. The license
18 * in use must be one of these two and the choice is under the control
19 * of the user of the license.
21 * The GNU Lesser General Public License, version 3 or later
22 * See the files "COPYING.lgplv3" and "COPYING.gplv3"
24 * The Modified Berkeley Software Distribution License
25 * See the file "COPYING.mbsd"
27 * These files have the following sha256 sums:
29 * 8584710e9b04216a394078dc156b781d0b47e1729104d666658aecef8ee32e95 COPYING.gplv3
30 * 4379e7444a0e2ce2b12dd6f5a52a27a4d02d39d247901d3285c88cf0d37f477b COPYING.lgplv3
31 * 13aa749a5b0a454917a944ed8fffc530b784f5ead522b1aacaf4ec8aa55a6239 COPYING.mbsd
34 /* = = = START-STATIC-FORWARD = = = */
36 find_dir_name(tOptions * opts, int * p_free);
39 find_file_name(tOptions * opts, int * p_free_name);
42 prt_entry(FILE * fp, tOptDesc * od, char const * l_arg);
45 prt_value(FILE * fp, int depth, tOptDesc * pOD, tOptionValue const * ovp);
48 prt_string(FILE * fp, char const * name, char const * pz);
51 prt_val_list(FILE * fp, char const * name, tArgList * al);
54 prt_nested(FILE * fp, tOptDesc * p);
57 open_sv_file(tOptions * opts);
60 prt_no_arg_opt(FILE * fp, tOptDesc * p, tOptDesc * pOD);
63 prt_str_arg(FILE * fp, tOptDesc * pOD);
66 prt_enum_arg(FILE * fp, tOptDesc * od);
69 prt_set_arg(FILE * fp, tOptDesc * od);
72 prt_file_arg(FILE * fp, tOptDesc * od, tOptions * opts);
73 /* = = = END-STATIC-FORWARD = = = */
78 find_dir_name(tOptions * opts, int * p_free)
82 if ( (opts->specOptIdx.save_opts == NO_EQUIVALENT)
83 || (opts->specOptIdx.save_opts == 0))
86 pzDir = opts->pOptDesc[ opts->specOptIdx.save_opts ].optArg.argString;
87 if ((pzDir != NULL) && (*pzDir != NUL))
91 * This function only works if there is a directory where
92 * we can stash the RC (INI) file.
95 char const * const * papz = opts->papzHomeList;
99 while (papz[1] != NULL) papz++;
104 * IF it does not require deciphering an env value, then just copy it
110 char const * pzEndDir = strchr(++pzDir, DIRCH);
114 if (pzEndDir != NULL) {
115 char z[ AO_NAME_SIZE ];
116 if ((pzEndDir - pzDir) > AO_NAME_LIMIT )
118 memcpy(z, pzDir, (size_t)(pzEndDir - pzDir));
119 z[pzEndDir - pzDir] = NUL;
124 * Make sure we can get the env value (after stripping off
125 * any trailing directory or file names)
127 pzEnv = getenv(pzDir);
131 fprintf(stderr, zsave_warn, opts->pzProgName);
132 fprintf(stderr, zNotDef, pzDir);
136 if (pzEndDir == NULL)
140 size_t sz = strlen(pzEnv) + strlen(pzEndDir) + 2;
141 pzFileName = (char *)AGALOC(sz, "dir name");
144 if (pzFileName == NULL)
149 * Glue together the full name into the allocated memory.
150 * FIXME: We lose track of this memory.
152 sprintf(pzFileName, "%s/%s", pzEnv, pzEndDir);
160 find_file_name(tOptions * opts, int * p_free_name)
163 int free_dir_name = 0;
165 char const * pzDir = find_dir_name(opts, &free_dir_name);
170 * See if we can find the specified directory. We use a once-only loop
171 * structure so we can bail out early.
173 if (stat(pzDir, &stBuf) != 0) do {
178 * IF we could not, check to see if we got a full
179 * path to a file name that has not been created yet.
181 if (errno != ENOENT) {
183 fprintf(stderr, zsave_warn, opts->pzProgName);
184 fprintf(stderr, zNoStat, errno, strerror(errno), pzDir);
191 * Strip off the last component, stat the remaining string and
192 * that string must name a directory
194 dirchp = strrchr(pzDir, DIRCH);
195 if (dirchp == NULL) {
196 stBuf.st_mode = S_IFREG;
197 break; /* found directory -- viz., "." */
200 if ((size_t)(dirchp - pzDir) >= sizeof(z))
203 memcpy(z, pzDir, (size_t)(dirchp - pzDir));
204 z[dirchp - pzDir] = NUL;
206 if ((stat(z, &stBuf) != 0) || ! S_ISDIR(stBuf.st_mode))
208 stBuf.st_mode = S_IFREG; /* file within this directory */
212 * IF what we found was a directory,
213 * THEN tack on the config file name
215 if (S_ISDIR(stBuf.st_mode)) {
216 size_t sz = strlen(pzDir) + strlen(opts->pzRcName) + 2;
219 char * pzPath = (char *)AGALOC(sz, "file name");
221 snprintf(pzPath, sz, "%s/%s", pzDir, opts->pzRcName);
223 sprintf(pzPath, "%s/%s", pzDir, opts->pzRcName);
232 * IF we cannot stat the object for any reason other than
233 * it does not exist, then we bail out
235 if (stat(pzDir, &stBuf) != 0) {
236 if (errno != ENOENT) {
237 fprintf(stderr, zsave_warn, opts->pzProgName);
238 fprintf(stderr, zNoStat, errno, strerror(errno),
245 * It does not exist yet, but it will be a regular file
247 stBuf.st_mode = S_IFREG;
252 * Make sure that whatever we ultimately found, that it either is
253 * or will soon be a file.
255 if (! S_ISREG(stBuf.st_mode)) {
256 fprintf(stderr, zsave_warn, opts->pzProgName, pzDir);
263 * Get rid of the old file
266 *p_free_name = free_dir_name;
271 * print one option entry to the save file.
273 * @param[in] fp the file pointer for the save file
274 * @param[in] od the option descriptor to print
275 * @param[in] l_arg the last argument for the option
278 prt_entry(FILE * fp, tOptDesc * od, char const * l_arg)
283 * There is an argument. Pad the name so values line up.
284 * Not disabled *OR* this got equivalenced to another opt,
285 * then use current option name.
286 * Otherwise, there must be a disablement name.
290 (! DISABLED_OPT(od) || (od->optEquivIndex != NO_EQUIVALENT))
292 : od->pz_DisableName;
293 space_ct = 17 - strlen(pz);
298 && (OPTST_GET_ARGTYPE(od->fOptState) != OPARG_TYPE_NUMERIC))
302 while (space_ct-- > 0) fputc(' ', fp);
305 * IF the option is numeric only,
306 * THEN the char pointer is really the number
308 if (OPTST_GET_ARGTYPE(od->fOptState) == OPARG_TYPE_NUMERIC)
309 fprintf(fp, "%d", (int)(intptr_t)l_arg);
313 char const * eol = strchr(l_arg, NL);
316 * IF this is the last line
317 * THEN bail and print it
323 * Print the continuation and the text from the current line
325 (void)fwrite(l_arg, (size_t)(eol - l_arg), (size_t)1, fp);
326 l_arg = eol+1; /* advance the Last Arg pointer */
331 * Terminate the entry
343 prt_value(FILE * fp, int depth, tOptDesc * pOD, tOptionValue const * ovp)
346 putc(' ', fp), putc(' ', fp);
348 switch (ovp->valType) {
350 case OPARG_TYPE_NONE:
351 fprintf(fp, NULL_ATR_FMT, ovp->pzName);
354 case OPARG_TYPE_STRING:
355 prt_string(fp, ovp->pzName, ovp->v.strVal);
358 case OPARG_TYPE_ENUMERATION:
359 case OPARG_TYPE_MEMBERSHIP:
361 uint32_t opt_state = pOD->fOptState;
362 uintptr_t val = pOD->optArg.argEnum;
363 char const * typ = (ovp->valType == OPARG_TYPE_ENUMERATION)
364 ? "keyword" : "set-membership";
366 fprintf(fp, TYPE_ATR_FMT, ovp->pzName, typ);
369 * This is a magic incantation that will convert the
370 * bit flag values back into a string suitable for printing.
372 (*(pOD->pOptProc))(OPTPROC_RETURN_VALNAME, pOD );
373 if (pOD->optArg.argString != NULL) {
374 fputs(pOD->optArg.argString, fp);
376 if (ovp->valType != OPARG_TYPE_ENUMERATION) {
378 * set membership strings get allocated
380 AGFREE(pOD->optArg.argString);
384 pOD->optArg.argEnum = val;
385 pOD->fOptState = opt_state;
386 fprintf(fp, END_XML_FMT, ovp->pzName);
391 case OPARG_TYPE_NUMERIC:
392 fprintf(fp, NUMB_ATR_FMT, ovp->pzName, ovp->v.longVal);
395 case OPARG_TYPE_BOOLEAN:
396 fprintf(fp, BOOL_ATR_FMT, ovp->pzName,
397 ovp->v.boolVal ? "true" : "false");
400 case OPARG_TYPE_HIERARCHY:
401 prt_val_list(fp, ovp->pzName, ovp->v.nestVal);
409 prt_string(FILE * fp, char const * name, char const * pz)
411 fprintf(fp, OPEN_XML_FMT, name);
413 int ch = ((int)*(pz++)) & 0xFF;
416 case NUL: goto string_done;
422 case 1 ... (' ' - 1):
423 case ('~' + 1) ... 0xFF:
425 emit_special_char(fp, ch);
430 if ( ((ch >= 1) && (ch <= (' ' - 1)))
431 || ((ch >= ('~' + 1)) && (ch <= 0xFF)) ) {
432 emit_special_char(fp, ch);
439 fprintf(fp, END_XML_FMT, name);
445 prt_val_list(FILE * fp, char const * name, tArgList * al)
447 static int depth = 1;
456 opt_list = VOIDP(al->apzArgs);
459 fprintf(fp, OPEN_CLOSE_FMT, name);
463 fprintf(fp, NESTED_OPT_FMT, name);
466 while (--opt_ct >= 0) {
467 tOptionValue const * ovp = *(opt_list++);
469 prt_value(fp, depth, NULL, ovp);
473 for (sp_ct = depth; --sp_ct >= 0;)
474 putc(' ', fp), putc(' ', fp);
475 fprintf(fp, "</%s>\n", name);
481 prt_nested(FILE * fp, tOptDesc * p)
484 tArgList * al = p->optCookie;
491 opt_list = VOIDP(al->apzArgs);
497 tOptionValue const * base = *(opt_list++);
498 tOptionValue const * ovp = optionGetValue(base, NULL);
503 fprintf(fp, NESTED_OPT_FMT, p->pz_Name);
506 prt_value(fp, 1, p, ovp);
508 } while (ovp = optionNextValue(base, ovp),
511 fprintf(fp, "</%s>\n", p->pz_Name);
512 } while (--opt_ct > 0);
516 * open the file for saving option state.
518 * @param[in] opts the program options structure
519 * @returns the open file pointer. It may be NULL.
522 open_sv_file(tOptions * opts)
528 char const * pzFName = find_file_name(opts, &free_name);
532 fp = fopen(pzFName, "w" FOPEN_BINARY_FLAG);
534 fprintf(stderr, zsave_warn, opts->pzProgName);
535 fprintf(stderr, zNoCreat, errno, strerror(errno), pzFName);
547 char const * e = strchr(opts->pzUsageTitle, NL);
549 fwrite(opts->pzUsageTitle, 1, e - opts->pzUsageTitle, fp);
553 time_t cur_time = time(NULL);
554 char * time_str = ctime(&cur_time);
556 fprintf(fp, zPresetFile, time_str);
557 #ifdef HAVE_ALLOCATED_CTIME
559 * The return values for ctime(), localtime(), and gmtime()
560 * normally point to static data that is overwritten by each call.
561 * The test to detect allocated ctime, so we leak the memory.
573 prt_no_arg_opt(FILE * fp, tOptDesc * p, tOptDesc * pOD)
576 * The aliased to argument indicates whether or not the option
577 * is "disabled". However, the original option has the name
578 * string, so we get that there, not with "p".
581 (DISABLED_OPT(p)) ? pOD->pz_DisableName : pOD->pz_Name;
583 * If the option was disabled and the disablement name is NULL,
584 * then the disablement was caused by aliasing.
585 * Use the name as the string to emit.
590 fprintf(fp, "%s\n", pznm);
596 prt_str_arg(FILE * fp, tOptDesc * pOD)
598 if (pOD->fOptState & OPTST_STACKED) {
599 tArgList * pAL = (tArgList *)pOD->optCookie;
600 int uct = pAL->useCt;
601 char const ** ppz = pAL->apzArgs;
604 * un-disable multiple copies of disabled options.
607 pOD->fOptState &= ~OPTST_DISABLED;
610 prt_entry(fp, pOD, *(ppz++));
612 prt_entry(fp, pOD, pOD->optArg.argString);
617 * print the string value of an enumeration.
619 * @param[in] fp the file pointer to write to
620 * @param[in] od the option descriptor with the enumerated value
623 prt_enum_arg(FILE * fp, tOptDesc * od)
625 uintptr_t val = od->optArg.argEnum;
628 * This is a magic incantation that will convert the
629 * bit flag values back into a string suitable for printing.
631 (*(od->pOptProc))(OPTPROC_RETURN_VALNAME, od);
632 prt_entry(fp, od, VOIDP(od->optArg.argString));
634 od->optArg.argEnum = val;
638 * Print the bits set in a bit mask option.
639 * We call the option handling function with a magic value for
640 * the options pointer and it allocates and fills in the string.
641 * We print that with a call to prt_entry().
643 * @param[in] fp the file pointer to write to
644 * @param[in] od the option descriptor with a bit mask value type
647 prt_set_arg(FILE * fp, tOptDesc * od)
649 char * list = optionMemberList(od);
650 size_t len = strlen(list);
651 char * buf = (char *)AGALOC(len + 3, "dir name");
653 memcpy(buf+1, list, len + 1);
654 prt_entry(fp, od, buf);
660 * figure out what the option file name argument is.
661 * If one can be found, call prt_entry() to emit it.
663 * @param[in] fp the file pointer to write to.
664 * @param[in] od the option descriptor with a bit mask value type
665 * @param[in] opts the program options descriptor
668 prt_file_arg(FILE * fp, tOptDesc * od, tOptions * opts)
671 * If the cookie is not NULL, then it has the file name, period.
672 * Otherwise, if we have a non-NULL string argument, then....
674 if (od->optCookie != NULL)
675 prt_entry(fp, od, od->optCookie);
677 else if (HAS_originalOptArgArray(opts)) {
679 opts->originalOptArgArray[od->optIndex].argString;
681 if (od->optArg.argString == orig)
684 prt_entry(fp, od, od->optArg.argString);
688 /*=export_func optionSaveFile
690 * what: saves the option state to a file
692 * arg: tOptions *, opts, program options descriptor
696 * This routine will save the state of option processing to a file. The name
697 * of that file can be specified with the argument to the @code{--save-opts}
698 * option, or by appending the @code{rcfile} attribute to the last
699 * @code{homerc} attribute. If no @code{rcfile} attribute was specified, it
700 * will default to @code{.@i{programname}rc}. If you wish to specify another
701 * file, you should invoke the @code{SET_OPT_SAVE_OPTS(@i{filename})} macro.
703 * The recommend usage is as follows:
705 * optionProcess(&progOptions, argc, argv);
706 * if (i_want_a_non_standard_place_for_this)
707 * SET_OPT_SAVE_OPTS("myfilename");
708 * optionSaveFile(&progOptions);
713 * If no @code{homerc} file was specified, this routine will silently return
714 * and do nothing. If the output file cannot be created or updated, a message
715 * will be printed to @code{stderr} and the routine will return.
718 optionSaveFile(tOptions * opts)
722 FILE * fp = open_sv_file(opts);
728 * FOR each of the defined options, ...
730 ct = opts->presetOptCt;
736 * IF the option has not been defined
737 * OR it does not take an initialization value
738 * OR it is equivalenced to another option
739 * THEN continue (ignore it)
741 * Equivalenced options get picked up when the equivalenced-to
742 * option is processed.
747 if ((od->fOptState & OPTST_DO_NOT_SAVE_MASK) != 0)
750 if ( (od->optEquivIndex != NO_EQUIVALENT)
751 && (od->optEquivIndex != od->optIndex))
755 * The option argument data are found at the equivalenced-to option,
756 * but the actual option argument type comes from the original
757 * option descriptor. Be careful!
759 p = ((od->fOptState & OPTST_EQUIVALENCE) != 0)
760 ? (opts->pOptDesc + od->optActualIndex) : od;
762 switch (OPTST_GET_ARGTYPE(od->fOptState)) {
763 case OPARG_TYPE_NONE:
764 prt_no_arg_opt(fp, p, od);
767 case OPARG_TYPE_NUMERIC:
768 prt_entry(fp, p, VOIDP(p->optArg.argInt));
771 case OPARG_TYPE_STRING:
775 case OPARG_TYPE_ENUMERATION:
779 case OPARG_TYPE_MEMBERSHIP:
783 case OPARG_TYPE_BOOLEAN:
784 prt_entry(fp, p, p->optArg.argBool ? "true" : "false");
787 case OPARG_TYPE_HIERARCHY:
791 case OPARG_TYPE_FILE:
792 prt_file_arg(fp, p, opts);
796 break; /* cannot handle - skip it */
798 } while (od++, (--ct > 0));
806 * c-file-style: "stroustrup"
807 * indent-tabs-mode: nil
809 * end of autoopts/save.c */