Blame lib/argmatch.h

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