Coverage Report

Created: 2026-02-26 06:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ntp-dev/sntp/libopts/configfile.c
Line
Count
Source
1
/**
2
 * \file configfile.c
3
 *
4
 *  configuration/rc/ini file handling.
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-2018 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
/**
32
 *  Skip over some unknown attribute
33
 *  @param[in] txt   start of skpped text
34
 *  @returns   character after skipped text
35
 */
36
inline static char const *
37
skip_unkn(char const * txt)
38
0
{
39
0
    txt = BRK_END_XML_TOKEN_CHARS(txt);
40
0
    return (*txt == NUL) ? NULL : txt;
41
0
}
42
43
/*=export_func  configFileLoad
44
 *
45
 * what:  parse a configuration file
46
 * arg:   + char const * + fname + the file to load +
47
 *
48
 * ret_type:  const tOptionValue *
49
 * ret_desc:  An allocated, compound value structure
50
 *
51
 * doc:
52
 *  This routine will load a named configuration file and parse the
53
 *  text as a hierarchically valued option.  The option descriptor
54
 *  created from an option definition file is not used via this interface.
55
 *  The returned value is "named" with the input file name and is of
56
 *  type "@code{OPARG_TYPE_HIERARCHY}".  It may be used in calls to
57
 *  @code{optionGetValue()}, @code{optionNextValue()} and
58
 *  @code{optionUnloadNested()}.
59
 *
60
 * err:
61
 *  If the file cannot be loaded or processed, @code{NULL} is returned and
62
 *  @var{errno} is set.  It may be set by a call to either @code{open(2)}
63
 *  @code{mmap(2)} or other file system calls, or it may be:
64
 *  @itemize @bullet
65
 *  @item
66
 *  @code{ENOENT} - the file was not found.
67
 *  @item
68
 *  @code{ENOMSG} - the file was empty.
69
 *  @item
70
 *  @code{EINVAL} - the file contents are invalid -- not properly formed.
71
 *  @item
72
 *  @code{ENOMEM} - not enough memory to allocate the needed structures.
73
 *  @end itemize
74
=*/
75
const tOptionValue *
76
configFileLoad(char const * fname)
77
0
{
78
0
    tmap_info_t    cfgfile;
79
0
    tOptionValue * res = NULL;
80
0
    tOptionLoadMode save_mode = option_load_mode;
81
82
0
    char * txt = text_mmap(fname, PROT_READ, MAP_PRIVATE, &cfgfile);
83
84
0
    if (TEXT_MMAP_FAILED_ADDR(txt))
85
0
        return NULL; /* errno is set */
86
87
0
    option_load_mode = OPTION_LOAD_COOKED;
88
0
    res = optionLoadNested(txt, fname, strlen(fname));
89
90
0
    if (res == NULL) {
91
0
        int err = errno;
92
0
        text_munmap(&cfgfile);
93
0
        errno = err;
94
0
    } else
95
0
        text_munmap(&cfgfile);
96
97
0
    option_load_mode = save_mode;
98
0
    return res;
99
0
}
100
101
102
/*=export_func  optionFindValue
103
 *
104
 * what:  find a hierarcicaly valued option instance
105
 * arg:   + const tOptDesc * + odesc + an option with a nested arg type +
106
 * arg:   + char const *     + name  + name of value to find +
107
 * arg:   + char const *     + val   + the matching value    +
108
 *
109
 * ret_type:  const tOptionValue *
110
 * ret_desc:  a compound value structure
111
 *
112
 * doc:
113
 *  This routine will find an entry in a nested value option or configurable.
114
 *  It will search through the list and return a matching entry.
115
 *
116
 * err:
117
 *  The returned result is NULL and errno is set:
118
 *  @itemize @bullet
119
 *  @item
120
 *  @code{EINVAL} - the @code{pOptValue} does not point to a valid
121
 *  hierarchical option value.
122
 *  @item
123
 *  @code{ENOENT} - no entry matched the given name.
124
 *  @end itemize
125
=*/
126
const tOptionValue *
127
optionFindValue(const tOptDesc * odesc, char const * name, char const * val)
128
0
{
129
0
    const tOptionValue * res = NULL;
130
131
0
    if (  (odesc == NULL)
132
0
       || (OPTST_GET_ARGTYPE(odesc->fOptState) != OPARG_TYPE_HIERARCHY))  {
133
0
        errno = EINVAL;
134
0
    }
135
136
0
    else if (odesc->optCookie == NULL) {
137
0
        errno = ENOENT;
138
0
    }
139
140
0
    else do {
141
0
        tArgList * argl  = odesc->optCookie;
142
0
        int        argct = argl->useCt;
143
0
        void **    poptv = (void **)(argl->apzArgs);
144
145
0
        if (argct == 0) {
146
0
            errno = ENOENT;
147
0
            break;
148
0
        }
149
150
0
        if (name == NULL) {
151
0
            res = (tOptionValue *)*poptv;
152
0
            break;
153
0
        }
154
155
0
        while (--argct >= 0) {
156
0
            const tOptionValue * ov = *(poptv++);
157
0
            const tOptionValue * rv = optionGetValue(ov, name);
158
159
0
            if (rv == NULL)
160
0
                continue;
161
162
0
            if (val == NULL) {
163
0
                res = ov;
164
0
                break;
165
0
            }
166
0
        }
167
0
        if (res == NULL)
168
0
            errno = ENOENT;
169
0
    } while (false);
170
171
0
    return res;
172
0
}
173
174
175
/*=export_func  optionFindNextValue
176
 *
177
 * FIXME: the handling of 'pzName' and 'pzVal' is just wrong.
178
 *
179
 * what:  find a hierarcicaly valued option instance
180
 * arg:   + const tOptDesc * + odesc + an option with a nested arg type +
181
 * arg:   + const tOptionValue * + pPrevVal + the last entry +
182
 * arg:   + char const *     + name     + name of value to find +
183
 * arg:   + char const *     + value    + the matching value    +
184
 *
185
 * ret_type:  const tOptionValue *
186
 * ret_desc:  a compound value structure
187
 *
188
 * doc:
189
 *  This routine will find the next entry in a nested value option or
190
 *  configurable.  It will search through the list and return the next entry
191
 *  that matches the criteria.
192
 *
193
 * err:
194
 *  The returned result is NULL and errno is set:
195
 *  @itemize @bullet
196
 *  @item
197
 *  @code{EINVAL} - the @code{pOptValue} does not point to a valid
198
 *  hierarchical option value.
199
 *  @item
200
 *  @code{ENOENT} - no entry matched the given name.
201
 *  @end itemize
202
=*/
203
tOptionValue const *
204
optionFindNextValue(const tOptDesc * odesc, const tOptionValue * pPrevVal,
205
                    char const * pzName, char const * pzVal)
