From b42f9c2cf75742d3fcb611b6f2132da506ef2d6b Mon Sep 17 00:00:00 2001 From: Simon Tatham Date: Sat, 21 May 2022 09:07:42 +0100 Subject: [PATCH 1/4] Fix a type warning in conpty.c. Turns out that standard C 'size_t' and the Win32 API's 'SIZE_T' aren't the same integer type in all cases! They have the same _size_, but in 32-bit, one of them is a typedef for 'unsigned int' and the other for 'unsigned long', leading to annoying pointer-type mismatch warnings. --- windows/conpty.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/windows/conpty.c b/windows/conpty.c index ca907020..385a7da3 100644 --- a/windows/conpty.c +++ b/windows/conpty.c @@ -190,7 +190,7 @@ static char *conpty_init(const BackendVtable *vt, Seat *seat, si.StartupInfo.cb = sizeof(si); - size_t attrsize = 0; + SIZE_T attrsize = 0; InitializeProcThreadAttributeList(NULL, 1, 0, &attrsize); si.lpAttributeList = smalloc(attrsize); if (!InitializeProcThreadAttributeList( From 4ae8b742ab3b916d549f74c05bce84c5e91453ad Mon Sep 17 00:00:00 2001 From: Simon Tatham Date: Sat, 21 May 2022 09:34:38 +0100 Subject: [PATCH 2/4] pterm.exe: load the ConPTY API at run time. This allows a better error message if you try to run pterm.exe on a Windows machine too old for kernel32.dll to contain the required API functions. --- windows/conpty.c | 37 ++++++++++++++++++++++++++++++++----- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/windows/conpty.c b/windows/conpty.c index 385a7da3..c23c81e1 100644 --- a/windows/conpty.c +++ b/windows/conpty.c @@ -25,6 +25,27 @@ struct ConPTY { Backend backend; }; +DECL_WINDOWS_FUNCTION(static, HRESULT, CreatePseudoConsole, + (COORD, HANDLE, HANDLE, DWORD, HPCON *)); +DECL_WINDOWS_FUNCTION(static, void, ClosePseudoConsole, (HPCON)); +DECL_WINDOWS_FUNCTION(static, HRESULT, ResizePseudoConsole, (HPCON, COORD)); + +static bool init_conpty_api(void) +{ + static bool tried = false; + if (!tried) { + tried = true; + HMODULE kernel32_module = load_system32_dll("kernel32.dll"); + GET_WINDOWS_FUNCTION(kernel32_module, CreatePseudoConsole); + GET_WINDOWS_FUNCTION(kernel32_module, ClosePseudoConsole); + GET_WINDOWS_FUNCTION(kernel32_module, ResizePseudoConsole); + } + + return (p_CreatePseudoConsole != NULL && + p_ClosePseudoConsole != NULL && + p_ResizePseudoConsole != NULL); +} + static void conpty_terminate(ConPTY *conpty) { if (conpty->out) { @@ -49,7 +70,7 @@ static void conpty_terminate(ConPTY *conpty) conpty->hprocess = INVALID_HANDLE_VALUE; } if (conpty->pseudoconsole != INVALID_HANDLE_VALUE) { - ClosePseudoConsole(conpty->pseudoconsole); + p_ClosePseudoConsole(conpty->pseudoconsole); conpty->pseudoconsole = INVALID_HANDLE_VALUE; } } @@ -78,7 +99,7 @@ static void conpty_process_wait_callback(void *vctx) * as things clean themselves up. */ if (conpty->pseudoconsole != INVALID_HANDLE_VALUE) { - ClosePseudoConsole(conpty->pseudoconsole); + p_ClosePseudoConsole(conpty->pseudoconsole); conpty->pseudoconsole = INVALID_HANDLE_VALUE; } } @@ -158,6 +179,12 @@ static char *conpty_init(const BackendVtable *vt, Seat *seat, STARTUPINFOEX si; memset(&si, 0, sizeof(si)); + if (!init_conpty_api()) { + err = dupprintf("Pseudo-console API is not available on this " + "Windows system"); + goto out; + } + if (!CreatePipe(&in_r, &in_w, NULL, 0)) { err = dupprintf("CreatePipe: %s", win_strerror(GetLastError())); goto out; @@ -171,7 +198,7 @@ static char *conpty_init(const BackendVtable *vt, Seat *seat, size.X = conf_get_int(conf, CONF_width); size.Y = conf_get_int(conf, CONF_height); - HRESULT result = CreatePseudoConsole(size, in_r, out_w, 0, &pcon); + HRESULT result = p_CreatePseudoConsole(size, in_r, out_w, 0, &pcon); if (FAILED(result)) { if (HRESULT_FACILITY(result) == FACILITY_WIN32) err = dupprintf("CreatePseudoConsole: %s", @@ -271,7 +298,7 @@ static char *conpty_init(const BackendVtable *vt, Seat *seat, if (out_w != INVALID_HANDLE_VALUE) CloseHandle(out_w); if (pcon_needs_cleanup) - ClosePseudoConsole(pcon); + p_ClosePseudoConsole(pcon); sfree(si.lpAttributeList); return err; } @@ -311,7 +338,7 @@ static void conpty_size(Backend *be, int width, int height) COORD size; size.X = width; size.Y = height; - ResizePseudoConsole(conpty->pseudoconsole, size); + p_ResizePseudoConsole(conpty->pseudoconsole, size); } static void conpty_special(Backend *be, SessionSpecialCode code, int arg) From 1a0d076dfb4ecdf19eb7c31f28dc92d74f423156 Mon Sep 17 00:00:00 2001 From: Simon Tatham Date: Sat, 21 May 2022 10:23:01 +0100 Subject: [PATCH 3/4] Disallow -4 and -6 in TOOLTYPE_NONNETWORK. They were accepted by Unix pterm, by mistake, and then totally ignored because nothing in pterm ever makes a network connection, so nothing cares whether you configured it to use IPv4 or IPv6. --- cmdline.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cmdline.c b/cmdline.c index 8e48b358..68eafc8e 100644 --- a/cmdline.c +++ b/cmdline.c @@ -749,11 +749,13 @@ int cmdline_process_param(const char *p, char *value, if (!strcmp(p, "-4") || !strcmp(p, "-ipv4")) { RETURN(1); + UNAVAILABLE_IN(TOOLTYPE_NONNETWORK); SAVEABLE(1); conf_set_int(conf, CONF_addressfamily, ADDRTYPE_IPV4); } if (!strcmp(p, "-6") || !strcmp(p, "-ipv6")) { RETURN(1); + UNAVAILABLE_IN(TOOLTYPE_NONNETWORK); SAVEABLE(1); conf_set_int(conf, CONF_addressfamily, ADDRTYPE_IPV6); } From e06a3dda4505dfd0fdeec5b183f21e79e13da0d1 Mon Sep 17 00:00:00 2001 From: Simon Tatham Date: Sat, 21 May 2022 10:32:32 +0100 Subject: [PATCH 4/4] pterm.exe: run command-line options through cmdline.c. This makes pterm.exe support the same (very small) subset of the standard option collection that Unix pterm does. Namely, -load (which won't do anything useful with a hostname to connect to, but is still useful if you have a saved session containing configuration like colours or default size or what have you), and also -sessionlog. To make this work, I've had to move the 'tooltype' definition out of window.c into {putty,pterm}.c, so that it can be defined differently in the two. --- windows/pterm.c | 18 ++++++++++++++++-- windows/putty.c | 5 +++++ windows/window.c | 5 ----- 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/windows/pterm.c b/windows/pterm.c index 0df849f9..bb68245d 100644 --- a/windows/pterm.c +++ b/windows/pterm.c @@ -1,6 +1,10 @@ #include "putty.h" #include "storage.h" +const unsigned cmdline_tooltype = + TOOLTYPE_NONNETWORK | + TOOLTYPE_NO_VERBOSE_OPTION; + void gui_term_process_cmdline(Conf *conf, char *cmdline) { do_defaults(NULL, conf); @@ -16,8 +20,16 @@ void gui_term_process_cmdline(Conf *conf, char *cmdline) split_into_argv(cmdline, &argc, &argv, &argstart); for (int i = 0; i < argc; i++) { - const char *arg = argv[i]; - if (!strcmp(arg, "-e")) { + char *arg = argv[i]; + int retd = cmdline_process_param( + arg, i+1