argmatch.c 7.2 KB

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