Coverage Report

Created: 2023-05-19 06:16

/src/ntp-dev/sntp/libopts/numeric.c
Line
Count
Source (jump to first uncovered line)
1
2
/**
3
 * \file numeric.c
4
 *
5
 * Handle options with numeric (integer) arguments.
6
 *
7
 * @addtogroup autoopts
8
 * @{
9
 */
10
/*
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
14
 *
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.
18
 *
19
 *   The GNU Lesser General Public License, version 3 or later
20
 *      See the files "COPYING.lgplv3" and "COPYING.gplv3"
21
 *
22
 *   The Modified Berkeley Software Distribution License
23
 *      See the file "COPYING.mbsd"
24
 *
25
 *  These files have the following sha256 sums:
26
 *
27
 *  8584710e9b04216a394078dc156b781d0b47e1729104d666658aecef8ee32e95  COPYING.gplv3
28
 *  4379e7444a0e2ce2b12dd6f5a52a27a4d02d39d247901d3285c88cf0d37f477b  COPYING.lgplv3
29
 *  13aa749a5b0a454917a944ed8fffc530b784f5ead522b1aacaf4ec8aa55a6239  COPYING.mbsd
30
 */
31
32
/*=export_func  optionShowRange
33
 * private:
34
 *
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       +
40
 *
41
 * doc:
42
 *   Show information about a numeric option with range constraints.
43
=*/
44
void
45
optionShowRange(tOptions * pOpts, tOptDesc * pOD, void * rng_table, int rng_ct)
46
0
{
47
0
    const struct {long const rmin, rmax;} * rng = rng_table;
48
49
0
    char const * pz_indent = zTabHyp + tab_skip_ct;
50
51
    /*
52
     * The range is shown only for full usage requests and an error
53
     * in this particular option.
54
     */
55
0
    if (pOpts != OPTPROC_EMIT_USAGE) {
56
0
        if (pOpts <= OPTPROC_EMIT_LIMIT)
57
0
            return;
58
0
        pz_indent = ONE_TAB_STR;
59
60
0
        fprintf(option_usage_fp, zRangeErr, pOpts->pzProgName,
61
0
                pOD->pz_Name, pOD->optArg.argInt);
62
0
        pz_indent = "";
63
0
    }
64
65
0
    if (pOD->fOptState & OPTST_SCALED_NUM)
66
0
        fprintf(option_usage_fp, zRangeScaled, pz_indent);
67
68
0
    fprintf(option_usage_fp, (rng_ct > 1) ? zRangeLie : zRangeOnly, pz_indent);
69
0
    pz_indent = (pOpts != OPTPROC_EMIT_USAGE)
70
0
        ? ONE_TAB_STR
71
0
        : (zTabSpace + tab_skip_ct);
72
73
0
    for (;;) {
74
0
        if (rng->rmax == LONG_MIN)
75
0
            fprintf(option_usage_fp, zRangeExact, pz_indent, rng->rmin);
76
0
        else if (rng->rmin == LONG_MIN)
77
0
            fprintf(option_usage_fp, zRangeUpto, pz_indent, rng->rmax);
78
0
        else if (rng->rmax == LONG_MAX)
79
0
            fprintf(option_usage_fp, zRangeAbove, pz_indent, rng->rmin);
80
0
        else
81
0
            fprintf(option_usage_fp, zRange, pz_indent, rng->rmin,
82
0
                    rng->rmax);
83
84
0
        if  (--rng_ct <= 0) {
85
0
            fputc(NL, option_usage_fp);
86
0
            break;
87
0
        }
88
0
        fputs(zRangeOr, option_usage_fp);
89
0
        rng++;
90
0
    }
91
92
0
    if (pOpts > OPTPROC_EMIT_LIMIT)
93
0
        pOpts->pUsageProc(pOpts, EXIT_FAILURE);
94
0
}
95
96
/*=export_func  optionNumericVal
97
 * private:
98
 *
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 +
102
 *
103
 * doc:
104
 *  Decipher a numeric value.
105
=*/
106
void
107
optionNumericVal(tOptions * opts, tOptDesc * od)
108
0
{
109
0
    char * pz;
110
0
    long   val;
111
112
    /*
113
     *  Guard against all the different ways this procedure might get invoked
114
     *  when there is no string argument provided.
115
     */
116
0
    if (INQUERY_CALL(opts, od) || (od->optArg.argString == NULL))
117
0
        return;
118
119
    /*
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.
124
     */
125
0
    if (  (od == NULL)
126
0
       || (od->optArg.argString == NULL)
127
0
       || ((od->fOptState & OPTST_RESET) != 0))
128
0
        return;
129
130
0
    errno = 0;
131
0
    val = strtol(od->optArg.argString, &pz, 0);
132
0
    if ((pz == od->optArg.argString) || (errno != 0))
133
0
        goto bad_number;
134
135
0
    if ((od->fOptState & OPTST_SCALED_NUM) != 0)
136
0
        switch (*(pz++)) {
137
0
        case NUL:  pz--; break;
138
0
        case 't':  val *= 1000;
139
0
        case 'g':  val *= 1000;
140
0
        case 'm':  val *= 1000;
141
0
        case 'k':  val *= 1000; break;
142
143
0
        case 'T':  val *= 1024;
144
0
        case 'G':  val *= 1024;
145
0
        case 'M':  val *= 1024;
146
0
        case 'K':  val *= 1024; break;
147
148
0
        default:   goto bad_number;
149
0
        }
150
151
0
    if (*pz != NUL)
152
0
        goto bad_number;
153
154
0
    if (od->fOptState & OPTST_ALLOC_ARG) {
155
0
        AGFREE(od->optArg.argString);
156
0
        od->fOptState &= ~OPTST_ALLOC_ARG;
157
0
    }
158
159
0
    od->optArg.argInt = val;
160
0
    return;
161
162
0
    bad_number:
163
164
0
    fprintf( stderr, zNotNumber, opts->pzProgName, od->optArg.argString );
165
0
    if ((opts->fOptSet & OPTPROC_ERRSTOP) != 0)
166
0
        (*(opts->pUsageProc))(opts, EXIT_FAILURE);
167
168
0
    errno = EINVAL;
169
0
    od->optArg.argInt = ~0;
170
0
}
171
172
/** @}
173
 *
174
 * Local Variables:
175
 * mode: C
176
 * c-file-style: "stroustrup"
177
 * indent-tabs-mode: nil
178
 * End:
179
 * end of autoopts/numeric.c */