From 6a3b07c4415751bd35438266a20506e27d3ad728 Mon Sep 17 00:00:00 2001 From: Randy Heit Date: Fri, 11 Jan 2013 03:21:46 +0000 Subject: [PATCH] - Removed the BuildString() override that took a char** argv, because it was used nowhere. - Fixed: BuildString() failed to properly generate command lines for arguments with embedded " characters. SVN r4025 (trunk) --- src/c_dispatch.cpp | 46 ++++++++++++++++++---------------------------- src/c_dispatch.h | 1 - 2 files changed, 18 insertions(+), 29 deletions(-) diff --git a/src/c_dispatch.cpp b/src/c_dispatch.cpp index 83c6ae37df..7b701bd534 100644 --- a/src/c_dispatch.cpp +++ b/src/c_dispatch.cpp @@ -1019,32 +1019,7 @@ FConsoleAlias::~FConsoleAlias () m_Command[1] = m_Command[0] = FString(); } -FString BuildString (int argc, char **argv) -{ - if (argc == 1) - { - return *argv; - } - else - { - FString buf; - int arg; - - for (arg = 0; arg < argc; arg++) - { - if (strchr (argv[arg], ' ')) - { - buf.AppendFormat ("\"%s\" ", argv[arg]); - } - else - { - buf.AppendFormat ("%s ", argv[arg]); - } - } - return buf; - } -} - +// Given an argument vector, reconstitute the command line it could have been produced from. FString BuildString (int argc, FString *argv) { if (argc == 1) @@ -1058,8 +1033,23 @@ FString BuildString (int argc, FString *argv) for (arg = 0; arg < argc; arg++) { - if (strchr (argv[arg], ' ')) - { + if (strchr(argv[arg], '"')) + { // If it contains one or more quotes, we need to escape them. + buf << '"'; + long substr_start = 0, quotepos; + while ((quotepos = argv[arg].IndexOf('"', substr_start)) >= 0) + { + if (substr_start < quotepos) + { + buf << argv[arg].Mid(substr_start, quotepos - substr_start); + } + buf << "\\\""; + substr_start = quotepos + 1; + } + buf << argv[arg].Mid(substr_start) << "\" "; + } + else if (strchr(argv[arg], ' ')) + { // If it contains a space, it needs to be quoted. buf << '"' << argv[arg] << "\" "; } else diff --git a/src/c_dispatch.h b/src/c_dispatch.h index 5aa3f98702..12ea559de3 100644 --- a/src/c_dispatch.h +++ b/src/c_dispatch.h @@ -61,7 +61,6 @@ void C_SetAlias (const char *name, const char *cmd); void C_ClearAliases (); // build a single string out of multiple strings -FString BuildString (int argc, char **argv); FString BuildString (int argc, FString *argv); // Class that can parse command lines