11e72d8d2Sderaadt /* argmatch.c -- find a match for a string in an array
21e72d8d2Sderaadt Copyright (C) 1990 Free Software Foundation, Inc.
31e72d8d2Sderaadt
41e72d8d2Sderaadt This program is free software; you can redistribute it and/or modify
51e72d8d2Sderaadt it under the terms of the GNU General Public License as published by
61e72d8d2Sderaadt the Free Software Foundation; either version 2, or (at your option)
71e72d8d2Sderaadt any later version.
81e72d8d2Sderaadt
91e72d8d2Sderaadt This program is distributed in the hope that it will be useful,
101e72d8d2Sderaadt but WITHOUT ANY WARRANTY; without even the implied warranty of
111e72d8d2Sderaadt MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12*461cc63eStholo GNU General Public License for more details. */
131e72d8d2Sderaadt
141e72d8d2Sderaadt /* Written by David MacKenzie <djm@ai.mit.edu> */
151e72d8d2Sderaadt
161e72d8d2Sderaadt #ifdef HAVE_CONFIG_H
171e72d8d2Sderaadt #include "config.h"
181e72d8d2Sderaadt #endif
191e72d8d2Sderaadt
201e72d8d2Sderaadt #include <sys/types.h>
211e72d8d2Sderaadt
221e72d8d2Sderaadt #include <stdio.h>
231e72d8d2Sderaadt #ifdef STDC_HEADERS
241e72d8d2Sderaadt #include <string.h>
251e72d8d2Sderaadt #endif
261e72d8d2Sderaadt
271e72d8d2Sderaadt extern char *program_name;
281e72d8d2Sderaadt
291e72d8d2Sderaadt /* If ARG is an unambiguous match for an element of the
301e72d8d2Sderaadt null-terminated array OPTLIST, return the index in OPTLIST
311e72d8d2Sderaadt of the matched element, else -1 if it does not match any element
321e72d8d2Sderaadt or -2 if it is ambiguous (is a prefix of more than one element). */
331e72d8d2Sderaadt
341e72d8d2Sderaadt int
argmatch(arg,optlist)351e72d8d2Sderaadt argmatch (arg, optlist)
361e72d8d2Sderaadt char *arg;
371e72d8d2Sderaadt char **optlist;
381e72d8d2Sderaadt {
391e72d8d2Sderaadt int i; /* Temporary index in OPTLIST. */
401e72d8d2Sderaadt size_t arglen; /* Length of ARG. */
411e72d8d2Sderaadt int matchind = -1; /* Index of first nonexact match. */
421e72d8d2Sderaadt int ambiguous = 0; /* If nonzero, multiple nonexact match(es). */
431e72d8d2Sderaadt
441e72d8d2Sderaadt arglen = strlen (arg);
451e72d8d2Sderaadt
461e72d8d2Sderaadt /* Test all elements for either exact match or abbreviated matches. */
471e72d8d2Sderaadt for (i = 0; optlist[i]; i++)
481e72d8d2Sderaadt {
491e72d8d2Sderaadt if (!strncmp (optlist[i], arg, arglen))
501e72d8d2Sderaadt {
511e72d8d2Sderaadt if (strlen (optlist[i]) == arglen)
521e72d8d2Sderaadt /* Exact match found. */
531e72d8d2Sderaadt return i;
541e72d8d2Sderaadt else if (matchind == -1)
551e72d8d2Sderaadt /* First nonexact match found. */
561e72d8d2Sderaadt matchind = i;
571e72d8d2Sderaadt else
581e72d8d2Sderaadt /* Second nonexact match found. */
591e72d8d2Sderaadt ambiguous = 1;
601e72d8d2Sderaadt }
611e72d8d2Sderaadt }
621e72d8d2Sderaadt if (ambiguous)
631e72d8d2Sderaadt return -2;
641e72d8d2Sderaadt else
651e72d8d2Sderaadt return matchind;
661e72d8d2Sderaadt }
671e72d8d2Sderaadt
681e72d8d2Sderaadt /* Error reporting for argmatch.
691e72d8d2Sderaadt KIND is a description of the type of entity that was being matched.
701e72d8d2Sderaadt VALUE is the invalid value that was given.
711e72d8d2Sderaadt PROBLEM is the return value from argmatch. */
721e72d8d2Sderaadt
731e72d8d2Sderaadt void
invalid_arg(kind,value,problem)741e72d8d2Sderaadt invalid_arg (kind, value, problem)
751e72d8d2Sderaadt char *kind;
761e72d8d2Sderaadt char *value;
771e72d8d2Sderaadt int problem;
781e72d8d2Sderaadt {
791e72d8d2Sderaadt fprintf (stderr, "%s: ", program_name);
801e72d8d2Sderaadt if (problem == -1)
811e72d8d2Sderaadt fprintf (stderr, "invalid");
821e72d8d2Sderaadt else /* Assume -2. */
831e72d8d2Sderaadt fprintf (stderr, "ambiguous");
841e72d8d2Sderaadt fprintf (stderr, " %s `%s'\n", kind, value);
851e72d8d2Sderaadt }
86