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