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 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 8 | */ |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 9 | #include "libbb.h" |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 10 | |
Matt Kraai | 9133c98 | 2000-10-25 16:48:15 +0000 | [diff] [blame] | 11 | #define BINARY_KEYMAP_MAGIC "bkeymap" |
| 12 | |
Eric Andersen | bd22ed8 | 2000-07-08 18:55:24 +0000 | [diff] [blame] | 13 | /* From <linux/kd.h> */ |
| 14 | struct kbentry { |
| 15 | unsigned char kb_table; |
| 16 | unsigned char kb_index; |
| 17 | unsigned short kb_value; |
| 18 | }; |
Glenn L McGrath | acbdc47 | 2003-11-21 09:27:02 +0000 | [diff] [blame] | 19 | /* sets one entry in translation table */ |
| 20 | #define KDSKBENT 0x4B47 |
Eric Andersen | bd22ed8 | 2000-07-08 18:55:24 +0000 | [diff] [blame] | 21 | |
| 22 | /* From <linux/keyboard.h> */ |
Glenn L McGrath | acbdc47 | 2003-11-21 09:27:02 +0000 | [diff] [blame] | 23 | #define NR_KEYS 128 |
| 24 | #define MAX_NR_KEYMAPS 256 |
Eric Andersen | bd22ed8 | 2000-07-08 18:55:24 +0000 | [diff] [blame] | 25 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 26 | int loadkmap_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denys Vlasenko | 8aab0c9 | 2010-06-06 18:10:50 +0200 | [diff] [blame] | 27 | int loadkmap_main(int argc UNUSED_PARAM, char **argv) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 28 | { |
| 29 | struct kbentry ke; |
Glenn L McGrath | acbdc47 | 2003-11-21 09:27:02 +0000 | [diff] [blame] | 30 | int i, j, fd; |
Denis Vlasenko | 2870301 | 2006-12-19 20:32:02 +0000 | [diff] [blame] | 31 | uint16_t ibuff[NR_KEYS]; |
Denis Vlasenko | dc70069 | 2008-11-08 21:39:06 +0000 | [diff] [blame] | 32 | /* const char *tty_name = CURRENT_TTY; */ |
Denys Vlasenko | a355da0 | 2010-01-04 13:16:08 +0100 | [diff] [blame] | 33 | RESERVE_CONFIG_BUFFER(flags, MAX_NR_KEYMAPS); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 34 | |
Denys Vlasenko | 2fd45c2 | 2010-06-06 18:09:57 +0200 | [diff] [blame] | 35 | /* When user accidentally runs "loadkmap FILE" |
| 36 | * instead of "loadkmap <FILE", we end up waiting for input from tty. |
| 37 | * Let's prevent it: */ |
| 38 | if (argv[1]) |
| 39 | bb_show_usage(); |
Denys Vlasenko | a355da0 | 2010-01-04 13:16:08 +0100 | [diff] [blame] | 40 | /* bb_warn_ignoring_args(argv[1]); */ |
Denis Vlasenko | 2afd5ab | 2008-08-05 23:32:27 +0000 | [diff] [blame] | 41 | fd = get_console_fd_or_die(); |
Denis Vlasenko | dc70069 | 2008-11-08 21:39:06 +0000 | [diff] [blame] | 42 | /* or maybe: |
| 43 | opt = getopt32(argv, "C:", &tty_name); |
Bernhard Reutner-Fischer | a483087 | 2009-10-26 23:27:08 +0100 | [diff] [blame] | 44 | fd = xopen_nonblocking(tty_name); |
Denis Vlasenko | dc70069 | 2008-11-08 21:39:06 +0000 | [diff] [blame] | 45 | */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 46 | |
Bernhard Reutner-Fischer | ae4342c | 2008-05-19 08:18:50 +0000 | [diff] [blame] | 47 | xread(STDIN_FILENO, flags, 7); |
| 48 | if (strncmp(flags, BINARY_KEYMAP_MAGIC, 7)) |
| 49 | bb_error_msg_and_die("not a valid binary keymap"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 50 | |
Bernhard Reutner-Fischer | ae4342c | 2008-05-19 08:18:50 +0000 | [diff] [blame] | 51 | xread(STDIN_FILENO, flags, MAX_NR_KEYMAPS); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 52 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 53 | for (i = 0; i < MAX_NR_KEYMAPS; i++) { |
| 54 | if (flags[i] == 1) { |
Bernhard Reutner-Fischer | ae4342c | 2008-05-19 08:18:50 +0000 | [diff] [blame] | 55 | xread(STDIN_FILENO, ibuff, NR_KEYS * sizeof(uint16_t)); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 56 | for (j = 0; j < NR_KEYS; j++) { |
| 57 | ke.kb_index = j; |
| 58 | ke.kb_table = i; |
| 59 | ke.kb_value = ibuff[j]; |
| 60 | ioctl(fd, KDSKBENT, &ke); |
| 61 | } |
| 62 | } |
| 63 | } |
Glenn L McGrath | acbdc47 | 2003-11-21 09:27:02 +0000 | [diff] [blame] | 64 | |
Bernhard Reutner-Fischer | ae4342c | 2008-05-19 08:18:50 +0000 | [diff] [blame] | 65 | if (ENABLE_FEATURE_CLEAN_UP) { |
| 66 | close(fd); |
| 67 | RELEASE_CONFIG_BUFFER(flags); |
| 68 | } |
| 69 | return EXIT_SUCCESS; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 70 | } |