restore.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. * restore.c $Id: restore.c,v 4.4 2005/03/13 19:51:59 bkorb Exp $
  3. * Time-stamp: "2005-02-23 15:10:20 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-2005 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. * 59 Temple Place - Suite 330,
  26. * Boston, MA 02111-1307, 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. /*=export_func optionSaveState
  52. *
  53. * what: saves the option state to memory
  54. * arg: tOptions*, pOpts, program options descriptor
  55. *
  56. * doc: This routine will allocate enough memory to save the current
  57. * option processing state. If this routine has been called before,
  58. * that memory will be reused. You may only save one copy of the
  59. * option state. This routine may be called before optionProcess(3AO).
  60. * If you do call it before the first call to optionProcess, then
  61. * you may also change the contents of argc/argv after you call
  62. * optionRestore(3AO)
  63. *
  64. * err: If it fails to allocate the memory,
  65. * it will print a message to stderr and exit.
  66. * Otherwise, it will always succeed.
  67. =*/
  68. void
  69. optionSaveState( tOptions* pOpts )
  70. {
  71. if (pOpts->pSavedState == NULL) {
  72. size_t sz = sizeof( *pOpts ) + (pOpts->optCt * sizeof( tOptDesc ));
  73. pOpts->pSavedState = AGALOC( sz, "saved option state" );
  74. if (pOpts->pSavedState == NULL) {
  75. tCC* pzName = pOpts->pzProgName;
  76. if (pzName == NULL) {
  77. pzName = pOpts->pzPROGNAME;
  78. if (pzName == NULL)
  79. pzName = zNil;
  80. }
  81. fprintf( stderr, zCantSave, pzName, sz );
  82. exit( EXIT_FAILURE );
  83. }
  84. }
  85. {
  86. tOptions* p = pOpts->pSavedState;
  87. tOptDesc* pOD = pOpts->pOptDesc;
  88. int ct = pOpts->optCt;
  89. memcpy( p, pOpts, sizeof( *p ));
  90. memcpy( p + 1, pOpts->pOptDesc, p->optCt * sizeof( tOptDesc ));
  91. /*
  92. * Make sure that allocated stuff is only referenced in the
  93. * archived copy of the data.
  94. */
  95. for (; ct-- > 0; pOD++) {
  96. switch (OPTST_GET_ARGTYPE(pOD->fOptState)) {
  97. case OPARG_TYPE_STRING:
  98. if (pOD->fOptState & OPTST_STACKED) {
  99. tOptDesc* q = p->pOptDesc + (pOD - pOpts->pOptDesc);
  100. q->optCookie = NULL;
  101. }
  102. break;
  103. case OPARG_TYPE_HIERARCHY:
  104. {
  105. tOptDesc* q = p->pOptDesc + (pOD - pOpts->pOptDesc);
  106. q->optCookie = NULL;
  107. }
  108. }
  109. }
  110. }
  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. memcpy( pOpts, p, sizeof( *p ));
  142. memcpy( pOpts->pOptDesc, p+1, p->optCt * sizeof( tOptDesc ));
  143. }
  144. /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
  145. /*=export_func optionFree
  146. *
  147. * what: free allocated option processing memory
  148. * arg: tOptions*, pOpts, program options descriptor
  149. *
  150. * doc: AutoOpts sometimes allocates memory and puts pointers to it in the
  151. * option state structures. This routine deallocates all such memory.
  152. *
  153. * err: As long as memory has not been corrupted,
  154. * this routine is always successful.
  155. =*/
  156. void
  157. optionFree( tOptions* pOpts )
  158. {
  159. if (pOpts->pSavedState != NULL) {
  160. AGFREE( pOpts->pSavedState );
  161. pOpts->pSavedState = NULL;
  162. }
  163. {
  164. tOptDesc* p = pOpts->pOptDesc;
  165. int ct = pOpts->optCt;
  166. do {
  167. switch (OPTST_GET_ARGTYPE(p->fOptState)) {
  168. case OPARG_TYPE_STRING:
  169. if ( (p->fOptState & OPTST_STACKED)
  170. && (p->optCookie != NULL)) {
  171. AGFREE( p->optCookie );
  172. p->fOptState &= OPTST_PERSISTENT;
  173. if ((p->fOptState & OPTST_INITENABLED) == 0)
  174. p->fOptState |= OPTST_DISABLED;
  175. }
  176. break;
  177. case OPARG_TYPE_HIERARCHY:
  178. if (p->optCookie != NULL)
  179. optionUnloadNested(p->optCookie);
  180. break;
  181. }
  182. p->optCookie = NULL;
  183. } while (p++, --ct > 0);
  184. }
  185. }
  186. /*
  187. * Local Variables:
  188. * mode: C
  189. * c-file-style: "stroustrup"
  190. * tab-width: 4
  191. * indent-tabs-mode: nil
  192. * End:
  193. * end of autoopts/restore.c */