206
0
{
207
0
    bool old_found = false;
208
0
    tOptionValue * res = NULL;
209
210
0
    (void)pzName;
211
0
    (void)pzVal;
212
213
0
    if (  (odesc == NULL)
214
0
       || (OPTST_GET_ARGTYPE(odesc->fOptState) != OPARG_TYPE_HIERARCHY))  {
215
0
        errno = EINVAL;
216
0
    }
217
218
0
    else if (odesc->optCookie == NULL) {
219
0
        errno = ENOENT;
220
0
    }
221
222
0
    else do {
223
0
        tArgList * argl = odesc->optCookie;
224
0
        int        ct   = argl->useCt;
225
0
        void **   poptv = (void **)argl->apzArgs;
226
227
0
        while (--ct >= 0) {
228
0
            tOptionValue * pOV = *(poptv++);
229
0
            if (old_found) {
230
0
                res = pOV;
231
0
                break;
232
0
            }
233
0
            if (pOV == pPrevVal)
234
0
                old_found = true;
235
0
        }
236
0
        if (res == NULL)
237
0
            errno = ENOENT;
238
0
    } while (false);
239
240
0
    return res;
241
0
}
242
243
244
/*=export_func  optionGetValue
245
 *
246
 * what:  get a specific value from a hierarcical list
247
 * arg:   + const tOptionValue * + pOptValue + a hierarchcal value +
248
 * arg:   + char const *         + valueName + name of value to get +
249
 *
250
 * ret_type:  const tOptionValue *
251
 * ret_desc:  a compound value structure
252
 *
253
 * doc:
254
 *  This routine will find an entry in a nested value option or configurable.
255
 *  If "valueName" is NULL, then the first entry is returned.  Otherwise,
256
 *  the first entry with a name that exactly matches the argument will be
257
 *  returned.  If there is no matching value, NULL is returned and errno is
258
 *  set to ENOENT. If the provided option value is not a hierarchical value,
259
 *  NULL is also returned and errno is set to EINVAL.
260
 *
261
 * err:
262
 *  The returned result is NULL and errno is set:
263
 *  @itemize @bullet
264
 *  @item
265
 *  @code{EINVAL} - the @code{pOptValue} does not point to a valid
266
 *  hierarchical option value.
267
 *  @item
268
 *  @code{ENOENT} - no entry matched the given name.
269
 *  @end itemize
270
=*/
271
tOptionValue const *
272
optionGetValue(tOptionValue const * oov, char const * vname)
273
0
{
274
0
    tArgList *     arg_list;
275
0
    tOptionValue * res = NULL;
276
277
0
    if ((oov == NULL) || (oov->valType != OPARG_TYPE_HIERARCHY)) {
278
0
        errno = EINVAL;
279
0
        return res;
280
0
    }
281
0
    arg_list = oov->v.nestVal;
282
283
0
    if (arg_list->useCt > 0) {
284
0
        int     ct     = arg_list->useCt;
285
0
        void ** ovlist = (void **)(arg_list->apzArgs);
286
287
0
        if (vname == NULL) {
288
0
            res = (tOptionValue *)*ovlist;
289
290
0
        } else do {
291
0
            tOptionValue * opt_val = *(ovlist++);
292
0
            if (strcmp(opt_val->pzName, vname) == 0) {
293
0
                res = opt_val;
294
0
                break;
295
0
            }
296
0
        } while (--ct > 0);
297
0
    }
298
0
    if (res == NULL)
299
0
        errno = ENOENT;
300
0
    return res;
301
0
}
302
303
/*=export_func  optionNextValue
304
 *
305
 * what:  get the next value from a hierarchical list
306
 * arg:   + const tOptionValue * + pOptValue + a hierarchcal list value +
307
 * arg:   + const tOptionValue * + pOldValue + a value from this list   +
308
 *
309
 * ret_type:  const tOptionValue *
310
 * ret_desc:  a compound value structure
311
 *
312
 * doc:
313
 *  This routine will return the next entry after the entry passed in.  At the
314
 *  end of the list, NULL will be returned.  If the entry is not found on the
315
 *  list, NULL will be returned and "@var{errno}" will be set to EINVAL.
316
 *  The "@var{pOldValue}" must have been gotten from a prior call to this
317
 *  routine or to "@code{opitonGetValue()}".
318
 *
319
 * err:
320
 *  The returned result is NULL and errno is set:
321
 *  @itemize @bullet
322
 *  @item
323
 *  @code{EINVAL} - the @code{pOptValue} does not point to a valid
324
 *  hierarchical option value or @code{pOldValue} does not point to a
325
 *  member of that option value.
326
 *  @item
327
 *  @code{ENOENT} - the supplied @code{pOldValue} pointed to the last entry.
328
 *  @end itemize
329
=*/
330
tOptionValue const *
331
optionNextValue(tOptionValue const * ov_list,tOptionValue const * oov )
332
0
{
333
0
    tArgList *     arg_list;
334
0
    tOptionValue * res = NULL;
335
0
    int            err = EINVAL;
336
337
0
    if ((ov_list == NULL) || (ov_list->valType != OPARG_TYPE_HIERARCHY)) {
338
0
        errno = EINVAL;
339
0
        return NULL;
340
0
    }
341
0
    arg_list = ov_list->v.nestVal;
342
0
    {
343
0
        int     ct    = arg_list->useCt;
344
0
        void ** o_list = (void **)(arg_list->apzArgs);
345
346
0
        while (ct-- > 0) {
347
0
            tOptionValue * nov = *(o_list++);
348
0
            if (nov == oov) {
349
0
                if (ct == 0) {
350
0
                    err = ENOENT;
351
352
0
                } else {
353
0
                    err = 0;
354
0
                    res = (tOptionValue *)*o_list;
355
0
                }
356
0
                break;
357
0
            }
358
0
        }
359
0
    }
360
0
    if (err != 0)
361
0
        errno = err;
362
0
    return res;
363
0
}
364
365
/**
366
 *  Load a file containing presetting information (a configuration file).
367
 */
