lineedit: do not hang on error, but return error indicator.
[oweals/busybox.git] / libbb / read_key.c
index a2253ce3e8f3c6493dd0a939d857ea796fd7cd93..840325c99e99edaa6fc8bf9268269b48f04241f3 100644 (file)
@@ -5,11 +5,11 @@
  * 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;
@@ -90,14 +90,27 @@ int64_t FAST_FUNC read_key(int fd, char *buffer)
                /* 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
@@ -121,8 +134,6 @@ int64_t FAST_FUNC read_key(int fd, char *buffer)
        }
 
        /* 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 */
@@ -203,7 +214,7 @@ int64_t FAST_FUNC read_key(int fd, char *buffer)
                }
                n++;
                /* Try to decipher "ESC [ NNN ; NNN R" sequence */
-               if (ENABLE_FEATURE_EDITING_ASK_TERMINAL
+               if ((ENABLE_FEATURE_EDITING_ASK_TERMINAL || ENABLE_FEATURE_VI_ASK_TERMINAL)
                 && n >= 5
                 && buffer[0] == '['
                 && buffer[n-1] == 'R'
@@ -246,3 +257,12 @@ int64_t FAST_FUNC read_key(int fd, char *buffer)
        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;
+}