xref: /freebsd/contrib/ntp/sntp/libopts/reset.c (revision a466cc55373fc3cf86837f09da729535b57e69a1)
12b15cb3dSCy Schubert 
22b15cb3dSCy Schubert /**
32b15cb3dSCy Schubert  * \file reset.c
42b15cb3dSCy Schubert  *
52b15cb3dSCy Schubert  *  Reset the option state to the compiled state.
62b15cb3dSCy Schubert  *
72b15cb3dSCy Schubert  * @addtogroup autoopts
82b15cb3dSCy Schubert  * @{
92b15cb3dSCy Schubert  */
102b15cb3dSCy Schubert /*
112b15cb3dSCy Schubert  *  This file is part of AutoOpts, a companion to AutoGen.
122b15cb3dSCy Schubert  *  AutoOpts is free software.
13*a466cc55SCy Schubert  *  AutoOpts is Copyright (C) 1992-2018 by Bruce Korb - all rights reserved
142b15cb3dSCy Schubert  *
152b15cb3dSCy Schubert  *  AutoOpts is available under any one of two licenses.  The license
162b15cb3dSCy Schubert  *  in use must be one of these two and the choice is under the control
172b15cb3dSCy Schubert  *  of the user of the license.
182b15cb3dSCy Schubert  *
192b15cb3dSCy Schubert  *   The GNU Lesser General Public License, version 3 or later
202b15cb3dSCy Schubert  *      See the files "COPYING.lgplv3" and "COPYING.gplv3"
212b15cb3dSCy Schubert  *
222b15cb3dSCy Schubert  *   The Modified Berkeley Software Distribution License
232b15cb3dSCy Schubert  *      See the file "COPYING.mbsd"
242b15cb3dSCy Schubert  *
252b15cb3dSCy Schubert  *  These files have the following sha256 sums:
262b15cb3dSCy Schubert  *
272b15cb3dSCy Schubert  *  8584710e9b04216a394078dc156b781d0b47e1729104d666658aecef8ee32e95  COPYING.gplv3
282b15cb3dSCy Schubert  *  4379e7444a0e2ce2b12dd6f5a52a27a4d02d39d247901d3285c88cf0d37f477b  COPYING.lgplv3
292b15cb3dSCy Schubert  *  13aa749a5b0a454917a944ed8fffc530b784f5ead522b1aacaf4ec8aa55a6239  COPYING.mbsd
302b15cb3dSCy Schubert  */
312b15cb3dSCy Schubert 
322b15cb3dSCy Schubert static void
optionReset(tOptions * pOpts,tOptDesc * pOD)332b15cb3dSCy Schubert optionReset(tOptions * pOpts, tOptDesc * pOD)
342b15cb3dSCy Schubert {
352b15cb3dSCy Schubert     pOD->fOptState &= OPTST_PERSISTENT_MASK;
362b15cb3dSCy Schubert     pOD->fOptState |= OPTST_RESET;
372b15cb3dSCy Schubert     if (pOD->pOptProc != NULL)
382b15cb3dSCy Schubert         pOD->pOptProc(pOpts, pOD);
392b15cb3dSCy Schubert     pOD->optArg.argString =
402b15cb3dSCy Schubert         pOpts->originalOptArgArray[ pOD->optIndex ].argString;
412b15cb3dSCy Schubert     pOD->optCookie = pOpts->originalOptArgCookie[ pOD->optIndex ];
422b15cb3dSCy Schubert     pOD->fOptState &= OPTST_PERSISTENT_MASK;
432b15cb3dSCy Schubert }
442b15cb3dSCy Schubert 
452b15cb3dSCy Schubert 
462b15cb3dSCy Schubert static void
optionResetEverything(tOptions * pOpts)472b15cb3dSCy Schubert optionResetEverything(tOptions * pOpts)
482b15cb3dSCy Schubert {
492b15cb3dSCy Schubert     tOptDesc * pOD = pOpts->pOptDesc;
502b15cb3dSCy Schubert     int        ct  = pOpts->presetOptCt;
512b15cb3dSCy Schubert 
522b15cb3dSCy Schubert     for (;;) {
532b15cb3dSCy Schubert         optionReset(pOpts, pOD);
542b15cb3dSCy Schubert 
552b15cb3dSCy Schubert         if (--ct <= 0)
562b15cb3dSCy Schubert             break;
572b15cb3dSCy Schubert         pOD++;
582b15cb3dSCy Schubert     }
592b15cb3dSCy Schubert }
602b15cb3dSCy Schubert 
612b15cb3dSCy Schubert 
622b15cb3dSCy Schubert /*=export_func  optionResetOpt
632b15cb3dSCy Schubert  * private:
642b15cb3dSCy Schubert  *
652b15cb3dSCy Schubert  * what:  Reset the value of an option
662b15cb3dSCy Schubert  * arg:   + tOptions * + pOpts    + program options descriptor  +
672b15cb3dSCy Schubert  * arg:   + tOptDesc * + pOptDesc + the descriptor for this arg +
682b15cb3dSCy Schubert  *
692b15cb3dSCy Schubert  * doc:
702b15cb3dSCy Schubert  *  This code will cause another option to be reset to its initial state.
712b15cb3dSCy Schubert  *  For example, --reset=foo will cause the --foo option to be reset.
722b15cb3dSCy Schubert =*/
732b15cb3dSCy Schubert void
optionResetOpt(tOptions * pOpts,tOptDesc * pOD)742b15cb3dSCy Schubert optionResetOpt(tOptions * pOpts, tOptDesc * pOD)
752b15cb3dSCy Schubert {
762b15cb3dSCy Schubert     static bool reset_active = false;
772b15cb3dSCy Schubert 
782b15cb3dSCy Schubert     tOptState opt_state = OPTSTATE_INITIALIZER(DEFINED);
792b15cb3dSCy Schubert     char const * pzArg = pOD->optArg.argString;
802b15cb3dSCy Schubert     tSuccess     succ;
812b15cb3dSCy Schubert 
822b15cb3dSCy Schubert     if (pOpts <= OPTPROC_EMIT_LIMIT)
832b15cb3dSCy Schubert         return;
842b15cb3dSCy Schubert 
852b15cb3dSCy Schubert     if (reset_active)
862b15cb3dSCy Schubert         return;
872b15cb3dSCy Schubert 
882b15cb3dSCy Schubert     if (  (! HAS_originalOptArgArray(pOpts))
892b15cb3dSCy Schubert        || (pOpts->originalOptArgCookie == NULL))
902b15cb3dSCy Schubert         ao_bug(zno_reset);
912b15cb3dSCy Schubert 
922b15cb3dSCy Schubert     if ((pzArg == NULL) || (*pzArg == NUL)) {
932b15cb3dSCy Schubert         fprintf(stderr, zreset_arg, pOpts->pzProgName, pOD->pz_Name);
942b15cb3dSCy Schubert         pOpts->pUsageProc(pOpts, EXIT_FAILURE);
952b15cb3dSCy Schubert         /* NOTREACHED */
962b15cb3dSCy Schubert         assert(0 == 1);
972b15cb3dSCy Schubert     }
982b15cb3dSCy Schubert 
992b15cb3dSCy Schubert     reset_active = true;
1002b15cb3dSCy Schubert 
1012b15cb3dSCy Schubert     if (pzArg[1] == NUL) {
1022b15cb3dSCy Schubert         if (*pzArg == '*') {
1032b15cb3dSCy Schubert             optionResetEverything(pOpts);
1042b15cb3dSCy Schubert             reset_active = false;
1052b15cb3dSCy Schubert             return;
1062b15cb3dSCy Schubert         }
1072b15cb3dSCy Schubert 
1082b15cb3dSCy Schubert         succ = opt_find_short(pOpts, (uint8_t)*pzArg, &opt_state);
1092b15cb3dSCy Schubert         if (! SUCCESSFUL(succ)) {
1102b15cb3dSCy Schubert             fprintf(stderr, zIllOptChr, pOpts->pzProgPath, *pzArg);
1112b15cb3dSCy Schubert             pOpts->pUsageProc(pOpts, EXIT_FAILURE);
1122b15cb3dSCy Schubert             /* NOTREACHED */
1132b15cb3dSCy Schubert             assert(0 == 1);
1142b15cb3dSCy Schubert         }
1152b15cb3dSCy Schubert     } else {
116*a466cc55SCy Schubert         succ = opt_find_long(pOpts, (char *)pzArg, &opt_state);
1172b15cb3dSCy Schubert         if (! SUCCESSFUL(succ)) {
1182b15cb3dSCy Schubert             fprintf(stderr, zIllOptStr, pOpts->pzProgPath, pzArg);
1192b15cb3dSCy Schubert             pOpts->pUsageProc(pOpts, EXIT_FAILURE);
1202b15cb3dSCy Schubert             /* NOTREACHED */
1212b15cb3dSCy Schubert             assert(0 == 1);
1222b15cb3dSCy Schubert         }
1232b15cb3dSCy Schubert     }
1242b15cb3dSCy Schubert 
1252b15cb3dSCy Schubert     /*
1262b15cb3dSCy Schubert      *  We've found the indicated option.  Turn off all non-persistent
1272b15cb3dSCy Schubert      *  flags because we're forcing the option back to its initialized state.
1282b15cb3dSCy Schubert      *  Call any callout procedure to handle whatever it needs to.
1292b15cb3dSCy Schubert      *  Finally, clear the reset flag, too.
1302b15cb3dSCy Schubert      */
1312b15cb3dSCy Schubert     optionReset(pOpts, opt_state.pOD);
1322b15cb3dSCy Schubert     reset_active = false;
1332b15cb3dSCy Schubert }
1342b15cb3dSCy Schubert /** @}
1352b15cb3dSCy Schubert  *
1362b15cb3dSCy Schubert  * Local Variables:
1372b15cb3dSCy Schubert  * mode: C
1382b15cb3dSCy Schubert  * c-file-style: "stroustrup"
1392b15cb3dSCy Schubert  * indent-tabs-mode: nil
1402b15cb3dSCy Schubert  * End:
1412b15cb3dSCy Schubert  * end of autoopts/reset.c */
142