]> git.lizzy.rs Git - linenoise.git/commitdiff
Revert to fgets if stdin is not a tty
authorantirez <antirez@gmail.com>
Fri, 30 Apr 2010 07:19:04 +0000 (09:19 +0200)
committerantirez <antirez@gmail.com>
Fri, 30 Apr 2010 07:19:04 +0000 (09:19 +0200)
linenoise.c

index 1590f734d7aaed1234530faa773bfa4aead0d752..f7f2159fbf3e4a2955509d22b22572731be8e850 100644 (file)
@@ -351,10 +351,19 @@ static int linenoiseRaw(char *buf, size_t buflen, const char *prompt) {
         errno = EINVAL;
         return -1;
     }
-    if (enableRawMode(fd) == -1) return -1;
-    count = linenoisePrompt(fd, buf, buflen, prompt);
-    disableRawMode(fd);
-    printf("\n");
+    if (!isatty(STDIN_FILENO)) {
+        if (fgets(buf, buflen, stdin) == NULL) return -1;
+        count = strlen(buf);
+        if (count && buf[count-1] == '\n') {
+            count--;
+            buf[count] = '\0';
+        }
+    } else {
+        if (enableRawMode(fd) == -1) return -1;
+        count = linenoisePrompt(fd, buf, buflen, prompt);
+        disableRawMode(fd);
+        printf("\n");
+    }
     return count;
 }