numeric.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /**
  2. * \file numeric.c
  3. *
  4. * Handle options with numeric (integer) arguments.
  5. *
  6. * @addtogroup autoopts
  7. * @{
  8. */
  9. /*
  10. * This file is part of AutoOpts, a companion to AutoGen.
  11. * AutoOpts is free software.
  12. * AutoOpts is Copyright (C) 1992-2014 by Bruce Korb - all rights reserved
  13. *
  14. * AutoOpts is available under any one of two licenses. The license
  15. * in use must be one of these two and the choice is under the control
  16. * of the user of the license.
  17. *
  18. * The GNU Lesser General Public License, version 3 or later
  19. * See the files "COPYING.lgplv3" and "COPYING.gplv3"
  20. *
  21. * The Modified Berkeley Software Distribution License
  22. * See the file "COPYING.mbsd"
  23. *
  24. * These files have the following sha256 sums:
  25. *
  26. * 8584710e9b04216a394078dc156b781d0b47e1729104d666658aecef8ee32e95 COPYING.gplv3
  27. * 4379e7444a0e2ce2b12dd6f5a52a27a4d02d39d247901d3285c88cf0d37f477b COPYING.lgplv3
  28. * 13aa749a5b0a454917a944ed8fffc530b784f5ead522b1aacaf4ec8aa55a6239 COPYING.mbsd
  29. */
  30. /*=export_func optionShowRange
  31. * private:
  32. *
  33. * what: Show info about range constraints
  34. * arg: + tOptions* + pOpts + program options descriptor +
  35. * arg: + tOptDesc* + pOptDesc + the descriptor for this arg +
  36. * arg: + void * + rng_table + the value range tables +
  37. * arg: + int + rng_count + the number of entries +
  38. *
  39. * doc:
  40. * Show information about a numeric option with range constraints.
  41. =*/
  42. void
  43. optionShowRange(tOptions * pOpts, tOptDesc * pOD, void * rng_table, int rng_ct)
  44. {
  45. const struct {long const rmin, rmax;} * rng = rng_table;
  46. char const * pz_indent = zTabHyp + tab_skip_ct;
  47. /*
  48. * The range is shown only for full usage requests and an error
  49. * in this particular option.
  50. */
  51. if (pOpts != OPTPROC_EMIT_USAGE) {
  52. if (pOpts <= OPTPROC_EMIT_LIMIT)
  53. return;
  54. fprintf(option_usage_fp, zRangeErr, pOpts->pzProgName,
  55. pOD->pz_Name, pOD->optArg.argInt);
  56. pz_indent = "";
  57. }
  58. if (pOD->fOptState & OPTST_SCALED_NUM)
  59. fprintf(option_usage_fp, zRangeScaled, pz_indent);
  60. fprintf(option_usage_fp, (rng_ct > 1) ? zRangeLie : zRangeOnly, pz_indent);
  61. pz_indent = (pOpts != OPTPROC_EMIT_USAGE)
  62. ? ONE_TAB_STR
  63. : (zTabSpace + tab_skip_ct);
  64. for (;;) {
  65. if (rng->rmax == LONG_MIN)
  66. fprintf(option_usage_fp, zRangeExact, pz_indent, rng->rmin);
  67. else if (rng->rmin == LONG_MIN)
  68. fprintf(option_usage_fp, zRangeUpto, pz_indent, rng->rmax);
  69. else if (rng->rmax == LONG_MAX)
  70. fprintf(option_usage_fp, zRangeAbove, pz_indent, rng->rmin);
  71. else
  72. fprintf(option_usage_fp, zRange, pz_indent, rng->rmin,
  73. rng->rmax);
  74. if (--rng_ct <= 0) {
  75. fputc(NL, option_usage_fp);
  76. break;
  77. }
  78. fputs(zRangeOr, option_usage_fp);
  79. rng++;
  80. }
  81. if (pOpts > OPTPROC_EMIT_LIMIT)
  82. pOpts->pUsageProc(pOpts, EXIT_FAILURE);
  83. }
  84. /*=export_func optionNumericVal
  85. * private:
  86. *
  87. * what: process an option with a numeric value.
  88. * arg: + tOptions* + opts + program options descriptor +
  89. * arg: + tOptDesc* + od + the descriptor for this arg +
  90. *
  91. * doc:
  92. * Decipher a numeric value.
  93. =*/
  94. void
  95. optionNumericVal(tOptions * opts, tOptDesc * od)
  96. {
  97. char* pz;
  98. long val;
  99. /*
  100. * Guard against all the different ways this procedure might get invoked
  101. * when there is no string argument provided.
  102. */
  103. if (INQUERY_CALL(opts, od) || (od->optArg.argString == NULL))
  104. return;
  105. /*
  106. * Numeric options may have a range associated with it.
  107. * If it does, the usage procedure requests that it be
  108. * emitted by passing a NULL od pointer. Also bail out
  109. * if there is no option argument or if we are being reset.
  110. */
  111. if ( (od == NULL)
  112. || (od->optArg.argString == NULL)
  113. || ((od->fOptState & OPTST_RESET) != 0))
  114. return;
  115. errno = 0;
  116. val = strtol(od->optArg.argString, &pz, 0);
  117. if ((pz == od->optArg.argString) || (errno != 0))
  118. goto bad_number;
  119. if ((od->fOptState & OPTST_SCALED_NUM) != 0)
  120. switch (*(pz++)) {
  121. case NUL: pz--; break;
  122. case 't': val *= 1000;
  123. case 'g': val *= 1000;
  124. case 'm': val *= 1000;
  125. case 'k': val *= 1000; break;
  126. case 'T': val *= 1024;
  127. case 'G': val *= 1024;
  128. case 'M': val *= 1024;
  129. case 'K': val *= 1024; break;
  130. default: goto bad_number;
  131. }
  132. if (*pz != NUL)
  133. goto bad_number;
  134. if (od->fOptState & OPTST_ALLOC_ARG) {
  135. AGFREE(od->optArg.argString);
  136. od->fOptState &= ~OPTST_ALLOC_ARG;
  137. }
  138. od->optArg.argInt = val;
  139. return;
  140. bad_number:
  141. fprintf( stderr, zNotNumber, opts->pzProgName, od->optArg.argString );
  142. if ((opts->fOptSet & OPTPROC_ERRSTOP) != 0)
  143. (*(opts->pUsageProc))(opts, EXIT_FAILURE);
  144. errno = EINVAL;
  145. od->optArg.argInt = ~0;
  146. }
  147. /** @}
  148. *
  149. * Local Variables:
  150. * mode: C
  151. * c-file-style: "stroustrup"
  152. * indent-tabs-mode: nil
  153. * End:
  154. * end of autoopts/numeric.c */