dumpkmap.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. #include "common_bufsiz.h"
  20. /* From <linux/kd.h> */
  21. struct kbentry {
  22. unsigned char kb_table;
  23. unsigned char kb_index;
  24. unsigned short kb_value;
  25. };
  26. #define KDGKBENT 0x4B46 /* gets one entry in translation table */
  27. /* From <linux/keyboard.h> */
  28. #define NR_KEYS 128
  29. #define MAX_NR_KEYMAPS 256
  30. int dumpkmap_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  31. int dumpkmap_main(int argc UNUSED_PARAM, char **argv)
  32. {
  33. struct kbentry ke;
  34. int i, j, fd;
  35. #define flags bb_common_bufsiz1
  36. setup_common_bufsiz();
  37. /* When user accidentally runs "dumpkmap FILE"
  38. * instead of "dumpkmap >FILE", we'd dump binary stuff to tty.
  39. * Let's prevent it:
  40. */
  41. if (argv[1])
  42. bb_show_usage();
  43. /* bb_warn_ignoring_args(argv[1]);*/
  44. fd = get_console_fd_or_die();
  45. #if 0
  46. write(STDOUT_FILENO, "bkeymap", 7);
  47. /* Here we want to set everything to 0 except for indexes:
  48. * [0-2] [4-6] [8-10] [12]
  49. */
  50. /*memset(flags, 0x00, MAX_NR_KEYMAPS); - already is */
  51. memset(flags, 0x01, 13);
  52. flags[3] = flags[7] = flags[11] = 0;
  53. /* dump flags */
  54. write(STDOUT_FILENO, flags, MAX_NR_KEYMAPS);
  55. #define flags7 flags
  56. #else
  57. /* Same effect */
  58. /* 0 1 2 3 4 5 6 7 8 9 a b c=12 */
  59. memcpy(flags, "bkeymap\1\1\1\0\1\1\1\0\1\1\1\0\1",
  60. /* Can use sizeof, or sizeof-1. sizeof is even, using that */
  61. /****/ sizeof("bkeymap\1\1\1\0\1\1\1\0\1\1\1\0\1")
  62. );
  63. write(STDOUT_FILENO, flags, 7 + MAX_NR_KEYMAPS);
  64. #define flags7 (flags + 7)
  65. #endif
  66. for (i = 0; i < 13; i++) {
  67. if (flags7[i]) {
  68. for (j = 0; j < NR_KEYS; j++) {
  69. ke.kb_index = j;
  70. ke.kb_table = i;
  71. if (!ioctl_or_perror(fd, KDGKBENT, &ke,
  72. "ioctl(KDGKBENT{%d,%d}) failed",
  73. j, i)
  74. ) {
  75. write(STDOUT_FILENO, &ke.kb_value, 2);
  76. }
  77. }
  78. }
  79. }
  80. if (ENABLE_FEATURE_CLEAN_UP) {
  81. close(fd);
  82. }
  83. return EXIT_SUCCESS;
  84. }