dumpkmap.c 1.7 KB

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