Fixed up copyright notices and such
[oweals/busybox.git] / console-tools / loadkmap.c
1 /*
2  * Mini loadkmap implementation for busybox
3  *
4  * Copyright (C) 1998 Enrique Zanardi <ezanardi@ull.es>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  */
21
22 #include "internal.h"
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <stdio.h>
26 #include <linux/kd.h>
27 #include <linux/keyboard.h>
28 #include <sys/ioctl.h>
29
30
31 static const char loadkmap_usage[] = "loadkmap\n"
32 "\n"
33 "\tLoad a binary keyboard translation table from standard input.\n"
34 "\n";
35
36
37 int
38 loadkmap_main(int argc, char * * argv)
39
40     struct kbentry ke;
41     u_short *ibuff;
42     int i,j,fd,readsz,pos,ibuffsz=NR_KEYS * sizeof(u_short);
43     char flags[MAX_NR_KEYMAPS],magic[]="bkeymap",buff[7];
44
45     fd = open("/dev/tty0", O_RDWR);
46     if (fd < 0) {
47         fprintf(stderr, "Error opening /dev/tty0: %s\n", strerror(errno));
48         return 1;
49     }
50
51     read(0,buff,7);
52     if (0 != strncmp(buff,magic,7)) {
53         fprintf(stderr, "This is not a valid binary keymap.\n");
54         return 1;
55     }
56    
57     if ( MAX_NR_KEYMAPS != read(0,flags,MAX_NR_KEYMAPS) ) {
58         fprintf(stderr, "Error reading keymap flags: %s\n", strerror(errno));
59         return 1;
60     }
61
62     ibuff=(u_short *) malloc(ibuffsz);
63     if (!ibuff) {
64         fprintf(stderr, "Out of memory.\n");
65         return 1;
66     }
67
68     for(i=0; i<MAX_NR_KEYMAPS; i++) {
69         if (flags[i]==1){
70             pos=0;
71             while (pos < ibuffsz) {
72                 if ( (readsz = read(0,(char *)ibuff+pos,ibuffsz-pos)) < 0 ) {
73                     fprintf(stderr, "Error reading keymap: %s\n", 
74                         strerror(errno));
75                     return 1;
76                 }
77                 pos += readsz;
78             }
79             for(j=0; j<NR_KEYS; j++) {
80                 ke.kb_index = j;
81                 ke.kb_table = i;
82                 ke.kb_value = ibuff[j];
83                 ioctl(fd, KDSKBENT, &ke);
84             }
85         }
86     }
87     close (fd);
88     return 0;
89 }