dumpkmap.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Mini dumpkmap implementation for busybox
  4. *
  5. * Copyright (C) Arne Bernin <arne@matrix.loopback.org>
  6. *
  7. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  8. *
  9. */
  10. //config:config DUMPKMAP
  11. //config: bool "dumpkmap"
  12. //config: default y
  13. //config: select PLATFORM_LINUX
  14. //config: help
  15. //config: This program dumps the kernel's keyboard translation table to
  16. //config: stdout, in binary format. You can then use loadkmap to load it.
  17. //applet:IF_DUMPKMAP(APPLET(dumpkmap, BB_DIR_BIN, BB_SUID_DROP))
  18. //kbuild:lib-$(CONFIG_DUMPKMAP) += dumpkmap.o
  19. //usage:#define dumpkmap_trivial_usage
  20. //usage: "> keymap"
  21. //usage:#define dumpkmap_full_usage "\n\n"
  22. //usage: "Print a binary keyboard translation table to stdout"
  23. //usage:
  24. //usage:#define dumpkmap_example_usage
  25. //usage: "$ dumpkmap > keymap\n"
  26. #include "libbb.h"
  27. #include "common_bufsiz.h"
  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. #define KDGKBENT 0x4B46 /* gets one entry in translation table */
  35. /* From <linux/keyboard.h> */
  36. #define NR_KEYS 128
  37. #define MAX_NR_KEYMAPS 256
  38. int dumpkmap_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  39. int dumpkmap_main(int argc UNUSED_PARAM, char **argv)
  40. {
  41. struct kbentry ke;
  42. int i, j, fd;
  43. #define flags bb_common_bufsiz1
  44. setup_common_bufsiz();
  45. /* When user accidentally runs "dumpkmap FILE"
  46. * instead of "dumpkmap >FILE", we'd dump binary stuff to tty.
  47. * Let's prevent it:
  48. */
  49. if (argv[1])
  50. bb_show_usage();
  51. /* bb_warn_ignoring_args(argv[1]);*/
  52. fd = get_console_fd_or_die();
  53. #if 0
  54. write(STDOUT_FILENO, "bkeymap", 7);
  55. /* Here we want to set everything to 0 except for indexes:
  56. * [0-2] [4-6] [8-10] [12]
  57. */
  58. /*memset(flags, 0x00, MAX_NR_KEYMAPS); - already is */
  59. memset(flags, 0x01, 13);
  60. flags[3] = flags[7] = flags[11] = 0;
  61. /* dump flags */
  62. write(STDOUT_FILENO, flags, MAX_NR_KEYMAPS);
  63. #define flags7 flags
  64. #else
  65. /* Same effect */
  66. /* 0 1 2 3 4 5 6 7 8 9 a b c=12 */
  67. memcpy(flags, "bkeymap\1\1\1\0\1\1\1\0\1\1\1\0\1",
  68. /* Can use sizeof, or sizeof-1. sizeof is even, using that */
  69. /****/ sizeof("bkeymap\1\1\1\0\1\1\1\0\1\1\1\0\1")
  70. );
  71. write(STDOUT_FILENO, flags, 7 + MAX_NR_KEYMAPS);
  72. #define flags7 (flags + 7)
  73. #endif
  74. for (i = 0; i < 13; i++) {
  75. if (flags7[i]) {
  76. for (j = 0; j < NR_KEYS; j++) {
  77. ke.kb_index = j;
  78. ke.kb_table = i;
  79. if (!ioctl_or_perror(fd, KDGKBENT, &ke,
  80. "ioctl(KDGKBENT{%d,%d}) failed",
  81. j, i)
  82. ) {
  83. write(STDOUT_FILENO, &ke.kb_value, 2);
  84. }
  85. }
  86. }
  87. }
  88. if (ENABLE_FEATURE_CLEAN_UP) {
  89. close(fd);
  90. }
  91. return EXIT_SUCCESS;
  92. }