368
static void
369
file_preset(tOptions * opts, char const * fname, int dir)
370
0
{
371
0
    tmap_info_t       cfgfile;
372
0
    tOptState         optst = OPTSTATE_INITIALIZER(PRESET);
373
0
    opt_state_mask_t  st_flags = optst.flags;
374
0
    opt_state_mask_t  fl_save  = opts->fOptSet;
375
0
    char *            ftext =
376
0
        text_mmap(fname, PROT_READ|PROT_WRITE, MAP_PRIVATE, &cfgfile);
377
378
0
    if (TEXT_MMAP_FAILED_ADDR(ftext))
379
0
        return;
380
381
    /*
382
     * While processing config files, we ignore errors.
383
     */
384
0
    opts->fOptSet &= ~OPTPROC_ERRSTOP;
385
386
0
    if (dir == DIRECTION_CALLED) {
387
0
        st_flags = OPTST_DEFINED;
388
0
        dir   = DIRECTION_PROCESS;
389
0
    }
390
391
    /*
392
     *  IF this is called via "optionProcess", then we are presetting.
393
     *  This is the default and the PRESETTING bit will be set.
394
     *  If this is called via "optionFileLoad", then the bit is not set
395
     *  and we consider stuff set herein to be "set" by the client program.
396
     */
397
0
    if ((opts->fOptSet & OPTPROC_PRESETTING) == 0)
398
0
        st_flags = OPTST_SET;
399
400
0
    do  {
401
0
        optst.flags = st_flags;
402
0
        ftext = SPN_WHITESPACE_CHARS(ftext);
403
404
0
        if (IS_VAR_FIRST_CHAR(*ftext)) {
405
0
            ftext = handle_cfg(opts, &optst, ftext, dir);
406
407
0
        } else switch (*ftext) {
408
0
        case '<':
409
0
            if (IS_VAR_FIRST_CHAR(ftext[1]))
410
0
                ftext = handle_struct(opts, &optst, ftext, dir);
411
412
0
            else switch (ftext[1]) {
413
0
            case '?':
414
0
                ftext = handle_directive(opts, ftext);
415
0
                break;
416
417
0
            case '!':
418
0
                ftext = handle_comment(ftext);
419
0
                break;
420
421
0
            case '/':
422
0
                ftext = strchr(ftext + 2, '>');
423
0
                if (ftext++ != NULL)
424
0
                    break;
425
                /* FALLTHROUGH */
426
427
0
            default:
428
0
                ftext = NULL;
429
0
            }
430
0
            if (ftext == NULL)
431
0
                goto all_done;
432
0
            break;
433
434
0
        case '[':
435
0
            ftext = handle_section(opts, ftext);
436
0
            break;
437
438
0
        case '#':
439
0
            ftext = strchr(ftext + 1, NL);
440
0
            break;
441
442
0
        default:
443
0
            goto all_done; /* invalid format */
444
0
        }
445
0
    } while (ftext != NULL);
446
447
0
 all_done:
448
0
    text_munmap(&cfgfile);
449
0
    opts->fOptSet = fl_save;
450
0
}
451
452
/**
453
 *  "txt" points to a "<!" sequence.
454
 *  Theoretically, we should ensure that it begins with "<!--",
455
 *  but actually I don't care that much.  It ends with "-->".
456
 */
457
static char *
458
handle_comment(char * txt)
459
0
{
460
0
    char * pz = strstr(txt, "-->");
461
0
    if (pz != NULL)
462
0
        pz += 3;
463
0
    return pz;
464
0
}
465
466
/**
467
 *  "txt" points to the start of some value name.
468
 *  The end of the entry is the end of the line that is not preceded by
469
 *  a backslash escape character.  The string value is always processed
470
 *  in "cooked" mode.
471
 */
