xref: /freebsd/crypto/heimdal/appl/telnet/libtelnet/genget.c (revision 6a068746777241722b2b32c5d0bc443a2a64d80b)
113e3f4d6SMark Murray /*-
213e3f4d6SMark Murray  * Copyright (c) 1991, 1993
313e3f4d6SMark Murray  *	The Regents of the University of California.  All rights reserved.
413e3f4d6SMark Murray  *
513e3f4d6SMark Murray  * Redistribution and use in source and binary forms, with or without
613e3f4d6SMark Murray  * modification, are permitted provided that the following conditions
713e3f4d6SMark Murray  * are met:
813e3f4d6SMark Murray  * 1. Redistributions of source code must retain the above copyright
913e3f4d6SMark Murray  *    notice, this list of conditions and the following disclaimer.
1013e3f4d6SMark Murray  * 2. Redistributions in binary form must reproduce the above copyright
1113e3f4d6SMark Murray  *    notice, this list of conditions and the following disclaimer in the
1213e3f4d6SMark Murray  *    documentation and/or other materials provided with the distribution.
1313e3f4d6SMark Murray  * 3. All advertising materials mentioning features or use of this software
1413e3f4d6SMark Murray  *    must display the following acknowledgement:
1513e3f4d6SMark Murray  *	This product includes software developed by the University of
1613e3f4d6SMark Murray  *	California, Berkeley and its contributors.
1713e3f4d6SMark Murray  * 4. Neither the name of the University nor the names of its contributors
1813e3f4d6SMark Murray  *    may be used to endorse or promote products derived from this software
1913e3f4d6SMark Murray  *    without specific prior written permission.
2013e3f4d6SMark Murray  *
2113e3f4d6SMark Murray  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2213e3f4d6SMark Murray  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2313e3f4d6SMark Murray  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2413e3f4d6SMark Murray  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2513e3f4d6SMark Murray  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2613e3f4d6SMark Murray  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2713e3f4d6SMark Murray  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2813e3f4d6SMark Murray  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2913e3f4d6SMark Murray  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3013e3f4d6SMark Murray  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3113e3f4d6SMark Murray  * SUCH DAMAGE.
3213e3f4d6SMark Murray  */
3313e3f4d6SMark Murray 
3413e3f4d6SMark Murray #include <config.h>
35*ae771770SStanislav Sedov #ifdef HAVE_SYS_TYPES_H
36*ae771770SStanislav Sedov #include <sys/types.h>
37*ae771770SStanislav Sedov #endif
38*ae771770SStanislav Sedov #include <ctype.h>
3913e3f4d6SMark Murray #include "misc-proto.h"
4013e3f4d6SMark Murray 
41*ae771770SStanislav Sedov RCSID("$Id$");
4213e3f4d6SMark Murray 
4313e3f4d6SMark Murray 
4413e3f4d6SMark Murray #define	LOWER(x) (isupper(x) ? tolower(x) : (x))
4513e3f4d6SMark Murray /*
4613e3f4d6SMark Murray  * The prefix function returns 0 if *s1 is not a prefix
4713e3f4d6SMark Murray  * of *s2.  If *s1 exactly matches *s2, the negative of
4813e3f4d6SMark Murray  * the length is returned.  If *s1 is a prefix of *s2,
4913e3f4d6SMark Murray  * the length of *s1 is returned.
5013e3f4d6SMark Murray  */
5113e3f4d6SMark Murray 
5213e3f4d6SMark Murray int
isprefix(char * s1,char * s2)5313e3f4d6SMark Murray isprefix(char *s1, char *s2)
5413e3f4d6SMark Murray {
5513e3f4d6SMark Murray     char *os1;
5613e3f4d6SMark Murray     char c1, c2;
5713e3f4d6SMark Murray 
5813e3f4d6SMark Murray     if (*s1 == '\0')
5913e3f4d6SMark Murray 	return(-1);
6013e3f4d6SMark Murray     os1 = s1;
6113e3f4d6SMark Murray     c1 = *s1;
6213e3f4d6SMark Murray     c2 = *s2;
634137ff4cSJacques Vidrine     while (tolower((unsigned char)c1) == tolower((unsigned char)c2)) {
6413e3f4d6SMark Murray 	if (c1 == '\0')
6513e3f4d6SMark Murray 	    break;
6613e3f4d6SMark Murray 	c1 = *++s1;
6713e3f4d6SMark Murray 	c2 = *++s2;
6813e3f4d6SMark Murray     }
6913e3f4d6SMark Murray     return(*s1 ? 0 : (*s2 ? (s1 - os1) : (os1 - s1)));
7013e3f4d6SMark Murray }
7113e3f4d6SMark Murray 
7213e3f4d6SMark Murray static char *ambiguous;		/* special return value for command routines */
7313e3f4d6SMark Murray 
7413e3f4d6SMark Murray char **
genget(char * name,char ** table,int stlen)7513e3f4d6SMark Murray genget(char *name, char **table, int stlen)
7613e3f4d6SMark Murray      /* name to match */
7713e3f4d6SMark Murray      /* name entry in table */
7813e3f4d6SMark Murray 
7913e3f4d6SMark Murray {
8013e3f4d6SMark Murray     char **c, **found;
8113e3f4d6SMark Murray     int n;
8213e3f4d6SMark Murray 
8313e3f4d6SMark Murray     if (name == 0)
8413e3f4d6SMark Murray 	return 0;
8513e3f4d6SMark Murray 
8613e3f4d6SMark Murray     found = 0;
8713e3f4d6SMark Murray     for (c = table; *c != 0; c = (char **)((char *)c + stlen)) {
8813e3f4d6SMark Murray 	if ((n = isprefix(name, *c)) == 0)
8913e3f4d6SMark Murray 	    continue;
9013e3f4d6SMark Murray 	if (n < 0)		/* exact match */
9113e3f4d6SMark Murray 	    return(c);
9213e3f4d6SMark Murray 	if (found)
9313e3f4d6SMark Murray 	    return(&ambiguous);
9413e3f4d6SMark Murray 	found = c;
9513e3f4d6SMark Murray     }
9613e3f4d6SMark Murray     return(found);
9713e3f4d6SMark Murray }
9813e3f4d6SMark Murray 
9913e3f4d6SMark Murray /*
10013e3f4d6SMark Murray  * Function call version of Ambiguous()
10113e3f4d6SMark Murray  */
10213e3f4d6SMark Murray int
Ambiguous(void * s)10313e3f4d6SMark Murray Ambiguous(void *s)
10413e3f4d6SMark Murray {
10513e3f4d6SMark Murray     return((char **)s == &ambiguous);
10613e3f4d6SMark Murray }
107