mirror of
https://github.com/aphrody-labs/PowerShell.git
synced 2026-10-17 12:45:47 +00:00
Merge pull request #1435 from PowerShell/andschwa/commandlinetoargv
Remove dead code around `CommandLineToArgv`
This commit is contained in:
commit
b7fbad766a
3 files changed
+1
-221
No files matched your search
@@ -367,65 +367,6 @@ namespace System.Management.Automation
|
||||
// TODO:PSL clean this up
|
||||
return 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// This models the native call CommandLineToArgvW in managed code.
|
||||
/// </summary>
|
||||
internal static string[] CommandLineToArgv(string command)
|
||||
{
|
||||
StringBuilder arguments = new StringBuilder();
|
||||
int len = command.Length;
|
||||
IList<string> returnList = new List<string>();
|
||||
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
|
||||
|
||||
@@ -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
|
||||
|
||||
/// <summary>
|
||||
@@ -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
|
||||
@@ -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]);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user