xref: /openbsd-src/sys/dev/wscons/wskbdutil.c (revision a28daedfc357b214be5c701aa8ba8adb29a7f1c2)
1 /*	$OpenBSD: wskbdutil.c,v 1.7 2008/06/26 05:42:19 ray Exp $	*/
2 /*	$NetBSD: wskbdutil.c,v 1.7 1999/12/21 11:59:13 drochner Exp $	*/
3 
4 /*-
5  * Copyright (c) 1997 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Juergen Hannken-Illjes.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include <sys/param.h>
34 #include <sys/types.h>
35 #include <sys/cdefs.h>
36 #include <sys/errno.h>
37 #include <sys/systm.h>
38 #include <sys/malloc.h>
39 #include <dev/wscons/wsksymdef.h>
40 #include <dev/wscons/wsksymvar.h>
41 
42 static struct compose_tab_s {
43 	keysym_t elem[2];
44 	keysym_t result;
45 } compose_tab[] = {
46 	{ { KS_plus,			KS_plus },		KS_numbersign },
47 	{ { KS_a,			KS_a },			KS_at },
48 	{ { KS_parenleft,		KS_parenleft },		KS_bracketleft },
49 	{ { KS_slash,			KS_slash },		KS_backslash },
50 	{ { KS_parenright,		KS_parenright },	KS_bracketright },
51 	{ { KS_parenleft,		KS_minus },		KS_braceleft },
52 	{ { KS_slash,			KS_minus },		KS_bar },
53 	{ { KS_parenright,		KS_minus },		KS_braceright },
54 	{ { KS_exclam,			KS_exclam },		KS_exclamdown },
55 	{ { KS_c,			KS_slash },		KS_cent },
56 	{ { KS_l,			KS_minus },		KS_sterling },
57 	{ { KS_y,			KS_minus },		KS_yen },
58 	{ { KS_s,			KS_o },			KS_section },
59 	{ { KS_x,			KS_o },			KS_currency },
60 	{ { KS_c,			KS_o },			KS_copyright },
61 	{ { KS_less,			KS_less },		KS_guillemotleft },
62 	{ { KS_greater,			KS_greater },		KS_guillemotright },
63 	{ { KS_question,		KS_question },		KS_questiondown },
64 	{ { KS_dead_acute,		KS_space },		KS_apostrophe },
65 	{ { KS_dead_grave,		KS_space },		KS_grave },
66 	{ { KS_dead_tilde,		KS_space },		KS_asciitilde },
67 	{ { KS_dead_circumflex,		KS_space },		KS_asciicircum },
68 	{ { KS_dead_diaeresis,		KS_space },		KS_quotedbl },
69 	{ { KS_dead_cedilla,		KS_space },		KS_comma },
70 	{ { KS_dead_circumflex,		KS_A },			KS_Acircumflex },
71 	{ { KS_dead_diaeresis,		KS_A },			KS_Adiaeresis },
72 	{ { KS_dead_grave,		KS_A },			KS_Agrave },
73 	{ { KS_dead_abovering,		KS_A },			KS_Aring },
74 	{ { KS_dead_tilde,		KS_A },			KS_Atilde },
75 	{ { KS_dead_cedilla,		KS_C },			KS_Ccedilla },
76 	{ { KS_dead_acute,		KS_E },			KS_Eacute },
77 	{ { KS_dead_circumflex,		KS_E },			KS_Ecircumflex },
78 	{ { KS_dead_diaeresis,		KS_E },			KS_Ediaeresis },
79 	{ { KS_dead_grave,		KS_E },			KS_Egrave },
80 	{ { KS_dead_acute,		KS_I },			KS_Iacute },
81 	{ { KS_dead_circumflex,		KS_I },			KS_Icircumflex },
82 	{ { KS_dead_diaeresis,		KS_I },			KS_Idiaeresis },
83 	{ { KS_dead_grave,		KS_I },			KS_Igrave },
84 	{ { KS_dead_tilde,		KS_N },			KS_Ntilde },
85 	{ { KS_dead_acute,		KS_O },			KS_Oacute },
86 	{ { KS_dead_circumflex,		KS_O },			KS_Ocircumflex },
87 	{ { KS_dead_diaeresis,		KS_O },			KS_Odiaeresis },
88 	{ { KS_dead_grave,		KS_O },			KS_Ograve },
89 	{ { KS_dead_tilde,		KS_O },			KS_Otilde },
90 	{ { KS_dead_acute,		KS_U },			KS_Uacute },
91 	{ { KS_dead_circumflex,		KS_U },			KS_Ucircumflex },
92 	{ { KS_dead_diaeresis,		KS_U },			KS_Udiaeresis },
93 	{ { KS_dead_grave,		KS_U },			KS_Ugrave },
94 	{ { KS_dead_acute,		KS_Y },			KS_Yacute },
95 	{ { KS_dead_acute,		KS_a },			KS_aacute },
96 	{ { KS_dead_circumflex,		KS_a },			KS_acircumflex },
97 	{ { KS_dead_diaeresis,		KS_a },			KS_adiaeresis },
98 	{ { KS_dead_grave,		KS_a },			KS_agrave },
99 	{ { KS_dead_abovering,		KS_a },			KS_aring },
100 	{ { KS_dead_tilde,		KS_a },			KS_atilde },
101 	{ { KS_dead_cedilla,		KS_c },			KS_ccedilla },
102 	{ { KS_dead_acute,		KS_e },			KS_eacute },
103 	{ { KS_dead_circumflex,		KS_e },			KS_ecircumflex },
104 	{ { KS_dead_diaeresis,		KS_e },			KS_ediaeresis },
105 	{ { KS_dead_grave,		KS_e },			KS_egrave },
106 	{ { KS_dead_acute,		KS_i },			KS_iacute },
107 	{ { KS_dead_circumflex,		KS_i },			KS_icircumflex },
108 	{ { KS_dead_diaeresis,		KS_i },			KS_idiaeresis },
109 	{ { KS_dead_grave,		KS_i },			KS_igrave },
110 	{ { KS_dead_tilde,		KS_n },			KS_ntilde },
111 	{ { KS_dead_acute,		KS_o },			KS_oacute },
112 	{ { KS_dead_circumflex,		KS_o },			KS_ocircumflex },
113 	{ { KS_dead_diaeresis,		KS_o },			KS_odiaeresis },
114 	{ { KS_dead_grave,		KS_o },			KS_ograve },
115 	{ { KS_dead_tilde,		KS_o },			KS_otilde },
116 	{ { KS_dead_acute,		KS_u },			KS_uacute },
117 	{ { KS_dead_circumflex,		KS_u },			KS_ucircumflex },
118 	{ { KS_dead_diaeresis,		KS_u },			KS_udiaeresis },
119 	{ { KS_dead_grave,		KS_u },			KS_ugrave },
120 	{ { KS_dead_acute,		KS_y },			KS_yacute },
121 	{ { KS_dead_diaeresis,		KS_y },			KS_ydiaeresis },
122 	{ { KS_quotedbl,		KS_A },			KS_Adiaeresis },
123 	{ { KS_quotedbl,		KS_E },			KS_Ediaeresis },
124 	{ { KS_quotedbl,		KS_I },			KS_Idiaeresis },
125 	{ { KS_quotedbl,		KS_O },			KS_Odiaeresis },
126 	{ { KS_quotedbl,		KS_U },			KS_Udiaeresis },
127 	{ { KS_quotedbl,		KS_a },			KS_adiaeresis },
128 	{ { KS_quotedbl,		KS_e },			KS_ediaeresis },
129 	{ { KS_quotedbl,		KS_i },			KS_idiaeresis },
130 	{ { KS_quotedbl,		KS_o },			KS_odiaeresis },
131 	{ { KS_quotedbl,		KS_u },			KS_udiaeresis },
132 	{ { KS_quotedbl,		KS_y },			KS_ydiaeresis },
133 	{ { KS_acute,			KS_A },			KS_Aacute },
134 	{ { KS_asciicircum,		KS_A },			KS_Acircumflex },
135 	{ { KS_grave,			KS_A },			KS_Agrave },
136 	{ { KS_asterisk,		KS_A },			KS_Aring },
137 	{ { KS_asciitilde,		KS_A },			KS_Atilde },
138 	{ { KS_cedilla,			KS_C },			KS_Ccedilla },
139 	{ { KS_acute,			KS_E },			KS_Eacute },
140 	{ { KS_asciicircum,		KS_E },			KS_Ecircumflex },
141 	{ { KS_grave,			KS_E },			KS_Egrave },
142 	{ { KS_acute,			KS_I },			KS_Iacute },
143 	{ { KS_asciicircum,		KS_I },			KS_Icircumflex },
144 	{ { KS_grave,			KS_I },			KS_Igrave },
145 	{ { KS_asciitilde,		KS_N },			KS_Ntilde },
146 	{ { KS_acute,			KS_O },			KS_Oacute },
147 	{ { KS_asciicircum,		KS_O },			KS_Ocircumflex },
148 	{ { KS_grave,			KS_O },			KS_Ograve },
149 	{ { KS_asciitilde,		KS_O },			KS_Otilde },
150 	{ { KS_acute,			KS_U },			KS_Uacute },
151 	{ { KS_asciicircum,		KS_U },			KS_Ucircumflex },
152 	{ { KS_grave,			KS_U },			KS_Ugrave },
153 	{ { KS_acute,			KS_Y },			KS_Yacute },
154 	{ { KS_acute,			KS_a },			KS_aacute },
155 	{ { KS_asciicircum,		KS_a },			KS_acircumflex },
156 	{ { KS_grave,			KS_a },			KS_agrave },
157 	{ { KS_asterisk,		KS_a },			KS_aring },
158 	{ { KS_asciitilde,		KS_a },			KS_atilde },
159 	{ { KS_cedilla,			KS_c },			KS_ccedilla },
160 	{ { KS_acute,			KS_e },			KS_eacute },
161 	{ { KS_asciicircum,		KS_e },			KS_ecircumflex },
162 	{ { KS_grave,			KS_e },			KS_egrave },
163 	{ { KS_acute,			KS_i },			KS_iacute },
164 	{ { KS_asciicircum,		KS_i },			KS_icircumflex },
165 	{ { KS_grave,			KS_i },			KS_igrave },
166 	{ { KS_asciitilde,		KS_n },			KS_ntilde },
167 	{ { KS_acute,			KS_o },			KS_oacute },
168 	{ { KS_asciicircum,		KS_o },			KS_ocircumflex },
169 	{ { KS_grave,			KS_o },			KS_ograve },
170 	{ { KS_asciitilde,		KS_o },			KS_otilde },
171 	{ { KS_acute,			KS_u },			KS_uacute },
172 	{ { KS_asciicircum,		KS_u },			KS_ucircumflex },
173 	{ { KS_grave,			KS_u },			KS_ugrave },
174 	{ { KS_acute,			KS_y },			KS_yacute }
175 };
176 
177 #define COMPOSE_SIZE	sizeof(compose_tab)/sizeof(compose_tab[0])
178 
179 static int compose_tab_inorder = 0;
180 
181 inline int compose_tab_cmp(struct compose_tab_s *,
182 				struct compose_tab_s *);
183 keysym_t ksym_upcase(keysym_t);
184 void fillmapentry(const keysym_t *, int, struct wscons_keymap *);
185 
186 inline int
187 compose_tab_cmp(i, j)
188 	struct compose_tab_s *i, *j;
189 {
190 	if (i->elem[0] == j->elem[0])
191 		return(i->elem[1] - j->elem[1]);
192 	else
193 		return(i->elem[0] - j->elem[0]);
194 }
195 
196 keysym_t
197 wskbd_compose_value(compose_buf)
198 	keysym_t *compose_buf;
199 {
200 	int i, j, r;
201 	struct compose_tab_s v;
202 
203 	if (! compose_tab_inorder) {
204 		/* Insertion sort. */
205 		for (i = 1; i < COMPOSE_SIZE; i++) {
206 			v = compose_tab[i];
207 			/* find correct slot, moving others up */
208 			for (j = i; --j >= 0 && compose_tab_cmp(& v, & compose_tab[j]) < 0; )
209 				compose_tab[j + 1] = compose_tab[j];
210 			compose_tab[j + 1] = v;
211 		}
212 		compose_tab_inorder = 1;
213 	}
214 
215 	for (j = 0, i = COMPOSE_SIZE; i != 0; i /= 2) {
216 		if (compose_tab[j + i/2].elem[0] == compose_buf[0]) {
217 			if (compose_tab[j + i/2].elem[1] == compose_buf[1])
218 				return(compose_tab[j + i/2].result);
219 			r = compose_tab[j + i/2].elem[1] < compose_buf[1];
220 		} else
221 			r = compose_tab[j + i/2].elem[0] < compose_buf[0];
222 		if (r) {
223 			j += i/2 + 1;
224 			i--;
225 		}
226 	}
227 
228 	return(KS_voidSymbol);
229 }
230 
231 static const u_char latin1_to_upper[256] = {
232 /*      0  8  1  9  2  a  3  b  4  c  5  d  6  e  7  f               */
233 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 0 */
234 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 0 */
235 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 1 */
236 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 1 */
237 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 2 */
238 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 2 */
239 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 3 */
240 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 3 */
241 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 4 */
242 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 4 */
243 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 5 */
244 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 5 */
245 	0x00,  'A',  'B',  'C',  'D',  'E',  'F',  'G',		/* 6 */
246 	 'H',  'I',  'J',  'K',  'L',  'M',  'N',  'O',		/* 6 */
247 	 'P',  'Q',  'R',  'S',  'T',  'U',  'V',  'W',		/* 7 */
248 	 'X',  'Y',  'Z', 0x00, 0x00, 0x00, 0x00, 0x00,		/* 7 */
249 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 8 */
250 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 8 */
251 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 9 */
252 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 9 */
253 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* a */
254 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* a */
255 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* b */
256 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* b */
257 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* c */
258 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* c */
259 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* d */
260 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* d */
261 	0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,		/* e */
262 	0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf,		/* e */
263 	0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0x00,		/* f */
264 	0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0x00,		/* f */
265 };
266 
267 keysym_t
268 ksym_upcase(ksym)
269 	keysym_t ksym;
270 {
271 	if (ksym >= KS_f1 && ksym <= KS_f20)
272 		return(KS_F1 - KS_f1 + ksym);
273 
274 	if (KS_GROUP(ksym) == KS_GROUP_Ascii && ksym <= 0xff &&
275 	    latin1_to_upper[ksym] != 0x00)
276 		return(latin1_to_upper[ksym]);
277 
278 	return(ksym);
279 }
280 
281 void
282 fillmapentry(kp, len, mapentry)
283 	const keysym_t *kp;
284 	int len;
285 	struct wscons_keymap *mapentry;
286 {
287 	switch (len) {
288 	case 0:
289 		mapentry->group1[0] = KS_voidSymbol;
290 		mapentry->group1[1] = KS_voidSymbol;
291 		mapentry->group2[0] = KS_voidSymbol;
292 		mapentry->group2[1] = KS_voidSymbol;
293 		break;
294 
295 	case 1:
296 		mapentry->group1[0] = kp[0];
297 		mapentry->group1[1] = ksym_upcase(kp[0]);
298 		mapentry->group2[0] = mapentry->group1[0];
299 		mapentry->group2[1] = mapentry->group1[1];
300 		break;
301 
302 	case 2:
303 		mapentry->group1[0] = kp[0];
304 		mapentry->group1[1] = kp[1];
305 		mapentry->group2[0] = mapentry->group1[0];
306 		mapentry->group2[1] = mapentry->group1[1];
307 		break;
308 
309 	case 3:
310 		mapentry->group1[0] = kp[0];
311 		mapentry->group1[1] = kp[1];
312 		mapentry->group2[0] = kp[2];
313 		mapentry->group2[1] = ksym_upcase(kp[2]);
314 		break;
315 
316 	case 4:
317 		mapentry->group1[0] = kp[0];
318 		mapentry->group1[1] = kp[1];
319 		mapentry->group2[0] = kp[2];
320 		mapentry->group2[1] = kp[3];
321 		break;
322 
323 	}
324 }
325 
326 void
327 wskbd_get_mapentry(mapdata, kc, mapentry)
328 	const struct wskbd_mapdata *mapdata;
329 	int kc;
330 	struct wscons_keymap *mapentry;
331 {
332 	kbd_t cur;
333 	const keysym_t *kp;
334 	const struct wscons_keydesc *mp;
335 	int l;
336 	keysym_t ksg;
337 
338 	mapentry->command = KS_voidSymbol;
339 	mapentry->group1[0] = KS_voidSymbol;
340 	mapentry->group1[1] = KS_voidSymbol;
341 	mapentry->group2[0] = KS_voidSymbol;
342 	mapentry->group2[1] = KS_voidSymbol;
343 
344 	for (cur = mapdata->layout & ~KB_HANDLEDBYWSKBD; cur != 0; ) {
345 		mp = mapdata->keydesc;
346 		while (mp->map_size > 0) {
347 			if (mp->name == cur)
348 				break;
349 			mp++;
350 		}
351 
352 		/* If map not found, return */
353 		if (mp->map_size <= 0)
354 			return;
355 
356 		for (kp = mp->map; kp < mp->map + mp->map_size; kp++) {
357 			ksg = KS_GROUP(*kp);
358 			if (ksg == KS_GROUP_Keycode &&
359 			    KS_VALUE(*kp) == kc) {
360 				/* First skip keycode and possible command */
361 				kp++;
362 				if (KS_GROUP(*kp) == KS_GROUP_Command ||
363 				    *kp == KS_Cmd || *kp == KS_Cmd1 || *kp == KS_Cmd2)
364 					mapentry->command = *kp++;
365 
366 				for (l = 0; kp + l < mp->map + mp->map_size;
367 				    l++) {
368 					ksg = KS_GROUP(kp[l]);
369 					if (ksg == KS_GROUP_Keycode)
370 						break;
371 				}
372 				if (l > 4)
373 					panic("wskbd_get_mapentry: %d(%d): bad entry",
374 					      mp->name, *kp);
375 				fillmapentry(kp, l, mapentry);
376 				return;
377 			}
378 		}
379 
380 		cur = mp->base;
381 	}
382 }
383 
384 void
385 wskbd_init_keymap(newlen, map, maplen)
386 	int newlen;
387 	struct wscons_keymap **map;
388 	int *maplen;
389 {
390 	int i;
391 
392 	if (newlen != *maplen) {
393 		if (*maplen > 0)
394 			free(*map, M_TEMP);
395 		*maplen = newlen;
396 		*map = malloc(newlen*sizeof(struct wscons_keymap),
397 			      M_TEMP, M_WAITOK);
398 	}
399 
400 	for (i = 0; i < *maplen; i++) {
401 		(*map)[i].command = KS_voidSymbol;
402 		(*map)[i].group1[0] = KS_voidSymbol;
403 		(*map)[i].group1[1] = KS_voidSymbol;
404 		(*map)[i].group2[0] = KS_voidSymbol;
405 		(*map)[i].group2[1] = KS_voidSymbol;
406 	}
407 }
408 
409 int
410 wskbd_load_keymap(mapdata, map, maplen)
411 	const struct wskbd_mapdata *mapdata;
412 	struct wscons_keymap **map;
413 	int *maplen;
414 {
415 	int i, s, kc, stack_ptr;
416 	const keysym_t *kp;
417 	const struct wscons_keydesc *mp, *stack[10];
418 	kbd_t cur;
419 	keysym_t ksg;
420 
421 	for (cur = mapdata->layout & ~KB_HANDLEDBYWSKBD, stack_ptr = 0;
422 	     cur != 0; stack_ptr++) {
423 		mp = mapdata->keydesc;
424 		while (mp->map_size > 0) {
425 			if (cur == 0 || mp->name == cur) {
426 				break;
427 			}
428 			mp++;
429 		}
430 
431 		if (stack_ptr == sizeof(stack)/sizeof(stack[0]))
432 			panic("wskbd_load_keymap: %d: recursion too deep",
433 			      mapdata->layout);
434 		if (mp->map_size <= 0)
435 			return(EINVAL);
436 
437 		stack[stack_ptr] = mp;
438 		cur = mp->base;
439 	}
440 
441 	for (i = 0, s = stack_ptr - 1; s >= 0; s--) {
442 		mp = stack[s];
443 		for (kp = mp->map; kp < mp->map + mp->map_size; kp++) {
444 			ksg = KS_GROUP(*kp);
445 			if (ksg == KS_GROUP_Keycode && KS_VALUE(*kp) > i)
446 				i = KS_VALUE(*kp);
447 		}
448 	}
449 
450 	wskbd_init_keymap(i + 1, map, maplen);
451 
452 	for (s = stack_ptr - 1; s >= 0; s--) {
453 		mp = stack[s];
454 		for (kp = mp->map; kp < mp->map + mp->map_size; ) {
455 			ksg = KS_GROUP(*kp);
456 			if (ksg != KS_GROUP_Keycode)
457 				panic("wskbd_load_keymap: %d(%d): bad entry",
458 				      mp->name, *kp);
459 
460 			kc = KS_VALUE(*kp);
461 			kp++;
462 
463 			if (KS_GROUP(*kp) == KS_GROUP_Command ||
464 			    *kp == KS_Cmd || *kp == KS_Cmd1 || *kp == KS_Cmd2) {
465 				(*map)[kc].command = *kp;
466 				kp++;
467 			}
468 
469 			for (i = 0; kp + i < mp->map + mp->map_size; i++) {
470 				ksg = KS_GROUP(kp[i]);
471 				if (ksg == KS_GROUP_Keycode)
472 					break;
473 			}
474 
475 			if (i > 4)
476 				panic("wskbd_load_keymap: %d(%d): bad entry",
477 				      mp->name, *kp);
478 
479 			fillmapentry(kp, i, &(*map)[kc]);
480 			kp += i;
481 		}
482 	}
483 
484 	return(0);
485 }
486