]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - contrib/gnu-sort/lib/argmatch.h
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / contrib / gnu-sort / lib / argmatch.h
1 /* argmatch.h -- definitions and prototypes for argmatch.c
2
3    Copyright (C) 1990, 1998, 1999, 2001, 2002, 2004 Free Software
4    Foundation, Inc.
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2, or (at your option)
9    any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software Foundation,
18    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
19
20 /* Written by David MacKenzie <djm@ai.mit.edu>
21    Modified by Akim Demaille <demaille@inf.enst.fr> */
22
23 #ifndef ARGMATCH_H_
24 # define ARGMATCH_H_ 1
25
26 # include <stddef.h>
27
28 # define ARRAY_CARDINALITY(Array) (sizeof (Array) / sizeof *(Array))
29
30 # define ARGMATCH_CONSTRAINT(Arglist, Vallist) \
31   (ARRAY_CARDINALITY (Arglist) == ARRAY_CARDINALITY (Vallist) + 1)
32
33 /* Assert there are as many real arguments as there are values
34    (argument list ends with a NULL guard).  ARGMATCH_VERIFY is
35    preferred, since it is guaranteed to be checked at compile-time.
36    ARGMATCH_ASSERT is for backward compatibility only.  */
37
38 # define ARGMATCH_VERIFY(Arglist, Vallist)                                \
39   struct argmatch_verify                                                  \
40   {                                                                       \
41     char argmatch_verify[ARGMATCH_CONSTRAINT(Arglist, Vallist) ? 1 : -1]; \
42   }
43
44 # define ARGMATCH_ASSERT(Arglist, Vallist) \
45   assert (ARGMATCH_CONSTRAINT (Arglist, Vallist))
46
47 /* Return the index of the element of ARGLIST (NULL terminated) that
48    matches with ARG.  If VALLIST is not NULL, then use it to resolve
49    false ambiguities (i.e., different matches of ARG but corresponding
50    to the same values in VALLIST).  */
51
52 ptrdiff_t argmatch (char const *arg, char const *const *arglist,
53                     char const *vallist, size_t valsize);
54
55 # define ARGMATCH(Arg, Arglist, Vallist) \
56   argmatch (Arg, Arglist, (char const *) (Vallist), sizeof *(Vallist))
57
58 /* xargmatch calls this function when it fails.  This function should not
59    return.  By default, this is a function that calls ARGMATCH_DIE which
60    in turn defaults to `exit (exit_failure)'.  */
61 typedef void (*argmatch_exit_fn) (void);
62 extern argmatch_exit_fn argmatch_die;
63
64 /* Report on stderr why argmatch failed.  Report correct values. */
65
66 void argmatch_invalid (char const *context, char const *value,
67                        ptrdiff_t problem);
68
69 /* Left for compatibility with the old name invalid_arg */
70
71 # define invalid_arg(Context, Value, Problem) \
72   argmatch_invalid (Context, Value, Problem)
73
74
75
76 /* Report on stderr the list of possible arguments.  */
77
78 void argmatch_valid (char const *const *arglist,
79                      char const *vallist, size_t valsize);
80
81 # define ARGMATCH_VALID(Arglist, Vallist) \
82   argmatch_valid (Arglist, (char const *) (Vallist), sizeof *(Vallist))
83
84
85
86 /* Same as argmatch, but upon failure, reports a explanation on the
87    failure, and exits using the function EXIT_FN. */
88
89 ptrdiff_t __xargmatch_internal (char const *context,
90                                 char const *arg, char const *const *arglist,
91                                 char const *vallist, size_t valsize,
92                                 argmatch_exit_fn exit_fn);
93
94 /* Programmer friendly interface to __xargmatch_internal. */
95
96 # define XARGMATCH(Context, Arg, Arglist, Vallist)              \
97   ((Vallist) [__xargmatch_internal (Context, Arg, Arglist,      \
98                                     (char const *) (Vallist),   \
99                                     sizeof *(Vallist),          \
100                                     argmatch_die)])
101
102 /* Convert a value into a corresponding argument. */
103
104 char const *argmatch_to_argument (char const *value,
105                                   char const *const *arglist,
106                                   char const *vallist, size_t valsize);
107
108 # define ARGMATCH_TO_ARGUMENT(Value, Arglist, Vallist)                  \
109   argmatch_to_argument (Value, Arglist,                                 \
110                         (char const *) (Vallist), sizeof *(Vallist))
111
112 #endif /* ARGMATCH_H_ */