dumpkmap.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. #define flags bb_common_bufsiz1
  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. */
  39. if (argv[1])
  40. bb_show_usage();
  41. /* bb_warn_ignoring_args(argv[1]);*/
  42. fd = get_console_fd_or_die();
  43. #if 0
  44. write(STDOUT_FILENO, "bkeymap", 7);
  45. /* Here we want to set everything to 0 except for indexes:
  46. * [0-2] [4-6] [8-10] [12]
  47. */
  48. /*memset(flags, 0x00, MAX_NR_KEYMAPS); - already is */
  49. memset(flags, 0x01, 13);
  50. flags[3] = flags[7] = flags[11] = 0;
  51. /* dump flags */
  52. write(STDOUT_FILENO, flags, MAX_NR_KEYMAPS);
  53. #define flags7 flags
  54. #else
  55. /* Same effect */
  56. /* 0 1 2 3 4 5 6 7 8 9 a b c=12 */
  57. memcpy(flags, "bkeymap\1\1\1\0\1\1\1\0\1\1\1\0\1",
  58. /* Can use sizeof, or sizeof-1. sizeof is even, using that */
  59. /****/ sizeof("bkeymap\1\1\1\0\1\1\1\0\1\1\1\0\1")
  60. );
  61. write(STDOUT_FILENO, flags, 7 + MAX_NR_KEYMAPS);
  62. #define flags7 (flags + 7)
  63. #endif
  64. for (i = 0; i < 13; i++) {
  65. if (flags7[i]) {
  66. for (j = 0; j < NR_KEYS; j++) {
  67. ke.kb_index = j;
  68. ke.kb_table = i;
  69. if (!ioctl_or_perror(fd, KDGKBENT, &ke,
  70. "ioctl(KDGKBENT{%d,%d}) failed",
  71. j, i)
  72. ) {
  73. write(STDOUT_FILENO, &ke.kb_value, 2);
  74. }
  75. }
  76. }
  77. }
  78. if (ENABLE_FEATURE_CLEAN_UP) {
  79. close(fd);
  80. }
  81. return EXIT_SUCCESS;
  82. }