From 645249b8457696bdf21e17cf3b8029b22e314c1e Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Mon, 21 Nov 2022 10:08:35 -0800 Subject: [PATCH] [release/v7.3.1] Fix `Switch-Process` to set `termios` appropriate for child process (#18572) * add getting and setting termios * chanage to use .NET exported function * add comments Co-authored-by: Steve Lee --- .../engine/Modules/SwitchProcessCommand.cs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/System.Management.Automation/engine/Modules/SwitchProcessCommand.cs b/src/System.Management.Automation/engine/Modules/SwitchProcessCommand.cs index 1bad35bfd3..8ce2f08d84 100644 --- a/src/System.Management.Automation/engine/Modules/SwitchProcessCommand.cs +++ b/src/System.Management.Automation/engine/Modules/SwitchProcessCommand.cs @@ -69,10 +69,12 @@ namespace Microsoft.PowerShell.Commands // need null terminator at end execArgs[execArgs.Length - 1] = null; + // setup termios for a child process as .NET modifies termios dynamically for use with ReadKey() + ConfigureTerminalForChildProcess(true); int exitCode = Exec(command.Source, execArgs); - if (exitCode < 0) { + ConfigureTerminalForChildProcess(false); ThrowTerminatingError( new ErrorRecord( new Exception( @@ -108,6 +110,10 @@ namespace Microsoft.PowerShell.Commands CharSet = CharSet.Ansi, SetLastError = true)] private static extern int Exec(string path, string?[] args); + + // leverage .NET runtime's native library which abstracts the need to handle different OS and architectures for termios api + [DllImport("libSystem.Native", EntryPoint = "SystemNative_ConfigureTerminalForChildProcess")] + private static extern void ConfigureTerminalForChildProcess([MarshalAs(UnmanagedType.Bool)] bool childUsesTerminal); } }