argmatch.c 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /* argmatch.c -- find a match for a string in an array
  2. Copyright (C) 1990, 1997 Free Software Foundation, Inc.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2, or (at your option)
  6. any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; see the file COPYING.
  13. If not, write to the Free Software Foundation,
  14. 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  15. /* Written by David MacKenzie <djm@gnu.ai.mit.edu> */
  16. #if HAVE_CONFIG_H
  17. # include <config.h>
  18. #endif
  19. #include <argmatch.h>
  20. #include <sys/types.h>
  21. #include <stdio.h>
  22. #if HAVE_STRING_H
  23. # include <string.h>
  24. #else
  25. # include <strings.h>
  26. #endif
  27. /* If ARG is an unambiguous match for an element of the
  28. null-terminated array OPTLIST, return the index in OPTLIST
  29. of the matched element, else -1 if it does not match any element
  30. or -2 if it is ambiguous (is a prefix of more than one element). */
  31. int
  32. argmatch (arg, optlist)
  33. const char *arg;
  34. const char *const *optlist;
  35. {
  36. int i; /* Temporary index in OPTLIST. */
  37. size_t arglen; /* Length of ARG. */
  38. int matchind = -1; /* Index of first nonexact match. */
  39. int ambiguous = 0; /* If nonzero, multiple nonexact match(es). */
  40. arglen = strlen (arg);
  41. /* Test all elements for either exact match or abbreviated matches. */
  42. for (i = 0; optlist[i]; i++)
  43. {
  44. if (!strncmp (optlist[i], arg, arglen))
  45. {
  46. if (strlen (optlist[i]) == arglen)
  47. /* Exact match found. */
  48. return i;
  49. else if (matchind == -1)
  50. /* First nonexact match found. */
  51. matchind = i;
  52. else
  53. /* Second nonexact match found. */
  54. ambiguous = 1;
  55. }
  56. }
  57. if (ambiguous)
  58. return -2;
  59. else
  60. return matchind;
  61. }
  62. /* Error reporting for argmatch.
  63. KIND is a description of the type of entity that was being matched.
  64. VALUE is the invalid value that was given.
  65. PROBLEM is the return value from argmatch. */
  66. void
  67. invalid_arg (kind, value, problem)
  68. const char *kind;
  69. const char *value;
  70. int problem;
  71. {
  72. fprintf (stderr, "%s: ", program_name);
  73. if (problem == -1)
  74. fprintf (stderr, "invalid");
  75. else /* Assume -2. */
  76. fprintf (stderr, "ambiguous");
  77. fprintf (stderr, " %s `%s'\n", kind, value);
  78. }