]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - gnu/usr.bin/cvs/lib/argmatch.c
Original sources from CVS-1.4A2 munged to fit our directory structure.
[FreeBSD/FreeBSD.git] / gnu / usr.bin / cvs / lib / argmatch.c
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 #include "config.h"
22 #endif
23
24 #include <stdio.h>
25 #ifdef STDC_HEADERS
26 #include <string.h>
27 #endif
28
29 extern char *program_name;
30
31 /* If ARG is an unambiguous match for an element of the
32    null-terminated array OPTLIST, return the index in OPTLIST
33    of the matched element, else -1 if it does not match any element
34    or -2 if it is ambiguous (is a prefix of more than one element). */
35
36 int
37 argmatch (arg, optlist)
38      char *arg;
39      char **optlist;
40 {
41   int i;                        /* Temporary index in OPTLIST. */
42   int arglen;                   /* Length of ARG. */
43   int matchind = -1;            /* Index of first nonexact match. */
44   int ambiguous = 0;            /* If nonzero, multiple nonexact match(es). */
45   
46   arglen = strlen (arg);
47   
48   /* Test all elements for either exact match or abbreviated matches.  */
49   for (i = 0; optlist[i]; i++)
50     {
51       if (!strncmp (optlist[i], arg, arglen))
52         {
53           if (strlen (optlist[i]) == arglen)
54             /* Exact match found.  */
55             return i;
56           else if (matchind == -1)
57             /* First nonexact match found.  */
58             matchind = i;
59           else
60             /* Second nonexact match found.  */
61             ambiguous = 1;
62         }
63     }
64   if (ambiguous)
65     return -2;
66   else
67     return matchind;
68 }
69
70 /* Error reporting for argmatch.
71    KIND is a description of the type of entity that was being matched.
72    VALUE is the invalid value that was given.
73    PROBLEM is the return value from argmatch. */
74
75 void
76 invalid_arg (kind, value, problem)
77      char *kind;
78      char *value;
79      int problem;
80 {
81   fprintf (stderr, "%s: ", program_name);
82   if (problem == -1)
83     fprintf (stderr, "invalid");
84   else                          /* Assume -2. */
85     fprintf (stderr, "ambiguous");
86   fprintf (stderr, " %s `%s'\n", kind, value);
87 }