save.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
  1. /*
  2. * save.c $Id: save.c,v 4.28 2008/09/28 23:23:36 bkorb Exp $
  3. * Time-stamp: "2008-09-23 14:24:58 bkorb"
  4. *
  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.
  8. *
  9. * This file is part of AutoOpts, a companion to AutoGen.
  10. * AutoOpts is free software.
  11. * AutoOpts is copyright (c) 1992-2008 by Bruce Korb - all rights reserved
  12. * AutoOpts is copyright (c) 1992-2008 by Bruce Korb - all rights reserved
  13. *
  14. * AutoOpts is available under any one of two licenses. The license
  15. * in use must be one of these two and the choice is under the control
  16. * of the user of the license.
  17. *
  18. * The GNU Lesser General Public License, version 3 or later
  19. * See the files "COPYING.lgplv3" and "COPYING.gplv3"
  20. *
  21. * The Modified Berkeley Software Distribution License
  22. * See the file "COPYING.mbsd"
  23. *
  24. * These files have the following md5sums:
  25. *
  26. * 239588c55c22c60ffe159946a760a33e pkg/libopts/COPYING.gplv3
  27. * fa82ca978890795162346e661b47161a pkg/libopts/COPYING.lgplv3
  28. * 66a5cedaf62c4b2637025f049f9b826f pkg/libopts/COPYING.mbsd
  29. */
  30. static char const zWarn[] = "%s WARNING: cannot save options - ";
  31. static char const close_xml[] = "</%s>\n";
  32. /* = = = START-STATIC-FORWARD = = = */
  33. /* static forward declarations maintained by mk-fwd */
  34. static tCC*
  35. findDirName( tOptions* pOpts, int* p_free );
  36. static tCC*
  37. findFileName( tOptions* pOpts, int* p_free_name );
  38. static void
  39. printEntry(
  40. FILE * fp,
  41. tOptDesc * p,
  42. tCC* pzLA );
  43. static void
  44. print_a_value(FILE * fp, int depth, tOptDesc * pOD, tOptionValue const * ovp);
  45. static void
  46. print_a_string(FILE * fp, char const * name, char const * pz);
  47. static void
  48. printValueList(FILE * fp, char const * name, tArgList * al);
  49. static void
  50. printHierarchy(FILE * fp, tOptDesc * p);
  51. static FILE *
  52. openSaveFile( tOptions* pOpts );
  53. static void
  54. printNoArgOpt(FILE * fp, tOptDesc * p, tOptDesc * pOD);
  55. static void
  56. printStringArg(FILE * fp, tOptDesc * pOD);
  57. static void
  58. printEnumArg(FILE * fp, tOptDesc * pOD);
  59. static void
  60. printSetMemberArg(FILE * fp, tOptDesc * pOD);
  61. static void
  62. printFileArg(FILE * fp, tOptDesc * pOD, tOptions* pOpts);
  63. /* = = = END-STATIC-FORWARD = = = */
  64. static tCC*
  65. findDirName( tOptions* pOpts, int* p_free )
  66. {
  67. tCC* pzDir;
  68. if (pOpts->specOptIdx.save_opts == 0)
  69. return NULL;
  70. pzDir = pOpts->pOptDesc[ pOpts->specOptIdx.save_opts ].optArg.argString;
  71. if ((pzDir != NULL) && (*pzDir != NUL))
  72. return pzDir;
  73. /*
  74. * This function only works if there is a directory where
  75. * we can stash the RC (INI) file.
  76. */
  77. {
  78. tCC* const* papz = pOpts->papzHomeList;
  79. if (papz == NULL)
  80. return NULL;
  81. while (papz[1] != NULL) papz++;
  82. pzDir = *papz;
  83. }
  84. /*
  85. * IF it does not require deciphering an env value, then just copy it
  86. */
  87. if (*pzDir != '$')
  88. return pzDir;
  89. {
  90. tCC* pzEndDir = strchr( ++pzDir, DIRCH );
  91. char* pzFileName;
  92. char* pzEnv;
  93. if (pzEndDir != NULL) {
  94. char z[ AO_NAME_SIZE ];
  95. if ((pzEndDir - pzDir) > AO_NAME_LIMIT )
  96. return NULL;
  97. strncpy( z, pzDir, (size_t)(pzEndDir - pzDir) );
  98. z[ (pzEndDir - pzDir) ] = NUL;
  99. pzEnv = getenv( z );
  100. } else {
  101. /*
  102. * Make sure we can get the env value (after stripping off
  103. * any trailing directory or file names)
  104. */
  105. pzEnv = getenv( pzDir );
  106. }
  107. if (pzEnv == NULL) {
  108. fprintf( stderr, zWarn, pOpts->pzProgName );
  109. fprintf( stderr, zNotDef, pzDir );
  110. return NULL;
  111. }
  112. if (pzEndDir == NULL)
  113. return pzEnv;
  114. {
  115. size_t sz = strlen( pzEnv ) + strlen( pzEndDir ) + 2;
  116. pzFileName = (char*)AGALOC( sz, "dir name" );
  117. }
  118. if (pzFileName == NULL)
  119. return NULL;
  120. *p_free = 1;
  121. /*
  122. * Glue together the full name into the allocated memory.
  123. * FIXME: We lose track of this memory.
  124. */
  125. sprintf( pzFileName, "%s/%s", pzEnv, pzEndDir );
  126. return pzFileName;
  127. }
  128. }
  129. static tCC*
  130. findFileName( tOptions* pOpts, int* p_free_name )
  131. {
  132. tCC* pzDir;
  133. struct stat stBuf;
  134. int free_dir_name = 0;
  135. pzDir = findDirName( pOpts, &free_dir_name );
  136. if (pzDir == NULL)
  137. return NULL;
  138. /*
  139. * See if we can find the specified directory. We use a once-only loop
  140. * structure so we can bail out early.
  141. */
  142. if (stat( pzDir, &stBuf ) != 0) do {
  143. /*
  144. * IF we could not, check to see if we got a full
  145. * path to a file name that has not been created yet.
  146. */
  147. if (errno == ENOENT) {
  148. char z[AG_PATH_MAX];
  149. /*
  150. * Strip off the last component, stat the remaining string and
  151. * that string must name a directory
  152. */
  153. char* pzDirCh = strrchr( pzDir, DIRCH );
  154. if (pzDirCh == NULL) {
  155. stBuf.st_mode = S_IFREG;
  156. continue; /* bail out of error condition */
  157. }
  158. strncpy( z, pzDir, (size_t)(pzDirCh - pzDir));
  159. z[ pzDirCh - pzDir ] = NUL;
  160. if ( (stat( z, &stBuf ) == 0)
  161. && S_ISDIR( stBuf.st_mode )) {
  162. /*
  163. * We found the directory. Restore the file name and
  164. * mark the full name as a regular file
  165. */
  166. stBuf.st_mode = S_IFREG;
  167. continue; /* bail out of error condition */
  168. }
  169. }
  170. /*
  171. * We got a bogus name.
  172. */
  173. fprintf( stderr, zWarn, pOpts->pzProgName );
  174. fprintf( stderr, zNoStat, errno, strerror( errno ), pzDir );
  175. if (free_dir_name)
  176. AGFREE( (void*)pzDir );
  177. return NULL;
  178. } while (0);
  179. /*
  180. * IF what we found was a directory,
  181. * THEN tack on the config file name
  182. */
  183. if (S_ISDIR( stBuf.st_mode )) {
  184. size_t sz = strlen( pzDir ) + strlen( pOpts->pzRcName ) + 2;
  185. {
  186. char* pzPath = (char*)AGALOC( sz, "file name" );
  187. #ifdef HAVE_SNPRINTF
  188. snprintf( pzPath, sz, "%s/%s", pzDir, pOpts->pzRcName );
  189. #else
  190. sprintf( pzPath, "%s/%s", pzDir, pOpts->pzRcName );
  191. #endif
  192. if (free_dir_name)
  193. AGFREE( (void*)pzDir );
  194. pzDir = pzPath;
  195. free_dir_name = 1;
  196. }
  197. /*
  198. * IF we cannot stat the object for any reason other than
  199. * it does not exist, then we bail out
  200. */
  201. if (stat( pzDir, &stBuf ) != 0) {
  202. if (errno != ENOENT) {
  203. fprintf( stderr, zWarn, pOpts->pzProgName );
  204. fprintf( stderr, zNoStat, errno, strerror( errno ),
  205. pzDir );
  206. AGFREE( (void*)pzDir );
  207. return NULL;
  208. }
  209. /*
  210. * It does not exist yet, but it will be a regular file
  211. */
  212. stBuf.st_mode = S_IFREG;
  213. }
  214. }
  215. /*
  216. * Make sure that whatever we ultimately found, that it either is
  217. * or will soon be a file.
  218. */
  219. if (! S_ISREG( stBuf.st_mode )) {
  220. fprintf( stderr, zWarn, pOpts->pzProgName );
  221. fprintf( stderr, zNotFile, pzDir );
  222. if (free_dir_name)
  223. AGFREE( (void*)pzDir );
  224. return NULL;
  225. }
  226. /*
  227. * Get rid of the old file
  228. */
  229. unlink( pzDir );
  230. *p_free_name = free_dir_name;
  231. return pzDir;
  232. }
  233. static void
  234. printEntry(
  235. FILE * fp,
  236. tOptDesc * p,
  237. tCC* pzLA )
  238. {
  239. /*
  240. * There is an argument. Pad the name so values line up.
  241. * Not disabled *OR* this got equivalenced to another opt,
  242. * then use current option name.
  243. * Otherwise, there must be a disablement name.
  244. */
  245. {
  246. char const * pz;
  247. if (! DISABLED_OPT(p) || (p->optEquivIndex != NO_EQUIVALENT))
  248. pz = p->pz_Name;
  249. else
  250. pz = p->pz_DisableName;
  251. fprintf(fp, "%-18s", pz);
  252. }
  253. /*
  254. * IF the option is numeric only,
  255. * THEN the char pointer is really the number
  256. */
  257. if (OPTST_GET_ARGTYPE(p->fOptState) == OPARG_TYPE_NUMERIC)
  258. fprintf( fp, " %d\n", (int)(t_word)pzLA );
  259. /*
  260. * OTHERWISE, FOR each line of the value text, ...
  261. */
  262. else if (pzLA == NULL)
  263. fputc( '\n', fp );
  264. else {
  265. fputc( ' ', fp ); fputc( ' ', fp );
  266. for (;;) {
  267. tCC* pzNl = strchr( pzLA, '\n' );
  268. /*
  269. * IF this is the last line
  270. * THEN bail and print it
  271. */
  272. if (pzNl == NULL)
  273. break;
  274. /*
  275. * Print the continuation and the text from the current line
  276. */
  277. (void)fwrite( pzLA, (size_t)(pzNl - pzLA), (size_t)1, fp );
  278. pzLA = pzNl+1; /* advance the Last Arg pointer */
  279. fputs( "\\\n", fp );
  280. }
  281. /*
  282. * Terminate the entry
  283. */
  284. fputs( pzLA, fp );
  285. fputc( '\n', fp );
  286. }
  287. }
  288. static void
  289. print_a_value(FILE * fp, int depth, tOptDesc * pOD, tOptionValue const * ovp)
  290. {
  291. static char const bool_atr[] = "<%1$s type=boolean>%2$s</%1$s>\n";
  292. static char const numb_atr[] = "<%1$s type=integer>0x%2$lX</%1$s>\n";
  293. static char const type_atr[] = "<%s type=%s>";
  294. static char const null_atr[] = "<%s/>\n";
  295. while (--depth >= 0)
  296. putc(' ', fp), putc(' ', fp);
  297. switch (ovp->valType) {
  298. default:
  299. case OPARG_TYPE_NONE:
  300. fprintf(fp, null_atr, ovp->pzName);
  301. break;
  302. case OPARG_TYPE_STRING:
  303. print_a_string(fp, ovp->pzName, ovp->v.strVal);
  304. break;
  305. case OPARG_TYPE_ENUMERATION:
  306. case OPARG_TYPE_MEMBERSHIP:
  307. if (pOD != NULL) {
  308. tAoUI opt_state = pOD->fOptState;
  309. uintptr_t val = pOD->optArg.argEnum;
  310. char const * typ = (ovp->valType == OPARG_TYPE_ENUMERATION)
  311. ? "keyword" : "set-membership";
  312. fprintf(fp, type_atr, ovp->pzName, typ);
  313. /*
  314. * This is a magic incantation that will convert the
  315. * bit flag values back into a string suitable for printing.
  316. */
  317. (*(pOD->pOptProc))(OPTPROC_RETURN_VALNAME, pOD );
  318. if (pOD->optArg.argString != NULL) {
  319. fputs(pOD->optArg.argString, fp);
  320. if (ovp->valType != OPARG_TYPE_ENUMERATION) {
  321. /*
  322. * set membership strings get allocated
  323. */
  324. AGFREE( (void*)pOD->optArg.argString );
  325. }
  326. }
  327. pOD->optArg.argEnum = val;
  328. pOD->fOptState = opt_state;
  329. fprintf(fp, close_xml, ovp->pzName);
  330. break;
  331. }
  332. /* FALLTHROUGH */
  333. case OPARG_TYPE_NUMERIC:
  334. fprintf(fp, numb_atr, ovp->pzName, ovp->v.longVal);
  335. break;
  336. case OPARG_TYPE_BOOLEAN:
  337. fprintf(fp, bool_atr, ovp->pzName,
  338. ovp->v.boolVal ? "true" : "false");
  339. break;
  340. case OPARG_TYPE_HIERARCHY:
  341. printValueList(fp, ovp->pzName, ovp->v.nestVal);
  342. break;
  343. }
  344. }
  345. static void
  346. print_a_string(FILE * fp, char const * name, char const * pz)
  347. {
  348. static char const open_atr[] = "<%s>";
  349. fprintf(fp, open_atr, name);
  350. for (;;) {
  351. int ch = ((int)*(pz++)) & 0xFF;
  352. switch (ch) {
  353. case NUL: goto string_done;
  354. case '&':
  355. case '<':
  356. case '>':
  357. #if __GNUC__ >= 4
  358. case 1 ... (' ' - 1):
  359. case ('~' + 1) ... 0xFF:
  360. #endif
  361. emit_special_char(fp, ch);
  362. break;
  363. default:
  364. #if __GNUC__ < 4
  365. if ( ((ch >= 1) && (ch <= (' ' - 1)))
  366. || ((ch >= ('~' + 1)) && (ch <= 0xFF)) ) {
  367. emit_special_char(fp, ch);
  368. break;
  369. }
  370. #endif
  371. putc(ch, fp);
  372. }
  373. } string_done:;
  374. fprintf(fp, close_xml, name);
  375. }
  376. static void
  377. printValueList(FILE * fp, char const * name, tArgList * al)
  378. {
  379. static int depth = 1;
  380. int sp_ct;
  381. int opt_ct;
  382. void ** opt_list;
  383. if (al == NULL)
  384. return;
  385. opt_ct = al->useCt;
  386. opt_list = (void **)al->apzArgs;
  387. if (opt_ct <= 0) {
  388. fprintf(fp, "<%s/>\n", name);
  389. return;
  390. }
  391. fprintf(fp, "<%s type=nested>\n", name);
  392. depth++;
  393. while (--opt_ct >= 0) {
  394. tOptionValue const * ovp = *(opt_list++);
  395. print_a_value(fp, depth, NULL, ovp);
  396. }
  397. depth--;
  398. for (sp_ct = depth; --sp_ct >= 0;)
  399. putc(' ', fp), putc(' ', fp);
  400. fprintf(fp, "</%s>\n", name);
  401. }
  402. static void
  403. printHierarchy(FILE * fp, tOptDesc * p)
  404. {
  405. int opt_ct;
  406. tArgList * al = p->optCookie;
  407. void ** opt_list;
  408. if (al == NULL)
  409. return;
  410. opt_ct = al->useCt;
  411. opt_list = (void **)al->apzArgs;
  412. if (opt_ct <= 0)
  413. return;
  414. do {
  415. tOptionValue const * base = *(opt_list++);
  416. tOptionValue const * ovp = optionGetValue(base, NULL);
  417. if (ovp == NULL)
  418. continue;
  419. fprintf(fp, "<%s type=nested>\n", p->pz_Name);
  420. do {
  421. print_a_value(fp, 1, p, ovp);
  422. } while (ovp = optionNextValue(base, ovp),
  423. ovp != NULL);
  424. fprintf(fp, "</%s>\n", p->pz_Name);
  425. } while (--opt_ct > 0);
  426. }
  427. static FILE *
  428. openSaveFile( tOptions* pOpts )
  429. {
  430. FILE* fp;
  431. {
  432. int free_name = 0;
  433. tCC* pzFName = findFileName( pOpts, &free_name );
  434. if (pzFName == NULL)
  435. return NULL;
  436. fp = fopen( pzFName, "w" FOPEN_BINARY_FLAG );
  437. if (fp == NULL) {
  438. fprintf( stderr, zWarn, pOpts->pzProgName );
  439. fprintf( stderr, zNoCreat, errno, strerror( errno ), pzFName );
  440. if (free_name)
  441. AGFREE((void*) pzFName );
  442. return fp;
  443. }
  444. if (free_name)
  445. AGFREE( (void*)pzFName );
  446. }
  447. {
  448. char const* pz = pOpts->pzUsageTitle;
  449. fputs( "# ", fp );
  450. do { fputc( *pz, fp ); } while (*(pz++) != '\n');
  451. }
  452. {
  453. time_t timeVal = time( NULL );
  454. char* pzTime = ctime( &timeVal );
  455. fprintf( fp, zPresetFile, pzTime );
  456. #ifdef HAVE_ALLOCATED_CTIME
  457. /*
  458. * The return values for ctime(), localtime(), and gmtime()
  459. * normally point to static data that is overwritten by each call.
  460. * The test to detect allocated ctime, so we leak the memory.
  461. */
  462. AGFREE( (void*)pzTime );
  463. #endif
  464. }
  465. return fp;
  466. }
  467. static void
  468. printNoArgOpt(FILE * fp, tOptDesc * p, tOptDesc * pOD)
  469. {
  470. /*
  471. * The aliased to argument indicates whether or not the option
  472. * is "disabled". However, the original option has the name
  473. * string, so we get that there, not with "p".
  474. */
  475. char const * pznm =
  476. (DISABLED_OPT( p )) ? pOD->pz_DisableName : pOD->pz_Name;
  477. /*
  478. * If the option was disabled and the disablement name is NULL,
  479. * then the disablement was caused by aliasing.
  480. * Use the name as the string to emit.
  481. */
  482. if (pznm == NULL)
  483. pznm = pOD->pz_Name;
  484. fprintf(fp, "%s\n", pznm);
  485. }
  486. static void
  487. printStringArg(FILE * fp, tOptDesc * pOD)
  488. {
  489. if (pOD->fOptState & OPTST_STACKED) {
  490. tArgList* pAL = (tArgList*)pOD->optCookie;
  491. int uct = pAL->useCt;
  492. tCC** ppz = pAL->apzArgs;
  493. /*
  494. * un-disable multiple copies of disabled options.
  495. */
  496. if (uct > 1)
  497. pOD->fOptState &= ~OPTST_DISABLED;
  498. while (uct-- > 0)
  499. printEntry( fp, pOD, *(ppz++) );
  500. } else {
  501. printEntry( fp, pOD, pOD->optArg.argString );
  502. }
  503. }
  504. static void
  505. printEnumArg(FILE * fp, tOptDesc * pOD)
  506. {
  507. uintptr_t val = pOD->optArg.argEnum;
  508. /*
  509. * This is a magic incantation that will convert the
  510. * bit flag values back into a string suitable for printing.
  511. */
  512. (*(pOD->pOptProc))(OPTPROC_RETURN_VALNAME, pOD);
  513. printEntry( fp, pOD, (void*)(pOD->optArg.argString));
  514. pOD->optArg.argEnum = val;
  515. }
  516. static void
  517. printSetMemberArg(FILE * fp, tOptDesc * pOD)
  518. {
  519. uintptr_t val = pOD->optArg.argEnum;
  520. /*
  521. * This is a magic incantation that will convert the
  522. * bit flag values back into a string suitable for printing.
  523. */
  524. (*(pOD->pOptProc))(OPTPROC_RETURN_VALNAME, pOD);
  525. printEntry( fp, pOD, (void*)(pOD->optArg.argString));
  526. if (pOD->optArg.argString != NULL) {
  527. /*
  528. * set membership strings get allocated
  529. */
  530. AGFREE( (void*)pOD->optArg.argString );
  531. pOD->fOptState &= ~OPTST_ALLOC_ARG;
  532. }
  533. pOD->optArg.argEnum = val;
  534. }
  535. static void
  536. printFileArg(FILE * fp, tOptDesc * pOD, tOptions* pOpts)
  537. {
  538. /*
  539. * If the cookie is not NULL, then it has the file name, period.
  540. * Otherwise, if we have a non-NULL string argument, then....
  541. */
  542. if (pOD->optCookie != NULL)
  543. printEntry(fp, pOD, pOD->optCookie);
  544. else if (HAS_originalOptArgArray(pOpts)) {
  545. char const * orig =
  546. pOpts->originalOptArgArray[pOD->optIndex].argString;
  547. if (pOD->optArg.argString == orig)
  548. return;
  549. printEntry(fp, pOD, pOD->optArg.argString);
  550. }
  551. }
  552. /*=export_func optionSaveFile
  553. *
  554. * what: saves the option state to a file
  555. *
  556. * arg: tOptions*, pOpts, program options descriptor
  557. *
  558. * doc:
  559. *
  560. * This routine will save the state of option processing to a file. The name
  561. * of that file can be specified with the argument to the @code{--save-opts}
  562. * option, or by appending the @code{rcfile} attribute to the last
  563. * @code{homerc} attribute. If no @code{rcfile} attribute was specified, it
  564. * will default to @code{.@i{programname}rc}. If you wish to specify another
  565. * file, you should invoke the @code{SET_OPT_SAVE_OPTS( @i{filename} )} macro.
  566. *
  567. * The recommend usage is as follows:
  568. * @example
  569. * optionProcess(&progOptions, argc, argv);
  570. * if (i_want_a_non_standard_place_for_this)
  571. * SET_OPT_SAVE_OPTS("myfilename");
  572. * optionSaveFile(&progOptions);
  573. * @end example
  574. *
  575. * err:
  576. *
  577. * If no @code{homerc} file was specified, this routine will silently return
  578. * and do nothing. If the output file cannot be created or updated, a message
  579. * will be printed to @code{stderr} and the routine will return.
  580. =*/
  581. void
  582. optionSaveFile( tOptions* pOpts )
  583. {
  584. tOptDesc* pOD;
  585. int ct;
  586. FILE* fp = openSaveFile(pOpts);
  587. if (fp == NULL)
  588. return;
  589. /*
  590. * FOR each of the defined options, ...
  591. */
  592. ct = pOpts->presetOptCt;
  593. pOD = pOpts->pOptDesc;
  594. do {
  595. tOptDesc* p;
  596. /*
  597. * IF the option has not been defined
  598. * OR it does not take an initialization value
  599. * OR it is equivalenced to another option
  600. * THEN continue (ignore it)
  601. *
  602. * Equivalenced options get picked up when the equivalenced-to
  603. * option is processed.
  604. */
  605. if (UNUSED_OPT( pOD ))
  606. continue;
  607. if ((pOD->fOptState & OPTST_DO_NOT_SAVE_MASK) != 0)
  608. continue;
  609. if ( (pOD->optEquivIndex != NO_EQUIVALENT)
  610. && (pOD->optEquivIndex != pOD->optIndex))
  611. continue;
  612. /*
  613. * The option argument data are found at the equivalenced-to option,
  614. * but the actual option argument type comes from the original
  615. * option descriptor. Be careful!
  616. */
  617. p = ((pOD->fOptState & OPTST_EQUIVALENCE) != 0)
  618. ? (pOpts->pOptDesc + pOD->optActualIndex) : pOD;
  619. switch (OPTST_GET_ARGTYPE(pOD->fOptState)) {
  620. case OPARG_TYPE_NONE:
  621. printNoArgOpt(fp, p, pOD);
  622. break;
  623. case OPARG_TYPE_NUMERIC:
  624. printEntry( fp, p, (void*)(p->optArg.argInt));
  625. break;
  626. case OPARG_TYPE_STRING:
  627. printStringArg(fp, p);
  628. break;
  629. case OPARG_TYPE_ENUMERATION:
  630. printEnumArg(fp, p);
  631. break;
  632. case OPARG_TYPE_MEMBERSHIP:
  633. printSetMemberArg(fp, p);
  634. break;
  635. case OPARG_TYPE_BOOLEAN:
  636. printEntry( fp, p, p->optArg.argBool ? "true" : "false" );
  637. break;
  638. case OPARG_TYPE_HIERARCHY:
  639. printHierarchy(fp, p);
  640. break;
  641. case OPARG_TYPE_FILE:
  642. printFileArg(fp, p, pOpts);
  643. break;
  644. default:
  645. break; /* cannot handle - skip it */
  646. }
  647. } while ( (pOD++), (--ct > 0));
  648. fclose( fp );
  649. }
  650. /*
  651. * Local Variables:
  652. * mode: C
  653. * c-file-style: "stroustrup"
  654. * indent-tabs-mode: nil
  655. * End:
  656. * end of autoopts/save.c */