dumpkmap.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. //config:config DUMPKMAP
  10. //config: bool "dumpkmap (1.9 kb)"
  11. //config: default y
  12. //config: help
  13. //config: This program dumps the kernel's keyboard translation table to
  14. //config: stdout, in binary format. You can then use loadkmap to load it.
  15. //applet:IF_DUMPKMAP(APPLET_NOEXEC(dumpkmap, dumpkmap, BB_DIR_BIN, BB_SUID_DROP, dumpkmap))
  16. /* bb_common_bufsiz1 usage here is safe wrt NOEXEC: not expecting it to be zeroed. */
  17. //kbuild:lib-$(CONFIG_DUMPKMAP) += dumpkmap.o
  18. //usage:#define dumpkmap_trivial_usage
  19. //usage: "> keymap"
  20. //usage:#define dumpkmap_full_usage "\n\n"
  21. //usage: "Print a binary keyboard translation table to stdout"
  22. //usage:
  23. //usage:#define dumpkmap_example_usage
  24. //usage: "$ dumpkmap > keymap\n"
  25. #include "libbb.h"
  26. #include "common_bufsiz.h"
  27. /* From <linux/kd.h> */
  28. struct kbentry {
  29. unsigned char kb_table;
  30. unsigned char kb_index;
  31. unsigned short kb_value;
  32. };
  33. #define KDGKBENT 0x4B46 /* gets one entry in translation table */
  34. /* From <linux/keyboard.h> */
  35. #define NR_KEYS 128
  36. #define MAX_NR_KEYMAPS 256
  37. int dumpkmap_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  38. int dumpkmap_main(int argc UNUSED_PARAM, char **argv)
  39. {
  40. struct kbentry ke;
  41. int i, j, fd;
  42. /* When user accidentally runs "dumpkmap FILE"
  43. * instead of "dumpkmap >FILE", we'd dump binary stuff to tty.
  44. * Let's prevent it:
  45. */
  46. if (argv[1])
  47. bb_show_usage();
  48. /* bb_warn_ignoring_args(argv[1]);*/
  49. fd = get_console_fd_or_die();
  50. #define flags bb_common_bufsiz1
  51. setup_common_bufsiz();
  52. /* 0 1 2 3 4 5 6 7 8 9 a b c=12 */
  53. memcpy(flags, "bkeymap\1\1\1\0\1\1\1\0\1\1\1\0\1",
  54. /* Can use sizeof, or sizeof-1. sizeof is even, using that */
  55. /****/ sizeof("bkeymap\1\1\1\0\1\1\1\0\1\1\1\0\1")
  56. );
  57. write(STDOUT_FILENO, flags, 7 + MAX_NR_KEYMAPS);
  58. #define flags7 (flags + 7)
  59. for (i = 0; i < 13; i++) {
  60. if (flags7[i]) {
  61. for (j = 0; j < NR_KEYS; j++) {
  62. ke.kb_index = j;
  63. ke.kb_table = i;
  64. if (!ioctl_or_perror(fd, KDGKBENT, &ke,
  65. "ioctl(KDGKBENT{%d,%d}) failed",
  66. j, i)
  67. ) {
  68. write(STDOUT_FILENO, &ke.kb_value, 2);
  69. }
  70. }
  71. }
  72. }
  73. if (ENABLE_FEATURE_CLEAN_UP) {
  74. close(fd);
  75. }
  76. return EXIT_SUCCESS;
  77. }