Commit aca8b8d382c38484fdad403c7f3e5651fba4a181

antirez 2010-04-30T09:19:04

Revert to fgets if stdin is not a tty

diff --git a/linenoise.c b/linenoise.c
index 1590f73..f7f2159 100644
--- a/linenoise.c
+++ b/linenoise.c
@@ -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;
 }