1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-01-09 17:38:00 +00:00
putty-source/windows/putty.c

204 lines
7.4 KiB
C
Raw Permalink Normal View History

#include "putty.h"
#include "storage.h"
extern bool sesslist_demo_mode;
extern Filename *dialog_box_demo_screenshot_filename;
static strbuf *demo_terminal_data = NULL;
static Filename *terminal_demo_screenshot_filename;
const unsigned cmdline_tooltype =
TOOLTYPE_HOST_ARG |
TOOLTYPE_PORT_ARG |
TOOLTYPE_NO_VERBOSE_OPTION;
void gui_term_process_cmdline(Conf *conf, char *cmdline)
{
char *p;
bool special_launchable_argument = false;
bool demo_config_box = false;
settings_set_default_protocol(be_default_protocol);
/* Find the appropriate default port. */
{
const struct BackendVtable *vt =
backend_vt_from_proto(be_default_protocol);
settings_set_default_port(0); /* illegal */
if (vt)
settings_set_default_port(vt->default_port);
}
conf_set_int(conf, CONF_logtype, LGTYP_NONE);
do_defaults(NULL, conf);
p = handle_restrict_acl_cmdline_prefix(cmdline);
if (handle_special_sessionname_cmdline(p, conf)) {
if (!conf_launchable(conf) && !do_config(conf)) {
cleanup_exit(0);
}
special_launchable_argument = true;
} else if (handle_special_filemapping_cmdline(p, conf)) {
special_launchable_argument = true;
} else if (!*p) {
/* Do-nothing case for an empty command line - or rather,
* for a command line that's empty _after_ we strip off
* the &R prefix. */
} else {
/*
* Otherwise, break up the command line and deal with
* it sensibly.
*/
New abstraction for command-line arguments. This begins the process of enabling our Windows applications to handle Unicode characters on their command lines which don't fit in the system code page. Instead of passing plain strings to cmdline_process_param, we now pass a partially opaque and platform-specific thing called a CmdlineArg. This has a method that extracts the argument word as a default-encoded string, and another one that tries to extract it as UTF-8 (though it may fail if the UTF-8 isn't available). On Windows, the command line is now constructed by calling split_into_argv_w on the Unicode command line returned by GetCommandLineW(), and the UTF-8 method returns text converted directly from that wide-character form, not going via the system code page. So it _can_ include UTF-8 characters that wouldn't have round-tripped via CP_ACP. This commit introduces the abstraction and switches over the cross-platform and Windows argv-handling code to use it, with minimal functional change. Nothing yet tries to call cmdline_arg_get_utf8(). I say 'cross-platform and Windows' because on the Unix side there's still a lot of use of plain old argv which I haven't converted. That would be a much larger project, and isn't currently needed: the _current_ aim of this abstraction is to get the right things to happen relating to Unicode on Windows, so for code that doesn't run on Windows anyway, it's not adding value. (Also there's a tension with GTK, which wants to talk to standard argv and extract arguments _it_ knows about, so at the very least we'd have to let it munge argv before importing it into this new system.)
2024-09-25 09:18:38 +00:00
CmdlineArgList *arglist = cmdline_arg_list_from_GetCommandLineW();
size_t arglistpos = 0;
while (arglist->args[arglistpos]) {
CmdlineArg *arg = arglist->args[arglistpos++];
CmdlineArg *nextarg = arglist->args[arglistpos];
const char *p = cmdline_arg_to_str(arg);
int ret = cmdline_process_param(arg, nextarg, 1, conf);
if (ret == -2) {
cmdline_error("option \"%s\" requires an argument", p);
} else if (ret == 2) {
New abstraction for command-line arguments. This begins the process of enabling our Windows applications to handle Unicode characters on their command lines which don't fit in the system code page. Instead of passing plain strings to cmdline_process_param, we now pass a partially opaque and platform-specific thing called a CmdlineArg. This has a method that extracts the argument word as a default-encoded string, and another one that tries to extract it as UTF-8 (though it may fail if the UTF-8 isn't available). On Windows, the command line is now constructed by calling split_into_argv_w on the Unicode command line returned by GetCommandLineW(), and the UTF-8 method returns text converted directly from that wide-character form, not going via the system code page. So it _can_ include UTF-8 characters that wouldn't have round-tripped via CP_ACP. This commit introduces the abstraction and switches over the cross-platform and Windows argv-handling code to use it, with minimal functional change. Nothing yet tries to call cmdline_arg_get_utf8(). I say 'cross-platform and Windows' because on the Unix side there's still a lot of use of plain old argv which I haven't converted. That would be a much larger project, and isn't currently needed: the _current_ aim of this abstraction is to get the right things to happen relating to Unicode on Windows, so for code that doesn't run on Windows anyway, it's not adding value. (Also there's a tension with GTK, which wants to talk to standard argv and extract arguments _it_ knows about, so at the very least we'd have to let it munge argv before importing it into this new system.)
2024-09-25 09:18:38 +00:00
arglistpos++; /* skip next argument */
} else if (ret == 1) {
continue; /* nothing further needs doing */
} else if (!strcmp(p, "-cleanup")) {
/*
* `putty -cleanup'. Remove all registry
* entries associated with PuTTY, and also find
* and delete the random seed file.
*/
char *s1, *s2;
s1 = dupprintf("This procedure will remove ALL Registry entries\n"
"associated with %s, and will also remove\n"
"the random seed file. (This only affects the\n"
"currently logged-in user.)\n"
"\n"
"THIS PROCESS WILL DESTROY YOUR SAVED SESSIONS.\n"
"Are you really sure you want to continue?",
appname);
s2 = dupprintf("%s Warning", appname);
if (message_box(NULL, s1, s2,
MB_YESNO | MB_ICONWARNING | MB_DEFBUTTON2,
false, HELPCTXID(option_cleanup)) == IDYES) {
cleanup_all();
}
sfree(s1);
sfree(s2);
exit(0);
} else if (!strcmp(p, "-pgpfp")) {
pgp_fingerprints_msgbox(NULL);
exit(0);
} else if (has_ca_config_box &&
(!strcmp(p, "-host-ca") || !strcmp(p, "--host-ca") ||
!strcmp(p, "-host_ca") || !strcmp(p, "--host_ca"))) {
show_ca_config_box(NULL);
exit(0);
} else if (!strcmp(p, "-demo-config-box")) {
New abstraction for command-line arguments. This begins the process of enabling our Windows applications to handle Unicode characters on their command lines which don't fit in the system code page. Instead of passing plain strings to cmdline_process_param, we now pass a partially opaque and platform-specific thing called a CmdlineArg. This has a method that extracts the argument word as a default-encoded string, and another one that tries to extract it as UTF-8 (though it may fail if the UTF-8 isn't available). On Windows, the command line is now constructed by calling split_into_argv_w on the Unicode command line returned by GetCommandLineW(), and the UTF-8 method returns text converted directly from that wide-character form, not going via the system code page. So it _can_ include UTF-8 characters that wouldn't have round-tripped via CP_ACP. This commit introduces the abstraction and switches over the cross-platform and Windows argv-handling code to use it, with minimal functional change. Nothing yet tries to call cmdline_arg_get_utf8(). I say 'cross-platform and Windows' because on the Unix side there's still a lot of use of plain old argv which I haven't converted. That would be a much larger project, and isn't currently needed: the _current_ aim of this abstraction is to get the right things to happen relating to Unicode on Windows, so for code that doesn't run on Windows anyway, it's not adding value. (Also there's a tension with GTK, which wants to talk to standard argv and extract arguments _it_ knows about, so at the very least we'd have to let it munge argv before importing it into this new system.)
2024-09-25 09:18:38 +00:00
if (!arglist->args[arglistpos]) {
cmdline_error("%s expects an output filename", p);
} else {
demo_config_box = true;
New abstraction for command-line arguments. This begins the process of enabling our Windows applications to handle Unicode characters on their command lines which don't fit in the system code page. Instead of passing plain strings to cmdline_process_param, we now pass a partially opaque and platform-specific thing called a CmdlineArg. This has a method that extracts the argument word as a default-encoded string, and another one that tries to extract it as UTF-8 (though it may fail if the UTF-8 isn't available). On Windows, the command line is now constructed by calling split_into_argv_w on the Unicode command line returned by GetCommandLineW(), and the UTF-8 method returns text converted directly from that wide-character form, not going via the system code page. So it _can_ include UTF-8 characters that wouldn't have round-tripped via CP_ACP. This commit introduces the abstraction and switches over the cross-platform and Windows argv-handling code to use it, with minimal functional change. Nothing yet tries to call cmdline_arg_get_utf8(). I say 'cross-platform and Windows' because on the Unix side there's still a lot of use of plain old argv which I haven't converted. That would be a much larger project, and isn't currently needed: the _current_ aim of this abstraction is to get the right things to happen relating to Unicode on Windows, so for code that doesn't run on Windows anyway, it's not adding value. (Also there's a tension with GTK, which wants to talk to standard argv and extract arguments _it_ knows about, so at the very least we'd have to let it munge argv before importing it into this new system.)
2024-09-25 09:18:38 +00:00
dialog_box_demo_screenshot_filename =
cmdline_arg_to_filename(arglist->args[arglistpos++]);
}
} else if (!strcmp(p, "-demo-terminal")) {
New abstraction for command-line arguments. This begins the process of enabling our Windows applications to handle Unicode characters on their command lines which don't fit in the system code page. Instead of passing plain strings to cmdline_process_param, we now pass a partially opaque and platform-specific thing called a CmdlineArg. This has a method that extracts the argument word as a default-encoded string, and another one that tries to extract it as UTF-8 (though it may fail if the UTF-8 isn't available). On Windows, the command line is now constructed by calling split_into_argv_w on the Unicode command line returned by GetCommandLineW(), and the UTF-8 method returns text converted directly from that wide-character form, not going via the system code page. So it _can_ include UTF-8 characters that wouldn't have round-tripped via CP_ACP. This commit introduces the abstraction and switches over the cross-platform and Windows argv-handling code to use it, with minimal functional change. Nothing yet tries to call cmdline_arg_get_utf8(). I say 'cross-platform and Windows' because on the Unix side there's still a lot of use of plain old argv which I haven't converted. That would be a much larger project, and isn't currently needed: the _current_ aim of this abstraction is to get the right things to happen relating to Unicode on Windows, so for code that doesn't run on Windows anyway, it's not adding value. (Also there's a tension with GTK, which wants to talk to standard argv and extract arguments _it_ knows about, so at the very least we'd have to let it munge argv before importing it into this new system.)
2024-09-25 09:18:38 +00:00
if (!arglist->args[arglistpos] ||
!arglist->args[arglistpos+1]) {
cmdline_error("%s expects input and output filenames", p);
} else {
New abstraction for command-line arguments. This begins the process of enabling our Windows applications to handle Unicode characters on their command lines which don't fit in the system code page. Instead of passing plain strings to cmdline_process_param, we now pass a partially opaque and platform-specific thing called a CmdlineArg. This has a method that extracts the argument word as a default-encoded string, and another one that tries to extract it as UTF-8 (though it may fail if the UTF-8 isn't available). On Windows, the command line is now constructed by calling split_into_argv_w on the Unicode command line returned by GetCommandLineW(), and the UTF-8 method returns text converted directly from that wide-character form, not going via the system code page. So it _can_ include UTF-8 characters that wouldn't have round-tripped via CP_ACP. This commit introduces the abstraction and switches over the cross-platform and Windows argv-handling code to use it, with minimal functional change. Nothing yet tries to call cmdline_arg_get_utf8(). I say 'cross-platform and Windows' because on the Unix side there's still a lot of use of plain old argv which I haven't converted. That would be a much larger project, and isn't currently needed: the _current_ aim of this abstraction is to get the right things to happen relating to Unicode on Windows, so for code that doesn't run on Windows anyway, it's not adding value. (Also there's a tension with GTK, which wants to talk to standard argv and extract arguments _it_ knows about, so at the very least we'd have to let it munge argv before importing it into this new system.)
2024-09-25 09:18:38 +00:00
const char *infile =
cmdline_arg_to_str(arglist->args[arglistpos++]);
terminal_demo_screenshot_filename =
cmdline_arg_to_filename(arglist->args[arglistpos++]);
FILE *fp = fopen(infile, "rb");
if (!fp)
cmdline_error("can't open input file '%s'", infile);
demo_terminal_data = strbuf_new();
char buf[4096];
int retd;
while ((retd = fread(buf, 1, sizeof(buf), fp)) > 0)
put_data(demo_terminal_data, buf, retd);
fclose(fp);
}
} else if (*p != '-') {
cmdline_error("unexpected argument \"%s\"", p);
} else {
cmdline_error("unknown option \"%s\"", p);
}
}
}
cmdline_run_saved(conf);
if (demo_config_box) {
sesslist_demo_mode = true;
load_open_settings(NULL, conf);
conf_set_str(conf, CONF_host, "demo-server.example.com");
do_config(conf);
cleanup_exit(0);
} else if (demo_terminal_data) {
/* Ensure conf will cause an immediate session launch */
load_open_settings(NULL, conf);
conf_set_str(conf, CONF_host, "demo-server.example.com");
conf_set_int(conf, CONF_close_on_exit, FORCE_OFF);
} else {
/*
* Bring up the config dialog if the command line hasn't
* (explicitly) specified a launchable configuration.
*/
if (!(special_launchable_argument || cmdline_host_ok(conf))) {
if (!do_config(conf))
cleanup_exit(0);
}
}
prepare_session(conf);
}
const struct BackendVtable *backend_vt_from_conf(Conf *conf)
{
if (demo_terminal_data) {
return &null_backend;
}
/*
* Select protocol. This is farmed out into a table in a
* separate file to enable an ssh-free variant.
*/
const struct BackendVtable *vt = backend_vt_from_proto(
conf_get_int(conf, CONF_protocol));
if (!vt) {
char *str = dupprintf("%s Internal Error", appname);
MessageBox(NULL, "Unsupported protocol number found",
str, MB_OK | MB_ICONEXCLAMATION);
sfree(str);
cleanup_exit(1);
}
return vt;
}
const wchar_t *get_app_user_model_id(void)
{
return L"SimonTatham.PuTTY";
}
static void demo_terminal_screenshot(void *ctx, unsigned long now)
{
HWND hwnd = (HWND)ctx;
char *err = save_screenshot(hwnd, terminal_demo_screenshot_filename);
if (err) {
MessageBox(hwnd, err, "Demo screenshot failure", MB_OK | MB_ICONERROR);
sfree(err);
}
cleanup_exit(0);
}
void gui_terminal_ready(HWND hwnd, Seat *seat, Backend *backend)
{
if (demo_terminal_data) {
ptrlen data = ptrlen_from_strbuf(demo_terminal_data);
seat_stdout(seat, data.ptr, data.len);
schedule_timer(TICKSPERSEC, demo_terminal_screenshot, (void *)hwnd);
}
}