restore.c 6.8 KB

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