2020-04-16 05:23:37 -04:00
|
|
|
package term
|
2017-04-17 18:08:24 -04:00
|
|
|
|
|
|
|
import (
|
2023-05-02 08:22:36 -04:00
|
|
|
"fmt"
|
2017-04-17 18:08:24 -04:00
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
|
2020-04-16 05:23:37 -04:00
|
|
|
windowsconsole "github.com/moby/term/windows"
|
2020-05-09 11:28:17 -04:00
|
|
|
"golang.org/x/sys/windows"
|
2017-04-17 18:08:24 -04:00
|
|
|
)
|
|
|
|
|
2023-05-02 08:22:36 -04:00
|
|
|
// terminalState holds the platform-specific state / console mode for the terminal.
|
|
|
|
type terminalState struct {
|
2017-04-17 18:08:24 -04:00
|
|
|
mode uint32
|
|
|
|
}
|
|
|
|
|
2017-10-11 09:30:07 -04:00
|
|
|
// vtInputSupported is true if winterm.ENABLE_VIRTUAL_TERMINAL_INPUT is supported by the console
|
2017-04-17 18:08:24 -04:00
|
|
|
var vtInputSupported bool
|
|
|
|
|
2023-05-02 08:22:36 -04:00
|
|
|
func stdStreams() (stdIn io.ReadCloser, stdOut, stdErr io.Writer) {
|
2017-04-17 18:08:24 -04:00
|
|
|
// Turn on VT handling on all std handles, if possible. This might
|
|
|
|
// fail, in which case we will fall back to terminal emulation.
|
2020-05-09 11:28:17 -04:00
|
|
|
var (
|
|
|
|
emulateStdin, emulateStdout, emulateStderr bool
|
|
|
|
|
|
|
|
mode uint32
|
|
|
|
)
|
|
|
|
|
|
|
|
fd := windows.Handle(os.Stdin.Fd())
|
|
|
|
if err := windows.GetConsoleMode(fd, &mode); err == nil {
|
2017-10-11 09:30:07 -04:00
|
|
|
// Validate that winterm.ENABLE_VIRTUAL_TERMINAL_INPUT is supported, but do not set it.
|
2020-05-09 11:28:17 -04:00
|
|
|
if err = windows.SetConsoleMode(fd, mode|windows.ENABLE_VIRTUAL_TERMINAL_INPUT); err != nil {
|
2017-04-17 18:08:24 -04:00
|
|
|
emulateStdin = true
|
|
|
|
} else {
|
|
|
|
vtInputSupported = true
|
|
|
|
}
|
|
|
|
// Unconditionally set the console mode back even on failure because SetConsoleMode
|
|
|
|
// remembers invalid bits on input handles.
|
2020-05-09 11:28:17 -04:00
|
|
|
_ = windows.SetConsoleMode(fd, mode)
|
2017-04-17 18:08:24 -04:00
|
|
|
}
|
|
|
|
|
2020-05-09 11:28:17 -04:00
|
|
|
fd = windows.Handle(os.Stdout.Fd())
|
|
|
|
if err := windows.GetConsoleMode(fd, &mode); err == nil {
|
2017-10-11 09:30:07 -04:00
|
|
|
// Validate winterm.DISABLE_NEWLINE_AUTO_RETURN is supported, but do not set it.
|
2020-05-09 11:28:17 -04:00
|
|
|
if err = windows.SetConsoleMode(fd, mode|windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING|windows.DISABLE_NEWLINE_AUTO_RETURN); err != nil {
|
2017-04-17 18:08:24 -04:00
|
|
|
emulateStdout = true
|
|
|
|
} else {
|
2020-05-09 11:28:17 -04:00
|
|
|
_ = windows.SetConsoleMode(fd, mode|windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING)
|
2017-04-17 18:08:24 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-09 11:28:17 -04:00
|
|
|
fd = windows.Handle(os.Stderr.Fd())
|
|
|
|
if err := windows.GetConsoleMode(fd, &mode); err == nil {
|
2017-10-11 09:30:07 -04:00
|
|
|
// Validate winterm.DISABLE_NEWLINE_AUTO_RETURN is supported, but do not set it.
|
2020-05-09 11:28:17 -04:00
|
|
|
if err = windows.SetConsoleMode(fd, mode|windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING|windows.DISABLE_NEWLINE_AUTO_RETURN); err != nil {
|
2017-04-17 18:08:24 -04:00
|
|
|
emulateStderr = true
|
|
|
|
} else {
|
2020-05-09 11:28:17 -04:00
|
|
|
_ = windows.SetConsoleMode(fd, mode|windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING)
|
2017-04-17 18:08:24 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if emulateStdin {
|
2020-11-23 16:10:34 -05:00
|
|
|
h := uint32(windows.STD_INPUT_HANDLE)
|
|
|
|
stdIn = windowsconsole.NewAnsiReader(int(h))
|
2017-04-17 18:08:24 -04:00
|
|
|
} else {
|
|
|
|
stdIn = os.Stdin
|
|
|
|
}
|
|
|
|
|
|
|
|
if emulateStdout {
|
2020-11-23 16:10:34 -05:00
|
|
|
h := uint32(windows.STD_OUTPUT_HANDLE)
|
|
|
|
stdOut = windowsconsole.NewAnsiWriter(int(h))
|
2017-04-17 18:08:24 -04:00
|
|
|
} else {
|
|
|
|
stdOut = os.Stdout
|
|
|
|
}
|
|
|
|
|
|
|
|
if emulateStderr {
|
2020-11-23 16:10:34 -05:00
|
|
|
h := uint32(windows.STD_ERROR_HANDLE)
|
|
|
|
stdErr = windowsconsole.NewAnsiWriter(int(h))
|
2017-04-17 18:08:24 -04:00
|
|
|
} else {
|
|
|
|
stdErr = os.Stderr
|
|
|
|
}
|
|
|
|
|
2023-05-02 08:22:36 -04:00
|
|
|
return stdIn, stdOut, stdErr
|
2017-04-17 18:08:24 -04:00
|
|
|
}
|
|
|
|
|
2023-05-02 08:22:36 -04:00
|
|
|
func getFdInfo(in interface{}) (uintptr, bool) {
|
2017-05-10 18:41:46 -04:00
|
|
|
return windowsconsole.GetHandleInfo(in)
|
2017-04-17 18:08:24 -04:00
|
|
|
}
|
|
|
|
|
2023-05-02 08:22:36 -04:00
|
|
|
func getWinsize(fd uintptr) (*Winsize, error) {
|
2020-05-09 11:28:17 -04:00
|
|
|
var info windows.ConsoleScreenBufferInfo
|
|
|
|
if err := windows.GetConsoleScreenBufferInfo(windows.Handle(fd), &info); err != nil {
|
2017-04-17 18:08:24 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
winsize := &Winsize{
|
|
|
|
Width: uint16(info.Window.Right - info.Window.Left + 1),
|
|
|
|
Height: uint16(info.Window.Bottom - info.Window.Top + 1),
|
|
|
|
}
|
|
|
|
|
|
|
|
return winsize, nil
|
|
|
|
}
|
|
|
|
|
2023-05-02 08:22:36 -04:00
|
|
|
func setWinsize(fd uintptr, ws *Winsize) error {
|
|
|
|
return fmt.Errorf("not implemented on Windows")
|
|
|
|
}
|
|
|
|
|
|
|
|
func isTerminal(fd uintptr) bool {
|
2020-05-09 11:28:17 -04:00
|
|
|
var mode uint32
|
|
|
|
err := windows.GetConsoleMode(windows.Handle(fd), &mode)
|
|
|
|
return err == nil
|
2017-04-17 18:08:24 -04:00
|
|
|
}
|
|
|
|
|
2023-05-02 08:22:36 -04:00
|
|
|
func restoreTerminal(fd uintptr, state *State) error {
|
2020-05-09 11:28:17 -04:00
|
|
|
return windows.SetConsoleMode(windows.Handle(fd), state.mode)
|
2017-04-17 18:08:24 -04:00
|
|
|
}
|
|
|
|
|
2023-05-02 08:22:36 -04:00
|
|
|
func saveState(fd uintptr) (*State, error) {
|
2020-05-09 11:28:17 -04:00
|
|
|
var mode uint32
|
|
|
|
|
|
|
|
if err := windows.GetConsoleMode(windows.Handle(fd), &mode); err != nil {
|
|
|
|
return nil, err
|
2017-04-17 18:08:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return &State{mode: mode}, nil
|
|
|
|
}
|
|
|
|
|
2023-05-02 08:22:36 -04:00
|
|
|
func disableEcho(fd uintptr, state *State) error {
|
|
|
|
// See https://msdn.microsoft.com/en-us/library/windows/desktop/ms683462(v=vs.85).aspx
|
2017-04-17 18:08:24 -04:00
|
|
|
mode := state.mode
|
2020-05-09 11:28:17 -04:00
|
|
|
mode &^= windows.ENABLE_ECHO_INPUT
|
|
|
|
mode |= windows.ENABLE_PROCESSED_INPUT | windows.ENABLE_LINE_INPUT
|
|
|
|
err := windows.SetConsoleMode(windows.Handle(fd), mode)
|
2017-04-17 18:08:24 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register an interrupt handler to catch and restore prior state
|
|
|
|
restoreAtInterrupt(fd, state)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-05-02 08:22:36 -04:00
|
|
|
func setRawTerminal(fd uintptr) (*State, error) {
|
|
|
|
oldState, err := MakeRaw(fd)
|
2017-04-17 18:08:24 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register an interrupt handler to catch and restore prior state
|
2023-05-02 08:22:36 -04:00
|
|
|
restoreAtInterrupt(fd, oldState)
|
|
|
|
return oldState, err
|
2017-04-17 18:08:24 -04:00
|
|
|
}
|
|
|
|
|
2023-05-02 08:22:36 -04:00
|
|
|
func setRawTerminalOutput(fd uintptr) (*State, error) {
|
|
|
|
oldState, err := saveState(fd)
|
2017-04-17 18:08:24 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-10-11 09:30:07 -04:00
|
|
|
// Ignore failures, since winterm.DISABLE_NEWLINE_AUTO_RETURN might not be supported on this
|
2017-04-17 18:08:24 -04:00
|
|
|
// version of Windows.
|
2023-05-02 08:22:36 -04:00
|
|
|
_ = windows.SetConsoleMode(windows.Handle(fd), oldState.mode|windows.DISABLE_NEWLINE_AUTO_RETURN)
|
|
|
|
return oldState, err
|
2017-04-17 18:08:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func restoreAtInterrupt(fd uintptr, state *State) {
|
|
|
|
sigchan := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(sigchan, os.Interrupt)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
_ = <-sigchan
|
2020-05-09 11:28:17 -04:00
|
|
|
_ = RestoreTerminal(fd, state)
|
2017-04-17 18:08:24 -04:00
|
|
|
os.Exit(0)
|
|
|
|
}()
|
|
|
|
}
|