home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / f / futi14as.zip / ARGMATCH.C next >
C/C++ Source or Header  |  1992-02-22  |  3KB  |  101 lines

  1. /* argmatch.c -- find a match for a string in an array
  2.    Copyright (C) 1990 Free Software Foundation, Inc.
  3.  
  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 1, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* Written by David MacKenzie <djm@ai.mit.edu> */
  19.  
  20. /* MS-DOS port (c) 1990 by Thorsten Ohl, ohl@gnu.ai.mit.edu
  21.    This port is also distributed under the terms of the
  22.    GNU General Public License as published by the
  23.    Free Software Foundation.
  24.  
  25.    Please note that this file is not identical to the
  26.    original GNU release, you should have received this
  27.    code as patch to the official release.
  28.  
  29.    $Header: e:/gnu/fileutil/RCS/argmatch.c 1.4.0.2 90/09/19 12:27:25 tho Exp $
  30.  */
  31.  
  32. #include <stdio.h>
  33. #ifdef STDC_HEADERS
  34. #include <string.h>
  35. #endif
  36.  
  37. #ifdef MSDOS
  38. int argmatch (char *arg, char **optlist);
  39. void invalid_arg (char *kind, char *value, int problem);
  40. #endif /* MSDOS */
  41.  
  42. extern char *program_name;
  43.  
  44. /* If ARG is an unambiguous match for an element of the
  45.    null-terminated array OPTLIST, return the index in OPTLIST
  46.    of the matched element, else -1 if it does not match any element
  47.    or -2 if it is ambiguous (is a prefix of more than one element). */
  48.  
  49. int
  50. argmatch (arg, optlist)
  51.      char *arg;
  52.      char **optlist;
  53. {
  54.   int i;            /* Temporary index in OPTLIST. */
  55.   int arglen;            /* Length of ARG. */
  56.   int matchind = -1;        /* Index of first nonexact match. */
  57.   int ambiguous = 0;        /* If nonzero, multiple nonexact match(es). */
  58.   
  59.   arglen = strlen (arg);
  60.   
  61.   /* Test all elements for either exact match or abbreviated matches.  */
  62.   for (i = 0; optlist[i]; i++)
  63.     {
  64.       if (!strncmp (optlist[i], arg, arglen))
  65.     {
  66.       if (strlen (optlist[i]) == arglen)
  67.         /* Exact match found.  */
  68.         return i;
  69.       else if (matchind == -1)
  70.         /* First nonexact match found.  */
  71.         matchind = i;
  72.       else
  73.         /* Second nonexact match found.  */
  74.         ambiguous = 1;
  75.     }
  76.     }
  77.   if (ambiguous)
  78.     return -2;
  79.   else
  80.     return matchind;
  81. }
  82.  
  83. /* Error reporting for argmatch.
  84.    KIND is a description of the type of entity that was being matched.
  85.    VALUE is the invalid value that was given.
  86.    PROBLEM is the return value from argmatch. */
  87.  
  88. void
  89. invalid_arg (kind, value, problem)
  90.      char *kind;
  91.      char *value;
  92.      int problem;
  93. {
  94.   fprintf (stderr, "%s: ", program_name);
  95.   if (problem == -1)
  96.     fprintf (stderr, "invalid");
  97.   else                /* Assume -2. */
  98.     fprintf (stderr, "ambiguous");
  99.   fprintf (stderr, " %s `%s'\n", kind, value);
  100. }
  101.