Initial revision
[platform/upstream/busybox.git] / console-tools / loadkmap.c
1 #include "internal.h"
2 #include <errno.h>
3 #include <fcntl.h>
4 #include <stdio.h>
5 #include <linux/kd.h>
6 #include <linux/keyboard.h>
7 #include <sys/ioctl.h>
8
9
10 const char                      loadkmap_usage[] = "loadkmap\n"
11 "\n"
12 "\tLoad a binary keyboard translation table from standard input.\n"
13 "\n";
14
15
16 int
17 loadkmap_main(struct FileInfo * info, int argc, char * * argv)
18
19     struct kbentry ke;
20     u_short *ibuff;
21     int i,j,fd,readsz,pos,ibuffsz=NR_KEYS * sizeof(u_short);
22     char flags[MAX_NR_KEYMAPS],magic[]="bkeymap",buff[7];
23
24     fd = open("/dev/tty0", O_RDWR);
25     if (fd < 0) {
26         fprintf(stderr, "Error opening /dev/tty0: %s\n", strerror(errno));
27         return 1;
28     }
29
30     read(0,buff,7);
31     if (0 != strncmp(buff,magic,7)) {
32         fprintf(stderr, "This is not a valid binary keymap.\n");
33         return 1;
34     }
35    
36     if ( MAX_NR_KEYMAPS != read(0,flags,MAX_NR_KEYMAPS) ) {
37         fprintf(stderr, "Error reading keymap flags: %s\n", strerror(errno));
38         return 1;
39     }
40
41     ibuff=(u_short *) malloc(ibuffsz);
42     if (!ibuff) {
43         fprintf(stderr, "Out of memory.\n");
44         return 1;
45     }
46
47     for(i=0; i<MAX_NR_KEYMAPS; i++) {
48         if (flags[i]==1){
49             pos=0;
50             while (pos < ibuffsz) {
51                 if ( (readsz = read(0,ibuff+pos,ibuffsz-pos)) < 0 ) {
52                     fprintf(stderr, "Error reading keymap: %s\n", 
53                         strerror(errno));
54                     return 1;
55                 }
56                 pos += readsz;
57             }
58             for(j=0; j<NR_KEYS; j++) {
59                 ke.kb_index = j;
60                 ke.kb_table = i;
61                 ke.kb_value = ibuff[j];
62                 ioctl(fd, KDSKBENT, &ke);
63             }
64         }
65     }
66     close (fd);
67     return 0;
68 }