From c9c3e0b9be86d59e2bf93fd2c57f4c8cf10b6fc0 Mon Sep 17 00:00:00 2001 From: Andrew Schwartzmeyer Date: Tue, 19 Jul 2016 12:27:09 -0700 Subject: [PATCH 1/2] Remove managed CommandLineToArgv It is entirely unused. --- .../CoreCLR/CorePsPlatform.cs | 59 ---------- test/csharp/test_CorePsPlatform.cs | 107 ------------------ 2 files changed, 166 deletions(-) diff --git a/src/System.Management.Automation/CoreCLR/CorePsPlatform.cs b/src/System.Management.Automation/CoreCLR/CorePsPlatform.cs index 7ce0a4419..9171933bb 100644 --- a/src/System.Management.Automation/CoreCLR/CorePsPlatform.cs +++ b/src/System.Management.Automation/CoreCLR/CorePsPlatform.cs @@ -367,65 +367,6 @@ namespace System.Management.Automation // TODO:PSL clean this up return 0; } - - /// - /// This models the native call CommandLineToArgvW in managed code. - /// - internal static string[] CommandLineToArgv(string command) - { - StringBuilder arguments = new StringBuilder(); - int len = command.Length; - IList returnList = new List(); - bool inquote = false; - char current = '\0'; - - for (int argIndex = 0; argIndex < len; argIndex++) - { - current = command[argIndex]; - - if (current.Equals('"')) - { - // Treat anything in quotes as a single argument - // Because C# treats quotes differently than C++, instead of counting the slashes - // it makes more sense to count the quotes. - inquote = !inquote; - arguments.Append('"'); - continue; - } - - // If we're inside a quote, add the current character and cycle through - if (inquote && !current.Equals('"')) - { - arguments.Append(current.ToString()); - continue; - } - - if (current.Equals("\\")) - { - arguments.Append("\\"); - continue; - } - - if (string.IsNullOrWhiteSpace(current.ToString())) - { - if (arguments.Length > 0) - { - returnList.Add(arguments.ToString()); - arguments.Clear(); - } - continue; - } - - // All other exclusion scenarios being exhausted, append the current character. - arguments.Append(current.ToString()); - } - - // add the final object to the arguments list - returnList.Add(arguments.ToString()); - arguments.Clear(); - - return returnList.ToArray(); - } } internal static class Unix diff --git a/test/csharp/test_CorePsPlatform.cs b/test/csharp/test_CorePsPlatform.cs index 80d387891..2a6fa2d94 100644 --- a/test/csharp/test_CorePsPlatform.cs +++ b/test/csharp/test_CorePsPlatform.cs @@ -255,112 +255,5 @@ namespace PSTests File.Delete(path); File.Delete(link); } - - [Fact] - public static void TestCommandLineArgvReturnsAnElement() - { - string testCommand= "Today"; - - string[] retval = Platform.CommandLineToArgv(testCommand); - - Assert.Equal(1, retval.Length); - } - - [Fact] - public static void TestCommandLineArgvWWithSpaces() - { - string testCommand= "Today is a good day or is it"; - - string[] retval = Platform.CommandLineToArgv(testCommand); - - Assert.Equal("Today", retval[0]); - Assert.Equal("is", retval[1]); - Assert.Equal("a", retval[2]); - Assert.Equal("good", retval[3]); - Assert.Equal("day", retval[4]); - Assert.Equal("or", retval[5]); - Assert.Equal("is", retval[6]); - Assert.Equal("it", retval[7]); - } - - [Fact] - public static void TestCommandLineToArgvWTabsAreTreatedAsSpaces() - { - string testCommand = "Today \t is a good day"; - - string[] retval = Platform.CommandLineToArgv(testCommand); - - Assert.Equal("Today", retval[0]); - Assert.Equal("is", retval[1]); - Assert.Equal("a", retval[2]); - Assert.Equal("good", retval[3]); - Assert.Equal("day", retval[4]); - } - - [Fact] - public static void TestCommandLineToArgvWQuotesAreArgs() - { - string testCommand = "Today is \"a good\" day"; - - string[] retval = Platform.CommandLineToArgv(testCommand); - - Assert.Equal(4, retval.Length); - Assert.Equal("Today", retval[0]); - Assert.Equal("is", retval[1]); - Assert.Equal("\"a good\"", retval[2]); - Assert.Equal("day", retval[3]); - } - - [Fact] - public static void TestCommandLineToArgvWEvenNumberBackSlashes() - { - string test1 = "a\\b c d"; - string test2 = "a\\b\\c d"; - string test3 = "a \\b\\c\\d"; - string test4 = "a\\\\\\b"; - string[] retval; - - retval = Platform.CommandLineToArgv(test1); - - Assert.Equal(3, retval.Length); - Assert.Equal("a\\b", retval[0]); - Assert.Equal("c", retval[1]); - Assert.Equal("d", retval[2]); - - retval = Platform.CommandLineToArgv(test2); - - Assert.Equal(2, retval.Length); - Assert.Equal("a\\b\\c", retval[0]); - Assert.Equal("d", retval[1]); - - retval = Platform.CommandLineToArgv(test3); - - Assert.Equal(2, retval.Length); - Assert.Equal("a", retval[0]); - Assert.Equal("\\b\\c\\d", retval[1]); - - retval = Platform.CommandLineToArgv(test4); - - Assert.Equal(1, retval.Length); - Assert.Equal("a\\\\\\b", retval[0]); - } - - [Fact] - public static void TestCommandLineToArgvwOddNumberWithBackSlashes() - { - string test1 = "a\\\"b"; - string test2 = "a\\\\\\\"b"; - string[] retval; - - retval = Platform.CommandLineToArgv(test1); - - Assert.Equal(1, retval.Length); - Assert.Equal("a\\\"b", retval[0]); - - retval = Platform.CommandLineToArgv(test2); - - Assert.Equal(1, retval.Length); - Assert.Equal("a\\\\\\\"b", retval[0]); - } } } From 8eda0df29ec046c48d2dae39cc47fceaff7a83b7 Mon Sep 17 00:00:00 2001 From: Andrew Schwartzmeyer Date: Tue, 19 Jul 2016 12:27:34 -0700 Subject: [PATCH 2/2] Remove CommandLineParameterBinderNativeMethods This was dead code. PreParseCommandLine was called only when tracing was enabled, which meant that the tracing itself was useless, as it was not tracing what the actual code was doing. I believe the intention of this tracing was to "guess" how the native commands on Windows *might* parse their command-line arguments, but the traced result was not guaranteed to be accurate. Since we cannot provide accurate tracing, the tracer was removed in its entirety. The PreParseCommandLine function was the only place where CommandLineToArgv was called, so with it removed, the entire native methods class here can be removed. --- .../engine/NativeCommandParameterBinder.cs | 56 +------------------ 1 file changed, 1 insertion(+), 55 deletions(-) diff --git a/src/System.Management.Automation/engine/NativeCommandParameterBinder.cs b/src/System.Management.Automation/engine/NativeCommandParameterBinder.cs index 50a4675cc..102ba2ede 100644 --- a/src/System.Management.Automation/engine/NativeCommandParameterBinder.cs +++ b/src/System.Management.Automation/engine/NativeCommandParameterBinder.cs @@ -16,11 +16,6 @@ namespace System.Management.Automation /// internal class NativeCommandParameterBinder : ParameterBinderBase { - #region tracer - [TraceSource("NativeCommandParameterBinder","The parameter binder for native commands")] - static private PSTraceSource tracer = PSTraceSource.GetTracer ("NativeCommandParameterBinder", "The parameter binder for native commands"); - #endregion tracer - #region ctor /// @@ -129,21 +124,7 @@ namespace System.Management.Automation { get { - var rawArgs = arguments.ToString(); - - if (tracer.IsEnabled && Platform.IsWindows) - { - // This tracing really shouldn't be in the property getter... - tracer.WriteLine("Raw argument string: {0}", rawArgs); - string[] parsedArguments = CommandLineParameterBinderNativeMethods.PreParseCommandLine(rawArgs); - - for (int counter = 0; counter < parsedArguments.Length; counter++) - { - tracer.WriteLine("Argument {0}: {1}", counter, parsedArguments[counter]); - } - } - - return rawArgs; + return arguments.ToString(); } } // Arguments private readonly StringBuilder arguments = new StringBuilder(); @@ -250,39 +231,4 @@ namespace System.Management.Automation #endregion private members } - internal static class CommandLineParameterBinderNativeMethods - { - public static string[] PreParseCommandLine(string commandLine) - { - int numberOfArguments = 0; - IntPtr parsedCommandLine = CommandLineToArgvW(commandLine, out numberOfArguments); - - if(parsedCommandLine == IntPtr.Zero) - return null; - - try - { - string[] results = new string[numberOfArguments - 1]; - for (int counter = 1; counter < numberOfArguments; counter++) - { - results[counter - 1] = Marshal.PtrToStringUni( - Marshal.ReadIntPtr(parsedCommandLine, counter * IntPtr.Size)); - } - - return results; - } - finally - { - LocalFree(parsedCommandLine); - } - } - - [DllImport(PinvokeDllNames.CommandLineToArgvDllName, SetLastError = true)] - static extern IntPtr CommandLineToArgvW( - [MarshalAs(UnmanagedType.LPWStr)] string lpCmdLine, - out int pNumArgs); - - [DllImport(PinvokeDllNames.LocalFreeDllName)] - static extern IntPtr LocalFree(IntPtr hMem); - } } // namespace System.Management.Automation