save.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916
  1. /*
  2. * \file save.c
  3. *
  4. * This module's routines will take the currently set options and
  5. * store them into an ".rc" file for re-interpretation the next
  6. * time the invoking program is run.
  7. *
  8. * @addtogroup autoopts
  9. * @{
  10. */
  11. /*
  12. * This file is part of AutoOpts, a companion to AutoGen.
  13. * AutoOpts is free software.
  14. * AutoOpts is Copyright (C) 1992-2018 by Bruce Korb - all rights reserved
  15. *
  16. * AutoOpts is available under any one of two licenses. The license
  17. * in use must be one of these two and the choice is under the control
  18. * of the user of the license.
  19. *
  20. * The GNU Lesser General Public License, version 3 or later
  21. * See the files "COPYING.lgplv3" and "COPYING.gplv3"
  22. *
  23. * The Modified Berkeley Software Distribution License
  24. * See the file "COPYING.mbsd"
  25. *
  26. * These files have the following sha256 sums:
  27. *
  28. * 8584710e9b04216a394078dc156b781d0b47e1729104d666658aecef8ee32e95 COPYING.gplv3
  29. * 4379e7444a0e2ce2b12dd6f5a52a27a4d02d39d247901d3285c88cf0d37f477b COPYING.lgplv3
  30. * 13aa749a5b0a454917a944ed8fffc530b784f5ead522b1aacaf4ec8aa55a6239 COPYING.mbsd
  31. */
  32. #include "save-flags.h"
  33. /**
  34. * find the config file directory name
  35. *
  36. * @param opts the options descriptor
  37. * @param p_free tell caller if name was allocated or not
  38. */
  39. static char const *
  40. find_dir_name(tOptions * opts, int * p_free)
  41. {
  42. char const * dir;
  43. if ( (opts->specOptIdx.save_opts == NO_EQUIVALENT)
  44. || (opts->specOptIdx.save_opts == 0))
  45. return NULL;
  46. dir = opts->pOptDesc[ opts->specOptIdx.save_opts ].optArg.argString;
  47. if ((dir != NULL) && (*dir != NUL)) {
  48. char const * pz = strchr(dir, '>');
  49. if (pz == NULL)
  50. return dir;
  51. while (*(++pz) == '>') ;
  52. pz += strspn(pz, " \t");
  53. dir = pz;
  54. if (*dir != NUL)
  55. return dir;
  56. }
  57. if (opts->papzHomeList == NULL)
  58. return NULL;
  59. /*
  60. * This function only works if there is a directory where
  61. * we can stash the RC (INI) file.
  62. */
  63. for (int idx = 0;; idx++) {
  64. char f_name[ AG_PATH_MAX+1 ];
  65. dir = opts->papzHomeList[idx];
  66. switch (*dir) {
  67. case '$':
  68. break;
  69. case NUL:
  70. continue;
  71. default:
  72. return dir;
  73. }
  74. if (optionMakePath(f_name, (int)sizeof(f_name), dir, opts->pzProgPath)) {
  75. *p_free = true;
  76. AGDUPSTR(dir, f_name, "homerc");
  77. return dir;
  78. }
  79. }
  80. return NULL;
  81. }
  82. /**
  83. * Find the name of the save-the-options file
  84. *
  85. * @param opts the options descriptor
  86. * @param p_free_name tell caller if name was allocated or not
  87. */
  88. static char const *
  89. find_file_name(tOptions * opts, int * p_free_name)
  90. {
  91. struct stat stBuf;
  92. int free_dir_name = 0;
  93. char const * res = find_dir_name(opts, &free_dir_name);
  94. if (res == NULL)
  95. return res;
  96. /*
  97. * See if we can find the specified directory. We use a once-only loop
  98. * structure so we can bail out early.
  99. */
  100. if (stat(res, &stBuf) != 0) do {
  101. char z[AG_PATH_MAX];
  102. char * dirchp;
  103. /*
  104. * IF we could not, check to see if we got a full
  105. * path to a file name that has not been created yet.
  106. */
  107. if (errno != ENOENT) {
  108. bogus_name:
  109. fprintf(stderr, zsave_warn, opts->pzProgName, res);
  110. fprintf(stderr, zNoStat, errno, strerror(errno), res);
  111. if (free_dir_name)
  112. AGFREE(res);
  113. return NULL;
  114. }
  115. /*
  116. * Strip off the last component, stat the remaining string and
  117. * that string must name a directory
  118. */
  119. dirchp = strrchr(res, DIRCH);
  120. if (dirchp == NULL) {
  121. stBuf.st_mode = S_IFREG;
  122. break; /* found directory -- viz., "." */
  123. }
  124. if ((size_t)(dirchp - res) >= sizeof(z))
  125. goto bogus_name;
  126. memcpy(z, res, (size_t)(dirchp - res));
  127. z[dirchp - res] = NUL;
  128. if ((stat(z, &stBuf) != 0) || ! S_ISDIR(stBuf.st_mode))
  129. goto bogus_name;
  130. stBuf.st_mode = S_IFREG; /* file within this directory */
  131. } while (false);
  132. /*
  133. * IF what we found was a directory,
  134. * THEN tack on the config file name
  135. */
  136. if (S_ISDIR(stBuf.st_mode)) {
  137. {
  138. size_t sz = strlen(res) + strlen(opts->pzRcName) + 2;
  139. char * pzPath = (char *)AGALOC(sz, "file name");
  140. if ( snprintf(pzPath, sz, "%s/%s", res, opts->pzRcName)
  141. >= (int)sz)
  142. option_exits(EXIT_FAILURE);
  143. if (free_dir_name)
  144. AGFREE(res);
  145. res = pzPath;
  146. free_dir_name = 1;
  147. }
  148. /*
  149. * IF we cannot stat the object for any reason other than
  150. * it does not exist, then we bail out
  151. */
  152. if (stat(res, &stBuf) != 0) {
  153. if (errno != ENOENT) {
  154. fprintf(stderr, zsave_warn, opts->pzProgName, res);
  155. fprintf(stderr, zNoStat, errno, strerror(errno),
  156. res);
  157. AGFREE(res);
  158. return NULL;
  159. }
  160. /*
  161. * It does not exist yet, but it will be a regular file
  162. */
  163. stBuf.st_mode = S_IFREG;
  164. }
  165. }
  166. /*
  167. * Make sure that whatever we ultimately found, that it either is
  168. * or will soon be a file.
  169. */
  170. if (! S_ISREG(stBuf.st_mode)) {
  171. fprintf(stderr, zsave_warn, opts->pzProgName, res);
  172. if (free_dir_name)
  173. AGFREE(res);
  174. return NULL;
  175. }
  176. /*
  177. * Get rid of the old file
  178. */
  179. *p_free_name = free_dir_name;
  180. return res;
  181. }
  182. /**
  183. * print one option entry to the save file.
  184. *
  185. * @param[in] fp the file pointer for the save file
  186. * @param[in] od the option descriptor to print
  187. * @param[in] l_arg the last argument for the option
  188. * @param[in] save_fl include usage in comments
  189. */
  190. static void
  191. prt_entry(FILE * fp, tOptDesc * od, char const * l_arg, save_flags_mask_t save_fl)
  192. {
  193. int space_ct;
  194. if (save_fl & SVFL_USAGE)
  195. fprintf(fp, ao_name_use_fmt, od->pz_Name, od->pzText);
  196. if (UNUSED_OPT(od) && (save_fl & SVFL_DEFAULT))
  197. fputs(ao_default_use, fp);
  198. /*
  199. * There is an argument. Pad the name so values line up.
  200. * Not disabled *OR* this got equivalenced to another opt,
  201. * then use current option name.
  202. * Otherwise, there must be a disablement name.
  203. */
  204. {
  205. char const * pz =
  206. (od->pz_DisableName == NULL)
  207. ? od->pz_Name
  208. : (DISABLED_OPT(od)
  209. ? od->pz_DisableName
  210. : ((od->optEquivIndex == NO_EQUIVALENT)
  211. ? od->pz_Name : od->pz_DisableName)
  212. );
  213. space_ct = 17 - strlen(pz);
  214. fputs(pz, fp);
  215. }
  216. if ( (l_arg == NULL)
  217. && (OPTST_GET_ARGTYPE(od->fOptState) != OPARG_TYPE_NUMERIC))
  218. goto end_entry;
  219. fputs(" = ", fp);
  220. while (space_ct-- > 0) fputc(' ', fp);
  221. /*
  222. * IF the option is numeric only,
  223. * THEN the char pointer is really the number
  224. */
  225. if (OPTST_GET_ARGTYPE(od->fOptState) == OPARG_TYPE_NUMERIC)
  226. fprintf(fp, "%d", (int)(intptr_t)l_arg);
  227. else {
  228. for (;;) {
  229. char const * eol = strchr(l_arg, NL);
  230. /*
  231. * IF this is the last line
  232. * THEN bail and print it
  233. */
  234. if (eol == NULL)
  235. break;
  236. /*
  237. * Print the continuation and the text from the current line
  238. */
  239. (void)fwrite(l_arg, (size_t)(eol - l_arg), (size_t)1, fp);
  240. l_arg = eol+1; /* advance the Last Arg pointer */
  241. fputs("\\\n", fp);
  242. }
  243. /*
  244. * Terminate the entry
  245. */
  246. fputs(l_arg, fp);
  247. }
  248. end_entry:
  249. fputc(NL, fp);
  250. }
  251. /**
  252. * print an option's value
  253. *
  254. * @param[in] fp the file pointer for the save file
  255. * @param[in] od the option descriptor to print
  256. */
  257. static void
  258. prt_value(FILE * fp, int depth, tOptDesc * od, tOptionValue const * ovp)
  259. {
  260. while (--depth >= 0)
  261. putc(' ', fp), putc(' ', fp);
  262. switch (ovp->valType) {
  263. default:
  264. case OPARG_TYPE_NONE:
  265. fprintf(fp, NULL_ATR_FMT, ovp->pzName);
  266. break;
  267. case OPARG_TYPE_STRING:
  268. prt_string(fp, ovp->pzName, ovp->v.strVal);
  269. break;
  270. case OPARG_TYPE_ENUMERATION:
  271. case OPARG_TYPE_MEMBERSHIP:
  272. if (od != NULL) {
  273. uint32_t opt_state = od->fOptState;
  274. uintptr_t val = od->optArg.argEnum;
  275. char const * typ = (ovp->valType == OPARG_TYPE_ENUMERATION)
  276. ? "keyword" : "set-membership";
  277. fprintf(fp, TYPE_ATR_FMT, ovp->pzName, typ);
  278. /*
  279. * This is a magic incantation that will convert the
  280. * bit flag values back into a string suitable for printing.
  281. */
  282. (*(od->pOptProc))(OPTPROC_RETURN_VALNAME, od );
  283. if (od->optArg.argString != NULL) {
  284. fputs(od->optArg.argString, fp);
  285. if (ovp->valType != OPARG_TYPE_ENUMERATION) {
  286. /*
  287. * set membership strings get allocated
  288. */
  289. AGFREE(od->optArg.argString);
  290. }
  291. }
  292. od->optArg.argEnum = val;
  293. od->fOptState = opt_state;
  294. fprintf(fp, END_XML_FMT, ovp->pzName);
  295. break;
  296. }
  297. /* FALLTHROUGH */
  298. case OPARG_TYPE_NUMERIC:
  299. fprintf(fp, NUMB_ATR_FMT, ovp->pzName, ovp->v.longVal);
  300. break;
  301. case OPARG_TYPE_BOOLEAN:
  302. fprintf(fp, BOOL_ATR_FMT, ovp->pzName,
  303. ovp->v.boolVal ? "true" : "false");
  304. break;
  305. case OPARG_TYPE_HIERARCHY:
  306. prt_val_list(fp, ovp->pzName, ovp->v.nestVal);
  307. break;
  308. }
  309. }
  310. /**
  311. * Print a string value in XML format
  312. *
  313. * @param[in] fp the file pointer for the save file
  314. */
  315. static void
  316. prt_string(FILE * fp, char const * name, char const * pz)
  317. {
  318. fprintf(fp, OPEN_XML_FMT, name);
  319. for (;;) {
  320. int ch = ((int)*(pz++)) & 0xFF;
  321. switch (ch) {
  322. case NUL: goto string_done;
  323. case '&':
  324. case '<':
  325. case '>':
  326. #if __GNUC__ >= 4
  327. case 1 ... (' ' - 1):
  328. case ('~' + 1) ... 0xFF:
  329. #endif
  330. emit_special_char(fp, ch);
  331. break;
  332. default:
  333. #if __GNUC__ < 4
  334. if ( ((ch >= 1) && (ch <= (' ' - 1)))
  335. || ((ch >= ('~' + 1)) && (ch <= 0xFF)) ) {
  336. emit_special_char(fp, ch);
  337. break;
  338. }
  339. #endif
  340. putc(ch, fp);
  341. }
  342. } string_done:;
  343. fprintf(fp, END_XML_FMT, name);
  344. }
  345. /**
  346. * Print an option that can have multiple values in XML format
  347. *
  348. * @param[in] fp file pointer
  349. */
  350. static void
  351. prt_val_list(FILE * fp, char const * name, tArgList * al)
  352. {
  353. static int depth = 1;
  354. int sp_ct;
  355. int opt_ct;
  356. void ** opt_list;
  357. if (al == NULL)
  358. return;
  359. opt_ct = al->useCt;
  360. opt_list = (void **)al->apzArgs;
  361. if (opt_ct <= 0) {
  362. fprintf(fp, OPEN_CLOSE_FMT, name);
  363. return;
  364. }
  365. fprintf(fp, NESTED_OPT_FMT, name);
  366. depth++;
  367. while (--opt_ct >= 0) {
  368. tOptionValue const * ovp = *(opt_list++);
  369. prt_value(fp, depth, NULL, ovp);
  370. }
  371. depth--;
  372. for (sp_ct = depth; --sp_ct >= 0;)
  373. putc(' ', fp), putc(' ', fp);
  374. fprintf(fp, "</%s>\n", name);
  375. }
  376. /**
  377. * printed a nested/hierarchical value
  378. *
  379. * @param[in] fp file pointer
  380. * @param[in] od option descriptor
  381. * @param[in] save_fl include usage in comments
  382. */
  383. static void
  384. prt_nested(FILE * fp, tOptDesc * od, save_flags_mask_t save_fl)
  385. {
  386. int opt_ct;
  387. tArgList * al = od->optCookie;
  388. void ** opt_list;
  389. if (save_fl & SVFL_USAGE)
  390. fprintf(fp, ao_name_use_fmt, od->pz_Name, od->pzText);
  391. /*
  392. * Never show a default value if a hierarchical value is empty.
  393. */
  394. if (UNUSED_OPT(od) || (al == NULL))
  395. return;
  396. opt_ct = al->useCt;
  397. opt_list = (void **)al->apzArgs;
  398. if (opt_ct <= 0)
  399. return;
  400. do {
  401. tOptionValue const * base = *(opt_list++);
  402. tOptionValue const * ovp = optionGetValue(base, NULL);
  403. if (ovp == NULL)
  404. continue;
  405. fprintf(fp, NESTED_OPT_FMT, od->pz_Name);
  406. do {
  407. prt_value(fp, 1, od, ovp);
  408. } while (ovp = optionNextValue(base, ovp),
  409. ovp != NULL);
  410. fprintf(fp, "</%s>\n", od->pz_Name);
  411. } while (--opt_ct > 0);
  412. }
  413. /**
  414. * remove the current program settings
  415. *
  416. * @param[in] opts the program options structure
  417. * @param[in] fname the save file name
  418. */
  419. static void
  420. remove_settings(tOptions * opts, char const * fname)
  421. {
  422. size_t const name_len = strlen(opts->pzProgName);
  423. tmap_info_t map_info;
  424. char * text = text_mmap(fname, PROT_READ|PROT_WRITE, MAP_PRIVATE, &map_info);
  425. char * scan = text;
  426. for (;;) {
  427. char * next = scan = strstr(scan, zCfgProg);
  428. if (scan == NULL)
  429. goto leave;
  430. scan = SPN_WHITESPACE_CHARS(scan + zCfgProg_LEN);
  431. if ( (strneqvcmp(scan, opts->pzProgName, (int)name_len) == 0)
  432. && (IS_END_XML_TOKEN_CHAR(scan[name_len])) ) {
  433. scan = next;
  434. break;
  435. }
  436. }
  437. /*
  438. * If not NULL, "scan" points to the "<?program" string introducing
  439. * the program segment we are to remove. See if another segment follows.
  440. * If so, copy text. If not se trim off this segment.
  441. */
  442. {
  443. char * next = strstr(scan + zCfgProg_LEN, zCfgProg);
  444. size_t new_sz;
  445. if (next == NULL)
  446. new_sz = map_info.txt_size - strlen(scan);
  447. else {
  448. int fd = open(fname, O_RDWR);
  449. if (fd < 0) return;
  450. if (lseek(fd, (scan - text), SEEK_SET) < 0)
  451. scan = next;
  452. else if (write(fd, next, strlen(next)) < 0)
  453. scan = next;
  454. if (close(fd) < 0)
  455. scan = next;
  456. new_sz = map_info.txt_size - (next - scan);
  457. }
  458. if (new_sz != map_info.txt_size)
  459. if (truncate(fname, new_sz) < 0)
  460. scan = next; // we removed it, so shorten file
  461. }
  462. leave:
  463. text_munmap(&map_info);
  464. }
  465. /**
  466. * open the file for saving option state.
  467. *
  468. * @param[in] opts the program options structure
  469. * @param[in] save_fl flags for saving data
  470. * @returns the open file pointer. It may be NULL.
  471. */
  472. static FILE *
  473. open_sv_file(tOptions * opts, save_flags_mask_t save_fl)
  474. {
  475. FILE * fp;
  476. {
  477. int free_name = 0;
  478. char const * fname = find_file_name(opts, &free_name);
  479. if (fname == NULL)
  480. return NULL;
  481. if (save_fl == 0)
  482. unlink(fname);
  483. else
  484. remove_settings(opts, fname);
  485. fp = fopen(fname, "a" FOPEN_BINARY_FLAG);
  486. if (fp == NULL) {
  487. fprintf(stderr, zsave_warn, opts->pzProgName, fname);
  488. fprintf(stderr, zNoCreat, errno, strerror(errno), fname);
  489. if (free_name)
  490. AGFREE(fname);
  491. return fp;
  492. }
  493. if (free_name)
  494. AGFREE(fname);
  495. }
  496. do {
  497. struct stat sbuf;
  498. if (fstat(fileno(fp), &sbuf) < 0)
  499. break;
  500. if (sbuf.st_size > zPresetFile_LEN) {
  501. /* non-zero size implies save_fl is non-zero */
  502. fprintf(fp, zFmtProg, opts->pzProgName);
  503. return fp;
  504. }
  505. } while (false);
  506. /*
  507. * We have a new file. Insert a header
  508. */
  509. fputs("# ", fp);
  510. {
  511. char const * e = strchr(opts->pzUsageTitle, NL);
  512. if (e++ != NULL)
  513. fwrite(opts->pzUsageTitle, 1, e - opts->pzUsageTitle, fp);
  514. }
  515. {
  516. time_t cur_time = time(NULL);
  517. char * time_str = ctime(&cur_time);
  518. fprintf(fp, zPresetFile, time_str);
  519. #ifdef HAVE_ALLOCATED_CTIME
  520. /*
  521. * The return values for ctime(), localtime(), and gmtime()
  522. * normally point to static data that is overwritten by each call.
  523. * The test to detect allocated ctime, so we leak the memory.
  524. */
  525. AGFREE(time_str);
  526. #endif
  527. }
  528. if (save_fl != 0)
  529. fprintf(fp, zFmtProg, opts->pzProgName);
  530. return fp;
  531. }
  532. /**
  533. * print option without an arg
  534. *
  535. * @param[in] fp file pointer
  536. * @param[in] vod value option descriptor
  537. * @param[in] pod primary option descriptor
  538. * @param[in] save_fl include usage in comments
  539. */
  540. static void
  541. prt_no_arg_opt(FILE * fp, tOptDesc * vod, tOptDesc * pod, save_flags_mask_t save_fl)
  542. {
  543. /*
  544. * The aliased to argument indicates whether or not the option
  545. * is "disabled". However, the original option has the name
  546. * string, so we get that there, not with "vod".
  547. */
  548. char const * pznm =
  549. (DISABLED_OPT(vod)) ? pod->pz_DisableName : pod->pz_Name;
  550. /*
  551. * If the option was disabled and the disablement name is NULL,
  552. * then the disablement was caused by aliasing.
  553. * Use the name as the string to emit.
  554. */
  555. if (pznm == NULL)
  556. pznm = pod->pz_Name;
  557. if (save_fl & SVFL_USAGE)
  558. fprintf(fp, ao_name_use_fmt, pod->pz_Name, pod->pzText);
  559. if (UNUSED_OPT(pod) && (save_fl & SVFL_DEFAULT))
  560. fputs(ao_default_use, fp);
  561. fprintf(fp, "%s\n", pznm);
  562. }
  563. /**
  564. * print the string valued argument(s).
  565. *
  566. * @param[in] fp file pointer
  567. * @param[in] od value option descriptor
  568. * @param[in] save_fl include usage in comments
  569. */
  570. static void
  571. prt_str_arg(FILE * fp, tOptDesc * od, save_flags_mask_t save_fl)
  572. {
  573. if (UNUSED_OPT(od) || ((od->fOptState & OPTST_STACKED) == 0)) {
  574. char const * arg = od->optArg.argString;
  575. if (arg == NULL)
  576. arg = "''";
  577. prt_entry(fp, od, arg, save_fl);
  578. } else {
  579. tArgList * pAL = (tArgList *)od->optCookie;
  580. int uct = pAL->useCt;
  581. char const ** ppz = pAL->apzArgs;
  582. /*
  583. * un-disable multiple copies of disabled options.
  584. */
  585. if (uct > 1)
  586. od->fOptState &= ~OPTST_DISABLED;
  587. while (uct-- > 0) {
  588. prt_entry(fp, od, *(ppz++), save_fl);
  589. save_fl &= ~SVFL_USAGE;
  590. }
  591. }
  592. }
  593. /**
  594. * print the string value of an enumeration.
  595. *
  596. * @param[in] fp the file pointer to write to
  597. * @param[in] od the option descriptor with the enumerated value
  598. * @param[in] save_fl include usage in comments
  599. */
  600. static void
  601. prt_enum_arg(FILE * fp, tOptDesc * od, save_flags_mask_t save_fl)
  602. {
  603. uintptr_t val = od->optArg.argEnum;
  604. /*
  605. * This is a magic incantation that will convert the
  606. * bit flag values back into a string suitable for printing.
  607. */
  608. (*(od->pOptProc))(OPTPROC_RETURN_VALNAME, od);
  609. prt_entry(fp, od, VOIDP(od->optArg.argString), save_fl);
  610. od->optArg.argEnum = val;
  611. }
  612. /**
  613. * Print the bits set in a bit mask option.
  614. *
  615. * We call the option handling function with a magic value for
  616. * the options pointer and it allocates and fills in the string.
  617. * We print that with a call to prt_entry().
  618. *
  619. * @param[in] fp the file pointer to write to
  620. * @param[in] od the option descriptor with a bit mask value type
  621. * @param[in] save_fl include usage in comments
  622. */
  623. static void
  624. prt_set_arg(FILE * fp, tOptDesc * od, save_flags_mask_t save_fl)
  625. {
  626. char * list = optionMemberList(od);
  627. size_t len = strlen(list);
  628. char * buf = (char *)AGALOC(len + 3, "dir name");
  629. *buf= '=';
  630. memcpy(buf+1, list, len + 1);
  631. prt_entry(fp, od, buf, save_fl);
  632. AGFREE(buf);
  633. AGFREE(list);
  634. }
  635. /**
  636. * figure out what the option file name argument is.
  637. * If one can be found, call prt_entry() to emit it.
  638. *
  639. * @param[in] fp the file pointer to write to.
  640. * @param[in] od the option descriptor with a bit mask value type
  641. * @param[in] opts the program options descriptor
  642. * @param[in] save_fl include usage in comments
  643. */
  644. static void
  645. prt_file_arg(FILE * fp, tOptDesc * od, tOptions * opts, save_flags_mask_t save_fl)
  646. {
  647. /*
  648. * If the cookie is not NULL, then it has the file name, period.
  649. * Otherwise, if we have a non-NULL string argument, then....
  650. */
  651. if (od->optCookie != NULL)
  652. prt_entry(fp, od, od->optCookie, save_fl);
  653. else if (HAS_originalOptArgArray(opts)) {
  654. char const * orig =
  655. opts->originalOptArgArray[od->optIndex].argString;
  656. if (od->optArg.argString == orig) {
  657. if (save_fl)
  658. fprintf(fp, ao_name_use_fmt, od->pz_Name, od->pzText);
  659. return;
  660. }
  661. prt_entry(fp, od, od->optArg.argString, save_fl);
  662. } else if (save_fl)
  663. fprintf(fp, ao_name_use_fmt, od->pz_Name, od->pzText);
  664. }
  665. /*=export_func optionSaveFile
  666. *
  667. * what: saves the option state to a file
  668. *
  669. * arg: tOptions *, opts, program options descriptor
  670. *
  671. * doc:
  672. *
  673. * This routine will save the state of option processing to a file. The name
  674. * of that file can be specified with the argument to the @code{--save-opts}
  675. * option, or by appending the @code{rcfile} attribute to the last
  676. * @code{homerc} attribute. If no @code{rcfile} attribute was specified, it
  677. * will default to @code{.@i{programname}rc}. If you wish to specify another
  678. * file, you should invoke the @code{SET_OPT_SAVE_OPTS(@i{filename})} macro.
  679. *
  680. * The recommend usage is as follows:
  681. * @example
  682. * optionProcess(&progOptions, argc, argv);
  683. * if (i_want_a_non_standard_place_for_this)
  684. * SET_OPT_SAVE_OPTS("myfilename");
  685. * optionSaveFile(&progOptions);
  686. * @end example
  687. *
  688. * err:
  689. *
  690. * If no @code{homerc} file was specified, this routine will silently return
  691. * and do nothing. If the output file cannot be created or updated, a message
  692. * will be printed to @code{stderr} and the routine will return.
  693. =*/
  694. void
  695. optionSaveFile(tOptions * opts)
  696. {
  697. tOptDesc * od;
  698. int ct;
  699. FILE * fp;
  700. save_flags_mask_t save_flags = SVFL_NONE;
  701. do {
  702. char * temp_str;
  703. char const * dir = opts->pOptDesc[ opts->specOptIdx.save_opts ].optArg.argString;
  704. size_t flen;
  705. if (dir == NULL)
  706. break;
  707. temp_str = strchr(dir, '>');
  708. if (temp_str == NULL)
  709. break;
  710. if (temp_str[1] == '>')
  711. save_flags = SVFL_UPDATE;
  712. flen = (temp_str - dir);
  713. if (flen == 0)
  714. break;
  715. temp_str = AGALOC(flen + 1, "flag search str");
  716. memcpy(temp_str, dir, flen);
  717. temp_str[flen] = NUL;
  718. save_flags |= save_flags_str2mask(temp_str, SVFL_NONE);
  719. AGFREE(temp_str);
  720. } while (false);
  721. fp = open_sv_file(opts, save_flags & SVFL_UPDATE);
  722. if (fp == NULL)
  723. return;
  724. /*
  725. * FOR each of the defined options, ...
  726. */
  727. ct = opts->presetOptCt;
  728. od = opts->pOptDesc;
  729. do {
  730. tOptDesc * vod;
  731. /*
  732. * Equivalenced options get picked up when the equivalenced-to
  733. * option is processed. And do not save options with any state
  734. * bits in the DO_NOT_SAVE collection
  735. *
  736. * ** option cannot be preset
  737. * #define OPTST_NO_INIT 0x0000100U
  738. * ** disable from cmd line
  739. * #define OPTST_NO_COMMAND 0x2000000U
  740. * ** alias for other option
  741. * #define OPTST_ALIAS 0x8000000U
  742. */
  743. if ((od->fOptState & OPTST_DO_NOT_SAVE_MASK) != 0)
  744. continue;
  745. if ( (od->optEquivIndex != NO_EQUIVALENT)
  746. && (od->optEquivIndex != od->optIndex))
  747. continue;
  748. if (UNUSED_OPT(od) && ((save_flags & SVFL_USAGE_DEFAULT_MASK) == SVFL_NONE))
  749. continue;
  750. /*
  751. * The option argument data are found at the equivalenced-to option,
  752. * but the actual option argument type comes from the original
  753. * option descriptor. Be careful!
  754. */
  755. vod = ((od->fOptState & OPTST_EQUIVALENCE) != 0)
  756. ? (opts->pOptDesc + od->optActualIndex) : od;
  757. switch (OPTST_GET_ARGTYPE(od->fOptState)) {
  758. case OPARG_TYPE_NONE:
  759. prt_no_arg_opt(fp, vod, od, save_flags);
  760. break;
  761. case OPARG_TYPE_NUMERIC:
  762. prt_entry(fp, vod, VOIDP(vod->optArg.argInt), save_flags);
  763. break;
  764. case OPARG_TYPE_STRING:
  765. prt_str_arg(fp, vod, save_flags);
  766. break;
  767. case OPARG_TYPE_ENUMERATION:
  768. prt_enum_arg(fp, vod, save_flags);
  769. break;
  770. case OPARG_TYPE_MEMBERSHIP:
  771. prt_set_arg(fp, vod, save_flags);
  772. break;
  773. case OPARG_TYPE_BOOLEAN:
  774. prt_entry(fp, vod, vod->optArg.argBool ? "true" : "false", save_flags);
  775. break;
  776. case OPARG_TYPE_HIERARCHY:
  777. prt_nested(fp, vod, save_flags);
  778. break;
  779. case OPARG_TYPE_FILE:
  780. prt_file_arg(fp, vod, opts, save_flags);
  781. break;
  782. default:
  783. break; /* cannot handle - skip it */
  784. }
  785. } while (od++, (--ct > 0));
  786. fclose(fp);
  787. }
  788. /** @}
  789. *
  790. * Local Variables:
  791. * mode: C
  792. * c-file-style: "stroustrup"
  793. * indent-tabs-mode: nil
  794. * End:
  795. * end of autoopts/save.c */