1 /* vi: set sw=4 ts=4: */
3 * Mini loadkmap implementation for busybox
5 * Copyright (C) 1998 Enrique Zanardi <ezanardi@ull.es>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #include <sys/ioctl.h>
29 /* From <linux/kd.h> */
31 unsigned char kb_table;
32 unsigned char kb_index;
33 unsigned short kb_value;
35 #define KDSKBENT 0x4B47 /* sets one entry in translation table */
37 /* From <linux/keyboard.h> */
39 #define MAX_NR_KEYMAPS 256
42 static const char loadkmap_usage[] = "loadkmap\n"
43 #ifndef BB_FEATURE_TRIVIAL_HELP
44 "\nLoads a binary keyboard translation table from standard input.\n"
49 int loadkmap_main(int argc, char **argv)
53 int i, j, fd, readsz, pos, ibuffsz = NR_KEYS * sizeof(u_short);
54 char flags[MAX_NR_KEYMAPS], magic[] = "bkeymap", buff[7];
56 if (argc>=2 && *argv[1]=='-') {
57 usage(loadkmap_usage);
60 fd = open("/dev/tty0", O_RDWR);
62 fprintf(stderr, "Error opening /dev/tty0: %s\n", strerror(errno));
67 if (0 != strncmp(buff, magic, 7)) {
68 fprintf(stderr, "This is not a valid binary keymap.\n");
72 if (MAX_NR_KEYMAPS != read(0, flags, MAX_NR_KEYMAPS)) {
73 fprintf(stderr, "Error reading keymap flags: %s\n",
78 ibuff = (u_short *) malloc(ibuffsz);
80 fprintf(stderr, "Out of memory.\n");
84 for (i = 0; i < MAX_NR_KEYMAPS; i++) {
87 while (pos < ibuffsz) {
88 if ((readsz = read(0, (char *) ibuff + pos, ibuffsz - pos))
90 fprintf(stderr, "Error reading keymap: %s\n",
96 for (j = 0; j < NR_KEYS; j++) {
99 ke.kb_value = ibuff[j];
100 ioctl(fd, KDSKBENT, &ke);
104 /* Don't bother to close files. Exit does that
105 * automagically, so we can save a few bytes */