1 /* argmatch.c -- find a match for a string in an array
2 Copyright (C) 1990, 1997 Free Software Foundation, Inc.
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)
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.
14 You should have received a copy of the GNU General Public License
15 along with this program; see the file COPYING.
16 If not, write to the Free Software Foundation,
17 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
19 /* Written by David MacKenzie <djm@gnu.ai.mit.edu> */
27 #include <sys/types.h>
36 /* If ARG is an unambiguous match for an element of the
37 null-terminated array OPTLIST, return the index in OPTLIST
38 of the matched element, else -1 if it does not match any element
39 or -2 if it is ambiguous (is a prefix of more than one element). */
42 argmatch (const char *arg
, const char *const *optlist
)
44 int i
; /* Temporary index in OPTLIST. */
45 size_t arglen
; /* Length of ARG. */
46 int matchind
= -1; /* Index of first nonexact match. */
47 int ambiguous
= 0; /* If nonzero, multiple nonexact match(es). */
49 arglen
= strlen (arg
);
51 /* Test all elements for either exact match or abbreviated matches. */
52 for (i
= 0; optlist
[i
]; i
++)
54 if (!strncmp (optlist
[i
], arg
, arglen
))
56 if (strlen (optlist
[i
]) == arglen
)
57 /* Exact match found. */
59 else if (matchind
== -1)
60 /* First nonexact match found. */
63 /* Second nonexact match found. */
73 /* Error reporting for argmatch.
74 KIND is a description of the type of entity that was being matched.
75 VALUE is the invalid value that was given.
76 PROBLEM is the return value from argmatch. */
79 invalid_arg (const char *kind
, const char *value
, int problem
)
81 const char *fmt
= (problem
== -1
82 ? "%s: invalid %s `%s'\n"
83 : "%s: ambiguous %s `%s'\n");
84 fprintf (stderr
, fmt
, program_name
, kind
, value
);