Coverage Report

Created: 2023-05-19 06:16

/src/ntp-dev/sntp/libopts/init.c
Line
Count
Source (jump to first uncovered line)
1
/**
2
 * \file initialize.c
3
 *
4
 *  initialize the libopts data structures.
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-2015 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
31
/* = = = START-STATIC-FORWARD = = = */
32
static tSuccess
33
do_presets(tOptions * opts);
34
/* = = = END-STATIC-FORWARD = = = */
35
36
/**
37
 *  Make sure the option descriptor is there and that we understand it.
38
 *  This should be called from any user entry point where one needs to
39
 *  worry about validity.  (Some entry points are free to assume that
40
 *  the call is not the first to the library and, thus, that this has
41
 *  already been called.)
42
 *
43
 *  Upon successful completion, pzProgName and pzProgPath are set.
44
 *
45
 *  @param[in,out] opts   program options descriptor
46
 *  @param[in]     pname  name of program, from argv[]
47
 *  @returns SUCCESS or FAILURE
48
 */
49
LOCAL tSuccess
50
validate_struct(tOptions * opts, char const * pname)
51
0
{
52
0
    if (opts == NULL) {
53
0
        fputs(zno_opt_arg, stderr);
54
0
        return FAILURE;
55
0
    }
56
0
    print_exit = ((opts->fOptSet & OPTPROC_SHELL_OUTPUT) != 0);
57
58
    /*
59
     *  IF the client has enabled translation and the translation procedure
60
     *  is available, then go do it.
61
     */
62
0
    if (  ((opts->fOptSet & OPTPROC_TRANSLATE) != 0)
63
0
       && (opts->pTransProc != NULL)
64
0
       && (option_xlateable_txt.field_ct != 0) ) {
65
        /*
66
         *  If option names are not to be translated at all, then do not do
67
         *  it for configuration parsing either.  (That is the bit that really
68
         *  gets tested anyway.)
69
         */
70
0
        if ((opts->fOptSet & OPTPROC_NO_XLAT_MASK) == OPTPROC_NXLAT_OPT)
71
0
            opts->fOptSet |= OPTPROC_NXLAT_OPT_CFG;
72
0
        opts->pTransProc();
73
0
    }
74
75
    /*
76
     *  IF the struct version is not the current, and also
77
     *     either too large (?!) or too small,
78
     *  THEN emit error message and fail-exit
79
     */
80
0
    if (  ( opts->structVersion  != OPTIONS_STRUCT_VERSION  )
81
0
       && (  (opts->structVersion > OPTIONS_STRUCT_VERSION  )
82
0
          || (opts->structVersion < OPTIONS_MINIMUM_VERSION )
83
0
       )  )  {
84
0
        fprintf(stderr, zwrong_ver, pname, NUM_TO_VER(opts->structVersion));
85
0
        if (opts->structVersion > OPTIONS_STRUCT_VERSION )
86
0
            fputs(ztoo_new, stderr);
87
0
        else
88
0
            fputs(ztoo_old, stderr);
89
90
0
        fwrite(ao_ver_string, sizeof(ao_ver_string) - 1, 1, stderr);
91
0
        return FAILURE;
92
0
    }
93
94
    /*
95
     *  If the program name hasn't been set, then set the name and the path
96
     *  and the set of equivalent characters.
97
     */
98
0
    if (opts->pzProgName == NULL) {
99
0
        char const *  pz = strrchr(pname, DIRCH);
100
0
        char const ** pp =
101
0
            (char const **)(void **)&(opts->pzProgName);
102
103
0
        if (pz != NULL)
104
0
            *pp = pz+1;
105
0
        else
106
0
            *pp = pname;
107
108
0
        pz = pathfind(getenv("PATH"), (char *)pname, "rx");
109
0
        if (pz != NULL)
110
0
            pname = VOIDP(pz);
111
112
0
        pp  = (char const **)VOIDP(&(opts->pzProgPath));
113
0
        *pp = pname;
114
115
        /*
116
         *  when comparing long names, these are equivalent
117
         */
118
0
        strequate(zSepChars);
119
0
    }
120
121
0
    return SUCCESS;
122
0
}
123
124
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
125
 *
126
 *  DO PRESETS
127
 *
128
 *  The next several routines do the immediate action pass on the command
129
 *  line options, then the environment variables, then the config files in
130
 *  reverse order.  Once done with that, the order is reversed and all
131
 *  the config files and environment variables are processed again, this
132
 *  time only processing the non-immediate action options.  do_presets()
133
 *  will then return for optionProcess() to do the final pass on the command
134
 *  line arguments.
135
 */
136
137
/**
138
 *  scan the command line for immediate action options.
139
 *  This is only called the first time through.
140
 *  While this procedure is active, the OPTPROC_IMMEDIATE is true.
141
 *
142
 *  @param pOpts   program options descriptor
143
 *  @returns SUCCESS or FAILURE
144
 */
