1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-01-09 09:27:59 +00:00

New application: a Windows version of 'pterm'!

This fulfills our long-standing Mayhem-difficulty wishlist item
'win-command-prompt': this is a Windows pterm in the sense that when
you run it you get a local cmd.exe running inside a PuTTY-style window.

Advantages of this: you get the same free choice of fonts as PuTTY has
(no restriction to a strange subset of the system's available fonts);
you get the same copy-paste gestures as PuTTY (no mental gear-shifting
when you have command prompts and SSH sessions open on the same
desktop); you get scrollback with the PuTTY semantics (scrolling to
the bottom gets you to where the action is, as opposed to the way you
could accidentally find yourself 500 lines past the end of the action
in a real console).

'win-command-prompt' was at Mayhem difficulty ('Probably impossible')
basically on the grounds that with Windows's old APIs for accessing
the contents of consoles, there was no way I could find to get this to
work sensibly. What was needed to make it feasible was a major piece
of re-engineering work inside Windows itself.

But, of course, that's exactly what happened! In 2019, the new ConPTY
API arrived, which lets you create an object that behaves like a
Windows console at one end, and round the back, emits a stream of
VT-style escape sequences as the screen contents evolve, and accepts a
VT-style input stream in return which it will parse function and arrow
keys out of in the usual way.

So now it's actually _easy_ to get this to basically work. The new
backend, in conpty.c, has to do a handful of magic Windows API calls
to set up the pseudo-console and its feeder pipes and start a
subprocess running in it, a further magic call every time the PuTTY
window is resized, and detect the end of the session by watching for
the subprocess terminating. But apart from that, all it has to do is
pass data back and forth unmodified between those pipes and the
backend's associated Seat!

That said, this is new and experimental, and there will undoubtedly be
issues. One that I already know about is that you can't copy and paste
a word that has wrapped between lines without getting an annoying
newline in the middle of it. As far as I can see this is a fundamental
limitation: the ConPTY system sends the _same_ escape sequence stream
for a line that wrapped as it would send for a line that had a logical
\n at what would have been the wrap point. Probably the best we can do
to mitigate this is to adopt a different heuristic for newline elision
that's right more often than it's wrong.

For the moment, that experimental-ness is indicated by the fact that
Buildscr will build, sign and deliver a copy of pterm.exe for each
flavour of Windows, but won't include it in the .zip file or in the
installer. (In fact, that puts it in exactly the same ad-hoc category
as PuTTYtel, although for completely different reasons.)
This commit is contained in:
Simon Tatham 2021-05-08 17:24:13 +01:00
parent cb33708f95
commit a55aac71e4
8 changed files with 493 additions and 6 deletions

View File

@ -257,11 +257,11 @@ in putty/windows do mkdir deliver
in putty/windows do for subdir in build32 abuild32 build64 abuild64 buildold; do mkdir deliver/$$subdir; done in putty/windows do for subdir in build32 abuild32 build64 abuild64 buildold; do mkdir deliver/$$subdir; done
in putty/windows do while read x; do mv $$x deliver/$$x; mv $$x.map deliver/$$x.map; done < to-sign.txt in putty/windows do while read x; do mv $$x deliver/$$x; mv $$x.map deliver/$$x.map; done < to-sign.txt
in putty/windows/deliver/buildold do zip -k -j putty.zip `ls *.exe | grep -v puttytel` ../../../docbuild/putty.chm in putty/windows/deliver/buildold do zip -k -j putty.zip `ls *.exe | grep -vxE '^(puttytel|pterm).exe'` ../../../docbuild/putty.chm
in putty/windows/deliver/build32 do zip -k -j putty.zip `ls *.exe | grep -v puttytel` ../../../docbuild/putty.chm in putty/windows/deliver/build32 do zip -k -j putty.zip `ls *.exe | grep -vxE '^(puttytel|pterm).exe'` ../../../docbuild/putty.chm
in putty/windows/deliver/build64 do zip -k -j putty.zip `ls *.exe | grep -v puttytel` ../../../docbuild/putty.chm in putty/windows/deliver/build64 do zip -k -j putty.zip `ls *.exe | grep -vxE '^(puttytel|pterm).exe'` ../../../docbuild/putty.chm
in putty/windows/deliver/abuild32 do zip -k -j putty.zip `ls *.exe | grep -v puttytel` ../../../docbuild/putty.chm in putty/windows/deliver/abuild32 do zip -k -j putty.zip `ls *.exe | grep -vxE '^(puttytel|pterm).exe'` ../../../docbuild/putty.chm
in putty/windows/deliver/abuild64 do zip -k -j putty.zip `ls *.exe | grep -v puttytel` ../../../docbuild/putty.chm in putty/windows/deliver/abuild64 do zip -k -j putty.zip `ls *.exe | grep -vxE '^(puttytel|pterm).exe'` ../../../docbuild/putty.chm
in docbuild/html do zip puttydoc.zip *.html in docbuild/html do zip puttydoc.zip *.html
# Deliver the actual PuTTY release directory into a subdir `putty'. # Deliver the actual PuTTY release directory into a subdir `putty'.

View File

@ -47,6 +47,7 @@ check_symbol_exists(SetDefaultDllDirectories "windows.h"
HAVE_SETDEFAULTDLLDIRECTORIES) HAVE_SETDEFAULTDLLDIRECTORIES)
check_symbol_exists(GetNamedPipeClientProcessId "windows.h" check_symbol_exists(GetNamedPipeClientProcessId "windows.h"
HAVE_GETNAMEDPIPECLIENTPROCESSID) HAVE_GETNAMEDPIPECLIENTPROCESSID)
check_symbol_exists(CreatePseudoConsole "windows.h" HAVE_CONPTY)
check_c_source_compiles(" check_c_source_compiles("
#include <windows.h> #include <windows.h>

View File

@ -134,6 +134,28 @@ set_target_properties(puttygen PROPERTIES
LINK_FLAGS "${LFLAG_MANIFEST_NO}") LINK_FLAGS "${LFLAG_MANIFEST_NO}")
installed_program(puttygen) installed_program(puttygen)
if(HAVE_CONPTY)
add_executable(pterm
window.c
pterm.c
help.c
conpty.c
be_conpty.c
${CMAKE_SOURCE_DIR}/nogss.c
${CMAKE_SOURCE_DIR}/norand.c
pterm.rc)
add_dependencies(pterm generated_licence_h)
target_link_libraries(pterm
guiterminal guimisc eventloop settings network utils
${platform_libraries})
set_target_properties(pterm PROPERTIES
WIN32_EXECUTABLE ON
LINK_FLAGS "${LFLAG_MANIFEST_NO}")
installed_program(pterm)
else()
message("ConPTY not available; cannot build Windows pterm")
endif()
add_executable(test_split_into_argv add_executable(test_split_into_argv
utils/split_into_argv.c) utils/split_into_argv.c)
target_compile_definitions(test_split_into_argv PRIVATE TEST) target_compile_definitions(test_split_into_argv PRIVATE TEST)

