-
Notifications
You must be signed in to change notification settings - Fork 323
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixing the issues related to response file. (#1196)
Fixing the issues related to response file
- Loading branch information
1 parent
2437088
commit 073c64a
Showing
3 changed files
with
117 additions
and
191 deletions.
There are no files selected for viewing
197 changes: 74 additions & 123 deletions
197
src/Microsoft.TestPlatform.Utilities/CommandLineUtilities.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,157 +1,108 @@ | ||
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
// Code taken from: https://github.com/dotnet/roslyn/blob/d00363d8f892f4f3c514718a964ea37783d21de5/src/Compilers/Core/Portable/InternalUtilities/CommandLineUtilities.cs | ||
|
||
namespace Microsoft.VisualStudio.TestPlatform.Utilities | ||
{ | ||
using Microsoft.VisualStudio.TestPlatform.ObjectModel; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Globalization; | ||
using System.IO; | ||
using System.Text; | ||
|
||
public static class CommandLineUtilities | ||
{ | ||
/// <summary> | ||
/// Split a command line by the same rules as Main would get the commands except the original | ||
/// state of backslashes and quotes are preserved. For example in normal Windows command line | ||
/// parsing the following command lines would produce equivalent Main arguments: | ||
/// | ||
/// - /r:a,b | ||
/// - /r:"a,b" | ||
/// | ||
/// This method will differ as the latter will have the quotes preserved. The only case where | ||
/// quotes are removed is when the entire argument is surrounded by quotes without any inner | ||
/// quotes. | ||
/// </summary> | ||
/// <remarks> | ||
/// Rules for command line parsing, according to MSDN: | ||
/// | ||
/// Arguments are delimited by white space, which is either a space or a tab. | ||
/// | ||
/// A string surrounded by double quotation marks ("string") is interpreted | ||
/// as a single argument, regardless of white space contained within. | ||
/// A quoted string can be embedded in an argument. | ||
/// | ||
/// A double quotation mark preceded by a backslash (\") is interpreted as a | ||
/// literal double quotation mark character ("). | ||
/// | ||
/// Backslashes are interpreted literally, unless they immediately precede a | ||
/// double quotation mark. | ||
/// | ||
/// If an even number of backslashes is followed by a double quotation mark, | ||
/// one backslash is placed in the argv array for every pair of backslashes, | ||
/// and the double quotation mark is interpreted as a string delimiter. | ||
/// | ||
/// If an odd number of backslashes is followed by a double quotation mark, | ||
/// one backslash is placed in the argv array for every pair of backslashes, | ||
/// and the double quotation mark is "escaped" by the remaining backslash, | ||
/// causing a literal double quotation mark (") to be placed in argv. | ||
/// </remarks> | ||
public static IEnumerable<string> SplitCommandLineIntoArguments(string commandLine, bool removeHashComments) | ||
{ | ||
char? unused; | ||
return SplitCommandLineIntoArguments(commandLine, removeHashComments, out unused); | ||
} | ||
|
||
public static IEnumerable<string> SplitCommandLineIntoArguments(string commandLine, bool removeHashComments, out char? illegalChar) | ||
public static bool SplitCommandLineIntoArguments(string args, out string[] arguments) | ||
{ | ||
var builder = new StringBuilder(commandLine.Length); | ||
var list = new List<string>(); | ||
var i = 0; | ||
bool hadError = false; | ||
var argArray = new List<string>(); | ||
var currentArg = new StringBuilder(); | ||
bool inQuotes = false; | ||
int index = 0; | ||
|
||
illegalChar = null; | ||
while (i < commandLine.Length) | ||
try | ||
{ | ||
while (i < commandLine.Length && char.IsWhiteSpace(commandLine[i])) | ||
while (true) | ||
{ | ||
i++; | ||
} | ||
|
||
if (i == commandLine.Length) | ||
{ | ||
break; | ||
} | ||
// skip whitespace | ||
while (char.IsWhiteSpace(args[index])) | ||
{ | ||
index += 1; | ||
} | ||
|
||
if (commandLine[i] == '#' && removeHashComments) | ||
{ | ||
break; | ||
} | ||
// # - comment to end of line | ||
if (args[index] == '#') | ||
{ | ||
index += 1; | ||
while (args[index] != '\n') | ||
{ | ||
index += 1; | ||
} | ||
continue; | ||
} | ||
|
||
var quoteCount = 0; | ||
builder.Length = 0; | ||
while (i < commandLine.Length && (!char.IsWhiteSpace(commandLine[i]) || (quoteCount % 2 != 0))) | ||
{ | ||
var current = commandLine[i]; | ||
switch (current) | ||
// do one argument | ||
do | ||
{ | ||
case '\\': | ||
if (args[index] == '\\') | ||
{ | ||
int cSlashes = 1; | ||
index += 1; | ||
while (index == args.Length && args[index] == '\\') | ||
{ | ||
var slashCount = 0; | ||
do | ||
{ | ||
builder.Append(commandLine[i]); | ||
i++; | ||
slashCount++; | ||
} while (i < commandLine.Length && commandLine[i] == '\\'); | ||
|
||
// Slashes not followed by a quote character can be ignored for now | ||
if (i >= commandLine.Length || commandLine[i] != '"') | ||
{ | ||
break; | ||
} | ||
|
||
// If there is an odd number of slashes then it is escaping the quote | ||
// otherwise it is just a quote. | ||
if (slashCount % 2 == 0) | ||
{ | ||
quoteCount++; | ||
} | ||
|
||
builder.Append('"'); | ||
i++; | ||
break; | ||
cSlashes += 1; | ||
} | ||
|
||
case '"': | ||
builder.Append(current); | ||
quoteCount++; | ||
i++; | ||
break; | ||
|
||
default: | ||
if ((current >= 0x1 && current <= 0x1f) || current == '|') | ||
if (index == args.Length || args[index] != '"') | ||
{ | ||
if (illegalChar == null) | ||
{ | ||
illegalChar = current; | ||
} | ||
currentArg.Append('\\', cSlashes); | ||
} | ||
else | ||
{ | ||
builder.Append(current); | ||
currentArg.Append('\\', (cSlashes >> 1)); | ||
if (0 != (cSlashes & 1)) | ||
{ | ||
currentArg.Append('"'); | ||
} | ||
else | ||
{ | ||
inQuotes = !inQuotes; | ||
} | ||
} | ||
|
||
i++; | ||
break; | ||
} | ||
} | ||
else if (args[index] == '"') | ||
{ | ||
inQuotes = !inQuotes; | ||
index += 1; | ||
} | ||
else | ||
{ | ||
currentArg.Append(args[index]); | ||
index += 1; | ||
} | ||
} while (!char.IsWhiteSpace(args[index]) || inQuotes); | ||
argArray.Add(currentArg.ToString()); | ||
currentArg.Clear(); | ||
} | ||
|
||
// If the quote string is surrounded by quotes with no interior quotes then | ||
// remove the quotes here. | ||
if (quoteCount == 2 && builder[0] == '"' && builder[builder.Length - 1] == '"') | ||
} | ||
catch (IndexOutOfRangeException) | ||
{ | ||
// got EOF | ||
if (inQuotes) | ||
{ | ||
builder.Remove(0, length: 1); | ||
builder.Remove(builder.Length - 1, length: 1); | ||
EqtTrace.Verbose("Executor.Execute: Exiting with exit code of {0}", 1); | ||
EqtTrace.Error(string.Format(CultureInfo.InvariantCulture, "Error: Unbalanced '\"' in command line argument file")); | ||
hadError = true; | ||
} | ||
|
||
if (builder.Length > 0) | ||
else if (currentArg.Length > 0) | ||
{ | ||
list.Add(builder.ToString()); | ||
// valid argument can be terminated by EOF | ||
argArray.Add(currentArg.ToString()); | ||
} | ||
} | ||
|
||
return list; | ||
arguments = argArray.ToArray(); | ||
return hadError; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.