Blame lib/argmatch.h

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