472
static char *
473
handle_cfg(tOptions * opts, tOptState * ost, char * txt, int dir)
474
0
{
475
0
    char * pzName = txt++;
476
0
    char * pzEnd  = strchr(txt, NL);
477
478
0
    if (pzEnd == NULL)
479
0
        return txt + strlen(txt);
480
481
0
    txt = SPN_VALUE_NAME_CHARS(txt);
482
0
    txt = SPN_WHITESPACE_CHARS(txt);
483
0
    if (txt > pzEnd) {
484
0
    name_only:
485
0
        *pzEnd++ = NUL;
486
0
        load_opt_line(opts, ost, pzName, dir, OPTION_LOAD_UNCOOKED);
487
0
        return pzEnd;
488
0
    }
489
490
    /*
491
     *  Either the first character after the name is a ':' or '=',
492
     *  or else we must have skipped over white space.  Anything else
493
     *  is an invalid format and we give up parsing the text.
494
     */
495
0
    if ((*txt == '=') || (*txt == ':')) {
496
0
        txt = SPN_WHITESPACE_CHARS(txt+1);
497
0
        if (txt > pzEnd)
498
0
            goto name_only;
499
0
    } else if (! IS_WHITESPACE_CHAR(txt[-1]))
500
0
        return NULL;
501
502
    /*
503
     *  IF the value is continued, remove the backslash escape and push "pzEnd"
504
     *  on to a newline *not* preceded by a backslash.
505
     */
506
0
    if (pzEnd[-1] == '\\') {
507
0
        char * pcD = pzEnd-1;
508
0
        char * pcS = pzEnd;
509
510
0
        for (;;) {
511
0
            char ch = *(pcS++);
512
0
            switch (ch) {
513
0
            case NUL:
514
0
                pcS = NULL;
515
                /* FALLTHROUGH */
516
517
0
            case NL:
518
0
                *pcD = NUL;
519
0
                pzEnd = pcS;
520
0
                goto copy_done;
521
522
0
            case '\\':
523
0
                if (*pcS == NL)
524
0
                    ch = *(pcS++);
525
                /* FALLTHROUGH */
526
0
            default:
527
0
                *(pcD++) = ch;
528
0
            }
529
0
        } copy_done:;
530
531
0
    } else {
532
        /*
533
         *  The newline was not preceded by a backslash.  NUL it out
534
         */
535
0
        *(pzEnd++) = NUL;
536
0
    }
537
538
    /*
539
     *  "pzName" points to what looks like text for one option/configurable.
540
     *  It is NUL terminated.  Process it.
541
     */
542
0
    load_opt_line(opts, ost, pzName, dir, OPTION_LOAD_UNCOOKED);
543
544
0
    return pzEnd;
545
0
}
546
547
/**
548
 *  "txt" points to a "<?" sequence.
549
 *  We handle "<?program" and "<?auto-options" directives.
550
 *  All others are treated as comments.
551
 *
552
 *  @param[in,out] opts  program option descriptor
553
 *  @param[in]     txt   scanning pointer
554
 *  @returns       the next character to look at
555
 */
556
static char *
557
handle_directive(tOptions * opts, char * txt)
558
0
{
559
0
#   define DIRECTIVE_TABLE                      \
560
0
    _dt_(zCfgProg,     program_directive)       \
561
0
    _dt_(zCfgAO_Flags, aoflags_directive)
562
563
0
    typedef char * (directive_func_t)(tOptions *, char *);
564
0
#   define _dt_(_s, _fn) _fn,
565
0
    static directive_func_t * dir_disp[] = {
566
0
        DIRECTIVE_TABLE
567
0
    };
568
0
#   undef  _dt_
569
570
0
#   define _dt_(_s, _fn) 1 +
571
0
    static int  const   dir_ct  = DIRECTIVE_TABLE 0;
572
0
    static char const * dir_names[DIRECTIVE_TABLE 0];
573
0
#   undef _dt_
574
575
0
    int    ix;
576
577
0
    if (dir_names[0] == NULL) {
578
0
        ix = 0;
579
0
#   define _dt_(_s, _fn) dir_names[ix++] = _s;
580
0
        DIRECTIVE_TABLE;
581
0
#   undef _dt_
582
0
    }
583
584
0
    for (ix = 0; ix < dir_ct; ix++) {
585
0
        size_t len = strlen(dir_names[ix]);
586
0
        if (  (strncmp(txt, dir_names[ix], len) == 0)
587
0
           && (! IS_VALUE_NAME_CHAR(txt[len])) )
588
0
            return dir_disp[ix](opts, txt + len);
589
0
    }
590
591
    /*
592
     *  We don't know what this is.  Skip it.
593
     */
594
0
    txt = strchr(txt+2, '>');
595
0
    if (txt != NULL)
596
0
        txt++;
597
0
    return txt;
598
0
#   undef DIRECTIVE_TABLE
599
0
}
600
601
/**
602
 *  handle AutoOpts mode flags.
603
 *
604
 *  @param[in,out] opts  program option descriptor
605
 *  @param[in]     txt   scanning pointer
606
 *  @returns       the next character to look at
607
 */
608
static char *
609
aoflags_directive(tOptions * opts, char * txt)
610
0
{
611
0
    char * pz;
612
613
0
    pz = SPN_WHITESPACE_CHARS(txt+1);
614
0
    txt = strchr(pz, '>');
615
0
    if (txt != NULL) {
616
617
0
        size_t len  = (unsigned)(txt - pz);
618
0
        char * ftxt = AGALOC(len + 1, "aoflags");
619
620
0
        memcpy(ftxt, pz, len);
621
0
        ftxt[len] = NUL;
622
0
        set_usage_flags(opts, ftxt);
623
0
        AGFREE(ftxt);
624
625
0
        txt++;
626
0
    }
627
628
0
    return txt;
629
0
}
630
631
/**
632
 * handle program segmentation of config file.
633
 *
634
 *  @param[in,out] opts  program option descriptor
635
 *  @param[in]     txt   scanning pointer
636
 *  @returns       the next character to look at
637
 */
638
static char *
639
program_directive(tOptions * opts, char * txt)
640
0
{
641
0
    size_t name_len = strlen(opts->pzProgName);
642
643
0
    for (;; txt += zCfgProg_LEN) {
644
0
        txt = SPN_WHITESPACE_CHARS(txt);
645
646
0
        if (  (strneqvcmp(txt, opts->pzProgName, (int)name_len) == 0)
647
0
           && (IS_END_XML_TOKEN_CHAR(txt[name_len])) )
648
649
0
            return txt + name_len;
650
651
0
        txt = strstr(txt, zCfgProg);
652
0
        if (txt == NULL)
653
0
            return txt;
654
0
    }
655
656
0
    for (;;) {
657
0
        if (*txt == NUL)
658
0
            return NULL;
659
660
0
        if (*(txt++) == '>')
661
0
            return txt;
662
0
    }
663
0
}
664
665
/**
666
 *  "txt" points to a '[' character.
667
 *  The "traditional" [PROG_NAME] segmentation of the config file.
668
 *  Do not ever mix with the "<?program prog-name>" variation.
669
 *  The templates reject program names over 16 characters.
670
 *
671
 *  @param[in,out] opts  program option descriptor
672
 *  @param[in]     txt   scanning pointer
673
 *  @returns       the next character to look at
674
 */
