1
0

save.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. /*
  2. * save.c $Id: save.c,v 4.20 2007/07/04 21:36:38 bkorb Exp $
  3. * Time-stamp: "2007-07-04 11:34: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. * This file is part of AutoOpts, a companion to AutoGen.
  10. * AutoOpts is free software.
  11. * AutoOpts is copyright (c) 1992-2007 by Bruce Korb - all rights reserved
  12. *
  13. * AutoOpts is available under any one of two licenses. The license
  14. * in use must be one of these two and the choice is under the control
  15. * of the user of the license.
  16. *
  17. * The GNU Lesser General Public License, version 3 or later
  18. * See the files "COPYING.lgplv3" and "COPYING.gplv3"
  19. *
  20. * The Modified Berkeley Software Distribution License
  21. * See the file "COPYING.mbsd"
  22. *
  23. * These files have the following md5sums:
  24. *
  25. * 239588c55c22c60ffe159946a760a33e pkg/libopts/COPYING.gplv3
  26. * fa82ca978890795162346e661b47161a pkg/libopts/COPYING.lgplv3
  27. * 66a5cedaf62c4b2637025f049f9b826f pkg/libopts/COPYING.mbsd
  28. */
  29. tSCC zWarn[] = "%s WARNING: cannot save options - ";
  30. /* = = = START-STATIC-FORWARD = = = */
  31. /* static forward declarations maintained by :mkfwd */
  32. static tCC*
  33. findDirName( tOptions* pOpts, int* p_free );
  34. static tCC*
  35. findFileName( tOptions* pOpts, int* p_free_name );
  36. static void
  37. printEntry(
  38. FILE * fp,
  39. tOptDesc * p,
  40. tCC* pzLA );
  41. /* = = = END-STATIC-FORWARD = = = */
  42. static tCC*
  43. findDirName( tOptions* pOpts, int* p_free )
  44. {
  45. tCC* pzDir;
  46. if (pOpts->specOptIdx.save_opts == 0)
  47. return NULL;
  48. pzDir = pOpts->pOptDesc[ pOpts->specOptIdx.save_opts ].optArg.argString;
  49. if ((pzDir != NULL) && (*pzDir != NUL))
  50. return pzDir;
  51. /*
  52. * This function only works if there is a directory where
  53. * we can stash the RC (INI) file.
  54. */
  55. {
  56. tCC* const* papz = pOpts->papzHomeList;
  57. if (papz == NULL)
  58. return NULL;
  59. while (papz[1] != NULL) papz++;
  60. pzDir = *papz;
  61. }
  62. /*
  63. * IF it does not require deciphering an env value, then just copy it
  64. */
  65. if (*pzDir != '$')
  66. return pzDir;
  67. {
  68. tCC* pzEndDir = strchr( ++pzDir, DIRCH );
  69. char* pzFileName;
  70. char* pzEnv;
  71. if (pzEndDir != NULL) {
  72. char z[ AO_NAME_SIZE ];
  73. if ((pzEndDir - pzDir) > AO_NAME_LIMIT )
  74. return NULL;
  75. strncpy( z, pzDir, (size_t)(pzEndDir - pzDir) );
  76. z[ (pzEndDir - pzDir) ] = NUL;
  77. pzEnv = getenv( z );
  78. } else {
  79. /*
  80. * Make sure we can get the env value (after stripping off
  81. * any trailing directory or file names)
  82. */
  83. pzEnv = getenv( pzDir );
  84. }
  85. if (pzEnv == NULL) {
  86. fprintf( stderr, zWarn, pOpts->pzProgName );
  87. fprintf( stderr, zNotDef, pzDir );
  88. return NULL;
  89. }
  90. if (pzEndDir == NULL)
  91. return pzEnv;
  92. {
  93. size_t sz = strlen( pzEnv ) + strlen( pzEndDir ) + 2;
  94. pzFileName = (char*)AGALOC( sz, "dir name" );
  95. }
  96. if (pzFileName == NULL)
  97. return NULL;
  98. *p_free = 1;
  99. /*
  100. * Glue together the full name into the allocated memory.
  101. * FIXME: We lose track of this memory.
  102. */
  103. sprintf( pzFileName, "%s/%s", pzEnv, pzEndDir );
  104. return pzFileName;
  105. }
  106. }
  107. static tCC*
  108. findFileName( tOptions* pOpts, int* p_free_name )
  109. {
  110. tCC* pzDir;
  111. struct stat stBuf;
  112. int free_dir_name = 0;
  113. pzDir = findDirName( pOpts, &free_dir_name );
  114. if (pzDir == NULL)
  115. return NULL;
  116. /*
  117. * See if we can find the specified directory. We use a once-only loop
  118. * structure so we can bail out early.
  119. */
  120. if (stat( pzDir, &stBuf ) != 0) do {
  121. /*
  122. * IF we could not, check to see if we got a full
  123. * path to a file name that has not been created yet.
  124. */
  125. if (errno == ENOENT) {
  126. char z[AG_PATH_MAX];
  127. /*
  128. * Strip off the last component, stat the remaining string and
  129. * that string must name a directory
  130. */
  131. char* pzDirCh = strrchr( pzDir, DIRCH );
  132. if (pzDirCh == NULL) {
  133. stBuf.st_mode = S_IFREG;
  134. continue; /* bail out of error condition */
  135. }
  136. strncpy( z, pzDir, (size_t)(pzDirCh - pzDir));
  137. z[ pzDirCh - pzDir ] = NUL;
  138. if ( (stat( z, &stBuf ) == 0)
  139. && S_ISDIR( stBuf.st_mode )) {
  140. /*
  141. * We found the directory. Restore the file name and
  142. * mark the full name as a regular file
  143. */
  144. stBuf.st_mode = S_IFREG;
  145. continue; /* bail out of error condition */
  146. }
  147. }
  148. /*
  149. * We got a bogus name.
  150. */
  151. fprintf( stderr, zWarn, pOpts->pzProgName );
  152. fprintf( stderr, zNoStat, errno, strerror( errno ), pzDir );
  153. if (free_dir_name)
  154. AGFREE( (void*)pzDir );
  155. return NULL;
  156. } while (0);
  157. /*
  158. * IF what we found was a directory,
  159. * THEN tack on the config file name
  160. */
  161. if (S_ISDIR( stBuf.st_mode )) {
  162. size_t sz = strlen( pzDir ) + strlen( pOpts->pzRcName ) + 2;
  163. {
  164. char* pzPath = (char*)AGALOC( sz, "file name" );
  165. #ifdef HAVE_SNPRINTF
  166. snprintf( pzPath, sz, "%s/%s", pzDir, pOpts->pzRcName );
  167. #else
  168. sprintf( pzPath, "%s/%s", pzDir, pOpts->pzRcName );
  169. #endif
  170. if (free_dir_name)
  171. AGFREE( (void*)pzDir );
  172. pzDir = pzPath;
  173. free_dir_name = 1;
  174. }
  175. /*
  176. * IF we cannot stat the object for any reason other than
  177. * it does not exist, then we bail out
  178. */
  179. if (stat( pzDir, &stBuf ) != 0) {
  180. if (errno != ENOENT) {
  181. fprintf( stderr, zWarn, pOpts->pzProgName );
  182. fprintf( stderr, zNoStat, errno, strerror( errno ),
  183. pzDir );
  184. AGFREE( (void*)pzDir );
  185. return NULL;
  186. }
  187. /*
  188. * It does not exist yet, but it will be a regular file
  189. */
  190. stBuf.st_mode = S_IFREG;
  191. }
  192. }
  193. /*
  194. * Make sure that whatever we ultimately found, that it either is
  195. * or will soon be a file.
  196. */
  197. if (! S_ISREG( stBuf.st_mode )) {
  198. fprintf( stderr, zWarn, pOpts->pzProgName );
  199. fprintf( stderr, zNotFile, pzDir );
  200. if (free_dir_name)
  201. AGFREE( (void*)pzDir );
  202. return NULL;
  203. }
  204. /*
  205. * Get rid of the old file
  206. */
  207. unlink( pzDir );
  208. *p_free_name = free_dir_name;
  209. return pzDir;
  210. }
  211. static void
  212. printEntry(
  213. FILE * fp,
  214. tOptDesc * p,
  215. tCC* pzLA )
  216. {
  217. /*
  218. * There is an argument. Pad the name so values line up.
  219. * Not disabled *OR* this got equivalenced to another opt,
  220. * then use current option name.
  221. * Otherwise, there must be a disablement name.
  222. */
  223. {
  224. char const * pz;
  225. if (! DISABLED_OPT(p) || (p->optEquivIndex != NO_EQUIVALENT))
  226. pz = p->pz_Name;
  227. else
  228. pz = p->pz_DisableName;
  229. fprintf(fp, "%-18s", pz);
  230. }
  231. /*
  232. * IF the option is numeric only,
  233. * THEN the char pointer is really the number
  234. */
  235. if (OPTST_GET_ARGTYPE(p->fOptState) == OPARG_TYPE_NUMERIC)
  236. fprintf( fp, " %d\n", (int)(t_word)pzLA );
  237. /*
  238. * OTHERWISE, FOR each line of the value text, ...
  239. */
  240. else if (pzLA == NULL)
  241. fputc( '\n', fp );
  242. else {
  243. fputc( ' ', fp ); fputc( ' ', fp );
  244. for (;;) {
  245. tCC* pzNl = strchr( pzLA, '\n' );
  246. /*
  247. * IF this is the last line
  248. * THEN bail and print it
  249. */
  250. if (pzNl == NULL)
  251. break;
  252. /*
  253. * Print the continuation and the text from the current line
  254. */
  255. (void)fwrite( pzLA, (size_t)(pzNl - pzLA), (size_t)1, fp );
  256. pzLA = pzNl+1; /* advance the Last Arg pointer */
  257. fputs( "\\\n", fp );
  258. }
  259. /*
  260. * Terminate the entry
  261. */
  262. fputs( pzLA, fp );
  263. fputc( '\n', fp );
  264. }
  265. }
  266. /*=export_func optionSaveFile
  267. *
  268. * what: saves the option state to a file
  269. *
  270. * arg: tOptions*, pOpts, program options descriptor
  271. *
  272. * doc:
  273. *
  274. * This routine will save the state of option processing to a file. The name
  275. * of that file can be specified with the argument to the @code{--save-opts}
  276. * option, or by appending the @code{rcfile} attribute to the last
  277. * @code{homerc} attribute. If no @code{rcfile} attribute was specified, it
  278. * will default to @code{.@i{programname}rc}. If you wish to specify another
  279. * file, you should invoke the @code{SET_OPT_SAVE_OPTS( @i{filename} )} macro.
  280. *
  281. * err:
  282. *
  283. * If no @code{homerc} file was specified, this routine will silently return
  284. * and do nothing. If the output file cannot be created or updated, a message
  285. * will be printed to @code{stderr} and the routine will return.
  286. =*/
  287. void
  288. optionSaveFile( tOptions* pOpts )
  289. {
  290. tOptDesc* pOD;
  291. int ct;
  292. FILE* fp;
  293. {
  294. int free_name = 0;
  295. tCC* pzFName = findFileName( pOpts, &free_name );
  296. if (pzFName == NULL)
  297. return;
  298. fp = fopen( pzFName, "w" FOPEN_BINARY_FLAG );
  299. if (fp == NULL) {
  300. fprintf( stderr, zWarn, pOpts->pzProgName );
  301. fprintf( stderr, zNoCreat, errno, strerror( errno ), pzFName );
  302. if (free_name)
  303. AGFREE((void*) pzFName );
  304. return;
  305. }
  306. if (free_name)
  307. AGFREE( (void*)pzFName );
  308. }
  309. {
  310. char const* pz = pOpts->pzUsageTitle;
  311. fputs( "# ", fp );
  312. do { fputc( *pz, fp ); } while (*(pz++) != '\n');
  313. }
  314. {
  315. time_t timeVal = time( NULL );
  316. char* pzTime = ctime( &timeVal );
  317. fprintf( fp, zPresetFile, pzTime );
  318. #ifdef HAVE_ALLOCATED_CTIME
  319. /*
  320. * The return values for ctime(), localtime(), and gmtime()
  321. * normally point to static data that is overwritten by each call.
  322. * The test to detect allocated ctime, so we leak the memory.
  323. */
  324. AGFREE( (void*)pzTime );
  325. #endif
  326. }
  327. /*
  328. * FOR each of the defined options, ...
  329. */
  330. ct = pOpts->presetOptCt;
  331. pOD = pOpts->pOptDesc;
  332. do {
  333. int arg_state;
  334. tOptDesc* p;
  335. /*
  336. * IF the option has not been defined
  337. * OR it does not take an initialization value
  338. * OR it is equivalenced to another option
  339. * THEN continue (ignore it)
  340. */
  341. if (UNUSED_OPT( pOD ))
  342. continue;
  343. if ((pOD->fOptState & (OPTST_NO_INIT|OPTST_DOCUMENT|OPTST_OMITTED))
  344. != 0)
  345. continue;
  346. if ( (pOD->optEquivIndex != NO_EQUIVALENT)
  347. && (pOD->optEquivIndex != pOD->optIndex))
  348. continue;
  349. /*
  350. * Set a temporary pointer to the real option description
  351. * (i.e. account for equivalencing)
  352. */
  353. p = ((pOD->fOptState & OPTST_EQUIVALENCE) != 0)
  354. ? (pOpts->pOptDesc + pOD->optActualIndex) : pOD;
  355. /*
  356. * IF no arguments are allowed
  357. * THEN just print the name and continue
  358. */
  359. if (OPTST_GET_ARGTYPE(pOD->fOptState) == OPARG_TYPE_NONE) {
  360. fprintf( fp, "%s\n",
  361. (DISABLED_OPT( p )) ? p->pz_DisableName : p->pz_Name );
  362. continue;
  363. }
  364. arg_state = OPTST_GET_ARGTYPE(p->fOptState);
  365. switch (arg_state) {
  366. case 0:
  367. case OPARG_TYPE_NUMERIC:
  368. printEntry( fp, p, (void*)(p->optArg.argInt));
  369. break;
  370. case OPARG_TYPE_STRING:
  371. if (p->fOptState & OPTST_STACKED) {
  372. tArgList* pAL = (tArgList*)p->optCookie;
  373. int uct = pAL->useCt;
  374. tCC** ppz = pAL->apzArgs;
  375. /*
  376. * Disallow multiple copies of disabled options.
  377. */
  378. if (uct > 1)
  379. p->fOptState &= ~OPTST_DISABLED;
  380. while (uct-- > 0)
  381. printEntry( fp, p, *(ppz++) );
  382. } else {
  383. printEntry( fp, p, p->optArg.argString );
  384. }
  385. break;
  386. case OPARG_TYPE_ENUMERATION:
  387. case OPARG_TYPE_MEMBERSHIP:
  388. {
  389. uintptr_t val = p->optArg.argEnum;
  390. /*
  391. * This is a magic incantation that will convert the
  392. * bit flag values back into a string suitable for printing.
  393. */
  394. (*(p->pOptProc))( (tOptions*)2UL, p );
  395. printEntry( fp, p, (void*)(p->optArg.argString));
  396. if ( (p->optArg.argString != NULL)
  397. && (arg_state != OPARG_TYPE_ENUMERATION)) {
  398. /*
  399. * set membership strings get allocated
  400. */
  401. AGFREE( (void*)p->optArg.argString );
  402. p->fOptState &= ~OPTST_ALLOC_ARG;
  403. }
  404. p->optArg.argEnum = val;
  405. break;
  406. }
  407. case OPARG_TYPE_BOOLEAN:
  408. printEntry( fp, p, p->optArg.argBool ? "true" : "false" );
  409. break;
  410. default:
  411. break; /* cannot handle - skip it */
  412. }
  413. } while ( (pOD++), (--ct > 0));
  414. fclose( fp );
  415. }
  416. /*
  417. * Local Variables:
  418. * mode: C
  419. * c-file-style: "stroustrup"
  420. * indent-tabs-mode: nil
  421. * End:
  422. * end of autoopts/save.c */