-
Notifications
You must be signed in to change notification settings - Fork 355
Fixing the issues related to response file. #1196
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
codito
merged 5 commits into
microsoft:master
from
singhsarab:dev/sasin/responseFileFix
Oct 12, 2017
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c106939
Fixing the issues related to response file.
singhsarab 6ea6c04
Merge branch 'master' into dev/sasin/responseFileFix
harshjain2 c17e218
Change as per comment
singhsarab 30edc89
Merge remote-tracking branch 'upstream/master' into dev/sasin/respons…
singhsarab 7cce0b7
Merge branch 'dev/sasin/responseFileFix' of https://github.com/singhs…
singhsarab File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
197 changes: 74 additions & 123 deletions
197
src/Microsoft.TestPlatform.Utilities/CommandLineUtilities.cs
This file contains hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can use File.ReadAllText.