Comment tweak.
[gnulib.git] / lib / argmatch.h
1 /* argmatch.h -- definitions and prototypes for argmatch.c
2    Copyright (C) 1990, 1998, 1999, 2001, 2002 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 # include <stddef.h>
25
26 # define ARRAY_CARDINALITY(Array) (sizeof (Array) / sizeof *(Array))
27
28 # define ARGMATCH_CONSTRAINT(Arglist, Vallist) \
29   (ARRAY_CARDINALITY (Arglist) == ARRAY_CARDINALITY (Vallist) + 1)
30
31 /* Assert there are as many real arguments as there are values
32    (argument list ends with a NULL guard).  ARGMATCH_VERIFY is
33    preferred, since it is guaranteed to be checked at compile-time.
34    ARGMATCH_ASSERT is for backward compatibility only.  */
35
36 # define ARGMATCH_VERIFY(Arglist, Vallist)                                \
37   struct argmatch_verify                                                  \
38   {                                                                       \
39     char argmatch_verify[ARGMATCH_CONSTRAINT(Arglist, Vallist) ? 1 : -1]; \
40   }
41
42 # define ARGMATCH_ASSERT(Arglist, Vallist) \
43   assert (ARGMATCH_CONSTRAINT (Arglist, Vallist))
44
45 /* Return the index of the element of ARGLIST (NULL terminated) that
46    matches with ARG.  If VALLIST is not NULL, then use it to resolve
47    false ambiguities (i.e., different matches of ARG but corresponding
48    to the same values in VALLIST).  */
49
50 int argmatch (char const *arg, char const *const *arglist,
51               char const *vallist, size_t valsize);
52 int argcasematch (char const *arg, char const *const *arglist,
53                   char const *vallist, size_t valsize);
54
55 # define ARGMATCH(Arg, Arglist, Vallist) \
56   argmatch (Arg, Arglist, (char const *) (Vallist), sizeof *(Vallist))
57
58 # define ARGCASEMATCH(Arg, Arglist, Vallist) \
59   argcasematch (Arg, Arglist, (char const *) (Vallist), sizeof *(Vallist))
60
61 /* xargmatch calls this function when it fails.  This function should not
62    return.  By default, this is a function that calls ARGMATCH_DIE which
63    in turn defaults to `exit (EXIT_FAILURE)'.  */
64 typedef void (*argmatch_exit_fn) (void);
65 extern argmatch_exit_fn argmatch_die;
66
67 /* Report on stderr why argmatch failed.  Report correct values. */
68
69 void argmatch_invalid (char const *context, char const *value, int problem);
70
71 /* Left for compatibility with the old name invalid_arg */
72
73 # define invalid_arg(Context, Value, Problem) \
74   argmatch_invalid (Context, Value, Problem)
75
76
77
78 /* Report on stderr the list of possible arguments.  */
79
80 void argmatch_valid (char const *const *arglist,
81                      char const *vallist, size_t valsize);
82
83 # define ARGMATCH_VALID(Arglist, Vallist) \
84   argmatch_valid (Arglist, (char const *) (Vallist), sizeof *(Vallist))
85
86
87
88 /* Same as argmatch, but upon failure, reports a explanation on the
89    failure, and exits using the function EXIT_FN. */
90
91 int __xargmatch_internal (char const *context,
92                           char const *arg, char const *const *arglist,
93                           char const *vallist, size_t valsize,
94                           int case_sensitive, argmatch_exit_fn exit_fn);
95
96 /* Programmer friendly interface to __xargmatch_internal. */
97
98 # define XARGMATCH(Context, Arg, Arglist, Vallist)              \
99   ((Vallist) [__xargmatch_internal (Context, Arg, Arglist,      \
100                                     (char const *) (Vallist),   \
101                                     sizeof *(Vallist),          \
102                                     1, argmatch_die)])
103
104 # define XARGCASEMATCH(Context, Arg, Arglist, Vallist)          \
105   ((Vallist) [__xargmatch_internal (Context, Arg, Arglist,      \
106                                     (char const *) (Vallist),   \
107                                     sizeof *(Vallist),          \
108                                     0, argmatch_die)])
109
110 /* Convert a value into a corresponding argument. */
111
112 char const *argmatch_to_argument (char const *value,
113                                   char const *const *arglist,
114                                   char const *vallist, size_t valsize);
115
116 # define ARGMATCH_TO_ARGUMENT(Value, Arglist, Vallist)                  \
117   argmatch_to_argument (Value, Arglist,                                 \
118                         (char const *) (Vallist), sizeof *(Vallist))
119
120 #endif /* ARGMATCH_H_ */