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