home *** CD-ROM | disk | FTP | other *** search
/ Mega Top 1 / os2_top1.zip / os2_top1 / APPS / TEKST / GRECODE / ARGMATCH.C < prev    next >
C/C++ Source or Header  |  1993-10-12  |  3KB  |  95 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 2, 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. #ifdef HAVE_CONFIG_H
  21. #if defined (CONFIG_BROKETS)
  22. /* We use <config.h> instead of "config.h" so that a compilation
  23.    using -I. -I$srcdir will use ./config.h rather than $srcdir/config.h
  24.    (which it would do because it found this file in $srcdir).  */
  25. #include <config.h>
  26. #else
  27. #include "config.h"
  28. #endif
  29. #endif
  30.  
  31. #include <stdio.h>
  32. #ifdef STDC_HEADERS
  33. #include <string.h>
  34. #endif
  35.  
  36. extern char *program_name;
  37.  
  38. /* If ARG is an unambiguous match for an element of the
  39.    null-terminated array OPTLIST, return the index in OPTLIST
  40.    of the matched element, else -1 if it does not match any element
  41.    or -2 if it is ambiguous (is a prefix of more than one element).  */
  42.  
  43. int
  44. argmatch (arg, optlist)
  45.      char *arg;
  46.      char **optlist;
  47. {
  48.   int i;            /* Temporary index in OPTLIST.  */
  49.   int arglen;            /* Length of ARG.  */
  50.   int matchind = -1;        /* Index of first nonexact match.  */
  51.   int ambiguous = 0;        /* If nonzero, multiple nonexact match(es).  */
  52.  
  53.   arglen = strlen (arg);
  54.  
  55.   /* Test all elements for either exact match or abbreviated matches.  */
  56.   for (i = 0; optlist[i]; i++)
  57.     {
  58.       if (!strncmp (optlist[i], arg, arglen))
  59.     {
  60.       if (strlen (optlist[i]) == arglen)
  61.         /* Exact match found.  */
  62.         return i;
  63.       else if (matchind == -1)
  64.         /* First nonexact match found.  */
  65.         matchind = i;
  66.       else
  67.         /* Second nonexact match found.  */
  68.         ambiguous = 1;
  69.     }
  70.     }
  71.   if (ambiguous)
  72.     return -2;
  73.   else
  74.     return matchind;
  75. }
  76.  
  77. /* Error reporting for argmatch.
  78.    KIND is a description of the type of entity that was being matched.
  79.    VALUE is the invalid value that was given.
  80.    PROBLEM is the return value from argmatch.  */
  81.  
  82. void
  83. invalid_arg (kind, value, problem)
  84.      char *kind;
  85.      char *value;
  86.      int problem;
  87. {
  88.   fprintf (stderr, "%s: ", program_name);
  89.   if (problem == -1)
  90.     fprintf (stderr, "invalid");
  91.   else                /* Assume -2.  */
  92.     fprintf (stderr, "ambiguous");
  93.   fprintf (stderr, " %s `%s'\n", kind, value);
  94. }
  95.