675
static char *
676
handle_section(tOptions * opts, char * txt)
677
0
{
678
0
    size_t len = strlen(opts->pzPROGNAME);
679
0
    if (   (strncmp(txt+1, opts->pzPROGNAME, len) == 0)
680
0
        && (txt[len+1] == ']'))
681
0
        return strchr(txt + len + 2, NL);
682
683
0
    if (len > 16)
684
0
        return NULL;
685
686
0
    {
687
0
        char z[24] = "[";
688
0
        memcpy(z+1, opts->pzPROGNAME, len);
689
0
        z[++len] = ']';
690
0
        z[++len] = NUL;
691
0
        txt = strstr(txt, z);
692
0
    }
693
694
0
    if (txt != NULL)
695
0
        txt = strchr(txt, NL);
696
0
    return txt;
697
0
}
698
699
/**
700
 * parse XML encodings
701
 */
702
static int
703
parse_xml_encoding(char ** ppz)
704
0
{
705
0
#   define XMLTABLE             \
706
0
        _xmlNm_(amp,   '&')     \
707
0
        _xmlNm_(lt,    '<')     \
708
0
        _xmlNm_(gt,    '>')     \
709
0
        _xmlNm_(ff,    '\f')    \
710
0
        _xmlNm_(ht,    '\t')    \
711
0
        _xmlNm_(cr,    '\r')    \
712
0
        _xmlNm_(vt,    '\v')    \
713
0
        _xmlNm_(bel,   '\a')    \
714
0
        _xmlNm_(nl,    NL)      \
715
0
        _xmlNm_(space, ' ')     \
716
0
        _xmlNm_(quot,  '"')     \
717
0
        _xmlNm_(apos,  '\'')
718
719
0
    static struct {
720
0
        char const * const  nm_str;
721
0
        unsigned short      nm_len;
722
0
        short               nm_val;
723
0
    } const xml_names[] = {
724
0
#   define _xmlNm_(_n, _v) { #_n ";", sizeof(#_n), _v },
725
0
        XMLTABLE
726
0
#   undef  _xmlNm_
727
0
#   undef XMLTABLE
728
0
    };
729
730
0
    static int const nm_ct = sizeof(xml_names) / sizeof(xml_names[0]);
731
0
    int    base = 10;
732
733
0
    char * pz = *ppz;
734
735
0
    if (*pz == '#') {
736
0
        pz++;
737
0
        goto parse_number;
738
0
    }
739
740
0
    if (IS_DEC_DIGIT_CHAR(*pz)) {
741
0
        unsigned long v;
742
743
0
    parse_number:
744
0
        switch (*pz) {
745
0
        case 'x': case 'X':
746
            /*
747
             * Some forms specify hex with:  &#xNN;
748
             */
749
0
            base = 16;
750
0
            pz++;
751
0
            break;
752
753
0
        case '0':
754
            /*
755
             *  &#0022; is hex and &#22; is decimal.  Cool.
756
             *  Ya gotta love it.
757
             */
758
0
            if (pz[1] == '0')
759
0
                base = 16;
760
0
            break;
761
0
        }
762
763
0
        v = strtoul(pz, &pz, base);
764
0
        if ((*pz != ';') || (v > 0x7F))
765
0
            return NUL;
766
0
        *ppz = pz + 1;
767
0
        return (int)v;
768
0
    }
769
770
0
    {
771
0
        int ix = 0;
772
0
        do  {
773
0
            if (strncmp(pz, xml_names[ix].nm_str, xml_names[ix].nm_len)
774
0
                == 0) {
775
0
                *ppz = pz + xml_names[ix].nm_len;
776
0
                return xml_names[ix].nm_val;
777
0
            }
778
0
        } while (++ix < nm_ct);
779
0
    }
780
781
0
    return NUL;
782
0
}
783
784
/**
785
 * Find the end marker for the named section of XML.
786
 * Trim that text there, trimming trailing white space for all modes
787
 * except for OPTION_LOAD_UNCOOKED.
788
 */
789
static char *
790
trim_xml_text(char * intxt, char const * pznm, tOptionLoadMode mode)
791
0
{
792
0
    size_t nm_len = strlen(pznm);
793
0
    char * etext;
794
795
0
    {
796
0
        char z[64], *pz = z;
797
798
0
        if (nm_len + 4 >= sizeof(z))
799
0
            pz = AGALOC(nm_len + 4, "scan name");
800
801
0
        pz[0] = '<';
802
0
        pz[1] = '/';
803
0
        memcpy(pz+2, pznm, nm_len);
804
0
        nm_len  += 2;
805
0
        pz[nm_len++] = '>';
806
0
        pz[nm_len]   = NUL;
807
808
0
        *intxt = ' ';
809
0
        etext = strstr(intxt, pz);
810
0
        if (pz != z) AGFREE(pz);
811
0
    }
812
813
0
    if (etext == NULL)
814
0
        return etext;
815
816
0
    {
817
0
        char * result = etext + nm_len;
818
819
0
        if (mode != OPTION_LOAD_UNCOOKED)
820
0
            etext = SPN_WHITESPACE_BACK(intxt, etext);
821
822
0
        *etext = NUL;
823
0
        return result;
824
0
    }
825
0
}
826
827
/**
828
 */
829
static void
830
cook_xml_text(char * pzData)
831
0
{
832
0
    char * pzs = pzData;
833
0
    char * pzd = pzData;
834
0
    char   bf[4];
835
0
    bf[2] = NUL;
836
837
0
    for (;;) {
838
0
        int ch = ((int)*(pzs++)) & 0xFF;
839
0
        switch (ch) {
840
0
        case NUL:
841
0
            *pzd = NUL;
842
0
            return;
843
844
0
        case '&':
845
0
            ch = parse_xml_encoding(&pzs);
846
0
            *(pzd++) = (char)ch;
847
0
            if (ch == NUL)
848
0
                return;
849
0
            break;
850
851
0
        case '%':
852
0
            bf[0] = *(pzs++);
853
0
            bf[1] = *(pzs++);
854
0
            if ((bf[0] == NUL) || (bf[1] == NUL)) {
855
0
                *pzd = NUL;
856
0
                return;
857
0
            }
858
859
0
            ch = (int)strtoul(bf, NULL, 16);
860
            /* FALLTHROUGH */
861
862
0
        default:
863
0
            *(pzd++) = (char)ch;
864
0
        }
865
0
    }
866
0
}
867
868
/**
869
 *  "txt" points to a '<' character, followed by an alpha.
870
 *  The end of the entry is either the "/>" following the name, or else a
871
 *  "</name>" string.
872
 */
