2014-10-23 19:44:57 -04:00
|
|
|
// +build windows
|
2015-07-01 16:16:16 -04:00
|
|
|
|
2014-10-23 19:44:57 -04:00
|
|
|
package term
|
|
|
|
|
2015-03-05 19:41:48 -05:00
|
|
|
import (
|
|
|
|
"io"
|
2015-03-23 14:38:43 -04:00
|
|
|
"os"
|
2015-04-16 17:01:37 -04:00
|
|
|
"os/signal"
|
2015-10-15 14:40:14 -04:00
|
|
|
"syscall"
|
2015-03-22 12:55:21 -04:00
|
|
|
|
2015-04-16 17:01:37 -04:00
|
|
|
"github.com/Azure/go-ansiterm/winterm"
|
2015-10-15 14:40:14 -04:00
|
|
|
"github.com/docker/docker/pkg/system"
|
2015-04-16 17:01:37 -04:00
|
|
|
"github.com/docker/docker/pkg/term/windows"
|
2015-03-05 19:41:48 -05:00
|
|
|
)
|
|
|
|
|
2015-01-23 20:33:49 -05:00
|
|
|
// State holds the console mode for the terminal.
|
2014-10-23 19:44:57 -04:00
|
|
|
type State struct {
|
2015-11-11 18:04:40 -05:00
|
|
|
inMode, outMode uint32
|
|
|
|
inHandle, outHandle syscall.Handle
|
2014-10-23 19:44:57 -04:00
|
|
|
}
|
|
|
|
|
2015-01-23 20:33:49 -05:00
|
|
|
// Winsize is used for window size.
|
2014-10-23 19:44:57 -04:00
|
|
|
type Winsize struct {
|
|
|
|
Height uint16
|
|
|
|
Width uint16
|
|
|
|
}
|
|
|
|
|
2015-11-11 18:04:40 -05:00
|
|
|
const (
|
|
|
|
// https://msdn.microsoft.com/en-us/library/windows/desktop/ms683167(v=vs.85).aspx
|
|
|
|
enableVirtualTerminalInput = 0x0200
|
|
|
|
enableVirtualTerminalProcessing = 0x0004
|
2016-03-24 18:37:47 -04:00
|
|
|
disableNewlineAutoReturn = 0x0008
|
2015-11-11 18:04:40 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// usingNativeConsole is true if we are using the Windows native console
|
|
|
|
var usingNativeConsole bool
|
|
|
|
|
2015-07-25 04:35:07 -04:00
|
|
|
// StdStreams returns the standard streams (stdin, stdout, stedrr).
|
2015-04-02 12:35:13 -04:00
|
|
|
func StdStreams() (stdIn io.ReadCloser, stdOut, stdErr io.Writer) {
|
|
|
|
switch {
|
|
|
|
case os.Getenv("ConEmuANSI") == "ON":
|
2015-10-30 04:10:12 -04:00
|
|
|
// The ConEmu terminal emulates ANSI on output streams well.
|
|
|
|
return windows.ConEmuStreams()
|
2015-04-02 12:35:13 -04:00
|
|
|
case os.Getenv("MSYSTEM") != "":
|
|
|
|
// MSYS (mingw) does not emulate ANSI well.
|
2015-04-16 17:01:37 -04:00
|
|
|
return windows.ConsoleStreams()
|
2015-04-02 12:35:13 -04:00
|
|
|
default:
|
2015-10-15 14:40:14 -04:00
|
|
|
if useNativeConsole() {
|
2015-11-11 18:04:40 -05:00
|
|
|
usingNativeConsole = true
|
2015-10-15 14:40:14 -04:00
|
|
|
return os.Stdin, os.Stdout, os.Stderr
|
|
|
|
}
|
2015-04-16 17:01:37 -04:00
|
|
|
return windows.ConsoleStreams()
|
2015-04-02 12:35:13 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-15 14:40:14 -04:00
|
|
|
// useNativeConsole determines if the docker client should use the built-in
|
|
|
|
// console which supports ANSI emulation, or fall-back to the golang emulator
|
|
|
|
// (github.com/azure/go-ansiterm).
|
|
|
|
func useNativeConsole() bool {
|
2016-03-16 21:45:40 -04:00
|
|
|
osv := system.GetOSVersion()
|
2015-10-15 14:40:14 -04:00
|
|
|
|
2015-11-11 18:04:40 -05:00
|
|
|
// Native console is not available before major version 10
|
2015-10-15 14:40:14 -04:00
|
|
|
if osv.MajorVersion < 10 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-11-11 18:04:40 -05:00
|
|
|
// Get the console modes. If this fails, we can't use the native console
|
|
|
|
state, err := getNativeConsole()
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Probe the console to see if it can be enabled.
|
|
|
|
if nil != probeNativeConsole(state) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-10-15 14:40:14 -04:00
|
|
|
// Environment variable override
|
|
|
|
if e := os.Getenv("USE_NATIVE_CONSOLE"); e != "" {
|
|
|
|
if e == "1" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-05-20 22:19:26 -04:00
|
|
|
// Must have a post-TP5 RS1 build of Windows Server 2016/Windows 10 for
|
|
|
|
// the native console to be usable.
|
|
|
|
if osv.Build < 14350 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
2015-11-11 18:04:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// getNativeConsole returns the console modes ('state') for the native Windows console
|
|
|
|
func getNativeConsole() (State, error) {
|
|
|
|
var (
|
|
|
|
err error
|
|
|
|
state State
|
|
|
|
)
|
|
|
|
|
2015-10-15 14:40:14 -04:00
|
|
|
// Get the handle to stdout
|
2015-11-11 18:04:40 -05:00
|
|
|
if state.outHandle, err = syscall.GetStdHandle(syscall.STD_OUTPUT_HANDLE); err != nil {
|
|
|
|
return state, err
|
2015-10-15 14:40:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get the console mode from the consoles stdout handle
|
2015-11-11 18:04:40 -05:00
|
|
|
if err = syscall.GetConsoleMode(state.outHandle, &state.outMode); err != nil {
|
|
|
|
return state, err
|
2015-10-15 14:40:14 -04:00
|
|
|
}
|
|
|
|
|
2015-11-11 18:04:40 -05:00
|
|
|
// Get the handle to stdin
|
|
|
|
if state.inHandle, err = syscall.GetStdHandle(syscall.STD_INPUT_HANDLE); err != nil {
|
|
|
|
return state, err
|
2015-10-15 14:40:14 -04:00
|
|
|
}
|
|
|
|
|
2015-11-11 18:04:40 -05:00
|
|
|
// Get the console mode from the consoles stdin handle
|
|
|
|
if err = syscall.GetConsoleMode(state.inHandle, &state.inMode); err != nil {
|
|
|
|
return state, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return state, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// probeNativeConsole probes the console to determine if native can be supported,
|
|
|
|
func probeNativeConsole(state State) error {
|
|
|
|
if err := winterm.SetConsoleMode(uintptr(state.outHandle), state.outMode|enableVirtualTerminalProcessing); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer winterm.SetConsoleMode(uintptr(state.outHandle), state.outMode)
|
|
|
|
|
|
|
|
if err := winterm.SetConsoleMode(uintptr(state.inHandle), state.inMode|enableVirtualTerminalInput); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer winterm.SetConsoleMode(uintptr(state.inHandle), state.inMode)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// enableNativeConsole turns on native console mode
|
|
|
|
func enableNativeConsole(state State) error {
|
2016-03-24 18:37:47 -04:00
|
|
|
// First attempt both enableVirtualTerminalProcessing and disableNewlineAutoReturn
|
|
|
|
if err := winterm.SetConsoleMode(uintptr(state.outHandle),
|
|
|
|
state.outMode|(enableVirtualTerminalProcessing|disableNewlineAutoReturn)); err != nil {
|
|
|
|
|
|
|
|
// That may fail, so fallback to trying just enableVirtualTerminalProcessing
|
|
|
|
if err := winterm.SetConsoleMode(uintptr(state.outHandle), state.outMode|enableVirtualTerminalProcessing); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-11-11 18:04:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := winterm.SetConsoleMode(uintptr(state.inHandle), state.inMode|enableVirtualTerminalInput); err != nil {
|
|
|
|
winterm.SetConsoleMode(uintptr(state.outHandle), state.outMode) // restore out if we can
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// disableNativeConsole turns off native console mode
|
|
|
|
func disableNativeConsole(state *State) error {
|
|
|
|
// Try and restore both in an out before error checking.
|
|
|
|
errout := winterm.SetConsoleMode(uintptr(state.outHandle), state.outMode)
|
|
|
|
errin := winterm.SetConsoleMode(uintptr(state.inHandle), state.inMode)
|
|
|
|
if errout != nil {
|
|
|
|
return errout
|
|
|
|
}
|
|
|
|
if errin != nil {
|
|
|
|
return errin
|
|
|
|
}
|
|
|
|
return nil
|
2015-10-15 14:40:14 -04:00
|
|
|
}
|
|
|
|
|
2015-07-25 04:35:07 -04:00
|
|
|
// GetFdInfo returns the file descriptor for an os.File and indicates whether the file represents a terminal.
|
2015-04-02 12:35:13 -04:00
|
|
|
func GetFdInfo(in interface{}) (uintptr, bool) {
|
2015-04-16 17:01:37 -04:00
|
|
|
return windows.GetHandleInfo(in)
|
2015-04-02 12:35:13 -04:00
|
|
|
}
|
|
|
|
|
2015-07-25 04:35:07 -04:00
|
|
|
// GetWinsize returns the window size based on the specified file descriptor.
|
2014-10-23 19:44:57 -04:00
|
|
|
func GetWinsize(fd uintptr) (*Winsize, error) {
|
2015-04-16 17:01:37 -04:00
|
|
|
info, err := winterm.GetConsoleScreenBufferInfo(fd)
|
2014-10-23 19:44:57 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-01-23 20:33:49 -05:00
|
|
|
|
2015-04-16 17:01:37 -04:00
|
|
|
winsize := &Winsize{
|
2015-04-06 17:31:42 -04:00
|
|
|
Width: uint16(info.Window.Right - info.Window.Left + 1),
|
|
|
|
Height: uint16(info.Window.Bottom - info.Window.Top + 1),
|
2016-03-31 13:53:21 -04:00
|
|
|
}
|
2015-04-16 17:01:37 -04:00
|
|
|
|
|
|
|
return winsize, nil
|
2014-10-23 19:44:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsTerminal returns true if the given file descriptor is a terminal.
|
|
|
|
func IsTerminal(fd uintptr) bool {
|
2015-04-16 17:01:37 -04:00
|
|
|
return windows.IsConsole(fd)
|
2014-10-23 19:44:57 -04:00
|
|
|
}
|
|
|
|
|
2015-07-25 04:35:07 -04:00
|
|
|
// RestoreTerminal restores the terminal connected to the given file descriptor
|
|
|
|
// to a previous state.
|
2014-10-23 19:44:57 -04:00
|
|
|
func RestoreTerminal(fd uintptr, state *State) error {
|
2015-11-11 18:04:40 -05:00
|
|
|
if usingNativeConsole {
|
|
|
|
return disableNativeConsole(state)
|
|
|
|
}
|
|
|
|
return winterm.SetConsoleMode(fd, state.outMode)
|
2014-10-23 19:44:57 -04:00
|
|
|
}
|
|
|
|
|
2015-04-02 12:35:13 -04:00
|
|
|
// SaveState saves the state of the terminal connected to the given file descriptor.
|
2014-10-23 19:44:57 -04:00
|
|
|
func SaveState(fd uintptr) (*State, error) {
|
2015-11-11 18:04:40 -05:00
|
|
|
if usingNativeConsole {
|
|
|
|
state, err := getNativeConsole()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &state, nil
|
|
|
|
}
|
|
|
|
|
2015-04-16 17:01:37 -04:00
|
|
|
mode, e := winterm.GetConsoleMode(fd)
|
2014-10-23 19:44:57 -04:00
|
|
|
if e != nil {
|
|
|
|
return nil, e
|
|
|
|
}
|
2015-11-11 18:04:40 -05:00
|
|
|
|
|
|
|
return &State{outMode: mode}, nil
|
2014-10-23 19:44:57 -04:00
|
|
|
}
|
|
|
|
|
2015-04-02 12:35:13 -04:00
|
|
|
// DisableEcho disables echo for the terminal connected to the given file descriptor.
|
2015-04-16 17:01:37 -04:00
|
|
|
// -- See https://msdn.microsoft.com/en-us/library/windows/desktop/ms683462(v=vs.85).aspx
|
2014-10-23 19:44:57 -04:00
|
|
|
func DisableEcho(fd uintptr, state *State) error {
|
2015-11-11 18:04:40 -05:00
|
|
|
mode := state.inMode
|
2015-04-16 17:01:37 -04:00
|
|
|
mode &^= winterm.ENABLE_ECHO_INPUT
|
|
|
|
mode |= winterm.ENABLE_PROCESSED_INPUT | winterm.ENABLE_LINE_INPUT
|
|
|
|
err := winterm.SetConsoleMode(fd, mode)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register an interrupt handler to catch and restore prior state
|
|
|
|
restoreAtInterrupt(fd, state)
|
|
|
|
return nil
|
2014-10-23 19:44:57 -04:00
|
|
|
}
|
|
|
|
|
2015-01-23 20:33:49 -05:00
|
|
|
// SetRawTerminal puts the terminal connected to the given file descriptor into raw
|
2015-07-25 04:35:07 -04:00
|
|
|
// mode and returns the previous state.
|
2014-10-23 19:44:57 -04:00
|
|
|
func SetRawTerminal(fd uintptr) (*State, error) {
|
2015-04-02 12:35:13 -04:00
|
|
|
state, err := MakeRaw(fd)
|
2014-10-23 19:44:57 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-04-16 17:01:37 -04:00
|
|
|
|
|
|
|
// Register an interrupt handler to catch and restore prior state
|
|
|
|
restoreAtInterrupt(fd, state)
|
2015-04-02 12:35:13 -04:00
|
|
|
return state, err
|
2014-10-23 19:44:57 -04:00
|
|
|
}
|
|
|
|
|
2015-04-16 17:01:37 -04:00
|
|
|
// MakeRaw puts the terminal (Windows Console) connected to the given file descriptor into raw
|
|
|
|
// mode and returns the previous state of the terminal so that it can be restored.
|
2014-10-23 19:44:57 -04:00
|
|
|
func MakeRaw(fd uintptr) (*State, error) {
|
|
|
|
state, err := SaveState(fd)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-11-11 18:04:40 -05:00
|
|
|
mode := state.inMode
|
|
|
|
if usingNativeConsole {
|
|
|
|
if err := enableNativeConsole(*state); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
mode |= enableVirtualTerminalInput
|
|
|
|
}
|
|
|
|
|
2015-04-02 12:35:13 -04:00
|
|
|
// See
|
|
|
|
// -- https://msdn.microsoft.com/en-us/library/windows/desktop/ms686033(v=vs.85).aspx
|
|
|
|
// -- https://msdn.microsoft.com/en-us/library/windows/desktop/ms683462(v=vs.85).aspx
|
|
|
|
|
|
|
|
// Disable these modes
|
2015-04-16 17:01:37 -04:00
|
|
|
mode &^= winterm.ENABLE_ECHO_INPUT
|
|
|
|
mode &^= winterm.ENABLE_LINE_INPUT
|
|
|
|
mode &^= winterm.ENABLE_MOUSE_INPUT
|
|
|
|
mode &^= winterm.ENABLE_WINDOW_INPUT
|
|
|
|
mode &^= winterm.ENABLE_PROCESSED_INPUT
|
2015-04-02 12:35:13 -04:00
|
|
|
|
|
|
|
// Enable these modes
|
2015-04-16 17:01:37 -04:00
|
|
|
mode |= winterm.ENABLE_EXTENDED_FLAGS
|
|
|
|
mode |= winterm.ENABLE_INSERT_MODE
|
|
|
|
mode |= winterm.ENABLE_QUICK_EDIT_MODE
|
2015-04-02 12:35:13 -04:00
|
|
|
|
2015-04-16 17:01:37 -04:00
|
|
|
err = winterm.SetConsoleMode(fd, mode)
|
2014-10-23 19:44:57 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return state, nil
|
|
|
|
}
|
2015-04-16 17:01:37 -04:00
|
|
|
|
|
|
|
func restoreAtInterrupt(fd uintptr, state *State) {
|
|
|
|
sigchan := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(sigchan, os.Interrupt)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
_ = <-sigchan
|
|
|
|
RestoreTerminal(fd, state)
|
|
|
|
os.Exit(0)
|
|
|
|
}()
|
|
|
|
}
|