reset.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /**
  2. * \file reset.c
  3. *
  4. * Reset the option state to the compiled state.
  5. *
  6. * @addtogroup autoopts
  7. * @{
  8. */
  9. /*
  10. * This file is part of AutoOpts, a companion to AutoGen.
  11. * AutoOpts is free software.
  12. * AutoOpts is Copyright (C) 1992-2016 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 sha256 sums:
  25. *
  26. * 8584710e9b04216a394078dc156b781d0b47e1729104d666658aecef8ee32e95 COPYING.gplv3
  27. * 4379e7444a0e2ce2b12dd6f5a52a27a4d02d39d247901d3285c88cf0d37f477b COPYING.lgplv3
  28. * 13aa749a5b0a454917a944ed8fffc530b784f5ead522b1aacaf4ec8aa55a6239 COPYING.mbsd
  29. */
  30. static void
  31. optionReset(tOptions * pOpts, tOptDesc * pOD)
  32. {
  33. pOD->fOptState &= OPTST_PERSISTENT_MASK;
  34. pOD->fOptState |= OPTST_RESET;
  35. if (pOD->pOptProc != NULL)
  36. pOD->pOptProc(pOpts, pOD);
  37. pOD->optArg.argString =
  38. pOpts->originalOptArgArray[ pOD->optIndex ].argString;
  39. pOD->optCookie = pOpts->originalOptArgCookie[ pOD->optIndex ];
  40. pOD->fOptState &= OPTST_PERSISTENT_MASK;
  41. }
  42. static void
  43. optionResetEverything(tOptions * pOpts)
  44. {
  45. tOptDesc * pOD = pOpts->pOptDesc;
  46. int ct = pOpts->presetOptCt;
  47. for (;;) {
  48. optionReset(pOpts, pOD);
  49. if (--ct <= 0)
  50. break;
  51. pOD++;
  52. }
  53. }
  54. /*=export_func optionResetOpt
  55. * private:
  56. *
  57. * what: Reset the value of an option
  58. * arg: + tOptions * + pOpts + program options descriptor +
  59. * arg: + tOptDesc * + pOptDesc + the descriptor for this arg +
  60. *
  61. * doc:
  62. * This code will cause another option to be reset to its initial state.
  63. * For example, --reset=foo will cause the --foo option to be reset.
  64. =*/
  65. void
  66. optionResetOpt(tOptions * pOpts, tOptDesc * pOD)
  67. {
  68. static bool reset_active = false;
  69. tOptState opt_state = OPTSTATE_INITIALIZER(DEFINED);
  70. char const * pzArg = pOD->optArg.argString;
  71. tSuccess succ;
  72. if (pOpts <= OPTPROC_EMIT_LIMIT)
  73. return;
  74. if (reset_active)
  75. return;
  76. if ( (! HAS_originalOptArgArray(pOpts))
  77. || (pOpts->originalOptArgCookie == NULL))
  78. ao_bug(zno_reset);
  79. if ((pzArg == NULL) || (*pzArg == NUL)) {
  80. fprintf(stderr, zreset_arg, pOpts->pzProgName, pOD->pz_Name);
  81. pOpts->pUsageProc(pOpts, EXIT_FAILURE);
  82. /* NOTREACHED */
  83. assert(0 == 1);
  84. }
  85. reset_active = true;
  86. if (pzArg[1] == NUL) {
  87. if (*pzArg == '*') {
  88. optionResetEverything(pOpts);
  89. reset_active = false;
  90. return;
  91. }
  92. succ = opt_find_short(pOpts, (uint8_t)*pzArg, &opt_state);
  93. if (! SUCCESSFUL(succ)) {
  94. fprintf(stderr, zIllOptChr, pOpts->pzProgPath, *pzArg);
  95. pOpts->pUsageProc(pOpts, EXIT_FAILURE);
  96. /* NOTREACHED */
  97. assert(0 == 1);
  98. }
  99. } else {
  100. succ = opt_find_long(pOpts, (char *)pzArg, &opt_state);
  101. if (! SUCCESSFUL(succ)) {
  102. fprintf(stderr, zIllOptStr, pOpts->pzProgPath, pzArg);
  103. pOpts->pUsageProc(pOpts, EXIT_FAILURE);
  104. /* NOTREACHED */
  105. assert(0 == 1);
  106. }
  107. }
  108. /*
  109. * We've found the indicated option. Turn off all non-persistent
  110. * flags because we're forcing the option back to its initialized state.
  111. * Call any callout procedure to handle whatever it needs to.
  112. * Finally, clear the reset flag, too.
  113. */
  114. optionReset(pOpts, opt_state.pOD);
  115. reset_active = false;
  116. }
  117. /** @}
  118. *
  119. * Local Variables:
  120. * mode: C
  121. * c-file-style: "stroustrup"
  122. * indent-tabs-mode: nil
  123. * End:
  124. * end of autoopts/reset.c */