loadkmap.c 2.7 KB

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