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