unstack, sort: cleanup and improvement
[minix.git] / commands / loadkeys / loadkeys.c
blobf1f0646b1d66a9fe1977a23c84d2ded5b4c8b8c6
1 /* loadkeys - load national keyboard map Author: Marcus Hampel
2 */
3 #include <sys/types.h>
4 #include <sys/ioctl.h>
5 #include <minix/keymap.h>
6 #include <fcntl.h>
7 #include <unistd.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <errno.h>
12 #if __minix_vmd
13 #define KBD_DEVICE "/dev/kbd"
14 #else
15 #define KBD_DEVICE "/dev/console"
16 #endif
18 u16_t keymap[NR_SCAN_CODES * MAP_COLS];
19 u8_t comprmap[4 + NR_SCAN_CODES * MAP_COLS * 9/8 * 2 + 1];
22 void tell(char *s)
24 write(2, s, strlen(s));
28 void fatal(char *say)
30 int err = errno;
31 tell("loadkeys: ");
32 if (say != NULL) {
33 tell(say);
34 tell(": ");
36 tell(strerror(err));
37 tell("\n");
38 exit(1);
42 void usage(void)
44 tell("Usage: loadkeys mapfile\n");
45 exit(1);
49 int main(int argc, char *argv[])
51 u8_t *cm;
52 u16_t *km;
53 int fd, n, fb;
55 if (argc != 2)
56 usage();
58 if ((fd = open(argv[1], O_RDONLY)) < 0) fatal(argv[1]);
60 if (read(fd, comprmap, sizeof(comprmap)) < 0) fatal(argv[1]);
62 if (memcmp(comprmap, KEY_MAGIC, 4) != 0) {
63 tell("loadkeys: ");
64 tell(argv[1]);
65 tell(": not a keymap file\n");
66 exit(1);
68 close(fd);
70 /* Decompress the keymap data. */
71 cm = comprmap + 4;
72 n = 8;
73 for (km = keymap; km < keymap + NR_SCAN_CODES * MAP_COLS; km++) {
74 if (n == 8) {
75 /* Need a new flag byte. */
76 fb = *cm++;
77 n = 0;
79 *km = *cm++; /* Low byte. */
80 if (fb & (1 << n)) {
81 *km |= (*cm++ << 8); /* One of the few special keys. */
83 n++;
86 if ((fd = open(KBD_DEVICE, O_WRONLY)) < 0) fatal(KBD_DEVICE);
88 if (ioctl(fd, KIOCSMAP, keymap) < 0) fatal(KBD_DEVICE);
90 return 0;