util.c revision 1.24 1 /* $NetBSD: util.c,v 1.24 2006/02/05 17:38:33 jmmv Exp $ */
2
3 /*-
4 * Copyright (c) 1998, 2006 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Juergen Hannken-Illjes and Julio M. Merino Vidal.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 #include <sys/time.h>
40 #include <dev/wscons/wsconsio.h>
41 #include <dev/wscons/wsksymdef.h>
42
43 #include <err.h>
44 #include <errno.h>
45 #include <limits.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <unistd.h>
50
51 #include "wsconsctl.h"
52
53 #define TABLEN(t) (sizeof(t)/sizeof(t[0]))
54
55 extern struct wskbd_map_data kbmap; /* from keyboard.c */
56 extern struct wskbd_map_data newkbmap; /* from map_parse.y */
57
58 struct nameint {
59 int value;
60 const char *name;
61 };
62
63 static struct nameint kbtype_tab[] = {
64 { WSKBD_TYPE_LK201, "lk201" },
65 { WSKBD_TYPE_LK401, "lk401" },
66 { WSKBD_TYPE_PC_XT, "pc-xt" },
67 { WSKBD_TYPE_PC_AT, "pc-at" },
68 { WSKBD_TYPE_USB, "usb" },
69 { WSKBD_TYPE_HPC_KBD, "hpc-kbd" },
70 { WSKBD_TYPE_HPC_BTN, "hpc-btn" },
71 { WSKBD_TYPE_ARCHIMEDES, "archimedes" },
72 { WSKBD_TYPE_RISCPC, "riscpc" },
73 { WSKBD_TYPE_ADB, "adb" },
74 { WSKBD_TYPE_HIL, "hil" },
75 { WSKBD_TYPE_AMIGA, "amiga" },
76 { WSKBD_TYPE_MAPLE, "maple" },
77 { WSKBD_TYPE_ATARI, "atari" },
78 { WSKBD_TYPE_SUN, "sun" },
79 { WSKBD_TYPE_SUN5, "sun-type5" },
80 { WSKBD_TYPE_SGI, "sgi" },
81 { WSKBD_TYPE_MATRIXKP, "matrix-keypad" },
82 };
83
84 static struct nameint mstype_tab[] = {
85 { WSMOUSE_TYPE_VSXXX, "dec-tc" },
86 { WSMOUSE_TYPE_PS2, "ps2" },
87 { WSMOUSE_TYPE_USB, "usb" },
88 { WSMOUSE_TYPE_LMS, "logitech-bus" },
89 { WSMOUSE_TYPE_MMS, "ms-inport" },
90 { WSMOUSE_TYPE_TPANEL, "touch-panel" },
91 { WSMOUSE_TYPE_NEXT, "next" },
92 { WSMOUSE_TYPE_ARCHIMEDES, "archimedes" },
93 { WSMOUSE_TYPE_HIL, "hil" },
94 { WSMOUSE_TYPE_AMIGA, "amiga" },
95 { WSMOUSE_TYPE_MAXINE, "dec-maxine" },
96 { WSMOUSE_TYPE_MAPLE, "maple" },
97 };
98
99 static struct nameint dpytype_tab[] = {
100 { WSDISPLAY_TYPE_UNKNOWN, "unknown" },
101 { WSDISPLAY_TYPE_PM_MONO, "dec-pm-mono" },
102 { WSDISPLAY_TYPE_PM_COLOR, "dec-pm-color" },
103 { WSDISPLAY_TYPE_CFB, "dec-cfb" },
104 { WSDISPLAY_TYPE_XCFB, "dec-xcfb" },
105 { WSDISPLAY_TYPE_MFB, "dec-mfb" },
106 { WSDISPLAY_TYPE_SFB, "dec-sfb" },
107 { WSDISPLAY_TYPE_ISAVGA, "vga-isa" },
108 { WSDISPLAY_TYPE_PCIVGA, "vga-pci" },
109 { WSDISPLAY_TYPE_TGA, "dec-tga-pci" },
110 { WSDISPLAY_TYPE_SFBP, "dec-sfb+" },
111 { WSDISPLAY_TYPE_PCIMISC, "generic-pci" },
112 { WSDISPLAY_TYPE_NEXTMONO, "next-mono" },
113 { WSDISPLAY_TYPE_PX, "dec-px" },
114 { WSDISPLAY_TYPE_PXG, "dec-pxg" },
115 { WSDISPLAY_TYPE_TX, "dec-tx" },
116 { WSDISPLAY_TYPE_HPCFB, "generic-hpc" },
117 { WSDISPLAY_TYPE_VIDC, "arm-vidc" },
118 { WSDISPLAY_TYPE_SPX, "dec-spx" },
119 { WSDISPLAY_TYPE_GPX, "dec-gpx" },
120 { WSDISPLAY_TYPE_LCG, "dec-lcg" },
121 { WSDISPLAY_TYPE_VAX_MONO, "dec-vax-mono" },
122 { WSDISPLAY_TYPE_SB_P9100, "sparcbook-p9100" },
123 { WSDISPLAY_TYPE_EGA, "ega" },
124 { WSDISPLAY_TYPE_DCPVR, "dreamcast-pvr" },
125 { WSDISPLAY_TYPE_GATOR, "hp-gator" },
126 { WSDISPLAY_TYPE_TOPCAT, "hp-topcat" },
127 { WSDISPLAY_TYPE_RENAISSANCE, "hp-renaissance" },
128 { WSDISPLAY_TYPE_CATSEYE, "hp-catseye" },
129 { WSDISPLAY_TYPE_DAVINCI, "hp-davinci" },
130 { WSDISPLAY_TYPE_TIGER, "hp-tiger" },
131 { WSDISPLAY_TYPE_HYPERION, "hp-hyperion" },
132 { WSDISPLAY_TYPE_AMIGACC, "amiga-cc" },
133 { WSDISPLAY_TYPE_SUN24, "sun24" },
134 { WSDISPLAY_TYPE_NEWPORT, "sgi-newport" },
135 { WSDISPLAY_TYPE_GR2, "sgi-gr2" },
136 { WSDISPLAY_TYPE_SUNCG12, "suncg12" },
137 { WSDISPLAY_TYPE_SUNCG14, "suncg14" },
138 { WSDISPLAY_TYPE_SUNTCX, "suntcx" },
139 { WSDISPLAY_TYPE_SUNFFB, "sunffb" },
140 };
141
142 static struct nameint kbdenc_tab[] = {
143 KB_ENCTAB
144 };
145
146 static struct nameint kbdvar_tab[] = {
147 KB_VARTAB
148 };
149
150 static struct nameint color_tab[] = {
151 { WSCOL_UNSUPPORTED, "unsupported" },
152 { WSCOL_BLACK, "black" },
153 { WSCOL_RED, "red" },
154 { WSCOL_GREEN, "green" },
155 { WSCOL_BROWN, "brown" },
156 { WSCOL_BLUE, "blue" },
157 { WSCOL_MAGENTA, "magenta" },
158 { WSCOL_CYAN, "cyan" },
159 { WSCOL_WHITE, "white" },
160 };
161
162 static struct nameint attr_tab[] = {
163 { WSATTR_NONE, "none" },
164 { WSATTR_REVERSE, "reverse" },
165 { WSATTR_HILIT, "hilit" },
166 { WSATTR_BLINK, "blink" },
167 { WSATTR_UNDERLINE, "underline" },
168 { WSATTR_WSCOLORS, "color" },
169 };
170
171 static struct field *field_tab;
172 static int field_tab_len;
173
174 static const char *int2name(int, int, struct nameint *, int);
175 static int name2int(char *, struct nameint *, int);
176 static void print_kmap(struct wskbd_map_data *);
177 static unsigned int rd_bitfield(const char *);
178 static void pr_bitfield(unsigned int);
179
180 void
181 field_setup(struct field *ftab, int len)
182 {
183 field_tab = ftab;
184 field_tab_len = len;
185 }
186
187 struct field *
188 field_by_name(char *name)
189 {
190 int i;
191
192 for (i = 0; i < field_tab_len; i++)
193 if (strcmp(field_tab[i].name, name) == 0)
194 return(field_tab + i);
195
196 errx(1, "%s: not found", name);
197 }
198
199 struct field *
200 field_by_value(void *addr)
201 {
202 int i;
203
204 for (i = 0; i < field_tab_len; i++)
205 if (field_tab[i].valp == addr)
206 return(field_tab + i);
207
208 errx(1, "internal error: field_by_value: not found");
209 }
210
211 void
212 field_disable_by_value(void *addr)
213 {
214 struct field *f;
215
216 f = field_by_value(addr);
217 f->flags |= FLG_DISABLED;
218 }
219
220 static const char *
221 int2name(int val, int uflag, struct nameint *tab, int len)
222 {
223 static char tmp[20];
224 int i;
225
226 for (i = 0; i < len; i++)
227 if (tab[i].value == val)
228 return(tab[i].name);
229
230 if (uflag) {
231 snprintf(tmp, sizeof(tmp), "unknown_%d", val);
232 return(tmp);
233 } else
234 return(NULL);
235 }
236
237 static int
238 name2int(char *val, struct nameint *tab, int len)
239 {
240 int i;
241
242 for (i = 0; i < len; i++)
243 if (strcmp(tab[i].name, val) == 0)
244 return(tab[i].value);
245 return(-1);
246 }
247
248 void
249 pr_field(struct field *f, const char *sep)
250 {
251 const char *p;
252 u_int flags;
253 int first, i, mask;
254
255 if (sep)
256 printf("%s%s", f->name, sep);
257
258 switch (f->format) {
259 case FMT_UINT:
260 printf("%u", *((u_int *) f->valp));
261 break;
262 case FMT_STRING:
263 printf("\"%s\"", *((char **) f->valp));
264 break;
265 case FMT_BITFIELD:
266 pr_bitfield(*((unsigned int *) f->valp));
267 break;
268 case FMT_KBDTYPE:
269 p = int2name(*((u_int *) f->valp), 1,
270 kbtype_tab, TABLEN(kbtype_tab));
271 printf("%s", p);
272 break;
273 case FMT_MSTYPE:
274 p = int2name(*((u_int *) f->valp), 1,
275 mstype_tab, TABLEN(mstype_tab));
276 printf("%s", p);
277 break;
278 case FMT_DPYTYPE:
279 p = int2name(*((u_int *) f->valp), 1,
280 dpytype_tab, TABLEN(dpytype_tab));
281 printf("%s", p);
282 break;
283 case FMT_KBDENC:
284 p = int2name(KB_ENCODING(*((u_int *) f->valp)), 1,
285 kbdenc_tab, TABLEN(kbdenc_tab));
286 printf("%s", p);
287
288 flags = KB_VARIANT(*((u_int *) f->valp));
289 for (i = 0; i < 32; i++) {
290 if (!(flags & (1 << i)))
291 continue;
292 p = int2name(flags & (1 << i), 1,
293 kbdvar_tab, TABLEN(kbdvar_tab));
294 printf(".%s", p);
295 }
296 break;
297 case FMT_KBMAP:
298 print_kmap((struct wskbd_map_data *) f->valp);
299 break;
300 case FMT_COLOR:
301 p = int2name(*((u_int *) f->valp), 1,
302 color_tab, TABLEN(color_tab));
303 printf("%s", p);
304 break;
305 case FMT_ATTRS:
306 mask = 0x10;
307 first = 1;
308 while (mask > 0) {
309 if (*((u_int *) f->valp) & mask) {
310 p = int2name(*((u_int *) f->valp) & mask, 1,
311 attr_tab, TABLEN(attr_tab));
312 printf("%s%s", first ? "" : ",", p);
313 first = 0;
314 }
315 mask >>= 1;
316 }
317 if (first)
318 printf("none");
319 break;
320 default:
321 errx(1, "internal error: pr_field: no format %d", f->format);
322 break;
323 }
324
325 printf("\n");
326 }
327
328 static void
329 pr_bitfield(unsigned int f)
330 {
331
332 if (f == 0)
333 printf("none");
334 else {
335 int i, first, mask;
336
337 for (i = 0, first = 1, mask = 1; i < sizeof(f) * 8; i++) {
338 if (f & mask) {
339 printf("%s%d", first ? "" : " ", i);
340 first = 0;
341 }
342 mask = mask << 1;
343 }
344 }
345 }
346
347 void
348 rd_field(struct field *f, char *val, int merge)
349 {
350 int i;
351 u_int u;
352 char *p;
353 struct wscons_keymap *mp;
354
355 switch (f->format) {
356 case FMT_UINT:
357 if (sscanf(val, "%u", &u) != 1)
358 errx(1, "%s: not a number", val);
359 if (merge)
360 *((u_int *) f->valp) += u;
361 else
362 *((u_int *) f->valp) = u;
363 break;
364 case FMT_STRING:
365 if ((*((char **) f->valp) = strdup(val)) == NULL)
366 err(1, "strdup");
367 break;
368 case FMT_BITFIELD:
369 *((unsigned int *) f->valp) = rd_bitfield(val);
370 break;
371 case FMT_KBDENC:
372 p = strchr(val, '.');
373 if (p != NULL)
374 *p++ = '\0';
375
376 i = name2int(val, kbdenc_tab, TABLEN(kbdenc_tab));
377 if (i == -1)
378 errx(1, "%s: not a valid encoding", val);
379 *((u_int *) f->valp) = i;
380
381 while (p) {
382 val = p;
383 p = strchr(p, '.');
384 if (p != NULL)
385 *p++ = '\0';
386 i = name2int(val, kbdvar_tab, TABLEN(kbdvar_tab));
387 if (i == -1)
388 errx(1, "%s: not a valid variant", val);
389 *((u_int *) f->valp) |= i;
390 }
391 break;
392 case FMT_KBMAP:
393 if (! merge)
394 kbmap.maplen = 0;
395 map_scan_setinput(val);
396 yyparse();
397 if (merge) {
398 if (newkbmap.maplen < kbmap.maplen)
399 newkbmap.maplen = kbmap.maplen;
400 for (i = 0; i < kbmap.maplen; i++) {
401 mp = newkbmap.map + i;
402 if (mp->command == KS_voidSymbol &&
403 mp->group1[0] == KS_voidSymbol &&
404 mp->group1[1] == KS_voidSymbol &&
405 mp->group2[0] == KS_voidSymbol &&
406 mp->group2[1] == KS_voidSymbol)
407 *mp = kbmap.map[i];
408 }
409 }
410 kbmap.maplen = newkbmap.maplen;
411 bcopy(newkbmap.map, kbmap.map,
412 kbmap.maplen*sizeof(struct wscons_keymap));
413 break;
414 case FMT_COLOR:
415 i = name2int(val, color_tab, TABLEN(color_tab));
416 if (i == -1)
417 errx(1, "%s: not a valid color", val);
418 *((u_int *) f->valp) = i;
419 break;
420 case FMT_ATTRS:
421 p = val;
422 while (p) {
423 val = p;
424 p = strchr(p, ',');
425 if (p != NULL)
426 *p++ = '\0';
427 i = name2int(val, attr_tab, TABLEN(attr_tab));
428 if (i == -1)
429 errx(1, "%s: not a valid attribute", val);
430 *((u_int *) f->valp) |= i;
431 }
432 break;
433 default:
434 errx(1, "internal error: rd_field: no format %d", f->format);
435 break;
436 }
437 }
438
439 static unsigned int
440 rd_bitfield(const char *str)
441 {
442 const char *ptr;
443 char *ep;
444 long lval;
445 unsigned int result;
446
447 ep = NULL;
448 ptr = str;
449 result = 0;
450 while (*ptr != '\0') {
451 errno = 0;
452 lval = strtol(ptr, &ep, 10);
453 if (*ep != '\0' && *ep != ' ')
454 errx(1, "%s: not a valid number list", str);
455 if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN))
456 errx(1, "%s: not a valid number list", str);
457 if (lval >= sizeof(result) * 8)
458 errx(1, "%ld: number out of range", lval);
459 result |= (1 << lval);
460
461 ptr = ep;
462 while (*ptr == ' ')
463 ptr++;
464 }
465
466 return result;
467 }
468
469 static void
470 print_kmap(struct wskbd_map_data *map)
471 {
472 int i;
473 struct wscons_keymap *mp;
474
475 for (i = 0; i < map->maplen; i++) {
476 mp = map->map + i;
477
478 if (mp->command == KS_voidSymbol &&
479 mp->group1[0] == KS_voidSymbol &&
480 mp->group1[1] == KS_voidSymbol &&
481 mp->group2[0] == KS_voidSymbol &&
482 mp->group2[1] == KS_voidSymbol)
483 continue;
484 printf("\n");
485 printf("keycode %u =", i);
486 if (mp->command != KS_voidSymbol)
487 printf(" %s", ksym2name(mp->command));
488 printf(" %s", ksym2name(mp->group1[0]));
489 if (mp->group1[0] != mp->group1[1] ||
490 mp->group1[0] != mp->group2[0] ||
491 mp->group1[0] != mp->group2[1]) {
492 printf(" %s", ksym2name(mp->group1[1]));
493 if (mp->group1[0] != mp->group2[0] ||
494 mp->group1[1] != mp->group2[1]) {
495 printf(" %s", ksym2name(mp->group2[0]));
496 printf(" %s", ksym2name(mp->group2[1]));
497 }
498 }
499 }
500 }
501