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