1
0

save.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. /*
  2. * save.c $Id: save.c,v 4.10 2005/03/13 19:51:59 bkorb Exp $
  3. * Time-stamp: "2005-02-20 13:49:46 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. /*
  10. * Automated Options copyright 1992-2005 Bruce Korb
  11. *
  12. * Automated Options is free software.
  13. * You may redistribute it and/or modify it under the terms of the
  14. * GNU General Public License, as published by the Free Software
  15. * Foundation; either version 2, or (at your option) any later version.
  16. *
  17. * Automated Options is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with Automated Options. See the file "COPYING". If not,
  24. * write to: The Free Software Foundation, Inc.,
  25. * 59 Temple Place - Suite 330,
  26. * Boston, MA 02111-1307, USA.
  27. *
  28. * As a special exception, Bruce Korb gives permission for additional
  29. * uses of the text contained in his release of AutoOpts.
  30. *
  31. * The exception is that, if you link the AutoOpts library with other
  32. * files to produce an executable, this does not by itself cause the
  33. * resulting executable to be covered by the GNU General Public License.
  34. * Your use of that executable is in no way restricted on account of
  35. * linking the AutoOpts library code into it.
  36. *
  37. * This exception does not however invalidate any other reasons why
  38. * the executable file might be covered by the GNU General Public License.
  39. *
  40. * This exception applies only to the code released by Bruce Korb under
  41. * the name AutoOpts. If you copy code from other sources under the
  42. * General Public License into a copy of AutoOpts, as the General Public
  43. * License permits, the exception does not apply to the code that you add
  44. * in this way. To avoid misleading anyone as to the status of such
  45. * modified files, you must delete this exception notice from them.
  46. *
  47. * If you write modifications of your own for AutoOpts, it is your choice
  48. * whether to permit this exception to apply to your modifications.
  49. * If you do not wish that, delete this exception notice.
  50. */
  51. tSCC zWarn[] = "%s WARNING: cannot save options - ";
  52. /* = = = START-STATIC-FORWARD = = = */
  53. /* static forward declarations maintained by :mkfwd */
  54. static tCC*
  55. findDirName( tOptions* pOpts, int* p_free );
  56. static tCC*
  57. findFileName( tOptions* pOpts, int* p_free_name );
  58. static void
  59. printEntry(
  60. FILE* fp,
  61. tOptDesc* p,
  62. tCC* pzLA );
  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 ].pzLastArg;
  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, '/' );
  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, (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[MAXPATHLEN];
  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, '/' );
  154. if (pzDirCh == NULL) {
  155. stBuf.st_mode = S_IFREG;
  156. continue; /* bail out of error condition */
  157. }
  158. strncpy( z, pzDir, 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. */
  242. fprintf( fp, "%-18s",
  243. (DISABLED_OPT( p )) ? p->pz_DisableName : p->pz_Name );
  244. /*
  245. * IF the option is numeric only,
  246. * THEN the char pointer is really the number
  247. */
  248. if (OPTST_GET_ARGTYPE(p->fOptState) == OPARG_TYPE_NUMERIC)
  249. fprintf( fp, " %d\n", (t_word)pzLA );
  250. /*
  251. * OTHERWISE, FOR each line of the value text, ...
  252. */
  253. else if (pzLA == NULL)
  254. fputc( '\n', fp );
  255. else {
  256. fputc( ' ', fp ); fputc( ' ', fp );
  257. for (;;) {
  258. tCC* pzNl = strchr( pzLA, '\n' );
  259. /*
  260. * IF this is the last line
  261. * THEN bail and print it
  262. */
  263. if (pzNl == NULL)
  264. break;
  265. /*
  266. * Print the continuation and the text from the current line
  267. */
  268. fwrite( pzLA, pzNl - pzLA, 1, fp );
  269. pzLA = pzNl+1; /* advance the Last Arg pointer */
  270. fputs( "\\\n", fp );
  271. }
  272. /*
  273. * Terminate the entry
  274. */
  275. fputs( pzLA, fp );
  276. fputc( '\n', fp );
  277. }
  278. }
  279. /*=export_func optionSaveFile
  280. *
  281. * what: saves the option state to a file
  282. *
  283. * arg: tOptions*, pOpts, program options descriptor
  284. *
  285. * doc:
  286. *
  287. * This routine will save the state of option processing to a file. The name
  288. * of that file can be specified with the argument to the @code{--save-opts}
  289. * option, or by appending the @code{rcfile} attribute to the last
  290. * @code{homerc} attribute. If no @code{rcfile} attribute was specified, it
  291. * will default to @code{.@i{programname}rc}. If you wish to specify another
  292. * file, you should invoke the @code{SET_OPT_SAVE_OPTS( @i{filename} )} macro.
  293. *
  294. * err:
  295. *
  296. * If no @code{homerc} file was specified, this routine will silently return
  297. * and do nothing. If the output file cannot be created or updated, a message
  298. * will be printed to @code{stderr} and the routine will return.
  299. =*/
  300. void
  301. optionSaveFile( tOptions* pOpts )
  302. {
  303. tOptDesc* pOD;
  304. int ct;
  305. FILE* fp;
  306. {
  307. int free_name = 0;
  308. tCC* pzFName = findFileName( pOpts, &free_name );
  309. if (pzFName == NULL)
  310. return;
  311. fp = fopen( pzFName, "w" FOPEN_BINARY_FLAG );
  312. if (fp == NULL) {
  313. fprintf( stderr, zWarn, pOpts->pzProgName );
  314. fprintf( stderr, zNoCreat, errno, strerror( errno ), pzFName );
  315. if (free_name)
  316. AGFREE((void*) pzFName );
  317. return;
  318. }
  319. if (free_name)
  320. AGFREE( (void*)pzFName );
  321. }
  322. {
  323. const char* pz = pOpts->pzUsageTitle;
  324. fputs( "# ", fp );
  325. do { fputc( *pz, fp ); } while (*(pz++) != '\n');
  326. }
  327. {
  328. time_t timeVal = time( NULL );
  329. char* pzTime = ctime( &timeVal );
  330. fprintf( fp, zPresetFile, pzTime );
  331. #ifdef HAVE_ALLOCATED_CTIME
  332. /*
  333. * The return values for ctime(), localtime(), and gmtime()
  334. * normally point to static data that is overwritten by each call.
  335. * The test to detect allocated ctime, so we leak the memory.
  336. */
  337. AGFREE( (void*)pzTime );
  338. #endif
  339. }
  340. /*
  341. * FOR each of the defined options, ...
  342. */
  343. ct = pOpts->presetOptCt;
  344. pOD = pOpts->pOptDesc;
  345. do {
  346. int arg_state;
  347. tOptDesc* p;
  348. /*
  349. * IF the option has not been defined
  350. * OR it does not take an initialization value
  351. * OR it is equivalenced to another option
  352. * THEN continue (ignore it)
  353. */
  354. if (UNUSED_OPT( pOD ))
  355. continue;
  356. if ((pOD->fOptState & (OPTST_NO_INIT|OPTST_DOCUMENT|OPTST_OMITTED))
  357. != 0)
  358. continue;
  359. if ( (pOD->optEquivIndex != NO_EQUIVALENT)
  360. && (pOD->optEquivIndex != pOD->optIndex))
  361. continue;
  362. /*
  363. * Set a temporary pointer to the real option description
  364. * (i.e. account for equivalencing)
  365. */
  366. p = ((pOD->fOptState & OPTST_EQUIVALENCE) != 0)
  367. ? (pOpts->pOptDesc + pOD->optActualIndex) : pOD;
  368. /*
  369. * IF no arguments are allowed
  370. * THEN just print the name and continue
  371. */
  372. if (OPTST_GET_ARGTYPE(pOD->fOptState) == OPARG_TYPE_NONE) {
  373. fprintf( fp, "%s\n",
  374. (DISABLED_OPT( p )) ? p->pz_DisableName : p->pz_Name );
  375. continue;
  376. }
  377. arg_state = OPTST_GET_ARGTYPE(p->fOptState);
  378. switch (arg_state) {
  379. case 0:
  380. case OPARG_TYPE_NUMERIC:
  381. printEntry( fp, p, p->pzLastArg );
  382. break;
  383. case OPARG_TYPE_STRING:
  384. if (p->fOptState & OPTST_STACKED) {
  385. tArgList* pAL = (tArgList*)p->optCookie;
  386. int uct = pAL->useCt;
  387. tCC** ppz = pAL->apzArgs;
  388. /*
  389. * Disallow multiple copies of disabled options.
  390. */
  391. if (uct > 1)
  392. p->fOptState &= ~OPTST_DISABLED;
  393. while (uct-- > 0)
  394. printEntry( fp, p, *(ppz++) );
  395. } else {
  396. printEntry( fp, p, p->pzLastArg );
  397. }
  398. break;
  399. case OPARG_TYPE_ENUMERATION:
  400. case OPARG_TYPE_MEMBERSHIP:
  401. {
  402. tCC* val = p->pzLastArg;
  403. /*
  404. * This is a magic incantation that will convert the
  405. * bit flag values back into a string suitable for printing.
  406. */
  407. (*(p->pOptProc))( (tOptions*)2UL, p );
  408. printEntry( fp, p, p->pzLastArg );
  409. if ((p->pzLastArg != NULL) && (arg_state != OPARG_TYPE_ENUMERATION))
  410. /*
  411. * bit flag and enumeration strings get allocated
  412. */
  413. AGFREE( (void*)p->pzLastArg );
  414. p->pzLastArg = val;
  415. break;
  416. }
  417. case OPARG_TYPE_BOOLEAN:
  418. printEntry( fp, p, (p->pzLastArg != 0) ? "true" : "false" );
  419. break;
  420. default:
  421. break; /* cannot handle - skip it */
  422. }
  423. } while ( (pOD++), (--ct > 0));
  424. fclose( fp );
  425. }
  426. /*
  427. * Local Variables:
  428. * mode: C
  429. * c-file-style: "stroustrup"
  430. * tab-width: 4
  431. * indent-tabs-mode: nil
  432. * End:
  433. * end of autoopts/save.c */