mirror of
https://github.com/aphrody-labs/PowerShell.git
synced 2026-10-17 04:36:39 +00:00
Import System.Console In/Out/Error stream changes
Thanks Jason.
This commit is contained in:
1 parent
eee564e465
commit
533a5e3225
4 files changed
+116
-359
No files matched your search
@@ -118,13 +118,13 @@ namespace Microsoft.PowerShell
|
||||
}
|
||||
}
|
||||
|
||||
internal bool ReadFromStdin
|
||||
internal bool ExplicitReadCommandsFromStdin
|
||||
{
|
||||
get
|
||||
{
|
||||
Dbg.Assert(dirty, "Parse has not been called yet");
|
||||
|
||||
return readFromStdin;
|
||||
return explicitReadCommandsFromStdin;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -488,7 +488,7 @@ namespace Microsoft.PowerShell
|
||||
{
|
||||
// the arg to -file is -, which is secret code for "read the commands from stdin with prompts"
|
||||
|
||||
readFromStdin = true;
|
||||
explicitReadCommandsFromStdin = true;
|
||||
noPrompt = false;
|
||||
}
|
||||
else
|
||||
@@ -850,7 +850,7 @@ namespace Microsoft.PowerShell
|
||||
{
|
||||
// the arg to -command is -, which is secret code for "read the commands from stdin with no prompts"
|
||||
|
||||
readFromStdin = true;
|
||||
explicitReadCommandsFromStdin = true;
|
||||
noPrompt = true;
|
||||
|
||||
++i;
|
||||
@@ -865,7 +865,7 @@ namespace Microsoft.PowerShell
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!parent.IsStandardInputRedirected)
|
||||
if (!Console.IsInputRedirected)
|
||||
{
|
||||
ui.WriteErrorLine(CommandLineParameterParserStrings.StdinNotRedirected);
|
||||
showHelp = true;
|
||||
@@ -974,7 +974,7 @@ namespace Microsoft.PowerShell
|
||||
// default is sta.
|
||||
private bool? staMode = null;
|
||||
private bool noExit = true;
|
||||
private bool readFromStdin;
|
||||
private bool explicitReadCommandsFromStdin;
|
||||
private bool noPrompt;
|
||||
private string commandLineCommand;
|
||||
private bool wasCommandEncoded;
|
||||
|
||||
@@ -199,10 +199,10 @@ namespace Microsoft.PowerShell
|
||||
theConsoleHost = ConsoleHost.CreateSingletonInstance(configuration);
|
||||
#if !OPEN
|
||||
theConsoleHost.BindBreakHandler();
|
||||
|
||||
PSHost.IsStdOutputRedirected = theConsoleHost.IsStandardOutputRedirected;
|
||||
#endif
|
||||
|
||||
PSHost.IsStdOutputRedirected = Console.IsOutputRedirected;
|
||||
|
||||
if (args == null)
|
||||
{
|
||||
args = new string[0];
|
||||
@@ -1210,7 +1210,7 @@ namespace Microsoft.PowerShell
|
||||
|
||||
//If this shell is invoked in minishell interop mode and error is redirected,
|
||||
//always write data in error stream in xml format.
|
||||
if (IsInteractive == false && IsStandardErrorRedirected && wasInitialCommandEncoded)
|
||||
if (IsInteractive == false && Console.IsErrorRedirected && wasInitialCommandEncoded)
|
||||
{
|
||||
format = Serialization.DataFormat.XML;
|
||||
}
|
||||
@@ -1222,19 +1222,7 @@ namespace Microsoft.PowerShell
|
||||
{
|
||||
get
|
||||
{
|
||||
if (IsInteractive)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (
|
||||
(OutputFormat != Serialization.DataFormat.Text)
|
||||
|| (IsStandardInputRedirected && !ui.ReadFromStdin))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
return !IsInteractive && ((OutputFormat != Serialization.DataFormat.Text) || Console.IsInputRedirected);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1253,7 +1241,7 @@ namespace Microsoft.PowerShell
|
||||
new WrappedSerializer(
|
||||
outputFormat,
|
||||
"Output",
|
||||
IsStandardOutputRedirected ? StandardOutputWriter : ConsoleTextWriter);
|
||||
Console.IsOutputRedirected ? Console.Out : ConsoleTextWriter);
|
||||
}
|
||||
return outputSerializer;
|
||||
}
|
||||
@@ -1269,7 +1257,7 @@ namespace Microsoft.PowerShell
|
||||
new WrappedSerializer(
|
||||
ErrorFormat,
|
||||
"Error",
|
||||
IsStandardErrorRedirected ? StandardErrorWriter : ConsoleTextWriter);
|
||||
Console.IsErrorRedirected ? Console.Error : ConsoleTextWriter);
|
||||
}
|
||||
return errorSerializer;
|
||||
}
|
||||
@@ -1345,7 +1333,7 @@ namespace Microsoft.PowerShell
|
||||
inputFormat = cpp.InputFormat;
|
||||
wasInitialCommandEncoded = cpp.WasInitialCommandEncoded;
|
||||
|
||||
ui.ReadFromStdin = cpp.ReadFromStdin;
|
||||
ui.ReadFromStdin = cpp.ExplicitReadCommandsFromStdin || Console.IsInputRedirected;
|
||||
ui.NoPrompt = cpp.NoPrompt;
|
||||
ui.ThrowOnReadAndPrompt = cpp.ThrowOnReadAndPrompt;
|
||||
noExit = cpp.NoExit;
|
||||
@@ -1503,8 +1491,15 @@ namespace Microsoft.PowerShell
|
||||
|
||||
private bool LoadPSReadline()
|
||||
{
|
||||
return ((cpp.InitialCommand == null && cpp.File == null) || cpp.NoExit) &&
|
||||
(!IsStandardInputRedirected && !cpp.NonInteractive && !cpp.ReadFromStdin);
|
||||
// Don't load PSReadline if:
|
||||
// * we don't think the process will be interactive, e.g. -command or -file
|
||||
// - exception: when -noexit is specified, we will be interactive after the command/file finishes
|
||||
// * -noniteractive: this should be obvious, they've asked that we don't every prompt
|
||||
//
|
||||
// Note that PSReadline doesn't support redirected stdin/stdout, but we don't check that here because
|
||||
// a future version might, and we should automatically load it at that unknown point in the future.
|
||||
// PSReadline will ideally fall back to Console.ReadLine or whatever when stdin/stdout is redirected.
|
||||
return ((cpp.InitialCommand == null && cpp.File == null) || cpp.NoExit) && !cpp.NonInteractive;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -1804,6 +1799,9 @@ namespace Microsoft.PowerShell
|
||||
if (AstSearcher.IsUsingDollarInput(parsedInput))
|
||||
{
|
||||
executionOptions |= Executor.ExecutionOptions.ReadInputObjects;
|
||||
|
||||
// We will consume all of the input to pass to the script, so don't try to read commands from stdin.
|
||||
ui.ReadFromStdin = false;
|
||||
}
|
||||
|
||||
exec.ExecuteCommandAsyncHelper(tempPipeline, out e1, executionOptions);
|
||||
@@ -1863,6 +1861,9 @@ namespace Microsoft.PowerShell
|
||||
if (AstSearcher.IsUsingDollarInput(parsedInput))
|
||||
{
|
||||
executionOptions |= Executor.ExecutionOptions.ReadInputObjects;
|
||||
|
||||
// We will consume all of the input to pass to the script, so don't try to read commands from stdin.
|
||||
ui.ReadFromStdin = false;
|
||||
}
|
||||
|
||||
exec.ExecuteCommandAsyncHelper(tempPipeline, out e1, executionOptions);
|
||||
@@ -2066,223 +2067,6 @@ namespace Microsoft.PowerShell
|
||||
|
||||
#endregion non-overrides
|
||||
|
||||
#region stdio redirection
|
||||
|
||||
|
||||
|
||||
#if !OPEN
|
||||
private delegate void InitializeStandardHandleDelegate(NakedWin32Handle handle);
|
||||
#endif
|
||||
|
||||
|
||||
#if !OPEN
|
||||
[ArchitectureSensitive]
|
||||
private bool IsStandardHandleRedirected(
|
||||
ConsoleControl.StandardHandleId handleId,
|
||||
ref bool isHandleRedirectionDetermined,
|
||||
ref bool isHandleRedirected,
|
||||
InitializeStandardHandleDelegate handleInit)
|
||||
{
|
||||
// lock because we update a bunch of instance vars here...
|
||||
|
||||
lock (hostGlobalLock)
|
||||
{
|
||||
if (!isHandleRedirectionDetermined)
|
||||
{
|
||||
isHandleRedirected = false;
|
||||
|
||||
NakedWin32Handle stdHandle = ConsoleControl.GetStdHandle(handleId);
|
||||
SafeFileHandle sfh = new SafeFileHandle(stdHandle, false);
|
||||
if (!sfh.IsInvalid)
|
||||
{
|
||||
var fileType = ConsoleControl.NativeMethods.GetFileType(stdHandle);
|
||||
if (fileType != ConsoleControl.NativeMethods.FileType.Char)
|
||||
{
|
||||
// stdHandle is not a console handle; so it has been redirected.
|
||||
|
||||
isHandleRedirected = true;
|
||||
handleInit(stdHandle);
|
||||
}
|
||||
}
|
||||
|
||||
isHandleRedirectionDetermined = true;
|
||||
}
|
||||
}
|
||||
|
||||
return isHandleRedirected;
|
||||
}
|
||||
#endif
|
||||
|
||||
internal bool IsStandardOutputRedirected
|
||||
{
|
||||
get
|
||||
{
|
||||
#if OPEN
|
||||
return Console.IsOutputRedirected;
|
||||
#else
|
||||
if (initStandardOutDelegate == null)
|
||||
{
|
||||
initStandardOutDelegate = new InitializeStandardHandleDelegate(InitializeStandardOutputWriter);
|
||||
}
|
||||
|
||||
return
|
||||
IsStandardHandleRedirected(
|
||||
ConsoleControl.StandardHandleId.Output,
|
||||
ref isStandardOutputRedirectionDetermined,
|
||||
ref isStandardOutputRedirected,
|
||||
initStandardOutDelegate);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
internal bool IsStandardInputRedirected
|
||||
{
|
||||
get
|
||||
{
|
||||
#if OPEN
|
||||
return Console.IsInputRedirected;
|
||||
#else
|
||||
if (initStandardInDelegate == null)
|
||||
{
|
||||
initStandardInDelegate = new InitializeStandardHandleDelegate(InitializeStandardInputReader);
|
||||
}
|
||||
|
||||
return
|
||||
IsStandardHandleRedirected(
|
||||
ConsoleControl.StandardHandleId.Input,
|
||||
ref isStandardInputRedirectionDetermined,
|
||||
ref isStandardInputRedirected,
|
||||
initStandardInDelegate);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
internal bool IsStandardErrorRedirected
|
||||
{
|
||||
get
|
||||
{
|
||||
#if OPEN
|
||||
return Console.IsErrorRedirected;
|
||||
#else
|
||||
if (initStandardErrorDelegate == null)
|
||||
{
|
||||
initStandardErrorDelegate = new InitializeStandardHandleDelegate(InitializeStandardErrorWriter);
|
||||
}
|
||||
|
||||
return
|
||||
IsStandardHandleRedirected(
|
||||
ConsoleControl.StandardHandleId.Error,
|
||||
ref isStandardErrorRedirectionDetermined,
|
||||
ref isStandardErrorRedirected,
|
||||
initStandardErrorDelegate);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
#if !OPEN
|
||||
[ArchitectureSensitive]
|
||||
private void InitializeStandardInputReader(NakedWin32Handle stdHandle)
|
||||
{
|
||||
Dbg.Assert(standardInputReader == null, "standardInputReader should not exist at this point");
|
||||
|
||||
// stdin has been redirected. Use ReadFile instead of ReadConsole.
|
||||
|
||||
uint codePage = (uint) ConsoleControl.NativeMethods.GetConsoleCP();
|
||||
Encoding encoding = Encoding.GetEncoding((int) codePage);
|
||||
|
||||
try
|
||||
{
|
||||
Stream s =
|
||||
new FileStream(
|
||||
|
||||
// since stdHandle was retreived not with an open, flag its safe wrapper as not needing to be closed.
|
||||
|
||||
new SafeFileHandle(stdHandle, false),
|
||||
FileAccess.Read);
|
||||
|
||||
// The "false" flag here means "don't detect the byte order mark in the input stream." I hope that's the right
|
||||
// thing.
|
||||
|
||||
#if CORECLR // There is no TextReader.Synchronized on CoreCLR
|
||||
standardInputReader = new StreamReader(s, encoding, false);
|
||||
#else
|
||||
standardInputReader = TextReader.Synchronized(new StreamReader(s, encoding, false));
|
||||
#endif
|
||||
}
|
||||
catch (System.IO.IOException)
|
||||
{
|
||||
#if CORECLR // There is no TextReader.Synchronized on CoreCLR
|
||||
standardInputReader = new StringReader("");
|
||||
#else
|
||||
standardInputReader = TextReader.Synchronized(new StringReader(""));
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
[ArchitectureSensitive]
|
||||
private void InitializeStandardOutputWriter(NakedWin32Handle stdHandle)
|
||||
{
|
||||
Dbg.Assert(standardOutputWriter == null, "standardOutputWriter should not exist at this point");
|
||||
standardOutputWriter = Console.Out;
|
||||
}
|
||||
|
||||
|
||||
|
||||
[ArchitectureSensitive]
|
||||
private void InitializeStandardErrorWriter(NakedWin32Handle stdHandle)
|
||||
{
|
||||
Dbg.Assert(standardErrorWriter == null, "standardErrorWriter should not exist at this point");
|
||||
standardErrorWriter = Console.Error;
|
||||
}
|
||||
#endif
|
||||
|
||||
internal TextWriter StandardOutputWriter
|
||||
{
|
||||
get
|
||||
{
|
||||
if (IsStandardOutputRedirected)
|
||||
{
|
||||
Dbg.Assert(standardOutputWriter != null, "standardOutputWriter should be initialized");
|
||||
|
||||
return standardOutputWriter;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
internal TextWriter StandardErrorWriter
|
||||
{
|
||||
get
|
||||
{
|
||||
if (IsStandardErrorRedirected)
|
||||
{
|
||||
Dbg.Assert(standardErrorWriter != null, "standardErrorWriter should be initialized");
|
||||
|
||||
return standardErrorWriter;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
internal TextReader StandardInReader
|
||||
{
|
||||
get
|
||||
{
|
||||
if (IsStandardInputRedirected)
|
||||
{
|
||||
Dbg.Assert(standardInputReader != null, "standardInputReader should be initialized");
|
||||
|
||||
return standardInputReader;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion stdio redirection
|
||||
|
||||
#region debugger
|
||||
|
||||
/// <summary>
|
||||
@@ -2579,12 +2363,6 @@ namespace Microsoft.PowerShell
|
||||
bool inBlockMode = false;
|
||||
bool previousResponseWasEmpty = false;
|
||||
StringBuilder inputBlock = new StringBuilder();
|
||||
bool displayPrompt = false;
|
||||
// If the prompt has not been displayed in the native code or if the input loop is nested, we need to display the prompt here
|
||||
if (!parent.promptDisplayedInNativeCode || inputLoopIsNested)
|
||||
{
|
||||
displayPrompt = true;
|
||||
}
|
||||
|
||||
while (!parent.ShouldEndSession && !shouldExit)
|
||||
{
|
||||
@@ -2594,8 +2372,6 @@ namespace Microsoft.PowerShell
|
||||
|
||||
string prompt = null;
|
||||
string line = null;
|
||||
if (displayPrompt)
|
||||
{
|
||||
if (!ui.NoPrompt)
|
||||
{
|
||||
if (inBlockMode)
|
||||
@@ -2631,16 +2407,7 @@ namespace Microsoft.PowerShell
|
||||
}
|
||||
previousResponseWasEmpty = false;
|
||||
// There could be a profile. So there could be a user defined custom readline command
|
||||
line = ui.ReadLineWithTabCompletion(exec, useUserDefinedCustomReadLine: true);
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
previousResponseWasEmpty = false;
|
||||
// There are no profiles. So there is no user defined custom readline command
|
||||
line = ui.ReadLineWithTabCompletion(exec, useUserDefinedCustomReadLine: false);
|
||||
displayPrompt = true;
|
||||
}
|
||||
line = ui.ReadLineWithTabCompletion(exec);
|
||||
|
||||
// line will be null in the case that Ctrl-C terminated the input
|
||||
|
||||
@@ -2658,7 +2425,7 @@ namespace Microsoft.PowerShell
|
||||
}
|
||||
inBlockMode = false;
|
||||
|
||||
if (parent.IsStandardInputRedirected)
|
||||
if (Console.IsInputRedirected)
|
||||
{
|
||||
// null is also the result of reading stdin to EOF.
|
||||
|
||||
@@ -2746,7 +2513,7 @@ namespace Microsoft.PowerShell
|
||||
}
|
||||
else
|
||||
{
|
||||
if (parent.IsRunningAsync)
|
||||
if (parent.IsRunningAsync && !parent.IsNested)
|
||||
{
|
||||
exec.ExecuteCommandAsync(line, out e, Executor.ExecutionOptions.AddOutputter | Executor.ExecutionOptions.AddToHistory);
|
||||
}
|
||||
@@ -3113,22 +2880,6 @@ namespace Microsoft.PowerShell
|
||||
private bool shouldEndSession;
|
||||
private int beginApplicationNotifyCount;
|
||||
|
||||
#if !OPEN
|
||||
private bool isStandardOutputRedirectionDetermined;
|
||||
private bool isStandardOutputRedirected;
|
||||
private bool isStandardErrorRedirectionDetermined;
|
||||
private bool isStandardErrorRedirected;
|
||||
private bool isStandardInputRedirectionDetermined;
|
||||
private bool isStandardInputRedirected;
|
||||
#endif
|
||||
private TextWriter standardOutputWriter = null;
|
||||
private TextWriter standardErrorWriter = null;
|
||||
private TextReader standardInputReader = null;
|
||||
#if !OPEN
|
||||
private InitializeStandardHandleDelegate initStandardOutDelegate;
|
||||
private InitializeStandardHandleDelegate initStandardInDelegate;
|
||||
private InitializeStandardHandleDelegate initStandardErrorDelegate;
|
||||
#endif
|
||||
private ConsoleTextWriter consoleWriter;
|
||||
private WrappedSerializer outputSerializer;
|
||||
private WrappedSerializer errorSerializer;
|
||||
|
||||
@@ -77,7 +77,6 @@ namespace Microsoft.PowerShell
|
||||
// systems ignore the setting.
|
||||
m = ConsoleControl.GetMode(handle);
|
||||
this._supportsVirtualTerminal = (m & ConsoleControl.ConsoleModes.VirtualTerminal) != 0;
|
||||
isTestingShiftTab = false;
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -145,17 +144,7 @@ namespace Microsoft.PowerShell
|
||||
///
|
||||
/// </summary>
|
||||
|
||||
internal bool ReadFromStdin
|
||||
{
|
||||
get
|
||||
{
|
||||
return readFromStdin;
|
||||
}
|
||||
set
|
||||
{
|
||||
readFromStdin = value;
|
||||
}
|
||||
}
|
||||
internal bool ReadFromStdin { get; set; }
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
@@ -563,7 +552,7 @@ namespace Microsoft.PowerShell
|
||||
internal void WriteToConsole(string value, bool transcribeResult)
|
||||
{
|
||||
#if OPEN
|
||||
Console.Write(value);
|
||||
Console.Out.Write(value);
|
||||
#else
|
||||
ConsoleHandle handle = ConsoleControl.GetActiveScreenBufferHandle();
|
||||
|
||||
@@ -589,10 +578,9 @@ namespace Microsoft.PowerShell
|
||||
ConsoleControl.WriteConsole(handle, value);
|
||||
#endif
|
||||
|
||||
if (isInteractiveTestToolListening && parent.IsStandardOutputRedirected)
|
||||
if (isInteractiveTestToolListening && Console.IsOutputRedirected)
|
||||
{
|
||||
Dbg.Assert(parent.StandardOutputWriter != null, "stdout writer should be initialized");
|
||||
parent.StandardOutputWriter.Write(value);
|
||||
Console.Out.Write(value);
|
||||
}
|
||||
|
||||
if (transcribeResult)
|
||||
@@ -673,10 +661,7 @@ namespace Microsoft.PowerShell
|
||||
// If the test hook is set, write to it and continue.
|
||||
if (_h != null) _h.Write(value);
|
||||
|
||||
TextWriter writer =
|
||||
(!parent.IsStandardOutputRedirected || parent.IsInteractive)
|
||||
? parent.ConsoleTextWriter
|
||||
: parent.StandardOutputWriter;
|
||||
TextWriter writer = Console.IsOutputRedirected ? Console.Out : parent.ConsoleTextWriter;
|
||||
|
||||
if (parent.IsRunningAsync)
|
||||
{
|
||||
@@ -1376,9 +1361,9 @@ namespace Microsoft.PowerShell
|
||||
}
|
||||
|
||||
TextWriter writer =
|
||||
(!parent.IsStandardErrorRedirected || parent.IsInteractive)
|
||||
(!Console.IsErrorRedirected || parent.IsInteractive)
|
||||
? parent.ConsoleTextWriter
|
||||
: parent.StandardErrorWriter;
|
||||
: Console.Error;
|
||||
|
||||
if (parent.ErrorFormat == Serialization.DataFormat.XML)
|
||||
{
|
||||
@@ -1392,7 +1377,7 @@ namespace Microsoft.PowerShell
|
||||
WriteLine(errorForegroundColor, errorBackgroundColor, value);
|
||||
else
|
||||
|
||||
parent.StandardErrorWriter.Write(value + Crlf);
|
||||
Console.Error.Write(value + Crlf);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1564,31 +1549,81 @@ namespace Microsoft.PowerShell
|
||||
|
||||
internal string ReadLine(bool endOnTab, string initialContent, out ReadLineResult result, bool calledFromPipeline, bool transcribeResult)
|
||||
{
|
||||
#if OPEN
|
||||
result = ReadLineResult.endedOnEnter;
|
||||
return Console.ReadLine();
|
||||
#else
|
||||
result = ReadLineResult.endedOnEnter;
|
||||
|
||||
// If the test hook is set, read from it.
|
||||
if (_h != null) return _h.ReadLine();
|
||||
|
||||
string s = "";
|
||||
if (parent.IsStandardInputRedirected && readFromStdin)
|
||||
{
|
||||
// When reading from a file handle instead of a console, endOnTab and initial content are ignored.
|
||||
string restOfLine = null;
|
||||
|
||||
// StreamReader.ReadLine simply returns null when EOF is reached.
|
||||
string s = ReadFromStdin
|
||||
? ReadLineFromFile(initialContent)
|
||||
: ReadLineFromConsole(endOnTab, initialContent, calledFromPipeline, ref restOfLine, ref result);
|
||||
|
||||
s = parent.StandardInReader.ReadLine();
|
||||
if (endOnTab && !string.IsNullOrEmpty(s) && s.IndexOf(Tab, StringComparison.OrdinalIgnoreCase) != -1)
|
||||
if (transcribeResult)
|
||||
{
|
||||
result = isTestingShiftTab ? ReadLineResult.endedOnShiftTab : ReadLineResult.endedOnTab;
|
||||
PostRead(s);
|
||||
}
|
||||
else
|
||||
{
|
||||
PostRead();
|
||||
}
|
||||
|
||||
if (restOfLine != null)
|
||||
s += restOfLine;
|
||||
|
||||
return s;
|
||||
}
|
||||
return s;
|
||||
|
||||
private string ReadLineFromFile(string initialContent)
|
||||
{
|
||||
var sb = new StringBuilder();
|
||||
if (initialContent != null)
|
||||
{
|
||||
sb.Append(initialContent);
|
||||
}
|
||||
|
||||
while (true)
|
||||
{
|
||||
var inC = Console.In.Read();
|
||||
if (inC == -1)
|
||||
{
|
||||
// EOF - we return null which tells our caller to exit
|
||||
return null;
|
||||
}
|
||||
|
||||
var c = unchecked((char)inC);
|
||||
if (!NoPrompt) Console.Out.Write(c);
|
||||
|
||||
if (c == '\r')
|
||||
{
|
||||
// Treat as newline, but consume \n if there is one.
|
||||
if (Console.In.Peek() == '\n')
|
||||
{
|
||||
if (!NoPrompt) Console.Out.Write('\n');
|
||||
Console.In.Read();
|
||||
}
|
||||
sb.Append('\n');
|
||||
break;
|
||||
}
|
||||
|
||||
sb.Append(c);
|
||||
|
||||
if (c == '\n')
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
private string ReadLineFromConsole(bool endOnTab, string initialContent, bool calledFromPipeline, ref string restOfLine, ref ReadLineResult result)
|
||||
{
|
||||
#if OPEN
|
||||
return ReadLineFromFile(initialContent);
|
||||
#else
|
||||
|
||||
ConsoleHandle handle = ConsoleControl.GetInputHandle();
|
||||
PreRead();
|
||||
// Ensure that we're in the proper line-input mode.
|
||||
@@ -1606,6 +1641,7 @@ namespace Microsoft.PowerShell
|
||||
m |= desiredMode;
|
||||
ConsoleControl.SetMode(handle, m);
|
||||
}
|
||||
|
||||
// If more characters are typed than you asked, then the next call to ReadConsole will return the
|
||||
// additional characters beyond those you requested.
|
||||
//
|
||||
@@ -1622,10 +1658,10 @@ namespace Microsoft.PowerShell
|
||||
// the empty string.
|
||||
|
||||
uint keyState = 0;
|
||||
string restOfLine = null;
|
||||
|
||||
rawui.ClearKeyCache();
|
||||
|
||||
string s = "";
|
||||
do
|
||||
{
|
||||
s += ConsoleControl.ReadConsole(handle, initialContent, maxInputLineLength, endOnTab, out keyState);
|
||||
@@ -1710,18 +1746,6 @@ namespace Microsoft.PowerShell
|
||||
|| (s != null && result != ReadLineResult.endedOnBreak),
|
||||
"s should only be null if input ended with a break");
|
||||
|
||||
if (transcribeResult)
|
||||
{
|
||||
PostRead(s);
|
||||
}
|
||||
else
|
||||
{
|
||||
PostRead();
|
||||
}
|
||||
|
||||
if (restOfLine != null)
|
||||
s += restOfLine;
|
||||
|
||||
return s;
|
||||
#endif
|
||||
}
|
||||
@@ -1785,21 +1809,13 @@ namespace Microsoft.PowerShell
|
||||
///
|
||||
/// The Executor instance on which to run any pipelines that are needed to find matches
|
||||
///
|
||||
/// </param>
|
||||
///
|
||||
/// <param name="useUserDefinedCustomReadLine">
|
||||
///
|
||||
/// Flag which decides if we should look for a user defined ReadLine function
|
||||
///
|
||||
/// </param>
|
||||
///
|
||||
/// <returns>
|
||||
///
|
||||
/// null on a break event
|
||||
/// the completed line otherwise
|
||||
///
|
||||
/// </returns>
|
||||
internal string ReadLineWithTabCompletion(Executor exec, bool useUserDefinedCustomReadLine)
|
||||
internal string ReadLineWithTabCompletion(Executor exec)
|
||||
{
|
||||
string input = null;
|
||||
string lastInput = "";
|
||||
@@ -1818,7 +1834,7 @@ namespace Microsoft.PowerShell
|
||||
|
||||
do
|
||||
{
|
||||
if (TryInvokeUserDefinedReadLine(out input, useUserDefinedCustomReadLine))
|
||||
if (!ReadFromStdin && TryInvokeUserDefinedReadLine(out input))
|
||||
{
|
||||
break;
|
||||
}
|
||||
@@ -1847,16 +1863,11 @@ namespace Microsoft.PowerShell
|
||||
int leftover = input.Length - tabIndex - 1;
|
||||
if (leftover > 0)
|
||||
{
|
||||
// Check if the std input is redirected, e.g. reading from a file
|
||||
bool isStdInputRedirected = parent.IsStandardInputRedirected && readFromStdin;
|
||||
if (!isStdInputRedirected)
|
||||
{
|
||||
// We are reading from the console
|
||||
// We are reading from the console (not redirected, b/c we don't end on tab when redirected)
|
||||
// If the cursor is at the end of a line, there is actually a space character at the cursor's position and when we type tab
|
||||
// at the end of a line, that space character is replaced by the tab. But when we type tab at the middle of a line, the space
|
||||
// character at the end is preserved, we should remove that space character because it's not provided by the user.
|
||||
input = input.Remove(input.Length - 1);
|
||||
}
|
||||
restOfLine = input.Substring(tabIndex + 1);
|
||||
}
|
||||
input = input.Remove(tabIndex);
|
||||
@@ -2038,23 +2049,23 @@ namespace Microsoft.PowerShell
|
||||
}
|
||||
|
||||
const string CustomReadlineCommand = "PSConsoleHostReadLine";
|
||||
private bool TryInvokeUserDefinedReadLine(out string input, bool useUserDefinedCustomReadLine)
|
||||
private bool TryInvokeUserDefinedReadLine(out string input)
|
||||
{
|
||||
// We're using GetCommands instead of GetCommand so we don't auto-load a module should the command exist, but isn't loaded.
|
||||
// The idea is that if someone hasn't defined the command (say because they started -noprofile), we shouldn't auto-load
|
||||
// this function.
|
||||
|
||||
// If we need to look for user defined custom readline command, then we need to wait for Runspace to be created.
|
||||
if (useUserDefinedCustomReadLine)
|
||||
{
|
||||
var runspace = this.parent.LocalRunspace;
|
||||
if (runspace != null
|
||||
&& runspace.Engine.Context.EngineIntrinsics.InvokeCommand.GetCommands(CustomReadlineCommand, CommandTypes.Function | CommandTypes.Cmdlet, nameIsPattern: false).Any())
|
||||
if (runspace != null &&
|
||||
runspace.Engine.Context.EngineIntrinsics.InvokeCommand.
|
||||
GetCommands(CustomReadlineCommand,
|
||||
CommandTypes.Function | CommandTypes.Cmdlet, nameIsPattern: false).Any())
|
||||
{
|
||||
try
|
||||
{
|
||||
PowerShell ps;
|
||||
if ((runspace.ExecutionContext.EngineHostInterface.NestedPromptCount > 0) && (Runspace.DefaultRunspace != null))
|
||||
if ((runspace.ExecutionContext.EngineHostInterface.NestedPromptCount > 0) &&
|
||||
(Runspace.DefaultRunspace != null))
|
||||
{
|
||||
ps = PowerShell.Create(RunspaceMode.CurrentRunspace);
|
||||
}
|
||||
@@ -2076,7 +2087,6 @@ namespace Microsoft.PowerShell
|
||||
CommandProcessorBase.CheckForSevereException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
input = null;
|
||||
return false;
|
||||
@@ -2088,7 +2098,6 @@ namespace Microsoft.PowerShell
|
||||
|
||||
private object instanceLock = new object();
|
||||
|
||||
private bool readFromStdin;
|
||||
private bool noPrompt;
|
||||
|
||||
//If this is true, class throws on read or prompt method which require
|
||||
@@ -2113,9 +2122,6 @@ namespace Microsoft.PowerShell
|
||||
// this is a test hook for the ConsoleInteractiveTestTool, which sets this field to true.
|
||||
|
||||
private bool isInteractiveTestToolListening;
|
||||
#if !OPEN
|
||||
private bool isTestingShiftTab;
|
||||
#endif
|
||||
|
||||
// This instance data is "read-only" and need not have access serialized.
|
||||
|
||||
|
||||
@@ -215,11 +215,11 @@ namespace Microsoft.PowerShell
|
||||
PipelineFinishedWaitHandle waiterThereIsAFlyInMySoup = new PipelineFinishedWaitHandle(tempPipeline);
|
||||
|
||||
tempPipeline.InvokeAsync();
|
||||
if ((options & ExecutionOptions.ReadInputObjects) > 0 && parent.IsStandardInputRedirected)
|
||||
if ((options & ExecutionOptions.ReadInputObjects) > 0 && Console.IsInputRedirected)
|
||||
{
|
||||
// read input objects from stdin
|
||||
|
||||
WrappedDeserializer des = new WrappedDeserializer(parent.InputFormat, "Input", parent.StandardInReader);
|
||||
WrappedDeserializer des = new WrappedDeserializer(parent.InputFormat, "Input", Console.In);
|
||||
while (!des.AtEnd)
|
||||
{
|
||||
object o = des.Deserialize();
|
||||
|
||||
Reference in new issue
Block a user