Blame lib/argmatch.h

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