dumpkmap.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. /* no options, no getopt */
  11. //usage:#define dumpkmap_trivial_usage
  12. //usage: "> keymap"
  13. //usage:#define dumpkmap_full_usage "\n\n"
  14. //usage: "Print a binary keyboard translation table to stdout"
  15. //usage:
  16. //usage:#define dumpkmap_example_usage
  17. //usage: "$ dumpkmap > keymap\n"
  18. #include "libbb.h"
  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. #define KDGKBENT 0x4B46 /* gets one entry in translation table */
  26. /* From <linux/keyboard.h> */
  27. #define NR_KEYS 128
  28. #define MAX_NR_KEYMAPS 256
  29. int dumpkmap_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  30. int dumpkmap_main(int argc UNUSED_PARAM, char **argv)
  31. {
  32. struct kbentry ke;
  33. int i, j, fd;
  34. RESERVE_CONFIG_BUFFER(flags, MAX_NR_KEYMAPS);
  35. /* When user accidentally runs "dumpkmap FILE"
  36. * instead of "dumpkmap >FILE", we'd dump binary stuff to tty.
  37. * Let's prevent it: */
  38. if (argv[1])
  39. bb_show_usage();
  40. /* bb_warn_ignoring_args(argv[1]);*/
  41. fd = get_console_fd_or_die();
  42. write(STDOUT_FILENO, "bkeymap", 7);
  43. /* Here we want to set everything to 0 except for indexes:
  44. * [0-2] [4-6] [8-10] [12] */
  45. memset(flags, 0x00, MAX_NR_KEYMAPS);
  46. memset(flags, 0x01, 13);
  47. flags[3] = flags[7] = flags[11] = 0;
  48. /* dump flags */
  49. write(STDOUT_FILENO, flags, MAX_NR_KEYMAPS);
  50. for (i = 0; i < MAX_NR_KEYMAPS; i++) {
  51. if (flags[i] == 1) {
  52. for (j = 0; j < NR_KEYS; j++) {
  53. ke.kb_index = j;
  54. ke.kb_table = i;
  55. if (!ioctl_or_perror(fd, KDGKBENT, &ke,
  56. "ioctl failed with %s, %s, %p",
  57. (char *)&ke.kb_index,
  58. (char *)&ke.kb_table,
  59. &ke.kb_value)
  60. ) {
  61. write(STDOUT_FILENO, (void*)&ke.kb_value, 2);
  62. }
  63. }
  64. }
  65. }
  66. if (ENABLE_FEATURE_CLEAN_UP) {
  67. close(fd);
  68. RELEASE_CONFIG_BUFFER(flags);
  69. }
  70. return EXIT_SUCCESS;
  71. }