1 | /* argmatch.h -- definitions and prototypes for argmatch.c
|
---|
2 |
|
---|
3 | Copyright (C) 1990, 1998-1999, 2001-2002, 2004-2005, 2009-2016 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 3 of the License, or
|
---|
9 | (at your option) 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, see <http://www.gnu.org/licenses/>. */
|
---|
18 |
|
---|
19 | /* Written by David MacKenzie <djm@ai.mit.edu>
|
---|
20 | Modified by Akim Demaille <demaille@inf.enst.fr> */
|
---|
21 |
|
---|
22 | #ifndef ARGMATCH_H_
|
---|
23 | # define ARGMATCH_H_ 1
|
---|
24 |
|
---|
25 | # include <stddef.h>
|
---|
26 |
|
---|
27 | # include "verify.h"
|
---|
28 |
|
---|
29 | #ifdef __cplusplus
|
---|
30 | extern "C" {
|
---|
31 | #endif
|
---|
32 |
|
---|
33 | # define ARRAY_CARDINALITY(Array) (sizeof (Array) / sizeof *(Array))
|
---|
34 |
|
---|
35 | /* Assert there are as many real arguments as there are values
|
---|
36 | (argument list ends with a NULL guard). */
|
---|
37 |
|
---|
38 | # define ARGMATCH_VERIFY(Arglist, Vallist) \
|
---|
39 | verify (ARRAY_CARDINALITY (Arglist) == ARRAY_CARDINALITY (Vallist) + 1)
|
---|
40 |
|
---|
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). */
|
---|
45 |
|
---|
46 | ptrdiff_t argmatch (char const *arg, char const *const *arglist,
|
---|
47 | char const *vallist, size_t valsize) _GL_ATTRIBUTE_PURE;
|
---|
48 |
|
---|
49 | # define ARGMATCH(Arg, Arglist, Vallist) \
|
---|
50 | argmatch (Arg, Arglist, (char const *) (Vallist), sizeof *(Vallist))
|
---|
51 |
|
---|
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;
|
---|
57 |
|
---|
58 | /* Report on stderr why argmatch failed. Report correct values. */
|
---|
59 |
|
---|
60 | void argmatch_invalid (char const *context, char const *value,
|
---|
61 | ptrdiff_t problem);
|
---|
62 |
|
---|
63 | /* Left for compatibility with the old name invalid_arg */
|
---|
64 |
|
---|
65 | # define invalid_arg(Context, Value, Problem) \
|
---|
66 | argmatch_invalid (Context, Value, Problem)
|
---|
67 |
|
---|
68 |
|
---|
69 |
|
---|
70 | /* Report on stderr the list of possible arguments. */
|
---|
71 |
|
---|
72 | void argmatch_valid (char const *const *arglist,
|
---|
73 | char const *vallist, size_t valsize);
|
---|
74 |
|
---|
75 | # define ARGMATCH_VALID(Arglist, Vallist) \
|
---|
76 | argmatch_valid (Arglist, (char const *) (Vallist), sizeof *(Vallist))
|
---|
77 |
|
---|
78 |
|
---|
79 |
|
---|
80 | /* Same as argmatch, but upon failure, report an explanation of the
|
---|
81 | failure, and exit using the function EXIT_FN. */
|
---|
82 |
|
---|
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);
|
---|
87 |
|
---|
88 | /* Programmer friendly interface to __xargmatch_internal. */
|
---|
89 |
|
---|
90 | # define XARGMATCH(Context, Arg, Arglist, Vallist) \
|
---|
91 | ((Vallist) [__xargmatch_internal (Context, Arg, Arglist, \
|
---|
92 | (char const *) (Vallist), \
|
---|
93 | sizeof *(Vallist), \
|
---|
94 | argmatch_die)])
|
---|
95 |
|
---|
96 | /* Convert a value into a corresponding argument. */
|
---|
97 |
|
---|
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;
|
---|
102 |
|
---|
103 | # define ARGMATCH_TO_ARGUMENT(Value, Arglist, Vallist) \
|
---|
104 | argmatch_to_argument (Value, Arglist, \
|
---|
105 | (char const *) (Vallist), sizeof *(Vallist))
|
---|
106 |
|
---|
107 | #ifdef __cplusplus
|
---|
108 | }
|
---|
109 | #endif
|
---|
110 |
|
---|
111 | #endif /* ARGMATCH_H_ */
|
---|