1 /* argmatch.h -- definitions and prototypes for argmatch.c
2 Copyright (C) 1990, 1998, 1999, 2001 Free Software 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 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 /* Written by David MacKenzie <djm@ai.mit.edu>
19 Modified by Akim Demaille <demaille@inf.enst.fr> */
22 # define ARGMATCH_H_ 1
28 # include <sys/types.h>
31 # if PROTOTYPES || (defined (__STDC__) && __STDC__)
32 # define PARAMS(args) args
34 # define PARAMS(args) ()
36 # endif /* Not PARAMS. */
38 /* Assert there are as many real arguments as there are values
39 (argument list ends with a NULL guard). There is no execution
40 cost, since it will be statically evalauted to `assert (0)' or
41 `assert (1)'. Unfortunately there is no -Wassert-0. */
43 # undef ARRAY_CARDINALITY
44 # define ARRAY_CARDINALITY(Array) (sizeof ((Array)) / sizeof (*(Array)))
46 # define ARGMATCH_ASSERT(Arglist, Vallist) \
47 assert (ARRAY_CARDINALITY ((Arglist)) == ARRAY_CARDINALITY ((Vallist)) + 1)
49 /* Return the index of the element of ARGLIST (NULL terminated) that
50 matches with ARG. If VALLIST is not NULL, then use it to resolve
51 false ambiguities (i.e., different matches of ARG but corresponding
52 to the same values in VALLIST). */
55 PARAMS ((const char *arg
, const char *const *arglist
,
56 const char *vallist
, size_t valsize
));
58 PARAMS ((const char *arg
, const char *const *arglist
,
59 const char *vallist
, size_t valsize
));
61 # define ARGMATCH(Arg, Arglist, Vallist) \
62 argmatch ((Arg), (Arglist), (const char *) (Vallist), sizeof (*(Vallist)))
64 # define ARGCASEMATCH(Arg, Arglist, Vallist) \
65 argcasematch ((Arg), (Arglist), (const char *) (Vallist), sizeof (*(Vallist)))
67 /* xargmatch calls this function when it fails. This function should not
68 return. By default, this is a function that calls ARGMATCH_DIE which
69 in turn defaults to `exit (EXIT_FAILURE)'. */
70 typedef void (*argmatch_exit_fn
) PARAMS ((void));
71 extern argmatch_exit_fn argmatch_die
;
73 /* Report on stderr why argmatch failed. Report correct values. */
76 PARAMS ((const char *context
, const char *value
, int problem
));
78 /* Left for compatibility with the old name invalid_arg */
80 # define invalid_arg(Context, Value, Problem) \
81 argmatch_invalid ((Context), (Value), (Problem))
85 /* Report on stderr the list of possible arguments. */
88 PARAMS ((const char *const *arglist
,
89 const char *vallist
, size_t valsize
));
91 # define ARGMATCH_VALID(Arglist, Vallist) \
92 argmatch_valid (Arglist, (const char *) Vallist, sizeof (*(Vallist)))
96 /* Same as argmatch, but upon failure, reports a explanation on the
97 failure, and exits using the function EXIT_FN. */
99 int __xargmatch_internal
100 PARAMS ((const char *context
,
101 const char *arg
, const char *const *arglist
,
102 const char *vallist
, size_t valsize
,
103 int case_sensitive
, argmatch_exit_fn exit_fn
));
105 /* Programmer friendly interface to __xargmatch_internal. */
107 # define XARGMATCH(Context, Arg, Arglist, Vallist) \
108 (Vallist [__xargmatch_internal ((Context), (Arg), (Arglist), \
109 (const char *) (Vallist), \
110 sizeof (*(Vallist)), \
113 # define XARGCASEMATCH(Context, Arg, Arglist, Vallist) \
114 (Vallist [__xargmatch_internal ((Context), (Arg), (Arglist), \
115 (const char *) (Vallist), \
116 sizeof (*(Vallist)), \
119 /* Convert a value into a corresponding argument. */
121 const char *argmatch_to_argument
122 PARAMS ((char const *value
, const char *const *arglist
,
123 const char *vallist
, size_t valsize
));
125 # define ARGMATCH_TO_ARGUMENT(Value, Arglist, Vallist) \
126 argmatch_to_argument ((Value), (Arglist), \
127 (const char *) (Vallist), sizeof (*(Vallist)))
129 #endif /* ARGMATCH_H_ */