1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-01-09 09:27:59 +00:00
putty-source/windows/utils/aux_match_opt.c
Simon Tatham 841bf321d4 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-26 11:30:07 +01:00

121 lines
3.3 KiB
C

/*
* Helper function for matching command-line options in the Windows
* auxiliary tools (PuTTYgen and Pageant).
*
* Supports basically the usual kinds of option, including GNUish
* --foo long options and simple -f short options. But historically
* those tools have also supported long options with a single dash, so
* we don't go full GNU and report those as syntax errors.
*/
#include "putty.h"
/*
* Call this to initialise the state structure.
*/
AuxMatchOpt aux_match_opt_init(aux_opt_error_fn_t opt_error)
{
AuxMatchOpt amo[1];
amo->arglist = cmdline_arg_list_from_GetCommandLineW();
amo->index = 0;
amo->doing_opts = true;
amo->error = opt_error;
return amo[0];
}
/*
* Call this with a NULL-terminated list of synonymous option names.
* 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, CmdlineArg **val,
const char *optname, ...)
{
/* Find the end of the option name */
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, "=");
/* Normalise GNU-style --foo long options to the -foo that this
* tool has historically used */
ptrlen argopt2 = make_ptrlen(NULL, 0);
if (ptrlen_startswith(argopt, PTRLEN_LITERAL("--"), NULL))
ptrlen_startswith(argopt, PTRLEN_LITERAL("-"), &argopt2);
/* See if this option matches any of our synonyms */
va_list ap;
va_start(ap, optname);
bool matched = false;
while (optname) {
if (ptrlen_eq_string(argopt, optname)) {
matched = true;
break;
}
if (argopt2.ptr && strlen(optname) > 2 &&
ptrlen_eq_string(argopt2, optname)) {
matched = true;
break;
}
optname = va_arg(ap, const char *);
}
va_end(ap);
if (!matched)
return false;
/* Check for a value */
if (opt[argopt.len]) {
if (!val)
amo->error("option '%s' does not expect a value", opt);
*val = cmdline_arg_from_utf8(optarg->list, opt + argopt.len + 1);
amo->index++;
} else if (!val) {
amo->index++;
} else {
if (!amo->arglist->args[amo->index + 1])
amo->error("option '%s' expects a value", opt);
*val = amo->arglist->args[amo->index + 1];
amo->index += 2;
}
return true;
}
/*
* Call this to return a non-option argument in *val.
*/
bool aux_match_arg(AuxMatchOpt *amo, CmdlineArg **val)
{
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 = optarg;
amo->index++;
return true;
}
return false;
}
/*
* And call this to test whether we're done. Also handles '--'.
*/
bool aux_match_done(AuxMatchOpt *amo)
{
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->arglist->args[amo->index] == NULL;
}