read_key.c 8.6 KB

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