home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / gnu / gnulib / ohlutil / argmatch.c next >
Encoding:
C/C++ Source or Header  |  1990-08-24  |  3.1 KB  |  106 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. /*
  21.  * MS-DOS port (c) 1990 by Thorsten Ohl, td12@ddagsi3.bitnet
  22.  *
  23.  * To this port, the same copying conditions apply as to the
  24.  * original release.
  25.  *
  26.  * IMPORTANT:
  27.  * This file is not identical to the original GNU release!
  28.  * You should have received this code as patch to the official
  29.  * GNU release.
  30.  *
  31.  * MORE IMPORTANT:
  32.  * This port comes with ABSOLUTELY NO WARRANTY.
  33.  *
  34.  * $Header: e:/gnu/fileutil/RCS/argmatch.c'v 1.3.0.2 90/06/29 00:46:28 tho Stable $
  35.  */
  36.  
  37. #include <stdio.h>
  38. #ifdef STDC_HEADERS
  39. #include <string.h>
  40. #endif
  41.  
  42. extern char *program_name;
  43.  
  44. #ifdef MSDOS
  45. extern int argmatch (char *arg, char **optlist);
  46. extern void invalid_arg (char *kind, char *value, int problem);
  47. #endif /* MSDOS */
  48.  
  49. /* If ARG is an unambiguous match for an element of the
  50.    null-terminated array OPTLIST, return the index in OPTLIST
  51.    of the matched element, else -1 if it does not match any element
  52.    or -2 if it is ambiguous (is a prefix of more than one element). */
  53.  
  54. int
  55. argmatch (arg, optlist)
  56.      char *arg;
  57.      char **optlist;
  58. {
  59.   int i;            /* Temporary index in OPTLIST. */
  60.   int arglen;            /* Length of ARG. */
  61.   int matchind = -1;        /* Index of first nonexact match. */
  62.   int ambiguous = 0;        /* If nonzero, multiple nonexact match(es). */
  63.   
  64.   arglen = strlen (arg);
  65.   
  66.   /* Test all elements for either exact match or abbreviated matches.  */
  67.   for (i = 0; optlist[i]; i++)
  68.     {
  69.       if (!strncmp (optlist[i], arg, arglen))
  70.     {
  71.       if (strlen (optlist[i]) == arglen)
  72.         /* Exact match found.  */
  73.         return i;
  74.       else if (matchind == -1)
  75.         /* First nonexact match found.  */
  76.         matchind = i;
  77.       else
  78.         /* Second nonexact match found.  */
  79.         ambiguous = 1;
  80.     }
  81.     }
  82.   if (ambiguous)
  83.     return -2;
  84.   else
  85.     return matchind;
  86. }
  87.  
  88. /* Error reporting for argmatch.
  89.    KIND is a description of the type of entity that was being matched.
  90.    VALUE is the invalid value that was given.
  91.    PROBLEM is the return value from argmatch. */
  92.  
  93. void
  94. invalid_arg (kind, value, problem)
  95.      char *kind;
  96.      char *value;
  97.      int problem;
  98. {
  99.   fprintf (stderr, "%s: ", program_name);
  100.   if (problem == -1)
  101.     fprintf (stderr, "invalid");
  102.   else                /* Assume -2. */
  103.     fprintf (stderr, "ambiguous");
  104.   fprintf (stderr, " %s `%s'\n", kind, value);
  105. }
  106.