xref: /freebsd-src/contrib/ntp/sntp/libopts/check.c (revision a466cc55373fc3cf86837f09da729535b57e69a1)
12b15cb3dSCy Schubert /**
22b15cb3dSCy Schubert  * @file check.c
32b15cb3dSCy Schubert  *
42b15cb3dSCy Schubert  * @brief option consistency checks.
52b15cb3dSCy Schubert  *
62b15cb3dSCy Schubert  * @addtogroup autoopts
72b15cb3dSCy Schubert  * @{
82b15cb3dSCy Schubert  */
92b15cb3dSCy Schubert /*
102b15cb3dSCy Schubert  *  This file is part of AutoOpts, a companion to AutoGen.
112b15cb3dSCy Schubert  *  AutoOpts is free software.
12*a466cc55SCy Schubert  *  AutoOpts is Copyright (C) 1992-2018 by Bruce Korb - all rights reserved
132b15cb3dSCy Schubert  *
142b15cb3dSCy Schubert  *  AutoOpts is available under any one of two licenses.  The license
152b15cb3dSCy Schubert  *  in use must be one of these two and the choice is under the control
162b15cb3dSCy Schubert  *  of the user of the license.
172b15cb3dSCy Schubert  *
182b15cb3dSCy Schubert  *   The GNU Lesser General Public License, version 3 or later
192b15cb3dSCy Schubert  *      See the files "COPYING.lgplv3" and "COPYING.gplv3"
202b15cb3dSCy Schubert  *
212b15cb3dSCy Schubert  *   The Modified Berkeley Software Distribution License
222b15cb3dSCy Schubert  *      See the file "COPYING.mbsd"
232b15cb3dSCy Schubert  *
242b15cb3dSCy Schubert  *  These files have the following sha256 sums:
252b15cb3dSCy Schubert  *
262b15cb3dSCy Schubert  *  8584710e9b04216a394078dc156b781d0b47e1729104d666658aecef8ee32e95  COPYING.gplv3
272b15cb3dSCy Schubert  *  4379e7444a0e2ce2b12dd6f5a52a27a4d02d39d247901d3285c88cf0d37f477b  COPYING.lgplv3
282b15cb3dSCy Schubert  *  13aa749a5b0a454917a944ed8fffc530b784f5ead522b1aacaf4ec8aa55a6239  COPYING.mbsd
292b15cb3dSCy Schubert  */
302b15cb3dSCy Schubert 
312b15cb3dSCy Schubert /**
322b15cb3dSCy Schubert  *  Check for conflicts based on "must" and "cannot" attributes.
332b15cb3dSCy Schubert  */
342b15cb3dSCy Schubert static bool
has_conflict(tOptions * pOpts,tOptDesc * od)352b15cb3dSCy Schubert has_conflict(tOptions * pOpts, tOptDesc * od)
362b15cb3dSCy Schubert {
372b15cb3dSCy Schubert     if (od->pOptMust != NULL) {
382b15cb3dSCy Schubert         int const * must = od->pOptMust;
392b15cb3dSCy Schubert 
402b15cb3dSCy Schubert         while (*must != NO_EQUIVALENT) {
412b15cb3dSCy Schubert             tOptDesc * p = pOpts->pOptDesc + *(must++);
422b15cb3dSCy Schubert             if (UNUSED_OPT(p)) {
432b15cb3dSCy Schubert                 const tOptDesc * ood = pOpts->pOptDesc + must[-1];
442b15cb3dSCy Schubert                 fprintf(stderr, zneed_fmt, pOpts->pzProgName,
452b15cb3dSCy Schubert                         od->pz_Name, ood->pz_Name);
462b15cb3dSCy Schubert                 return true;
472b15cb3dSCy Schubert             }
482b15cb3dSCy Schubert         }
492b15cb3dSCy Schubert     }
502b15cb3dSCy Schubert 
512b15cb3dSCy Schubert     if (od->pOptCant != NULL) {
522b15cb3dSCy Schubert         int const * cant = od->pOptCant;
532b15cb3dSCy Schubert 
542b15cb3dSCy Schubert         while (*cant != NO_EQUIVALENT) {
552b15cb3dSCy Schubert             tOptDesc * p = pOpts->pOptDesc + *(cant++);
562b15cb3dSCy Schubert             if (SELECTED_OPT(p)) {
572b15cb3dSCy Schubert                 const tOptDesc * ood = pOpts->pOptDesc + cant[-1];
582b15cb3dSCy Schubert                 fprintf(stderr, zconflict_fmt, pOpts->pzProgName,
592b15cb3dSCy Schubert                         od->pz_Name, ood->pz_Name);
602b15cb3dSCy Schubert                 return true;
612b15cb3dSCy Schubert             }
622b15cb3dSCy Schubert         }
632b15cb3dSCy Schubert     }
642b15cb3dSCy Schubert 
652b15cb3dSCy Schubert     return false;
662b15cb3dSCy Schubert }
672b15cb3dSCy Schubert 
682b15cb3dSCy Schubert /**
692b15cb3dSCy Schubert  *  Check that the option occurs often enough.  Too often is already checked.
702b15cb3dSCy Schubert  */
712b15cb3dSCy Schubert static bool
occurs_enough(tOptions * pOpts,tOptDesc * pOD)722b15cb3dSCy Schubert occurs_enough(tOptions * pOpts, tOptDesc * pOD)
732b15cb3dSCy Schubert {
742b15cb3dSCy Schubert     (void)pOpts;
752b15cb3dSCy Schubert 
762b15cb3dSCy Schubert     /*
772b15cb3dSCy Schubert      *  IF the occurrence counts have been satisfied,
782b15cb3dSCy Schubert      *  THEN there is no problem.
792b15cb3dSCy Schubert      */
802b15cb3dSCy Schubert     if (pOD->optOccCt >= pOD->optMinCt)
812b15cb3dSCy Schubert         return true;
822b15cb3dSCy Schubert 
832b15cb3dSCy Schubert     /*
842b15cb3dSCy Schubert      *  IF MUST_SET means SET and PRESET are okay,
852b15cb3dSCy Schubert      *  so min occurrence count doesn't count
862b15cb3dSCy Schubert      */
872b15cb3dSCy Schubert     if (  (pOD->fOptState & OPTST_MUST_SET)
882b15cb3dSCy Schubert        && (pOD->fOptState & (OPTST_PRESET | OPTST_SET)) )
892b15cb3dSCy Schubert         return true;
902b15cb3dSCy Schubert 
912b15cb3dSCy Schubert     if (pOD->optMinCt > 1)
922b15cb3dSCy Schubert          fprintf(stderr, zneed_more, pOpts->pzProgName, pOD->pz_Name,
932b15cb3dSCy Schubert                  pOD->optMinCt);
942b15cb3dSCy Schubert     else fprintf(stderr, zneed_one,  pOpts->pzProgName, pOD->pz_Name);
952b15cb3dSCy Schubert     return false;
962b15cb3dSCy Schubert }
972b15cb3dSCy Schubert 
982b15cb3dSCy Schubert /**
992b15cb3dSCy Schubert  *  Verify option consistency.
1002b15cb3dSCy Schubert  *
1012b15cb3dSCy Schubert  *  Make sure that the argument list passes our consistency tests.
1022b15cb3dSCy Schubert  */
103*a466cc55SCy Schubert static bool
is_consistent(tOptions * pOpts)1042b15cb3dSCy Schubert is_consistent(tOptions * pOpts)
1052b15cb3dSCy Schubert {
1062b15cb3dSCy Schubert     tOptDesc * pOD   = pOpts->pOptDesc;
1072b15cb3dSCy Schubert     int        oCt   = pOpts->presetOptCt;
1082b15cb3dSCy Schubert 
1092b15cb3dSCy Schubert     /*
1102b15cb3dSCy Schubert      *  FOR each of "oCt" options, ...
1112b15cb3dSCy Schubert      */
1122b15cb3dSCy Schubert     for (;;) {
1132b15cb3dSCy Schubert         /*
1142b15cb3dSCy Schubert          *  IF the current option was provided on the command line
1152b15cb3dSCy Schubert          *  THEN ensure that any "MUST" requirements are not
1162b15cb3dSCy Schubert          *       "DEFAULT" (unspecified) *AND* ensure that any
1172b15cb3dSCy Schubert          *       "CANT" options have not been SET or DEFINED.
1182b15cb3dSCy Schubert          */
1192b15cb3dSCy Schubert         if (SELECTED_OPT(pOD)) {
1202b15cb3dSCy Schubert             if (has_conflict(pOpts, pOD))
1212b15cb3dSCy Schubert                 return false;
1222b15cb3dSCy Schubert         }
1232b15cb3dSCy Schubert 
1242b15cb3dSCy Schubert         /*
1252b15cb3dSCy Schubert          *  IF       this option is not equivalenced to another,
1262b15cb3dSCy Schubert          *        OR it is equivalenced to itself (is the equiv. root)
1272b15cb3dSCy Schubert          *  THEN we need to make sure it occurs often enough.
1282b15cb3dSCy Schubert          */
1292b15cb3dSCy Schubert         if (  (pOD->optEquivIndex == NO_EQUIVALENT)
1302b15cb3dSCy Schubert            || (pOD->optEquivIndex == pOD->optIndex) )
1312b15cb3dSCy Schubert 
1322b15cb3dSCy Schubert             if (! occurs_enough(pOpts, pOD))
1332b15cb3dSCy Schubert                 return false;
1342b15cb3dSCy Schubert 
1352b15cb3dSCy Schubert         if (--oCt <= 0)
1362b15cb3dSCy Schubert             break;
1372b15cb3dSCy Schubert         pOD++;
1382b15cb3dSCy Schubert     }
1392b15cb3dSCy Schubert 
1402b15cb3dSCy Schubert     /*
1412b15cb3dSCy Schubert      *  IF we are stopping on errors, check to see if any remaining
1422b15cb3dSCy Schubert      *  arguments are required to be there or prohibited from being there.
1432b15cb3dSCy Schubert      */
1442b15cb3dSCy Schubert     if ((pOpts->fOptSet & OPTPROC_ERRSTOP) != 0) {
1452b15cb3dSCy Schubert 
1462b15cb3dSCy Schubert         /*
1472b15cb3dSCy Schubert          *  Check for prohibition
1482b15cb3dSCy Schubert          */
1492b15cb3dSCy Schubert         if ((pOpts->fOptSet & OPTPROC_NO_ARGS) != 0) {
1502b15cb3dSCy Schubert             if (pOpts->origArgCt > pOpts->curOptIdx) {
1512b15cb3dSCy Schubert                 fprintf(stderr, zNoArgs, pOpts->pzProgName);
1522b15cb3dSCy Schubert                 return false;
1532b15cb3dSCy Schubert             }
1542b15cb3dSCy Schubert         }
1552b15cb3dSCy Schubert 
1562b15cb3dSCy Schubert         /*
1572b15cb3dSCy Schubert          *  ELSE not prohibited, check for being required
1582b15cb3dSCy Schubert          */
1592b15cb3dSCy Schubert         else if ((pOpts->fOptSet & OPTPROC_ARGS_REQ) != 0) {
1602b15cb3dSCy Schubert             if (pOpts->origArgCt <= pOpts->curOptIdx) {
1612b15cb3dSCy Schubert                 fprintf(stderr, zargs_must, pOpts->pzProgName);
1622b15cb3dSCy Schubert                 return false;
1632b15cb3dSCy Schubert             }
1642b15cb3dSCy Schubert         }
1652b15cb3dSCy Schubert     }
1662b15cb3dSCy Schubert 
1672b15cb3dSCy Schubert     return true;
1682b15cb3dSCy Schubert }
1692b15cb3dSCy Schubert 
1702b15cb3dSCy Schubert /** @}
1712b15cb3dSCy Schubert  *
1722b15cb3dSCy Schubert  * Local Variables:
1732b15cb3dSCy Schubert  * mode: C
1742b15cb3dSCy Schubert  * c-file-style: "stroustrup"
1752b15cb3dSCy Schubert  * indent-tabs-mode: nil
1762b15cb3dSCy Schubert  * End:
1772b15cb3dSCy Schubert  * end of autoopts/check.c */
178