]>
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 | ||
24 | # if HAVE_CONFIG_H | |
25 | # include <config.h> | |
26 | # endif | |
27 | ||
28 | # include <sys/types.h> | |
29 | ||
30 | # ifndef PARAMS | |
31 | # if PROTOTYPES || (defined (__STDC__) && __STDC__) | |
32 | # define PARAMS(args) args | |
33 | # else | |
34 | # define PARAMS(args) () | |
35 | # endif /* GCC. */ | |
36 | # endif /* Not PARAMS. */ | |
37 | ||
38 | /* Assert there are as many real arguments as there are values | |
39 | (argument list ends with a NULL guard). There is no execution | |
40 | cost, since it will be statically evalauted to `assert (0)' or | |
41 | `assert (1)'. Unfortunately there is no -Wassert-0. */ | |
42 | ||
54049e5d AD |
43 | # define ARRAY_CARDINALITY(Array) (sizeof ((Array)) / sizeof (*(Array))) |
44 | ||
b973108e PE |
45 | # define ARGMATCH_CONSTRAINT(Arglist, Vallist) \ |
46 | (ARRAY_CARDINALITY ((Arglist)) == ARRAY_CARDINALITY ((Vallist)) + 1) | |
47 | # define ARGMATCH_ASSERT(Arglist, Vallist) \ | |
48 | assert (ARGMATCH_CONSTRAINT (Arglist, Vallist)) | |
54049e5d AD |
49 | |
50 | /* Return the index of the element of ARGLIST (NULL terminated) that | |
51 | matches with ARG. If VALLIST is not NULL, then use it to resolve | |
52 | false ambiguities (i.e., different matches of ARG but corresponding | |
53 | to the same values in VALLIST). */ | |
54 | ||
55 | int argmatch | |
56 | PARAMS ((const char *arg, const char *const *arglist, | |
57 | const char *vallist, size_t valsize)); | |
58 | int argcasematch | |
59 | PARAMS ((const char *arg, const char *const *arglist, | |
60 | const char *vallist, size_t valsize)); | |
61 | ||
62 | # define ARGMATCH(Arg, Arglist, Vallist) \ | |
63 | argmatch ((Arg), (Arglist), (const char *) (Vallist), sizeof (*(Vallist))) | |
64 | ||
65 | # define ARGCASEMATCH(Arg, Arglist, Vallist) \ | |
66 | argcasematch ((Arg), (Arglist), (const char *) (Vallist), sizeof (*(Vallist))) | |
67 | ||
68 | /* xargmatch calls this function when it fails. This function should not | |
69 | return. By default, this is a function that calls ARGMATCH_DIE which | |
70 | in turn defaults to `exit (EXIT_FAILURE)'. */ | |
71 | typedef void (*argmatch_exit_fn) PARAMS ((void)); | |
72 | extern argmatch_exit_fn argmatch_die; | |
73 | ||
74 | /* Report on stderr why argmatch failed. Report correct values. */ | |
75 | ||
76 | void argmatch_invalid | |
77 | PARAMS ((const char *context, const char *value, int problem)); | |
78 | ||
79 | /* Left for compatibility with the old name invalid_arg */ | |
80 | ||
81 | # define invalid_arg(Context, Value, Problem) \ | |
82 | argmatch_invalid ((Context), (Value), (Problem)) | |
83 | ||
84 | ||
85 | ||
86 | /* Report on stderr the list of possible arguments. */ | |
87 | ||
88 | void argmatch_valid | |
89 | PARAMS ((const char *const *arglist, | |
90 | const char *vallist, size_t valsize)); | |
91 | ||
92 | # define ARGMATCH_VALID(Arglist, Vallist) \ | |
93 | argmatch_valid (Arglist, (const char *) Vallist, sizeof (*(Vallist))) | |
94 | ||
95 | ||
96 | ||
97 | /* Same as argmatch, but upon failure, reports a explanation on the | |
98 | failure, and exits using the function EXIT_FN. */ | |
99 | ||
100 | int __xargmatch_internal | |
101 | PARAMS ((const char *context, | |
102 | const char *arg, const char *const *arglist, | |
103 | const char *vallist, size_t valsize, | |
104 | int case_sensitive, argmatch_exit_fn exit_fn)); | |
105 | ||
106 | /* Programmer friendly interface to __xargmatch_internal. */ | |
107 | ||
eb956856 | 108 | # define XARGMATCH(Context, Arg, Arglist, Vallist) \ |
54049e5d AD |
109 | (Vallist [__xargmatch_internal ((Context), (Arg), (Arglist), \ |
110 | (const char *) (Vallist), \ | |
111 | sizeof (*(Vallist)), \ | |
112 | 1, argmatch_die)]) | |
113 | ||
114 | # define XARGCASEMATCH(Context, Arg, Arglist, Vallist) \ | |
115 | (Vallist [__xargmatch_internal ((Context), (Arg), (Arglist), \ | |
116 | (const char *) (Vallist), \ | |
117 | sizeof (*(Vallist)), \ | |
118 | 0, argmatch_die)]) | |
119 | ||
120 | /* Convert a value into a corresponding argument. */ | |
121 | ||
122 | const char *argmatch_to_argument | |
123 | PARAMS ((char const *value, const char *const *arglist, | |
124 | const char *vallist, size_t valsize)); | |
125 | ||
126 | # define ARGMATCH_TO_ARGUMENT(Value, Arglist, Vallist) \ | |
eb956856 | 127 | argmatch_to_argument ((Value), (Arglist), \ |
54049e5d AD |
128 | (const char *) (Vallist), sizeof (*(Vallist))) |
129 | ||
130 | #endif /* ARGMATCH_H_ */ |