129088Smarkm/*-
229088Smarkm * Copyright (c) 1991, 1993
329088Smarkm *	The Regents of the University of California.  All rights reserved.
429088Smarkm *
529088Smarkm * Redistribution and use in source and binary forms, with or without
629088Smarkm * modification, are permitted provided that the following conditions
729088Smarkm * are met:
829088Smarkm * 1. Redistributions of source code must retain the above copyright
929088Smarkm *    notice, this list of conditions and the following disclaimer.
1029088Smarkm * 2. Redistributions in binary form must reproduce the above copyright
1129088Smarkm *    notice, this list of conditions and the following disclaimer in the
1229088Smarkm *    documentation and/or other materials provided with the distribution.
1329088Smarkm * 3. All advertising materials mentioning features or use of this software
1429088Smarkm *    must display the following acknowledgement:
1529088Smarkm *	This product includes software developed by the University of
1629088Smarkm *	California, Berkeley and its contributors.
1729088Smarkm * 4. Neither the name of the University nor the names of its contributors
1829088Smarkm *    may be used to endorse or promote products derived from this software
1929088Smarkm *    without specific prior written permission.
2029088Smarkm *
2129088Smarkm * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2229088Smarkm * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2329088Smarkm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2429088Smarkm * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2529088Smarkm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2629088Smarkm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2729088Smarkm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2829088Smarkm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2929088Smarkm * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3029088Smarkm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3129088Smarkm * SUCH DAMAGE.
3229088Smarkm */
3329088Smarkm
3484305Smarkm#include <sys/cdefs.h>
3587139Smarkm
3684305Smarkm__FBSDID("$FreeBSD$");
3784305Smarkm
3829088Smarkm#ifndef lint
3963248Speter#if 0
4029181Smarkmstatic const char sccsid[] = "@(#)genget.c	8.2 (Berkeley) 5/30/95";
4163248Speter#endif
4229088Smarkm#endif /* not lint */
4329088Smarkm
4429088Smarkm
4529088Smarkm#include <ctype.h>
4629088Smarkm
4787139Smarkm#include "misc-proto.h"
4887139Smarkm
4929088Smarkm#define	LOWER(x) (isupper(x) ? tolower(x) : (x))
5029088Smarkm/*
5129088Smarkm * The prefix function returns 0 if *s1 is not a prefix
5229088Smarkm * of *s2.  If *s1 exactly matches *s2, the negative of
5329088Smarkm * the length is returned.  If *s1 is a prefix of *s2,
5429088Smarkm * the length of *s1 is returned.
5529088Smarkm */
5681965Smarkmint
5787139Smarkmisprefix(char *s1, const char *s2)
5829088Smarkm{
5929088Smarkm	char *os1;
6081965Smarkm	char c1, c2;
6129088Smarkm
6229088Smarkm	if (*s1 == '\0')
6329088Smarkm		return(-1);
6429088Smarkm	os1 = s1;
6529088Smarkm	c1 = *s1;
6629088Smarkm	c2 = *s2;
6729088Smarkm	while (LOWER(c1) == LOWER(c2)) {
6829088Smarkm		if (c1 == '\0')
6929088Smarkm			break;
7029088Smarkm		c1 = *++s1;
7129088Smarkm		c2 = *++s2;
7229088Smarkm	}
7329088Smarkm	return(*s1 ? 0 : (*s2 ? (s1 - os1) : (os1 - s1)));
7429088Smarkm}
7529088Smarkm
7629088Smarkmstatic char *ambiguous;		/* special return value for command routines */
7729088Smarkm
7881965Smarkmchar **
7981965Smarkmgenget(char *name, char **table, int stlen)
8029088Smarkm{
8181965Smarkm	char **c, **found;
8281965Smarkm	int n;
8329088Smarkm
8429088Smarkm	if (name == 0)
8529088Smarkm	    return 0;
8629088Smarkm
8729088Smarkm	found = 0;
8829088Smarkm	for (c = table; *c != 0; c = (char **)((char *)c + stlen)) {
8929088Smarkm		if ((n = isprefix(name, *c)) == 0)
9029088Smarkm			continue;
9129088Smarkm		if (n < 0)		/* exact match */
9229088Smarkm			return(c);
9329088Smarkm		if (found)
9429088Smarkm			return(&ambiguous);
9529088Smarkm		found = c;
9629088Smarkm	}
9729088Smarkm	return(found);
9829088Smarkm}
9929088Smarkm
10029088Smarkm/*
10129088Smarkm * Function call version of Ambiguous()
10229088Smarkm */
10381965Smarkmint
10487139SmarkmAmbiguous(char **s)
10529088Smarkm{
10687139Smarkm	return(s == &ambiguous);
10729088Smarkm}
108