argmatch.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /* argmatch.c -- find a match for a string in an array
  2. Copyright (C) 1990, 1998-1999, 2001-2007, 2009-2020 Free Software
  3. Foundation, Inc.
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <https://www.gnu.org/licenses/>. */
  14. /* Written by David MacKenzie <djm@ai.mit.edu>
  15. Modified by Akim Demaille <demaille@inf.enst.fr> */
  16. #include <config.h>
  17. /* Specification. */
  18. #include "argmatch.h"
  19. #include <stdbool.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #define _(msgid) gettext (msgid)
  24. #include "error.h"
  25. #include "quotearg.h"
  26. #include "getprogname.h"
  27. #if USE_UNLOCKED_IO
  28. # include "unlocked-io.h"
  29. #endif
  30. /* When reporting an invalid argument, show nonprinting characters
  31. by using the quoting style ARGMATCH_QUOTING_STYLE. Do not use
  32. literal_quoting_style. */
  33. #ifndef ARGMATCH_QUOTING_STYLE
  34. # define ARGMATCH_QUOTING_STYLE locale_quoting_style
  35. #endif
  36. /* Non failing version of argmatch call this function after failing. */
  37. #ifndef ARGMATCH_DIE
  38. # include "exitfail.h"
  39. # define ARGMATCH_DIE exit (exit_failure)
  40. #endif
  41. #ifdef ARGMATCH_DIE_DECL
  42. ARGMATCH_DIE_DECL;
  43. #endif
  44. static void
  45. __argmatch_die (void)
  46. {
  47. ARGMATCH_DIE;
  48. }
  49. /* Used by XARGMATCH and XARGCASEMATCH. See description in argmatch.h.
  50. Default to __argmatch_die, but allow caller to change this at run-time. */
  51. argmatch_exit_fn argmatch_die = __argmatch_die;
  52. /* If ARG is an unambiguous match for an element of the
  53. NULL-terminated array ARGLIST, return the index in ARGLIST
  54. of the matched element, else -1 if it does not match any element
  55. or -2 if it is ambiguous (is a prefix of more than one element).
  56. If VALLIST is none null, use it to resolve ambiguities limited to
  57. synonyms, i.e., for
  58. "yes", "yop" -> 0
  59. "no", "nope" -> 1
  60. "y" is a valid argument, for 0, and "n" for 1. */
  61. ptrdiff_t
  62. argmatch (const char *arg, const char *const *arglist,
  63. const void *vallist, size_t valsize)
  64. {
  65. size_t i; /* Temporary index in ARGLIST. */
  66. size_t arglen; /* Length of ARG. */
  67. ptrdiff_t matchind = -1; /* Index of first nonexact match. */
  68. bool ambiguous = false; /* If true, multiple nonexact match(es). */
  69. arglen = strlen (arg);
  70. /* Test all elements for either exact match or abbreviated matches. */
  71. for (i = 0; arglist[i]; i++)
  72. {
  73. if (!strncmp (arglist[i], arg, arglen))
  74. {
  75. if (strlen (arglist[i]) == arglen)
  76. /* Exact match found. */
  77. return i;
  78. else if (matchind == -1)
  79. /* First nonexact match found. */
  80. matchind = i;
  81. else
  82. {
  83. /* Second nonexact match found. */
  84. if (vallist == NULL
  85. || memcmp ((char const *) vallist + valsize * matchind,
  86. (char const *) vallist + valsize * i, valsize))
  87. {
  88. /* There is a real ambiguity, or we could not
  89. disambiguate. */
  90. ambiguous = true;
  91. }
  92. }
  93. }
  94. }
  95. if (ambiguous)
  96. return -2;
  97. else
  98. return matchind;
  99. }
  100. /* Error reporting for argmatch.
  101. CONTEXT is a description of the type of entity that was being matched.
  102. VALUE is the invalid value that was given.
  103. PROBLEM is the return value from argmatch. */
  104. void
  105. argmatch_invalid (const char *context, const char *value, ptrdiff_t problem)
  106. {
  107. char const *format = (problem == -1
  108. ? _("invalid argument %s for %s")
  109. : _("ambiguous argument %s for %s"));
  110. error (0, 0, format, quotearg_n_style (0, ARGMATCH_QUOTING_STYLE, value),
  111. quote_n (1, context));
  112. }
  113. /* List the valid arguments for argmatch.
  114. ARGLIST is the same as in argmatch.
  115. VALLIST is a pointer to an array of values.
  116. VALSIZE is the size of the elements of VALLIST */
  117. void
  118. argmatch_valid (const char *const *arglist,
  119. const void *vallist, size_t valsize)
  120. {
  121. size_t i;
  122. const char *last_val = NULL;
  123. /* We try to put synonyms on the same line. The assumption is that
  124. synonyms follow each other */
  125. fputs (_("Valid arguments are:"), stderr);
  126. for (i = 0; arglist[i]; i++)
  127. if ((i == 0)
  128. || memcmp (last_val, (char const *) vallist + valsize * i, valsize))
  129. {
  130. fprintf (stderr, "\n - %s", quote (arglist[i]));
  131. last_val = (char const *) vallist + valsize * i;
  132. }
  133. else
  134. {
  135. fprintf (stderr, ", %s", quote (arglist[i]));
  136. }
  137. putc ('\n', stderr);
  138. }
  139. /* Never failing versions of the previous functions.
  140. CONTEXT is the context for which argmatch is called (e.g.,
  141. "--version-control", or "$VERSION_CONTROL" etc.). Upon failure,
  142. calls the (supposed never to return) function EXIT_FN. */
  143. ptrdiff_t
  144. __xargmatch_internal (const char *context,
  145. const char *arg, const char *const *arglist,
  146. const void *vallist, size_t valsize,
  147. argmatch_exit_fn exit_fn)
  148. {
  149. ptrdiff_t res = argmatch (arg, arglist, vallist, valsize);
  150. if (res >= 0)
  151. /* Success. */
  152. return res;
  153. /* We failed. Explain why. */
  154. argmatch_invalid (context, arg, res);
  155. argmatch_valid (arglist, vallist, valsize);
  156. (*exit_fn) ();
  157. return -1; /* To please the compilers. */
  158. }
  159. /* Look for VALUE in VALLIST, an array of objects of size VALSIZE and
  160. return the first corresponding argument in ARGLIST */
  161. const char *
  162. argmatch_to_argument (const void *value,
  163. const char *const *arglist,
  164. const void *vallist, size_t valsize)
  165. {
  166. size_t i;
  167. for (i = 0; arglist[i]; i++)
  168. if (!memcmp (value, (char const *) vallist + valsize * i, valsize))
  169. return arglist[i];
  170. return NULL;
  171. }
  172. #ifdef TEST
  173. /*
  174. * Based on "getversion.c" by David MacKenzie <djm@gnu.ai.mit.edu>
  175. */
  176. /* When to make backup files. */
  177. enum backup_type
  178. {
  179. /* Never make backups. */
  180. no_backups,
  181. /* Make simple backups of every file. */
  182. simple_backups,
  183. /* Make numbered backups of files that already have numbered backups,
  184. and simple backups of the others. */
  185. numbered_existing_backups,
  186. /* Make numbered backups of every file. */
  187. numbered_backups
  188. };
  189. /* Two tables describing arguments (keys) and their corresponding
  190. values */
  191. static const char *const backup_args[] =
  192. {
  193. "no", "none", "off",
  194. "simple", "never",
  195. "existing", "nil",
  196. "numbered", "t",
  197. 0
  198. };
  199. static const enum backup_type backup_vals[] =
  200. {
  201. no_backups, no_backups, no_backups,
  202. simple_backups, simple_backups,
  203. numbered_existing_backups, numbered_existing_backups,
  204. numbered_backups, numbered_backups
  205. };
  206. int
  207. main (int argc, const char *const *argv)
  208. {
  209. const char *cp;
  210. enum backup_type backup_type = no_backups;
  211. if (argc > 2)
  212. {
  213. fprintf (stderr, "Usage: %s [VERSION_CONTROL]\n", getprogname ());
  214. exit (1);
  215. }
  216. if ((cp = getenv ("VERSION_CONTROL")))
  217. backup_type = XARGMATCH ("$VERSION_CONTROL", cp,
  218. backup_args, backup_vals);
  219. if (argc == 2)
  220. backup_type = XARGMATCH (getprogname (), argv[1],
  221. backup_args, backup_vals);
  222. printf ("The version control is '%s'\n",
  223. ARGMATCH_TO_ARGUMENT (backup_type, backup_args, backup_vals));
  224. return 0;
  225. }
  226. #endif