Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / osf / uil / UilKeyTab.c
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these librararies and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /* 
24  *  @OSF_COPYRIGHT@
25  *  COPYRIGHT NOTICE
26  *  Copyright (c) 1990, 1991, 1992, 1993 Open Software Foundation, Inc.
27  *  ALL RIGHTS RESERVED (MOTIF). See the file named COPYRIGHT.MOTIF for
28  *  the full copyright text.
29 */ 
30 /* 
31  * HISTORY
32 */ 
33 #ifdef REV_INFO
34 #ifndef lint
35 static char rcsid[] = "$XConsortium: UilKeyTab.c /main/11 1995/07/14 09:34:29 drk $"
36 #endif
37 #endif
38
39 /*
40 *  (c) Copyright 1989, 1990, DIGITAL EQUIPMENT CORPORATION, MAYNARD, MASS. */
41
42 /*
43 **++
44 **  FACILITY:
45 **
46 **      User Interface Language Compiler (UIL)
47 **
48 **  ABSTRACT:
49 **
50 **      This module contains the keyword table used by the lexical analyzer
51 **      to look up the keywords in the UIL.
52 **
53 **--
54 **/
55
56
57 /*
58 **
59 **  INCLUDE FILES
60 **
61 **/
62
63 #include "UilDefI.h"
64
65
66 /*
67 **
68 **  DEFINE and MACRO DEFINITIONS
69 **
70 **/
71
72
73 /*
74 **
75 **  EXTERNAL VARIABLE DECLARATIONS
76 **
77 **/
78
79
80 /*
81 **
82 **  GLOBAL VARIABLE DECLARATIONS
83 **
84 **/
85
86
87 /*
88 **
89 **  OWN VARIABLE DECLARATIONS
90 **
91 **/
92
93 /*    Keyword table pointer.    */
94
95 static key_keytable_entry_type * key_keytable_ptr;
96
97 \f
98 /*
99 **++
100 **  FUNCTIONAL DESCRIPTION:
101 **
102 **      This routine searches for a symbol in the compiler's keyword table.
103 **      There are two arguments to the routine, the length of the symbol and
104 **      the address of the start of the symbol.  The routine returns the
105 **      address of the keyword entry found, or a NULL pointer if the
106 **      symbol is not found in the table.
107 **
108 **      The search for the symbol is case sensitive depending upon the
109 **      keytable binding that was established by the key_initialize routine.
110 **
111 **      The require file UilKeyTab.h defines and initializes the keyword
112 **      tables.  It is built automatically from other files, thus it should
113 **      not be hand editted.
114 **
115 **  FORMAL PARAMETERS:
116 **
117 **      symbol_length.rl.v :    length of symbol to look up
118 **      symbol_ptr.ra.v :       address of symbol to look up
119 **
120 **  IMPLICIT INPUTS:
121 **
122 **      key_keytable_ptr                : current keyword table
123 **
124 **  IMPLICIT OUTPUTS:
125 **
126 **      none
127 **
128 **  FUNCTION VALUE:
129 **
130 **      NULL            : if the symbol is not in the keyword table
131 **      otherwise       : the address of the keyword table entry for
132 **                        the specified symbol.
133 **
134 ** SIDE EFFECTS:
135 **
136 **      none
137 **
138 **--
139 **/
140 key_keytable_entry_type *
141         key_find_keyword (symbol_length, symbol_ptr)
142
143 unsigned int    symbol_length;
144 char            * symbol_ptr;
145
146 {
147     
148     int
149         lower_limit,
150         upper_limit;
151     
152 /*    Check the arguments.    */
153
154     if (symbol_length > key_k_keyword_max_length)
155         return NULL;
156
157 /*    Initialize region to search.    */
158     
159     lower_limit = 0;
160     upper_limit = key_k_keyword_count-1;
161     
162 /*    Perform binary search on keyword index.    */
163     
164     do {
165         int             mid_point, result;
166
167         key_keytable_entry_type * keyword_entry_ptr;
168
169         mid_point = (lower_limit + upper_limit) >> 1;   /* divide by 2 */
170
171         keyword_entry_ptr = & key_keytable_ptr [mid_point];
172
173         result = strcmp (symbol_ptr, keyword_entry_ptr -> at_name);
174
175         if (result == 0) {
176             return keyword_entry_ptr;           /*    Found keyword.    */
177         } else if (result < 0) {
178             upper_limit = mid_point - 1;        /*    Search lower half.    */
179         } else {
180             lower_limit = mid_point + 1;        /*    Search upper half.    */
181         }
182
183     } while (lower_limit <= upper_limit);
184
185 /*    If we fall out of the bottom of the loop, symbol was not found.    */
186
187     return NULL;
188
189 }
190 \f
191 /*
192 **++
193 **  FUNCTIONAL DESCRIPTION:
194 **
195 **      This routine initializes the keyword lookup facility.  It can be
196 **      called multiple times during a single compilation.  It must be called
197 **      at least once before the keyword table is accessed.
198 **
199 **  FORMAL PARAMETERS:
200 **
201 **      none
202 **
203 **  IMPLICIT INPUTS:
204 **
205 **      uil_v_case_sensitive    : case sensitive switch, determines which
206 **                              : keyword table to use.
207 **
208 **  IMPLICIT OUTPUTS:
209 **
210 **      key_keytable_ptr        : pointer to the keyword table to
211 **                                use for keyword lookups.
212 **
213 **  FUNCTION VALUE:
214 **
215 **      none
216 **
217 ** SIDE EFFECTS:
218 **
219 **      none
220 **
221 **--
222 **/
223 void
224         key_initialize ()
225
226 {
227
228 /*    Use the correct keyword table based on the global case
229       sensitivity.   */
230
231     if (uil_v_case_sensitive) {
232         key_keytable_ptr = key_table;
233     } else {
234         key_keytable_ptr = key_table_case_ins;
235     }
236
237 }    
238