Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Mini loadkmap implementation for busybox |
| 4 | * |
| 5 | * Copyright (C) 1998 Enrique Zanardi <ezanardi@ull.es> |
| 6 | * |
| 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. |
| 11 | * |
| 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. |
| 16 | * |
| 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 |
| 20 | * |
| 21 | */ |
| 22 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 23 | #include "internal.h" |
| 24 | #include <errno.h> |
| 25 | #include <fcntl.h> |
| 26 | #include <stdio.h> |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 27 | #include <sys/ioctl.h> |
| 28 | |
Eric Andersen | bd22ed8 | 2000-07-08 18:55:24 +0000 | [diff] [blame] | 29 | /* From <linux/kd.h> */ |
| 30 | struct kbentry { |
| 31 | unsigned char kb_table; |
| 32 | unsigned char kb_index; |
| 33 | unsigned short kb_value; |
| 34 | }; |
| 35 | #define KDSKBENT 0x4B47 /* sets one entry in translation table */ |
| 36 | |
| 37 | /* From <linux/keyboard.h> */ |
| 38 | #define NR_KEYS 128 |
| 39 | #define MAX_NR_KEYMAPS 256 |
| 40 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 41 | int loadkmap_main(int argc, char **argv) |
| 42 | { |
| 43 | struct kbentry ke; |
| 44 | u_short *ibuff; |
| 45 | int i, j, fd, readsz, pos, ibuffsz = NR_KEYS * sizeof(u_short); |
| 46 | char flags[MAX_NR_KEYMAPS], magic[] = "bkeymap", buff[7]; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 47 | |
Erik Andersen | 3fe7f9f | 2000-04-19 03:59:10 +0000 | [diff] [blame] | 48 | if (argc>=2 && *argv[1]=='-') { |
| 49 | usage(loadkmap_usage); |
| 50 | } |
| 51 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 52 | fd = open("/dev/tty0", O_RDWR); |
| 53 | if (fd < 0) { |
Matt Kraai | d537a95 | 2000-07-14 01:51:25 +0000 | [diff] [blame] | 54 | errorMsg("Error opening /dev/tty0: %s\n", strerror(errno)); |
Erik Andersen | 3fe7f9f | 2000-04-19 03:59:10 +0000 | [diff] [blame] | 55 | exit(FALSE); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 56 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 57 | |
| 58 | read(0, buff, 7); |
| 59 | if (0 != strncmp(buff, magic, 7)) { |
Matt Kraai | d537a95 | 2000-07-14 01:51:25 +0000 | [diff] [blame] | 60 | errorMsg("This is not a valid binary keymap.\n"); |
Erik Andersen | 3fe7f9f | 2000-04-19 03:59:10 +0000 | [diff] [blame] | 61 | exit(FALSE); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | if (MAX_NR_KEYMAPS != read(0, flags, MAX_NR_KEYMAPS)) { |
Matt Kraai | d537a95 | 2000-07-14 01:51:25 +0000 | [diff] [blame] | 65 | errorMsg("Error reading keymap flags: %s\n", |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 66 | strerror(errno)); |
Erik Andersen | 3fe7f9f | 2000-04-19 03:59:10 +0000 | [diff] [blame] | 67 | exit(FALSE); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | ibuff = (u_short *) malloc(ibuffsz); |
| 71 | if (!ibuff) { |
Matt Kraai | d537a95 | 2000-07-14 01:51:25 +0000 | [diff] [blame] | 72 | errorMsg("Out of memory.\n"); |
Erik Andersen | 3fe7f9f | 2000-04-19 03:59:10 +0000 | [diff] [blame] | 73 | exit(FALSE); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | for (i = 0; i < MAX_NR_KEYMAPS; i++) { |
| 77 | if (flags[i] == 1) { |
| 78 | pos = 0; |
| 79 | while (pos < ibuffsz) { |
| 80 | if ((readsz = read(0, (char *) ibuff + pos, ibuffsz - pos)) |
| 81 | < 0) { |
Matt Kraai | d537a95 | 2000-07-14 01:51:25 +0000 | [diff] [blame] | 82 | errorMsg("Error reading keymap: %s\n", |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 83 | strerror(errno)); |
Erik Andersen | 3fe7f9f | 2000-04-19 03:59:10 +0000 | [diff] [blame] | 84 | exit(FALSE); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 85 | } |
| 86 | pos += readsz; |
| 87 | } |
| 88 | for (j = 0; j < NR_KEYS; j++) { |
| 89 | ke.kb_index = j; |
| 90 | ke.kb_table = i; |
| 91 | ke.kb_value = ibuff[j]; |
| 92 | ioctl(fd, KDSKBENT, &ke); |
| 93 | } |
| 94 | } |
| 95 | } |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 96 | /* Don't bother to close files. Exit does that |
| 97 | * automagically, so we can save a few bytes */ |
| 98 | /* close(fd); */ |
Eric Andersen | b610615 | 2000-06-19 17:25:40 +0000 | [diff] [blame] | 99 | return(TRUE); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 100 | } |