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 | */ |
Denys Vlasenko | 6d93299 | 2016-11-23 10:39:27 +0100 | [diff] [blame] | 9 | //config:config LOADKMAP |
| 10 | //config: bool "loadkmap" |
| 11 | //config: default y |
| 12 | //config: select PLATFORM_LINUX |
| 13 | //config: help |
| 14 | //config: This program loads a keyboard translation table from |
| 15 | //config: standard input. |
| 16 | |
| 17 | //applet:IF_LOADKMAP(APPLET(loadkmap, BB_DIR_SBIN, BB_SUID_DROP)) |
| 18 | |
| 19 | //kbuild:lib-$(CONFIG_LOADKMAP) += loadkmap.o |
Pere Orga | 55068c4 | 2011-03-27 23:42:28 +0200 | [diff] [blame] | 20 | |
| 21 | //usage:#define loadkmap_trivial_usage |
| 22 | //usage: "< keymap" |
| 23 | //usage:#define loadkmap_full_usage "\n\n" |
Denys Vlasenko | 86031a5 | 2015-01-24 19:46:45 +0100 | [diff] [blame] | 24 | //usage: "Load a binary keyboard translation table from stdin" |
| 25 | ////usage: "\n" |
| 26 | ////usage: "\n -C TTY Affect TTY instead of /dev/tty" |
Pere Orga | 55068c4 | 2011-03-27 23:42:28 +0200 | [diff] [blame] | 27 | //usage: |
| 28 | //usage:#define loadkmap_example_usage |
| 29 | //usage: "$ loadkmap < /etc/i18n/lang-keymap\n" |
| 30 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 31 | #include "libbb.h" |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 32 | |
Matt Kraai | 9133c98 | 2000-10-25 16:48:15 +0000 | [diff] [blame] | 33 | #define BINARY_KEYMAP_MAGIC "bkeymap" |
| 34 | |
Eric Andersen | bd22ed8 | 2000-07-08 18:55:24 +0000 | [diff] [blame] | 35 | /* From <linux/kd.h> */ |
| 36 | struct kbentry { |
| 37 | unsigned char kb_table; |
| 38 | unsigned char kb_index; |
| 39 | unsigned short kb_value; |
| 40 | }; |
Glenn L McGrath | acbdc47 | 2003-11-21 09:27:02 +0000 | [diff] [blame] | 41 | /* sets one entry in translation table */ |
| 42 | #define KDSKBENT 0x4B47 |
Eric Andersen | bd22ed8 | 2000-07-08 18:55:24 +0000 | [diff] [blame] | 43 | |
| 44 | /* From <linux/keyboard.h> */ |
Glenn L McGrath | acbdc47 | 2003-11-21 09:27:02 +0000 | [diff] [blame] | 45 | #define NR_KEYS 128 |
| 46 | #define MAX_NR_KEYMAPS 256 |
Eric Andersen | bd22ed8 | 2000-07-08 18:55:24 +0000 | [diff] [blame] | 47 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 48 | int loadkmap_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denys Vlasenko | 8aab0c9 | 2010-06-06 18:10:50 +0200 | [diff] [blame] | 49 | int loadkmap_main(int argc UNUSED_PARAM, char **argv) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 50 | { |
| 51 | struct kbentry ke; |
Glenn L McGrath | acbdc47 | 2003-11-21 09:27:02 +0000 | [diff] [blame] | 52 | int i, j, fd; |
Denis Vlasenko | 2870301 | 2006-12-19 20:32:02 +0000 | [diff] [blame] | 53 | uint16_t ibuff[NR_KEYS]; |
Denis Vlasenko | dc70069 | 2008-11-08 21:39:06 +0000 | [diff] [blame] | 54 | /* const char *tty_name = CURRENT_TTY; */ |
Denys Vlasenko | a355da0 | 2010-01-04 13:16:08 +0100 | [diff] [blame] | 55 | RESERVE_CONFIG_BUFFER(flags, MAX_NR_KEYMAPS); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 56 | |
Denys Vlasenko | 2fd45c2 | 2010-06-06 18:09:57 +0200 | [diff] [blame] | 57 | /* When user accidentally runs "loadkmap FILE" |
| 58 | * instead of "loadkmap <FILE", we end up waiting for input from tty. |
| 59 | * Let's prevent it: */ |
| 60 | if (argv[1]) |
| 61 | bb_show_usage(); |
Denys Vlasenko | a355da0 | 2010-01-04 13:16:08 +0100 | [diff] [blame] | 62 | /* bb_warn_ignoring_args(argv[1]); */ |
Denys Vlasenko | 054f3eb | 2013-09-19 17:56:59 +0200 | [diff] [blame] | 63 | |
Denis Vlasenko | 2afd5ab | 2008-08-05 23:32:27 +0000 | [diff] [blame] | 64 | fd = get_console_fd_or_die(); |
Denis Vlasenko | dc70069 | 2008-11-08 21:39:06 +0000 | [diff] [blame] | 65 | /* or maybe: |
| 66 | opt = getopt32(argv, "C:", &tty_name); |
Bernhard Reutner-Fischer | a483087 | 2009-10-26 23:27:08 +0100 | [diff] [blame] | 67 | fd = xopen_nonblocking(tty_name); |
Denis Vlasenko | dc70069 | 2008-11-08 21:39:06 +0000 | [diff] [blame] | 68 | */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 69 | |
Bernhard Reutner-Fischer | ae4342c | 2008-05-19 08:18:50 +0000 | [diff] [blame] | 70 | xread(STDIN_FILENO, flags, 7); |
Denys Vlasenko | 8dff01d | 2015-03-12 17:48:34 +0100 | [diff] [blame] | 71 | if (!is_prefixed_with(flags, BINARY_KEYMAP_MAGIC)) |
Bernhard Reutner-Fischer | ae4342c | 2008-05-19 08:18:50 +0000 | [diff] [blame] | 72 | bb_error_msg_and_die("not a valid binary keymap"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 73 | |
Bernhard Reutner-Fischer | ae4342c | 2008-05-19 08:18:50 +0000 | [diff] [blame] | 74 | xread(STDIN_FILENO, flags, MAX_NR_KEYMAPS); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 75 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 76 | for (i = 0; i < MAX_NR_KEYMAPS; i++) { |
Denys Vlasenko | 054f3eb | 2013-09-19 17:56:59 +0200 | [diff] [blame] | 77 | if (flags[i] != 1) |
| 78 | continue; |
| 79 | xread(STDIN_FILENO, ibuff, NR_KEYS * sizeof(uint16_t)); |
| 80 | for (j = 0; j < NR_KEYS; j++) { |
| 81 | ke.kb_index = j; |
| 82 | ke.kb_table = i; |
| 83 | ke.kb_value = ibuff[j]; |
| 84 | /* |
| 85 | * Note: table[idx:0] can contain special value |
| 86 | * K_ALLOCATED (marks allocated tables in kernel). |
| 87 | * dumpkmap saves the value as-is; but attempts |
| 88 | * to load it here fail, since it isn't a valid |
| 89 | * key value: it is K(KT_SPEC,126) == 2<<8 + 126, |
| 90 | * whereas last valid KT_SPEC is |
| 91 | * K_BARENUMLOCK == K(KT_SPEC,19). |
| 92 | * So far we just ignore these errors: |
| 93 | */ |
| 94 | ioctl(fd, KDSKBENT, &ke); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 95 | } |
| 96 | } |
Glenn L McGrath | acbdc47 | 2003-11-21 09:27:02 +0000 | [diff] [blame] | 97 | |
Bernhard Reutner-Fischer | ae4342c | 2008-05-19 08:18:50 +0000 | [diff] [blame] | 98 | if (ENABLE_FEATURE_CLEAN_UP) { |
| 99 | close(fd); |
| 100 | RELEASE_CONFIG_BUFFER(flags); |
| 101 | } |
| 102 | return EXIT_SUCCESS; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 103 | } |