boolean.c 2.4 KB

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