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