1
0

restore.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * restore.c $Id: restore.c,v 4.14 2009/08/01 17:43:06 bkorb Exp $
  3. * Time-stamp: "2007-07-04 11:34:40 bkorb"
  4. *
  5. * This module's routines will save the current option state to memory
  6. * and restore it. If saved prior to the initial optionProcess call,
  7. * then the initial state will be restored.
  8. *
  9. * This file is part of AutoOpts, a companion to AutoGen.
  10. * AutoOpts is free software.
  11. * AutoOpts is copyright (c) 1992-2009 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. * 43b91e8ca915626ed3818ffb1b71248b pkg/libopts/COPYING.gplv3
  26. * 06a1a2e4760c90ea5e1dad8dfaac4d39 pkg/libopts/COPYING.lgplv3
  27. * 66a5cedaf62c4b2637025f049f9b826f pkg/libopts/COPYING.mbsd
  28. */
  29. /*
  30. * optionFixupSavedOpts Really, it just wipes out option state for
  31. * options that are troublesome to copy. viz., stacked strings and
  32. * hierarcicaly valued option args. We do duplicate string args that
  33. * have been marked as allocated though.
  34. */
  35. static void
  36. fixupSavedOptionArgs(tOptions* pOpts)
  37. {
  38. tOptions* p = pOpts->pSavedState;
  39. tOptDesc* pOD = pOpts->pOptDesc;
  40. int ct = pOpts->optCt;
  41. /*
  42. * Make sure that allocated stuff is only referenced in the
  43. * archived copy of the data.
  44. */
  45. for (; ct-- > 0; pOD++) {
  46. switch (OPTST_GET_ARGTYPE(pOD->fOptState)) {
  47. case OPARG_TYPE_STRING:
  48. if (pOD->fOptState & OPTST_STACKED) {
  49. tOptDesc* q = p->pOptDesc + (pOD - pOpts->pOptDesc);
  50. q->optCookie = NULL;
  51. }
  52. if (pOD->fOptState & OPTST_ALLOC_ARG) {
  53. tOptDesc* q = p->pOptDesc + (pOD - pOpts->pOptDesc);
  54. AGDUPSTR(q->optArg.argString, pOD->optArg.argString, "arg");
  55. }
  56. break;
  57. case OPARG_TYPE_HIERARCHY:
  58. {
  59. tOptDesc* q = p->pOptDesc + (pOD - pOpts->pOptDesc);
  60. q->optCookie = NULL;
  61. }
  62. }
  63. }
  64. }
  65. /*=export_func optionSaveState
  66. *
  67. * what: saves the option state to memory
  68. * arg: tOptions*, pOpts, program options descriptor
  69. *
  70. * doc:
  71. *
  72. * This routine will allocate enough memory to save the current option
  73. * processing state. If this routine has been called before, that memory
  74. * will be reused. You may only save one copy of the option state. This
  75. * routine may be called before optionProcess(3AO). If you do call it
  76. * before the first call to optionProcess, then you may also change the
  77. * contents of argc/argv after you call optionRestore(3AO)
  78. *
  79. * In fact, more strongly put: it is safest to only use this function
  80. * before having processed any options. In particular, the saving and
  81. * restoring of stacked string arguments and hierarchical values is
  82. * disabled. The values are not saved.
  83. *
  84. * err: If it fails to allocate the memory,
  85. * it will print a message to stderr and exit.
  86. * Otherwise, it will always succeed.
  87. =*/
  88. void
  89. optionSaveState(tOptions* pOpts)
  90. {
  91. tOptions* p = (tOptions*)pOpts->pSavedState;
  92. if (p == NULL) {
  93. size_t sz = sizeof( *pOpts ) + (pOpts->optCt * sizeof( tOptDesc ));
  94. p = AGALOC( sz, "saved option state" );
  95. if (p == NULL) {
  96. tCC* pzName = pOpts->pzProgName;
  97. if (pzName == NULL) {
  98. pzName = pOpts->pzPROGNAME;
  99. if (pzName == NULL)
  100. pzName = zNil;
  101. }
  102. fprintf( stderr, zCantSave, pzName, sz );
  103. exit( EXIT_FAILURE );
  104. }
  105. pOpts->pSavedState = p;
  106. }
  107. memcpy( p, pOpts, sizeof( *p ));
  108. memcpy( p + 1, pOpts->pOptDesc, p->optCt * sizeof( tOptDesc ));
  109. fixupSavedOptionArgs(pOpts);
  110. }
  111. /*=export_func optionRestore
  112. *
  113. * what: restore option state from memory copy
  114. * arg: tOptions*, pOpts, program options descriptor
  115. *
  116. * doc: Copy back the option state from saved memory.
  117. * The allocated memory is left intact, so this routine can be
  118. * called repeatedly without having to call optionSaveState again.
  119. * If you are restoring a state that was saved before the first call
  120. * to optionProcess(3AO), then you may change the contents of the
  121. * argc/argv parameters to optionProcess.
  122. *
  123. * err: If you have not called @code{optionSaveState} before, a diagnostic is
  124. * printed to @code{stderr} and exit is called.
  125. =*/
  126. void
  127. optionRestore( tOptions* pOpts )
  128. {
  129. tOptions* p = (tOptions*)pOpts->pSavedState;
  130. if (p == NULL) {
  131. tCC* pzName = pOpts->pzProgName;
  132. if (pzName == NULL) {
  133. pzName = pOpts->pzPROGNAME;
  134. if (pzName == NULL)
  135. pzName = zNil;
  136. }
  137. fprintf( stderr, zNoState, pzName );
  138. exit( EXIT_FAILURE );
  139. }
  140. pOpts->pSavedState = NULL;
  141. optionFree(pOpts);
  142. memcpy( pOpts, p, sizeof( *p ));
  143. memcpy( pOpts->pOptDesc, p+1, p->optCt * sizeof( tOptDesc ));
  144. pOpts->pSavedState = p;
  145. fixupSavedOptionArgs(pOpts);
  146. }
  147. /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
  148. /*=export_func optionFree
  149. *
  150. * what: free allocated option processing memory
  151. * arg: tOptions*, pOpts, program options descriptor
  152. *
  153. * doc: AutoOpts sometimes allocates memory and puts pointers to it in the
  154. * option state structures. This routine deallocates all such memory.
  155. *
  156. * err: As long as memory has not been corrupted,
  157. * this routine is always successful.
  158. =*/
  159. void
  160. optionFree( tOptions* pOpts )
  161. {
  162. free_saved_state:
  163. {
  164. tOptDesc* p = pOpts->pOptDesc;
  165. int ct = pOpts->optCt;
  166. do {
  167. if (p->fOptState & OPTST_ALLOC_ARG) {
  168. AGFREE(p->optArg.argString);
  169. p->optArg.argString = NULL;
  170. p->fOptState &= ~OPTST_ALLOC_ARG;
  171. }
  172. switch (OPTST_GET_ARGTYPE(p->fOptState)) {
  173. case OPARG_TYPE_STRING:
  174. #ifdef WITH_LIBREGEX
  175. if ( (p->fOptState & OPTST_STACKED)
  176. && (p->optCookie != NULL)) {
  177. p->optArg.argString = ".*";
  178. optionUnstackArg(pOpts, p);
  179. }
  180. #else
  181. /* leak memory */;
  182. #endif
  183. break;
  184. case OPARG_TYPE_HIERARCHY:
  185. if (p->optCookie != NULL)
  186. unloadNestedArglist(p->optCookie);
  187. break;
  188. }
  189. p->optCookie = NULL;
  190. } while (p++, --ct > 0);
  191. }
  192. if (pOpts->pSavedState != NULL) {
  193. tOptions * p = (tOptions*)pOpts->pSavedState;
  194. memcpy( pOpts, p, sizeof( *p ));
  195. memcpy( pOpts->pOptDesc, p+1, p->optCt * sizeof( tOptDesc ));
  196. AGFREE( pOpts->pSavedState );
  197. pOpts->pSavedState = NULL;
  198. goto free_saved_state;
  199. }
  200. }
  201. /*
  202. * Local Variables:
  203. * mode: C
  204. * c-file-style: "stroustrup"
  205. * indent-tabs-mode: nil
  206. * End:
  207. * end of autoopts/restore.c */