mirror of
https://git.tartarus.org/simon/putty.git
synced 2025-01-09 17:38:00 +00:00
split_into_argv: add special case for program name.
In the Windows API, there are two places you can get a command line in the form of a single unsplit string. One is via the command-line parameter to WinMain(); the other is by calling GetCommandLine(). But the two have different semantics: the WinMain command line string is only the part after the program name, whereas GetCommandLine() returns the full command line _including_ the program name. PuTTY has never yet had to parse the full output of GetCommandLine, but I have plans that will involve it beginning to do so. So I need to make sure the utility function split_into_argv() can handle it. This is not trivial because the quoting convention is different for the program name than for everything else. In the program's normal arguments, parsed by the C library startup code, the convention is that backslashes are special when they appear before a double quote, because that's how you write a literal double quote. But in the program name, backslashes are _never_ special, because that's how CreateProcess parses the program name at the start of the command line, and the C library must follow suit in order to correctly identify where the program name ends and the arguments begin. In particular, consider a command line such as this: "C:\Program Files\Foo\"foo.exe "hello \"world\"" The \" in the middle of the program name must be treated as a literal backslash, followed by a non-literal double quote which matches the one at the start of the string and causes the space in 'Program Files' to be treated as part of the pathname. But the same \" when it appears in the subsequent argument is treated as an escaped double quote, and turns into a literal " in the argument string. This commit adds support for this special initial-word handling in split_into_argv(), via an extra boolean argument indicating whether to turn that mode on. However, all existing call sites set the flag to false, because the new mode isn't needed _yet_. So there should be no functional change.
This commit is contained in:
parent
dbd0bde415
commit
f91c3127ad
@ -1598,7 +1598,7 @@ 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, &argc, &argv, &argstart);
|
||||
split_into_argv(cmdline, false, &argc, &argv, &argstart);
|
||||
bool add_keys_encrypted = false;
|
||||
AuxMatchOpt amo = aux_match_opt_init(argc, argv, 0, opt_error);
|
||||
while (!aux_match_done(&amo)) {
|
||||
|
@ -399,7 +399,8 @@ int message_box(HWND owner, LPCTSTR text, LPCTSTR caption,
|
||||
DWORD style, DWORD helpctxid);
|
||||
void MakeDlgItemBorderless(HWND parent, int id);
|
||||
char *GetDlgItemText_alloc(HWND hwnd, int id);
|
||||
void split_into_argv(char *, int *, char ***, char ***);
|
||||
void split_into_argv(char *, bool includes_program_name,
|
||||
int *, char ***, char ***);
|
||||
|
||||
/*
|
||||
* Private structure for prefslist state. Only in the header file
|
||||
|
@ -17,7 +17,7 @@ void gui_term_process_cmdline(Conf *conf, char *cmdline)
|
||||
|
||||
int argc;
|
||||
char **argv, **argstart;
|
||||
split_into_argv(cmdline, &argc, &argv, &argstart);
|
||||
split_into_argv(cmdline, false, &argc, &argv, &argstart);
|
||||
|
||||
for (int i = 0; i < argc; i++) {
|
||||
char *arg = argv[i];
|
||||
|
@ -51,7 +51,7 @@ void gui_term_process_cmdline(Conf *conf, char *cmdline)
|
||||
int argc, i;
|
||||
char **argv;
|
||||
|
||||
split_into_argv(cmdline, &argc, &argv, NULL);
|
||||
split_into_argv(cmdline, false, &argc, &argv, NULL);
|
||||
|
||||
for (i = 0; i < argc; i++) {
|
||||
char *p = argv[i];
|
||||
|
@ -2417,7 +2417,7 @@ int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show)
|
||||
|
||||
save_params = ppk_save_default_parameters;
|
||||
|
||||
split_into_argv(cmdline, &argc, &argv, NULL);
|
||||
split_into_argv(cmdline, false, &argc, &argv, NULL);
|
||||
|
||||
int argbits = -1;
|
||||
AuxMatchOpt amo = aux_match_opt_init(argc, argv, 0, opt_error);
|
||||
|
@ -161,8 +161,8 @@
|
||||
#define MOD3 0
|
||||
#endif
|
||||
|
||||
void split_into_argv(char *cmdline, int *argc, char ***argv,
|
||||
char ***argstart)
|
||||
void split_into_argv(char *cmdline, bool includes_program_name,
|
||||
int *argc, char ***argv, char ***argstart)
|
||||
{
|
||||
char *p;
|
||||
char *outputline, *q;
|
||||
@ -198,6 +198,40 @@ void split_into_argv(char *cmdline, int *argc, char ***argv,
|
||||
while (*p && isspace(*p)) p++;
|
||||
if (!*p) break;
|
||||
|
||||
/*
|
||||
* Check if this argument is the program name. If so,
|
||||
* different rules apply.
|
||||
*
|
||||
* In most arguments, the special characters are the double
|
||||
* quote and the backslash. An exception is the program name
|
||||
* at the start of the command line, in which backslashes are
|
||||
* _not_ special - if one appears before a quote, it does not
|
||||
* make the quote literal.
|
||||
*
|
||||
* The C library must implement this special rule, and we must
|
||||
* follow suit here, in order to match the way CreateProcess
|
||||
* scans the command line to determine the program name. It
|
||||
* will consider that all these commands refer to the same
|
||||
* file equally validly:
|
||||
*
|
||||
* "C:\Program Files\Foo"\bar.exe
|
||||
* "C:\Program Files\Foo\"bar.exe
|
||||
* "C:\Program Files\Foo\bar.exe"
|
||||
*
|
||||
* Each one contains a quoted section that protects the space
|
||||
* in "Program Files", and the closing quote takes effect the
|
||||
* same in all cases - even though, in the middle case, it's
|
||||
* immediately preceded by one of the path separators in the
|
||||
* name. For CreateProcess, backslashes aren't special.
|
||||
*
|
||||
* So, if our caller told us that the input command line
|
||||
* includes the program name (which it does if it came from
|
||||
* GetCommandLine, but not if it was passed in to WinMain),
|
||||
* then we must treat the 0th output argument specially, by
|
||||
* not considering backslashes to affect the quoting.
|
||||
*/
|
||||
bool backslash_special = !(outputargc == 0 && includes_program_name);
|
||||
|
||||
/* We have an argument; start it. */
|
||||
outputargv[outputargc] = q;
|
||||
outputargstart[outputargc] = p;
|
||||
@ -209,7 +243,7 @@ void split_into_argv(char *cmdline, int *argc, char ***argv,
|
||||
if (!quote && isspace(*p))
|
||||
break; /* argument is finished */
|
||||
|
||||
if (*p == '"' || *p == '\\') {
|
||||
if (*p == '"' || (*p == '\\' && backslash_special)) {
|
||||
/*
|
||||
* We have a sequence of zero or more backslashes
|
||||
* followed by a sequence of zero or more quotes.
|
||||
@ -273,6 +307,7 @@ void split_into_argv(char *cmdline, int *argc, char ***argv,
|
||||
const struct argv_test {
|
||||
const char *cmdline;
|
||||
const char *argv[10];
|
||||
bool include_program_name;
|
||||
} argv_tests[] = {
|
||||
/*
|
||||
* We generate this set of tests by invoking ourself with
|
||||
@ -463,6 +498,9 @@ const struct argv_test {
|
||||
{"\"a\\\\\\\\\"\"\"\"\"\"\"b c\" d", {"a\\\\\"\"b", "c d", NULL}},
|
||||
{"\"a\\\\\\\\\"\"\"\"\"\"\"\"b c\" d", {"a\\\\\"\"\"b", "c d", NULL}},
|
||||
#endif /* MOD3 */
|
||||
/* Common tests that check the special program-name rule. */
|
||||
{"\"a b\\\"c \"d e\" \"f g\"", {"a b\\c", "d e", "f g", NULL}, true},
|
||||
{"\"a b\\\"c \"d e\" \"f g\"", {"a b\"c d", "e f", "g", NULL}, false},
|
||||
};
|
||||
|
||||
void out_of_memory(void)
|
||||
@ -658,7 +696,8 @@ int main(int argc, char **argv)
|
||||
char **av;
|
||||
bool failed = false;
|
||||
|
||||
split_into_argv((char *)argv_tests[i].cmdline, &ac, &av, NULL);
|
||||
split_into_argv((char *)argv_tests[i].cmdline,
|
||||
argv_tests[i].include_program_name, &ac, &av, NULL);
|
||||
|
||||
for (j = 0; j < ac && argv_tests[i].argv[j]; j++) {
|
||||
if (strcmp(av[j], argv_tests[i].argv[j])) {
|
||||
|
Loading…
Reference in New Issue
Block a user