xref: /freebsd/contrib/ntp/sntp/libopts/boolean.c (revision 63d1fd5970ec814904aa0f4580b10a0d302d08b2)
1 
2 /**
3  * \file boolean.c
4  *
5  * Handle options with true/false values for arguments.
6  *
7  * @addtogroup autoopts
8  * @{
9  */
10 /*
11  *  This routine will run run-on options through a pager so the
12  *  user may examine, print or edit them at their leisure.
13  *
14  *  This file is part of AutoOpts, a companion to AutoGen.
15  *  AutoOpts is free software.
16  *  AutoOpts is Copyright (C) 1992-2015 by Bruce Korb - all rights reserved
17  *
18  *  AutoOpts is available under any one of two licenses.  The license
19  *  in use must be one of these two and the choice is under the control
20  *  of the user of the license.
21  *
22  *   The GNU Lesser General Public License, version 3 or later
23  *      See the files "COPYING.lgplv3" and "COPYING.gplv3"
24  *
25  *   The Modified Berkeley Software Distribution License
26  *      See the file "COPYING.mbsd"
27  *
28  *  These files have the following sha256 sums:
29  *
30  *  8584710e9b04216a394078dc156b781d0b47e1729104d666658aecef8ee32e95  COPYING.gplv3
31  *  4379e7444a0e2ce2b12dd6f5a52a27a4d02d39d247901d3285c88cf0d37f477b  COPYING.lgplv3
32  *  13aa749a5b0a454917a944ed8fffc530b784f5ead522b1aacaf4ec8aa55a6239  COPYING.mbsd
33  */
34 
35 /*=export_func  optionBooleanVal
36  * private:
37  *
38  * what:  Decipher a boolean value
39  * arg:   + tOptions * + opts + program options descriptor +
40  * arg:   + tOptDesc * + od  + the descriptor for this arg +
41  *
42  * doc:
43  *  Decipher a true or false value for a boolean valued option argument.
44  *  The value is true, unless it starts with 'n' or 'f' or "#f" or
45  *  it is an empty string or it is a number that evaluates to zero.
46 =*/
47 void
48 optionBooleanVal(tOptions * opts, tOptDesc * od)
49 {
50     char * pz;
51     bool   res = true;
52 
53     if (INQUERY_CALL(opts, od))
54         return;
55 
56     if (od->optArg.argString == NULL) {
57         od->optArg.argBool = false;
58         return;
59     }
60 
61     switch (*(od->optArg.argString)) {
62     case '0':
63     {
64         long  val = strtol(od->optArg.argString, &pz, 0);
65         if ((val != 0) || (*pz != NUL))
66             break;
67         /* FALLTHROUGH */
68     }
69     case 'N':
70     case 'n':
71     case 'F':
72     case 'f':
73     case NUL:
74         res = false;
75         break;
76     case '#':
77         if (od->optArg.argString[1] != 'f')
78             break;
79         res = false;
80     }
81 
82     if (od->fOptState & OPTST_ALLOC_ARG) {
83         AGFREE(od->optArg.argString);
84         od->fOptState &= ~OPTST_ALLOC_ARG;
85     }
86     od->optArg.argBool = res;
87 }
88 /** @}
89  *
90  * Local Variables:
91  * mode: C
92  * c-file-style: "stroustrup"
93  * indent-tabs-mode: nil
94  * End:
95  * end of autoopts/boolean.c */
96