reset.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * $Id: reset.c,v 4.7 2009/08/01 17:43:06 bkorb Exp $
  3. * Time-stamp: "2008-08-02 12:25:18 bkorb"
  4. *
  5. * This file is part of AutoOpts, a companion to AutoGen.
  6. * AutoOpts is free software.
  7. * AutoOpts is copyright (c) 1992-2009 by Bruce Korb - all rights reserved
  8. *
  9. * AutoOpts is available under any one of two licenses. The license
  10. * in use must be one of these two and the choice is under the control
  11. * of the user of the license.
  12. *
  13. * The GNU Lesser General Public License, version 3 or later
  14. * See the files "COPYING.lgplv3" and "COPYING.gplv3"
  15. *
  16. * The Modified Berkeley Software Distribution License
  17. * See the file "COPYING.mbsd"
  18. *
  19. * These files have the following md5sums:
  20. *
  21. * 43b91e8ca915626ed3818ffb1b71248b pkg/libopts/COPYING.gplv3
  22. * 06a1a2e4760c90ea5e1dad8dfaac4d39 pkg/libopts/COPYING.lgplv3
  23. * 66a5cedaf62c4b2637025f049f9b826f pkg/libopts/COPYING.mbsd
  24. */
  25. static void
  26. optionReset( tOptions* pOpts, tOptDesc* pOD )
  27. {
  28. pOD->fOptState &= OPTST_PERSISTENT_MASK;
  29. pOD->fOptState |= OPTST_RESET;
  30. if (pOD->pOptProc != NULL)
  31. pOD->pOptProc(pOpts, pOD);
  32. pOD->optArg.argString =
  33. pOpts->originalOptArgArray[ pOD->optIndex ].argString;
  34. pOD->optCookie = pOpts->originalOptArgCookie[ pOD->optIndex ];
  35. pOD->fOptState &= OPTST_PERSISTENT_MASK;
  36. }
  37. static void
  38. optionResetEverything(tOptions * pOpts)
  39. {
  40. tOptDesc * pOD = pOpts->pOptDesc;
  41. int ct = pOpts->presetOptCt;
  42. for (;;) {
  43. optionReset(pOpts, pOD);
  44. if (--ct <= 0)
  45. break;
  46. pOD++;
  47. }
  48. }
  49. /*=export_func optionResetOpt
  50. * private:
  51. *
  52. * what: Reset the value of an option
  53. * arg: + tOptions* + pOpts + program options descriptor +
  54. * arg: + tOptDesc* + pOptDesc + the descriptor for this arg +
  55. *
  56. * doc:
  57. * This code will cause another option to be reset to its initial state.
  58. * For example, --reset=foo will cause the --foo option to be reset.
  59. =*/
  60. void
  61. optionResetOpt( tOptions* pOpts, tOptDesc* pOD )
  62. {
  63. static ag_bool reset_active = AG_FALSE;
  64. tOptState opt_state = OPTSTATE_INITIALIZER(DEFINED);
  65. char const * pzArg = pOD->optArg.argString;
  66. tSuccess succ;
  67. if (reset_active)
  68. return;
  69. if ( (! HAS_originalOptArgArray(pOpts))
  70. || (pOpts->originalOptArgCookie == NULL)) {
  71. fputs(zResetNotConfig, stderr);
  72. _exit(EX_SOFTWARE);
  73. }
  74. if ((pzArg == NULL) || (*pzArg == NUL)) {
  75. fputs(zNoResetArg, stderr);
  76. pOpts->pUsageProc(pOpts, EXIT_FAILURE);
  77. }
  78. reset_active = AG_TRUE;
  79. if (pzArg[1] == NUL) {
  80. if (*pzArg == '*') {
  81. optionResetEverything(pOpts);
  82. reset_active = AG_FALSE;
  83. return;
  84. }
  85. succ = shortOptionFind(pOpts, (tAoUC)*pzArg, &opt_state);
  86. if (! SUCCESSFUL(succ)) {
  87. fprintf(stderr, zIllOptChr, pOpts->pzProgPath, *pzArg);
  88. pOpts->pUsageProc(pOpts, EXIT_FAILURE);
  89. }
  90. } else {
  91. succ = longOptionFind(pOpts, (char *)pzArg, &opt_state);
  92. if (! SUCCESSFUL(succ)) {
  93. fprintf(stderr, zIllOptStr, pOpts->pzProgPath, pzArg);
  94. pOpts->pUsageProc(pOpts, EXIT_FAILURE);
  95. }
  96. }
  97. /*
  98. * We've found the indicated option. Turn off all non-persistent
  99. * flags because we're forcing the option back to its initialized state.
  100. * Call any callout procedure to handle whatever it needs to.
  101. * Finally, clear the reset flag, too.
  102. */
  103. optionReset(pOpts, opt_state.pOD);
  104. reset_active = AG_FALSE;
  105. }
  106. /*
  107. * Local Variables:
  108. * mode: C
  109. * c-file-style: "stroustrup"
  110. * indent-tabs-mode: nil
  111. * End:
  112. * end of autoopts/reset.c */