restore.c 8.3 KB

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