* Copyright (C) 2008 Rob Landley <rob@landley.net>
* Copyright (C) 2008 Denys Vlasenko <vda.linux@googlemail.com>
*
- * Licensed under GPL version 2, see file LICENSE in this tarball for details.
+ * Licensed under GPLv2, see file LICENSE in this source tree.
*/
#include "libbb.h"
-int64_t FAST_FUNC read_key(int fd, char *buffer)
+int64_t FAST_FUNC read_key(int fd, char *buffer, int timeout)
{
struct pollfd pfd;
const char *seq;
/* ESC [ Z - Shift-Tab */
};
+ pfd.fd = fd;
+ pfd.events = POLLIN;
+
buffer++; /* saved chars counter is in buffer[-1] now */
start_over:
errno = 0;
n = (unsigned char)buffer[-1];
if (n == 0) {
- /* If no data, block waiting for input.
- * It is tempting to read more than one byte here,
+ /* If no data, wait for input.
+ * If requested, wait TIMEOUT ms. TIMEOUT = -1 is useful
+ * if fd can be in non-blocking mode.
+ */
+ if (timeout >= -1) {
+ if (safe_poll(&pfd, 1, timeout) == 0) {
+ /* Timed out */
+ errno = EAGAIN;
+ return -1;
+ }
+ }
+ /* It is tempting to read more than one byte here,
* but it breaks pasting. Example: at shell prompt,
* user presses "c","a","t" and then pastes "\nline\n".
* When we were reading 3 bytes here, we were eating
}
/* Loop through known ESC sequences */
- pfd.fd = fd;
- pfd.events = POLLIN;
seq = esccmds;
while (*seq != '\0') {
/* n - position in sequence we did not read yet */
break;
}
n++;
+ /* Try to decipher "ESC [ NNN ; NNN R" sequence */
+ if ((ENABLE_FEATURE_EDITING_ASK_TERMINAL || ENABLE_FEATURE_VI_ASK_TERMINAL)
+ && n >= 5
+ && buffer[0] == '['
+ && buffer[n-1] == 'R'
+ && isdigit(buffer[1])
+ ) {
+ char *end;
+ unsigned long row, col;
+
+ row = strtoul(buffer + 1, &end, 10);
+ if (*end != ';' || !isdigit(end[1]))
+ continue;
+ col = strtoul(end + 1, &end, 10);
+ if (*end != 'R')
+ continue;
+ if (row < 1 || col < 1 || (row | col) > 0x7fff)
+ continue;
+
+ buffer[-1] = 0;
+ /* Pack into "1 <row15bits> <col16bits>" 32-bit sequence */
+ col |= (((-1 << 15) | row) << 16);
+ /* Return it in high-order word */
+ return ((int64_t) col << 32) | (uint32_t)KEYCODE_CURSOR_POS;
+ }
}
got_all:
return 27;
}
- /* Try to decipher "ESC [ NNN ; NNN R" sequence */
- if (ENABLE_FEATURE_EDITING_ASK_TERMINAL
- && n >= 5
- && buffer[0] == '['
- && isdigit(buffer[1])
- && buffer[n-1] == 'R'
- ) {
- char *end;
- unsigned long row, col;
-
- row = strtoul(buffer + 1, &end, 10);
- if (*end != ';' || !isdigit(end[1]))
- goto not_R;
- col = strtoul(end + 1, &end, 10);
- if (*end != 'R')
- goto not_R;
- if (row < 1 || col < 1 || (row | col) > 0x7fff)
- goto not_R;
-
- buffer[-1] = 0;
-
- /* Pack into "1 <row15bits> <col16bits>" 32-bit sequence */
- col |= (((-1 << 15) | row) << 16);
- /* Return it in high-order word */
- return ((int64_t) col << 32) | (uint32_t)KEYCODE_CURSOR_POS;
- }
- not_R:
-
/* We were doing "buffer[-1] = n; return c;" here, but this results
* in unknown key sequences being interpreted as ESC + garbage.
* This was not useful. Pretend there was no key pressed,
buffer[-1] = 0;
goto start_over;
}
+
+void FAST_FUNC read_key_ungets(char *buffer, const char *str, unsigned len)
+{
+ unsigned cur_len = (unsigned char)buffer[0];
+ if (len > KEYCODE_BUFFER_SIZE-1 - cur_len)
+ len = KEYCODE_BUFFER_SIZE-1 - cur_len;
+ memcpy(buffer + 1 + cur_len, str, len);
+ buffer[0] += len;
+}