3
0

read_key.c 9.5 KB

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