alias.c 3.3 KB

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