]>
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 PE |
32 | (argument list ends with a NULL guard). ARGMATCH_VERIFY is |
33 | preferred, since it is guaranteed to be chedk at compile-time. | |
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); | |
52 | int argcasematch (char const *arg, char const *const *arglist, | |
53 | char const *vallist, size_t valsize); | |
54049e5d AD |
54 | |
55 | # define ARGMATCH(Arg, Arglist, Vallist) \ | |
6f5db564 | 56 | argmatch (Arg, Arglist, (char const *) (Vallist), sizeof *(Vallist)) |
54049e5d AD |
57 | |
58 | # define ARGCASEMATCH(Arg, Arglist, Vallist) \ | |
6f5db564 | 59 | argcasematch (Arg, Arglist, (char const *) (Vallist), sizeof *(Vallist)) |
54049e5d AD |
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)'. */ | |
6f5db564 | 64 | typedef void (*argmatch_exit_fn) (void); |
54049e5d AD |
65 | extern argmatch_exit_fn argmatch_die; |
66 | ||
67 | /* Report on stderr why argmatch failed. Report correct values. */ | |
68 | ||
6f5db564 | 69 | void argmatch_invalid (char const *context, char const *value, int problem); |
54049e5d AD |
70 | |
71 | /* Left for compatibility with the old name invalid_arg */ | |
72 | ||
73 | # define invalid_arg(Context, Value, Problem) \ | |
6f5db564 | 74 | argmatch_invalid (Context, Value, Problem) |
54049e5d AD |
75 | |
76 | ||
77 | ||
78 | /* Report on stderr the list of possible arguments. */ | |
79 | ||
6f5db564 PE |
80 | void argmatch_valid (char const *const *arglist, |
81 | char const *vallist, size_t valsize); | |
54049e5d AD |
82 | |
83 | # define ARGMATCH_VALID(Arglist, Vallist) \ | |
6f5db564 | 84 | argmatch_valid (Arglist, (char const *) (Vallist), sizeof *(Vallist)) |
54049e5d AD |
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 | ||
6f5db564 PE |
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); | |
54049e5d AD |
95 | |
96 | /* Programmer friendly interface to __xargmatch_internal. */ | |
97 | ||
6f5db564 PE |
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)]) | |
54049e5d AD |
103 | |
104 | # define XARGCASEMATCH(Context, Arg, Arglist, Vallist) \ | |
6f5db564 PE |
105 | ((Vallist) [__xargmatch_internal (Context, Arg, Arglist, \ |
106 | (char const *) (Vallist), \ | |
107 | sizeof *(Vallist), \ | |
108 | 0, argmatch_die)]) | |
54049e5d AD |
109 | |
110 | /* Convert a value into a corresponding argument. */ | |
111 | ||
6f5db564 PE |
112 | char const *argmatch_to_argument (char const *value, |
113 | char const *const *arglist, | |
114 | char const *vallist, size_t valsize); | |
54049e5d AD |
115 | |
116 | # define ARGMATCH_TO_ARGUMENT(Value, Arglist, Vallist) \ | |
6f5db564 PE |
117 | argmatch_to_argument (Value, Arglist, \ |
118 | (char const *) (Vallist), sizeof *(Vallist)) | |
54049e5d AD |
119 | |
120 | #endif /* ARGMATCH_H_ */ |