]>
Commit | Line | Data |
---|---|---|
1 | /* argmatch.h -- definitions and prototypes for argmatch.c | |
2 | Copyright (C) 1990, 1998, 1999, 2001 Free Software Foundation, Inc. | |
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 | ||
43 | # undef ARRAY_CARDINALITY | |
44 | # define ARRAY_CARDINALITY(Array) (sizeof ((Array)) / sizeof (*(Array))) | |
45 | ||
46 | # define ARGMATCH_ASSERT(Arglist, Vallist) \ | |
47 | assert (ARRAY_CARDINALITY ((Arglist)) == ARRAY_CARDINALITY ((Vallist)) + 1) | |
48 | ||
49 | /* Return the index of the element of ARGLIST (NULL terminated) that | |
50 | matches with ARG. If VALLIST is not NULL, then use it to resolve | |
51 | false ambiguities (i.e., different matches of ARG but corresponding | |
52 | to the same values in VALLIST). */ | |
53 | ||
54 | int argmatch | |
55 | PARAMS ((const char *arg, const char *const *arglist, | |
56 | const char *vallist, size_t valsize)); | |
57 | int argcasematch | |
58 | PARAMS ((const char *arg, const char *const *arglist, | |
59 | const char *vallist, size_t valsize)); | |
60 | ||
61 | # define ARGMATCH(Arg, Arglist, Vallist) \ | |
62 | argmatch ((Arg), (Arglist), (const char *) (Vallist), sizeof (*(Vallist))) | |
63 | ||
64 | # define ARGCASEMATCH(Arg, Arglist, Vallist) \ | |
65 | argcasematch ((Arg), (Arglist), (const char *) (Vallist), sizeof (*(Vallist))) | |
66 | ||
67 | /* xargmatch calls this function when it fails. This function should not | |
68 | return. By default, this is a function that calls ARGMATCH_DIE which | |
69 | in turn defaults to `exit (EXIT_FAILURE)'. */ | |
70 | typedef void (*argmatch_exit_fn) PARAMS ((void)); | |
71 | extern argmatch_exit_fn argmatch_die; | |
72 | ||
73 | /* Report on stderr why argmatch failed. Report correct values. */ | |
74 | ||
75 | void argmatch_invalid | |
76 | PARAMS ((const char *context, const char *value, int problem)); | |
77 | ||
78 | /* Left for compatibility with the old name invalid_arg */ | |
79 | ||
80 | # define invalid_arg(Context, Value, Problem) \ | |
81 | argmatch_invalid ((Context), (Value), (Problem)) | |
82 | ||
83 | ||
84 | ||
85 | /* Report on stderr the list of possible arguments. */ | |
86 | ||
87 | void argmatch_valid | |
88 | PARAMS ((const char *const *arglist, | |
89 | const char *vallist, size_t valsize)); | |
90 | ||
91 | # define ARGMATCH_VALID(Arglist, Vallist) \ | |
92 | argmatch_valid (Arglist, (const char *) Vallist, sizeof (*(Vallist))) | |
93 | ||
94 | ||
95 | ||
96 | /* Same as argmatch, but upon failure, reports a explanation on the | |
97 | failure, and exits using the function EXIT_FN. */ | |
98 | ||
99 | int __xargmatch_internal | |
100 | PARAMS ((const char *context, | |
101 | const char *arg, const char *const *arglist, | |
102 | const char *vallist, size_t valsize, | |
103 | int case_sensitive, argmatch_exit_fn exit_fn)); | |
104 | ||
105 | /* Programmer friendly interface to __xargmatch_internal. */ | |
106 | ||
107 | # define XARGMATCH(Context, Arg, Arglist, Vallist) \ | |
108 | (Vallist [__xargmatch_internal ((Context), (Arg), (Arglist), \ | |
109 | (const char *) (Vallist), \ | |
110 | sizeof (*(Vallist)), \ | |
111 | 1, argmatch_die)]) | |
112 | ||
113 | # define XARGCASEMATCH(Context, Arg, Arglist, Vallist) \ | |
114 | (Vallist [__xargmatch_internal ((Context), (Arg), (Arglist), \ | |
115 | (const char *) (Vallist), \ | |
116 | sizeof (*(Vallist)), \ | |
117 | 0, argmatch_die)]) | |
118 | ||
119 | /* Convert a value into a corresponding argument. */ | |
120 | ||
121 | const char *argmatch_to_argument | |
122 | PARAMS ((char const *value, const char *const *arglist, | |
123 | const char *vallist, size_t valsize)); | |
124 | ||
125 | # define ARGMATCH_TO_ARGUMENT(Value, Arglist, Vallist) \ | |
126 | argmatch_to_argument ((Value), (Arglist), \ | |
127 | (const char *) (Vallist), sizeof (*(Vallist))) | |
128 | ||
129 | #endif /* ARGMATCH_H_ */ |