|
|
1.1 root 1: /*
2: * driver routine for loadable keyboard tables.
3: *
4: * prior to firing off the ioctl() which loads the new keyboard tables,
5: * permform some simple validity checks on the table.
6: * if errors are found, bail out without setting the new table.
7: *
8: * Version 1.0, 6/8/91
9: */
10: #include <stdio.h>
11: #include <sgtty.h>
12: #include <sys/kb.h>
13: #include <sys/kbscan.h>
14: #include <errno.h>
15:
16: #define VERSION "1.0"
17: #define FALSE (0 != 0)
18: #define TRUE (0 == 0)
19:
20: /*
21: * globals
22: */
23: char *argv0; /* name of this executable */
24: int errors; /* for exit status */
25: char verbose; /* step-by-step details */
26: char debug; /* print out cooked table & exit */
27: KBTBL table[MAX_KEYS]; /* cooked table for ioctl() */
28: FNKEY *arena; /* function key arena */
29:
30: unsigned char keyval[] = { /* code set 3 mapped value */
31: none, K_1, K_2, K_3, K_4, K_5, K_6, K_7,
32: K_8, K_9, K_10, K_11, K_12, K_13, none, K_15,
33: K_16, K_17, K_18, K_19, K_20, K_21, K_22, K_23,
34: K_24, K_25, K_26, K_27, K_28, K_29, K_30, K_31,
35: K_32, K_33, K_34, K_35, K_36, K_37, K_38, K_39,
36: K_40, K_41, K_42, K_43, K_44, K_45, K_46, K_47,
37: K_48, K_49, K_50, K_51, K_52, K_53, K_54, K_55,
38: none, K_57, K_58, none, K_60, K_61, K_62, none,
39: K_64, none, none, none, none, none, none, none,
40: none, none, none, K_75, K_76, none, none, K_79,
41: K_80, K_81, none, K_83, K_84, K_85, K_86, none,
42: none, K_89, K_90, K_91, K_92, K_93, none, K_95,
43: K_96, K_97, K_98, K_99, K_100, K_101, K_102, K_103,
44: K_104, K_105, K_106, none, K_108, none, K_110, none,
45: K_112, K_113, K_114, K_115, K_116, K_117, K_118, K_119,
46: K_120, K_121, K_122, K_123, K_124, K_125, K_126
47: };
48:
49: /*
50: * externs from user-defined keyboard table
51: */
52: extern KBTBL kbtbl[]; /* actual table */
53: extern char tbl_name[]; /* name of table as text */
54: extern unsigned char *funkey[]; /* function key definitions */
55: extern int numfun; /* # of function keys in tbl */
56: extern int numkey; /* number of keys in kbtbl[] */
57:
58: main(argc, argv)
59: char *argv[];
60: {
61: unsigned char *cp, *ncp;
62: int i, j;
63: int fd; /* console file descriptor */
64:
65: argv0 = argv[0];
66: if ((arena = (FNKEY *)malloc(sizeof(FNKEY) + MAX_FCHAR)) == NULL) {
67: err("out of memory");
68: exit(errors);
69: }
70: if (argc > 1) {
71: if (strcmp(argv[1], "-V") == 0)
72: printf("Version %s\n", VERSION);
73: else if (strcmp(argv[1], "-D") == 0)
74: ++debug;
75: else
76: usage();
77: }
78:
79: if ((fd = open("/dev/console", 2)) < 0) {
80: err("unable to access console");
81: exit(errors);
82: }
83:
84: /*
85: * loop through the user's keyboard table validating each entry.
86: * if the entry is good, copy it to the destination table.
87: */
88: for (i = 0; i < numkey; ++i) { /* loop thru user's keys */
89: if (ok_entry(i)) {
90: j = kbtbl[i].k_key; /* map key */
91: table[j] = kbtbl[i]; /* copy entry */
92: } else
93: ++errors;
94: }
95:
96: if (errors)
97: exit(errors);
98: /*
99: * build a function key arena consisting of the user defined
100: * special and function keys.
101: */
102: ncp = arena->k_fnval;
103: for (i = 0; i < numfun; ++i) {
104: cp = funkey[i];
105: do {
106: if (ncp >= &arena->k_fnval[MAX_FCHAR]) {
107: err("function key table overflow");
108: exit(errors);
109: }
110: *ncp++ = *cp;
111: } while (*cp++ != DELIM);
112: }
113: arena->k_nfkeys = numfun;
114:
115: if (debug) {
116: dump(); /* print out cooked table */
117: exit(0);
118: }
119:
120: /*
121: * load the cooked keyboard table into the driver via a
122: * special ioctl() call.
123: */
124: ioctl(fd, TIOCSETKBT, table);
125: if (errno) {
126: perror("keyboard table ioctl() failed");
127: exit(++errors);
128: }
129:
130: /*
131: * load the cooked function key table into the driver via a
132: * special ioctl() call.
133: */
134: ioctl(fd, TIOCSETF, arena);
135: if (errno) {
136: perror("function key ioctl() failed");
137: exit(++errors);
138: }
139:
140: printf("Loaded %s\n", tbl_name);
141: close(fd);
142: exit(errors);
143: }
144:
145: /*
146: * validate a table entry
147: */
148: ok_entry(n)
149: register int n;
150: {
151: int i;
152: int key = lookup(kbtbl[n].k_key);
153:
154: if (!key) {
155: err("invalid key #0x%x in kbtbl[%d]", kbtbl[n].k_key, n);
156: return FALSE;
157: }
158: if ((kbtbl[n].k_flags & (S|F)) == (S|F)) {
159: err("invalid flag field for key K_%d", key);
160: return FALSE;
161: }
162: if (kbtbl[n].k_flags & S) {
163: for (i = BASE; i <= ALT_GR; ++i) {
164: if (kbtbl[n].k_val[i] != kbtbl[n].k_val[BASE]) {
165: err("inconsistent shift key entry for K_%d",
166: key);
167: return FALSE;
168: }
169: if (kbtbl[n].k_val[i] < scroll
170: || kbtbl[n].k_val[i] > altgr) {
171: err("bad shift key entry for K_%d",
172: key);
173: return FALSE;
174: }
175: } /* for */
176: } else if (kbtbl[n].k_flags & F) {
177: for (i = BASE; i <= ALT_GR; ++i) {
178: if (kbtbl[n].k_val[i] != none)
179: if (kbtbl[n].k_val[i] >= numfun) {
180: err("bad function key entry for K_%d",
181: key);
182: return FALSE;
183: }
184: } /* for */
185: } /* flag key */
186:
187: key = kbtbl[n].k_key;
188: if (table[key].k_key != 0) {
189: err("multiple entries for K_%d", key);
190: return FALSE;
191: }
192: return TRUE;
193: }
194:
195: /*
196: * lookup the physical key number associated with a given
197: * code set 3 scan code.
198: *
199: * return 0 if not found.
200: */
201: lookup(sc)
202: unsigned sc;
203: {
204: register int i;
205:
206: if (sc == none)
207: return 0;
208: for (i = 0; i < sizeof(keyval)/sizeof(keyval[0]); ++i)
209: if (keyval[i] == (unsigned char)sc)
210: return (i);
211: return 0;
212: }
213:
214: usage()
215: {
216: fprintf(stderr, "usage:\t%s [-V]\n", argv0);
217: exit(1);
218: }
219:
220: err(msg)
221: char *msg;
222: {
223: fprintf(stderr, "%s: ERROR: %r\n", argv0, &msg);
224: ++errors;
225: }
226:
227: /*
228: * dump the cooked keyboard table to stdout
229: */
230: dump()
231: {
232: int i, j;
233:
234: for (i = 0; i < MAX_KEYS; ++i) {
235: printf("%02x: %02x ", i, table[i].k_key);
236: for (j = 0; j < 9; ++j)
237: printf("%02x ", table[i].k_val[j]);
238: printf("(%02x)\n", table[i].k_flags);
239: }
240: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.