873
static char *
874
handle_struct(tOptions * opts, tOptState * ost, char * txt, int dir)
875
0
{
876
0
    tOptionLoadMode mode = option_load_mode;
877
0
    tOptionValue    valu;
878
879
0
    char * pzName = ++txt;
880
0
    char * pzData;
881
0
    char * pcNulPoint;
882
883
0
    txt = SPN_VALUE_NAME_CHARS(txt);
884
0
    pcNulPoint = txt;
885
0
    valu.valType = OPARG_TYPE_STRING;
886
887
0
    switch (*txt) {
888
0
    case ' ':
889
0
    case '\t':
890
0
        txt = VOIDP(parse_attrs(
891
0
            opts, SPN_WHITESPACE_CHARS(txt), &mode, &valu));
892
0
        if (txt == NULL)
893
0
            return txt;
894
0
        if (*txt == '>')
895
0
            break;
896
0
        if (*txt != '/')
897
0
            return NULL;
898
        /* FALLTHROUGH */
899
900
0
    case '/':
901
0
        if (txt[1] != '>')
902
0
            return NULL;
903
0
        *txt = NUL;
904
0
        txt += 2;
905
0
        load_opt_line(opts, ost, pzName, dir, mode);
906
0
        return txt;
907
908
0
    case '>':
909
0
        break;
910
911
0
    default:
912
0
        txt = strchr(txt, '>');
913
0
        if (txt != NULL)
914
0
            txt++;
915
0
        return txt;
916
0
    }
917
918
    /*
919
     *  If we are here, we have a value.  "txt" points to a closing angle
920
     *  bracket.  Separate the name from the value for a moment.
921
     */
922
0
    *pcNulPoint = NUL;
923
0
    pzData = ++txt;
924
0
    txt = trim_xml_text(txt, pzName, mode);
925
0
    if (txt == NULL)
926
0
        return txt;
927
928
    /*
929
     *  Rejoin the name and value for parsing by "load_opt_line()".
930
     *  Erase any attributes parsed by "parse_attrs()".
931
     */
932
0
    memset(pcNulPoint, ' ', (size_t)(pzData - pcNulPoint));
933
934
    /*
935
     *  If we are getting a "string" value that is to be cooked,
936
     *  then process the XML-ish &xx; XML-ish and %XX hex characters.
937
     */
938
0
    if (  (valu.valType == OPARG_TYPE_STRING)
939
0
       && (mode == OPTION_LOAD_COOKED))
940
0
        cook_xml_text(pzData);
941
942
    /*
943
     *  "pzName" points to what looks like text for one option/configurable.
944
     *  It is NUL terminated.  Process it.
945
     */
946
0
    load_opt_line(opts, ost, pzName, dir, mode);
947
948
0
    return txt;
949
0
}
950
951
/**
952
 *  Load a configuration file.  This may be invoked either from
953
 *  scanning the "homerc" list, or from a specific file request.
954
 *  (see "optionFileLoad()", the implementation for --load-opts)
955
 */
