1 /* argmatch.c -- find a match for a string in an array
2
3 Copyright (C) 1990, 1998-1999, 2001-2007, 2009-2022 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 <djm@ai.mit.edu>
20 Modified by Akim Demaille <demaille@inf.enst.fr> */
21
22 #include <config.h>
23
24 /* Specification. */
25 #include "argmatch.h"
26
27 #include <stdbool.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31
32 #define _(msgid) gettext (msgid)
33
34 #include "error.h"
35 #include "quotearg.h"
36 #include "getprogname.h"
37
38 #if USE_UNLOCKED_IO
39 # include "unlocked-io.h"
40 #endif
41
42 /* When reporting an invalid argument, show nonprinting characters
43 by using the quoting style ARGMATCH_QUOTING_STYLE. Do not use
44 literal_quoting_style. */
45 #ifndef ARGMATCH_QUOTING_STYLE
46 # define ARGMATCH_QUOTING_STYLE locale_quoting_style
47 #endif
48
49 /* Non failing version of argmatch call this function after failing. */
50 #ifndef ARGMATCH_DIE
51 # include "exitfail.h"
52 # define ARGMATCH_DIE exit (exit_failure)
53 #endif
54
55 #ifdef ARGMATCH_DIE_DECL
56 ARGMATCH_DIE_DECL;
57 #endif
58
59 static void
60 __argmatch_die (void)
61 {
62 ARGMATCH_DIE;
63 }
64
65 /* Used by XARGMATCH. See description in argmatch.h.
66 Default to __argmatch_die, but allow caller to change this at run-time. */
67 argmatch_exit_fn argmatch_die = __argmatch_die;
68
69
70 /* If ARG is an unambiguous match for an element of the
71 NULL-terminated array ARGLIST, return the index in ARGLIST
72 of the matched element, else -1 if it does not match any element
73 or -2 if it is ambiguous (is a prefix of more than one element).
74
75 If VALLIST is none null, use it to resolve ambiguities limited to
76 synonyms, i.e., for
77 "yes", "yop" -> 0
78 "no", "nope" -> 1
79 "y" is a valid argument, for 0, and "n" for 1. */
80
81 ptrdiff_t
82 argmatch (const char *arg, const char *const *arglist,
83 const void *vallist, size_t valsize)
84 {
85 size_t i; /* Temporary index in ARGLIST. */
86 size_t arglen; /* Length of ARG. */
87 ptrdiff_t matchind = -1; /* Index of first nonexact match. */
88 bool ambiguous = false; /* If true, multiple nonexact match(es). */
89
90 arglen = strlen (arg);
91
92 /* Test all elements for either exact match or abbreviated matches. */
93 for (i = 0; arglist[i]; i++)
94 {
95 if (!strncmp (arglist[i], arg, arglen))
96 {
97 if (strlen (arglist[i]) == arglen)
98 /* Exact match found. */
99 return i;
100 else if (matchind == -1)
101 /* First nonexact match found. */
102 matchind = i;
103 else
104 {
105 /* Second nonexact match found. */
106 if (vallist == NULL
107 || memcmp ((char const *) vallist + valsize * matchind,
108 (char const *) vallist + valsize * i, valsize))
109 {
110 /* There is a real ambiguity, or we could not
111 disambiguate. */
112 ambiguous = true;
113 }
114 }
115 }
116 }
117 if (ambiguous)
118 return -2;
119 else
120 return matchind;
121 }
122
123 ptrdiff_t
124 argmatch_exact (const char *arg, const char *const *arglist)
125 {
126 size_t i;
127
128 /* Test elements for exact match. */
129 for (i = 0; arglist[i]; i++)
130 {
131 if (!strcmp (arglist[i], arg))
132 return i;
133 }
134
135 return -1;
136 }
137
138 /* Error reporting for argmatch.
139 CONTEXT is a description of the type of entity that was being matched.
140 VALUE is the invalid value that was given.
141 PROBLEM is the return value from argmatch. */
142
143 void
144 argmatch_invalid (const char *context, const char *value, ptrdiff_t problem)
145 {
146 char const *format = (problem == -1
147 ? _("invalid argument %s for %s")
148 : _("ambiguous argument %s for %s"));
149
150 error (0, 0, format, quotearg_n_style (0, ARGMATCH_QUOTING_STYLE, value),
151 quote_n (1, context));
152 }
153
154 /* List the valid arguments for argmatch.
155 ARGLIST is the same as in argmatch.
156 VALLIST is a pointer to an array of values.
157 VALSIZE is the size of the elements of VALLIST */
158 void
159 argmatch_valid (const char *const *arglist,
160 const void *vallist, size_t valsize)
161 {
162 size_t i;
163 const char *last_val = NULL;
164
165 /* We try to put synonyms on the same line. The assumption is that
166 synonyms follow each other */
167 fputs (_("Valid arguments are:"), stderr);
168 for (i = 0; arglist[i]; i++)
169 if ((i == 0)
170 || memcmp (last_val, (char const *) vallist + valsize * i, valsize))
171 {
172 fprintf (stderr, "\n - %s", quote (arglist[i]));
173 last_val = (char const *) vallist + valsize * i;
174 }
175 else
176 {
177 fprintf (stderr, ", %s", quote (arglist[i]));
178 }
179 putc ('\n', stderr);
180 }
181
182 /* Never failing versions of the previous functions.
183
184 CONTEXT is the context for which argmatch is called (e.g.,
185 "--version-control", or "$VERSION_CONTROL" etc.). Upon failure,
186 calls the (supposed never to return) function EXIT_FN. */
187
188 ptrdiff_t
189 __xargmatch_internal (const char *context,
190 const char *arg, const char *const *arglist,
191 const void *vallist, size_t valsize,
192 argmatch_exit_fn exit_fn,
193 bool allow_abbreviation)
194 {
195 ptrdiff_t res;
196
197 if (allow_abbreviation)
198 res = argmatch (arg, arglist, vallist, valsize);
199 else
200 res = argmatch_exact (arg, arglist);
201
202 if (res >= 0)
203 /* Success. */
204 return res;
205
206 /* We failed. Explain why. */
207 argmatch_invalid (context, arg, res);
208 argmatch_valid (arglist, vallist, valsize);
209 (*exit_fn) ();
210
211 return -1; /* To please the compilers. */
212 }
213
214 /* Look for VALUE in VALLIST, an array of objects of size VALSIZE and
215 return the first corresponding argument in ARGLIST */
216 const char *
217 argmatch_to_argument (const void *value,
218 const char *const *arglist,
219 const void *vallist, size_t valsize)
220 {
221 size_t i;
222
223 for (i = 0; arglist[i]; i++)
224 if (!memcmp (value, (char const *) vallist + valsize * i, valsize))
225 return arglist[i];
226 return NULL;
227 }
228
229 #ifdef TEST
230 /*
231 * Based on "getversion.c" by David MacKenzie <djm@gnu.ai.mit.edu>
232 */
233
234 /* When to make backup files. */
235 enum backup_type
236 {
237 /* Never make backups. */
238 no_backups,
239
240 /* Make simple backups of every file. */
241 simple_backups,
242
243 /* Make numbered backups of files that already have numbered backups,
244 and simple backups of the others. */
245 numbered_existing_backups,
246
247 /* Make numbered backups of every file. */
248 numbered_backups
249 };
250
251 /* Two tables describing arguments (keys) and their corresponding
252 values */
253 static const char *const backup_args[] =
254 {
255 "no", "none", "off",
256 "simple", "never",
257 "existing", "nil",
258 "numbered", "t",
259 0
260 };
261
262 static const enum backup_type backup_vals[] =
263 {
264 no_backups, no_backups, no_backups,
265 simple_backups, simple_backups,
266 numbered_existing_backups, numbered_existing_backups,
267 numbered_backups, numbered_backups
268 };
269
270 int
271 main (int argc, const char *const *argv)
272 {
273 const char *cp;
274 enum backup_type backup_type = no_backups;
275
276 if (argc > 2)
277 {
278 fprintf (stderr, "Usage: %s [VERSION_CONTROL]\n", getprogname ());
279 exit (1);
280 }
281
282 if ((cp = getenv ("VERSION_CONTROL")))
283 backup_type = XARGMATCH ("$VERSION_CONTROL", cp,
284 backup_args, backup_vals);
285
286 if (argc == 2)
287 backup_type = XARGMATCH (getprogname (), argv[1],
288 backup_args, backup_vals);
289
290 printf ("The version control is '%s'\n",
291 ARGMATCH_TO_ARGUMENT (&backup_type, backup_args, backup_vals));
292
293 return 0;
294 }
295 #endif