argmatch.c revision 17721
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 1217721Speter GNU General Public License for more details. 1317721Speter 1417721Speter You should have received a copy of the GNU General Public License 1517721Speter along with this program; if not, write to the Free Software 1617721Speter Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ 1717721Speter 1817721Speter/* Written by David MacKenzie <djm@ai.mit.edu> */ 1917721Speter 2017721Speter#ifdef HAVE_CONFIG_H 2117721Speter#include "config.h" 2217721Speter#endif 2317721Speter 2417721Speter#include <sys/types.h> 2517721Speter 2617721Speter#include <stdio.h> 2717721Speter#ifdef STDC_HEADERS 2817721Speter#include <string.h> 2917721Speter#endif 3017721Speter 3117721Speterextern char *program_name; 3217721Speter 3317721Speter/* If ARG is an unambiguous match for an element of the 3417721Speter null-terminated array OPTLIST, return the index in OPTLIST 3517721Speter of the matched element, else -1 if it does not match any element 3617721Speter or -2 if it is ambiguous (is a prefix of more than one element). */ 3717721Speter 3817721Speterint 3917721Speterargmatch (arg, optlist) 4017721Speter char *arg; 4117721Speter char **optlist; 4217721Speter{ 4317721Speter int i; /* Temporary index in OPTLIST. */ 4417721Speter size_t arglen; /* Length of ARG. */ 4517721Speter int matchind = -1; /* Index of first nonexact match. */ 4617721Speter int ambiguous = 0; /* If nonzero, multiple nonexact match(es). */ 4717721Speter 4817721Speter arglen = strlen (arg); 4917721Speter 5017721Speter /* Test all elements for either exact match or abbreviated matches. */ 5117721Speter for (i = 0; optlist[i]; i++) 5217721Speter { 5317721Speter if (!strncmp (optlist[i], arg, arglen)) 5417721Speter { 5517721Speter if (strlen (optlist[i]) == arglen) 5617721Speter /* Exact match found. */ 5717721Speter return i; 5817721Speter else if (matchind == -1) 5917721Speter /* First nonexact match found. */ 6017721Speter matchind = i; 6117721Speter else 6217721Speter /* Second nonexact match found. */ 6317721Speter ambiguous = 1; 6417721Speter } 6517721Speter } 6617721Speter if (ambiguous) 6717721Speter return -2; 6817721Speter else 6917721Speter return matchind; 7017721Speter} 7117721Speter 7217721Speter/* Error reporting for argmatch. 7317721Speter KIND is a description of the type of entity that was being matched. 7417721Speter VALUE is the invalid value that was given. 7517721Speter PROBLEM is the return value from argmatch. */ 7617721Speter 7717721Spetervoid 7817721Speterinvalid_arg (kind, value, problem) 7917721Speter char *kind; 8017721Speter char *value; 8117721Speter int problem; 8217721Speter{ 8317721Speter fprintf (stderr, "%s: ", program_name); 8417721Speter if (problem == -1) 8517721Speter fprintf (stderr, "invalid"); 8617721Speter else /* Assume -2. */ 8717721Speter fprintf (stderr, "ambiguous"); 8817721Speter fprintf (stderr, " %s `%s'\n", kind, value); 8917721Speter} 90