| 1 | /* argmatch.h -- definitions and prototypes for argmatch.c | 
|---|
| 2 | Copyright (C) 1990, 1998, 1999, 2001, 2002 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 | # include <stddef.h> | 
|---|
| 25 |  | 
|---|
| 26 | # define ARRAY_CARDINALITY(Array) (sizeof (Array) / sizeof *(Array)) | 
|---|
| 27 |  | 
|---|
| 28 | # define ARGMATCH_CONSTRAINT(Arglist, Vallist) \ | 
|---|
| 29 | (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 checked 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) \ | 
|---|
| 43 | assert (ARGMATCH_CONSTRAINT (Arglist, Vallist)) | 
|---|
| 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 |  | 
|---|
| 50 | int argmatch (char const *arg, char const *const *arglist, | 
|---|
| 51 | char const *vallist, size_t valsize); | 
|---|
| 52 |  | 
|---|
| 53 | # define ARGMATCH(Arg, Arglist, Vallist) \ | 
|---|
| 54 | argmatch (Arg, Arglist, (char const *) (Vallist), sizeof *(Vallist)) | 
|---|
| 55 |  | 
|---|
| 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)'.  */ | 
|---|
| 59 | typedef void (*argmatch_exit_fn) (void); | 
|---|
| 60 | extern argmatch_exit_fn argmatch_die; | 
|---|
| 61 |  | 
|---|
| 62 | /* Report on stderr why argmatch failed.  Report correct values. */ | 
|---|
| 63 |  | 
|---|
| 64 | void argmatch_invalid (char const *context, char const *value, int problem); | 
|---|
| 65 |  | 
|---|
| 66 | /* Left for compatibility with the old name invalid_arg */ | 
|---|
| 67 |  | 
|---|
| 68 | # define invalid_arg(Context, Value, Problem) \ | 
|---|
| 69 | argmatch_invalid (Context, Value, Problem) | 
|---|
| 70 |  | 
|---|
| 71 |  | 
|---|
| 72 |  | 
|---|
| 73 | /* Report on stderr the list of possible arguments.  */ | 
|---|
| 74 |  | 
|---|
| 75 | void argmatch_valid (char const *const *arglist, | 
|---|
| 76 | char const *vallist, size_t valsize); | 
|---|
| 77 |  | 
|---|
| 78 | # define ARGMATCH_VALID(Arglist, Vallist) \ | 
|---|
| 79 | argmatch_valid (Arglist, (char const *) (Vallist), sizeof *(Vallist)) | 
|---|
| 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 |  | 
|---|
| 86 | int __xargmatch_internal (char const *context, | 
|---|
| 87 | char const *arg, char const *const *arglist, | 
|---|
| 88 | char const *vallist, size_t valsize, | 
|---|
| 89 | argmatch_exit_fn exit_fn); | 
|---|
| 90 |  | 
|---|
| 91 | /* Programmer friendly interface to __xargmatch_internal. */ | 
|---|
| 92 |  | 
|---|
| 93 | # define XARGMATCH(Context, Arg, Arglist, Vallist)              \ | 
|---|
| 94 | ((Vallist) [__xargmatch_internal (Context, Arg, Arglist,      \ | 
|---|
| 95 | (char const *) (Vallist),   \ | 
|---|
| 96 | sizeof *(Vallist),          \ | 
|---|
| 97 | argmatch_die)]) | 
|---|
| 98 |  | 
|---|
| 99 | /* Convert a value into a corresponding argument. */ | 
|---|
| 100 |  | 
|---|
| 101 | char const *argmatch_to_argument (char const *value, | 
|---|
| 102 | char const *const *arglist, | 
|---|
| 103 | char const *vallist, size_t valsize); | 
|---|
| 104 |  | 
|---|
| 105 | # define ARGMATCH_TO_ARGUMENT(Value, Arglist, Vallist)                  \ | 
|---|
| 106 | argmatch_to_argument (Value, Arglist,                                 \ | 
|---|
| 107 | (char const *) (Vallist), sizeof *(Vallist)) | 
|---|
| 108 |  | 
|---|
| 109 | #endif /* ARGMATCH_H_ */ | 
|---|