restore.c 7.2 KB

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