From 8f91d709b0006e3fcfa4cb59abd9b77938096d76 Mon Sep 17 00:00:00 2001 From: erw7 Date: Sat, 16 Nov 2019 13:50:52 +0900 Subject: Rename from os_win_conpty.{c,h} to pty_conpty_win.{c,h} --- src/nvim/CMakeLists.txt | 4 +- src/nvim/channel.c | 2 +- src/nvim/option.c | 2 +- src/nvim/os/os_win_conpty.c | 199 ------------------------------------------ src/nvim/os/os_win_conpty.h | 22 ----- src/nvim/os/pty_conpty_win.c | 199 ++++++++++++++++++++++++++++++++++++++++++ src/nvim/os/pty_conpty_win.h | 22 +++++ src/nvim/os/pty_process_win.c | 2 +- src/nvim/os/pty_process_win.h | 2 +- 9 files changed, 227 insertions(+), 227 deletions(-) delete mode 100644 src/nvim/os/os_win_conpty.c delete mode 100644 src/nvim/os/os_win_conpty.h create mode 100644 src/nvim/os/pty_conpty_win.c create mode 100644 src/nvim/os/pty_conpty_win.h (limited to 'src') diff --git a/src/nvim/CMakeLists.txt b/src/nvim/CMakeLists.txt index 62d45ff4a3..089dd537e9 100644 --- a/src/nvim/CMakeLists.txt +++ b/src/nvim/CMakeLists.txt @@ -134,7 +134,7 @@ foreach(sfile ${NVIM_SOURCES}) if(NOT WIN32 AND ${f} MATCHES "^(pty_process_win.c)$") list(APPEND to_remove ${sfile}) endif() - if(NOT WIN32 AND ${f} MATCHES "^(os_win_conpty.c)$") + if(NOT WIN32 AND ${f} MATCHES "^(pty_conpty_win.c)$") list(APPEND to_remove ${sfile}) endif() if(NOT WIN32 AND ${f} MATCHES "^(os_win_console.c)$") @@ -643,7 +643,7 @@ endfunction() set(NO_SINGLE_CHECK_HEADERS os/win_defs.h os/pty_process_win.h - os/os_win_conpty.h + os/pty_conpty_win.h os/os_win_console.h ) foreach(hfile ${NVIM_HEADERS}) diff --git a/src/nvim/channel.c b/src/nvim/channel.c index b346845c66..d3e2977eae 100644 --- a/src/nvim/channel.c +++ b/src/nvim/channel.c @@ -12,7 +12,7 @@ #include "nvim/msgpack_rpc/server.h" #include "nvim/os/shell.h" #ifdef WIN32 -# include "nvim/os/os_win_conpty.h" +# include "nvim/os/pty_conpty_win.h" # include "nvim/os/os_win_console.h" #endif #include "nvim/path.h" diff --git a/src/nvim/option.c b/src/nvim/option.c index 591c8234c3..014eb676a0 100644 --- a/src/nvim/option.c +++ b/src/nvim/option.c @@ -77,7 +77,7 @@ #include "nvim/window.h" #include "nvim/os/os.h" #ifdef WIN32 -# include "nvim/os/os_win_conpty.h" +# include "nvim/os/pty_conpty_win.h" #endif #include "nvim/api/private/helpers.h" #include "nvim/os/input.h" diff --git a/src/nvim/os/os_win_conpty.c b/src/nvim/os/os_win_conpty.c deleted file mode 100644 index 3d5ba83523..0000000000 --- a/src/nvim/os/os_win_conpty.c +++ /dev/null @@ -1,199 +0,0 @@ -// 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 - -#include "nvim/vim.h" -#include "nvim/os/os.h" -#include "nvim/os/os_win_conpty.h" - -#ifndef EXTENDED_STARTUPINFO_PRESENT -# define EXTENDED_STARTUPINFO_PRESENT 0x00080000 -#endif -#ifndef PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE -# define PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE 0x00020016 -#endif - -HRESULT (WINAPI *pCreatePseudoConsole)(COORD, HANDLE, HANDLE, DWORD, HPCON *); -HRESULT (WINAPI *pResizePseudoConsole)(HPCON, COORD); -void (WINAPI *pClosePseudoConsole)(HPCON); - -bool os_has_conpty_working(void) -{ - static TriState has_conpty = kNone; - if (has_conpty == kNone) { - has_conpty = os_dyn_conpty_init(); - } - - return has_conpty == kTrue; -} - -TriState os_dyn_conpty_init(void) -{ - uv_lib_t kernel; - if (uv_dlopen("kernel32.dll", &kernel)) { - uv_dlclose(&kernel); - return kFalse; - } - static struct { - char *name; - FARPROC *ptr; - } conpty_entry[] = { - { "CreatePseudoConsole", (FARPROC *)&pCreatePseudoConsole }, - { "ResizePseudoConsole", (FARPROC *)&pResizePseudoConsole }, - { "ClosePseudoConsole", (FARPROC *)&pClosePseudoConsole }, - { NULL, NULL } - }; - for (int i = 0; - conpty_entry[i].name != NULL && conpty_entry[i].ptr != NULL; i++) { - if (uv_dlsym(&kernel, conpty_entry[i].name, (void **)conpty_entry[i].ptr)) { - uv_dlclose(&kernel); - return kFalse; - } - } - return kTrue; -} - -conpty_t *os_conpty_init(char **in_name, char **out_name, - uint16_t width, uint16_t height) -{ - static int count = 0; - conpty_t *conpty_object = xcalloc(1, sizeof(*conpty_object)); - const char *emsg = NULL; - HANDLE in_read = INVALID_HANDLE_VALUE; - HANDLE out_write = INVALID_HANDLE_VALUE; - char buf[MAXPATHL]; - SECURITY_ATTRIBUTES sa = { 0 }; - const DWORD mode = PIPE_ACCESS_INBOUND - | PIPE_ACCESS_OUTBOUND | FILE_FLAG_FIRST_PIPE_INSTANCE; - - sa.nLength = sizeof(sa); - snprintf(buf, sizeof(buf), "\\\\.\\pipe\\nvim-term-in-%d-%d", - os_get_pid(), count); - *in_name = xstrdup(buf); - if ((in_read = CreateNamedPipeA( - *in_name, - mode, - PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT, - 1, - 0, - 0, - 30000, - &sa)) == INVALID_HANDLE_VALUE) { - emsg = "create input pipe failed"; - goto failed; - } - snprintf(buf, sizeof(buf), "\\\\.\\pipe\\nvim-term-out-%d-%d", - os_get_pid(), count); - *out_name = xstrdup(buf); - if ((out_write = CreateNamedPipeA( - *out_name, - mode, - PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT, - 1, - 0, - 0, - 30000, - &sa)) == INVALID_HANDLE_VALUE) { - emsg = "create output pipe failed"; - goto failed; - } - assert(width <= SHRT_MAX); - assert(height <= SHRT_MAX); - COORD size = { (int16_t)width, (int16_t)height }; - HRESULT hr; - hr = pCreatePseudoConsole(size, in_read, out_write, 0, &conpty_object->pty); - if (FAILED(hr)) { - emsg = "create psudo console failed"; - goto failed; - } - - conpty_object->si_ex.StartupInfo.cb = sizeof(conpty_object->si_ex); - size_t bytes_required; - InitializeProcThreadAttributeList(NULL, 1, 0, & bytes_required); - conpty_object->si_ex.lpAttributeList = - (PPROC_THREAD_ATTRIBUTE_LIST)xmalloc(bytes_required); - if (!InitializeProcThreadAttributeList( - conpty_object->si_ex.lpAttributeList, - 1, - 0, - &bytes_required)) { - emsg = "InitializeProcThreadAttributeList failed"; - goto failed; - } - if (!UpdateProcThreadAttribute( - conpty_object->si_ex.lpAttributeList, - 0, - PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE, - conpty_object->pty, - sizeof(conpty_object->pty), - NULL, - NULL)) { - emsg = "UpdateProcThreadAttribute failed"; - goto failed; - } - count++; - goto finished; - -failed: - ELOG("os_conpty_init:%s : error code: %d", - emsg, os_translate_sys_error((int)GetLastError())); - os_conpty_free(conpty_object); - conpty_object = NULL; -finished: - if (in_read != INVALID_HANDLE_VALUE) { - CloseHandle(in_read); - } - if (out_write != INVALID_HANDLE_VALUE) { - CloseHandle(out_write); - } - return conpty_object; -} - -bool os_conpty_spawn(conpty_t *conpty_object, HANDLE *process_handle, - wchar_t *name, wchar_t *cmd_line, wchar_t *cwd, - wchar_t *env) -{ - PROCESS_INFORMATION pi = { 0 }; - if (!CreateProcessW( - name, - cmd_line, - NULL, - NULL, - false, - EXTENDED_STARTUPINFO_PRESENT | CREATE_UNICODE_ENVIRONMENT, - env, - cwd, - &conpty_object->si_ex.StartupInfo, - &pi)) { - return false; - } - *process_handle = pi.hProcess; - return true; -} - -void os_conpty_set_size(conpty_t *conpty_object, - uint16_t width, uint16_t height) -{ - assert(width <= SHRT_MAX); - assert(height <= SHRT_MAX); - COORD size = { (int16_t)width, (int16_t)height }; - if (pResizePseudoConsole(conpty_object->pty, size) != S_OK) { - ELOG("ResizePseudoConsoel failed: error code: %d", - os_translate_sys_error((int)GetLastError())); - } -} - -void os_conpty_free(conpty_t *conpty_object) -{ - if (conpty_object != NULL) { - if (conpty_object->si_ex.lpAttributeList != NULL) { - DeleteProcThreadAttributeList(conpty_object->si_ex.lpAttributeList); - xfree(conpty_object->si_ex.lpAttributeList); - } - if (conpty_object->pty != NULL) { - pClosePseudoConsole(conpty_object->pty); - } - } - xfree(conpty_object); -} diff --git a/src/nvim/os/os_win_conpty.h b/src/nvim/os/os_win_conpty.h deleted file mode 100644 index 8a5f45e2cd..0000000000 --- a/src/nvim/os/os_win_conpty.h +++ /dev/null @@ -1,22 +0,0 @@ -#ifndef NVIM_OS_OS_WIN_CONPTY_H -#define NVIM_OS_OS_WIN_CONPTY_H - -#ifndef HPCON -# define HPCON VOID * -#endif - -extern HRESULT (WINAPI *pCreatePseudoConsole) // NOLINT(whitespace/parens) - (COORD, HANDLE, HANDLE, DWORD, HPCON *); -extern HRESULT (WINAPI *pResizePseudoConsole)(HPCON, COORD); -extern void (WINAPI *pClosePseudoConsole)(HPCON); - -typedef struct conpty { - HPCON pty; - STARTUPINFOEXW si_ex; -} conpty_t; - -#ifdef INCLUDE_GENERATED_DECLARATIONS -# include "os/os_win_conpty.h.generated.h" -#endif - -#endif // NVIM_OS_OS_WIN_CONPTY_H diff --git a/src/nvim/os/pty_conpty_win.c b/src/nvim/os/pty_conpty_win.c new file mode 100644 index 0000000000..5bcadd6490 --- /dev/null +++ b/src/nvim/os/pty_conpty_win.c @@ -0,0 +1,199 @@ +// 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 + +#include "nvim/vim.h" +#include "nvim/os/os.h" +#include "nvim/os/pty_conpty_win.h" + +#ifndef EXTENDED_STARTUPINFO_PRESENT +# define EXTENDED_STARTUPINFO_PRESENT 0x00080000 +#endif +#ifndef PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE +# define PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE 0x00020016 +#endif + +HRESULT (WINAPI *pCreatePseudoConsole)(COORD, HANDLE, HANDLE, DWORD, HPCON *); +HRESULT (WINAPI *pResizePseudoConsole)(HPCON, COORD); +void (WINAPI *pClosePseudoConsole)(HPCON); + +bool os_has_conpty_working(void) +{ + static TriState has_conpty = kNone; + if (has_conpty == kNone) { + has_conpty = os_dyn_conpty_init(); + } + + return has_conpty == kTrue; +} + +TriState os_dyn_conpty_init(void) +{ + uv_lib_t kernel; + if (uv_dlopen("kernel32.dll", &kernel)) { + uv_dlclose(&kernel); + return kFalse; + } + static struct { + char *name; + FARPROC *ptr; + } conpty_entry[] = { + { "CreatePseudoConsole", (FARPROC *)&pCreatePseudoConsole }, + { "ResizePseudoConsole", (FARPROC *)&pResizePseudoConsole }, + { "ClosePseudoConsole", (FARPROC *)&pClosePseudoConsole }, + { NULL, NULL } + }; + for (int i = 0; + conpty_entry[i].name != NULL && conpty_entry[i].ptr != NULL; i++) { + if (uv_dlsym(&kernel, conpty_entry[i].name, (void **)conpty_entry[i].ptr)) { + uv_dlclose(&kernel); + return kFalse; + } + } + return kTrue; +} + +conpty_t *os_conpty_init(char **in_name, char **out_name, + uint16_t width, uint16_t height) +{ + static int count = 0; + conpty_t *conpty_object = xcalloc(1, sizeof(*conpty_object)); + const char *emsg = NULL; + HANDLE in_read = INVALID_HANDLE_VALUE; + HANDLE out_write = INVALID_HANDLE_VALUE; + char buf[MAXPATHL]; + SECURITY_ATTRIBUTES sa = { 0 }; + const DWORD mode = PIPE_ACCESS_INBOUND + | PIPE_ACCESS_OUTBOUND | FILE_FLAG_FIRST_PIPE_INSTANCE; + + sa.nLength = sizeof(sa); + snprintf(buf, sizeof(buf), "\\\\.\\pipe\\nvim-term-in-%d-%d", + os_get_pid(), count); + *in_name = xstrdup(buf); + if ((in_read = CreateNamedPipeA( + *in_name, + mode, + PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT, + 1, + 0, + 0, + 30000, + &sa)) == INVALID_HANDLE_VALUE) { + emsg = "create input pipe failed"; + goto failed; + } + snprintf(buf, sizeof(buf), "\\\\.\\pipe\\nvim-term-out-%d-%d", + os_get_pid(), count); + *out_name = xstrdup(buf); + if ((out_write = CreateNamedPipeA( + *out_name, + mode, + PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT, + 1, + 0, + 0, + 30000, + &sa)) == INVALID_HANDLE_VALUE) { + emsg = "create output pipe failed"; + goto failed; + } + assert(width <= SHRT_MAX); + assert(height <= SHRT_MAX); + COORD size = { (int16_t)width, (int16_t)height }; + HRESULT hr; + hr = pCreatePseudoConsole(size, in_read, out_write, 0, &conpty_object->pty); + if (FAILED(hr)) { + emsg = "create psudo console failed"; + goto failed; + } + + conpty_object->si_ex.StartupInfo.cb = sizeof(conpty_object->si_ex); + size_t bytes_required; + InitializeProcThreadAttributeList(NULL, 1, 0, & bytes_required); + conpty_object->si_ex.lpAttributeList = + (PPROC_THREAD_ATTRIBUTE_LIST)xmalloc(bytes_required); + if (!InitializeProcThreadAttributeList( + conpty_object->si_ex.lpAttributeList, + 1, + 0, + &bytes_required)) { + emsg = "InitializeProcThreadAttributeList failed"; + goto failed; + } + if (!UpdateProcThreadAttribute( + conpty_object->si_ex.lpAttributeList, + 0, + PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE, + conpty_object->pty, + sizeof(conpty_object->pty), + NULL, + NULL)) { + emsg = "UpdateProcThreadAttribute failed"; + goto failed; + } + count++; + goto finished; + +failed: + ELOG("os_conpty_init:%s : error code: %d", + emsg, os_translate_sys_error((int)GetLastError())); + os_conpty_free(conpty_object); + conpty_object = NULL; +finished: + if (in_read != INVALID_HANDLE_VALUE) { + CloseHandle(in_read); + } + if (out_write != INVALID_HANDLE_VALUE) { + CloseHandle(out_write); + } + return conpty_object; +} + +bool os_conpty_spawn(conpty_t *conpty_object, HANDLE *process_handle, + wchar_t *name, wchar_t *cmd_line, wchar_t *cwd, + wchar_t *env) +{ + PROCESS_INFORMATION pi = { 0 }; + if (!CreateProcessW( + name, + cmd_line, + NULL, + NULL, + false, + EXTENDED_STARTUPINFO_PRESENT | CREATE_UNICODE_ENVIRONMENT, + env, + cwd, + &conpty_object->si_ex.StartupInfo, + &pi)) { + return false; + } + *process_handle = pi.hProcess; + return true; +} + +void os_conpty_set_size(conpty_t *conpty_object, + uint16_t width, uint16_t height) +{ + assert(width <= SHRT_MAX); + assert(height <= SHRT_MAX); + COORD size = { (int16_t)width, (int16_t)height }; + if (pResizePseudoConsole(conpty_object->pty, size) != S_OK) { + ELOG("ResizePseudoConsoel failed: error code: %d", + os_translate_sys_error((int)GetLastError())); + } +} + +void os_conpty_free(conpty_t *conpty_object) +{ + if (conpty_object != NULL) { + if (conpty_object->si_ex.lpAttributeList != NULL) { + DeleteProcThreadAttributeList(conpty_object->si_ex.lpAttributeList); + xfree(conpty_object->si_ex.lpAttributeList); + } + if (conpty_object->pty != NULL) { + pClosePseudoConsole(conpty_object->pty); + } + } + xfree(conpty_object); +} diff --git a/src/nvim/os/pty_conpty_win.h b/src/nvim/os/pty_conpty_win.h new file mode 100644 index 0000000000..c243db4fa5 --- /dev/null +++ b/src/nvim/os/pty_conpty_win.h @@ -0,0 +1,22 @@ +#ifndef NVIM_OS_PTY_CONPTY_WIN_H +#define NVIM_OS_PTY_CONPTY_WIN_H + +#ifndef HPCON +# define HPCON VOID * +#endif + +extern HRESULT (WINAPI *pCreatePseudoConsole) // NOLINT(whitespace/parens) + (COORD, HANDLE, HANDLE, DWORD, HPCON *); +extern HRESULT (WINAPI *pResizePseudoConsole)(HPCON, COORD); +extern void (WINAPI *pClosePseudoConsole)(HPCON); + +typedef struct conpty { + HPCON pty; + STARTUPINFOEXW si_ex; +} conpty_t; + +#ifdef INCLUDE_GENERATED_DECLARATIONS +# include "os/pty_conpty_win.h.generated.h" +#endif + +#endif // NVIM_OS_PTY_CONPTY_WIN_H diff --git a/src/nvim/os/pty_process_win.c b/src/nvim/os/pty_process_win.c index 7108d9c049..64d4408ece 100644 --- a/src/nvim/os/pty_process_win.c +++ b/src/nvim/os/pty_process_win.c @@ -12,7 +12,7 @@ #include "nvim/memory.h" #include "nvim/mbyte.h" // for utf8_to_utf16, utf16_to_utf8 #include "nvim/os/pty_process_win.h" -#include "nvim/os/os_win_conpty.h" +#include "nvim/os/pty_conpty_win.h" #ifdef INCLUDE_GENERATED_DECLARATIONS # include "os/pty_process_win.c.generated.h" diff --git a/src/nvim/os/pty_process_win.h b/src/nvim/os/pty_process_win.h index 04c8439c6c..8ad5ba7286 100644 --- a/src/nvim/os/pty_process_win.h +++ b/src/nvim/os/pty_process_win.h @@ -6,7 +6,7 @@ #include "nvim/event/process.h" #include "nvim/lib/queue.h" -#include "nvim/os/os_win_conpty.h" +#include "nvim/os/pty_conpty_win.h" typedef enum { kWinpty, -- cgit