145
LOCAL tSuccess
146
immediate_opts(tOptions * opts)
147
0
{
148
0
    tSuccess  res;
149
150
0
    opts->fOptSet  |= OPTPROC_IMMEDIATE;
151
0
    opts->curOptIdx = 1;     /* start by skipping program name */
152
0
    opts->pzCurOpt  = NULL;
153
154
    /*
155
     *  Examine all the options from the start.  We process any options that
156
     *  are marked for immediate processing.
157
     */
158
0
    for (;;) {
159
0
        tOptState opt_st = OPTSTATE_INITIALIZER(PRESET);
160
161
0
        res = next_opt(opts, &opt_st);
162
0
        switch (res) {
163
0
        case FAILURE: goto   failed_option;
164
0
        case PROBLEM: res = SUCCESS; goto leave;
165
0
        case SUCCESS: break;
166
0
        }
167
168
        /*
169
         *  IF this is an immediate-attribute option, then do it.
170
         */
171
0
        if (! DO_IMMEDIATELY(opt_st.flags))
172
0
            continue;
173
174
0
        if (! SUCCESSFUL(handle_opt(opts, &opt_st)))
175
0
            break;
176
0
    } failed_option:;
177
178
0
    if ((opts->fOptSet & OPTPROC_ERRSTOP) != 0)
179
0
        (*opts->pUsageProc)(opts, EXIT_FAILURE);
180
181
0
 leave:
182
183
0
    opts->fOptSet &= ~OPTPROC_IMMEDIATE;
184
0
    return res;
185
0
}
186
187
/**
188
 *  check for preset values from a config files or envrionment variables
189
 *
190
 * @param[in,out] opts  the structure with the option names to check
191
 */
192
static tSuccess
193
do_presets(tOptions * opts)
194
0
{
195
0
    tOptDesc * od = NULL;
196
197
0
    if (! SUCCESSFUL(immediate_opts(opts)))
198
0
        return FAILURE;
199
200
    /*
201
     *  IF this option set has a --save-opts option, then it also
202
     *  has a --load-opts option.  See if a command line option has disabled
203
     *  option presetting.
204
     */
205
0
    if (  (opts->specOptIdx.save_opts != NO_EQUIVALENT)
206
0
       && (opts->specOptIdx.save_opts != 0)) {
207
0
        od = opts->pOptDesc + opts->specOptIdx.save_opts + 1;
208
0
        if (DISABLED_OPT(od))
209
0
            return SUCCESS;
210
0
    }
211
212
    /*
213
     *  Until we return from this procedure, disable non-presettable opts
214
     */
215
0
    opts->fOptSet |= OPTPROC_PRESETTING;
216
    /*
217
     *  IF there are no config files,
218
     *  THEN do any environment presets and leave.
219
     */
220
0
    if (opts->papzHomeList == NULL) {
221
0
        env_presets(opts, ENV_ALL);
222
0
    }
223
0
    else {
224
0
        env_presets(opts, ENV_IMM);
225
226
        /*
227
         *  Check to see if environment variables have disabled presetting.
228
         */
229
0
        if ((od != NULL) && ! DISABLED_OPT(od))
230
0
            intern_file_load(opts);
231
232
        /*
233
         *  ${PROGRAM_LOAD_OPTS} value of "no" cannot disable other environment
234
         *  variable options.  Only the loading of .rc files.
235
         */
236
0
        env_presets(opts, ENV_NON_IMM);
237
0
    }
238
0
    opts->fOptSet &= ~OPTPROC_PRESETTING;
239
240
0
    return SUCCESS;
241
0
}
242
243
/**
244
 * AutoOpts initialization
245
 *
246
 * @param[in,out] opts  the structure to initialize
247
 * @param[in]     a_ct  program argument count
248
 * @param[in]     a_v   program argument vector
249
 */
250
LOCAL bool
251
ao_initialize(tOptions * opts, int a_ct, char ** a_v)
252
0
{
253
0
    if ((opts->fOptSet & OPTPROC_INITDONE) != 0)
254
0
        return true;
255
256
0
    opts->origArgCt   = (unsigned int)a_ct;
257
0
    opts->origArgVect = a_v;
258
0
    opts->fOptSet    |= OPTPROC_INITDONE;
259
260
0
    if (HAS_pzPkgDataDir(opts))
261
0
        program_pkgdatadir = opts->pzPkgDataDir;
262
263
0
    if (! SUCCESSFUL(do_presets(opts)))
264
0
        return false;
265
266
    /*
267
     *  IF option name conversion was suppressed but it is not suppressed
268
     *  for the command line, then it's time to translate option names.
269
     *  Usage text will not get retranslated.
270
     */
271
0
    if (  ((opts->fOptSet & OPTPROC_TRANSLATE) != 0)
272
0
       && (opts->pTransProc != NULL)
273
0
       && ((opts->fOptSet & OPTPROC_NO_XLAT_MASK) == OPTPROC_NXLAT_OPT_CFG)
274
0
       )  {
275
0
        opts->fOptSet &= ~OPTPROC_NXLAT_OPT_CFG;
276
0
        (*opts->pTransProc)();
277
0
    }
278
279
0
    if ((opts->fOptSet & OPTPROC_REORDER) != 0)
280
0
        optionSort(opts);
281
282
0
    opts->curOptIdx   = 1;
283
0
    opts->pzCurOpt    = NULL;
284
0
    return true;
285
0
}
286
287
/** @}
288
 *
289
 * Local Variables:
290
 * mode: C
291
 * c-file-style: "stroustrup"
292
 * indent-tabs-mode: nil
293
 * End:
294
 * end of autoopts/initialize.c */