Exit-status fixes from coreutils.
[gnulib.git] / lib / argmatch.h
1 /* argmatch.h -- definitions and prototypes for argmatch.c
2
3    Copyright (C) 1990, 1998, 1999, 2001, 2002, 2004 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 # define ARRAY_CARDINALITY(Array) (sizeof (Array) / sizeof *(Array))
29
30 # define ARGMATCH_CONSTRAINT(Arglist, Vallist) \
31   (ARRAY_CARDINALITY (Arglist) == ARRAY_CARDINALITY (Vallist) + 1)
32
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.  */
37
38 # define ARGMATCH_VERIFY(Arglist, Vallist)                                \
39   struct argmatch_verify                                                  \
40   {                                                                       \
41     char argmatch_verify[ARGMATCH_CONSTRAINT(Arglist, Vallist) ? 1 : -1]; \
42   }
43
44 # define ARGMATCH_ASSERT(Arglist, Vallist) \
45   assert (ARGMATCH_CONSTRAINT (Arglist, Vallist))
46
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).  */
51
52 int argmatch (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 /* 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;
63
64 /* Report on stderr why argmatch failed.  Report correct values. */
65
66 void argmatch_invalid (char const *context, char const *value, int problem);
67
68 /* Left for compatibility with the old name invalid_arg */
69
70 # define invalid_arg(Context, Value, Problem) \
71   argmatch_invalid (Context, Value, Problem)
72
73
74
75 /* Report on stderr the list of possible arguments.  */
76
77 void argmatch_valid (char const *const *arglist,
78                      char const *vallist, size_t valsize);
79
80 # define ARGMATCH_VALID(Arglist, Vallist) \
81   argmatch_valid (Arglist, (char const *) (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 (char const *context,
89                           char const *arg, char const *const *arglist,
90                           char const *vallist, size_t valsize,
91                           argmatch_exit_fn exit_fn);
92
93 /* Programmer friendly interface to __xargmatch_internal. */
94
95 # define XARGMATCH(Context, Arg, Arglist, Vallist)              \
96   ((Vallist) [__xargmatch_internal (Context, Arg, Arglist,      \
97                                     (char const *) (Vallist),   \
98                                     sizeof *(Vallist),          \
99                                     argmatch_die)])
100
101 /* Convert a value into a corresponding argument. */
102
103 char const *argmatch_to_argument (char const *value,
104                                   char const *const *arglist,
105                                   char const *vallist, size_t valsize);
106
107 # define ARGMATCH_TO_ARGUMENT(Value, Arglist, Vallist)                  \
108   argmatch_to_argument (Value, Arglist,                                 \
109                         (char const *) (Vallist), sizeof *(Vallist))
110
111 #endif /* ARGMATCH_H_ */