read_key.c 8.3 KB

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