Add stdin, stdout replacement functions

This commit is contained in:
erw7 2019-11-16 13:45:37 +09:00 committed by Björn Linse
parent 60c7eabb2f
commit 2c8016c704
6 changed files with 61 additions and 30 deletions

View File

@ -137,6 +137,9 @@ foreach(sfile ${NVIM_SOURCES})
if(NOT WIN32 AND ${f} MATCHES "^(os_win_conpty.c)$")
list(APPEND to_remove ${sfile})
endif()
if(NOT WIN32 AND ${f} MATCHES "^(os_win_console.c)$")
list(APPEND to_remove ${sfile})
endif()
endforeach()
list(REMOVE_ITEM NVIM_SOURCES ${to_remove})
@ -641,6 +644,7 @@ set(NO_SINGLE_CHECK_HEADERS
os/win_defs.h
os/pty_process_win.h
os/os_win_conpty.h
os/os_win_console.h
)
foreach(hfile ${NVIM_HEADERS})
get_test_target(test-includes "${hfile}" relative_path texe)

View File

@ -13,6 +13,7 @@
#include "nvim/os/shell.h"
#ifdef WIN32
# include "nvim/os/os_win_conpty.h"
# include "nvim/os/os_win_console.h"
#endif
#include "nvim/path.h"
#include "nvim/ascii.h"
@ -479,23 +480,9 @@ uint64_t channel_from_stdio(bool rpc, CallbackReader on_output,
// stdin and stdout with CONIN$ and CONOUT$, respectively.
if (embedded_mode && os_has_conpty_working()) {
stdin_dup_fd = os_dup(STDIN_FILENO);
close(STDIN_FILENO);
const HANDLE conin_handle =
CreateFile("CONIN$", GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ, (LPSECURITY_ATTRIBUTES)NULL,
OPEN_EXISTING, 0, (HANDLE)NULL);
assert(conin_handle != INVALID_HANDLE_VALUE);
const int conin_fd = _open_osfhandle((intptr_t)conin_handle, _O_RDONLY);
assert(conin_fd == STDIN_FILENO);
os_replace_stdin_to_conin();
stdout_dup_fd = os_dup(STDOUT_FILENO);
close(STDOUT_FILENO);
const HANDLE conout_handle =
CreateFile("CONOUT$", GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ, (LPSECURITY_ATTRIBUTES)NULL,
OPEN_EXISTING, 0, (HANDLE)NULL);
assert(conout_handle != INVALID_HANDLE_VALUE);
const int conout_fd = _open_osfhandle((intptr_t)conout_handle, 0);
assert(conout_fd == STDOUT_FILENO);
os_replace_stdout_to_conout();
}
#endif
rstream_init_fd(&main_loop, &channel->stream.stdio.in, stdin_dup_fd, 0);

View File

@ -64,6 +64,9 @@
#include "nvim/os/os.h"
#include "nvim/os/time.h"
#include "nvim/os/fileio.h"
#ifdef WIN32
# include "nvim/os/os_win_console.h"
#endif
#include "nvim/event/loop.h"
#include "nvim/os/signal.h"
#include "nvim/event/process.h"
@ -1120,13 +1123,7 @@ scripterror:
const int stdin_dup_fd = os_dup(STDIN_FILENO);
#ifdef WIN32
// 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);
os_replace_stdin_to_conin();
#endif
FileDescriptor *const stdin_dup = file_open_fd_new(
&error, stdin_dup_fd, kFileReadOnly|kFileNonBlocking);

View File

@ -0,0 +1,38 @@
// This is an open source non-commercial project. Dear PVS-Studio, please check
// it. PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
#include "nvim/vim.h"
int os_get_conin_fd(void)
{
const HANDLE conin_handle = CreateFile("CONIN$",
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
(LPSECURITY_ATTRIBUTES)NULL,
OPEN_EXISTING, 0, (HANDLE)NULL);
assert(conin_handle != INVALID_HANDLE_VALUE);
int conin_fd = _open_osfhandle((intptr_t)conin_handle, _O_RDONLY);
assert(conin_fd != -1);
return conin_fd;
}
void os_replace_stdin_to_conin(void)
{
close(STDIN_FILENO);
const int conin_fd = os_get_conin_fd();
assert(conin_fd == STDIN_FILENO);
}
void os_replace_stdout_to_conout(void)
{
close(STDOUT_FILENO);
const HANDLE conout_handle =
CreateFile("CONOUT$",
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
(LPSECURITY_ATTRIBUTES)NULL,
OPEN_EXISTING, 0, (HANDLE)NULL);
assert(conout_handle != INVALID_HANDLE_VALUE);
const int conout_fd = _open_osfhandle((intptr_t)conout_handle, 0);
assert(conout_fd == STDOUT_FILENO);
}

View File

@ -0,0 +1,8 @@
#ifndef NVIM_OS_OS_WIN_CONSOLE_H
#define NVIM_OS_OS_WIN_CONSOLE_H
#ifdef INCLUDE_GENERATED_DECLARATIONS
# include "os/os_win_console.h.generated.h"
#endif
#endif // NVIM_OS_OS_WIN_CONSOLE_H

View File

@ -14,6 +14,9 @@
#include "nvim/option.h"
#include "nvim/os/os.h"
#include "nvim/os/input.h"
#ifdef WIN32
# include "nvim/os/os_win_console.h"
#endif
#include "nvim/event/rstream.h"
#define KEY_BUFFER_SIZE 0xfff
@ -37,13 +40,7 @@ void tinput_init(TermInput *input, Loop *loop)
// ls *.md | xargs nvim
#ifdef WIN32
if (!os_isatty(input->in_fd)) {
const HANDLE conin_handle = CreateFile("CONIN$",
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
(LPSECURITY_ATTRIBUTES)NULL,
OPEN_EXISTING, 0, (HANDLE)NULL);
input->in_fd = _open_osfhandle(conin_handle, _O_RDONLY);
assert(input->in_fd != -1);
input->in_fd = os_get_conin_fd();
}
#else
if (!os_isatty(input->in_fd) && os_isatty(STDERR_FILENO)) {