1 /* argmatch.h -- definitions and prototypes for argmatch.c
3 Copyright (C) 1990, 1998, 1999, 2001, 2002, 2004 Free Software
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 2, or (at your option)
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.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software Foundation,
18 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20 /* Written by David MacKenzie <djm@ai.mit.edu>
21 Modified by Akim Demaille <demaille@inf.enst.fr> */
24 # define ARGMATCH_H_ 1
28 # define ARRAY_CARDINALITY(Array) (sizeof (Array) / sizeof *(Array))
30 # define ARGMATCH_CONSTRAINT(Arglist, Vallist) \
31 (ARRAY_CARDINALITY (Arglist) == ARRAY_CARDINALITY (Vallist) + 1)
33 /* Assert there are as many real arguments as there are values
34 (argument list ends with a NULL guard). ARGMATCH_VERIFY is
35 preferred, since it is guaranteed to be checked at compile-time.
36 ARGMATCH_ASSERT is for backward compatibility only. */
38 # define ARGMATCH_VERIFY(Arglist, Vallist) \
39 struct argmatch_verify \
41 char argmatch_verify[ARGMATCH_CONSTRAINT(Arglist, Vallist) ? 1 : -1]; \
44 # define ARGMATCH_ASSERT(Arglist, Vallist) \
45 assert (ARGMATCH_CONSTRAINT (Arglist, Vallist))
47 /* Return the index of the element of ARGLIST (NULL terminated) that
48 matches with ARG. If VALLIST is not NULL, then use it to resolve
49 false ambiguities (i.e., different matches of ARG but corresponding
50 to the same values in VALLIST). */
52 ptrdiff_t argmatch (char const *arg
, char const *const *arglist
,
53 char const *vallist
, size_t valsize
);
55 # define ARGMATCH(Arg, Arglist, Vallist) \
56 argmatch (Arg, Arglist, (char const *) (Vallist), sizeof *(Vallist))
58 /* xargmatch calls this function when it fails. This function should not
59 return. By default, this is a function that calls ARGMATCH_DIE which
60 in turn defaults to `exit (exit_failure)'. */
61 typedef void (*argmatch_exit_fn
) (void);
62 extern argmatch_exit_fn argmatch_die
;
64 /* Report on stderr why argmatch failed. Report correct values. */
66 void argmatch_invalid (char const *context
, char const *value
,
69 /* Left for compatibility with the old name invalid_arg */
71 # define invalid_arg(Context, Value, Problem) \
72 argmatch_invalid (Context, Value, Problem)
76 /* Report on stderr the list of possible arguments. */
78 void argmatch_valid (char const *const *arglist
,
79 char const *vallist
, size_t valsize
);
81 # define ARGMATCH_VALID(Arglist, Vallist) \
82 argmatch_valid (Arglist, (char const *) (Vallist), sizeof *(Vallist))
86 /* Same as argmatch, but upon failure, reports a explanation on the
87 failure, and exits using the function EXIT_FN. */
89 ptrdiff_t __xargmatch_internal (char const *context
,
90 char const *arg
, char const *const *arglist
,
91 char const *vallist
, size_t valsize
,
92 argmatch_exit_fn exit_fn
);
94 /* Programmer friendly interface to __xargmatch_internal. */
96 # define XARGMATCH(Context, Arg, Arglist, Vallist) \
97 ((Vallist) [__xargmatch_internal (Context, Arg, Arglist, \
98 (char const *) (Vallist), \
102 /* Convert a value into a corresponding argument. */
104 char const *argmatch_to_argument (char const *value
,
105 char const *const *arglist
,
106 char const *vallist
, size_t valsize
);
108 # define ARGMATCH_TO_ARGUMENT(Value, Arglist, Vallist) \
109 argmatch_to_argument (Value, Arglist, \
110 (char const *) (Vallist), sizeof *(Vallist))
112 #endif /* ARGMATCH_H_ */