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