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