read_key.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. int64_t FAST_FUNC read_key(int fd, 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. errno = 0;
  67. n = (unsigned char) *buffer++;
  68. if (n == 0) {
  69. /* If no data, block waiting for input.
  70. * It is tempting to read more than one byte here,
  71. * but it breaks pasting. Example: at shell prompt,
  72. * user presses "c","a","t" and then pastes "\nline\n".
  73. * When we were reading 3 bytes here, we were eating
  74. * "li" too, and cat was getting wrong input.
  75. */
  76. n = safe_read(fd, buffer, 1);
  77. if (n <= 0)
  78. return -1;
  79. }
  80. /* Grab character to return from buffer */
  81. c = (unsigned char)buffer[0];
  82. n--;
  83. if (n)
  84. memmove(buffer, buffer + 1, n);
  85. /* Only ESC starts ESC sequences */
  86. if (c != 27)
  87. goto ret;
  88. /* Loop through known ESC sequences */
  89. pfd.fd = fd;
  90. pfd.events = POLLIN;
  91. seq = esccmds;
  92. while (*seq != '\0') {
  93. /* n - position in sequence we did not read yet */
  94. int i = 0; /* position in sequence to compare */
  95. /* Loop through chars in this sequence */
  96. while (1) {
  97. /* So far escape sequence matched up to [i-1] */
  98. if (n <= i) {
  99. /* Need more chars, read another one if it wouldn't block.
  100. * Note that escape sequences come in as a unit,
  101. * so if we block for long it's not really an escape sequence.
  102. * Timeout is needed to reconnect escape sequences
  103. * split up by transmission over a serial console. */
  104. if (safe_poll(&pfd, 1, 50) == 0) {
  105. /* No more data!
  106. * Array is sorted from shortest to longest,
  107. * we can't match anything later in array,
  108. * break out of both loops. */
  109. goto ret;
  110. }
  111. errno = 0;
  112. if (safe_read(fd, buffer + n, 1) <= 0) {
  113. /* If EAGAIN, then fd is O_NONBLOCK and poll lied:
  114. * in fact, there is no data. */
  115. if (errno != EAGAIN)
  116. c = -1; /* otherwise it's EOF/error */
  117. goto ret;
  118. }
  119. n++;
  120. }
  121. if (buffer[i] != (seq[i] & 0x7f)) {
  122. /* This seq doesn't match, go to next */
  123. seq += i;
  124. /* Forward to last char */
  125. while (!(*seq & 0x80))
  126. seq++;
  127. /* Skip it and the keycode which follows */
  128. seq += 2;
  129. break;
  130. }
  131. if (seq[i] & 0x80) {
  132. /* Entire seq matched */
  133. c = (signed char)seq[i+1];
  134. n = 0;
  135. /* n -= i; memmove(...);
  136. * would be more correct,
  137. * but we never read ahead that much,
  138. * and n == i here. */
  139. goto ret;
  140. }
  141. i++;
  142. }
  143. }
  144. /* We did not find matching sequence, it was a bare ESC.
  145. * We possibly read and stored more input in buffer[] by now. */
  146. /* Try to decipher "ESC [ NNN ; NNN R" sequence */
  147. if (ENABLE_FEATURE_EDITING_ASK_TERMINAL
  148. && n != 0
  149. && buffer[0] == '['
  150. ) {
  151. char *end;
  152. unsigned long row, col;
  153. while (n < KEYCODE_BUFFER_SIZE-1) { /* 1 for cnt */
  154. if (safe_poll(&pfd, 1, 50) == 0) {
  155. /* No more data! */
  156. break;
  157. }
  158. errno = 0;
  159. if (safe_read(fd, buffer + n, 1) <= 0) {
  160. /* If EAGAIN, then fd is O_NONBLOCK and poll lied:
  161. * in fact, there is no data. */
  162. if (errno != EAGAIN)
  163. c = -1; /* otherwise it's EOF/error */
  164. goto ret;
  165. }
  166. if (buffer[n++] == 'R')
  167. goto got_R;
  168. }
  169. goto ret;
  170. got_R:
  171. if (!isdigit(buffer[1]))
  172. goto ret;
  173. row = strtoul(buffer + 1, &end, 10);
  174. if (*end != ';' || !isdigit(end[1]))
  175. goto ret;
  176. col = strtoul(end + 1, &end, 10);
  177. if (*end != 'R')
  178. goto ret;
  179. if (row < 1 || col < 1 || (row | col) > 0x7fff)
  180. goto ret;
  181. buffer[-1] = 0;
  182. /* Pack into "1 <row15bits> <col16bits>" 32-bit sequence */
  183. c = (((-1 << 15) | row) << 16) | col;
  184. /* Return it in high-order word */
  185. return ((int64_t) c << 32) | (uint32_t)KEYCODE_CURSOR_POS;
  186. }
  187. ret:
  188. buffer[-1] = n;
  189. return c;
  190. }