ctrlchar.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * Unified handling of special chars.
  3. *
  4. * Copyright IBM Corp. 2001
  5. * Author(s): Fritz Elfert <felfert@millenux.com> <elfert@de.ibm.com>
  6. *
  7. */
  8. #include <linux/stddef.h>
  9. #include <asm/errno.h>
  10. #include <linux/sysrq.h>
  11. #include <linux/ctype.h>
  12. #include "ctrlchar.h"
  13. #ifdef CONFIG_MAGIC_SYSRQ
  14. static int ctrlchar_sysrq_key;
  15. static void
  16. ctrlchar_handle_sysrq(struct work_struct *work)
  17. {
  18. handle_sysrq(ctrlchar_sysrq_key);
  19. }
  20. static DECLARE_WORK(ctrlchar_work, ctrlchar_handle_sysrq);
  21. #endif
  22. /**
  23. * Check for special chars at start of input.
  24. *
  25. * @param buf Console input buffer.
  26. * @param len Length of valid data in buffer.
  27. * @param tty The tty struct for this console.
  28. * @return CTRLCHAR_NONE, if nothing matched,
  29. * CTRLCHAR_SYSRQ, if sysrq was encountered
  30. * otherwise char to be inserted logically or'ed
  31. * with CTRLCHAR_CTRL
  32. */
  33. unsigned int
  34. ctrlchar_handle(const unsigned char *buf, int len, struct tty_struct *tty)
  35. {
  36. if ((len < 2) || (len > 3))
  37. return CTRLCHAR_NONE;
  38. /* hat is 0xb1 in codepage 037 (US etc.) and thus */
  39. /* converted to 0x5e in ascii ('^') */
  40. if ((buf[0] != '^') && (buf[0] != '\252'))
  41. return CTRLCHAR_NONE;
  42. #ifdef CONFIG_MAGIC_SYSRQ
  43. /* racy */
  44. if (len == 3 && buf[1] == '-') {
  45. ctrlchar_sysrq_key = buf[2];
  46. schedule_work(&ctrlchar_work);
  47. return CTRLCHAR_SYSRQ;
  48. }
  49. #endif
  50. if (len != 2)
  51. return CTRLCHAR_NONE;
  52. switch (tolower(buf[1])) {
  53. case 'c':
  54. return INTR_CHAR(tty) | CTRLCHAR_CTRL;
  55. case 'd':
  56. return EOF_CHAR(tty) | CTRLCHAR_CTRL;
  57. case 'z':
  58. return SUSP_CHAR(tty) | CTRLCHAR_CTRL;
  59. }
  60. return CTRLCHAR_NONE;
  61. }