|
Packit |
33f14e |
/* argmatch.h -- definitions and prototypes for argmatch.c
|
|
Packit |
33f14e |
|
|
Packit |
33f14e |
Copyright (C) 1990, 1998-1999, 2001-2002, 2004-2005, 2009-2017 Free Software
|
|
Packit |
33f14e |
Foundation, Inc.
|
|
Packit |
33f14e |
|
|
Packit |
33f14e |
This program is free software: you can redistribute it and/or modify
|
|
Packit |
33f14e |
it under the terms of the GNU General Public License as published by
|
|
Packit |
33f14e |
the Free Software Foundation; either version 3 of the License, or
|
|
Packit |
33f14e |
(at your option) any later version.
|
|
Packit |
33f14e |
|
|
Packit |
33f14e |
This program is distributed in the hope that it will be useful,
|
|
Packit |
33f14e |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
33f14e |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
Packit |
33f14e |
GNU General Public License for more details.
|
|
Packit |
33f14e |
|
|
Packit |
33f14e |
You should have received a copy of the GNU General Public License
|
|
Packit |
33f14e |
along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
|
Packit |
33f14e |
|
|
Packit |
33f14e |
/* Written by David MacKenzie <djm@ai.mit.edu>
|
|
Packit |
33f14e |
Modified by Akim Demaille <demaille@inf.enst.fr> */
|
|
Packit |
33f14e |
|
|
Packit |
33f14e |
#ifndef ARGMATCH_H_
|
|
Packit |
33f14e |
# define ARGMATCH_H_ 1
|
|
Packit |
33f14e |
|
|
Packit |
33f14e |
# include <stddef.h>
|
|
Packit |
33f14e |
|
|
Packit |
33f14e |
# include "verify.h"
|
|
Packit |
33f14e |
|
|
Packit |
33f14e |
#ifdef __cplusplus
|
|
Packit |
33f14e |
extern "C" {
|
|
Packit |
33f14e |
#endif
|
|
Packit |
33f14e |
|
|
Packit |
33f14e |
# define ARRAY_CARDINALITY(Array) (sizeof (Array) / sizeof *(Array))
|
|
Packit |
33f14e |
|
|
Packit |
33f14e |
/* Assert there are as many real arguments as there are values
|
|
Packit |
33f14e |
(argument list ends with a NULL guard). */
|
|
Packit |
33f14e |
|
|
Packit |
33f14e |
# define ARGMATCH_VERIFY(Arglist, Vallist) \
|
|
Packit |
33f14e |
verify (ARRAY_CARDINALITY (Arglist) == ARRAY_CARDINALITY (Vallist) + 1)
|
|
Packit |
33f14e |
|
|
Packit |
33f14e |
/* Return the index of the element of ARGLIST (NULL terminated) that
|
|
Packit |
33f14e |
matches with ARG. If VALLIST is not NULL, then use it to resolve
|
|
Packit |
33f14e |
false ambiguities (i.e., different matches of ARG but corresponding
|
|
Packit |
33f14e |
to the same values in VALLIST). */
|
|
Packit |
33f14e |
|
|
Packit |
33f14e |
ptrdiff_t argmatch (char const *arg, char const *const *arglist,
|
|
Packit |
33f14e |
char const *vallist, size_t valsize) _GL_ATTRIBUTE_PURE;
|
|
Packit |
33f14e |
|
|
Packit |
33f14e |
# define ARGMATCH(Arg, Arglist, Vallist) \
|
|
Packit |
33f14e |
argmatch (Arg, Arglist, (char const *) (Vallist), sizeof *(Vallist))
|
|
Packit |
33f14e |
|
|
Packit |
33f14e |
/* xargmatch calls this function when it fails. This function should not
|
|
Packit |
33f14e |
return. By default, this is a function that calls ARGMATCH_DIE which
|
|
Packit |
33f14e |
in turn defaults to 'exit (exit_failure)'. */
|
|
Packit |
33f14e |
typedef void (*argmatch_exit_fn) (void);
|
|
Packit |
33f14e |
extern argmatch_exit_fn argmatch_die;
|
|
Packit |
33f14e |
|
|
Packit |
33f14e |
/* Report on stderr why argmatch failed. Report correct values. */
|
|
Packit |
33f14e |
|
|
Packit |
33f14e |
void argmatch_invalid (char const *context, char const *value,
|
|
Packit |
33f14e |
ptrdiff_t problem);
|
|
Packit |
33f14e |
|
|
Packit |
33f14e |
/* Left for compatibility with the old name invalid_arg */
|
|
Packit |
33f14e |
|
|
Packit |
33f14e |
# define invalid_arg(Context, Value, Problem) \
|
|
Packit |
33f14e |
argmatch_invalid (Context, Value, Problem)
|
|
Packit |
33f14e |
|
|
Packit |
33f14e |
|
|
Packit |
33f14e |
|
|
Packit |
33f14e |
/* Report on stderr the list of possible arguments. */
|
|
Packit |
33f14e |
|
|
Packit |
33f14e |
void argmatch_valid (char const *const *arglist,
|
|
Packit |
33f14e |
char const *vallist, size_t valsize);
|
|
Packit |
33f14e |
|
|
Packit |
33f14e |
# define ARGMATCH_VALID(Arglist, Vallist) \
|
|
Packit |
33f14e |
argmatch_valid (Arglist, (char const *) (Vallist), sizeof *(Vallist))
|
|
Packit |
33f14e |
|
|
Packit |
33f14e |
|
|
Packit |
33f14e |
|
|
Packit |
33f14e |
/* Same as argmatch, but upon failure, report an explanation of the
|
|
Packit |
33f14e |
failure, and exit using the function EXIT_FN. */
|
|
Packit |
33f14e |
|
|
Packit |
33f14e |
ptrdiff_t __xargmatch_internal (char const *context,
|
|
Packit |
33f14e |
char const *arg, char const *const *arglist,
|
|
Packit |
33f14e |
char const *vallist, size_t valsize,
|
|
Packit |
33f14e |
argmatch_exit_fn exit_fn);
|
|
Packit |
33f14e |
|
|
Packit |
33f14e |
/* Programmer friendly interface to __xargmatch_internal. */
|
|
Packit |
33f14e |
|
|
Packit |
33f14e |
# define XARGMATCH(Context, Arg, Arglist, Vallist) \
|
|
Packit |
33f14e |
((Vallist) [__xargmatch_internal (Context, Arg, Arglist, \
|
|
Packit |
33f14e |
(char const *) (Vallist), \
|
|
Packit |
33f14e |
sizeof *(Vallist), \
|
|
Packit |
33f14e |
argmatch_die)])
|
|
Packit |
33f14e |
|
|
Packit |
33f14e |
/* Convert a value into a corresponding argument. */
|
|
Packit |
33f14e |
|
|
Packit |
33f14e |
char const *argmatch_to_argument (char const *value,
|
|
Packit |
33f14e |
char const *const *arglist,
|
|
Packit |
33f14e |
char const *vallist, size_t valsize)
|
|
Packit |
33f14e |
_GL_ATTRIBUTE_PURE;
|
|
Packit |
33f14e |
|
|
Packit |
33f14e |
# define ARGMATCH_TO_ARGUMENT(Value, Arglist, Vallist) \
|
|
Packit |
33f14e |
argmatch_to_argument (Value, Arglist, \
|
|
Packit |
33f14e |
(char const *) (Vallist), sizeof *(Vallist))
|
|
Packit |
33f14e |
|
|
Packit |
33f14e |
#ifdef __cplusplus
|
|
Packit |
33f14e |
}
|
|
Packit |
33f14e |
#endif
|
|
Packit |
33f14e |
|
|
Packit |
33f14e |
#endif /* ARGMATCH_H_ */
|