read_key.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. /* Known escape sequences for cursor and function keys */
  17. static const char esccmds[] ALIGN1 = {
  18. 'O','A' |0x80,KEYCODE_UP ,
  19. 'O','B' |0x80,KEYCODE_DOWN ,
  20. 'O','C' |0x80,KEYCODE_RIGHT ,
  21. 'O','D' |0x80,KEYCODE_LEFT ,
  22. 'O','H' |0x80,KEYCODE_HOME ,
  23. 'O','F' |0x80,KEYCODE_END ,
  24. #if 0
  25. 'O','P' |0x80,KEYCODE_FUN1 ,
  26. /* [ESC] ESC O [2] P - [Alt-][Shift-]F1 */
  27. /* ESC [ O 1 ; 2 P - Shift-F1 */
  28. /* ESC [ O 1 ; 3 P - Alt-F1 */
  29. /* ESC [ O 1 ; 4 P - Alt-Shift-F1 */
  30. /* ESC [ O 1 ; 5 P - Ctrl-F1 */
  31. /* ESC [ O 1 ; 6 P - Ctrl-Shift-F1 */
  32. 'O','Q' |0x80,KEYCODE_FUN2 ,
  33. 'O','R' |0x80,KEYCODE_FUN3 ,
  34. 'O','S' |0x80,KEYCODE_FUN4 ,
  35. #endif
  36. '[','A' |0x80,KEYCODE_UP ,
  37. '[','B' |0x80,KEYCODE_DOWN ,
  38. '[','C' |0x80,KEYCODE_RIGHT ,
  39. '[','D' |0x80,KEYCODE_LEFT ,
  40. /* ESC [ 1 ; 2 x, where x = A/B/C/D: Shift-<arrow> */
  41. /* ESC [ 1 ; 3 x, where x = A/B/C/D: Alt-<arrow> */
  42. /* ESC [ 1 ; 4 x, where x = A/B/C/D: Alt-Shift-<arrow> */
  43. /* ESC [ 1 ; 5 x, where x = A/B/C/D: Ctrl-<arrow> - implemented below */
  44. /* ESC [ 1 ; 6 x, where x = A/B/C/D: Ctrl-Shift-<arrow> */
  45. '[','H' |0x80,KEYCODE_HOME , /* xterm */
  46. /* [ESC] ESC [ [2] H - [Alt-][Shift-]Home */
  47. '[','F' |0x80,KEYCODE_END , /* xterm */
  48. '[','1','~' |0x80,KEYCODE_HOME , /* vt100? linux vt? or what? */
  49. '[','2','~' |0x80,KEYCODE_INSERT ,
  50. /* ESC [ 2 ; 3 ~ - Alt-Insert */
  51. '[','3','~' |0x80,KEYCODE_DELETE ,
  52. /* [ESC] ESC [ 3 [;2] ~ - [Alt-][Shift-]Delete */
  53. /* ESC [ 3 ; 3 ~ - Alt-Delete */
  54. /* ESC [ 3 ; 5 ~ - Ctrl-Delete */
  55. '[','4','~' |0x80,KEYCODE_END , /* vt100? linux vt? or what? */
  56. '[','5','~' |0x80,KEYCODE_PAGEUP ,
  57. /* ESC [ 5 ; 3 ~ - Alt-PgUp */
  58. /* ESC [ 5 ; 5 ~ - Ctrl-PgUp */
  59. /* ESC [ 5 ; 7 ~ - Ctrl-Alt-PgUp */
  60. '[','6','~' |0x80,KEYCODE_PAGEDOWN,
  61. '[','7','~' |0x80,KEYCODE_HOME , /* vt100? linux vt? or what? */
  62. '[','8','~' |0x80,KEYCODE_END , /* vt100? linux vt? or what? */
  63. #if 0
  64. '[','1','1','~'|0x80,KEYCODE_FUN1 ,
  65. '[','1','2','~'|0x80,KEYCODE_FUN2 ,
  66. '[','1','3','~'|0x80,KEYCODE_FUN3 ,
  67. '[','1','4','~'|0x80,KEYCODE_FUN4 ,
  68. '[','1','5','~'|0x80,KEYCODE_FUN5 ,
  69. /* [ESC] ESC [ 1 5 [;2] ~ - [Alt-][Shift-]F5 */
  70. '[','1','7','~'|0x80,KEYCODE_FUN6 ,
  71. '[','1','8','~'|0x80,KEYCODE_FUN7 ,
  72. '[','1','9','~'|0x80,KEYCODE_FUN8 ,
  73. '[','2','0','~'|0x80,KEYCODE_FUN9 ,
  74. '[','2','1','~'|0x80,KEYCODE_FUN10 ,
  75. '[','2','3','~'|0x80,KEYCODE_FUN11 ,
  76. '[','2','4','~'|0x80,KEYCODE_FUN12 ,
  77. /* ESC [ 2 4 ; 2 ~ - Shift-F12 */
  78. /* ESC [ 2 4 ; 3 ~ - Alt-F12 */
  79. /* ESC [ 2 4 ; 4 ~ - Alt-Shift-F12 */
  80. /* ESC [ 2 4 ; 5 ~ - Ctrl-F12 */
  81. /* ESC [ 2 4 ; 6 ~ - Ctrl-Shift-F12 */
  82. #endif
  83. /* '[','1',';','5','A' |0x80,KEYCODE_CTRL_UP , - unused */
  84. /* '[','1',';','5','B' |0x80,KEYCODE_CTRL_DOWN , - unused */
  85. '[','1',';','5','C' |0x80,KEYCODE_CTRL_RIGHT,
  86. '[','1',';','5','D' |0x80,KEYCODE_CTRL_LEFT ,
  87. 0
  88. /* ESC [ Z - Shift-Tab */
  89. };
  90. buffer++; /* saved chars counter is in buffer[-1] now */
  91. start_over:
  92. errno = 0;
  93. n = (unsigned char)buffer[-1];
  94. if (n == 0) {
  95. /* If no data, block waiting for input.
  96. * It is tempting to read more than one byte here,
  97. * but it breaks pasting. Example: at shell prompt,
  98. * user presses "c","a","t" and then pastes "\nline\n".
  99. * When we were reading 3 bytes here, we were eating
  100. * "li" too, and cat was getting wrong input.
  101. */
  102. n = safe_read(fd, buffer, 1);
  103. if (n <= 0)
  104. return -1;
  105. }
  106. {
  107. unsigned char c = buffer[0];
  108. n--;
  109. if (n)
  110. memmove(buffer, buffer + 1, n);
  111. /* Only ESC starts ESC sequences */
  112. if (c != 27) {
  113. buffer[-1] = n;
  114. return c;
  115. }
  116. }
  117. /* Loop through known ESC sequences */
  118. pfd.fd = fd;
  119. pfd.events = POLLIN;
  120. seq = esccmds;
  121. while (*seq != '\0') {
  122. /* n - position in sequence we did not read yet */
  123. int i = 0; /* position in sequence to compare */
  124. /* Loop through chars in this sequence */
  125. while (1) {
  126. /* So far escape sequence matched up to [i-1] */
  127. if (n <= i) {
  128. /* Need more chars, read another one if it wouldn't block.
  129. * Note that escape sequences come in as a unit,
  130. * so if we block for long it's not really an escape sequence.
  131. * Timeout is needed to reconnect escape sequences
  132. * split up by transmission over a serial console. */
  133. if (safe_poll(&pfd, 1, 50) == 0) {
  134. /* No more data!
  135. * Array is sorted from shortest to longest,
  136. * we can't match anything later in array -
  137. * anything later is longer than this seq.
  138. * Break out of both loops. */
  139. goto got_all;
  140. }
  141. errno = 0;
  142. if (safe_read(fd, buffer + n, 1) <= 0) {
  143. /* If EAGAIN, then fd is O_NONBLOCK and poll lied:
  144. * in fact, there is no data. */
  145. if (errno != EAGAIN) {
  146. /* otherwise: it's EOF/error */
  147. buffer[-1] = 0;
  148. return -1;
  149. }
  150. goto got_all;
  151. }
  152. n++;
  153. }
  154. if (buffer[i] != (seq[i] & 0x7f)) {
  155. /* This seq doesn't match, go to next */
  156. seq += i;
  157. /* Forward to last char */
  158. while (!(*seq & 0x80))
  159. seq++;
  160. /* Skip it and the keycode which follows */
  161. seq += 2;
  162. break;
  163. }
  164. if (seq[i] & 0x80) {
  165. /* Entire seq matched */
  166. n = 0;
  167. /* n -= i; memmove(...);
  168. * would be more correct,
  169. * but we never read ahead that much,
  170. * and n == i here. */
  171. buffer[-1] = 0;
  172. return (signed char)seq[i+1];
  173. }
  174. i++;
  175. }
  176. }
  177. /* We did not find matching sequence.
  178. * We possibly read and stored more input in buffer[] by now.
  179. * n = bytes read. Try to read more until we time out.
  180. */
  181. while (n < KEYCODE_BUFFER_SIZE-1) { /* 1 for count byte at buffer[-1] */
  182. if (safe_poll(&pfd, 1, 50) == 0) {
  183. /* No more data! */
  184. break;
  185. }
  186. errno = 0;
  187. if (safe_read(fd, buffer + n, 1) <= 0) {
  188. /* If EAGAIN, then fd is O_NONBLOCK and poll lied:
  189. * in fact, there is no data. */
  190. if (errno != EAGAIN) {
  191. /* otherwise: it's EOF/error */
  192. buffer[-1] = 0;
  193. return -1;
  194. }
  195. break;
  196. }
  197. n++;
  198. /* Try to decipher "ESC [ NNN ; NNN R" sequence */
  199. if (ENABLE_FEATURE_EDITING_ASK_TERMINAL
  200. && n >= 5
  201. && buffer[0] == '['
  202. && buffer[n-1] == 'R'
  203. && isdigit(buffer[1])
  204. ) {
  205. char *end;
  206. unsigned long row, col;
  207. row = strtoul(buffer + 1, &end, 10);
  208. if (*end != ';' || !isdigit(end[1]))
  209. continue;
  210. col = strtoul(end + 1, &end, 10);
  211. if (*end != 'R')
  212. continue;
  213. if (row < 1 || col < 1 || (row | col) > 0x7fff)
  214. continue;
  215. buffer[-1] = 0;
  216. /* Pack into "1 <row15bits> <col16bits>" 32-bit sequence */
  217. col |= (((-1 << 15) | row) << 16);
  218. /* Return it in high-order word */
  219. return ((int64_t) col << 32) | (uint32_t)KEYCODE_CURSOR_POS;
  220. }
  221. }
  222. got_all:
  223. if (n <= 1) {
  224. /* Alt-x is usually returned as ESC x.
  225. * Report ESC, x is remembered for the next call.
  226. */
  227. buffer[-1] = n;
  228. return 27;
  229. }
  230. /* We were doing "buffer[-1] = n; return c;" here, but this results
  231. * in unknown key sequences being interpreted as ESC + garbage.
  232. * This was not useful. Pretend there was no key pressed,
  233. * go and wait for a new keypress:
  234. */
  235. buffer[-1] = 0;
  236. goto start_over;
  237. }