s/argmatch_exit_failure/argmatch_die/
[gnulib.git] / lib / argmatch.h
1 /* argmatch.h -- definitions and prototypes for argmatch.c
2    Copyright (C) 1990, 1998, 1999 Free Software Foundation, Inc.
3
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)
7    any later version.
8
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.
13
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.  */
17
18 /* Written by David MacKenzie <djm@ai.mit.edu>
19    Modified by Akim Demaille <demaille@inf.enst.fr> */
20
21 #ifndef ARGMATCH_H_
22 # define ARGMATCH_H_ 1
23
24 # if HAVE_CONFIG_H
25 #  include <config.h>
26 # endif
27
28 # include <sys/types.h>
29
30 # ifndef PARAMS
31 #  if PROTOTYPES || (defined (__STDC__) && __STDC__)
32 #   define PARAMS(args) args
33 #  else
34 #   define PARAMS(args) ()
35 #  endif  /* GCC.  */
36 # endif  /* Not PARAMS.  */
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
43 int argmatch
44   PARAMS ((const char *arg, const char *const *arglist,
45            const char *vallist, size_t valsize));
46 int argcasematch
47   PARAMS ((const char *arg, const char *const *arglist,
48            const char *vallist, size_t valsize));
49
50 # define ARGMATCH(Arg, Arglist, Vallist) \
51   argmatch ((Arg), (Arglist), (const char *) (Vallist), sizeof (*(Vallist)))
52
53 # define ARGCASEMATCH(Arg, Arglist, Vallist) \
54   argcasematch ((Arg), (Arglist), (const char *) (Vallist), sizeof (*(Vallist)))
55
56 /* Function called when xargmatch failed.  Should not return.  By
57    default, set to a function calling the macro ARGMATCH_EXIT_FAILURE
58    which, by default is `exit (2)'.*/
59 typedef void (*argmatch_exit_fn) PARAMS ((void));
60 extern argmatch_exit_fn argmatch_die;
61
62 /* Report on stderr why argmatch failed.  Report correct values. */
63
64 void argmatch_invalid
65   PARAMS ((const char *context, const char *value, int problem));
66
67 /* Left for compatibility with the old name invalid_arg */
68
69 # define invalid_arg(Context, Value, Problem) \
70   argmatch_invalid ((Context), (Value), (Problem))
71
72
73
74 /* Report on stderr the list of possible arguments.  */
75
76 void argmatch_valid
77   PARAMS ((const char *const *arglist,
78            const char *vallist, size_t valsize));
79
80 # define ARGMATCH_VALID(Arglist, Vallist) \
81   argmatch_valid (Arglist, (const char *) Vallist, sizeof (*(Vallist)))
82
83
84
85 /* Same as argmatch, but upon failure, reports a explanation on the
86    failure, and exits using the function EXIT_FN. */
87
88 int __xargmatch_internal
89   PARAMS ((const char *context,
90            const char *arg, const char *const *arglist,
91            const char *vallist, size_t valsize,
92            int case_sensitive, argmatch_exit_fn exit_fn));
93
94 /* Programmer friendly interface to __xargmatch_internal. */
95
96 # define XARGMATCH(Context, Arg, Arglist, Vallist)                      \
97   (Vallist [__xargmatch_internal ((Context), (Arg), (Arglist),  \
98                                   (const char *) (Vallist),     \
99                                   sizeof (*(Vallist)),          \
100                                   1, argmatch_die)])
101
102 # define XARGCASEMATCH(Context, Arg, Arglist, Vallist)          \
103   (Vallist [__xargmatch_internal ((Context), (Arg), (Arglist),  \
104                                   (const char *) (Vallist),     \
105                                   sizeof (*(Vallist)),          \
106                                   0, argmatch_die)])
107
108 /* Convert a value into a corresponding argument. */
109
110 const char *argmatch_to_argument
111   PARAMS ((char const *value, const char *const *arglist,
112            const char *vallist, size_t valsize));
113
114 # define ARGMATCH_TO_ARGUMENT(Value, Arglist, Vallist)                  \
115   argmatch_to_argument ((char const *) &(Value), (Arglist),             \
116                         (const char *) (Vallist), sizeof (*(Vallist)))
117
118 #endif /* ARGMATCH_H_ */