loadkmap.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Mini loadkmap implementation for busybox
  4. *
  5. * Copyright (C) 1998 Enrique Zanardi <ezanardi@ull.es>
  6. *
  7. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  8. */
  9. #include "libbb.h"
  10. #define BINARY_KEYMAP_MAGIC "bkeymap"
  11. /* From <linux/kd.h> */
  12. struct kbentry {
  13. unsigned char kb_table;
  14. unsigned char kb_index;
  15. unsigned short kb_value;
  16. };
  17. /* sets one entry in translation table */
  18. #define KDSKBENT 0x4B47
  19. /* From <linux/keyboard.h> */
  20. #define NR_KEYS 128
  21. #define MAX_NR_KEYMAPS 256
  22. int loadkmap_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  23. int loadkmap_main(int argc UNUSED_PARAM, char **argv)
  24. {
  25. struct kbentry ke;
  26. int i, j, fd;
  27. uint16_t ibuff[NR_KEYS];
  28. /* const char *tty_name = CURRENT_TTY; */
  29. RESERVE_CONFIG_BUFFER(flags, MAX_NR_KEYMAPS);
  30. /* When user accidentally runs "loadkmap FILE"
  31. * instead of "loadkmap <FILE", we end up waiting for input from tty.
  32. * Let's prevent it: */
  33. if (argv[1])
  34. bb_show_usage();
  35. /* bb_warn_ignoring_args(argv[1]); */
  36. fd = get_console_fd_or_die();
  37. /* or maybe:
  38. opt = getopt32(argv, "C:", &tty_name);
  39. fd = xopen_nonblocking(tty_name);
  40. */
  41. xread(STDIN_FILENO, flags, 7);
  42. if (strncmp(flags, BINARY_KEYMAP_MAGIC, 7))
  43. bb_error_msg_and_die("not a valid binary keymap");
  44. xread(STDIN_FILENO, flags, MAX_NR_KEYMAPS);
  45. for (i = 0; i < MAX_NR_KEYMAPS; i++) {
  46. if (flags[i] == 1) {
  47. xread(STDIN_FILENO, ibuff, NR_KEYS * sizeof(uint16_t));
  48. for (j = 0; j < NR_KEYS; j++) {
  49. ke.kb_index = j;
  50. ke.kb_table = i;
  51. ke.kb_value = ibuff[j];
  52. ioctl(fd, KDSKBENT, &ke);
  53. }
  54. }
  55. }
  56. if (ENABLE_FEATURE_CLEAN_UP) {
  57. close(fd);
  58. RELEASE_CONFIG_BUFFER(flags);
  59. }
  60. return EXIT_SUCCESS;
  61. }