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