genget.c revision 81965
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
3429088Smarkm#ifndef lint
3563248Speter#if 0
3629181Smarkmstatic const char sccsid[] = "@(#)genget.c	8.2 (Berkeley) 5/30/95";
3763248Speter#else
3863248Speterstatic const char rcsid[] =
3963248Speter "$FreeBSD: head/contrib/telnet/libtelnet/genget.c 81965 2001-08-20 12:28:40Z markm $";
4063248Speter#endif
4129088Smarkm#endif /* not lint */
4229088Smarkm
4329088Smarkm
4429088Smarkm#include <ctype.h>
4529088Smarkm
4629088Smarkm#define	LOWER(x) (isupper(x) ? tolower(x) : (x))
4729088Smarkm/*
4829088Smarkm * The prefix function returns 0 if *s1 is not a prefix
4929088Smarkm * of *s2.  If *s1 exactly matches *s2, the negative of
5029088Smarkm * the length is returned.  If *s1 is a prefix of *s2,
5129088Smarkm * the length of *s1 is returned.
5229088Smarkm */
5381965Smarkmint
5481965Smarkmisprefix(char *s1, char *s2)
5529088Smarkm{
5629088Smarkm	char *os1;
5781965Smarkm	char c1, c2;
5829088Smarkm
5929088Smarkm	if (*s1 == '\0')
6029088Smarkm		return(-1);
6129088Smarkm	os1 = s1;
6229088Smarkm	c1 = *s1;
6329088Smarkm	c2 = *s2;
6429088Smarkm	while (LOWER(c1) == LOWER(c2)) {
6529088Smarkm		if (c1 == '\0')
6629088Smarkm			break;
6729088Smarkm		c1 = *++s1;
6829088Smarkm		c2 = *++s2;
6929088Smarkm	}
7029088Smarkm	return(*s1 ? 0 : (*s2 ? (s1 - os1) : (os1 - s1)));
7129088Smarkm}
7229088Smarkm
7329088Smarkmstatic char *ambiguous;		/* special return value for command routines */
7429088Smarkm
7581965Smarkmchar **
7681965Smarkmgenget(char *name, char **table, int stlen)
7729088Smarkm{
7881965Smarkm	char **c, **found;
7981965Smarkm	int n;
8029088Smarkm
8129088Smarkm	if (name == 0)
8229088Smarkm	    return 0;
8329088Smarkm
8429088Smarkm	found = 0;
8529088Smarkm	for (c = table; *c != 0; c = (char **)((char *)c + stlen)) {
8629088Smarkm		if ((n = isprefix(name, *c)) == 0)
8729088Smarkm			continue;
8829088Smarkm		if (n < 0)		/* exact match */
8929088Smarkm			return(c);
9029088Smarkm		if (found)
9129088Smarkm			return(&ambiguous);
9229088Smarkm		found = c;
9329088Smarkm	}
9429088Smarkm	return(found);
9529088Smarkm}
9629088Smarkm
9729088Smarkm/*
9829088Smarkm * Function call version of Ambiguous()
9929088Smarkm */
10081965Smarkmint
10181965SmarkmAmbiguous(char *s)
10229088Smarkm{
10381965Smarkm	return((char **)s == &ambiguous);
10429088Smarkm}
105