loadkmap.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. continue;
  56. xread(STDIN_FILENO, ibuff, NR_KEYS * sizeof(uint16_t));
  57. for (j = 0; j < NR_KEYS; j++) {
  58. ke.kb_index = j;
  59. ke.kb_table = i;
  60. ke.kb_value = ibuff[j];
  61. /*
  62. * Note: table[idx:0] can contain special value
  63. * K_ALLOCATED (marks allocated tables in kernel).
  64. * dumpkmap saves the value as-is; but attempts
  65. * to load it here fail, since it isn't a valid
  66. * key value: it is K(KT_SPEC,126) == 2<<8 + 126,
  67. * whereas last valid KT_SPEC is
  68. * K_BARENUMLOCK == K(KT_SPEC,19).
  69. * So far we just ignore these errors:
  70. */
  71. ioctl(fd, KDSKBENT, &ke);
  72. }
  73. }
  74. if (ENABLE_FEATURE_CLEAN_UP) {
  75. close(fd);
  76. RELEASE_CONFIG_BUFFER(flags);
  77. }
  78. return EXIT_SUCCESS;
  79. }