read_key.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Utility routines.
  4. *
  5. * Copyright (C) 2008 Rob Landley <rob@landley.net>
  6. * Copyright (C) 2008 Denys Vlasenko <vda.linux@googlemail.com>
  7. *
  8. * Licensed under GPL version 2, see file LICENSE in this tarball for details.
  9. */
  10. #include "libbb.h"
  11. int FAST_FUNC read_key(int fd, smalluint *nbuffered, char *buffer)
  12. {
  13. struct pollfd pfd;
  14. const char *seq;
  15. int n;
  16. int c;
  17. /* Known escape sequences for cursor and function keys */
  18. static const char esccmds[] ALIGN1 = {
  19. 'O','A' |0x80,KEYCODE_UP ,
  20. 'O','B' |0x80,KEYCODE_DOWN ,
  21. 'O','C' |0x80,KEYCODE_RIGHT ,
  22. 'O','D' |0x80,KEYCODE_LEFT ,
  23. 'O','H' |0x80,KEYCODE_HOME ,
  24. 'O','F' |0x80,KEYCODE_END ,
  25. #if 0
  26. 'O','P' |0x80,KEYCODE_FUN1 ,
  27. /* [ESC] ESC O [2] P - [Alt-][Shift-]F1 */
  28. /* Ctrl- seems to not affect sequences */
  29. 'O','Q' |0x80,KEYCODE_FUN2 ,
  30. 'O','R' |0x80,KEYCODE_FUN3 ,
  31. 'O','S' |0x80,KEYCODE_FUN4 ,
  32. #endif
  33. '[','A' |0x80,KEYCODE_UP ,
  34. '[','B' |0x80,KEYCODE_DOWN ,
  35. '[','C' |0x80,KEYCODE_RIGHT ,
  36. '[','D' |0x80,KEYCODE_LEFT ,
  37. '[','H' |0x80,KEYCODE_HOME , /* xterm */
  38. /* [ESC] ESC [ [2] H - [Alt-][Shift-]Home */
  39. '[','F' |0x80,KEYCODE_END , /* xterm */
  40. '[','1','~' |0x80,KEYCODE_HOME , /* vt100? linux vt? or what? */
  41. '[','2','~' |0x80,KEYCODE_INSERT ,
  42. '[','3','~' |0x80,KEYCODE_DELETE ,
  43. /* [ESC] ESC [ 3 [;2] ~ - [Alt-][Shift-]Delete */
  44. '[','4','~' |0x80,KEYCODE_END , /* vt100? linux vt? or what? */
  45. '[','5','~' |0x80,KEYCODE_PAGEUP ,
  46. '[','6','~' |0x80,KEYCODE_PAGEDOWN,
  47. '[','7','~' |0x80,KEYCODE_HOME , /* vt100? linux vt? or what? */
  48. '[','8','~' |0x80,KEYCODE_END , /* vt100? linux vt? or what? */
  49. #if 0
  50. '[','1','1','~'|0x80,KEYCODE_FUN1 ,
  51. '[','1','2','~'|0x80,KEYCODE_FUN2 ,
  52. '[','1','3','~'|0x80,KEYCODE_FUN3 ,
  53. '[','1','4','~'|0x80,KEYCODE_FUN4 ,
  54. '[','1','5','~'|0x80,KEYCODE_FUN5 ,
  55. /* [ESC] ESC [ 1 5 [;2] ~ - [Alt-][Shift-]F5 */
  56. '[','1','7','~'|0x80,KEYCODE_FUN6 ,
  57. '[','1','8','~'|0x80,KEYCODE_FUN7 ,
  58. '[','1','9','~'|0x80,KEYCODE_FUN8 ,
  59. '[','2','0','~'|0x80,KEYCODE_FUN9 ,
  60. '[','2','1','~'|0x80,KEYCODE_FUN10 ,
  61. '[','2','3','~'|0x80,KEYCODE_FUN11 ,
  62. '[','2','4','~'|0x80,KEYCODE_FUN12 ,
  63. #endif
  64. 0
  65. };
  66. n = 0;
  67. if (nbuffered)
  68. n = *nbuffered;
  69. if (n == 0) {
  70. /* If no data, block waiting for input. If we read more
  71. * than the minimal ESC sequence size, the "n=0" below
  72. * would instead have to figure out how much to keep,
  73. * resulting in larger code. */
  74. n = safe_read(fd, buffer, 3);
  75. if (n <= 0)
  76. return -1;
  77. }
  78. /* Grab character to return from buffer */
  79. c = (unsigned char)buffer[0];
  80. n--;
  81. if (n)
  82. memmove(buffer, buffer + 1, n);
  83. /* Only ESC starts ESC sequences */
  84. if (c != 27)
  85. goto ret;
  86. /* Loop through known ESC sequences */
  87. pfd.fd = fd;
  88. pfd.events = POLLIN;
  89. seq = esccmds;
  90. while (*seq != '\0') {
  91. /* n - position in sequence we did not read yet */
  92. int i = 0; /* position in sequence to compare */
  93. /* Loop through chars in this sequence */
  94. while (1) {
  95. /* So far escape sequence matched up to [i-1] */
  96. if (n <= i) {
  97. /* Need more chars, read another one if it wouldn't block.
  98. * Note that escape sequences come in as a unit,
  99. * so if we block for long it's not really an escape sequence.
  100. * Timeout is needed to reconnect escape sequences
  101. * split up by transmission over a serial console. */
  102. if (safe_poll(&pfd, 1, 50) == 0) {
  103. /* No more data!
  104. * Array is sorted from shortest to longest,
  105. * we can't match anything later in array,
  106. * break out of both loops. */
  107. goto ret;
  108. }
  109. errno = 0;
  110. if (safe_read(fd, buffer + n, 1) <= 0) {
  111. /* If EAGAIN, then fd is O_NONBLOCK and poll lied:
  112. * in fact, there is no data. */
  113. if (errno != EAGAIN)
  114. c = -1; /* otherwise it's EOF/error */
  115. goto ret;
  116. }
  117. n++;
  118. }
  119. if (buffer[i] != (seq[i] & 0x7f)) {
  120. /* This seq doesn't match, go to next */
  121. seq += i;
  122. /* Forward to last char */
  123. while (!(*seq & 0x80))
  124. seq++;
  125. /* Skip it and the keycode which follows */
  126. seq += 2;
  127. break;
  128. }
  129. if (seq[i] & 0x80) {
  130. /* Entire seq matched */
  131. c = (signed char)seq[i+1];
  132. n = 0;
  133. /* n -= i; memmove(...);
  134. * would be more correct,
  135. * but we never read ahead that much,
  136. * and n == i here. */
  137. goto ret;
  138. }
  139. i++;
  140. }
  141. }
  142. /* We did not find matching sequence, it was a bare ESC.
  143. * We possibly read and stored more input in buffer[]
  144. * by now. */
  145. ret:
  146. if (nbuffered)
  147. *nbuffered = n;
  148. return c;
  149. }