xref: /freebsd/contrib/ntp/sntp/libopts/boolean.c (revision a466cc55373fc3cf86837f09da729535b57e69a1)
1ea906c41SOllivier Robert 
22b15cb3dSCy Schubert /**
32b15cb3dSCy Schubert  * \file boolean.c
42b15cb3dSCy Schubert  *
52b15cb3dSCy Schubert  * Handle options with true/false values for arguments.
62b15cb3dSCy Schubert  *
72b15cb3dSCy Schubert  * @addtogroup autoopts
82b15cb3dSCy Schubert  * @{
92b15cb3dSCy Schubert  */
10ea906c41SOllivier Robert /*
11ea906c41SOllivier Robert  *  This routine will run run-on options through a pager so the
12ea906c41SOllivier Robert  *  user may examine, print or edit them at their leisure.
13ea906c41SOllivier Robert  *
142b15cb3dSCy Schubert  *  This file is part of AutoOpts, a companion to AutoGen.
152b15cb3dSCy Schubert  *  AutoOpts is free software.
16*a466cc55SCy Schubert  *  AutoOpts is Copyright (C) 1992-2018 by Bruce Korb - all rights reserved
17ea906c41SOllivier Robert  *
182b15cb3dSCy Schubert  *  AutoOpts is available under any one of two licenses.  The license
192b15cb3dSCy Schubert  *  in use must be one of these two and the choice is under the control
202b15cb3dSCy Schubert  *  of the user of the license.
21ea906c41SOllivier Robert  *
222b15cb3dSCy Schubert  *   The GNU Lesser General Public License, version 3 or later
232b15cb3dSCy Schubert  *      See the files "COPYING.lgplv3" and "COPYING.gplv3"
24ea906c41SOllivier Robert  *
252b15cb3dSCy Schubert  *   The Modified Berkeley Software Distribution License
262b15cb3dSCy Schubert  *      See the file "COPYING.mbsd"
27ea906c41SOllivier Robert  *
282b15cb3dSCy Schubert  *  These files have the following sha256 sums:
29ea906c41SOllivier Robert  *
302b15cb3dSCy Schubert  *  8584710e9b04216a394078dc156b781d0b47e1729104d666658aecef8ee32e95  COPYING.gplv3
312b15cb3dSCy Schubert  *  4379e7444a0e2ce2b12dd6f5a52a27a4d02d39d247901d3285c88cf0d37f477b  COPYING.lgplv3
322b15cb3dSCy Schubert  *  13aa749a5b0a454917a944ed8fffc530b784f5ead522b1aacaf4ec8aa55a6239  COPYING.mbsd
33ea906c41SOllivier Robert  */
34ea906c41SOllivier Robert 
35ea906c41SOllivier Robert /*=export_func  optionBooleanVal
36ea906c41SOllivier Robert  * private:
37ea906c41SOllivier Robert  *
38ea906c41SOllivier Robert  * what:  Decipher a boolean value
392b15cb3dSCy Schubert  * arg:   + tOptions * + opts + program options descriptor +
402b15cb3dSCy Schubert  * arg:   + tOptDesc * + od  + the descriptor for this arg +
41ea906c41SOllivier Robert  *
42ea906c41SOllivier Robert  * doc:
43ea906c41SOllivier Robert  *  Decipher a true or false value for a boolean valued option argument.
44ea906c41SOllivier Robert  *  The value is true, unless it starts with 'n' or 'f' or "#f" or
45ea906c41SOllivier Robert  *  it is an empty string or it is a number that evaluates to zero.
46ea906c41SOllivier Robert =*/
47ea906c41SOllivier Robert void
optionBooleanVal(tOptions * opts,tOptDesc * od)482b15cb3dSCy Schubert optionBooleanVal(tOptions * opts, tOptDesc * od)
49ea906c41SOllivier Robert {
50ea906c41SOllivier Robert     char * pz;
512b15cb3dSCy Schubert     bool   res = true;
52ea906c41SOllivier Robert 
532b15cb3dSCy Schubert     if (INQUERY_CALL(opts, od))
542b15cb3dSCy Schubert         return;
552b15cb3dSCy Schubert 
562b15cb3dSCy Schubert     if (od->optArg.argString == NULL) {
572b15cb3dSCy Schubert         od->optArg.argBool = false;
582b15cb3dSCy Schubert         return;
592b15cb3dSCy Schubert     }
602b15cb3dSCy Schubert 
612b15cb3dSCy Schubert     switch (*(od->optArg.argString)) {
62ea906c41SOllivier Robert     case '0':
63ea906c41SOllivier Robert     {
642b15cb3dSCy Schubert         long  val = strtol(od->optArg.argString, &pz, 0);
65ea906c41SOllivier Robert         if ((val != 0) || (*pz != NUL))
66ea906c41SOllivier Robert             break;
67ea906c41SOllivier Robert     }
68*a466cc55SCy Schubert     /* FALLTHROUGH */
69ea906c41SOllivier Robert     case 'N':
70ea906c41SOllivier Robert     case 'n':
71ea906c41SOllivier Robert     case 'F':
72ea906c41SOllivier Robert     case 'f':
73ea906c41SOllivier Robert     case NUL:
742b15cb3dSCy Schubert         res = false;
75ea906c41SOllivier Robert         break;
76ea906c41SOllivier Robert     case '#':
772b15cb3dSCy Schubert         if (od->optArg.argString[1] != 'f')
78ea906c41SOllivier Robert             break;
792b15cb3dSCy Schubert         res = false;
80ea906c41SOllivier Robert     }
81ea906c41SOllivier Robert 
822b15cb3dSCy Schubert     if (od->fOptState & OPTST_ALLOC_ARG) {
832b15cb3dSCy Schubert         AGFREE(od->optArg.argString);
842b15cb3dSCy Schubert         od->fOptState &= ~OPTST_ALLOC_ARG;
85ea906c41SOllivier Robert     }
862b15cb3dSCy Schubert     od->optArg.argBool = res;
87ea906c41SOllivier Robert }
882b15cb3dSCy Schubert /** @}
892b15cb3dSCy Schubert  *
90ea906c41SOllivier Robert  * Local Variables:
91ea906c41SOllivier Robert  * mode: C
92ea906c41SOllivier Robert  * c-file-style: "stroustrup"
93ea906c41SOllivier Robert  * indent-tabs-mode: nil
94ea906c41SOllivier Robert  * End:
95ea906c41SOllivier Robert  * end of autoopts/boolean.c */
96