mirror of
https://git.tartarus.org/simon/putty.git
synced 2025-07-02 03:52:49 -05:00
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.)
This commit is contained in:
@ -6,6 +6,7 @@ add_sources_from_current_dir(utils
|
||||
utils/arm_arch_queries.c
|
||||
utils/aux_match_opt.c
|
||||
utils/centre_window.c
|
||||
utils/cmdline_arg.c
|
||||
utils/cryptoapi.c
|
||||
utils/defaults.c
|
||||
utils/dll_hijacking_protection.c
|
||||
|
@ -1537,8 +1537,6 @@ int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show)
|
||||
const char *command = NULL;
|
||||
const char *unixsocket = NULL;
|
||||
bool show_keylist_on_startup = false;
|
||||
int argc;
|
||||
char **argv, **argstart;
|
||||
const char *openssh_config_file = NULL;
|
||||
|
||||
typedef struct CommandLineKey {
|
||||
@ -1598,24 +1596,23 @@ int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show)
|
||||
* started up the main agent. Details of keys to be added are
|
||||
* stored in the 'clkeys' array.
|
||||
*/
|
||||
split_into_argv(cmdline, false, &argc, &argv, &argstart);
|
||||
bool add_keys_encrypted = false;
|
||||
AuxMatchOpt amo = aux_match_opt_init(argc, argv, 0, opt_error);
|
||||
AuxMatchOpt amo = aux_match_opt_init(opt_error);
|
||||
while (!aux_match_done(&amo)) {
|
||||
char *val;
|
||||
CmdlineArg *valarg;
|
||||
#define match_opt(...) aux_match_opt( \
|
||||
&amo, NULL, __VA_ARGS__, (const char *)NULL)
|
||||
#define match_optval(...) aux_match_opt( \
|
||||
&amo, &val, __VA_ARGS__, (const char *)NULL)
|
||||
&amo, &valarg, __VA_ARGS__, (const char *)NULL)
|
||||
|
||||
if (aux_match_arg(&amo, &val)) {
|
||||
if (aux_match_arg(&amo, &valarg)) {
|
||||
/*
|
||||
* Non-option arguments are expected to be key files, and
|
||||
* added to clkeys.
|
||||
*/
|
||||
sgrowarray(clkeys, clkeysize, nclkeys);
|
||||
CommandLineKey *clkey = &clkeys[nclkeys++];
|
||||
clkey->fn = filename_from_str(val);
|
||||
clkey->fn = filename_from_str(cmdline_arg_to_str(valarg));
|
||||
clkey->add_encrypted = add_keys_encrypted;
|
||||
} else if (match_opt("-pgpfp")) {
|
||||
pgp_fingerprints_msgbox(NULL);
|
||||
@ -1631,21 +1628,26 @@ int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show)
|
||||
} else if (match_opt("-keylist")) {
|
||||
show_keylist_on_startup = true;
|
||||
} else if (match_optval("-openssh-config", "-openssh_config")) {
|
||||
openssh_config_file = val;
|
||||
openssh_config_file = cmdline_arg_to_str(valarg);
|
||||
} else if (match_optval("-unix")) {
|
||||
unixsocket = val;
|
||||
unixsocket = cmdline_arg_to_str(valarg);
|
||||
} else if (match_opt("-c")) {
|
||||
/*
|
||||
* If we see `-c', then the rest of the command line
|
||||
* should be treated as a command to be spawned.
|
||||
*/
|
||||
if (amo.index < amo.argc)
|
||||
command = argstart[amo.index];
|
||||
else
|
||||
if (amo.arglist->args[amo.index]) {
|
||||
/* UNICODE: should use the UTF-8 or wide version, and
|
||||
* CreateProcessW, to pass through arbitrary command lines */
|
||||
command = cmdline_arg_remainder_acp(
|
||||
amo.arglist->args[amo.index]);
|
||||
} else {
|
||||
command = "";
|
||||
}
|
||||
break;
|
||||
} else {
|
||||
opt_error("unrecognised option '%s'\n", amo.argv[amo.index]);
|
||||
opt_error("unrecognised option '%s'\n",
|
||||
cmdline_arg_to_str(amo.arglist->args[amo.index]));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -848,15 +848,15 @@ void unlock_interprocess_mutex(HANDLE mutex);
|
||||
|
||||
typedef void (*aux_opt_error_fn_t)(const char *, ...);
|
||||
typedef struct AuxMatchOpt {
|
||||
int index, argc;
|
||||
char **argv;
|
||||
CmdlineArgList *arglist;
|
||||
size_t index;
|
||||
bool doing_opts;
|
||||
aux_opt_error_fn_t error;
|
||||
} AuxMatchOpt;
|
||||
AuxMatchOpt aux_match_opt_init(int argc, char **argv, int start_index,
|
||||
aux_opt_error_fn_t opt_error);
|
||||
bool aux_match_arg(AuxMatchOpt *amo, char **val);
|
||||
bool aux_match_opt(AuxMatchOpt *amo, char **val, const char *optname, ...);
|
||||
AuxMatchOpt aux_match_opt_init(aux_opt_error_fn_t opt_error);
|
||||
bool aux_match_arg(AuxMatchOpt *amo, CmdlineArg **val);
|
||||
bool aux_match_opt(AuxMatchOpt *amo, CmdlineArg **val,
|
||||
const char *optname, ...);
|
||||
bool aux_match_done(AuxMatchOpt *amo);
|
||||
|
||||
char *save_screenshot(HWND hwnd, const char *outfile);
|
||||
@ -864,4 +864,11 @@ void gui_terminal_ready(HWND hwnd, Seat *seat, Backend *backend);
|
||||
|
||||
void setup_gui_timing(void);
|
||||
|
||||
/* Windows-specific extra functions in cmdline_arg.c */
|
||||
CmdlineArgList *cmdline_arg_list_from_GetCommandLineW(void);
|
||||
const wchar_t *cmdline_arg_remainder_wide(CmdlineArg *);
|
||||
char *cmdline_arg_remainder_acp(CmdlineArg *);
|
||||
char *cmdline_arg_remainder_utf8(CmdlineArg *);
|
||||
CmdlineArg *cmdline_arg_from_utf8(CmdlineArgList *list, const char *string);
|
||||
|
||||
#endif /* PUTTY_WINDOWS_PLATFORM_H */
|
||||
|
@ -328,16 +328,19 @@ int main(int argc, char **argv)
|
||||
}
|
||||
}
|
||||
}
|
||||
while (--argc) {
|
||||
char *p = *++argv;
|
||||
int ret = cmdline_process_param(p, (argc > 1 ? argv[1] : NULL),
|
||||
1, conf);
|
||||
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) {
|
||||
fprintf(stderr,
|
||||
"plink: option \"%s\" requires an argument\n", p);
|
||||
errors = true;
|
||||
} else if (ret == 2) {
|
||||
--argc, ++argv;
|
||||
arglistpos++;
|
||||
} else if (ret == 1) {
|
||||
continue;
|
||||
} else if (!strcmp(p, "-s")) {
|
||||
@ -369,12 +372,11 @@ int main(int argc, char **argv)
|
||||
} else if (*p != '-') {
|
||||
strbuf *cmdbuf = strbuf_new();
|
||||
|
||||
while (argc > 0) {
|
||||
while (arg) {
|
||||
if (cmdbuf->len > 0)
|
||||
put_byte(cmdbuf, ' '); /* add space separator */
|
||||
put_dataz(cmdbuf, p);
|
||||
if (--argc > 0)
|
||||
p = *++argv;
|
||||
put_dataz(cmdbuf, cmdline_arg_to_str(arg));
|
||||
arg = arglist->args[arglistpos++];
|
||||
}
|
||||
|
||||
conf_set_str(conf, CONF_remote_cmd, cmdbuf->s);
|
||||
|
@ -8,13 +8,15 @@
|
||||
|
||||
static const PsocksPlatform platform = {
|
||||
NULL /* open_pipes */,
|
||||
NULL /* found_subcommand */,
|
||||
NULL /* start_subcommand */,
|
||||
};
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
psocks_state *ps = psocks_new(&platform);
|
||||
psocks_cmdline(ps, argc, argv);
|
||||
CmdlineArgList *arglist = cmdline_arg_list_from_GetCommandLineW();
|
||||
psocks_cmdline(ps, arglist);
|
||||
|
||||
sk_init();
|
||||
winselcli_setup();
|
||||
|
@ -15,33 +15,34 @@ void gui_term_process_cmdline(Conf *conf, char *cmdline)
|
||||
handle_special_filemapping_cmdline(cmdline, conf))
|
||||
return;
|
||||
|
||||
int argc;
|
||||
char **argv, **argstart;
|
||||
split_into_argv(cmdline, false, &argc, &argv, &argstart);
|
||||
|
||||
for (int i = 0; i < argc; i++) {
|
||||
char *arg = argv[i];
|
||||
int retd = cmdline_process_param(
|
||||
arg, i+1<argc?argv[i+1]:NULL, 1, conf);
|
||||
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 *argstr = cmdline_arg_to_str(arg);
|
||||
int retd = cmdline_process_param(arg, nextarg, 1, conf);
|
||||
if (retd == -2) {
|
||||
cmdline_error("option \"%s\" requires an argument", arg);
|
||||
cmdline_error("option \"%s\" requires an argument", argstr);
|
||||
} else if (retd == 2) {
|
||||
i++; /* skip next argument */
|
||||
arglistpos++; /* skip next argument */
|
||||
} else if (retd == 1) {
|
||||
continue; /* nothing further needs doing */
|
||||
} else if (!strcmp(arg, "-e")) {
|
||||
if (i+1 < argc) {
|
||||
} else if (!strcmp(argstr, "-e")) {
|
||||
if (nextarg) {
|
||||
/* 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]);
|
||||
char *cmd = cmdline_arg_remainder_acp(nextarg);
|
||||
conf_set_str(conf, CONF_remote_cmd, cmd);
|
||||
sfree(cmd);
|
||||
return;
|
||||
} else {
|
||||
cmdline_error("option \"%s\" requires an argument", arg);
|
||||
cmdline_error("option \"%s\" requires an argument", argstr);
|
||||
}
|
||||
} else if (arg[0] == '-') {
|
||||
cmdline_error("unrecognised option \"%s\"", arg);
|
||||
} else if (argstr[0] == '-') {
|
||||
cmdline_error("unrecognised option \"%s\"", argstr);
|
||||
} else {
|
||||
cmdline_error("unexpected non-option argument \"%s\"", arg);
|
||||
cmdline_error("unexpected non-option argument \"%s\"", argstr);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -48,21 +48,17 @@ void gui_term_process_cmdline(Conf *conf, char *cmdline)
|
||||
* Otherwise, break up the command line and deal with
|
||||
* it sensibly.
|
||||
*/
|
||||
int argc, i;
|
||||
char **argv;
|
||||
|
||||
split_into_argv(cmdline, false, &argc, &argv, NULL);
|
||||
|
||||
for (i = 0; i < argc; i++) {
|
||||
char *p = argv[i];
|
||||
int ret;
|
||||
|
||||
ret = cmdline_process_param(p, i+1<argc?argv[i+1]:NULL,
|
||||
1, conf);
|
||||
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) {
|
||||
i++; /* skip next argument */
|
||||
arglistpos++; /* skip next argument */
|
||||
} else if (ret == 1) {
|
||||
continue; /* nothing further needs doing */
|
||||
} else if (!strcmp(p, "-cleanup")) {
|
||||
@ -98,18 +94,22 @@ void gui_term_process_cmdline(Conf *conf, char *cmdline)
|
||||
show_ca_config_box(NULL);
|
||||
exit(0);
|
||||
} else if (!strcmp(p, "-demo-config-box")) {
|
||||
if (i+1 >= argc) {
|
||||
if (!arglist->args[arglistpos]) {
|
||||
cmdline_error("%s expects an output filename", p);
|
||||
} else {
|
||||
demo_config_box = true;
|
||||
dialog_box_demo_screenshot_filename = argv[++i];
|
||||
dialog_box_demo_screenshot_filename =
|
||||
cmdline_arg_to_str(arglist->args[arglistpos++]);
|
||||
}
|
||||
} else if (!strcmp(p, "-demo-terminal")) {
|
||||
if (i+2 >= argc) {
|
||||
if (!arglist->args[arglistpos] ||
|
||||
!arglist->args[arglistpos+1]) {
|
||||
cmdline_error("%s expects input and output filenames", p);
|
||||
} else {
|
||||
const char *infile = argv[++i];
|
||||
terminal_demo_screenshot_filename = argv[++i];
|
||||
const char *infile =
|
||||
cmdline_arg_to_str(arglist->args[arglistpos++]);
|
||||
terminal_demo_screenshot_filename =
|
||||
cmdline_arg_to_str(arglist->args[arglistpos++]);
|
||||
FILE *fp = fopen(infile, "rb");
|
||||
if (!fp)
|
||||
cmdline_error("can't open input file '%s'", infile);
|
||||
|
@ -26,7 +26,7 @@
|
||||
#define DEFAULT_ECCURVE_INDEX 0
|
||||
#define DEFAULT_EDCURVE_INDEX 0
|
||||
|
||||
static char *cmdline_keyfile = NULL;
|
||||
static const char *cmdline_keyfile = NULL;
|
||||
static ptrlen cmdline_demo_keystr;
|
||||
static const char *demo_screenshot_filename = NULL;
|
||||
|
||||
@ -2392,8 +2392,6 @@ static NORETURN void opt_error(const char *fmt, ...)
|
||||
|
||||
int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show)
|
||||
{
|
||||
int argc;
|
||||
char **argv;
|
||||
int ret;
|
||||
struct InitialParams params[1];
|
||||
|
||||
@ -2417,27 +2415,26 @@ int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show)
|
||||
|
||||
save_params = ppk_save_default_parameters;
|
||||
|
||||
split_into_argv(cmdline, false, &argc, &argv, NULL);
|
||||
|
||||
int argbits = -1;
|
||||
AuxMatchOpt amo = aux_match_opt_init(argc, argv, 0, opt_error);
|
||||
AuxMatchOpt amo = aux_match_opt_init(opt_error);
|
||||
while (!aux_match_done(&amo)) {
|
||||
char *val;
|
||||
CmdlineArg *valarg;
|
||||
#define match_opt(...) aux_match_opt( \
|
||||
&amo, NULL, __VA_ARGS__, (const char *)NULL)
|
||||
#define match_optval(...) aux_match_opt( \
|
||||
&amo, &val, __VA_ARGS__, (const char *)NULL)
|
||||
&amo, &valarg, __VA_ARGS__, (const char *)NULL)
|
||||
|
||||
if (aux_match_arg(&amo, &val)) {
|
||||
if (aux_match_arg(&amo, &valarg)) {
|
||||
if (!cmdline_keyfile) {
|
||||
/*
|
||||
* Assume the first argument to be a private key file, and
|
||||
* attempt to load it.
|
||||
*/
|
||||
cmdline_keyfile = val;
|
||||
cmdline_keyfile = cmdline_arg_to_str(valarg);
|
||||
continue;
|
||||
} else {
|
||||
opt_error("unexpected extra argument '%s'\n", val);
|
||||
opt_error("unexpected extra argument '%s'\n",
|
||||
cmdline_arg_to_str(valarg));
|
||||
}
|
||||
} else if (match_opt("-pgpfp")) {
|
||||
pgp_fingerprints_msgbox(NULL);
|
||||
@ -2446,6 +2443,7 @@ int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show)
|
||||
"-restrictacl")) {
|
||||
restrict_process_acl();
|
||||
} else if (match_optval("-t")) {
|
||||
const char *val = cmdline_arg_to_str(valarg);
|
||||
if (!strcmp(val, "rsa") || !strcmp(val, "rsa2")) {
|
||||
params->keybutton = IDC_KEYSSH2RSA;
|
||||
} else if (!strcmp(val, "rsa1")) {
|
||||
@ -2466,8 +2464,9 @@ int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show)
|
||||
opt_error("unknown key type '%s'\n", val);
|
||||
}
|
||||
} else if (match_optval("-b")) {
|
||||
argbits = atoi(val);
|
||||
argbits = atoi(cmdline_arg_to_str(valarg));
|
||||
} else if (match_optval("-E")) {
|
||||
const char *val = cmdline_arg_to_str(valarg);
|
||||
if (!strcmp(val, "md5"))
|
||||
params->fptype = SSH_FPTYPE_MD5;
|
||||
else if (!strcmp(val, "sha256"))
|
||||
@ -2475,6 +2474,7 @@ int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show)
|
||||
else
|
||||
opt_error("unknown fingerprint type '%s'\n", val);
|
||||
} else if (match_optval("-primes")) {
|
||||
const char *val = cmdline_arg_to_str(valarg);
|
||||
if (!strcmp(val, "probable") ||
|
||||
!strcmp(val, "probabilistic")) {
|
||||
params->primepolicybutton = IDC_PRIMEGEN_PROB;
|
||||
@ -2495,6 +2495,7 @@ int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show)
|
||||
} else if (match_opt("-strong-rsa")) {
|
||||
params->rsa_strong = true;
|
||||
} else if (match_optval("-ppk-param", "-ppk-params")) {
|
||||
char *val = dupstr(cmdline_arg_to_str(valarg));
|
||||
char *nextval;
|
||||
for (; val; val = nextval) {
|
||||
nextval = strchr(val, ',');
|
||||
@ -2547,8 +2548,9 @@ int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show)
|
||||
opt_error("unrecognised PPK parameter '%s'\n", val);
|
||||
}
|
||||
}
|
||||
sfree(val);
|
||||
} else if (match_optval("-demo-screenshot")) {
|
||||
demo_screenshot_filename = val;
|
||||
demo_screenshot_filename = cmdline_arg_to_str(valarg);
|
||||
cmdline_demo_keystr = PTRLEN_LITERAL(
|
||||
"PuTTY-User-Key-File-3: ssh-ed25519\n"
|
||||
"Encryption: none\n"
|
||||
@ -2564,7 +2566,8 @@ int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show)
|
||||
params->keybutton = IDC_KEYSSH2EDDSA;
|
||||
argbits = 255;
|
||||
} else {
|
||||
opt_error("unrecognised option '%s'\n", amo.argv[amo.index]);
|
||||
opt_error("unrecognised option '%s'\n",
|
||||
cmdline_arg_to_str(amo.arglist->args[amo.index]));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -650,7 +650,8 @@ int main(int argc, char *argv[])
|
||||
|
||||
dll_hijacking_protection();
|
||||
|
||||
ret = psftp_main(argc, argv);
|
||||
CmdlineArgList *arglist = cmdline_arg_list_from_GetCommandLineW();
|
||||
ret = psftp_main(arglist);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
@ -17,20 +17,21 @@ int main(int argc, char **argv)
|
||||
{
|
||||
const char *outfile = NULL;
|
||||
|
||||
AuxMatchOpt amo = aux_match_opt_init(argc-1, argv+1, 0, fatal_error);
|
||||
AuxMatchOpt amo = aux_match_opt_init(fatal_error);
|
||||
while (!aux_match_done(&amo)) {
|
||||
char *val;
|
||||
CmdlineArg *val;
|
||||
#define match_opt(...) aux_match_opt( \
|
||||
&amo, NULL, __VA_ARGS__, (const char *)NULL)
|
||||
#define match_optval(...) aux_match_opt( \
|
||||
&amo, &val, __VA_ARGS__, (const char *)NULL)
|
||||
|
||||
if (aux_match_arg(&amo, &val)) {
|
||||
fatal_error("unexpected argument '%s'", val);
|
||||
fatal_error("unexpected argument '%s'", cmdline_arg_to_str(val));
|
||||
} else if (match_optval("-o", "--output")) {
|
||||
outfile = val;
|
||||
outfile = cmdline_arg_to_str(val);
|
||||
} else {
|
||||
fatal_error("unrecognised option '%s'\n", amo.argv[amo.index]);
|
||||
fatal_error("unrecognised option '%s'\n",
|
||||
cmdline_arg_to_str(amo.arglist->args[amo.index]));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -13,14 +13,12 @@
|
||||
/*
|
||||
* Call this to initialise the state structure.
|
||||
*/
|
||||
AuxMatchOpt aux_match_opt_init(int argc, char **argv, int start_index,
|
||||
aux_opt_error_fn_t opt_error)
|
||||
AuxMatchOpt aux_match_opt_init(aux_opt_error_fn_t opt_error)
|
||||
{
|
||||
AuxMatchOpt amo[1];
|
||||
|
||||
amo->argc = argc;
|
||||
amo->argv = argv;
|
||||
amo->index = start_index;
|
||||
amo->arglist = cmdline_arg_list_from_GetCommandLineW();
|
||||
amo->index = 0;
|
||||
amo->doing_opts = true;
|
||||
amo->error = opt_error;
|
||||
|
||||
@ -32,12 +30,14 @@ AuxMatchOpt aux_match_opt_init(int argc, char **argv, int start_index,
|
||||
* Point 'val' at a char * to receive the option argument, if one is
|
||||
* expected. Set 'val' to NULL if no argument is expected.
|
||||
*/
|
||||
bool aux_match_opt(AuxMatchOpt *amo, char **val, const char *optname, ...)
|
||||
bool aux_match_opt(AuxMatchOpt *amo, CmdlineArg **val,
|
||||
const char *optname, ...)
|
||||
{
|
||||
assert(amo->index < amo->argc);
|
||||
|
||||
/* Find the end of the option name */
|
||||
char *opt = amo->argv[amo->index];
|
||||
CmdlineArg *optarg = amo->arglist->args[amo->index];
|
||||
assert(optarg);
|
||||
const char *opt = cmdline_arg_to_utf8(optarg);
|
||||
|
||||
ptrlen argopt;
|
||||
argopt.ptr = opt;
|
||||
argopt.len = strcspn(opt, "=");
|
||||
@ -72,14 +72,14 @@ bool aux_match_opt(AuxMatchOpt *amo, char **val, const char *optname, ...)
|
||||
if (opt[argopt.len]) {
|
||||
if (!val)
|
||||
amo->error("option '%s' does not expect a value", opt);
|
||||
*val = opt + argopt.len + 1;
|
||||
*val = cmdline_arg_from_utf8(optarg->list, opt + argopt.len + 1);
|
||||
amo->index++;
|
||||
} else if (!val) {
|
||||
amo->index++;
|
||||
} else {
|
||||
if (amo->index + 1 >= amo->argc)
|
||||
if (!amo->arglist->args[amo->index + 1])
|
||||
amo->error("option '%s' expects a value", opt);
|
||||
*val = amo->argv[amo->index + 1];
|
||||
*val = amo->arglist->args[amo->index + 1];
|
||||
amo->index += 2;
|
||||
}
|
||||
|
||||
@ -89,13 +89,14 @@ bool aux_match_opt(AuxMatchOpt *amo, char **val, const char *optname, ...)
|
||||
/*
|
||||
* Call this to return a non-option argument in *val.
|
||||
*/
|
||||
bool aux_match_arg(AuxMatchOpt *amo, char **val)
|
||||
bool aux_match_arg(AuxMatchOpt *amo, CmdlineArg **val)
|
||||
{
|
||||
assert(amo->index < amo->argc);
|
||||
char *opt = amo->argv[amo->index];
|
||||
CmdlineArg *optarg = amo->arglist->args[amo->index];
|
||||
assert(optarg);
|
||||
const char *opt = cmdline_arg_to_utf8(optarg);
|
||||
|
||||
if (!amo->doing_opts || opt[0] != '-' || !strcmp(opt, "-")) {
|
||||
*val = opt;
|
||||
*val = optarg;
|
||||
amo->index++;
|
||||
return true;
|
||||
}
|
||||
@ -108,10 +109,12 @@ bool aux_match_arg(AuxMatchOpt *amo, char **val)
|
||||
*/
|
||||
bool aux_match_done(AuxMatchOpt *amo)
|
||||
{
|
||||
if (amo->index < amo->argc && !strcmp(amo->argv[amo->index], "--")) {
|
||||
CmdlineArg *optarg = amo->arglist->args[amo->index];
|
||||
const char *opt = cmdline_arg_to_utf8(optarg);
|
||||
if (opt && !strcmp(opt, "--")) {
|
||||
amo->doing_opts = false;
|
||||
amo->index++;
|
||||
}
|
||||
|
||||
return amo->index >= amo->argc;
|
||||
return amo->arglist->args[amo->index] == NULL;
|
||||
}
|
||||
|
209
windows/utils/cmdline_arg.c
Normal file
209
windows/utils/cmdline_arg.c
Normal file
@ -0,0 +1,209 @@
|
||||
/*
|
||||
* Implementation of the CmdlineArg abstraction for Windows
|
||||
*/
|
||||
|
||||
#include <wchar.h>
|
||||
#include "putty.h"
|
||||
|
||||
typedef struct CmdlineArgWin CmdlineArgWin;
|
||||
struct CmdlineArgWin {
|
||||
/*
|
||||
* The original wide-character argument.
|
||||
*/
|
||||
wchar_t *wide;
|
||||
|
||||
/*
|
||||
* Two translations of the wide-character argument into UTF-8
|
||||
* (maximally faithful to the original) and CP_ACP (the normal
|
||||
* system code page).
|
||||
*/
|
||||
char *utf8, *acp;
|
||||
|
||||
/*
|
||||
* Our index in the CmdlineArgList, or (size_t)-1 if we don't have
|
||||
* one and are an argument invented later.
|
||||
*/
|
||||
size_t index;
|
||||
|
||||
/*
|
||||
* Public part of the structure.
|
||||
*/
|
||||
CmdlineArg argp;
|
||||
};
|
||||
|
||||
typedef struct CmdlineArgListWin CmdlineArgListWin;
|
||||
struct CmdlineArgListWin {
|
||||
/*
|
||||
* Wide string pointer returned from GetCommandLineW. This points
|
||||
* to the 'official' version of the command line, in the sense
|
||||
* that overwriting it causes different text to show up in the
|
||||
* Task Manager display of the process's command line. (So this is
|
||||
* what we'll need to overwrite _on purpose_ for cmdline_arg_wipe.)
|
||||
*/
|
||||
wchar_t *cmdline;
|
||||
|
||||
/*
|
||||
* Data returned from split_into_argv_w.
|
||||
*/
|
||||
size_t argc;
|
||||
wchar_t **argv, **argstart;
|
||||
|
||||
/*
|
||||
* Public part of the structure.
|
||||
*/
|
||||
CmdlineArgList listp;
|
||||
};
|
||||
|
||||
static CmdlineArgWin *cmdline_arg_new_in_list(CmdlineArgList *listp)
|
||||
{
|
||||
CmdlineArgWin *arg = snew(CmdlineArgWin);
|
||||
arg->wide = NULL;
|
||||
arg->utf8 = arg->acp = NULL;
|
||||
arg->index = (size_t)-1;
|
||||
arg->argp.list = listp;
|
||||
sgrowarray(listp->args, listp->argssize, listp->nargs);
|
||||
listp->args[listp->nargs++] = &arg->argp;
|
||||
return arg;
|
||||
}
|
||||
|
||||
static CmdlineArg *cmdline_arg_from_wide_argv_word(
|
||||
CmdlineArgList *list, wchar_t *word)
|
||||
{
|
||||
CmdlineArgWin *arg = cmdline_arg_new_in_list(list);
|
||||
arg->wide = dupwcs(word);
|
||||
arg->utf8 = dup_wc_to_mb(CP_UTF8, word, "");
|
||||
arg->acp = dup_wc_to_mb(CP_ACP, word, "");
|
||||
return &arg->argp;
|
||||
}
|
||||
|
||||
CmdlineArgList *cmdline_arg_list_from_GetCommandLineW(void)
|
||||
{
|
||||
CmdlineArgListWin *list = snew(CmdlineArgListWin);
|
||||
CmdlineArgList *listp = &list->listp;
|
||||
|
||||
list->cmdline = GetCommandLineW();
|
||||
|
||||
int argc;
|
||||
split_into_argv_w(list->cmdline, true,
|
||||
&argc, &list->argv, &list->argstart);
|
||||
list->argc = (size_t)argc;
|
||||
|
||||
listp->args = NULL;
|
||||
listp->nargs = listp->argssize = 0;
|
||||
for (size_t i = 1; i < list->argc; i++) {
|
||||
CmdlineArg *argp = cmdline_arg_from_wide_argv_word(
|
||||
listp, list->argv[i]);
|
||||
CmdlineArgWin *arg = container_of(argp, CmdlineArgWin, argp);
|
||||
arg->index = i - 1; /* index in list->args[], not in argv[] */
|
||||
}
|
||||
sgrowarray(listp->args, listp->argssize, listp->nargs);
|
||||
listp->args[listp->nargs++] = NULL;
|
||||
return listp;
|
||||
}
|
||||
|
||||
void cmdline_arg_free(CmdlineArg *argp)
|
||||
{
|
||||
if (!argp)
|
||||
return;
|
||||
|
||||
CmdlineArgWin *arg = container_of(argp, CmdlineArgWin, argp);
|
||||
burnwcs(arg->wide);
|
||||
burnstr(arg->utf8);
|
||||
burnstr(arg->acp);
|
||||
sfree(arg);
|
||||
}
|
||||
|
||||
void cmdline_arg_list_free(CmdlineArgList *listp)
|
||||
{
|
||||
CmdlineArgListWin *list = container_of(listp, CmdlineArgListWin, listp);
|
||||
for (size_t i = 0; i < listp->nargs; i++)
|
||||
cmdline_arg_free(listp->args[i]);
|
||||
/* list->argv[0] points at the start of the string allocated by
|
||||
* split_into_argv_w */
|
||||
sfree(list->argv[0]);
|
||||
sfree(list->argv);
|
||||
sfree(list->argstart);
|
||||
sfree(list);
|
||||
}
|
||||
|
||||
CmdlineArg *cmdline_arg_from_str(CmdlineArgList *listp, const char *string)
|
||||
{
|
||||
CmdlineArgWin *arg = cmdline_arg_new_in_list(listp);
|
||||
arg->acp = dupstr(string);
|
||||
arg->wide = dup_mb_to_wc(CP_ACP, string);
|
||||
arg->utf8 = dup_wc_to_mb(CP_UTF8, arg->wide, "");
|
||||
return &arg->argp;
|
||||
}
|
||||
|
||||
CmdlineArg *cmdline_arg_from_utf8(CmdlineArgList *listp, const char *string)
|
||||
{
|
||||
CmdlineArgWin *arg = cmdline_arg_new_in_list(listp);
|
||||
arg->acp = dupstr(string);
|
||||
arg->wide = dup_mb_to_wc(CP_UTF8, string);
|
||||
arg->utf8 = dup_wc_to_mb(CP_ACP, arg->wide, "");
|
||||
return &arg->argp;
|
||||
}
|
||||
|
||||
const char *cmdline_arg_to_str(CmdlineArg *argp)
|
||||
{
|
||||
if (!argp)
|
||||
return NULL;
|
||||
|
||||
CmdlineArgWin *arg = container_of(argp, CmdlineArgWin, argp);
|
||||
return arg->acp;
|
||||
}
|
||||
|
||||
const char *cmdline_arg_to_utf8(CmdlineArg *argp)
|
||||
{
|
||||
if (!argp)
|
||||
return NULL;
|
||||
|
||||
CmdlineArgWin *arg = container_of(argp, CmdlineArgWin, argp);
|
||||
return arg->utf8;
|
||||
}
|
||||
|
||||
void cmdline_arg_wipe(CmdlineArg *argp)
|
||||
{
|
||||
if (!argp)
|
||||
return;
|
||||
|
||||
CmdlineArgWin *arg = container_of(argp, CmdlineArgWin, argp);
|
||||
if (arg->index != (size_t)-1) {
|
||||
CmdlineArgList *listp = argp->list;
|
||||
CmdlineArgListWin *list = container_of(
|
||||
listp, CmdlineArgListWin, listp);
|
||||
|
||||
/* arg->index starts from the first argument _after_ program
|
||||
* name, whereas argstart is indexed from argv[0] */
|
||||
wchar_t *p = list->argstart[arg->index + 1];
|
||||
wchar_t *end = (arg->index + 2 < list->argc ?
|
||||
list->argstart[arg->index + 2] :
|
||||
p + wcslen(p));
|
||||
while (p < end)
|
||||
*p++ = L' ';
|
||||
}
|
||||
}
|
||||
|
||||
const wchar_t *cmdline_arg_remainder_wide(CmdlineArg *argp)
|
||||
{
|
||||
CmdlineArgWin *arg = container_of(argp, CmdlineArgWin, argp);
|
||||
CmdlineArgList *listp = argp->list;
|
||||
CmdlineArgListWin *list = container_of(listp, CmdlineArgListWin, listp);
|
||||
|
||||
size_t index = arg->index;
|
||||
assert(index != (size_t)-1);
|
||||
|
||||
/* arg->index starts from the first argument _after_ program
|
||||
* name, whereas argstart is indexed from argv[0] */
|
||||
return list->argstart[index + 1];
|
||||
}
|
||||
|
||||
char *cmdline_arg_remainder_acp(CmdlineArg *argp)
|
||||
{
|
||||
return dup_wc_to_mb(CP_ACP, cmdline_arg_remainder_wide(argp), "");
|
||||
}
|
||||
|
||||
char *cmdline_arg_remainder_utf8(CmdlineArg *argp)
|
||||
{
|
||||
return dup_wc_to_mb(CP_UTF8, cmdline_arg_remainder_wide(argp), "");
|
||||
}
|
Reference in New Issue
Block a user