117721Speter/* argmatch.c -- find a match for a string in an array
217721Speter   Copyright (C) 1990 Free Software Foundation, Inc.
317721Speter
417721Speter   This program is free software; you can redistribute it and/or modify
517721Speter   it under the terms of the GNU General Public License as published by
617721Speter   the Free Software Foundation; either version 2, or (at your option)
717721Speter   any later version.
817721Speter
917721Speter   This program is distributed in the hope that it will be useful,
1017721Speter   but WITHOUT ANY WARRANTY; without even the implied warranty of
1117721Speter   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1225839Speter   GNU General Public License for more details.  */
1317721Speter
1417721Speter/* Written by David MacKenzie <djm@ai.mit.edu> */
1517721Speter
1617721Speter#ifdef HAVE_CONFIG_H
1717721Speter#include "config.h"
1817721Speter#endif
1917721Speter
2017721Speter#include <sys/types.h>
2117721Speter
2217721Speter#include <stdio.h>
2317721Speter#ifdef STDC_HEADERS
2417721Speter#include <string.h>
2517721Speter#endif
2617721Speter
2717721Speterextern char *program_name;
2817721Speter
2917721Speter/* If ARG is an unambiguous match for an element of the
3017721Speter   null-terminated array OPTLIST, return the index in OPTLIST
3117721Speter   of the matched element, else -1 if it does not match any element
3217721Speter   or -2 if it is ambiguous (is a prefix of more than one element). */
3317721Speter
3417721Speterint
3517721Speterargmatch (arg, optlist)
3617721Speter     char *arg;
3717721Speter     char **optlist;
3817721Speter{
3917721Speter  int i;			/* Temporary index in OPTLIST. */
4017721Speter  size_t arglen;		/* Length of ARG. */
4117721Speter  int matchind = -1;		/* Index of first nonexact match. */
4217721Speter  int ambiguous = 0;		/* If nonzero, multiple nonexact match(es). */
4317721Speter
4417721Speter  arglen = strlen (arg);
4517721Speter
4617721Speter  /* Test all elements for either exact match or abbreviated matches.  */
4717721Speter  for (i = 0; optlist[i]; i++)
4817721Speter    {
4917721Speter      if (!strncmp (optlist[i], arg, arglen))
5017721Speter	{
5117721Speter	  if (strlen (optlist[i]) == arglen)
5217721Speter	    /* Exact match found.  */
5317721Speter	    return i;
5417721Speter	  else if (matchind == -1)
5517721Speter	    /* First nonexact match found.  */
5617721Speter	    matchind = i;
5717721Speter	  else
5817721Speter	    /* Second nonexact match found.  */
5917721Speter	    ambiguous = 1;
6017721Speter	}
6117721Speter    }
6217721Speter  if (ambiguous)
6317721Speter    return -2;
6417721Speter  else
6517721Speter    return matchind;
6617721Speter}
6717721Speter
6817721Speter/* Error reporting for argmatch.
6917721Speter   KIND is a description of the type of entity that was being matched.
7017721Speter   VALUE is the invalid value that was given.
7117721Speter   PROBLEM is the return value from argmatch. */
7217721Speter
7317721Spetervoid
7417721Speterinvalid_arg (kind, value, problem)
7517721Speter     char *kind;
7617721Speter     char *value;
7717721Speter     int problem;
7817721Speter{
7917721Speter  fprintf (stderr, "%s: ", program_name);
8017721Speter  if (problem == -1)
8117721Speter    fprintf (stderr, "invalid");
8217721Speter  else				/* Assume -2. */
8317721Speter    fprintf (stderr, "ambiguous");
8417721Speter  fprintf (stderr, " %s `%s'\n", kind, value);
8517721Speter}
86