From 65c41e6c2b5015ff0b0485aadd362c0883a02a85 Mon Sep 17 00:00:00 2001 From: ZyX Date: Thu, 16 Mar 2017 23:24:20 +0300 Subject: main: Make `-s -` read from stdin --- src/nvim/main.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'src/nvim/main.c') diff --git a/src/nvim/main.c b/src/nvim/main.c index 4416bd067c..99dd9fc18f 100644 --- a/src/nvim/main.c +++ b/src/nvim/main.c @@ -1056,7 +1056,9 @@ scripterror: mch_errmsg("\"\n"); mch_exit(2); } - if ((scriptin[0] = mch_fopen(argv[0], READBIN)) == NULL) { + if (STRCMP(argv[0], "-") == 0) { + scriptin[0] = stdin; + } else if ((scriptin[0] = mch_fopen(argv[0], READBIN)) == NULL) { mch_errmsg(_("Cannot open for reading: \"")); mch_errmsg(argv[0]); mch_errmsg("\"\n"); -- cgit From fdfa1ed578afd41a68f05c88dc419d88051b7240 Mon Sep 17 00:00:00 2001 From: ZyX Date: Sun, 19 Mar 2017 16:09:48 +0300 Subject: main: Temporary fix assertion error This variant uses `fdopen()` which is not standard, but it fixes problem on my system. In next commit `scriptin` will use `FileDescriptor*` from os/fileio in place of `FILE*`. --- src/nvim/main.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'src/nvim/main.c') diff --git a/src/nvim/main.c b/src/nvim/main.c index 99dd9fc18f..ce0426bd8e 100644 --- a/src/nvim/main.c +++ b/src/nvim/main.c @@ -1057,7 +1057,9 @@ scripterror: mch_exit(2); } if (STRCMP(argv[0], "-") == 0) { - scriptin[0] = stdin; + const int stdin_dup_fd = os_dup(STDIN_FILENO); + FILE *const stdin_dup = fdopen(stdin_dup_fd, "r"); + scriptin[0] = stdin_dup; } else if ((scriptin[0] = mch_fopen(argv[0], READBIN)) == NULL) { mch_errmsg(_("Cannot open for reading: \"")); mch_errmsg(argv[0]); -- cgit From bd798a3267a496c644b339c45189b09e2a952014 Mon Sep 17 00:00:00 2001 From: ZyX Date: Sun, 19 Mar 2017 16:55:37 +0300 Subject: getchar: Use fileio instead of fdopen Problem: as fileio is cached and reads blocks this is going to wait until either EOF or reading enough characters to fill rbuffer. This is not good when reading user input from stdin as script. --- src/nvim/main.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) (limited to 'src/nvim/main.c') diff --git a/src/nvim/main.c b/src/nvim/main.c index ce0426bd8e..da3ec4381e 100644 --- a/src/nvim/main.c +++ b/src/nvim/main.c @@ -1056,14 +1056,20 @@ scripterror: mch_errmsg("\"\n"); mch_exit(2); } + int error; if (STRCMP(argv[0], "-") == 0) { - const int stdin_dup_fd = os_dup(STDIN_FILENO); - FILE *const stdin_dup = fdopen(stdin_dup_fd, "r"); + const int stdin_dup_fd = os_dup(OS_STDIN_FILENO); + FileDescriptor *const stdin_dup = file_open_fd_new( + &error, stdin_dup_fd, false, 0); + assert(stdin_dup != NULL); scriptin[0] = stdin_dup; - } else if ((scriptin[0] = mch_fopen(argv[0], READBIN)) == NULL) { + } else if ((scriptin[0] = file_open_new( + &error, argv[0], kFileReadOnly, 0)) == NULL) { mch_errmsg(_("Cannot open for reading: \"")); mch_errmsg(argv[0]); - mch_errmsg("\"\n"); + mch_errmsg("\": "); + mch_errmsg(os_strerror(error)); + mch_errmsg("\n"); mch_exit(2); } save_typebuf(); -- cgit From e78e75d85d91e9f14964465ea136b3899b774d6e Mon Sep 17 00:00:00 2001 From: ZyX Date: Sun, 19 Mar 2017 17:29:48 +0300 Subject: fileio,main: Do not restart syscall at EAGAIN when reading for -s --- src/nvim/main.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/nvim/main.c') diff --git a/src/nvim/main.c b/src/nvim/main.c index da3ec4381e..8114164158 100644 --- a/src/nvim/main.c +++ b/src/nvim/main.c @@ -1060,11 +1060,11 @@ scripterror: if (STRCMP(argv[0], "-") == 0) { const int stdin_dup_fd = os_dup(OS_STDIN_FILENO); FileDescriptor *const stdin_dup = file_open_fd_new( - &error, stdin_dup_fd, false, 0); + &error, stdin_dup_fd, kFileReadOnly|kFileNonBlocking, 0); assert(stdin_dup != NULL); scriptin[0] = stdin_dup; } else if ((scriptin[0] = file_open_new( - &error, argv[0], kFileReadOnly, 0)) == NULL) { + &error, argv[0], kFileReadOnly|kFileNonBlocking, 0)) == NULL) { mch_errmsg(_("Cannot open for reading: \"")); mch_errmsg(argv[0]); mch_errmsg("\": "); -- cgit From ca116df2606a0191f6600acfcd6088d5ce15ce6f Mon Sep 17 00:00:00 2001 From: ZyX Date: Sun, 19 Mar 2017 19:28:16 +0300 Subject: main: Translate full -s error message, not part of it --- src/nvim/main.c | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) (limited to 'src/nvim/main.c') diff --git a/src/nvim/main.c b/src/nvim/main.c index 8114164158..efe7944fa4 100644 --- a/src/nvim/main.c +++ b/src/nvim/main.c @@ -1049,11 +1049,10 @@ static void command_line_scan(mparm_T *parmp) case 's': /* "-s {scriptin}" read from script file */ if (scriptin[0] != NULL) { scripterror: - mch_errmsg(_("Attempt to open script file again: \"")); - mch_errmsg(argv[-1]); - mch_errmsg(" "); - mch_errmsg(argv[0]); - mch_errmsg("\"\n"); + vim_snprintf((char *)IObuff, IOSIZE, + _("Attempt to open script file again: \"%s %s\"\n"), + argv[-1], argv[0]); + mch_errmsg((const char *)IObuff); mch_exit(2); } int error; @@ -1065,11 +1064,10 @@ scripterror: scriptin[0] = stdin_dup; } else if ((scriptin[0] = file_open_new( &error, argv[0], kFileReadOnly|kFileNonBlocking, 0)) == NULL) { - mch_errmsg(_("Cannot open for reading: \"")); - mch_errmsg(argv[0]); - mch_errmsg("\": "); - mch_errmsg(os_strerror(error)); - mch_errmsg("\n"); + vim_snprintf((char *)IObuff, IOSIZE, + _("Cannot open for reading: \"%s\": %s\n"), + argv[0], os_strerror(error)); + mch_errmsg((const char *)IObuff); mch_exit(2); } save_typebuf(); -- cgit From 387fbcd95cade4b0c037d18f404944676a59db09 Mon Sep 17 00:00:00 2001 From: b-r-o-c-k Date: Sat, 14 Apr 2018 14:21:36 -0500 Subject: win: Fix reading from stdin * Reading from stdin on Windows is fixed in the same way as it was in #8267. * The file_read function was returning without filling the destination buffer when it was called with a non-blocking file descriptor. --- src/nvim/main.c | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'src/nvim/main.c') diff --git a/src/nvim/main.c b/src/nvim/main.c index 067184c8c7..4f9a5a979f 100644 --- a/src/nvim/main.c +++ b/src/nvim/main.c @@ -1096,6 +1096,17 @@ scripterror: int error; if (STRCMP(argv[0], "-") == 0) { const int stdin_dup_fd = os_dup(STDIN_FILENO); +#ifdef WIN32 + // On Windows, replace the original stdin with the + // console input handle. + close(STDIN_FILENO); + const HANDLE conin_handle = + CreateFile("CONIN$", GENERIC_READ | GENERIC_WRITE, + FILE_SHARE_READ, (LPSECURITY_ATTRIBUTES)NULL, + OPEN_EXISTING, 0, (HANDLE)NULL); + const int conin_fd = _open_osfhandle(conin_handle, _O_RDONLY); + assert(conin_fd == STDIN_FILENO); +#endif FileDescriptor *const stdin_dup = file_open_fd_new( &error, stdin_dup_fd, kFileReadOnly|kFileNonBlocking); assert(stdin_dup != NULL); -- cgit