13
windows/be_conpty.c Normal file
View File

@ -0,0 +1,13 @@
#include <stdio.h>
#include "putty.h"
const char *const appname = "pterm";
const int be_default_protocol = -1;
const struct BackendVtable *const backends[] = {
&conpty_backend,
NULL
};
const size_t n_ui_backends = 1;

389
windows/conpty.c Normal file
View File

@ -0,0 +1,389 @@
/*
* Backend to run a Windows console session using ConPTY.
*/
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include "putty.h"
#include <windows.h>
#include <consoleapi.h>
typedef struct ConPTY ConPTY;
struct ConPTY {
HPCON pseudoconsole;
HANDLE outpipe, inpipe, hprocess;
struct handle *out, *in, *subprocess;
bool exited;
DWORD exitstatus;
Seat *seat;
LogContext *logctx;
int bufsize;
Backend backend;
};
static void conpty_terminate(ConPTY *conpty)
{
if (conpty->out) {
handle_free(conpty->out);
conpty->out = NULL;
}
if (conpty->outpipe != INVALID_HANDLE_VALUE) {
CloseHandle(conpty->outpipe);
conpty->outpipe = INVALID_HANDLE_VALUE;
}
if (conpty->in) {
handle_free(conpty->in);
conpty->in = NULL;
}
if (conpty->inpipe != INVALID_HANDLE_VALUE) {
CloseHandle(conpty->inpipe);
conpty->inpipe = INVALID_HANDLE_VALUE;
}
if (conpty->subprocess) {
handle_free(conpty->subprocess);
conpty->subprocess = NULL;
conpty->hprocess = INVALID_HANDLE_VALUE;
}
if (conpty->pseudoconsole != INVALID_HANDLE_VALUE) {
ClosePseudoConsole(conpty->pseudoconsole);
conpty->pseudoconsole = INVALID_HANDLE_VALUE;
}
}
static void conpty_process_wait_callback(void *vctx)
{
ConPTY *conpty = (ConPTY *)vctx;
if (!GetExitCodeProcess(conpty->hprocess, &conpty->exitstatus))
return;
conpty->exited = true;
/*
* We can stop waiting for the process now.
*/
if (conpty->subprocess) {
handle_free(conpty->subprocess);
conpty->subprocess = NULL;
conpty->hprocess = INVALID_HANDLE_VALUE;
}
/*
* Once the contained process exits, close the pseudo-console as
* well. But don't close the pipes yet, since apparently
* ClosePseudoConsole can trigger a final bout of terminal output
* as things clean themselves up.
*/
if (conpty->pseudoconsole != INVALID_HANDLE_VALUE) {
ClosePseudoConsole(conpty->pseudoconsole);
conpty->pseudoconsole = INVALID_HANDLE_VALUE;
}
}
static size_t conpty_gotdata(
struct handle *h, const void *data, size_t len, int err)
{
ConPTY *conpty = (ConPTY *)handle_get_privdata(h);
if (err || len == 0) {
char *error_msg;
conpty_terminate(conpty);
seat_notify_remote_exit(conpty->seat);
if (!err && conpty->exited) {
/*
* The clean-exit case: our subprocess terminated, we
* deleted the PseudoConsole ourself, and now we got the
* expected EOF on the pipe.
*/
return 0;
}
if (err)
error_msg = dupprintf("Error reading from console pty: %s",
win_strerror(err));
else
error_msg = dupprintf(
"Unexpected end of file reading from console pty");
logevent(conpty->logctx, error_msg);
seat_connection_fatal(conpty->seat, "%s", error_msg);
sfree(error_msg);
return 0;
} else {
return seat_stdout(conpty->seat, data, len);
}
}
static void conpty_sentdata(struct handle *h, size_t new_backlog, int err)
{
ConPTY *conpty = (ConPTY *)handle_get_privdata(h);
if (err) {
const char *error_msg = "Error writing to conpty device";
conpty_terminate(conpty);
seat_notify_remote_exit(conpty->seat);
logevent(conpty->logctx, error_msg);
seat_connection_fatal(conpty->seat, "%s", error_msg);
} else {
conpty->bufsize = new_backlog;
}
}
static char *conpty_init(const BackendVtable *vt, Seat *seat,
Backend **backend_handle, LogContext *logctx,
Conf *conf, const char *host, int port,
char **realhost, bool nodelay, bool keepalive)
{
ConPTY *conpty;
char *err = NULL;
HANDLE in_r = INVALID_HANDLE_VALUE;
HANDLE in_w = INVALID_HANDLE_VALUE;
HANDLE out_r = INVALID_HANDLE_VALUE;
HANDLE out_w = INVALID_HANDLE_VALUE;
HPCON pcon;
bool pcon_needs_cleanup = false;
STARTUPINFOEX si;
memset(&si, 0, sizeof(si));
if (!CreatePipe(&in_r, &in_w, NULL, 0)) {
err = dupprintf("CreatePipe: %s", win_strerror(GetLastError()));
goto out;
}
if (!CreatePipe(&out_r, &out_w, NULL, 0)) {
err = dupprintf("CreatePipe: %s", win_strerror(GetLastError()));
goto out;
}
COORD size;
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);
if (FAILED(result)) {
if (HRESULT_FACILITY(result) == FACILITY_WIN32)
err = dupprintf("CreatePseudoConsole: %s",
win_strerror(HRESULT_CODE(result)));
else
err = dupprintf("CreatePseudoConsole failed: HRESULT=0x%08x",
(unsigned)result);
goto out;
}
pcon_needs_cleanup = true;
CloseHandle(in_r);
in_r = INVALID_HANDLE_VALUE;
CloseHandle(out_w);
out_w = INVALID_HANDLE_VALUE;
si.StartupInfo.cb = sizeof(si);
size_t attrsize = 0;
InitializeProcThreadAttributeList(NULL, 1, 0, &attrsize);
si.lpAttributeList = smalloc(attrsize);
if (!InitializeProcThreadAttributeList(
si.lpAttributeList, 1, 0, &attrsize)) {
err = dupprintf("InitializeProcThreadAttributeList: %s",
win_strerror(GetLastError()));
goto out;
}
if (!UpdateProcThreadAttribute(
si.lpAttributeList,
0, PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE,
pcon, sizeof(pcon), NULL, NULL)) {
err = dupprintf("UpdateProcThreadAttribute: %s",
win_strerror(GetLastError()));
goto out;
}
PROCESS_INFORMATION pi;
memset(&pi, 0, sizeof(pi));
char *command;
const char *conf_cmd = conf_get_str(conf, CONF_remote_cmd);
if (*conf_cmd) {
command = dupstr(conf_cmd);
} else {
command = dupcat(get_system_dir(), "\\cmd.exe");
}
bool created_ok = CreateProcess(NULL, command, NULL, NULL,
false, EXTENDED_STARTUPINFO_PRESENT,
NULL, NULL, &si.StartupInfo, &pi);
sfree(command);
if (!created_ok) {
err = dupprintf("CreateProcess: %s",
win_strerror(GetLastError()));
goto out;
}
/* No local authentication phase in this protocol */
seat_set_trust_status(seat, false);
conpty = snew(ConPTY);
conpty->pseudoconsole = pcon;
pcon_needs_cleanup = false;
conpty->outpipe = in_w;
conpty->out = handle_output_new(in_w, conpty_sentdata, conpty, 0);
in_w = INVALID_HANDLE_VALUE;
conpty->inpipe = out_r;
conpty->in = handle_input_new(out_r, conpty_gotdata, conpty, 0);
out_r = INVALID_HANDLE_VALUE;
conpty->subprocess = handle_add_foreign_event(
pi.hProcess, conpty_process_wait_callback, conpty);
conpty->hprocess = pi.hProcess;
CloseHandle(pi.hThread);
conpty->exited = false;
conpty->exitstatus = 0;
conpty->bufsize = 0;
conpty->backend.vt = vt;
*backend_handle = &conpty->backend;
conpty->seat = seat;
conpty->logctx = logctx;
*realhost = dupstr("");
/*
* Specials are always available.
*/
seat_update_specials_menu(conpty->seat);
out:
if (in_r != INVALID_HANDLE_VALUE)
CloseHandle(in_r);
if (in_w != INVALID_HANDLE_VALUE)
CloseHandle(in_w);
if (out_r != INVALID_HANDLE_VALUE)
CloseHandle(out_r);
if (out_w != INVALID_HANDLE_VALUE)
CloseHandle(out_w);
if (pcon_needs_cleanup)
ClosePseudoConsole(pcon);
sfree(si.lpAttributeList);
return err;
}
static void conpty_free(Backend *be)
{
ConPTY *conpty = container_of(be, ConPTY, backend);
conpty_terminate(conpty);
expire_timer_context(conpty);
sfree(conpty);
}
static void conpty_reconfig(Backend *be, Conf *conf)
{
}
static size_t conpty_send(Backend *be, const char *buf, size_t len)
{
ConPTY *conpty = container_of(be, ConPTY, backend);
if (conpty->out == NULL)
return 0;
conpty->bufsize = handle_write(conpty->out, buf, len);
return conpty->bufsize;
}
static size_t conpty_sendbuffer(Backend *be)
{
ConPTY *conpty = container_of(be, ConPTY, backend);
return conpty->bufsize;
}
static void conpty_size(Backend *be, int width, int height)
{
ConPTY *conpty = container_of(be, ConPTY, backend);
COORD size;
size.X = width;
size.Y = height;
ResizePseudoConsole(conpty->pseudoconsole, size);
}
static void conpty_special(Backend *be, SessionSpecialCode code, int arg)
{
}
static const SessionSpecial *conpty_get_specials(Backend *be)
{
static const SessionSpecial specials[] = {
{NULL, SS_EXITMENU}
};
return specials;
}
static bool conpty_connected(Backend *be)
{
return true; /* always connected */
}
static bool conpty_sendok(Backend *be)
{
return true;
}
static void conpty_unthrottle(Backend *be, size_t backlog)
{
ConPTY *conpty = container_of(be, ConPTY, backend);
if (conpty->in)
handle_unthrottle(conpty->in, backlog);
}
static bool conpty_ldisc(Backend *be, int option)
{
return false;
}
static void conpty_provide_ldisc(Backend *be, Ldisc *ldisc)
{
}
static int conpty_exitcode(Backend *be)
{
ConPTY *conpty = container_of(be, ConPTY, backend);
if (conpty->exited &&
0 <= conpty->exitstatus &&
conpty->exitstatus <= INT_MAX)
return conpty->exitstatus;
else
return -1;
}
static int conpty_cfg_info(Backend *be)
{
return 0;
}
const BackendVtable conpty_backend = {
.init = conpty_init,
.free = conpty_free,
.reconfig = conpty_reconfig,
.send = conpty_send,
.sendbuffer = conpty_sendbuffer,
.size = conpty_size,
.special = conpty_special,
.get_specials = conpty_get_specials,
.connected = conpty_connected,
.exitcode = conpty_exitcode,
.sendok = conpty_sendok,
.ldisc_option_state = conpty_ldisc,
.provide_ldisc = conpty_provide_ldisc,
.unthrottle = conpty_unthrottle,
.cfg_info = conpty_cfg_info,
.id = "conpty",
.displayname = "ConPTY",
.protocol = -1,
};

