xref: /freebsd-src/contrib/ntp/sntp/libopts/alias.c (revision a466cc55373fc3cf86837f09da729535b57e69a1)
12b15cb3dSCy Schubert 
22b15cb3dSCy Schubert /**
32b15cb3dSCy Schubert  * \file alias.c
42b15cb3dSCy Schubert  *
52b15cb3dSCy Schubert  * Handle options that are aliases for another option.
62b15cb3dSCy Schubert  *
72b15cb3dSCy Schubert  * @addtogroup autoopts
82b15cb3dSCy Schubert  * @{
92b15cb3dSCy Schubert  */
102b15cb3dSCy Schubert /*
112b15cb3dSCy Schubert  *  This routine will forward an option alias to the correct option code.
122b15cb3dSCy Schubert  *
132b15cb3dSCy Schubert  *  This file is part of AutoOpts, a companion to AutoGen.
142b15cb3dSCy Schubert  *  AutoOpts is free software.
15*a466cc55SCy Schubert  *  AutoOpts is Copyright (C) 1992-2018 by Bruce Korb - all rights reserved
162b15cb3dSCy Schubert  *
172b15cb3dSCy Schubert  *  AutoOpts is available under any one of two licenses.  The license
182b15cb3dSCy Schubert  *  in use must be one of these two and the choice is under the control
192b15cb3dSCy Schubert  *  of the user of the license.
202b15cb3dSCy Schubert  *
212b15cb3dSCy Schubert  *   The GNU Lesser General Public License, version 3 or later
222b15cb3dSCy Schubert  *      See the files "COPYING.lgplv3" and "COPYING.gplv3"
232b15cb3dSCy Schubert  *
242b15cb3dSCy Schubert  *   The Modified Berkeley Software Distribution License
252b15cb3dSCy Schubert  *      See the file "COPYING.mbsd"
262b15cb3dSCy Schubert  *
272b15cb3dSCy Schubert  *  These files have the following sha256 sums:
282b15cb3dSCy Schubert  *
292b15cb3dSCy Schubert  *  8584710e9b04216a394078dc156b781d0b47e1729104d666658aecef8ee32e95  COPYING.gplv3
302b15cb3dSCy Schubert  *  4379e7444a0e2ce2b12dd6f5a52a27a4d02d39d247901d3285c88cf0d37f477b  COPYING.lgplv3
312b15cb3dSCy Schubert  *  13aa749a5b0a454917a944ed8fffc530b784f5ead522b1aacaf4ec8aa55a6239  COPYING.mbsd
322b15cb3dSCy Schubert  */
332b15cb3dSCy Schubert 
34*a466cc55SCy Schubert static tSuccess
too_many_occurrences(tOptions * opts,tOptDesc * od)352b15cb3dSCy Schubert too_many_occurrences(tOptions * opts, tOptDesc * od)
362b15cb3dSCy Schubert {
372b15cb3dSCy Schubert     if ((opts->fOptSet & OPTPROC_ERRSTOP) != 0) {
382b15cb3dSCy Schubert         char const * eqv = (od->optEquivIndex != NO_EQUIVALENT) ? zequiv : zNil;
392b15cb3dSCy Schubert 
402b15cb3dSCy Schubert         fprintf(stderr, ztoo_often_fmt, opts->pzProgName);
412b15cb3dSCy Schubert 
422b15cb3dSCy Schubert         if (od->optMaxCt > 1)
432b15cb3dSCy Schubert             fprintf(stderr, zat_most, od->optMaxCt, od->pz_Name, eqv);
442b15cb3dSCy Schubert         else
452b15cb3dSCy Schubert             fprintf(stderr, zonly_one, od->pz_Name, eqv);
462b15cb3dSCy Schubert         (*opts->pUsageProc)(opts, EXIT_FAILURE);
472b15cb3dSCy Schubert         /* NOTREACHED */
482b15cb3dSCy Schubert     }
492b15cb3dSCy Schubert 
502b15cb3dSCy Schubert     return FAILURE;
512b15cb3dSCy Schubert }
522b15cb3dSCy Schubert 
532b15cb3dSCy Schubert /*=export_func  optionAlias
542b15cb3dSCy Schubert  * private:
552b15cb3dSCy Schubert  *
562b15cb3dSCy Schubert  * what:  relay an option to its alias
572b15cb3dSCy Schubert  * arg:   + tOptions *   + opts   + program options descriptor  +
582b15cb3dSCy Schubert  * arg:   + tOptDesc *   + old_od + the descriptor for this arg +
592b15cb3dSCy Schubert  * arg:   + unsigned int + alias  + the aliased-to option index +
602b15cb3dSCy Schubert  * ret-type: int
612b15cb3dSCy Schubert  *
622b15cb3dSCy Schubert  * doc:
632b15cb3dSCy Schubert  *  Handle one option as if it had been specified as another.  Exactly.
642b15cb3dSCy Schubert  *  Returns "-1" if the aliased-to option has appeared too many times.
652b15cb3dSCy Schubert =*/
662b15cb3dSCy Schubert int
optionAlias(tOptions * opts,tOptDesc * old_od,unsigned int alias)672b15cb3dSCy Schubert optionAlias(tOptions * opts, tOptDesc * old_od, unsigned int alias)
682b15cb3dSCy Schubert {
692b15cb3dSCy Schubert     tOptDesc * new_od;
702b15cb3dSCy Schubert 
712b15cb3dSCy Schubert     if (opts <= OPTPROC_EMIT_LIMIT)
722b15cb3dSCy Schubert         return 0;
732b15cb3dSCy Schubert 
742b15cb3dSCy Schubert     new_od = opts->pOptDesc + alias;
752b15cb3dSCy Schubert     if ((unsigned)opts->optCt <= alias) {
762b15cb3dSCy Schubert         fputs(zbad_alias_id, stderr);
772b15cb3dSCy Schubert         option_exits(EXIT_FAILURE);
782b15cb3dSCy Schubert     }
792b15cb3dSCy Schubert 
802b15cb3dSCy Schubert     /*
812b15cb3dSCy Schubert      *  Copy over the option instance flags
822b15cb3dSCy Schubert      */
832b15cb3dSCy Schubert     new_od->fOptState &= OPTST_PERSISTENT_MASK;
842b15cb3dSCy Schubert     new_od->fOptState |= (old_od->fOptState & ~OPTST_PERSISTENT_MASK);
852b15cb3dSCy Schubert     new_od->optArg.argString = old_od->optArg.argString;
862b15cb3dSCy Schubert 
872b15cb3dSCy Schubert     /*
882b15cb3dSCy Schubert      *  Keep track of count only for DEFINED (command line) options.
892b15cb3dSCy Schubert      *  IF we have too many, build up an error message and bail.
902b15cb3dSCy Schubert      */
912b15cb3dSCy Schubert     if (  (new_od->fOptState & OPTST_DEFINED)
922b15cb3dSCy Schubert        && (++new_od->optOccCt > new_od->optMaxCt)  )
932b15cb3dSCy Schubert         return too_many_occurrences(opts, new_od);
942b15cb3dSCy Schubert 
952b15cb3dSCy Schubert     /*
962b15cb3dSCy Schubert      *  Clear the state bits and counters
972b15cb3dSCy Schubert      */
982b15cb3dSCy Schubert     old_od->fOptState &= OPTST_PERSISTENT_MASK;
992b15cb3dSCy Schubert     old_od->optOccCt   = 0;
1002b15cb3dSCy Schubert 
1012b15cb3dSCy Schubert     /*
1022b15cb3dSCy Schubert      *  If there is a procedure to call, call it
1032b15cb3dSCy Schubert      */
1042b15cb3dSCy Schubert     if (new_od->pOptProc != NULL)
1052b15cb3dSCy Schubert         (*new_od->pOptProc)(opts, new_od);
1062b15cb3dSCy Schubert     return 0;
1072b15cb3dSCy Schubert }
1082b15cb3dSCy Schubert 
1092b15cb3dSCy Schubert /** @}
1102b15cb3dSCy Schubert  *
1112b15cb3dSCy Schubert  * Local Variables:
1122b15cb3dSCy Schubert  * mode: C
1132b15cb3dSCy Schubert  * c-file-style: "stroustrup"
1142b15cb3dSCy Schubert  * indent-tabs-mode: nil
1152b15cb3dSCy Schubert  * End:
1162b15cb3dSCy Schubert  * end of autoopts/alias.c */
117