loadkmap.c 2.4 KB

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