View File

@ -705,7 +705,9 @@ void cli_main_loop(cliloop_pre_t pre, cliloop_post_t post, void *ctx);
bool cliloop_null_pre(void *vctx, const HANDLE **, size_t *); bool cliloop_null_pre(void *vctx, const HANDLE **, size_t *);
bool cliloop_null_post(void *vctx, size_t); bool cliloop_null_post(void *vctx, size_t);
/* Functions that parametrise window.c */ extern const struct BackendVtable conpty_backend;
/* Functions that parametrise window.c between PuTTY and pterm */
void gui_term_process_cmdline(Conf *conf, char *cmdline); void gui_term_process_cmdline(Conf *conf, char *cmdline);
const struct BackendVtable *backend_vt_from_conf(Conf *conf); const struct BackendVtable *backend_vt_from_conf(Conf *conf);
const wchar_t *get_app_user_model_id(void); const wchar_t *get_app_user_model_id(void);

45
windows/pterm.c Normal file
View File

@ -0,0 +1,45 @@
#include "putty.h"
#include "storage.h"
void gui_term_process_cmdline(Conf *conf, char *cmdline)
{
do_defaults(NULL, conf);
conf_set_str(conf, CONF_remote_cmd, "");
cmdline = handle_restrict_acl_cmdline_prefix(cmdline);
if (handle_special_sessionname_cmdline(cmdline, conf) ||
handle_special_filemapping_cmdline(cmdline, conf))
return;
int argc;
char **argv, **argstart;
split_into_argv(cmdline, &argc, &argv, &argstart);
for (int i = 0; i < argc; i++) {
const char *arg = argv[i];
if (!strcmp(arg, "-e")) {
if (i+1 < argc) {
/* The command to execute is taken to be the unparsed
* version of the whole remainder of the command line. */
conf_set_str(conf, CONF_remote_cmd, argstart[i+1]);
return;
} else {
cmdline_error("option \"%s\" requires an argument", arg);
}
} else if (arg[0] == '-') {
cmdline_error("unrecognised option \"%s\"", arg);
} else {
cmdline_error("unexpected non-option argument \"%s\"", arg);
}
}
}
const struct BackendVtable *backend_vt_from_conf(Conf *conf)
{
return &conpty_backend;
}
const wchar_t *get_app_user_model_id(void)
{
return L"SimonTatham.Pterm";
}

15
windows/pterm.rc Normal file
View File

@ -0,0 +1,15 @@
#include "rcstuff.h"
#include "putty-rc.h"
#define APPNAME "pterm"
#define APPDESC "PuTTY-style wrapper for Windows command prompts"
IDI_MAINICON ICON "pterm.ico"
IDI_CFGICON ICON "ptermcfg.ico"
#include "help.rc2"
#include "putty-common.rc2"
#ifndef NO_MANIFESTS
1 RT_MANIFEST "putty.mft"
#endif /* NO_MANIFESTS */