lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 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 | fd = get_console_fd_or_die(); |
| 52 | /* or maybe: |
| 53 | opt = getopt32(argv, "C:", &tty_name); |
| 54 | fd = xopen_nonblocking(tty_name); |
| 55 | */ |
| 56 | |
| 57 | xread(STDIN_FILENO, flags, 7); |
| 58 | if (strncmp(flags, BINARY_KEYMAP_MAGIC, 7)) |
| 59 | bb_error_msg_and_die("not a valid binary keymap"); |
| 60 | |
| 61 | xread(STDIN_FILENO, flags, MAX_NR_KEYMAPS); |
| 62 | |
| 63 | for (i = 0; i < MAX_NR_KEYMAPS; i++) { |
| 64 | if (flags[i] == 1) { |
| 65 | xread(STDIN_FILENO, ibuff, NR_KEYS * sizeof(uint16_t)); |
| 66 | for (j = 0; j < NR_KEYS; j++) { |
| 67 | ke.kb_index = j; |
| 68 | ke.kb_table = i; |
| 69 | ke.kb_value = ibuff[j]; |
| 70 | ioctl(fd, KDSKBENT, &ke); |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | if (ENABLE_FEATURE_CLEAN_UP) { |
| 76 | close(fd); |
| 77 | RELEASE_CONFIG_BUFFER(flags); |
| 78 | } |
| 79 | return EXIT_SUCCESS; |
| 80 | } |