kbdcontrol.c revision 267540
1/*- 2 * Copyright (c) 1994-1995 S��ren Schmidt 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer, 10 * in this position and unchanged. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. The name of the author may not be used to endorse or promote products 15 * derived from this software without specific prior written permission 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29#include <sys/cdefs.h> 30__FBSDID("$FreeBSD: stable/10/usr.sbin/kbdcontrol/kbdcontrol.c 267540 2014-06-16 11:51:00Z ray $"); 31 32#include <ctype.h> 33#include <err.h> 34#include <stdio.h> 35#include <stdlib.h> 36#include <string.h> 37#include <unistd.h> 38#include <fcntl.h> 39#include <sys/kbio.h> 40#include <sys/consio.h> 41#include <sys/sysctl.h> 42#include "path.h" 43#include "lex.h" 44 45/* 46 * HALT, PDWN, and PASTE aren't defined in 4.x, but we need them to bridge 47 * to 5.0-current so define them here as a stop gap transition measure. 48 */ 49#ifndef HALT 50#define HALT 0xa1 /* halt machine */ 51#endif 52#ifndef PDWN 53#define PDWN 0xa2 /* halt machine and power down */ 54#endif 55#ifndef PASTE 56#define PASTE 0xa3 /* paste from cut-paste buffer */ 57#endif 58 59#define SPECIAL 0x80000000 60 61static const char ctrl_names[32][4] = { 62 "nul", "soh", "stx", "etx", "eot", "enq", "ack", "bel", 63 "bs ", "ht ", "nl ", "vt ", "ff ", "cr ", "so ", "si ", 64 "dle", "dc1", "dc2", "dc3", "dc4", "nak", "syn", "etb", 65 "can", "em ", "sub", "esc", "fs ", "gs ", "rs ", "us " 66 }; 67 68static const char acc_names[15][5] = { 69 "dgra", "dacu", "dcir", "dtil", "dmac", "dbre", "ddot", 70 "duml", "dsla", "drin", "dced", "dapo", "ddac", "dogo", 71 "dcar", 72 }; 73 74static const char acc_names_u[15][5] = { 75 "DGRA", "DACU", "DCIR", "DTIL", "DMAC", "DBRE", "DDOT", 76 "DUML", "DSLA", "DRIN", "DCED", "DAPO", "DDAC", "DOGO", 77 "DCAR", 78 }; 79 80static const char fkey_table[96][MAXFK] = { 81/* 01-04 */ "\033[M", "\033[N", "\033[O", "\033[P", 82/* 05-08 */ "\033[Q", "\033[R", "\033[S", "\033[T", 83/* 09-12 */ "\033[U", "\033[V", "\033[W", "\033[X", 84/* 13-16 */ "\033[Y", "\033[Z", "\033[a", "\033[b", 85/* 17-20 */ "\033[c", "\033[d", "\033[e", "\033[f", 86/* 21-24 */ "\033[g", "\033[h", "\033[i", "\033[j", 87/* 25-28 */ "\033[k", "\033[l", "\033[m", "\033[n", 88/* 29-32 */ "\033[o", "\033[p", "\033[q", "\033[r", 89/* 33-36 */ "\033[s", "\033[t", "\033[u", "\033[v", 90/* 37-40 */ "\033[w", "\033[x", "\033[y", "\033[z", 91/* 41-44 */ "\033[@", "\033[[", "\033[\\","\033[]", 92/* 45-48 */ "\033[^", "\033[_", "\033[`", "\033[{", 93/* 49-52 */ "\033[H", "\033[A", "\033[I", "-" , 94/* 53-56 */ "\033[D", "\033[E", "\033[C", "+" , 95/* 57-60 */ "\033[F", "\033[B", "\033[G", "\033[L", 96/* 61-64 */ "\177", "\033[J", "\033[~", "\033[}", 97/* 65-68 */ "" , "" , "" , "" , 98/* 69-72 */ "" , "" , "" , "" , 99/* 73-76 */ "" , "" , "" , "" , 100/* 77-80 */ "" , "" , "" , "" , 101/* 81-84 */ "" , "" , "" , "" , 102/* 85-88 */ "" , "" , "" , "" , 103/* 89-92 */ "" , "" , "" , "" , 104/* 93-96 */ "" , "" , "" , "" , 105 }; 106 107static const int delays[] = {250, 500, 750, 1000}; 108static const int repeats[] = { 34, 38, 42, 46, 50, 55, 59, 63, 109 68, 76, 84, 92, 100, 110, 118, 126, 110 136, 152, 168, 184, 200, 220, 236, 252, 111 272, 304, 336, 368, 400, 440, 472, 504}; 112static const int ndelays = (sizeof(delays) / sizeof(int)); 113static const int nrepeats = (sizeof(repeats) / sizeof(int)); 114static int hex = 0; 115static int token; 116 117int number; 118char letter; 119 120static void dump_accent_definition(char *name, accentmap_t *accentmap); 121static void dump_entry(int value); 122static void dump_key_definition(char *name, keymap_t *keymap); 123static int get_accent_definition_line(accentmap_t *); 124static int get_entry(void); 125static int get_key_definition_line(keymap_t *); 126static void load_keymap(char *opt, int dumponly); 127static void load_default_functionkeys(void); 128static char * nextarg(int ac, char **av, int *indp, int oc); 129static char * mkfullname(const char *s1, const char *s2, const char *s3); 130static void print_accent_definition_line(FILE *fp, int accent, 131 struct acc_t *key); 132static void print_entry(FILE *fp, int value); 133static void print_key_definition_line(FILE *fp, int scancode, 134 struct keyent_t *key); 135static void print_keymap(void); 136static void release_keyboard(void); 137static void mux_keyboard(u_int op, char *kbd); 138static void set_bell_values(char *opt); 139static void set_functionkey(char *keynumstr, char *string); 140static void set_keyboard(char *device); 141static void set_keyrates(char *opt); 142static void show_kbd_info(void); 143static void usage(void) __dead2; 144 145/* Detect presence of vt(4). */ 146static int 147is_vt4(void) 148{ 149 150 if (sysctlbyname("kern.vt.deadtimer", NULL, NULL, NULL, 0) == 0) 151 return (1); 152 153 return (0); 154} 155 156static char * 157nextarg(int ac, char **av, int *indp, int oc) 158{ 159 if (*indp < ac) 160 return(av[(*indp)++]); 161 warnx("option requires two arguments -- %c", oc); 162 usage(); 163} 164 165 166static char * 167mkfullname(const char *s1, const char *s2, const char *s3) 168{ 169 static char *buf = NULL; 170 static int bufl = 0; 171 int f; 172 173 f = strlen(s1) + strlen(s2) + strlen(s3) + 1; 174 if (f > bufl) { 175 if (buf) 176 buf = (char *)realloc(buf, f); 177 else 178 buf = (char *)malloc(f); 179 } 180 if (!buf) { 181 bufl = 0; 182 return(NULL); 183 } 184 185 bufl = f; 186 strcpy(buf, s1); 187 strcat(buf, s2); 188 strcat(buf, s3); 189 return(buf); 190} 191 192 193static int 194get_entry(void) 195{ 196 switch ((token = yylex())) { 197 case TNOP: 198 return NOP | SPECIAL; 199 case TLSH: 200 return LSH | SPECIAL; 201 case TRSH: 202 return RSH | SPECIAL; 203 case TCLK: 204 return CLK | SPECIAL; 205 case TNLK: 206 return NLK | SPECIAL; 207 case TSLK: 208 return SLK | SPECIAL; 209 case TBTAB: 210 return BTAB | SPECIAL; 211 case TLALT: 212 return LALT | SPECIAL; 213 case TLCTR: 214 return LCTR | SPECIAL; 215 case TNEXT: 216 return NEXT | SPECIAL; 217 case TPREV: 218 return PREV | SPECIAL; 219 case TRCTR: 220 return RCTR | SPECIAL; 221 case TRALT: 222 return RALT | SPECIAL; 223 case TALK: 224 return ALK | SPECIAL; 225 case TASH: 226 return ASH | SPECIAL; 227 case TMETA: 228 return META | SPECIAL; 229 case TRBT: 230 return RBT | SPECIAL; 231 case TDBG: 232 return DBG | SPECIAL; 233 case TSUSP: 234 return SUSP | SPECIAL; 235 case TSPSC: 236 return SPSC | SPECIAL; 237 case TPANIC: 238 return PNC | SPECIAL; 239 case TLSHA: 240 return LSHA | SPECIAL; 241 case TRSHA: 242 return RSHA | SPECIAL; 243 case TLCTRA: 244 return LCTRA | SPECIAL; 245 case TRCTRA: 246 return RCTRA | SPECIAL; 247 case TLALTA: 248 return LALTA | SPECIAL; 249 case TRALTA: 250 return RALTA | SPECIAL; 251 case THALT: 252 return HALT | SPECIAL; 253 case TPDWN: 254 return PDWN | SPECIAL; 255 case TPASTE: 256 return PASTE | SPECIAL; 257 case TACC: 258 if (ACC(number) > L_ACC) 259 return -1; 260 return ACC(number) | SPECIAL; 261 case TFUNC: 262 if (F(number) > L_FN) 263 return -1; 264 return F(number) | SPECIAL; 265 case TSCRN: 266 if (S(number) > L_SCR) 267 return -1; 268 return S(number) | SPECIAL; 269 case TLET: 270 return (unsigned char)letter; 271 case TNUM: 272 if (number < 0x000000 || number > 0x10FFFF) 273 return -1; 274 return number; 275 default: 276 return -1; 277 } 278} 279 280static int 281get_definition_line(FILE *fd, keymap_t *keymap, accentmap_t *accentmap) 282{ 283 int c; 284 285 yyin = fd; 286 287 if (token < 0) 288 token = yylex(); 289 switch (token) { 290 case TNUM: 291 c = get_key_definition_line(keymap); 292 if (c < 0) 293 errx(1, "invalid key definition"); 294 if (c > keymap->n_keys) 295 keymap->n_keys = c; 296 break; 297 case TACC: 298 c = get_accent_definition_line(accentmap); 299 if (c < 0) 300 errx(1, "invalid accent key definition"); 301 if (c > accentmap->n_accs) 302 accentmap->n_accs = c; 303 break; 304 case 0: 305 /* EOF */ 306 return -1; 307 default: 308 errx(1, "illegal definition line"); 309 } 310 return c; 311} 312 313static int 314get_key_definition_line(keymap_t *map) 315{ 316 int i, def, scancode; 317 318 /* check scancode number */ 319 if (number < 0 || number >= NUM_KEYS) 320 return -1; 321 scancode = number; 322 323 /* get key definitions */ 324 map->key[scancode].spcl = 0; 325 for (i=0; i<NUM_STATES; i++) { 326 if ((def = get_entry()) == -1) 327 return -1; 328 if (def & SPECIAL) 329 map->key[scancode].spcl |= (0x80 >> i); 330 map->key[scancode].map[i] = def & ~SPECIAL; 331 } 332 /* get lock state key def */ 333 if ((token = yylex()) != TFLAG) 334 return -1; 335 map->key[scancode].flgs = number; 336 token = yylex(); 337 return (scancode + 1); 338} 339 340static int 341get_accent_definition_line(accentmap_t *map) 342{ 343 int accent; 344 int c1, c2; 345 int i; 346 347 if (ACC(number) < F_ACC || ACC(number) > L_ACC) 348 /* number out of range */ 349 return -1; 350 accent = number; 351 if (map->acc[accent].accchar != 0) { 352 /* this entry has already been defined before! */ 353 errx(1, "duplicated accent key definition"); 354 } 355 356 switch ((token = yylex())) { 357 case TLET: 358 map->acc[accent].accchar = letter; 359 break; 360 case TNUM: 361 map->acc[accent].accchar = number; 362 break; 363 default: 364 return -1; 365 } 366 367 for (i = 0; (token = yylex()) == '(';) { 368 switch ((token = yylex())) { 369 case TLET: 370 c1 = letter; 371 break; 372 case TNUM: 373 c1 = number; 374 break; 375 default: 376 return -1; 377 } 378 switch ((token = yylex())) { 379 case TLET: 380 c2 = letter; 381 break; 382 case TNUM: 383 c2 = number; 384 break; 385 default: 386 return -1; 387 } 388 if ((token = yylex()) != ')') 389 return -1; 390 if (i >= NUM_ACCENTCHARS) { 391 warnx("too many accented characters, ignored"); 392 continue; 393 } 394 map->acc[accent].map[i][0] = c1; 395 map->acc[accent].map[i][1] = c2; 396 ++i; 397 } 398 return (accent + 1); 399} 400 401static void 402print_entry(FILE *fp, int value) 403{ 404 int val = value & ~SPECIAL; 405 406 switch (value) { 407 case NOP | SPECIAL: 408 fprintf(fp, " nop "); 409 break; 410 case LSH | SPECIAL: 411 fprintf(fp, " lshift"); 412 break; 413 case RSH | SPECIAL: 414 fprintf(fp, " rshift"); 415 break; 416 case CLK | SPECIAL: 417 fprintf(fp, " clock "); 418 break; 419 case NLK | SPECIAL: 420 fprintf(fp, " nlock "); 421 break; 422 case SLK | SPECIAL: 423 fprintf(fp, " slock "); 424 break; 425 case BTAB | SPECIAL: 426 fprintf(fp, " btab "); 427 break; 428 case LALT | SPECIAL: 429 fprintf(fp, " lalt "); 430 break; 431 case LCTR | SPECIAL: 432 fprintf(fp, " lctrl "); 433 break; 434 case NEXT | SPECIAL: 435 fprintf(fp, " nscr "); 436 break; 437 case PREV | SPECIAL: 438 fprintf(fp, " pscr "); 439 break; 440 case RCTR | SPECIAL: 441 fprintf(fp, " rctrl "); 442 break; 443 case RALT | SPECIAL: 444 fprintf(fp, " ralt "); 445 break; 446 case ALK | SPECIAL: 447 fprintf(fp, " alock "); 448 break; 449 case ASH | SPECIAL: 450 fprintf(fp, " ashift"); 451 break; 452 case META | SPECIAL: 453 fprintf(fp, " meta "); 454 break; 455 case RBT | SPECIAL: 456 fprintf(fp, " boot "); 457 break; 458 case DBG | SPECIAL: 459 fprintf(fp, " debug "); 460 break; 461 case SUSP | SPECIAL: 462 fprintf(fp, " susp "); 463 break; 464 case SPSC | SPECIAL: 465 fprintf(fp, " saver "); 466 break; 467 case PNC | SPECIAL: 468 fprintf(fp, " panic "); 469 break; 470 case LSHA | SPECIAL: 471 fprintf(fp, " lshifta"); 472 break; 473 case RSHA | SPECIAL: 474 fprintf(fp, " rshifta"); 475 break; 476 case LCTRA | SPECIAL: 477 fprintf(fp, " lctrla"); 478 break; 479 case RCTRA | SPECIAL: 480 fprintf(fp, " rctrla"); 481 break; 482 case LALTA | SPECIAL: 483 fprintf(fp, " lalta "); 484 break; 485 case RALTA | SPECIAL: 486 fprintf(fp, " ralta "); 487 break; 488 case HALT | SPECIAL: 489 fprintf(fp, " halt "); 490 break; 491 case PDWN | SPECIAL: 492 fprintf(fp, " pdwn "); 493 break; 494 case PASTE | SPECIAL: 495 fprintf(fp, " paste "); 496 break; 497 default: 498 if (value & SPECIAL) { 499 if (val >= F_FN && val <= L_FN) 500 fprintf(fp, " fkey%02d", val - F_FN + 1); 501 else if (val >= F_SCR && val <= L_SCR) 502 fprintf(fp, " scr%02d ", val - F_SCR + 1); 503 else if (val >= F_ACC && val <= L_ACC) 504 fprintf(fp, " %-6s", acc_names[val - F_ACC]); 505 else if (hex) 506 fprintf(fp, " 0x%02x ", val); 507 else 508 fprintf(fp, " %3d ", val); 509 } 510 else { 511 if (val < ' ') 512 fprintf(fp, " %s ", ctrl_names[val]); 513 else if (val == 127) 514 fprintf(fp, " del "); 515 else if (isascii(val) && isprint(val)) 516 fprintf(fp, " '%c' ", val); 517 else if (hex) 518 fprintf(fp, " 0x%02x ", val); 519 else 520 fprintf(fp, " %3d ", val); 521 } 522 } 523} 524 525static void 526print_key_definition_line(FILE *fp, int scancode, struct keyent_t *key) 527{ 528 int i; 529 530 /* print scancode number */ 531 if (hex) 532 fprintf(fp, " 0x%02x ", scancode); 533 else 534 fprintf(fp, " %03d ", scancode); 535 536 /* print key definitions */ 537 for (i=0; i<NUM_STATES; i++) { 538 if (key->spcl & (0x80 >> i)) 539 print_entry(fp, key->map[i] | SPECIAL); 540 else 541 print_entry(fp, key->map[i]); 542 } 543 544 /* print lock state key def */ 545 switch (key->flgs) { 546 case 0: 547 fprintf(fp, " O\n"); 548 break; 549 case 1: 550 fprintf(fp, " C\n"); 551 break; 552 case 2: 553 fprintf(fp, " N\n"); 554 break; 555 case 3: 556 fprintf(fp, " B\n"); 557 break; 558 } 559} 560 561static void 562print_accent_definition_line(FILE *fp, int accent, struct acc_t *key) 563{ 564 int c; 565 int i; 566 567 if (key->accchar == 0) 568 return; 569 570 /* print accent number */ 571 fprintf(fp, " %-6s", acc_names[accent]); 572 if (isascii(key->accchar) && isprint(key->accchar)) 573 fprintf(fp, "'%c' ", key->accchar); 574 else if (hex) 575 fprintf(fp, "0x%02x ", key->accchar); 576 else 577 fprintf(fp, "%03d ", key->accchar); 578 579 for (i = 0; i < NUM_ACCENTCHARS; ++i) { 580 c = key->map[i][0]; 581 if (c == 0) 582 break; 583 if ((i > 0) && ((i % 4) == 0)) 584 fprintf(fp, "\n "); 585 if (isascii(c) && isprint(c)) 586 fprintf(fp, "( '%c' ", c); 587 else if (hex) 588 fprintf(fp, "(0x%02x ", c); 589 else 590 fprintf(fp, "( %03d ", c); 591 c = key->map[i][1]; 592 if (isascii(c) && isprint(c)) 593 fprintf(fp, "'%c' ) ", c); 594 else if (hex) 595 fprintf(fp, "0x%02x) ", c); 596 else 597 fprintf(fp, "%03d ) ", c); 598 } 599 fprintf(fp, "\n"); 600} 601 602static void 603dump_entry(int value) 604{ 605 if (value & SPECIAL) { 606 value &= ~SPECIAL; 607 switch (value) { 608 case NOP: 609 printf(" NOP, "); 610 break; 611 case LSH: 612 printf(" LSH, "); 613 break; 614 case RSH: 615 printf(" RSH, "); 616 break; 617 case CLK: 618 printf(" CLK, "); 619 break; 620 case NLK: 621 printf(" NLK, "); 622 break; 623 case SLK: 624 printf(" SLK, "); 625 break; 626 case BTAB: 627 printf(" BTAB, "); 628 break; 629 case LALT: 630 printf(" LALT, "); 631 break; 632 case LCTR: 633 printf(" LCTR, "); 634 break; 635 case NEXT: 636 printf(" NEXT, "); 637 break; 638 case PREV: 639 printf(" PREV, "); 640 break; 641 case RCTR: 642 printf(" RCTR, "); 643 break; 644 case RALT: 645 printf(" RALT, "); 646 break; 647 case ALK: 648 printf(" ALK, "); 649 break; 650 case ASH: 651 printf(" ASH, "); 652 break; 653 case META: 654 printf(" META, "); 655 break; 656 case RBT: 657 printf(" RBT, "); 658 break; 659 case DBG: 660 printf(" DBG, "); 661 break; 662 case SUSP: 663 printf(" SUSP, "); 664 break; 665 case SPSC: 666 printf(" SPSC, "); 667 break; 668 case PNC: 669 printf(" PNC, "); 670 break; 671 case LSHA: 672 printf(" LSHA, "); 673 break; 674 case RSHA: 675 printf(" RSHA, "); 676 break; 677 case LCTRA: 678 printf("LCTRA, "); 679 break; 680 case RCTRA: 681 printf("RCTRA, "); 682 break; 683 case LALTA: 684 printf("LALTA, "); 685 break; 686 case RALTA: 687 printf("RALTA, "); 688 break; 689 case HALT: 690 printf(" HALT, "); 691 break; 692 case PDWN: 693 printf(" PDWN, "); 694 break; 695 case PASTE: 696 printf("PASTE, "); 697 break; 698 default: 699 if (value >= F_FN && value <= L_FN) 700 printf(" F(%2d),", value - F_FN + 1); 701 else if (value >= F_SCR && value <= L_SCR) 702 printf(" S(%2d),", value - F_SCR + 1); 703 else if (value >= F_ACC && value <= L_ACC) 704 printf(" %-4s, ", acc_names_u[value - F_ACC]); 705 else 706 printf(" 0x%02X, ", value); 707 break; 708 } 709 } else if (value == '\'') { 710 printf(" '\\'', "); 711 } else if (value == '\\') { 712 printf(" '\\\\', "); 713 } else if (isascii(value) && isprint(value)) { 714 printf(" '%c', ", value); 715 } else { 716 printf(" 0x%02X, ", value); 717 } 718} 719 720static void 721dump_key_definition(char *name, keymap_t *keymap) 722{ 723 int i, j; 724 725 printf("static keymap_t keymap_%s = { 0x%02x, {\n", 726 name, (unsigned)keymap->n_keys); 727 printf( 728"/* alt\n" 729" * scan cntrl alt alt cntrl\n" 730" * code base shift cntrl shift alt shift cntrl shift spcl flgs\n" 731" * ---------------------------------------------------------------------------\n" 732" */\n"); 733 for (i = 0; i < keymap->n_keys; i++) { 734 printf("/*%02x*/{{", i); 735 for (j = 0; j < NUM_STATES; j++) { 736 if (keymap->key[i].spcl & (0x80 >> j)) 737 dump_entry(keymap->key[i].map[j] | SPECIAL); 738 else 739 dump_entry(keymap->key[i].map[j]); 740 } 741 printf("}, 0x%02X,0x%02X },\n", 742 (unsigned)keymap->key[i].spcl, 743 (unsigned)keymap->key[i].flgs); 744 } 745 printf("} };\n\n"); 746} 747 748static void 749dump_accent_definition(char *name, accentmap_t *accentmap) 750{ 751 int i, j; 752 int c; 753 754 printf("static accentmap_t accentmap_%s = { %d", 755 name, accentmap->n_accs); 756 if (accentmap->n_accs <= 0) { 757 printf(" };\n\n"); 758 return; 759 } 760 printf(", {\n"); 761 for (i = 0; i < NUM_DEADKEYS; i++) { 762 printf(" /* %s=%d */\n {", acc_names[i], i); 763 c = accentmap->acc[i].accchar; 764 if (c == '\'') 765 printf(" '\\'', {"); 766 else if (c == '\\') 767 printf(" '\\\\', {"); 768 else if (isascii(c) && isprint(c)) 769 printf(" '%c', {", c); 770 else if (c == 0) { 771 printf(" 0x00 }, \n"); 772 continue; 773 } else 774 printf(" 0x%02x, {", c); 775 for (j = 0; j < NUM_ACCENTCHARS; j++) { 776 c = accentmap->acc[i].map[j][0]; 777 if (c == 0) 778 break; 779 if ((j > 0) && ((j % 4) == 0)) 780 printf("\n\t "); 781 if (isascii(c) && isprint(c)) 782 printf(" { '%c',", c); 783 else 784 printf(" { 0x%02x,", c); 785 printf("0x%02x },", accentmap->acc[i].map[j][1]); 786 } 787 printf(" }, },\n"); 788 } 789 printf("} };\n\n"); 790} 791 792static void 793load_keymap(char *opt, int dumponly) 794{ 795 keymap_t keymap; 796 accentmap_t accentmap; 797 FILE *fd; 798 int i, j; 799 char *name, *cp; 800 char blank[] = "", keymap_path[] = KEYMAP_PATH; 801 char vt_keymap_path[] = VT_KEYMAP_PATH, dotkbd[] = ".kbd"; 802 char *prefix[] = {blank, blank, keymap_path, NULL}; 803 char *postfix[] = {blank, dotkbd, NULL}; 804 805 if (is_vt4()) 806 prefix[2] = vt_keymap_path; 807 cp = getenv("KEYMAP_PATH"); 808 if (cp != NULL) 809 asprintf(&(prefix[0]), "%s/", cp); 810 811 fd = NULL; 812 for (i=0; prefix[i] && fd == NULL; i++) { 813 for (j=0; postfix[j] && fd == NULL; j++) { 814 name = mkfullname(prefix[i], opt, postfix[j]); 815 fd = fopen(name, "r"); 816 } 817 } 818 if (fd == NULL) { 819 warn("keymap file \"%s\" not found", opt); 820 return; 821 } 822 memset(&keymap, 0, sizeof(keymap)); 823 memset(&accentmap, 0, sizeof(accentmap)); 824 token = -1; 825 while (1) { 826 if (get_definition_line(fd, &keymap, &accentmap) < 0) 827 break; 828 } 829 if (dumponly) { 830 /* fix up the filename to make it a valid C identifier */ 831 for (cp = opt; *cp; cp++) 832 if (!isalpha(*cp) && !isdigit(*cp)) *cp = '_'; 833 printf("/*\n" 834 " * Automatically generated from %s.\n" 835 " * DO NOT EDIT!\n" 836 " */\n", name); 837 dump_key_definition(opt, &keymap); 838 dump_accent_definition(opt, &accentmap); 839 return; 840 } 841 if ((keymap.n_keys > 0) && (ioctl(0, PIO_KEYMAP, &keymap) < 0)) { 842 warn("setting keymap"); 843 fclose(fd); 844 return; 845 } 846 if ((accentmap.n_accs > 0) 847 && (ioctl(0, PIO_DEADKEYMAP, &accentmap) < 0)) { 848 warn("setting accentmap"); 849 fclose(fd); 850 return; 851 } 852} 853 854static void 855print_keymap(void) 856{ 857 keymap_t keymap; 858 accentmap_t accentmap; 859 int i; 860 861 if (ioctl(0, GIO_KEYMAP, &keymap) < 0) 862 err(1, "getting keymap"); 863 if (ioctl(0, GIO_DEADKEYMAP, &accentmap) < 0) 864 memset(&accentmap, 0, sizeof(accentmap)); 865 printf( 866"# alt\n" 867"# scan cntrl alt alt cntrl lock\n" 868"# code base shift cntrl shift alt shift cntrl shift state\n" 869"# ------------------------------------------------------------------\n" 870 ); 871 for (i=0; i<keymap.n_keys; i++) 872 print_key_definition_line(stdout, i, &keymap.key[i]); 873 874 printf("\n"); 875 for (i = 0; i < NUM_DEADKEYS; i++) 876 print_accent_definition_line(stdout, i, &accentmap.acc[i]); 877 878} 879 880static void 881load_default_functionkeys(void) 882{ 883 fkeyarg_t fkey; 884 int i; 885 886 for (i=0; i<NUM_FKEYS; i++) { 887 fkey.keynum = i; 888 strcpy(fkey.keydef, fkey_table[i]); 889 fkey.flen = strlen(fkey_table[i]); 890 if (ioctl(0, SETFKEY, &fkey) < 0) 891 warn("setting function key"); 892 } 893} 894 895static void 896set_functionkey(char *keynumstr, char *string) 897{ 898 fkeyarg_t fkey; 899 900 if (!strcmp(keynumstr, "load") && !strcmp(string, "default")) { 901 load_default_functionkeys(); 902 return; 903 } 904 fkey.keynum = atoi(keynumstr); 905 if (fkey.keynum < 1 || fkey.keynum > NUM_FKEYS) { 906 warnx("function key number must be between 1 and %d", 907 NUM_FKEYS); 908 return; 909 } 910 if ((fkey.flen = strlen(string)) > MAXFK) { 911 warnx("function key string too long (%d > %d)", 912 fkey.flen, MAXFK); 913 return; 914 } 915 strncpy(fkey.keydef, string, MAXFK); 916 fkey.keynum -= 1; 917 if (ioctl(0, SETFKEY, &fkey) < 0) 918 warn("setting function key"); 919} 920 921static void 922set_bell_values(char *opt) 923{ 924 int bell, duration, pitch; 925 926 bell = 0; 927 if (!strncmp(opt, "quiet.", 6)) { 928 bell = CONS_QUIET_BELL; 929 opt += 6; 930 } 931 if (!strcmp(opt, "visual")) 932 bell |= CONS_VISUAL_BELL; 933 else if (!strcmp(opt, "normal")) 934 duration = 5, pitch = 800; 935 else if (!strcmp(opt, "off")) 936 duration = 0, pitch = 0; 937 else { 938 char *v1; 939 940 bell = 0; 941 duration = strtol(opt, &v1, 0); 942 if ((duration < 0) || (*v1 != '.')) 943 goto badopt; 944 opt = ++v1; 945 pitch = strtol(opt, &v1, 0); 946 if ((pitch < 0) || (*opt == '\0') || (*v1 != '\0')) { 947badopt: 948 warnx("argument to -b must be duration.pitch or [quiet.]visual|normal|off"); 949 return; 950 } 951 if (pitch != 0) 952 pitch = 1193182 / pitch; /* in Hz */ 953 duration /= 10; /* in 10 m sec */ 954 } 955 956 ioctl(0, CONS_BELLTYPE, &bell); 957 if (!(bell & CONS_VISUAL_BELL)) 958 fprintf(stderr, "[=%d;%dB", pitch, duration); 959} 960 961static void 962set_keyrates(char *opt) 963{ 964 int arg[2]; 965 int repeat; 966 int delay; 967 int r, d; 968 969 if (!strcmp(opt, "slow")) { 970 delay = 1000, repeat = 500; 971 d = 3, r = 31; 972 } else if (!strcmp(opt, "normal")) { 973 delay = 500, repeat = 125; 974 d = 1, r = 15; 975 } else if (!strcmp(opt, "fast")) { 976 delay = repeat = 0; 977 d = r = 0; 978 } else { 979 int n; 980 char *v1; 981 982 delay = strtol(opt, &v1, 0); 983 if ((delay < 0) || (*v1 != '.')) 984 goto badopt; 985 opt = ++v1; 986 repeat = strtol(opt, &v1, 0); 987 if ((repeat < 0) || (*opt == '\0') || (*v1 != '\0')) { 988badopt: 989 warnx("argument to -r must be delay.repeat or slow|normal|fast"); 990 return; 991 } 992 for (n = 0; n < ndelays - 1; n++) 993 if (delay <= delays[n]) 994 break; 995 d = n; 996 for (n = 0; n < nrepeats - 1; n++) 997 if (repeat <= repeats[n]) 998 break; 999 r = n; 1000 } 1001 1002 arg[0] = delay; 1003 arg[1] = repeat; 1004 if (ioctl(0, KDSETREPEAT, arg)) { 1005 if (ioctl(0, KDSETRAD, (d << 5) | r)) 1006 warn("setting keyboard rate"); 1007 } 1008} 1009 1010static const char * 1011get_kbd_type_name(int type) 1012{ 1013 static struct { 1014 int type; 1015 const char *name; 1016 } name_table[] = { 1017 { KB_84, "AT 84" }, 1018 { KB_101, "AT 101/102" }, 1019 { KB_OTHER, "generic" }, 1020 }; 1021 unsigned int i; 1022 1023 for (i = 0; i < sizeof(name_table)/sizeof(name_table[0]); ++i) { 1024 if (type == name_table[i].type) 1025 return name_table[i].name; 1026 } 1027 return "unknown"; 1028} 1029 1030static void 1031show_kbd_info(void) 1032{ 1033 keyboard_info_t info; 1034 1035 if (ioctl(0, KDGKBINFO, &info) == -1) { 1036 warn("unable to obtain keyboard information"); 1037 return; 1038 } 1039 printf("kbd%d:\n", info.kb_index); 1040 printf(" %.*s%d, type:%s (%d)\n", 1041 (int)sizeof(info.kb_name), info.kb_name, info.kb_unit, 1042 get_kbd_type_name(info.kb_type), info.kb_type); 1043} 1044 1045static void 1046set_keyboard(char *device) 1047{ 1048 keyboard_info_t info; 1049 int fd; 1050 1051 fd = open(device, O_RDONLY); 1052 if (fd < 0) { 1053 warn("cannot open %s", device); 1054 return; 1055 } 1056 if (ioctl(fd, KDGKBINFO, &info) == -1) { 1057 warn("unable to obtain keyboard information"); 1058 close(fd); 1059 return; 1060 } 1061 /* 1062 * The keyboard device driver won't release the keyboard by 1063 * the following ioctl, but it automatically will, when the device 1064 * is closed. So, we don't check error here. 1065 */ 1066 ioctl(fd, CONS_RELKBD, 0); 1067 close(fd); 1068#if 1 1069 printf("kbd%d\n", info.kb_index); 1070 printf(" %.*s%d, type:%s (%d)\n", 1071 (int)sizeof(info.kb_name), info.kb_name, info.kb_unit, 1072 get_kbd_type_name(info.kb_type), info.kb_type); 1073#endif 1074 1075 if (ioctl(0, CONS_SETKBD, info.kb_index) == -1) 1076 warn("unable to set keyboard"); 1077} 1078 1079static void 1080release_keyboard(void) 1081{ 1082 keyboard_info_t info; 1083 1084 /* 1085 * If stdin is not associated with a keyboard, the following ioctl 1086 * will fail. 1087 */ 1088 if (ioctl(0, KDGKBINFO, &info) == -1) { 1089 warn("unable to obtain keyboard information"); 1090 return; 1091 } 1092#if 1 1093 printf("kbd%d\n", info.kb_index); 1094 printf(" %.*s%d, type:%s (%d)\n", 1095 (int)sizeof(info.kb_name), info.kb_name, info.kb_unit, 1096 get_kbd_type_name(info.kb_type), info.kb_type); 1097#endif 1098 if (ioctl(0, CONS_RELKBD, 0) == -1) 1099 warn("unable to release the keyboard"); 1100} 1101 1102static void 1103mux_keyboard(u_int op, char *kbd) 1104{ 1105 keyboard_info_t info; 1106 char *unit, *ep; 1107 1108 /* 1109 * If stdin is not associated with a keyboard, the following ioctl 1110 * will fail. 1111 */ 1112 if (ioctl(0, KDGKBINFO, &info) == -1) { 1113 warn("unable to obtain keyboard information"); 1114 return; 1115 } 1116#if 1 1117 printf("kbd%d\n", info.kb_index); 1118 printf(" %.*s%d, type:%s (%d)\n", 1119 (int)sizeof(info.kb_name), info.kb_name, info.kb_unit, 1120 get_kbd_type_name(info.kb_type), info.kb_type); 1121#endif 1122 /* 1123 * split kbd into name and unit. find the right most part of the 1124 * kbd string that consist of only digits. 1125 */ 1126 1127 memset(&info, 0, sizeof(info)); 1128 1129 info.kb_unit = -1; 1130 ep = kbd - 1; 1131 1132 do { 1133 unit = strpbrk(ep + 1, "0123456789"); 1134 if (unit != NULL) { 1135 info.kb_unit = strtol(unit, &ep, 10); 1136 if (*ep != '\0') 1137 info.kb_unit = -1; 1138 } 1139 } while (unit != NULL && info.kb_unit == -1); 1140 1141 if (info.kb_unit == -1) { 1142 warnx("unable to find keyboard driver unit in '%s'", kbd); 1143 return; 1144 } 1145 1146 if (unit == kbd) { 1147 warnx("unable to find keyboard driver name in '%s'", kbd); 1148 return; 1149 } 1150 if (unit - kbd >= (int) sizeof(info.kb_name)) { 1151 warnx("keyboard name '%s' is too long", kbd); 1152 return; 1153 } 1154 1155 strncpy(info.kb_name, kbd, unit - kbd); 1156 1157 /* 1158 * If stdin is not associated with a kbdmux(4) keyboard, the following 1159 * ioctl will fail. 1160 */ 1161 1162 if (ioctl(0, op, &info) == -1) 1163 warn("unable to (un)mux the keyboard"); 1164} 1165 1166static void 1167usage(void) 1168{ 1169 fprintf(stderr, "%s\n%s\n%s\n", 1170"usage: kbdcontrol [-dFKix] [-A name] [-a name] [-b duration.pitch | [quiet.]belltype]", 1171" [-r delay.repeat | speed] [-l mapfile] [-f # string]", 1172" [-k device] [-L mapfile]"); 1173 exit(1); 1174} 1175 1176 1177int 1178main(int argc, char **argv) 1179{ 1180 int opt; 1181 1182 while((opt = getopt(argc, argv, "A:a:b:df:iKk:Fl:L:r:x")) != -1) 1183 switch(opt) { 1184 case 'A': 1185 case 'a': 1186 mux_keyboard((opt == 'A')? KBRELKBD : KBADDKBD, optarg); 1187 break; 1188 case 'b': 1189 set_bell_values(optarg); 1190 break; 1191 case 'd': 1192 print_keymap(); 1193 break; 1194 case 'l': 1195 load_keymap(optarg, 0); 1196 break; 1197 case 'L': 1198 load_keymap(optarg, 1); 1199 break; 1200 case 'f': 1201 set_functionkey(optarg, 1202 nextarg(argc, argv, &optind, 'f')); 1203 break; 1204 case 'F': 1205 load_default_functionkeys(); 1206 break; 1207 case 'i': 1208 show_kbd_info(); 1209 break; 1210 case 'K': 1211 release_keyboard(); 1212 break; 1213 case 'k': 1214 set_keyboard(optarg); 1215 break; 1216 case 'r': 1217 set_keyrates(optarg); 1218 break; 1219 case 'x': 1220 hex = 1; 1221 break; 1222 default: 1223 usage(); 1224 } 1225 if ((optind != argc) || (argc == 1)) 1226 usage(); 1227 exit(0); 1228} 1229