Permalink
Browse files

Revert to fgets if stdin is not a tty

  • Loading branch information...
1 parent 6770cf5 commit aca8b8d382c38484fdad403c7f3e5651fba4a181 @antirez committed Apr 30, 2010
Showing with 13 additions and 4 deletions.
  1. +13 −4 linenoise.c
View
@@ -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;
}

0 comments on commit aca8b8d

Please sign in to comment.