From 533a5e3225d9433d864a13777463a9f3c7341186 Mon Sep 17 00:00:00 2001 From: Jason Shirk Date: Wed, 11 May 2016 13:12:06 -0700 Subject: [PATCH] Import System.Console In/Out/Error stream changes Thanks Jason. --- .../host/msh/CommandLineParameterParser.cs | 12 +- .../host/msh/ConsoleHost.cs | 357 +++--------------- .../host/msh/ConsoleHostUserInterface.cs | 218 +++++------ .../host/msh/Executor.cs | 4 +- 4 files changed, 174 insertions(+), 417 deletions(-) diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs index d32847a4e..2f9a374e6 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs @@ -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; diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs index 04a132d93..66c4aa8d3 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs @@ -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; } /// @@ -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 /// @@ -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,53 +2372,42 @@ namespace Microsoft.PowerShell string prompt = null; string line = null; - if (displayPrompt) + if (!ui.NoPrompt) { - if (!ui.NoPrompt) + if (inBlockMode) { - if (inBlockMode) - { - // use a special prompt that denotes block mode + // use a special prompt that denotes block mode - prompt = ">> "; - } - else - { - // Make sure the cursor is at the start of the line - some external programs don't - // write a newline, so we do that for them. - if (ui.RawUI.CursorPosition.X != 0) - ui.WriteLine(); - - // Evaluate any suggestions - if(! previousResponseWasEmpty) - { - EvaluateSuggestions(ui); - } - - // Then output the prompt - if (this.parent.InDebugMode) - { - prompt = EvaluateDebugPrompt(); - } - if (prompt == null) - { - prompt = EvaluatePrompt(); - } - } - ui.Write(prompt); + prompt = ">> "; } - previousResponseWasEmpty = false; - // There could be a profile. So there could be a user defined custom readline command - line = ui.ReadLineWithTabCompletion(exec, useUserDefinedCustomReadLine: true); + else + { + // Make sure the cursor is at the start of the line - some external programs don't + // write a newline, so we do that for them. + if (ui.RawUI.CursorPosition.X != 0) + ui.WriteLine(); + // Evaluate any suggestions + if(! previousResponseWasEmpty) + { + EvaluateSuggestions(ui); + } + + // Then output the prompt + if (this.parent.InDebugMode) + { + prompt = EvaluateDebugPrompt(); + } + if (prompt == null) + { + prompt = EvaluatePrompt(); + } + } + ui.Write(prompt); } - else - { - previousResponseWasEmpty = false; - // There are no profiles. So there is no user defined custom readline command - line = ui.ReadLineWithTabCompletion(exec, useUserDefinedCustomReadLine: false); - displayPrompt = true; - } + previousResponseWasEmpty = false; + // There could be a profile. So there could be a user defined custom readline command + 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; diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs index a166b93f3..ed12c95fd 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs @@ -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 /// /// - internal bool ReadFromStdin - { - get - { - return readFromStdin; - } - set - { - readFromStdin = value; - } - } + internal bool ReadFromStdin { get; set; } /// /// @@ -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,30 +1549,80 @@ 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) + string restOfLine = null; + + string s = ReadFromStdin + ? ReadLineFromFile(initialContent) + : ReadLineFromConsole(endOnTab, initialContent, calledFromPipeline, ref restOfLine, ref result); + + if (transcribeResult) { - // When reading from a file handle instead of a console, endOnTab and initial content are ignored. - - // StreamReader.ReadLine simply returns null when EOF is reached. - - s = parent.StandardInReader.ReadLine(); - if (endOnTab && !string.IsNullOrEmpty(s) && s.IndexOf(Tab, StringComparison.OrdinalIgnoreCase) != -1) - { - result = isTestingShiftTab ? ReadLineResult.endedOnShiftTab : ReadLineResult.endedOnTab; - return s; - } - return s; + PostRead(s); } + else + { + PostRead(); + } + + if (restOfLine != null) + s += restOfLine; + + 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(); @@ -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); @@ -1706,21 +1742,9 @@ namespace Microsoft.PowerShell while (true); Dbg.Assert( - (s == null && result == ReadLineResult.endedOnBreak) - || (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; + (s == null && result == ReadLineResult.endedOnBreak) + || (s != null && result != ReadLineResult.endedOnBreak), + "s should only be null if input ended with a break"); 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 /// - /// - /// - /// - /// - /// Flag which decides if we should look for a user defined ReadLine function - /// - /// - /// /// /// /// null on a break event /// the completed line otherwise /// /// - 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 - // 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); - } + // 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,44 +2049,43 @@ 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()) { - var runspace = this.parent.LocalRunspace; - if (runspace != null - && runspace.Engine.Context.EngineIntrinsics.InvokeCommand.GetCommands(CustomReadlineCommand, CommandTypes.Function | CommandTypes.Cmdlet, nameIsPattern: false).Any()) + try { - try + PowerShell ps; + if ((runspace.ExecutionContext.EngineHostInterface.NestedPromptCount > 0) && + (Runspace.DefaultRunspace != null)) { - PowerShell ps; - if ((runspace.ExecutionContext.EngineHostInterface.NestedPromptCount > 0) && (Runspace.DefaultRunspace != null)) - { - ps = PowerShell.Create(RunspaceMode.CurrentRunspace); - } - else - { - ps = PowerShell.Create(); - ps.Runspace = runspace; - } + ps = PowerShell.Create(RunspaceMode.CurrentRunspace); + } + else + { + ps = PowerShell.Create(); + ps.Runspace = runspace; + } - var result = ps.AddCommand(CustomReadlineCommand).Invoke(); - if (result.Count == 1) - { - input = PSObject.Base(result[0]) as string; - return true; - } - } - catch (Exception e) + var result = ps.AddCommand(CustomReadlineCommand).Invoke(); + if (result.Count == 1) { - CommandProcessorBase.CheckForSevereException(e); + input = PSObject.Base(result[0]) as string; + return true; } } + catch (Exception e) + { + CommandProcessorBase.CheckForSevereException(e); + } } input = null; @@ -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. diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/Executor.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/Executor.cs index 32670dc51..9ba4e829f 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/Executor.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/Executor.cs @@ -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();