xref: /freebsd/contrib/ntp/sntp/libopts/alias.c (revision 911f0260390e18cf85f3dbf2c719b593efdc1e3c)
1 
2 /**
3  * \file alias.c
4  *
5  * Handle options that are aliases for another option.
6  *
7  * @addtogroup autoopts
8  * @{
9  */
10 /*
11  *  This routine will forward an option alias to the correct option code.
12  *
13  *  This file is part of AutoOpts, a companion to AutoGen.
14  *  AutoOpts is free software.
15  *  AutoOpts is Copyright (C) 1992-2018 by Bruce Korb - all rights reserved
16  *
17  *  AutoOpts is available under any one of two licenses.  The license
18  *  in use must be one of these two and the choice is under the control
19  *  of the user of the license.
20  *
21  *   The GNU Lesser General Public License, version 3 or later
22  *      See the files "COPYING.lgplv3" and "COPYING.gplv3"
23  *
24  *   The Modified Berkeley Software Distribution License
25  *      See the file "COPYING.mbsd"
26  *
27  *  These files have the following sha256 sums:
28  *
29  *  8584710e9b04216a394078dc156b781d0b47e1729104d666658aecef8ee32e95  COPYING.gplv3
30  *  4379e7444a0e2ce2b12dd6f5a52a27a4d02d39d247901d3285c88cf0d37f477b  COPYING.lgplv3
31  *  13aa749a5b0a454917a944ed8fffc530b784f5ead522b1aacaf4ec8aa55a6239  COPYING.mbsd
32  */
33 
34 static tSuccess
35 too_many_occurrences(tOptions * opts, tOptDesc * od)
36 {
37     if ((opts->fOptSet & OPTPROC_ERRSTOP) != 0) {
38         char const * eqv = (od->optEquivIndex != NO_EQUIVALENT) ? zequiv : zNil;
39 
40         fprintf(stderr, ztoo_often_fmt, opts->pzProgName);
41 
42         if (od->optMaxCt > 1)
43             fprintf(stderr, zat_most, od->optMaxCt, od->pz_Name, eqv);
44         else
45             fprintf(stderr, zonly_one, od->pz_Name, eqv);
46         (*opts->pUsageProc)(opts, EXIT_FAILURE);
47         /* NOTREACHED */
48     }
49 
50     return FAILURE;
51 }
52 
53 /*=export_func  optionAlias
54  * private:
55  *
56  * what:  relay an option to its alias
57  * arg:   + tOptions *   + opts   + program options descriptor  +
58  * arg:   + tOptDesc *   + old_od + the descriptor for this arg +
59  * arg:   + unsigned int + alias  + the aliased-to option index +
60  * ret-type: int
61  *
62  * doc:
63  *  Handle one option as if it had been specified as another.  Exactly.
64  *  Returns "-1" if the aliased-to option has appeared too many times.
65 =*/
66 int
67 optionAlias(tOptions * opts, tOptDesc * old_od, unsigned int alias)
68 {
69     tOptDesc * new_od;
70 
71     if (opts <= OPTPROC_EMIT_LIMIT)
72         return 0;
73 
74     new_od = opts->pOptDesc + alias;
75     if ((unsigned)opts->optCt <= alias) {
76         fputs(zbad_alias_id, stderr);
77         option_exits(EXIT_FAILURE);
78     }
79 
80     /*
81      *  Copy over the option instance flags
82      */
83     new_od->fOptState &= OPTST_PERSISTENT_MASK;
84     new_od->fOptState |= (old_od->fOptState & ~OPTST_PERSISTENT_MASK);
85     new_od->optArg.argString = old_od->optArg.argString;
86 
87     /*
88      *  Keep track of count only for DEFINED (command line) options.
89      *  IF we have too many, build up an error message and bail.
90      */
91     if (  (new_od->fOptState & OPTST_DEFINED)
92        && (++new_od->optOccCt > new_od->optMaxCt)  )
93         return too_many_occurrences(opts, new_od);
94 
95     /*
96      *  Clear the state bits and counters
97      */
98     old_od->fOptState &= OPTST_PERSISTENT_MASK;
99     old_od->optOccCt   = 0;
100 
101     /*
102      *  If there is a procedure to call, call it
103      */
104     if (new_od->pOptProc != NULL)
105         (*new_od->pOptProc)(opts, new_od);
106     return 0;
107 }
108 
109 /** @}
110  *
111  * Local Variables:
112  * mode: C
113  * c-file-style: "stroustrup"
114  * indent-tabs-mode: nil
115  * End:
116  * end of autoopts/alias.c */
117