956
static void
957
intern_file_load(tOptions * opts)
958
0
{
959
0
    uint32_t  svfl;
960
0
    int       idx;
961
0
    int       inc;
962
0
    char      f_name[ AG_PATH_MAX+1 ];
963
964
0
    if (opts->papzHomeList == NULL)
965
0
        return;
966
967
0
    svfl = opts->fOptSet;
968
0
    inc  = DIRECTION_PRESET;
969
970
    /*
971
     *  Never stop on errors in config files.
972
     */
973
0
    opts->fOptSet &= ~OPTPROC_ERRSTOP;
974
975
    /*
976
     *  Find the last RC entry (highest priority entry)
977
     */
978
0
    for (idx = 0; opts->papzHomeList[ idx+1 ] != NULL; ++idx)  ;
979
980
    /*
981
     *  For every path in the home list, ...  *TWICE* We start at the last
982
     *  (highest priority) entry, work our way down to the lowest priority,
983
     *  handling the immediate options.
984
     *  Then we go back up, doing the normal options.
985
     */
986
0
    for (;;) {
987
0
        struct stat sb;
988
0
        cch_t *  path;
989
990
        /*
991
         *  IF we've reached the bottom end, change direction
992
         */
993
0
        if (idx < 0) {
994
0
            inc = DIRECTION_PROCESS;
995
0
            idx = 0;
996
0
        }
997
998
0
        path = opts->papzHomeList[ idx ];
999
1000
        /*
1001
         *  IF we've reached the top end, bail out
1002
         */
1003
0
        if (path == NULL)
1004
0
            break;
1005
1006
0
        idx += inc;
1007
1008
0
        if (! optionMakePath(f_name, (int)sizeof(f_name),
1009
0
                             path, opts->pzProgPath))
1010
0
            continue;
1011
1012
        /*
1013
         *  IF the file name we constructed is a directory,
1014
         *  THEN append the Resource Configuration file name
1015
         *  ELSE we must have the complete file name
1016
         */
1017
0
        if (stat(f_name, &sb) != 0)
1018
0
            continue; /* bogus name - skip the home list entry */
1019
1020
0
        if (S_ISDIR(sb.st_mode)) {
1021
0
            size_t len = strlen(f_name);
1022
0
            size_t nln = strlen(opts->pzRcName) + 1;
1023
0
            char * pz  = f_name + len;
1024
1025
0
            if (len + 1 + nln >= sizeof(f_name))
1026
0
                continue;
1027
1028
0
            if (pz[-1] != DIRCH)
1029
0
                *(pz++) = DIRCH;
1030
0
            memcpy(pz, opts->pzRcName, nln);
1031
0
        }
1032
1033
0
        file_preset(opts, f_name, inc);
1034
1035
        /*
1036
         *  IF we are now to skip config files AND we are presetting,
1037
         *  THEN change direction.  We must go the other way.
1038
         */
1039
0
        {
1040
0
            tOptDesc * od = opts->pOptDesc + opts->specOptIdx.save_opts + 1;
1041
0
            if (DISABLED_OPT(od) && PRESETTING(inc)) {
1042
0
                idx -= inc;  /* go back and reprocess current file */
1043
0
                inc =  DIRECTION_PROCESS;
1044
0
            }
1045
0
        }
1046
0
    } /* twice for every path in the home list, ... */
1047
1048
0
    opts->fOptSet = svfl;
1049
0
}
1050
1051
/*=export_func optionFileLoad
1052
 *
1053
 * what: Load the locatable config files, in order
1054
 *
1055
 * arg:  + tOptions *   + opts + program options descriptor +
1056
 * arg:  + char const * + prog + program name +
1057
 *
1058
 * ret_type:  int
1059
 * ret_desc:  0 -> SUCCESS, -1 -> FAILURE
1060
 *
1061
 * doc:
1062
 *
1063
 * This function looks in all the specified directories for a configuration
1064
 * file ("rc" file or "ini" file) and processes any found twice.  The first
1065
 * time through, they are processed in reverse order (last file first).  At
1066
 * that time, only "immediate action" configurables are processed.  For
1067
 * example, if the last named file specifies not processing any more
1068
 * configuration files, then no more configuration files will be processed.
1069
 * Such an option in the @strong{first} named directory will have no effect.
1070
 *
1071
 * Once the immediate action configurables have been handled, then the
1072
 * directories are handled in normal, forward order.  In that way, later
1073
 * config files can override the settings of earlier config files.
1074
 *
1075
 * See the AutoOpts documentation for a thorough discussion of the
1076
 * config file format.
1077
 *
1078
 * Configuration files not found or not decipherable are simply ignored.
1079
 *
1080
 * err:  Returns the value, "-1" if the program options descriptor
1081
 *       is out of date or indecipherable.  Otherwise, the value "0" will
1082
 *       always be returned.
1083
=*/
1084
int
1085
optionFileLoad(tOptions * opts, char const * prog)
1086
0
{
1087
0
    if (! SUCCESSFUL(validate_struct(opts, prog)))
1088
0
        return -1;
1089
1090
    /*
1091
     * The pointer to the program name is "const".  However, the
1092
     * structure is in writable memory, so we coerce the address
1093
     * of this pointer to point to writable memory.
1094
     */
1095
0
    {
1096
0
        char const ** pp = VOIDP(&(opts->pzProgName));
1097
0
        *pp = prog;
1098
0
    }
1099
1100
0
    intern_file_load(opts);
1101
0
    return 0;
1102
0
}
1103
1104
/*=export_func  optionLoadOpt
1105
 * private:
1106
 *
1107
 * what:  Load an option rc/ini file
1108
 * arg:   + tOptions * + opts  + program options descriptor +
1109
 * arg:   + tOptDesc * + odesc + the descriptor for this arg +
1110
 *
1111
 * doc:
1112
 *  Processes the options found in the file named with
1113
 *  odesc->optArg.argString.
1114
=*/
1115
void
1116
optionLoadOpt(tOptions * opts, tOptDesc * odesc)
1117
0
{
1118
0
    struct stat sb;
1119
1120
0
    if (opts <= OPTPROC_EMIT_LIMIT)
1121
0
        return;
1122
1123
    /*
1124
     *  IF the option is not being disabled, THEN load the file.  There must
1125
     *  be a file.  (If it is being disabled, then the disablement processing
1126
     *  already took place.  It must be done to suppress preloading of ini/rc
1127
     *  files.)
1128
     */
1129
0
    if (  DISABLED_OPT(odesc)
1130
0
       || ((odesc->fOptState & OPTST_RESET) != 0))
1131
0
        return;
1132
1133
0
    if (stat(odesc->optArg.argString, &sb) != 0) {
1134
0
        if ((opts->fOptSet & OPTPROC_ERRSTOP) == 0)
1135
0
            return;
1136
1137
0
        fserr_exit(opts->pzProgName, "stat", odesc->optArg.argString);
1138
        /* NOT REACHED */
1139
0
    }
1140
1141
0
    if (! S_ISREG(sb.st_mode)) {
1142
0
        if ((opts->fOptSet & OPTPROC_ERRSTOP) == 0)
1143
0
            return;
1144
0
        errno = EINVAL;
1145
0
        fserr_exit(opts->pzProgName, "stat", odesc->optArg.argString);
1146
        /* NOT REACHED */
1147
0
    }
1148
1149
0
    file_preset(opts, odesc->optArg.argString, DIRECTION_CALLED);
1150
0
}
1151
1152
/**
1153
 *  Parse the various attributes of an XML-styled config file entry
1154
 *
1155
 * @returns NULL on failure, otherwise the scan point
1156
 */
1157
static char const *
1158
parse_attrs(tOptions * opts, char const * txt, tOptionLoadMode * pMode,
1159
            tOptionValue * pType)
