157416Smarkm/*-
257416Smarkm * Copyright (c) 1991, 1993
357416Smarkm *	The Regents of the University of California.  All rights reserved.
457416Smarkm *
557416Smarkm * Redistribution and use in source and binary forms, with or without
657416Smarkm * modification, are permitted provided that the following conditions
757416Smarkm * are met:
857416Smarkm * 1. Redistributions of source code must retain the above copyright
957416Smarkm *    notice, this list of conditions and the following disclaimer.
1057416Smarkm * 2. Redistributions in binary form must reproduce the above copyright
1157416Smarkm *    notice, this list of conditions and the following disclaimer in the
1257416Smarkm *    documentation and/or other materials provided with the distribution.
1357416Smarkm * 3. All advertising materials mentioning features or use of this software
1457416Smarkm *    must display the following acknowledgement:
1557416Smarkm *	This product includes software developed by the University of
1657416Smarkm *	California, Berkeley and its contributors.
1757416Smarkm * 4. Neither the name of the University nor the names of its contributors
1857416Smarkm *    may be used to endorse or promote products derived from this software
1957416Smarkm *    without specific prior written permission.
2057416Smarkm *
2157416Smarkm * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2257416Smarkm * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2357416Smarkm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2457416Smarkm * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2557416Smarkm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2657416Smarkm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2757416Smarkm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2857416Smarkm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2957416Smarkm * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3057416Smarkm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3157416Smarkm * SUCH DAMAGE.
3257416Smarkm */
3357416Smarkm
3457416Smarkm#include <config.h>
35233294Sstas#ifdef HAVE_SYS_TYPES_H
36233294Sstas#include <sys/types.h>
37233294Sstas#endif
38233294Sstas#include <ctype.h>
3957416Smarkm#include "misc-proto.h"
4057416Smarkm
41233294SstasRCSID("$Id$");
4257416Smarkm
4357416Smarkm
4457416Smarkm#define	LOWER(x) (isupper(x) ? tolower(x) : (x))
4557416Smarkm/*
4657416Smarkm * The prefix function returns 0 if *s1 is not a prefix
4757416Smarkm * of *s2.  If *s1 exactly matches *s2, the negative of
4857416Smarkm * the length is returned.  If *s1 is a prefix of *s2,
4957416Smarkm * the length of *s1 is returned.
5057416Smarkm */
5157416Smarkm
5257416Smarkmint
5357416Smarkmisprefix(char *s1, char *s2)
5457416Smarkm{
5557416Smarkm    char *os1;
5657416Smarkm    char c1, c2;
5757416Smarkm
5857416Smarkm    if (*s1 == '\0')
5957416Smarkm	return(-1);
6057416Smarkm    os1 = s1;
6157416Smarkm    c1 = *s1;
6257416Smarkm    c2 = *s2;
6390926Snectar    while (tolower((unsigned char)c1) == tolower((unsigned char)c2)) {
6457416Smarkm	if (c1 == '\0')
6557416Smarkm	    break;
6657416Smarkm	c1 = *++s1;
6757416Smarkm	c2 = *++s2;
6857416Smarkm    }
6957416Smarkm    return(*s1 ? 0 : (*s2 ? (s1 - os1) : (os1 - s1)));
7057416Smarkm}
7157416Smarkm
7257416Smarkmstatic char *ambiguous;		/* special return value for command routines */
7357416Smarkm
7457416Smarkmchar **
7557416Smarkmgenget(char *name, char **table, int stlen)
7657416Smarkm     /* name to match */
7757416Smarkm     /* name entry in table */
78233294Sstas
7957416Smarkm{
8057416Smarkm    char **c, **found;
8157416Smarkm    int n;
8257416Smarkm
8357416Smarkm    if (name == 0)
8457416Smarkm	return 0;
8557416Smarkm
8657416Smarkm    found = 0;
8757416Smarkm    for (c = table; *c != 0; c = (char **)((char *)c + stlen)) {
8857416Smarkm	if ((n = isprefix(name, *c)) == 0)
8957416Smarkm	    continue;
9057416Smarkm	if (n < 0)		/* exact match */
9157416Smarkm	    return(c);
9257416Smarkm	if (found)
9357416Smarkm	    return(&ambiguous);
9457416Smarkm	found = c;
9557416Smarkm    }
9657416Smarkm    return(found);
9757416Smarkm}
9857416Smarkm
9957416Smarkm/*
10057416Smarkm * Function call version of Ambiguous()
10157416Smarkm */
10257416Smarkmint
10357416SmarkmAmbiguous(void *s)
10457416Smarkm{
10557416Smarkm    return((char **)s == &ambiguous);
10657416Smarkm}
107