blob: 66ec3b043e8e3fab7e71636c3e5aa167b57b41c3
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 | |
10 | //usage:#define loadkmap_trivial_usage |
11 | //usage: "< keymap" |
12 | //usage:#define loadkmap_full_usage "\n\n" |
13 | //usage: "Load a binary keyboard translation table from stdin\n" |
14 | /* //usage: "\n -C TTY Affect TTY instead of /dev/tty" */ |
15 | //usage: |
16 | //usage:#define loadkmap_example_usage |
17 | //usage: "$ loadkmap < /etc/i18n/lang-keymap\n" |
18 | |
19 | #include "libbb.h" |
20 | |
21 | #define BINARY_KEYMAP_MAGIC "bkeymap" |
22 | |
23 | /* From <linux/kd.h> */ |
24 | struct kbentry { |
25 | unsigned char kb_table; |
26 | unsigned char kb_index; |
27 | unsigned short kb_value; |
28 | }; |
29 | /* sets one entry in translation table */ |
30 | #define KDSKBENT 0x4B47 |
31 | |
32 | /* From <linux/keyboard.h> */ |
33 | #define NR_KEYS 128 |
34 | #define MAX_NR_KEYMAPS 256 |
35 | |
36 | int loadkmap_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
37 | int loadkmap_main(int argc UNUSED_PARAM, char **argv) |
38 | { |
39 | struct kbentry ke; |
40 | int i, j, fd; |
41 | uint16_t ibuff[NR_KEYS]; |
42 | /* const char *tty_name = CURRENT_TTY; */ |
43 | RESERVE_CONFIG_BUFFER(flags, MAX_NR_KEYMAPS); |
44 | |
45 | /* When user accidentally runs "loadkmap FILE" |
46 | * instead of "loadkmap <FILE", we end up waiting for input from tty. |
47 | * Let's prevent it: */ |
48 | if (argv[1]) |
49 | bb_show_usage(); |
50 | /* bb_warn_ignoring_args(argv[1]); */ |
51 | |
52 | fd = get_console_fd_or_die(); |
53 | /* or maybe: |
54 | opt = getopt32(argv, "C:", &tty_name); |
55 | fd = xopen_nonblocking(tty_name); |
56 | */ |
57 | |
58 | xread(STDIN_FILENO, flags, 7); |
59 | if (strncmp(flags, BINARY_KEYMAP_MAGIC, 7)) |
60 | bb_error_msg_and_die("not a valid binary keymap"); |
61 | |
62 | xread(STDIN_FILENO, flags, MAX_NR_KEYMAPS); |
63 | |
64 | for (i = 0; i < MAX_NR_KEYMAPS; i++) { |
65 | if (flags[i] != 1) |
66 | continue; |
67 | xread(STDIN_FILENO, ibuff, NR_KEYS * sizeof(uint16_t)); |
68 | for (j = 0; j < NR_KEYS; j++) { |
69 | ke.kb_index = j; |
70 | ke.kb_table = i; |
71 | ke.kb_value = ibuff[j]; |
72 | /* |
73 | * Note: table[idx:0] can contain special value |
74 | * K_ALLOCATED (marks allocated tables in kernel). |
75 | * dumpkmap saves the value as-is; but attempts |
76 | * to load it here fail, since it isn't a valid |
77 | * key value: it is K(KT_SPEC,126) == 2<<8 + 126, |
78 | * whereas last valid KT_SPEC is |
79 | * K_BARENUMLOCK == K(KT_SPEC,19). |
80 | * So far we just ignore these errors: |
81 | */ |
82 | ioctl(fd, KDSKBENT, &ke); |
83 | } |
84 | } |
85 | |
86 | if (ENABLE_FEATURE_CLEAN_UP) { |
87 | close(fd); |
88 | RELEASE_CONFIG_BUFFER(flags); |
89 | } |
90 | return EXIT_SUCCESS; |
91 | } |
92 |