1 /* vi: set sw=4 ts=4: */
3 * Mini loadkmap implementation for busybox
5 * Copyright (C) 1998 Enrique Zanardi <ezanardi@ull.es>
7 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
9 //config:config LOADKMAP
10 //config: bool "loadkmap"
12 //config: select PLATFORM_LINUX
14 //config: This program loads a keyboard translation table from
15 //config: standard input.
17 //applet:IF_LOADKMAP(APPLET(loadkmap, BB_DIR_SBIN, BB_SUID_DROP))
19 //kbuild:lib-$(CONFIG_LOADKMAP) += loadkmap.o
21 //usage:#define loadkmap_trivial_usage
23 //usage:#define loadkmap_full_usage "\n\n"
24 //usage: "Load a binary keyboard translation table from stdin"
26 ////usage: "\n -C TTY Affect TTY instead of /dev/tty"
28 //usage:#define loadkmap_example_usage
29 //usage: "$ loadkmap < /etc/i18n/lang-keymap\n"
33 #define BINARY_KEYMAP_MAGIC "bkeymap"
35 /* From <linux/kd.h> */
37 unsigned char kb_table;
38 unsigned char kb_index;
39 unsigned short kb_value;
41 /* sets one entry in translation table */
42 #define KDSKBENT 0x4B47
44 /* From <linux/keyboard.h> */
46 #define MAX_NR_KEYMAPS 256
48 int loadkmap_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
49 int loadkmap_main(int argc UNUSED_PARAM, char **argv)
53 uint16_t ibuff[NR_KEYS];
54 /* const char *tty_name = CURRENT_TTY; */
55 RESERVE_CONFIG_BUFFER(flags, MAX_NR_KEYMAPS);
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: */
62 /* bb_warn_ignoring_args(argv[1]); */
64 fd = get_console_fd_or_die();
66 opt = getopt32(argv, "C:", &tty_name);
67 fd = xopen_nonblocking(tty_name);
70 xread(STDIN_FILENO, flags, 7);
71 if (!is_prefixed_with(flags, BINARY_KEYMAP_MAGIC))
72 bb_error_msg_and_die("not a valid binary keymap");
74 xread(STDIN_FILENO, flags, MAX_NR_KEYMAPS);
76 for (i = 0; i < MAX_NR_KEYMAPS; i++) {
79 xread(STDIN_FILENO, ibuff, NR_KEYS * sizeof(uint16_t));
80 for (j = 0; j < NR_KEYS; j++) {
83 ke.kb_value = ibuff[j];
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:
94 ioctl(fd, KDSKBENT, &ke);
98 if (ENABLE_FEATURE_CLEAN_UP) {
100 RELEASE_CONFIG_BUFFER(flags);