save.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. /*
  2. * save.c $Id: save.c,v 4.18 2007/04/15 19:01:18 bkorb Exp $
  3. * Time-stamp: "2007-04-15 11:11:10 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-2007 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. * 51 Franklin Street, Fifth Floor,
  26. * Boston, MA 02110-1301, 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 ].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. /*=export_func optionSaveFile
  289. *
  290. * what: saves the option state to a file
  291. *
  292. * arg: tOptions*, pOpts, program options descriptor
  293. *
  294. * doc:
  295. *
  296. * This routine will save the state of option processing to a file. The name
  297. * of that file can be specified with the argument to the @code{--save-opts}
  298. * option, or by appending the @code{rcfile} attribute to the last
  299. * @code{homerc} attribute. If no @code{rcfile} attribute was specified, it
  300. * will default to @code{.@i{programname}rc}. If you wish to specify another
  301. * file, you should invoke the @code{SET_OPT_SAVE_OPTS( @i{filename} )} macro.
  302. *
  303. * err:
  304. *
  305. * If no @code{homerc} file was specified, this routine will silently return
  306. * and do nothing. If the output file cannot be created or updated, a message
  307. * will be printed to @code{stderr} and the routine will return.
  308. =*/
  309. void
  310. optionSaveFile( tOptions* pOpts )
  311. {
  312. tOptDesc* pOD;
  313. int ct;
  314. FILE* fp;
  315. {
  316. int free_name = 0;
  317. tCC* pzFName = findFileName( pOpts, &free_name );
  318. if (pzFName == NULL)
  319. return;
  320. fp = fopen( pzFName, "w" FOPEN_BINARY_FLAG );
  321. if (fp == NULL) {
  322. fprintf( stderr, zWarn, pOpts->pzProgName );
  323. fprintf( stderr, zNoCreat, errno, strerror( errno ), pzFName );
  324. if (free_name)
  325. AGFREE((void*) pzFName );
  326. return;
  327. }
  328. if (free_name)
  329. AGFREE( (void*)pzFName );
  330. }
  331. {
  332. char const* pz = pOpts->pzUsageTitle;
  333. fputs( "# ", fp );
  334. do { fputc( *pz, fp ); } while (*(pz++) != '\n');
  335. }
  336. {
  337. time_t timeVal = time( NULL );
  338. char* pzTime = ctime( &timeVal );
  339. fprintf( fp, zPresetFile, pzTime );
  340. #ifdef HAVE_ALLOCATED_CTIME
  341. /*
  342. * The return values for ctime(), localtime(), and gmtime()
  343. * normally point to static data that is overwritten by each call.
  344. * The test to detect allocated ctime, so we leak the memory.
  345. */
  346. AGFREE( (void*)pzTime );
  347. #endif
  348. }
  349. /*
  350. * FOR each of the defined options, ...
  351. */
  352. ct = pOpts->presetOptCt;
  353. pOD = pOpts->pOptDesc;
  354. do {
  355. int arg_state;
  356. tOptDesc* p;
  357. /*
  358. * IF the option has not been defined
  359. * OR it does not take an initialization value
  360. * OR it is equivalenced to another option
  361. * THEN continue (ignore it)
  362. */
  363. if (UNUSED_OPT( pOD ))
  364. continue;
  365. if ((pOD->fOptState & (OPTST_NO_INIT|OPTST_DOCUMENT|OPTST_OMITTED))
  366. != 0)
  367. continue;
  368. if ( (pOD->optEquivIndex != NO_EQUIVALENT)
  369. && (pOD->optEquivIndex != pOD->optIndex))
  370. continue;
  371. /*
  372. * Set a temporary pointer to the real option description
  373. * (i.e. account for equivalencing)
  374. */
  375. p = ((pOD->fOptState & OPTST_EQUIVALENCE) != 0)
  376. ? (pOpts->pOptDesc + pOD->optActualIndex) : pOD;
  377. /*
  378. * IF no arguments are allowed
  379. * THEN just print the name and continue
  380. */
  381. if (OPTST_GET_ARGTYPE(pOD->fOptState) == OPARG_TYPE_NONE) {
  382. fprintf( fp, "%s\n",
  383. (DISABLED_OPT( p )) ? p->pz_DisableName : p->pz_Name );
  384. continue;
  385. }
  386. arg_state = OPTST_GET_ARGTYPE(p->fOptState);
  387. switch (arg_state) {
  388. case 0:
  389. case OPARG_TYPE_NUMERIC:
  390. printEntry( fp, p, (void*)(p->optArg.argInt));
  391. break;
  392. case OPARG_TYPE_STRING:
  393. if (p->fOptState & OPTST_STACKED) {
  394. tArgList* pAL = (tArgList*)p->optCookie;
  395. int uct = pAL->useCt;
  396. tCC** ppz = pAL->apzArgs;
  397. /*
  398. * Disallow multiple copies of disabled options.
  399. */
  400. if (uct > 1)
  401. p->fOptState &= ~OPTST_DISABLED;
  402. while (uct-- > 0)
  403. printEntry( fp, p, *(ppz++) );
  404. } else {
  405. printEntry( fp, p, p->optArg.argString );
  406. }
  407. break;
  408. case OPARG_TYPE_ENUMERATION:
  409. case OPARG_TYPE_MEMBERSHIP:
  410. {
  411. uintptr_t val = p->optArg.argEnum;
  412. /*
  413. * This is a magic incantation that will convert the
  414. * bit flag values back into a string suitable for printing.
  415. */
  416. (*(p->pOptProc))( (tOptions*)2UL, p );
  417. printEntry( fp, p, (void*)(p->optArg.argString));
  418. if ( (p->optArg.argString != NULL)
  419. && (arg_state != OPARG_TYPE_ENUMERATION)) {
  420. /*
  421. * set membership strings get allocated
  422. */
  423. AGFREE( (void*)p->optArg.argString );
  424. p->fOptState &= ~OPTST_ALLOC_ARG;
  425. }
  426. p->optArg.argEnum = val;
  427. break;
  428. }
  429. case OPARG_TYPE_BOOLEAN:
  430. printEntry( fp, p, p->optArg.argBool ? "true" : "false" );
  431. break;
  432. default:
  433. break; /* cannot handle - skip it */
  434. }
  435. } while ( (pOD++), (--ct > 0));
  436. fclose( fp );
  437. }
  438. /*
  439. * Local Variables:
  440. * mode: C
  441. * c-file-style: "stroustrup"
  442. * indent-tabs-mode: nil
  443. * End:
  444. * end of autoopts/save.c */