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