dumpkmap.c 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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.3 kb)"
  11. //config: default y
  12. //config: select PLATFORM_LINUX
  13. //config: help
  14. //config: This program dumps the kernel's keyboard translation table to
  15. //config: stdout, in binary format. You can then use loadkmap to load it.
  16. //applet:IF_DUMPKMAP(APPLET_NOEXEC(dumpkmap, dumpkmap, BB_DIR_BIN, BB_SUID_DROP, dumpkmap))
  17. /* bb_common_bufsiz1 usage here is safe wrt NOEXEC: not expecting it to be zeroed. */
  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. /* When user accidentally runs "dumpkmap FILE"
  44. * instead of "dumpkmap >FILE", we'd dump binary stuff to tty.
  45. * Let's prevent it:
  46. */
  47. if (argv[1])
  48. bb_show_usage();
  49. /* bb_warn_ignoring_args(argv[1]);*/
  50. fd = get_console_fd_or_die();
  51. #define flags bb_common_bufsiz1
  52. setup_common_bufsiz();
  53. /* 0 1 2 3 4 5 6 7 8 9 a b c=12 */
  54. memcpy(flags, "bkeymap\1\1\1\0\1\1\1\0\1\1\1\0\1",
  55. /* Can use sizeof, or sizeof-1. sizeof is even, using that */
  56. /****/ sizeof("bkeymap\1\1\1\0\1\1\1\0\1\1\1\0\1")
  57. );
  58. write(STDOUT_FILENO, flags, 7 + MAX_NR_KEYMAPS);
  59. #define flags7 (flags + 7)
  60. for (i = 0; i < 13; i++) {
  61. if (flags7[i]) {
  62. for (j = 0; j < NR_KEYS; j++) {
  63. ke.kb_index = j;
  64. ke.kb_table = i;
  65. if (!ioctl_or_perror(fd, KDGKBENT, &ke,
  66. "ioctl(KDGKBENT{%d,%d}) failed",
  67. j, i)
  68. ) {
  69. write(STDOUT_FILENO, &ke.kb_value, 2);
  70. }
  71. }
  72. }
  73. }
  74. if (ENABLE_FEATURE_CLEAN_UP) {
  75. close(fd);
  76. }
  77. return EXIT_SUCCESS;
  78. }