blob: 70ab55a8e88cb0a2980ec7d0aa35612cd852f2f4
1 | /* vi: set sw=4 ts=4: */ |
2 | /* |
3 | * Mini loadkmap implementation for busybox |
4 | * |
5 | * Copyright (C) 1998 Enrique Zanardi <ezanardi@ull.es> |
6 | * |
7 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
8 | */ |
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 |
20 | |
21 | //usage:#define loadkmap_trivial_usage |
22 | //usage: "< keymap" |
23 | //usage:#define loadkmap_full_usage "\n\n" |
24 | //usage: "Load a binary keyboard translation table from stdin" |
25 | ////usage: "\n" |
26 | ////usage: "\n -C TTY Affect TTY instead of /dev/tty" |
27 | //usage: |
28 | //usage:#define loadkmap_example_usage |
29 | //usage: "$ loadkmap < /etc/i18n/lang-keymap\n" |
30 | |
31 | #include "libbb.h" |
32 | |
33 | #define BINARY_KEYMAP_MAGIC "bkeymap" |
34 | |
35 | /* From <linux/kd.h> */ |
36 | struct kbentry { |
37 | unsigned char kb_table; |
38 | unsigned char kb_index; |
39 | unsigned short kb_value; |
40 | }; |
41 | /* sets one entry in translation table */ |
42 | #define KDSKBENT 0x4B47 |
43 | |
44 | /* From <linux/keyboard.h> */ |
45 | #define NR_KEYS 128 |
46 | #define MAX_NR_KEYMAPS 256 |
47 | |
48 | int loadkmap_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
49 | int loadkmap_main(int argc UNUSED_PARAM, char **argv) |
50 | { |
51 | struct kbentry ke; |
52 | int i, j, fd; |
53 | uint16_t ibuff[NR_KEYS]; |
54 | /* const char *tty_name = CURRENT_TTY; */ |
55 | RESERVE_CONFIG_BUFFER(flags, MAX_NR_KEYMAPS); |
56 | |
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(); |
62 | /* bb_warn_ignoring_args(argv[1]); */ |
63 | |
64 | fd = get_console_fd_or_die(); |
65 | /* or maybe: |
66 | opt = getopt32(argv, "C:", &tty_name); |
67 | fd = xopen_nonblocking(tty_name); |
68 | */ |
69 | |
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"); |
73 | |
74 | xread(STDIN_FILENO, flags, MAX_NR_KEYMAPS); |
75 | |
76 | for (i = 0; i < MAX_NR_KEYMAPS; i++) { |
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); |
95 | } |
96 | } |
97 | |
98 | if (ENABLE_FEATURE_CLEAN_UP) { |
99 | close(fd); |
100 | RELEASE_CONFIG_BUFFER(flags); |
101 | } |
102 | return EXIT_SUCCESS; |
103 | } |
104 |