]>
Commit | Line | Data |
---|---|---|
54049e5d | 1 | /* argmatch.h -- definitions and prototypes for argmatch.c |
b973108e | 2 | Copyright (C) 1990, 1998, 1999, 2001, 2002 Free Software Foundation, Inc. |
54049e5d AD |
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 | ||
6f5db564 | 24 | # include <stddef.h> |
54049e5d | 25 | |
6f5db564 | 26 | # define ARRAY_CARDINALITY(Array) (sizeof (Array) / sizeof *(Array)) |
54049e5d | 27 | |
6f5db564 PE |
28 | # define ARGMATCH_CONSTRAINT(Arglist, Vallist) \ |
29 | (ARRAY_CARDINALITY (Arglist) == ARRAY_CARDINALITY (Vallist) + 1) | |
54049e5d AD |
30 | |
31 | /* Assert there are as many real arguments as there are values | |
6f5db564 | 32 | (argument list ends with a NULL guard). ARGMATCH_VERIFY is |
0368ae12 | 33 | preferred, since it is guaranteed to be checked at compile-time. |
6f5db564 | 34 | ARGMATCH_ASSERT is for backward compatibility only. */ |
54049e5d | 35 | |
6f5db564 PE |
36 | # define ARGMATCH_VERIFY(Arglist, Vallist) \ |
37 | struct argmatch_verify \ | |
38 | { \ | |
39 | char argmatch_verify[ARGMATCH_CONSTRAINT(Arglist, Vallist) ? 1 : -1]; \ | |
40 | } | |
54049e5d | 41 | |
b973108e PE |
42 | # define ARGMATCH_ASSERT(Arglist, Vallist) \ |
43 | assert (ARGMATCH_CONSTRAINT (Arglist, Vallist)) | |
54049e5d AD |
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 | ||
6f5db564 PE |
50 | int argmatch (char const *arg, char const *const *arglist, |
51 | char const *vallist, size_t valsize); | |
54049e5d AD |
52 | |
53 | # define ARGMATCH(Arg, Arglist, Vallist) \ | |
6f5db564 | 54 | argmatch (Arg, Arglist, (char const *) (Vallist), sizeof *(Vallist)) |
54049e5d | 55 | |
54049e5d AD |
56 | /* xargmatch calls this function when it fails. This function should not |
57 | return. By default, this is a function that calls ARGMATCH_DIE which | |
58 | in turn defaults to `exit (EXIT_FAILURE)'. */ | |
6f5db564 | 59 | typedef void (*argmatch_exit_fn) (void); |
54049e5d AD |
60 | extern argmatch_exit_fn argmatch_die; |
61 | ||
62 | /* Report on stderr why argmatch failed. Report correct values. */ | |
63 | ||
6f5db564 | 64 | void argmatch_invalid (char const *context, char const *value, int problem); |
54049e5d AD |
65 | |
66 | /* Left for compatibility with the old name invalid_arg */ | |
67 | ||
68 | # define invalid_arg(Context, Value, Problem) \ | |
6f5db564 | 69 | argmatch_invalid (Context, Value, Problem) |
54049e5d AD |
70 | |
71 | ||
72 | ||
73 | /* Report on stderr the list of possible arguments. */ | |
74 | ||
6f5db564 PE |
75 | void argmatch_valid (char const *const *arglist, |
76 | char const *vallist, size_t valsize); | |
54049e5d AD |
77 | |
78 | # define ARGMATCH_VALID(Arglist, Vallist) \ | |
6f5db564 | 79 | argmatch_valid (Arglist, (char const *) (Vallist), sizeof *(Vallist)) |
54049e5d AD |
80 | |
81 | ||
82 | ||
83 | /* Same as argmatch, but upon failure, reports a explanation on the | |
84 | failure, and exits using the function EXIT_FN. */ | |
85 | ||
6f5db564 PE |
86 | int __xargmatch_internal (char const *context, |
87 | char const *arg, char const *const *arglist, | |
88 | char const *vallist, size_t valsize, | |
69e16b4c | 89 | argmatch_exit_fn exit_fn); |
54049e5d AD |
90 | |
91 | /* Programmer friendly interface to __xargmatch_internal. */ | |
92 | ||
6f5db564 PE |
93 | # define XARGMATCH(Context, Arg, Arglist, Vallist) \ |
94 | ((Vallist) [__xargmatch_internal (Context, Arg, Arglist, \ | |
95 | (char const *) (Vallist), \ | |
96 | sizeof *(Vallist), \ | |
69e16b4c | 97 | argmatch_die)]) |
54049e5d AD |
98 | |
99 | /* Convert a value into a corresponding argument. */ | |
100 | ||
6f5db564 PE |
101 | char const *argmatch_to_argument (char const *value, |
102 | char const *const *arglist, | |
103 | char const *vallist, size_t valsize); | |
54049e5d AD |
104 | |
105 | # define ARGMATCH_TO_ARGUMENT(Value, Arglist, Vallist) \ | |
6f5db564 PE |
106 | argmatch_to_argument (Value, Arglist, \ |
107 | (char const *) (Vallist), sizeof *(Vallist)) | |
54049e5d AD |
108 | |
109 | #endif /* ARGMATCH_H_ */ |