read_key.c 9.2 KB

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