1 /* $NetBSD: reset.c,v 1.9 2020/05/25 20:47:35 christos Exp $ */ 2 3 4 /** 5 * \file reset.c 6 * 7 * Reset the option state to the compiled state. 8 * 9 * @addtogroup autoopts 10 * @{ 11 */ 12 /* 13 * This file is part of AutoOpts, a companion to AutoGen. 14 * AutoOpts is free software. 15 * AutoOpts is Copyright (C) 1992-2015 by Bruce Korb - all rights reserved 16 * 17 * AutoOpts is available under any one of two licenses. The license 18 * in use must be one of these two and the choice is under the control 19 * of the user of the license. 20 * 21 * The GNU Lesser General Public License, version 3 or later 22 * See the files "COPYING.lgplv3" and "COPYING.gplv3" 23 * 24 * The Modified Berkeley Software Distribution License 25 * See the file "COPYING.mbsd" 26 * 27 * These files have the following sha256 sums: 28 * 29 * 8584710e9b04216a394078dc156b781d0b47e1729104d666658aecef8ee32e95 COPYING.gplv3 30 * 4379e7444a0e2ce2b12dd6f5a52a27a4d02d39d247901d3285c88cf0d37f477b COPYING.lgplv3 31 * 13aa749a5b0a454917a944ed8fffc530b784f5ead522b1aacaf4ec8aa55a6239 COPYING.mbsd 32 */ 33 34 static void 35 optionReset(tOptions * pOpts, tOptDesc * pOD) 36 { 37 pOD->fOptState &= OPTST_PERSISTENT_MASK; 38 pOD->fOptState |= OPTST_RESET; 39 if (pOD->pOptProc != NULL) 40 pOD->pOptProc(pOpts, pOD); 41 pOD->optArg.argString = 42 pOpts->originalOptArgArray[ pOD->optIndex ].argString; 43 pOD->optCookie = pOpts->originalOptArgCookie[ pOD->optIndex ]; 44 pOD->fOptState &= OPTST_PERSISTENT_MASK; 45 } 46 47 48 static void 49 optionResetEverything(tOptions * pOpts) 50 { 51 tOptDesc * pOD = pOpts->pOptDesc; 52 int ct = pOpts->presetOptCt; 53 54 for (;;) { 55 optionReset(pOpts, pOD); 56 57 if (--ct <= 0) 58 break; 59 pOD++; 60 } 61 } 62 63 64 /*=export_func optionResetOpt 65 * private: 66 * 67 * what: Reset the value of an option 68 * arg: + tOptions * + pOpts + program options descriptor + 69 * arg: + tOptDesc * + pOptDesc + the descriptor for this arg + 70 * 71 * doc: 72 * This code will cause another option to be reset to its initial state. 73 * For example, --reset=foo will cause the --foo option to be reset. 74 =*/ 75 void 76 optionResetOpt(tOptions * pOpts, tOptDesc * pOD) 77 { 78 static bool reset_active = false; 79 80 tOptState opt_state = OPTSTATE_INITIALIZER(DEFINED); 81 char const * pzArg = pOD->optArg.argString; 82 tSuccess succ; 83 84 if (pOpts <= OPTPROC_EMIT_LIMIT) 85 return; 86 87 if (reset_active) 88 return; 89 90 if ( (! HAS_originalOptArgArray(pOpts)) 91 || (pOpts->originalOptArgCookie == NULL)) 92 ao_bug(zno_reset); 93 94 if ((pzArg == NULL) || (*pzArg == NUL)) { 95 fprintf(stderr, zreset_arg, pOpts->pzProgName, pOD->pz_Name); 96 pOpts->pUsageProc(pOpts, EXIT_FAILURE); 97 /* NOTREACHED */ 98 assert(0 == 1); 99 } 100 101 reset_active = true; 102 103 if (pzArg[1] == NUL) { 104 if (*pzArg == '*') { 105 optionResetEverything(pOpts); 106 reset_active = false; 107 return; 108 } 109 110 succ = opt_find_short(pOpts, (uint8_t)*pzArg, &opt_state); 111 if (! SUCCESSFUL(succ)) { 112 fprintf(stderr, zIllOptChr, pOpts->pzProgPath, *pzArg); 113 pOpts->pUsageProc(pOpts, EXIT_FAILURE); 114 /* NOTREACHED */ 115 assert(0 == 1); 116 } 117 } else { 118 succ = opt_find_long(pOpts, pzArg, &opt_state); 119 if (! SUCCESSFUL(succ)) { 120 fprintf(stderr, zIllOptStr, pOpts->pzProgPath, pzArg); 121 pOpts->pUsageProc(pOpts, EXIT_FAILURE); 122 /* NOTREACHED */ 123 assert(0 == 1); 124 } 125 } 126 127 /* 128 * We've found the indicated option. Turn off all non-persistent 129 * flags because we're forcing the option back to its initialized state. 130 * Call any callout procedure to handle whatever it needs to. 131 * Finally, clear the reset flag, too. 132 */ 133 optionReset(pOpts, opt_state.pOD); 134 reset_active = false; 135 } 136 /** @} 137 * 138 * Local Variables: 139 * mode: C 140 * c-file-style: "stroustrup" 141 * indent-tabs-mode: nil 142 * End: 143 * end of autoopts/reset.c */ 144