1/* argmatch.h -- definitions and prototypes for argmatch.c
2
3   Copyright (C) 1990, 1998, 1999, 2001, 2002, 2004, 2005 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 2, or (at your option)
9   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, write to the Free Software Foundation,
18   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
19
20/* Written by David MacKenzie <djm@ai.mit.edu>
21   Modified by Akim Demaille <demaille@inf.enst.fr> */
22
23#ifndef ARGMATCH_H_
24# define ARGMATCH_H_ 1
25
26# include <stddef.h>
27
28# include "verify.h"
29
30# define ARRAY_CARDINALITY(Array) (sizeof (Array) / sizeof *(Array))
31
32/* Assert there are as many real arguments as there are values
33   (argument list ends with a NULL guard).  */
34
35# define ARGMATCH_VERIFY(Arglist, Vallist) \
36    verify (ARRAY_CARDINALITY (Arglist) == ARRAY_CARDINALITY (Vallist) + 1)
37
38/* Return the index of the element of ARGLIST (NULL terminated) that
39   matches with ARG.  If VALLIST is not NULL, then use it to resolve
40   false ambiguities (i.e., different matches of ARG but corresponding
41   to the same values in VALLIST).  */
42
43ptrdiff_t argmatch (char const *arg, char const *const *arglist,
44		    char const *vallist, size_t valsize);
45
46# define ARGMATCH(Arg, Arglist, Vallist) \
47  argmatch (Arg, Arglist, (char const *) (Vallist), sizeof *(Vallist))
48
49/* xargmatch calls this function when it fails.  This function should not
50   return.  By default, this is a function that calls ARGMATCH_DIE which
51   in turn defaults to `exit (exit_failure)'.  */
52typedef void (*argmatch_exit_fn) (void);
53extern argmatch_exit_fn argmatch_die;
54
55/* Report on stderr why argmatch failed.  Report correct values. */
56
57void argmatch_invalid (char const *context, char const *value,
58		       ptrdiff_t problem);
59
60/* Left for compatibility with the old name invalid_arg */
61
62# define invalid_arg(Context, Value, Problem) \
63  argmatch_invalid (Context, Value, Problem)
64
65
66
67/* Report on stderr the list of possible arguments.  */
68
69void argmatch_valid (char const *const *arglist,
70		     char const *vallist, size_t valsize);
71
72# define ARGMATCH_VALID(Arglist, Vallist) \
73  argmatch_valid (Arglist, (char const *) (Vallist), sizeof *(Vallist))
74
75
76
77/* Same as argmatch, but upon failure, reports a explanation on the
78   failure, and exits using the function EXIT_FN. */
79
80ptrdiff_t __xargmatch_internal (char const *context,
81				char const *arg, char const *const *arglist,
82				char const *vallist, size_t valsize,
83				argmatch_exit_fn exit_fn);
84
85/* Programmer friendly interface to __xargmatch_internal. */
86
87# define XARGMATCH(Context, Arg, Arglist, Vallist)		\
88  ((Vallist) [__xargmatch_internal (Context, Arg, Arglist,	\
89				    (char const *) (Vallist),	\
90				    sizeof *(Vallist),		\
91				    argmatch_die)])
92
93/* Convert a value into a corresponding argument. */
94
95char const *argmatch_to_argument (char const *value,
96				  char const *const *arglist,
97				  char const *vallist, size_t valsize);
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