read_key.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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, int timeout)
  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. pfd.fd = fd;
  91. pfd.events = POLLIN;
  92. buffer++; /* saved chars counter is in buffer[-1] now */
  93. start_over:
  94. errno = 0;
  95. n = (unsigned char)buffer[-1];
  96. if (n == 0) {
  97. /* If no data, wait for input.
  98. * If requested, wait TIMEOUT ms. TIMEOUT = -1 is useful
  99. * if fd can be in non-blocking mode.
  100. */
  101. if (timeout >= -1) {
  102. if (safe_poll(&pfd, 1, timeout) == 0) {
  103. /* Timed out */
  104. errno = EAGAIN;
  105. return -1;
  106. }
  107. }
  108. /* It is tempting to read more than one byte here,
  109. * but it breaks pasting. Example: at shell prompt,
  110. * user presses "c","a","t" and then pastes "\nline\n".
  111. * When we were reading 3 bytes here, we were eating
  112. * "li" too, and cat was getting wrong input.
  113. */
  114. n = safe_read(fd, buffer, 1);
  115. if (n <= 0)
  116. return -1;
  117. }
  118. {
  119. unsigned char c = buffer[0];
  120. n--;
  121. if (n)
  122. memmove(buffer, buffer + 1, n);
  123. /* Only ESC starts ESC sequences */
  124. if (c != 27) {
  125. buffer[-1] = n;
  126. return c;
  127. }
  128. }
  129. /* Loop through known ESC sequences */
  130. seq = esccmds;
  131. while (*seq != '\0') {
  132. /* n - position in sequence we did not read yet */
  133. int i = 0; /* position in sequence to compare */
  134. /* Loop through chars in this sequence */
  135. while (1) {
  136. /* So far escape sequence matched up to [i-1] */
  137. if (n <= i) {
  138. /* Need more chars, read another one if it wouldn't block.
  139. * Note that escape sequences come in as a unit,
  140. * so if we block for long it's not really an escape sequence.
  141. * Timeout is needed to reconnect escape sequences
  142. * split up by transmission over a serial console. */
  143. if (safe_poll(&pfd, 1, 50) == 0) {
  144. /* No more data!
  145. * Array is sorted from shortest to longest,
  146. * we can't match anything later in array -
  147. * anything later is longer than this seq.
  148. * Break out of both loops. */
  149. goto got_all;
  150. }
  151. errno = 0;
  152. if (safe_read(fd, buffer + n, 1) <= 0) {
  153. /* If EAGAIN, then fd is O_NONBLOCK and poll lied:
  154. * in fact, there is no data. */
  155. if (errno != EAGAIN) {
  156. /* otherwise: it's EOF/error */
  157. buffer[-1] = 0;
  158. return -1;
  159. }
  160. goto got_all;
  161. }
  162. n++;
  163. }
  164. if (buffer[i] != (seq[i] & 0x7f)) {
  165. /* This seq doesn't match, go to next */
  166. seq += i;
  167. /* Forward to last char */
  168. while (!(*seq & 0x80))
  169. seq++;
  170. /* Skip it and the keycode which follows */
  171. seq += 2;
  172. break;
  173. }
  174. if (seq[i] & 0x80) {
  175. /* Entire seq matched */
  176. n = 0;
  177. /* n -= i; memmove(...);
  178. * would be more correct,
  179. * but we never read ahead that much,
  180. * and n == i here. */
  181. buffer[-1] = 0;
  182. return (signed char)seq[i+1];
  183. }
  184. i++;
  185. }
  186. }
  187. /* We did not find matching sequence.
  188. * We possibly read and stored more input in buffer[] by now.
  189. * n = bytes read. Try to read more until we time out.
  190. */
  191. while (n < KEYCODE_BUFFER_SIZE-1) { /* 1 for count byte at buffer[-1] */
  192. if (safe_poll(&pfd, 1, 50) == 0) {
  193. /* No more data! */
  194. break;
  195. }
  196. errno = 0;
  197. if (safe_read(fd, buffer + n, 1) <= 0) {
  198. /* If EAGAIN, then fd is O_NONBLOCK and poll lied:
  199. * in fact, there is no data. */
  200. if (errno != EAGAIN) {
  201. /* otherwise: it's EOF/error */
  202. buffer[-1] = 0;
  203. return -1;
  204. }
  205. break;
  206. }
  207. n++;
  208. /* Try to decipher "ESC [ NNN ; NNN R" sequence */
  209. if ((ENABLE_FEATURE_EDITING_ASK_TERMINAL || ENABLE_FEATURE_VI_ASK_TERMINAL)
  210. && n >= 5
  211. && buffer[0] == '['
  212. && buffer[n-1] == 'R'
  213. && isdigit(buffer[1])
  214. ) {
  215. char *end;
  216. unsigned long row, col;
  217. row = strtoul(buffer + 1, &end, 10);
  218. if (*end != ';' || !isdigit(end[1]))
  219. continue;
  220. col = strtoul(end + 1, &end, 10);
  221. if (*end != 'R')
  222. continue;
  223. if (row < 1 || col < 1 || (row | col) > 0x7fff)
  224. continue;
  225. buffer[-1] = 0;
  226. /* Pack into "1 <row15bits> <col16bits>" 32-bit sequence */
  227. col |= (((-1 << 15) | row) << 16);
  228. /* Return it in high-order word */
  229. return ((int64_t) col << 32) | (uint32_t)KEYCODE_CURSOR_POS;
  230. }
  231. }
  232. got_all:
  233. if (n <= 1) {
  234. /* Alt-x is usually returned as ESC x.
  235. * Report ESC, x is remembered for the next call.
  236. */
  237. buffer[-1] = n;
  238. return 27;
  239. }
  240. /* We were doing "buffer[-1] = n; return c;" here, but this results
  241. * in unknown key sequences being interpreted as ESC + garbage.
  242. * This was not useful. Pretend there was no key pressed,
  243. * go and wait for a new keypress:
  244. */
  245. buffer[-1] = 0;
  246. goto start_over;
  247. }
  248. void FAST_FUNC read_key_ungets(char *buffer, const char *str, unsigned len)
  249. {
  250. unsigned cur_len = (unsigned char)buffer[0];
  251. if (len > KEYCODE_BUFFER_SIZE-1 - cur_len)
  252. len = KEYCODE_BUFFER_SIZE-1 - cur_len;
  253. memcpy(buffer + 1 + cur_len, str, len);
  254. buffer[0] += len;
  255. }