1 /****************************************************************
2 Copyright (C) 1997 Lucent Technologies
3 All Rights Reserved
4
5 Permission to use, copy, modify, and distribute this software and
6 its documentation for any purpose and without fee is hereby
7 granted, provided that the above copyright notice appear in all
8 copies and that both that the copyright notice and this
9 permission notice and warranty disclaimer appear in supporting
10 documentation, and that the name of Lucent or any of its entities
11 not be used in advertising or publicity pertaining to
12 distribution of the software without specific, written prior
13 permission.
14
15 LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
16 INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
17 IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
18 SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
20 IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
21 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
22 THIS SOFTWARE.
23 ****************************************************************/
24
25 /* Binary search for keywords. */
26 /* Assuming
27 *
28 * typedef struct { char *name; ... } option_word;
29 * option_word keywords[] = { ... };
30 *
31 * b_search has calling sequence
32 *
33 * b_search((char *)keywords, (int)sizeof(option_word),
34 * (int)(number of keywords = sizeof(keywords)/sizeof(option_word),
35 * char **sp)
36 *
37 * where *sp is a string that may contain a keyword.
38 * No keyword ==> b_search returns 0 with **sp = 0.
39 * Bad keyword ==> b_search returns 0 with *sp = the bad keyword.
40 * Keyword matched ==> b_search returns the matched option_word*,
41 * sets *sp to the next token (skipping '=' if present), and sets
42 * oi->eqsign to "=" if '=' is present and to " " otherwise.
43 */
44
45 #ifdef __cplusplus
46 extern "C" {
47 #endif
48
49
50 #ifdef KR_headers
51 #define Void /* void */
52 #else
53 #define Void void
54 #endif
55
56 #ifdef Use_tolower
57 #include "ctype.h"
58 #define Tolower(x) tolower(x)
59 #else
60 static unsigned char lc[256];
61
62 static void
63 lc_init(Void)
64 {
65 register int i;
66 register char *s;
67 for(i = 0; i < 256; i++)
68 lc[i] = i;
69 for(s = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; *s; s++)
70 lc[*s] = *s + 'a' - 'A';
71 }
72 #define Tolower(x) lc[x]
73 #endif
74
75 #ifdef KR_headers
76 char *
b_search_ASL(ow,owsize,n,sp,peq)77 b_search_ASL(ow, owsize, n, sp, peq)
78 char *ow; int owsize, n; char **sp, **peq;
79 #else
80 void *
81 b_search_ASL(char *ow, int owsize, int n, char **sp, char **peq)
82 #endif
83 {
84 int c, c1, c2, n1;
85 char *s, *s1, *s2;
86 char *ow1;
87 #ifndef Use_tolower
88 static int first = 1;
89 if (first) {
90 lc_init();
91 first = 0;
92 }
93 #endif
94
95 for(s = *sp; (c = *(unsigned char *)s) <= ' '; s++)
96 if (!c)
97 goto no_ow1;
98
99 /* binary search */
100
101 while(n > 0) {
102 ow1 = ow + (n1 = n >> 1)*owsize;
103 s2 = *(char **)ow1;
104 for(s1 = s;; s1++) {
105 c1 = Tolower(*(unsigned char *)s1);
106 if (!(c2 = *s2++)) {
107 if (c1 <= ' ' || c1 == '=')
108 goto found;
109 break;
110 }
111 if (c1 != c2)
112 break;
113 }
114 if (c1 == '=' || c1 < c2)
115 n = n1;
116 else {
117 n -= n1 + 1;
118 ow = ow1 + owsize;
119 }
120 continue;
121 found:
122 *peq = " ";
123 while(*s1 && *s1 <= ' ')
124 s1++;
125 if (*s1 == '=') {
126 *peq = "=";
127 while(*++s1 && *s1 <= ' ');
128 }
129 *sp = s1;
130 return ow1;
131 }
132 no_ow1:
133 ow1 = 0;
134 *sp = s;
135 return 0;
136 }
137
138 #ifdef __cplusplus
139 }
140 #endif
141