Debian Codegraph @

libtelnet/genget.c

raw
1 /*
2 Copyright (C) 1993-2026 Free Software Foundation, Inc.
3
4 This file is part of GNU Inetutils.
5
6 GNU Inetutils is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or (at
9 your option) any later version.
10
11 GNU Inetutils is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see `http://www.gnu.org/licenses/'. */
18
19 /*
20 * Copyright (c) 1991, 1993
21 * The Regents of the University of California. All rights reserved.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the above copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. Neither the name of the University nor the names of its contributors
32 * may be used to endorse or promote products derived from this software
33 * without specific prior written permission.
34 *
35 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
36 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
38 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
39 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
40 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
41 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
42 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
43 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
44 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
45 * SUCH DAMAGE.
46 */
47
48 #include <config.h>
49
50 #include <ctype.h>
51
52 #define LOWER(x) (isupper(x) ? tolower(x) : (x))
53 /*
54 * The prefix function returns 0 if *s1 is not a prefix
55 * of *s2. If *s1 exactly matches *s2, the negative of
56 * the length is returned. If *s1 is a prefix of *s2,
57 * the length of *s1 is returned.
58 */
59 int
60 isprefix (const char *s1, const char *s2)
61 {
62 const char *os1;
63 char c1, c2;
64
65 if (*s1 == '\0')
66 return (-1);
67 os1 = s1;
68 c1 = *s1;
69 c2 = *s2;
70 while (LOWER (c1) == LOWER (c2))
71 {
72 if (c1 == '\0')
73 break;
74 c1 = *++s1;
75 c2 = *++s2;
76 }
77 return (*s1 ? 0 : (*s2 ? (s1 - os1) : (os1 - s1)));
78 }
79
80 static char *ambiguous; /* special return value for command routines */
81
82 /* char *name; name to match */
83 /* char **table; name entry in table */
84 char **
85 genget (const char *name, char **table, int stlen)
86 {
87 char **c, **found;
88 int n;
89
90 if (name == 0)
91 return 0;
92
93 found = 0;
94 for (c = table; *c != 0; c = (char **) ((char *) c + stlen))
95 {
96 if ((n = isprefix (name, *c)) == 0)
97 continue;
98 if (n < 0) /* exact match */
99 return (c);
100 if (found)
101 return (&ambiguous);
102 found = c;
103 }
104 return (found);
105 }
106
107 /*
108 * Function call version of Ambiguous()
109 */
110 int
111 Ambiguous (void *s)
112 {
113 return (s == &ambiguous);
114 }