1*57aff589Schristos /* $NetBSD: genget.c,v 1.14 2018/12/14 23:42:39 christos Exp $ */
20667d122Schristos
361f28255Scgd /*-
450c0885eScgd * Copyright (c) 1991, 1993
550c0885eScgd * The Regents of the University of California. All rights reserved.
661f28255Scgd *
761f28255Scgd * Redistribution and use in source and binary forms, with or without
861f28255Scgd * modification, are permitted provided that the following conditions
961f28255Scgd * are met:
1061f28255Scgd * 1. Redistributions of source code must retain the above copyright
1161f28255Scgd * notice, this list of conditions and the following disclaimer.
1261f28255Scgd * 2. Redistributions in binary form must reproduce the above copyright
1361f28255Scgd * notice, this list of conditions and the following disclaimer in the
1461f28255Scgd * documentation and/or other materials provided with the distribution.
15eb7c1594Sagc * 3. Neither the name of the University nor the names of its contributors
1661f28255Scgd * may be used to endorse or promote products derived from this software
1761f28255Scgd * without specific prior written permission.
1861f28255Scgd *
1961f28255Scgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2061f28255Scgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2161f28255Scgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2261f28255Scgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2361f28255Scgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2461f28255Scgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2561f28255Scgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2661f28255Scgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2761f28255Scgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2861f28255Scgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2961f28255Scgd * SUCH DAMAGE.
3061f28255Scgd */
3161f28255Scgd
320a48c27cSlukem #include <sys/cdefs.h>
3361f28255Scgd #ifndef lint
340a48c27cSlukem #if 0
350a48c27cSlukem static char sccsid[] = "@(#)genget.c 8.2 (Berkeley) 5/30/95";
360a48c27cSlukem #else
37*57aff589Schristos __RCSID("$NetBSD: genget.c,v 1.14 2018/12/14 23:42:39 christos Exp $");
380a48c27cSlukem #endif
3961f28255Scgd #endif /* not lint */
4061f28255Scgd
410dcff754Sjtk
4261f28255Scgd #include <ctype.h>
430667d122Schristos #include "misc.h"
4461f28255Scgd
451793b7ddSdsl #define LOWER(x) (isupper((unsigned char)x) ? tolower((unsigned char)x) : (x))
4661f28255Scgd /*
4761f28255Scgd * The prefix function returns 0 if *s1 is not a prefix
4861f28255Scgd * of *s2. If *s1 exactly matches *s2, the negative of
4961f28255Scgd * the length is returned. If *s1 is a prefix of *s2,
5061f28255Scgd * the length of *s1 is returned.
5161f28255Scgd */
5261f28255Scgd int
isprefix(const char * s1,const char * s2)53*57aff589Schristos isprefix(const char *s1, const char *s2)
5461f28255Scgd {
55*57aff589Schristos const char *os1;
566a6c8f61Schristos char c1, c2;
5761f28255Scgd
5861f28255Scgd if (*s1 == '\0')
5961f28255Scgd return(-1);
6061f28255Scgd os1 = s1;
6161f28255Scgd c1 = *s1;
6261f28255Scgd c2 = *s2;
6361f28255Scgd while (LOWER(c1) == LOWER(c2)) {
6461f28255Scgd if (c1 == '\0')
6561f28255Scgd break;
6661f28255Scgd c1 = *++s1;
6761f28255Scgd c2 = *++s2;
6861f28255Scgd }
6961f28255Scgd return(*s1 ? 0 : (*s2 ? (s1 - os1) : (os1 - s1)));
7061f28255Scgd }
7161f28255Scgd
7261f28255Scgd static char *ambiguous; /* special return value for command routines */
7361f28255Scgd
7461f28255Scgd char **
genget(const char * name,char ** table,int stlen)75*57aff589Schristos genget(const char *name, /* name to match */
76f9113d00Smatt char **table, /* name entry in table */
77f9113d00Smatt int stlen)
7861f28255Scgd {
7961f28255Scgd register char **c, **found;
8061f28255Scgd register int n;
8161f28255Scgd
8261f28255Scgd if (name == 0)
8361f28255Scgd return 0;
8461f28255Scgd
8561f28255Scgd found = 0;
8661f28255Scgd for (c = table; *c != 0; c = (char **)((char *)c + stlen)) {
8761f28255Scgd if ((n = isprefix(name, *c)) == 0)
8861f28255Scgd continue;
8961f28255Scgd if (n < 0) /* exact match */
9061f28255Scgd return(c);
9161f28255Scgd if (found)
9261f28255Scgd return(&ambiguous);
9361f28255Scgd found = c;
9461f28255Scgd }
9561f28255Scgd return(found);
9661f28255Scgd }
9761f28255Scgd
9861f28255Scgd /*
9961f28255Scgd * Function call version of Ambiguous()
10061f28255Scgd */
10161f28255Scgd int
Ambiguous(void * s)102f9113d00Smatt Ambiguous(void *s)
10361f28255Scgd {
1040667d122Schristos return(s == &ambiguous);
10561f28255Scgd }
106