1160
0
{
1161
0
    size_t len = 0;
1162
1163
0
    for (;;) {
1164
0
        len = (size_t)(SPN_LOWER_CASE_CHARS(txt) - txt);
1165
1166
        /*
1167
         * The enumeration used in this switch is derived from this switch
1168
         * statement itself.  The "find_option_xat_attribute_cmd" function
1169
         * will return XAT_CMD_MEMBERS for the "txt" string value
1170
         * "members", etc.
1171
         */
1172
0
        switch (find_option_xat_attribute_cmd(txt, len)) {
1173
0
        case XAT_CMD_TYPE:
1174
0
            txt = parse_value(txt+len, pType);
1175
0
            break;
1176
1177
0
        case XAT_CMD_WORDS:
1178
0
            txt = parse_keyword(opts, txt+len, pType);
1179
0
            break;
1180
1181
0
        case XAT_CMD_MEMBERS:
1182
0
            txt = parse_set_mem(opts, txt+len, pType);
1183
0
            break;
1184
1185
0
        case XAT_CMD_COOKED:
1186
0
            txt += len;
1187
0
            if (! IS_END_XML_TOKEN_CHAR(*txt))
1188
0
                goto invalid_kwd;
1189
1190
0
            *pMode = OPTION_LOAD_COOKED;
1191
0
            break;
1192
1193
0
        case XAT_CMD_UNCOOKED:
1194
0
            txt += len;
1195
0
            if (! IS_END_XML_TOKEN_CHAR(*txt))
1196
0
                goto invalid_kwd;
1197
1198
0
            *pMode = OPTION_LOAD_UNCOOKED;
1199
0
            break;
1200
1201
0
        case XAT_CMD_KEEP:
1202
0
            txt += len;
1203
0
            if (! IS_END_XML_TOKEN_CHAR(*txt))
1204
0
                goto invalid_kwd;
1205
1206
0
            *pMode = OPTION_LOAD_KEEP;
1207
0
            break;
1208
1209
0
        default:
1210
0
        case XAT_INVALID_CMD:
1211
0
        invalid_kwd:
1212
0
            pType->valType = OPARG_TYPE_NONE;
1213
0
            return skip_unkn(txt);
1214
0
        }
1215
1216
0
        if (txt == NULL)
1217
0
            return NULL;
1218
0
        txt = SPN_WHITESPACE_CHARS(txt);
1219
0
        switch (*txt) {
1220
0
            case '/': pType->valType = OPARG_TYPE_NONE;
1221
                      /* FALLTHROUGH */
1222
0
            case '>': return txt;
1223
0
        }
1224
0
        if (! IS_LOWER_CASE_CHAR(*txt))
1225
0
            return NULL;
1226
0
    }
1227
0
}
1228
1229
/**
1230
 *  "txt" points to the character after "words=".
1231
 *  What should follow is a name of a keyword (enumeration) list.
1232
 *
1233
 *  @param     opts  unused
1234
 *  @param[in] txt   keyword to skip over
1235
 *  @param     type  unused value type
1236
 *  @returns   pointer after skipped text
1237
 */
1238
static char const *
1239
parse_keyword(tOptions * opts, char const * txt, tOptionValue * typ)
1240
0
{
1241
0
    (void)opts;
1242
0
    (void)typ;
1243
1244
0
    return skip_unkn(txt);
1245
0
}
1246
1247
/**
1248
 *  "txt" points to the character after "members="
1249
 *  What should follow is a name of a "set membership".
1250
 *  A collection of bit flags.
1251
 *
1252
 *  @param     opts  unused
1253
 *  @param[in] txt   keyword to skip over
1254
 *  @param     type  unused value type
1255
 *  @returns   pointer after skipped text
1256
 */
1257
static char const *
1258
parse_set_mem(tOptions * opts, char const * txt, tOptionValue * typ)
1259
0
{
1260
0
    (void)opts;
1261
0
    (void)typ;
1262
1263
0
    return skip_unkn(txt);
1264
0
}
1265
1266
/**
1267
 *  parse the type.  The keyword "type" was found, now figure out
1268
 *  the type that follows the type.
1269
 *
1270
 *  @param[in]  txt  points to the '=' character after the "type" keyword.
1271
 *  @param[out] typ  where to store the type found
1272
 *  @returns    the next byte after the type name
1273
 */
1274
static char const *
1275
parse_value(char const * txt, tOptionValue * typ)
1276
0
{
1277
0
    size_t len = 0;
1278
1279
0
    if (*(txt++) != '=')
1280
0
        goto woops;
1281
1282
0
    len = (size_t)(SPN_OPTION_NAME_CHARS(txt) - txt);
1283
1284
0
    if ((len == 0) || (! IS_END_XML_TOKEN_CHAR(txt[len]))) {
1285
0
    woops:
1286
0
        typ->valType = OPARG_TYPE_NONE;
1287
0
        return skip_unkn(txt + len);
1288
0
    }
1289
1290
    /*
1291
     * The enumeration used in this switch is derived from this switch
1292
     * statement itself.  The "find_option_value_type_cmd" function
1293
     * will return VTP_CMD_INTEGER for the "txt" string value
1294
     * "integer", etc.
1295
     */
1296
0
    switch (find_option_value_type_cmd(txt, len)) {
1297
0
    default:
1298
0
    case VTP_INVALID_CMD: goto woops;
1299
1300
0
    case VTP_CMD_STRING:
1301
0
        typ->valType = OPARG_TYPE_STRING;
1302
0
        break;
1303
1304
0
    case VTP_CMD_INTEGER:
1305
0
        typ->valType = OPARG_TYPE_NUMERIC;
1306
0
        break;
1307
1308
0
    case VTP_CMD_BOOL:
1309
0
    case VTP_CMD_BOOLEAN:
1310
0
        typ->valType = OPARG_TYPE_BOOLEAN;
1311
0
        break;
1312
1313
0
    case VTP_CMD_KEYWORD:
1314
0
        typ->valType = OPARG_TYPE_ENUMERATION;
1315
0
        break;
1316
1317
0
    case VTP_CMD_SET:
1318
0
    case VTP_CMD_SET_MEMBERSHIP:
1319
0
        typ->valType = OPARG_TYPE_MEMBERSHIP;
1320
0
        break;
1321
1322
0
    case VTP_CMD_NESTED:
1323
0
    case VTP_CMD_HIERARCHY:
1324
0
        typ->valType = OPARG_TYPE_HIERARCHY;
1325
0
    }
1326
1327
0
    return txt + len;
1328
0
}
1329
1330
/** @}
1331
 *
1332
 * Local Variables:
1333
 * mode: C
1334
 * c-file-style: "stroustrup"
1335
 * indent-tabs-mode: nil
1336
 * End:
1337
 * end of autoopts/configfile.c */