loadkmap.c 2.7 KB

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