Debian Codegraph @

lib/argmatch.h

raw symbols SCIP
1 /* argmatch.h -- definitions and prototypes for argmatch.c
2
3 Copyright (C) 1990, 1998-1999, 2001-2002, 2004-2005, 2009-2023 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 3 of the License, or
9 (at your option) 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, see <https://www.gnu.org/licenses/>. */
18
19 /* Written by David MacKenzie <[email protected]>
20 Modified by Akim Demaille <[email protected]> */
21
22 #ifndef ARGMATCH_H_
23 # define ARGMATCH_H_ 1
24
25 # include <limits.h>
26 # include <stddef.h>
27 # include <stdio.h>
28 # include <string.h> /* memcmp */
29
30 # include "gettext.h"
31 # include "quote.h"
32
33 # ifdef __cplusplus
34 extern "C" {
35 # endif
36
37 # define ARRAY_CARDINALITY(Array) (sizeof (Array) / sizeof *(Array))
38
39 /* Assert there are as many real arguments as there are values
40 (argument list ends with a NULL guard). */
41
42 # define ARGMATCH_VERIFY(Arglist, Vallist) \
43 static_assert (ARRAY_CARDINALITY (Arglist) \
44 == ARRAY_CARDINALITY (Vallist) + 1)
45
46 /* Return the index of the element of ARGLIST (NULL terminated) that
47 matches with ARG. If VALLIST is not NULL, then use it to resolve
48 false ambiguities (i.e., different matches of ARG but corresponding
49 to the same values in VALLIST). */
50
51 ptrdiff_t argmatch (char const *arg, char const *const *arglist,
52 void const *vallist, size_t valsize) _GL_ATTRIBUTE_PURE;
53
54 ptrdiff_t argmatch_exact (char const *arg, char const *const *arglist)
55 _GL_ATTRIBUTE_PURE;
56
57 # define ARGMATCH(Arg, Arglist, Vallist) \
58 argmatch (Arg, Arglist, (void const *) (Vallist), sizeof *(Vallist))
59
60 # define ARGMATCH_EXACT(Arg, Arglist) \
61 argmatch_exact (Arg, Arglist)
62
63 /* xargmatch calls this function when it fails. This function should not
64 return. By default, this is a function that calls ARGMATCH_DIE which
65 in turn defaults to 'exit (exit_failure)'. */
66 typedef void (*argmatch_exit_fn) (void);
67 extern argmatch_exit_fn argmatch_die;
68
69 /* Report on stderr why argmatch failed. Report correct values. */
70
71 void argmatch_invalid (char const *context, char const *value,
72 ptrdiff_t problem);
73
74 /* Left for compatibility with the old name invalid_arg */
75
76 # define invalid_arg(Context, Value, Problem) \
77 argmatch_invalid (Context, Value, Problem)
78
79
80
81 /* Report on stderr the list of possible arguments. */
82
83 void argmatch_valid (char const *const *arglist,
84 void const *vallist, size_t valsize);
85
86 # define ARGMATCH_VALID(Arglist, Vallist) \
87 argmatch_valid (Arglist, (void const *) (Vallist), sizeof *(Vallist))
88
89
90
91 /* Like argmatch/argmatch_exact, but upon failure, report an explanation
92 of the failure, and exit using the function EXIT_FN. */
93
94 ptrdiff_t __xargmatch_internal (char const *context,
95 char const *arg, char const *const *arglist,
96 void const *vallist, size_t valsize,
97 argmatch_exit_fn exit_fn,
98 bool allow_abbreviation);
99
100 /* Programmer friendly interface to __xargmatch_internal. */
101
102 # define XARGMATCH(Context, Arg, Arglist, Vallist) \
103 ((Vallist) [__xargmatch_internal (Context, Arg, Arglist, \
104 (void const *) (Vallist), \
105 sizeof *(Vallist), \
106 argmatch_die, \
107 true)])
108
109 # define XARGMATCH_EXACT(Context, Arg, Arglist, Vallist) \
110 ((Vallist) [__xargmatch_internal (Context, Arg, Arglist, \
111 (void const *) (Vallist), \
112 sizeof *(Vallist), \
113 argmatch_die, \
114 false)])
115
116 /* Convert a value into a corresponding argument. */
117
118 char const *argmatch_to_argument (void const *value,
119 char const *const *arglist,
120 void const *vallist, size_t valsize)
121 _GL_ATTRIBUTE_PURE;
122
123 # define ARGMATCH_TO_ARGUMENT(Value, Arglist, Vallist) \
124 argmatch_to_argument (Value, Arglist, \
125 (void const *) (Vallist), sizeof *(Vallist))
126
127 # define ARGMATCH_DEFINE_GROUP(Name, Type) \
128 /* The type of the values of this group. */ \
129 typedef Type argmatch_##Name##_type; \
130 \
131 /* The size of the type of the values of this group. */ \
132 enum argmatch_##Name##_size_enum \
133 { \
134 argmatch_##Name##_size = sizeof (argmatch_##Name##_type) \
135 }; \
136 \
137 /* Argument mapping of this group. */ \
138 typedef struct \
139 { \
140 /* Argument (e.g., "simple"). */ \
141 const char *arg; \
142 /* Value (e.g., simple_backups). */ \
143 const argmatch_##Name##_type val; \
144 } argmatch_##Name##_arg; \
145 \
146 /* Documentation of this group. */ \
147 typedef struct \
148 { \
149 /* Argument (e.g., "simple"). */ \
150 const char *arg; \
151 /* Documentation (e.g., N_("always make simple backups")). */ \
152 const char *doc; \
153 } argmatch_##Name##_doc; \
154 \
155 /* All the features of an argmatch group. */ \
156 typedef struct \
157 { \
158 const argmatch_##Name##_arg* args; \
159 const argmatch_##Name##_doc* docs; \
160 \
161 /* Printed before the usage message. */ \
162 const char *doc_pre; \
163 /* Printed after the usage message. */ \
164 const char *doc_post; \
165 } argmatch_##Name##_group_type; \
166 \
167 /* The structure the user must build. */ \
168 extern const argmatch_##Name##_group_type argmatch_##Name##_group; \
169 \
170 /* Print the documentation of this group. */ \
171 void argmatch_##Name##_usage (FILE *out); \
172 \
173 /* If nonnegative, the index I of ARG in ARGS, i.e, \
174 ARGS[I] == ARG. \
175 Return -1 for invalid argument, -2 for ambiguous argument. */ \
176 ptrdiff_t argmatch_##Name##_choice (const char *arg); \
177 \
178 /* A pointer to the corresponding value if it exists, or \
179 report an error and exit with failure if the argument was \
180 not recognized. */ \
181 const argmatch_##Name##_type* \
182 argmatch_##Name##_value (const char *context, const char *arg); \
183 \
184 /* The first argument in ARGS that matches this value, or NULL. */ \
185 const char * \
186 argmatch_##Name##_argument (const argmatch_##Name##_type *val); \
187 \
188 ptrdiff_t \
189 argmatch_##Name##_choice (const char *arg) \
190 { \
191 const argmatch_##Name##_group_type *g = &argmatch_##Name##_group; \
192 size_t size = argmatch_##Name##_size; \
193 ptrdiff_t res = -1; /* Index of first nonexact match. */ \
194 bool ambiguous = false; /* Whether multiple nonexact match(es). */ \
195 size_t arglen = strlen (arg); \
196 \
197 /* Test all elements for either exact match or abbreviated \
198 matches. */ \
199 for (size_t i = 0; g->args[i].arg; i++) \
200 if (!strncmp (g->args[i].arg, arg, arglen)) \
201 { \
202 if (strlen (g->args[i].arg) == arglen) \
203 /* Exact match found. */ \
204 return i; \
205 else if (res == -1) \
206 /* First nonexact match found. */ \
207 res = i; \
208 else if (memcmp (&g->args[res].val, &g->args[i].val, size)) \
209 /* Second nonexact match found. */ \
210 /* There is a real ambiguity, or we could not \
211 disambiguate. */ \
212 ambiguous = true; \
213 } \
214 return ambiguous ? -2 : res; \
215 } \
216 \
217 const char * \
218 argmatch_##Name##_argument (const argmatch_##Name##_type *val) \
219 { \
220 const argmatch_##Name##_group_type *g = &argmatch_##Name##_group; \
221 size_t size = argmatch_##Name##_size; \
222 for (size_t i = 0; g->args[i].arg; i++) \
223 if (!memcmp (val, &g->args[i].val, size)) \
224 return g->args[i].arg; \
225 return NULL; \
226 } \
227 \
228 /* List the valid values of this group. */ \
229 static void \
230 argmatch_##Name##_valid (FILE *out) \
231 { \
232 const argmatch_##Name##_group_type *g = &argmatch_##Name##_group; \
233 size_t size = argmatch_##Name##_size; \
234 \
235 /* Try to put synonyms on the same line. Synonyms are expected \
236 to follow each other. */ \
237 fputs (gettext ("Valid arguments are:"), out); \
238 for (int i = 0; g->args[i].arg; i++) \
239 if (i == 0 \
240 || memcmp (&g->args[i-1].val, &g->args[i].val, size)) \
241 fprintf (out, "\n - %s", quote (g->args[i].arg)); \
242 else \
243 fprintf (out, ", %s", quote (g->args[i].arg)); \
244 putc ('\n', out); \
245 } \
246 \
247 const argmatch_##Name##_type* \
248 argmatch_##Name##_value (const char *context, const char *arg) \
249 { \
250 const argmatch_##Name##_group_type *g = &argmatch_##Name##_group; \
251 ptrdiff_t res = argmatch_##Name##_choice (arg); \
252 if (res < 0) \
253 { \
254 argmatch_invalid (context, arg, res); \
255 argmatch_##Name##_valid (stderr); \
256 argmatch_die (); \
257 } \
258 return &g->args[res].val; \
259 } \
260 \
261 /* The column in which the documentation is displayed. \
262 The leftmost possible, but no more than 20. */ \
263 static int \
264 argmatch_##Name##_doc_col (void) \
265 { \
266 const argmatch_##Name##_group_type *g = &argmatch_##Name##_group; \
267 size_t size = argmatch_##Name##_size; \
268 int res = 0; \
269 for (int i = 0; g->docs[i].arg; ++i) \
270 { \
271 int col = 4; \
272 int ival = argmatch_##Name##_choice (g->docs[i].arg); \
273 if (ival < 0) \
274 /* Pseudo argument, display it. */ \
275 col += strlen (g->docs[i].arg); \
276 else \
277 /* Genuine argument, display it with its synonyms. */ \
278 for (int j = 0; g->args[j].arg; ++j) \
279 if (! memcmp (&g->args[ival].val, &g->args[j].val, size)) \
280 col += (col == 4 ? 0 : 2) + strlen (g->args[j].arg); \
281 if (res <= col) \
282 res = col <= 20 ? col : 20; \
283 } \
284 return res ? res : 20; \
285 } \
286 \
287 void \
288 argmatch_##Name##_usage (FILE *out) \
289 { \
290 const argmatch_##Name##_group_type *g = &argmatch_##Name##_group; \
291 size_t size = argmatch_##Name##_size; \
292 /* Width of the screen. Help2man does not seem to support \
293 arguments on several lines, so in that case pretend a very \
294 large width. */ \
295 const int screen_width = getenv ("HELP2MAN") ? INT_MAX : 80; \
296 if (g->doc_pre) \
297 fprintf (out, "%s\n", gettext (g->doc_pre)); \
298 int doc_col = argmatch_##Name##_doc_col (); \
299 for (int i = 0; g->docs[i].arg; ++i) \
300 { \
301 int col = 0; \
302 bool first = true; \
303 int ival = argmatch_##Name##_choice (g->docs[i].arg); \
304 if (ival < 0) \
305 /* Pseudo argument, display it. */ \
306 col += fprintf (out, " %s", g->docs[i].arg); \
307 else \
308 /* Genuine argument, display it with its synonyms. */ \
309 for (int j = 0; g->args[j].arg; ++j) \
310 if (! memcmp (&g->args[ival].val, &g->args[j].val, size)) \
311 { \
312 if (!first \
313 && screen_width < col + 2 + strlen (g->args[j].arg)) \
314 { \
315 fprintf (out, ",\n"); \
316 col = 0; \
317 first = true; \
318 } \
319 if (first) \
320 { \
321 col += fprintf (out, " "); \
322 first = false; \
323 } \
324 else \
325 col += fprintf (out, ","); \
326 col += fprintf (out, " %s", g->args[j].arg); \
327 } \
328 /* The doc. Separated by at least two spaces. */ \
329 if (doc_col < col + 2) \
330 { \
331 fprintf (out, "\n"); \
332 col = 0; \
333 } \
334 fprintf (out, "%*s%s\n", \
335 doc_col - col, "", gettext (g->docs[i].doc)); \
336 } \
337 if (g->doc_post) \
338 fprintf (out, "%s\n", gettext (g->doc_post)); \
339 }
340
341 # ifdef __cplusplus
342 }
343 # endif
344
345 #endif /* ARGMATCH_H_ */