5 * Handle options with numeric (integer) arguments.
11 * This file is part of AutoOpts, a companion to AutoGen.
12 * AutoOpts is free software.
13 * AutoOpts is Copyright (C) 1992-2015 by Bruce Korb - all rights reserved
15 * AutoOpts is available under any one of two licenses. The license
16 * in use must be one of these two and the choice is under the control
17 * of the user of the license.
19 * The GNU Lesser General Public License, version 3 or later
20 * See the files "COPYING.lgplv3" and "COPYING.gplv3"
22 * The Modified Berkeley Software Distribution License
23 * See the file "COPYING.mbsd"
25 * These files have the following sha256 sums:
27 * 8584710e9b04216a394078dc156b781d0b47e1729104d666658aecef8ee32e95 COPYING.gplv3
28 * 4379e7444a0e2ce2b12dd6f5a52a27a4d02d39d247901d3285c88cf0d37f477b COPYING.lgplv3
29 * 13aa749a5b0a454917a944ed8fffc530b784f5ead522b1aacaf4ec8aa55a6239 COPYING.mbsd
32 /*=export_func optionShowRange
35 * what: Show info about range constraints
36 * arg: + tOptions * + pOpts + program options descriptor +
37 * arg: + tOptDesc * + pOptDesc + the descriptor for this arg +
38 * arg: + void * + rng_table + the value range tables +
39 * arg: + int + rng_count + the number of entries +
42 * Show information about a numeric option with range constraints.
45 optionShowRange(tOptions * pOpts, tOptDesc * pOD, void * rng_table, int rng_ct)
47 const struct {long const rmin, rmax;} * rng = rng_table;
49 char const * pz_indent = zTabHyp + tab_skip_ct;
52 * The range is shown only for full usage requests and an error
53 * in this particular option.
55 if (pOpts != OPTPROC_EMIT_USAGE) {
56 if (pOpts <= OPTPROC_EMIT_LIMIT)
58 pz_indent = ONE_TAB_STR;
60 fprintf(option_usage_fp, zRangeErr, pOpts->pzProgName,
61 pOD->pz_Name, pOD->optArg.argInt);
65 if (pOD->fOptState & OPTST_SCALED_NUM)
66 fprintf(option_usage_fp, zRangeScaled, pz_indent);
68 fprintf(option_usage_fp, (rng_ct > 1) ? zRangeLie : zRangeOnly, pz_indent);
69 pz_indent = (pOpts != OPTPROC_EMIT_USAGE)
71 : (zTabSpace + tab_skip_ct);
74 if (rng->rmax == LONG_MIN)
75 fprintf(option_usage_fp, zRangeExact, pz_indent, rng->rmin);
76 else if (rng->rmin == LONG_MIN)
77 fprintf(option_usage_fp, zRangeUpto, pz_indent, rng->rmax);
78 else if (rng->rmax == LONG_MAX)
79 fprintf(option_usage_fp, zRangeAbove, pz_indent, rng->rmin);
81 fprintf(option_usage_fp, zRange, pz_indent, rng->rmin,
85 fputc(NL, option_usage_fp);
88 fputs(zRangeOr, option_usage_fp);
92 if (pOpts > OPTPROC_EMIT_LIMIT)
93 pOpts->pUsageProc(pOpts, EXIT_FAILURE);
96 /*=export_func optionNumericVal
99 * what: process an option with a numeric value.
100 * arg: + tOptions * + opts + program options descriptor +
101 * arg: + tOptDesc * + od + the descriptor for this arg +
104 * Decipher a numeric value.
107 optionNumericVal(tOptions * opts, tOptDesc * od)
113 * Guard against all the different ways this procedure might get invoked
114 * when there is no string argument provided.
116 if (INQUERY_CALL(opts, od) || (od->optArg.argString == NULL))
120 * Numeric options may have a range associated with it.
121 * If it does, the usage procedure requests that it be
122 * emitted by passing a NULL od pointer. Also bail out
123 * if there is no option argument or if we are being reset.
126 || (od->optArg.argString == NULL)
127 || ((od->fOptState & OPTST_RESET) != 0)
128 || (opts <= OPTPROC_EMIT_LIMIT))
132 val = strtol(od->optArg.argString, &pz, 0);
133 if ((pz == od->optArg.argString) || (errno != 0))
136 if ((od->fOptState & OPTST_SCALED_NUM) != 0)
138 case NUL: pz--; break;
139 case 't': val *= 1000;
140 case 'g': val *= 1000;
141 case 'm': val *= 1000;
142 case 'k': val *= 1000; break;
144 case 'T': val *= 1024;
145 case 'G': val *= 1024;
146 case 'M': val *= 1024;
147 case 'K': val *= 1024; break;
149 default: goto bad_number;
155 if (od->fOptState & OPTST_ALLOC_ARG) {
156 AGFREE(od->optArg.argString);
157 od->fOptState &= ~OPTST_ALLOC_ARG;
160 od->optArg.argInt = val;
165 fprintf( stderr, zNotNumber, opts->pzProgName, od->optArg.argString );
166 if ((opts->fOptSet & OPTPROC_ERRSTOP) != 0)
167 (*(opts->pUsageProc))(opts, EXIT_FAILURE);
170 od->optArg.argInt = ~0;
177 * c-file-style: "stroustrup"
178 * indent-tabs-mode: nil
180 * end of autoopts/numeric.c */