1 /* $NetBSD: util.c,v 1.30 2011/12/15 14:25:12 phx 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_GBOX, "hp-gator" }, 122 { WSDISPLAY_TYPE_TOPCAT, "hp-topcat" }, 123 { WSDISPLAY_TYPE_RBOX, "hp-renaissance" }, 124 { WSDISPLAY_TYPE_CATSEYE, "hp-catseye" }, 125 { WSDISPLAY_TYPE_DVBOX, "hp-davinci" }, 126 { WSDISPLAY_TYPE_TVRX, "hp-tiger" }, 127 { WSDISPLAY_TYPE_HYPERION, "hp-hyperion" }, 128 { WSDISPLAY_TYPE_AMIGACC, "amiga-cc" }, 129 { WSDISPLAY_TYPE_GRF, "grf" }, 130 { WSDISPLAY_TYPE_SUN24, "sun24" }, 131 { WSDISPLAY_TYPE_NEWPORT, "sgi-newport" }, 132 { WSDISPLAY_TYPE_GR2, "sgi-gr2" }, 133 { WSDISPLAY_TYPE_SUNCG12, "suncg12" }, 134 { WSDISPLAY_TYPE_SUNCG14, "suncg14" }, 135 { WSDISPLAY_TYPE_SUNTCX, "suntcx" }, 136 { WSDISPLAY_TYPE_SUNFFB, "sunffb" }, 137 }; 138 139 static struct nameint kbdenc_tab[] = { 140 KB_ENCTAB 141 }; 142 143 static struct nameint kbdvar_tab[] = { 144 KB_VARTAB 145 }; 146 147 static struct nameint color_tab[] = { 148 { WSCOL_UNSUPPORTED, "unsupported" }, 149 { WSCOL_BLACK, "black" }, 150 { WSCOL_RED, "red" }, 151 { WSCOL_GREEN, "green" }, 152 { WSCOL_BROWN, "brown" }, 153 { WSCOL_BLUE, "blue" }, 154 { WSCOL_MAGENTA, "magenta" }, 155 { WSCOL_CYAN, "cyan" }, 156 { WSCOL_WHITE, "white" }, 157 }; 158 159 static struct nameint attr_tab[] = { 160 { WSATTR_NONE, "none" }, 161 { WSATTR_REVERSE, "reverse" }, 162 { WSATTR_HILIT, "hilit" }, 163 { WSATTR_BLINK, "blink" }, 164 { WSATTR_UNDERLINE, "underline" }, 165 { WSATTR_WSCOLORS, "color" }, 166 }; 167 168 static struct field *field_tab; 169 static int field_tab_len; 170 171 static const char *int2name(int, int, struct nameint *, int); 172 static int name2int(char *, struct nameint *, int); 173 static void print_kmap(struct wskbd_map_data *); 174 static unsigned int rd_bitfield(const char *); 175 static void pr_bitfield(unsigned int); 176 177 void 178 field_setup(struct field *ftab, int len) 179 { 180 181 field_tab = ftab; 182 field_tab_len = len; 183 } 184 185 struct field * 186 field_by_name(char *name) 187 { 188 int i; 189 190 for (i = 0; i < field_tab_len; i++) 191 if (strcmp(field_tab[i].name, name) == 0) 192 return field_tab + i; 193 194 errx(EXIT_FAILURE, "%s: not found", name); 195 } 196 197 struct field * 198 field_by_value(void *addr) 199 { 200 int i; 201 202 for (i = 0; i < field_tab_len; i++) 203 if (field_tab[i].valp == addr) 204 return field_tab + i; 205 206 errx(EXIT_FAILURE, "internal error: field_by_value: not found"); 207 } 208 209 void 210 field_disable_by_value(void *addr) 211 { 212 struct field *f; 213 214 f = field_by_value(addr); 215 f->flags |= FLG_DISABLED; 216 } 217 218 static const char * 219 int2name(int val, int uflag, struct nameint *tab, int len) 220 { 221 static char tmp[20]; 222 int i; 223 224 for (i = 0; i < len; i++) 225 if (tab[i].value == val) 226 return tab[i].name; 227 228 if (uflag) { 229 (void)snprintf(tmp, sizeof(tmp), "unknown_%d", val); 230 return tmp; 231 } else 232 return NULL; 233 } 234 235 static int 236 name2int(char *val, struct nameint *tab, int len) 237 { 238 int i; 239 240 for (i = 0; i < len; i++) 241 if (strcmp(tab[i].name, val) == 0) 242 return tab[i].value; 243 return -1; 244 } 245 246 void 247 pr_field(struct field *f, const char *sep) 248 { 249 const char *p; 250 unsigned int flags; 251 int first, i, mask; 252 253 if (sep) 254 (void)printf("%s%s", f->name, sep); 255 256 switch (f->format) { 257 case FMT_UINT: 258 (void)printf("%u", *((unsigned int *) f->valp)); 259 break; 260 case FMT_STRING: 261 (void)printf("\"%s\"", *((char **) f->valp)); 262 break; 263 case FMT_BITFIELD: 264 pr_bitfield(*((unsigned int *) f->valp)); 265 break; 266 case FMT_KBDTYPE: 267 p = int2name(*((unsigned int *) f->valp), 1, 268 kbtype_tab, TABLEN(kbtype_tab)); 269 (void)printf("%s", p); 270 break; 271 case FMT_MSTYPE: 272 p = int2name(*((unsigned int *) f->valp), 1, 273 mstype_tab, TABLEN(mstype_tab)); 274 (void)printf("%s", p); 275 break; 276 case FMT_DPYTYPE: 277 p = int2name(*((unsigned int *) f->valp), 1, 278 dpytype_tab, TABLEN(dpytype_tab)); 279 (void)printf("%s", p); 280 break; 281 case FMT_KBDENC: 282 p = int2name(KB_ENCODING(*((unsigned int *) f->valp)), 1, 283 kbdenc_tab, TABLEN(kbdenc_tab)); 284 (void)printf("%s", p); 285 286 flags = KB_VARIANT(*((unsigned int *) f->valp)); 287 for (i = 0; i < 32; i++) { 288 if (!(flags & (1 << i))) 289 continue; 290 p = int2name(flags & (1 << i), 1, 291 kbdvar_tab, TABLEN(kbdvar_tab)); 292 (void)printf(".%s", p); 293 } 294 break; 295 case FMT_KBMAP: 296 print_kmap((struct wskbd_map_data *) f->valp); 297 break; 298 case FMT_COLOR: 299 p = int2name(*((unsigned int *) f->valp), 1, 300 color_tab, TABLEN(color_tab)); 301 (void)printf("%s", p); 302 break; 303 case FMT_ATTRS: 304 mask = 0x10; 305 first = 1; 306 while (mask > 0) { 307 if (*((unsigned int *) f->valp) & mask) { 308 p = int2name(*((unsigned int *) f->valp) & mask, 309 1, attr_tab, TABLEN(attr_tab)); 310 (void)printf("%s%s", first ? "" : ",", p); 311 first = 0; 312 } 313 mask >>= 1; 314 } 315 if (first) 316 (void)printf("none"); 317 break; 318 default: 319 errx(EXIT_FAILURE, "internal error: pr_field: no format %d", 320 f->format); 321 break; 322 } 323 324 (void)printf("\n"); 325 } 326 327 static void 328 pr_bitfield(unsigned int f) 329 { 330 331 if (f == 0) 332 (void)printf("none"); 333 else { 334 unsigned int i; 335 int first, mask; 336 337 for (i = 0, first = 1, mask = 1; i < sizeof(f) * 8; i++) { 338 if (f & mask) { 339 (void)printf("%s%u", 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 unsigned 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(EXIT_FAILURE, "%s: not a number", val); 359 if (merge) 360 *((unsigned int *) f->valp) += u; 361 else 362 *((unsigned int *) f->valp) = u; 363 break; 364 case FMT_STRING: 365 if ((*((char **) f->valp) = strdup(val)) == NULL) 366 err(EXIT_FAILURE, "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(EXIT_FAILURE, "%s: not a valid encoding", val); 379 *((unsigned 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(EXIT_FAILURE, "%s: not a valid variant", 389 val); 390 *((unsigned int *) f->valp) |= i; 391 } 392 break; 393 case FMT_KBMAP: 394 if (! merge) 395 kbmap.maplen = 0; 396 map_scan_setinput(val); 397 yyparse(); 398 if (merge) { 399 if (newkbmap.maplen < kbmap.maplen) 400 newkbmap.maplen = kbmap.maplen; 401 for (u = 0; u < kbmap.maplen; u++) { 402 mp = newkbmap.map + u; 403 if (mp->command == KS_voidSymbol && 404 mp->group1[0] == KS_voidSymbol && 405 mp->group1[1] == KS_voidSymbol && 406 mp->group2[0] == KS_voidSymbol && 407 mp->group2[1] == KS_voidSymbol) 408 *mp = kbmap.map[u]; 409 } 410 } 411 kbmap.maplen = newkbmap.maplen; 412 bcopy(newkbmap.map, kbmap.map, 413 kbmap.maplen * sizeof(struct wscons_keymap)); 414 break; 415 case FMT_COLOR: 416 i = name2int(val, color_tab, TABLEN(color_tab)); 417 if (i == -1) 418 errx(EXIT_FAILURE, "%s: not a valid color", val); 419 *((unsigned int *) f->valp) = i; 420 break; 421 case FMT_ATTRS: 422 p = val; 423 while (p) { 424 val = p; 425 p = strchr(p, ','); 426 if (p != NULL) 427 *p++ = '\0'; 428 i = name2int(val, attr_tab, TABLEN(attr_tab)); 429 if (i == -1) 430 errx(EXIT_FAILURE, "%s: not a valid attribute", 431 val); 432 *((unsigned int *) f->valp) |= i; 433 } 434 break; 435 default: 436 errx(EXIT_FAILURE, "internal error: rd_field: no format %d", 437 f->format); 438 break; 439 } 440 } 441 442 static unsigned int 443 rd_bitfield(const char *str) 444 { 445 const char *ptr; 446 char *ep; 447 long lval; 448 unsigned int result; 449 450 ep = NULL; 451 ptr = str; 452 result = 0; 453 while (*ptr != '\0') { 454 errno = 0; 455 lval = strtol(ptr, &ep, 10); 456 if (*ep != '\0' && *ep != ' ') 457 errx(EXIT_FAILURE, "%s: not a valid number list", str); 458 if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN)) 459 errx(EXIT_FAILURE, "%s: not a valid number list", str); 460 if (lval >= (long)sizeof(result) * 8) 461 errx(EXIT_FAILURE, "%ld: number out of range", lval); 462 result |= (1 << lval); 463 464 ptr = ep; 465 while (*ptr == ' ') 466 ptr++; 467 } 468 469 return result; 470 } 471 472 static void 473 print_kmap(struct wskbd_map_data *map) 474 { 475 unsigned int i; 476 struct wscons_keymap *mp; 477 478 for (i = 0; i < map->maplen; i++) { 479 mp = map->map + i; 480 481 if (mp->command == KS_voidSymbol && 482 mp->group1[0] == KS_voidSymbol && 483 mp->group1[1] == KS_voidSymbol && 484 mp->group2[0] == KS_voidSymbol && 485 mp->group2[1] == KS_voidSymbol) 486 continue; 487 (void)printf("\n"); 488 (void)printf("keycode %u =", i); 489 if (mp->command != KS_voidSymbol) 490 (void)printf(" %s", ksym2name(mp->command)); 491 (void)printf(" %s", ksym2name(mp->group1[0])); 492 if (mp->group1[0] != mp->group1[1] || 493 mp->group1[0] != mp->group2[0] || 494 mp->group1[0] != mp->group2[1]) { 495 (void)printf(" %s", ksym2name(mp->group1[1])); 496 if (mp->group1[0] != mp->group2[0] || 497 mp->group1[1] != mp->group2[1]) { 498 (void)printf(" %s", ksym2name(mp->group2[0])); 499 (void)printf(" %s", ksym2name(mp->group2[1])); 500 } 501 } 502 } 503 } 504