argmatch.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /* argmatch.h -- definitions and prototypes for argmatch.c
  2. Copyright (C) 1990, 1998-1999, 2001-2002, 2004-2005, 2009-2013 Free Software
  3. Foundation, Inc.
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. /* Written by David MacKenzie <djm@ai.mit.edu>
  15. Modified by Akim Demaille <demaille@inf.enst.fr> */
  16. #ifndef ARGMATCH_H_
  17. # define ARGMATCH_H_ 1
  18. # include <stddef.h>
  19. # include "verify.h"
  20. #ifdef __cplusplus
  21. extern "C" {
  22. #endif
  23. # define ARRAY_CARDINALITY(Array) (sizeof (Array) / sizeof *(Array))
  24. /* Assert there are as many real arguments as there are values
  25. (argument list ends with a NULL guard). */
  26. # define ARGMATCH_VERIFY(Arglist, Vallist) \
  27. verify (ARRAY_CARDINALITY (Arglist) == ARRAY_CARDINALITY (Vallist) + 1)
  28. /* Return the index of the element of ARGLIST (NULL terminated) that
  29. matches with ARG. If VALLIST is not NULL, then use it to resolve
  30. false ambiguities (i.e., different matches of ARG but corresponding
  31. to the same values in VALLIST). */
  32. ptrdiff_t argmatch (char const *arg, char const *const *arglist,
  33. char const *vallist, size_t valsize) _GL_ATTRIBUTE_PURE;
  34. # define ARGMATCH(Arg, Arglist, Vallist) \
  35. argmatch (Arg, Arglist, (char const *) (Vallist), sizeof *(Vallist))
  36. /* xargmatch calls this function when it fails. This function should not
  37. return. By default, this is a function that calls ARGMATCH_DIE which
  38. in turn defaults to 'exit (exit_failure)'. */
  39. typedef void (*argmatch_exit_fn) (void);
  40. extern argmatch_exit_fn argmatch_die;
  41. /* Report on stderr why argmatch failed. Report correct values. */
  42. void argmatch_invalid (char const *context, char const *value,
  43. ptrdiff_t problem);
  44. /* Left for compatibility with the old name invalid_arg */
  45. # define invalid_arg(Context, Value, Problem) \
  46. argmatch_invalid (Context, Value, Problem)
  47. /* Report on stderr the list of possible arguments. */
  48. void argmatch_valid (char const *const *arglist,
  49. char const *vallist, size_t valsize);
  50. # define ARGMATCH_VALID(Arglist, Vallist) \
  51. argmatch_valid (Arglist, (char const *) (Vallist), sizeof *(Vallist))
  52. /* Same as argmatch, but upon failure, report an explanation of the
  53. failure, and exit using the function EXIT_FN. */
  54. ptrdiff_t __xargmatch_internal (char const *context,
  55. char const *arg, char const *const *arglist,
  56. char const *vallist, size_t valsize,
  57. argmatch_exit_fn exit_fn);
  58. /* Programmer friendly interface to __xargmatch_internal. */
  59. # define XARGMATCH(Context, Arg, Arglist, Vallist) \
  60. ((Vallist) [__xargmatch_internal (Context, Arg, Arglist, \
  61. (char const *) (Vallist), \
  62. sizeof *(Vallist), \
  63. argmatch_die)])
  64. /* Convert a value into a corresponding argument. */
  65. char const *argmatch_to_argument (char const *value,
  66. char const *const *arglist,
  67. char const *vallist, size_t valsize)
  68. _GL_ATTRIBUTE_PURE;
  69. # define ARGMATCH_TO_ARGUMENT(Value, Arglist, Vallist) \
  70. argmatch_to_argument (Value, Arglist, \
  71. (char const *) (Vallist), sizeof *(Vallist))
  72. #ifdef __cplusplus
  73. }
  74. #endif
  75. #endif /* ARGMATCH_H_ */