Denis Vlasenko | 39b0135 | 2008-10-25 23:23:32 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Utility routines. |
| 4 | * |
| 5 | * Copyright (C) 2008 Denys Vlasenko |
| 6 | * |
| 7 | * Licensed under GPL version 2, see file LICENSE in this tarball for details. |
| 8 | */ |
| 9 | #include "libbb.h" |
| 10 | |
| 11 | int FAST_FUNC read_key(int fd, smalluint *nbuffered, char *buffer) |
| 12 | { |
| 13 | struct pollfd pfd; |
| 14 | const char *seq; |
| 15 | int n; |
| 16 | int c; |
| 17 | |
| 18 | /* Known escape sequences for cursor and function keys */ |
| 19 | static const char esccmds[] ALIGN1 = { |
| 20 | 'O','A' |0x80,KEYCODE_UP , |
| 21 | 'O','B' |0x80,KEYCODE_DOWN , |
| 22 | 'O','C' |0x80,KEYCODE_RIGHT , |
| 23 | 'O','D' |0x80,KEYCODE_LEFT , |
| 24 | 'O','H' |0x80,KEYCODE_HOME , |
| 25 | 'O','F' |0x80,KEYCODE_END , |
| 26 | #if 0 |
| 27 | 'O','P' |0x80,KEYCODE_FUN1 , |
Denis Vlasenko | bdd2d8f | 2008-10-25 23:59:41 +0000 | [diff] [blame^] | 28 | /* [ESC] ESC O [2] P - [Alt-][Shift-]F1 */ |
| 29 | /* Ctrl seem to not affect sequences */ |
Denis Vlasenko | 39b0135 | 2008-10-25 23:23:32 +0000 | [diff] [blame] | 30 | 'O','Q' |0x80,KEYCODE_FUN2 , |
| 31 | 'O','R' |0x80,KEYCODE_FUN3 , |
| 32 | 'O','S' |0x80,KEYCODE_FUN4 , |
| 33 | #endif |
| 34 | '[','A' |0x80,KEYCODE_UP , |
| 35 | '[','B' |0x80,KEYCODE_DOWN , |
| 36 | '[','C' |0x80,KEYCODE_RIGHT , |
| 37 | '[','D' |0x80,KEYCODE_LEFT , |
Denis Vlasenko | 5f6aaf3 | 2008-10-25 23:27:29 +0000 | [diff] [blame] | 38 | '[','H' |0x80,KEYCODE_HOME , /* xterm */ |
Denis Vlasenko | bdd2d8f | 2008-10-25 23:59:41 +0000 | [diff] [blame^] | 39 | /* [ESC] ESC [ [2] H - [Alt-][Shift-]Home */ |
Denis Vlasenko | 5f6aaf3 | 2008-10-25 23:27:29 +0000 | [diff] [blame] | 40 | '[','F' |0x80,KEYCODE_END , /* xterm */ |
| 41 | '[','1','~' |0x80,KEYCODE_HOME , /* vt100? linux vt? or what? */ |
Denis Vlasenko | 39b0135 | 2008-10-25 23:23:32 +0000 | [diff] [blame] | 42 | '[','2','~' |0x80,KEYCODE_INSERT , |
| 43 | '[','3','~' |0x80,KEYCODE_DELETE , |
Denis Vlasenko | bdd2d8f | 2008-10-25 23:59:41 +0000 | [diff] [blame^] | 44 | /* [ESC] ESC [ 3 [;2] ~ - [Alt-][Shift-]Delete */ |
Denis Vlasenko | 5f6aaf3 | 2008-10-25 23:27:29 +0000 | [diff] [blame] | 45 | '[','4','~' |0x80,KEYCODE_END , /* vt100? linux vt? or what? */ |
Denis Vlasenko | 39b0135 | 2008-10-25 23:23:32 +0000 | [diff] [blame] | 46 | '[','5','~' |0x80,KEYCODE_PAGEUP , |
| 47 | '[','6','~' |0x80,KEYCODE_PAGEDOWN, |
Denis Vlasenko | 5f6aaf3 | 2008-10-25 23:27:29 +0000 | [diff] [blame] | 48 | '[','7','~' |0x80,KEYCODE_HOME , /* vt100? linux vt? or what? */ |
| 49 | '[','8','~' |0x80,KEYCODE_END , /* vt100? linux vt? or what? */ |
Denis Vlasenko | 39b0135 | 2008-10-25 23:23:32 +0000 | [diff] [blame] | 50 | #if 0 |
| 51 | '[','1','1','~'|0x80,KEYCODE_FUN1 , |
| 52 | '[','1','2','~'|0x80,KEYCODE_FUN2 , |
| 53 | '[','1','3','~'|0x80,KEYCODE_FUN3 , |
| 54 | '[','1','4','~'|0x80,KEYCODE_FUN4 , |
| 55 | '[','1','5','~'|0x80,KEYCODE_FUN5 , |
Denis Vlasenko | bdd2d8f | 2008-10-25 23:59:41 +0000 | [diff] [blame^] | 56 | /* [ESC] ESC [ 1 5 [;2] ~ - [Alt-][Shift-]F5 */ |
Denis Vlasenko | 39b0135 | 2008-10-25 23:23:32 +0000 | [diff] [blame] | 57 | '[','1','7','~'|0x80,KEYCODE_FUN6 , |
| 58 | '[','1','8','~'|0x80,KEYCODE_FUN7 , |
| 59 | '[','1','9','~'|0x80,KEYCODE_FUN8 , |
| 60 | '[','2','0','~'|0x80,KEYCODE_FUN9 , |
| 61 | '[','2','1','~'|0x80,KEYCODE_FUN10 , |
| 62 | '[','2','3','~'|0x80,KEYCODE_FUN11 , |
| 63 | '[','2','4','~'|0x80,KEYCODE_FUN12 , |
| 64 | #endif |
| 65 | 0 |
| 66 | }; |
| 67 | |
Denis Vlasenko | 5f6aaf3 | 2008-10-25 23:27:29 +0000 | [diff] [blame] | 68 | n = 0; |
| 69 | if (nbuffered) |
| 70 | n = *nbuffered; |
Denis Vlasenko | 39b0135 | 2008-10-25 23:23:32 +0000 | [diff] [blame] | 71 | if (n == 0) { |
| 72 | /* If no data, block waiting for input. If we read more |
| 73 | * than the minimal ESC sequence size, the "n=0" below |
| 74 | * would instead have to figure out how much to keep, |
| 75 | * resulting in larger code. */ |
| 76 | n = safe_read(fd, buffer, 3); |
| 77 | if (n <= 0) |
| 78 | return -1; |
| 79 | } |
| 80 | |
| 81 | /* Grab character to return from buffer */ |
| 82 | c = (unsigned char)buffer[0]; |
| 83 | n--; |
| 84 | if (n) |
| 85 | memmove(buffer, buffer + 1, n); |
| 86 | |
| 87 | /* Only ESC starts ESC sequences */ |
| 88 | if (c != 27) |
| 89 | goto ret; |
| 90 | |
| 91 | /* Loop through known ESC sequences */ |
| 92 | pfd.fd = fd; |
| 93 | pfd.events = POLLIN; |
| 94 | seq = esccmds; |
| 95 | while (*seq != '\0') { |
| 96 | /* n - position in sequence we did not read yet */ |
| 97 | int i = 0; /* position in sequence to compare */ |
| 98 | |
| 99 | /* Loop through chars in this sequence */ |
| 100 | while (1) { |
| 101 | /* So far escape sequence matched up to [i-1] */ |
| 102 | if (n <= i) { |
| 103 | /* Need more chars, read another one if it wouldn't block. |
| 104 | * Note that escape sequences come in as a unit, |
| 105 | * so if we block for long it's not really an escape sequence. |
| 106 | * Timeout is needed to reconnect escape sequences |
| 107 | * split up by transmission over a serial console. */ |
| 108 | if (safe_poll(&pfd, 1, 50) == 0) { |
| 109 | /* No more data! |
| 110 | * Array is sorted from shortest to longest, |
| 111 | * we can't match anything later in array, |
| 112 | * break out of both loops. */ |
| 113 | goto ret; |
| 114 | } |
| 115 | errno = 0; |
| 116 | if (safe_read(fd, buffer + n, 1) <= 0) { |
| 117 | /* If EAGAIN, then fd is O_NONBLOCK and poll lied: |
| 118 | * in fact, there is no data. */ |
| 119 | if (errno != EAGAIN) |
| 120 | c = -1; /* otherwise it's EOF/error */ |
| 121 | goto ret; |
| 122 | } |
| 123 | n++; |
| 124 | } |
| 125 | if (buffer[i] != (seq[i] & 0x7f)) { |
| 126 | /* This seq doesn't match, go to next */ |
| 127 | seq += i; |
| 128 | /* Forward to last char */ |
| 129 | while (!(*seq & 0x80)) |
| 130 | seq++; |
| 131 | /* Skip it and the keycode which follows */ |
| 132 | seq += 2; |
| 133 | break; |
| 134 | } |
| 135 | if (seq[i] & 0x80) { |
| 136 | /* Entire seq matched */ |
| 137 | c = (signed char)seq[i+1]; |
| 138 | n = 0; |
| 139 | /* n -= i; memmove(...); |
| 140 | * would be more correct, |
| 141 | * but we never read ahead that much, |
| 142 | * and n == i here. */ |
| 143 | goto ret; |
| 144 | } |
| 145 | i++; |
| 146 | } |
| 147 | } |
| 148 | /* We did not find matching sequence, it was a bare ESC. |
| 149 | * We possibly read and stored more input in buffer[] |
| 150 | * by now. */ |
| 151 | |
| 152 | ret: |
Denis Vlasenko | 5f6aaf3 | 2008-10-25 23:27:29 +0000 | [diff] [blame] | 153 | if (nbuffered) |
| 154 | *nbuffered = n; |
Denis Vlasenko | 39b0135 | 2008-10-25 23:23:32 +0000 | [diff] [blame] | 155 | return c; |
| 156 | } |