sys_socket: Add support for OpenVMS.
[gnulib.git] / lib / argmatch.h
blobc7215c8efc8f8a67e26d1188093f79228de90717
1 /* argmatch.h -- definitions and prototypes for argmatch.c
3 Copyright (C) 1990, 1998-1999, 2001-2002, 2004-2005, 2009-2017 Free Software
4 Foundation, Inc.
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 3 of the License, or
9 (at your option) any later version.
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.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 /* Written by David MacKenzie <djm@ai.mit.edu>
20 Modified by Akim Demaille <demaille@inf.enst.fr> */
22 #ifndef ARGMATCH_H_
23 # define ARGMATCH_H_ 1
25 # include <stddef.h>
27 # include "verify.h"
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
33 # define ARRAY_CARDINALITY(Array) (sizeof (Array) / sizeof *(Array))
35 /* Assert there are as many real arguments as there are values
36 (argument list ends with a NULL guard). */
38 # define ARGMATCH_VERIFY(Arglist, Vallist) \
39 verify (ARRAY_CARDINALITY (Arglist) == ARRAY_CARDINALITY (Vallist) + 1)
41 /* Return the index of the element of ARGLIST (NULL terminated) that
42 matches with ARG. If VALLIST is not NULL, then use it to resolve
43 false ambiguities (i.e., different matches of ARG but corresponding
44 to the same values in VALLIST). */
46 ptrdiff_t argmatch (char const *arg, char const *const *arglist,
47 char const *vallist, size_t valsize) _GL_ATTRIBUTE_PURE;
49 # define ARGMATCH(Arg, Arglist, Vallist) \
50 argmatch (Arg, Arglist, (char const *) (Vallist), sizeof *(Vallist))
52 /* xargmatch calls this function when it fails. This function should not
53 return. By default, this is a function that calls ARGMATCH_DIE which
54 in turn defaults to 'exit (exit_failure)'. */
55 typedef void (*argmatch_exit_fn) (void);
56 extern argmatch_exit_fn argmatch_die;
58 /* Report on stderr why argmatch failed. Report correct values. */
60 void argmatch_invalid (char const *context, char const *value,
61 ptrdiff_t problem);
63 /* Left for compatibility with the old name invalid_arg */
65 # define invalid_arg(Context, Value, Problem) \
66 argmatch_invalid (Context, Value, Problem)
70 /* Report on stderr the list of possible arguments. */
72 void argmatch_valid (char const *const *arglist,
73 char const *vallist, size_t valsize);
75 # define ARGMATCH_VALID(Arglist, Vallist) \
76 argmatch_valid (Arglist, (char const *) (Vallist), sizeof *(Vallist))
80 /* Same as argmatch, but upon failure, report an explanation of the
81 failure, and exit using the function EXIT_FN. */
83 ptrdiff_t __xargmatch_internal (char const *context,
84 char const *arg, char const *const *arglist,
85 char const *vallist, size_t valsize,
86 argmatch_exit_fn exit_fn);
88 /* Programmer friendly interface to __xargmatch_internal. */
90 # define XARGMATCH(Context, Arg, Arglist, Vallist) \
91 ((Vallist) [__xargmatch_internal (Context, Arg, Arglist, \
92 (char const *) (Vallist), \
93 sizeof *(Vallist), \
94 argmatch_die)])
96 /* Convert a value into a corresponding argument. */
98 char const *argmatch_to_argument (char const *value,
99 char const *const *arglist,
100 char const *vallist, size_t valsize)
101 _GL_ATTRIBUTE_PURE;
103 # define ARGMATCH_TO_ARGUMENT(Value, Arglist, Vallist) \
104 argmatch_to_argument (Value, Arglist, \
105 (char const *) (Vallist), sizeof *(Vallist))
107 #ifdef __cplusplus
109 #endif
111 #endif /* ARGMATCH_H_ */