3
0

loadkmap.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. //usage:#define loadkmap_trivial_usage
  10. //usage: "< keymap"
  11. //usage:#define loadkmap_full_usage "\n\n"
  12. //usage: "Load a binary keyboard translation table from stdin\n"
  13. /* //usage: "\n -C TTY Affect TTY instead of /dev/tty" */
  14. //usage:
  15. //usage:#define loadkmap_example_usage
  16. //usage: "$ loadkmap < /etc/i18n/lang-keymap\n"
  17. #include "libbb.h"
  18. #define BINARY_KEYMAP_MAGIC "bkeymap"
  19. /* From <linux/kd.h> */
  20. struct kbentry {
  21. unsigned char kb_table;
  22. unsigned char kb_index;
  23. unsigned short kb_value;
  24. };
  25. /* sets one entry in translation table */
  26. #define KDSKBENT 0x4B47
  27. /* From <linux/keyboard.h> */
  28. #define NR_KEYS 128
  29. #define MAX_NR_KEYMAPS 256
  30. int loadkmap_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  31. int loadkmap_main(int argc UNUSED_PARAM, char **argv)
  32. {
  33. struct kbentry ke;
  34. int i, j, fd;
  35. uint16_t ibuff[NR_KEYS];
  36. /* const char *tty_name = CURRENT_TTY; */
  37. RESERVE_CONFIG_BUFFER(flags, MAX_NR_KEYMAPS);
  38. /* When user accidentally runs "loadkmap FILE"
  39. * instead of "loadkmap <FILE", we end up waiting for input from tty.
  40. * Let's prevent it: */
  41. if (argv[1])
  42. bb_show_usage();
  43. /* bb_warn_ignoring_args(argv[1]); */
  44. fd = get_console_fd_or_die();
  45. /* or maybe:
  46. opt = getopt32(argv, "C:", &tty_name);
  47. fd = xopen_nonblocking(tty_name);
  48. */
  49. xread(STDIN_FILENO, flags, 7);
  50. if (strncmp(flags, BINARY_KEYMAP_MAGIC, 7))
  51. bb_error_msg_and_die("not a valid binary keymap");
  52. xread(STDIN_FILENO, flags, MAX_NR_KEYMAPS);
  53. for (i = 0; i < MAX_NR_KEYMAPS; i++) {
  54. if (flags[i] == 1) {
  55. xread(STDIN_FILENO, ibuff, NR_KEYS * sizeof(uint16_t));
  56. for (j = 0; j < NR_KEYS; j++) {
  57. ke.kb_index = j;
  58. ke.kb_table = i;
  59. ke.kb_value = ibuff[j];
  60. ioctl(fd, KDSKBENT, &ke);
  61. }
  62. }
  63. }
  64. if (ENABLE_FEATURE_CLEAN_UP) {
  65. close(fd);
  66. RELEASE_CONFIG_BUFFER(flags);
  67. }
  68. return EXIT_SUCCESS;
  69. }