Exemple #1
1
// getNativeConsole returns the console modes ('state') for the native Windows console
func getNativeConsole() (State, error) {
	var (
		err   error
		state State
	)

	// Get the handle to stdout
	if state.outHandle, err = syscall.GetStdHandle(syscall.STD_OUTPUT_HANDLE); err != nil {
		return state, err
	}

	// Get the console mode from the consoles stdout handle
	if err = syscall.GetConsoleMode(state.outHandle, &state.outMode); err != nil {
		return state, err
	}

	// Get the handle to stdin
	if state.inHandle, err = syscall.GetStdHandle(syscall.STD_INPUT_HANDLE); err != nil {
		return state, err
	}

	// 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
}
func (pr passwordReader) PromptForPassword(promptText string, args ...interface{}) (passwd string) {

	hStdin := syscall.Handle(os.Stdin.Fd())
	var originalMode uint32

	err := syscall.GetConsoleMode(hStdin, &originalMode)
	if err != nil {
		return
	}

	var newMode uint32 = (originalMode &^ ENABLE_ECHO_INPUT)

	err = setConsoleMode(hStdin, newMode)
	defer setConsoleMode(hStdin, originalMode)

	if err != nil {
		return
	}

	// Display the prompt.
	fmt.Printf(promptText+": ", args...)
	defer fmt.Println("")

	rd := bufio.NewReader(os.Stdin)

	line, err := rd.ReadString('\r')

	if err == nil {
		return strings.TrimSpace(line)
	}
	return ""
}
// NewTerminal creates a terminal and sets it to raw input mode.
func NewTerminal() (*Terminal, error) {
	term := &Terminal{
		In:       os.Stdin,
		Out:      os.Stdout,
		History:  make([]string, 0, 10),
		histIdx:  -1,
		terminal: new(terminal),
	}

	err := syscall.GetConsoleMode(syscall.Handle(term.In.Fd()), &term.origMode)
	if err != nil {
		return term, nil
	}
	mode := term.origMode
	term.isTerm = true

	// Set new mode flags.
	mode &^= (echoInputFlag | insertModeFlag | lineInputFlag | mouseInputFlag |
		processedInputFlag | windowInputFlag)

	ret, _, err := setConsoleMode.Call(term.In.Fd(), uintptr(mode))
	if ret == 0 {
		return nil, err
	}

	return term, nil
}
Exemple #4
0
// newFile returns a new File with the given file handle and name.
// Unlike NewFile, it does not check that h is syscall.InvalidHandle.
func newFile(h syscall.Handle, name string) *File {
	f := &File{&file{fd: h, name: name}}
	var m uint32
	if syscall.GetConsoleMode(f.fd, &m) == nil {
		f.isConsole = true
	}
	runtime.SetFinalizer(f.file, (*file).close)
	return f
}
Exemple #5
0
// DisableFlags disables the terminal flags specified in the function
// argument 'flags', and returns the previous state of the terminal
// so that it can be restored.
func DisableFlags(fd FileDescriptor, flags Flag) (*State, error) {
	var st uint32
	err := syscall.GetConsoleMode(handle(fd), &st)
	if err != nil {
		return nil, err
	}
	st &^= uint32(flags)
	err = win.SetConsoleMode(handle(fd), st)
	return &State{st}, err
}
// Might not catch everything...
func IsAtty(fd uintptr) bool {
	handle := syscall.Handle(fd)

	var mode uint32
	err := syscall.GetConsoleMode(handle, &mode)
	if err != nil {
		return false
	}
	return true
}
Exemple #7
0
// NewFile returns a new File with the given file descriptor and name.
func NewFile(fd uintptr, name string) *File {
	h := syscall.Handle(fd)
	if h == syscall.InvalidHandle {
		return nil
	}
	var m uint32
	if syscall.GetConsoleMode(h, &m) == nil {
		return newConsoleFile(h, name)
	}
	return newFile(h, name)
}
Exemple #8
0
// NewFile returns a new File with the given file descriptor and name.
func NewFile(fd uintptr, name string) *File {
	h := syscall.Handle(fd)
	if h == syscall.InvalidHandle {
		return nil
	}
	f := &File{&file{fd: h, name: name}}
	var m uint32
	if syscall.GetConsoleMode(f.fd, &m) == nil {
		f.isConsole = true
	}
	runtime.SetFinalizer(f.file, (*file).close)
	return f
}
Exemple #9
0
func main() {
	var mode uint32
	err := syscall.GetConsoleMode(syscall.Stdin, &mode)

	isConsole := err == nil

	commandName := filepath.Base(os.Args[0])

	if isConsole == true || commandName != names.Jujud {
		runConsole()
	} else {
		runService()
	}
}
// 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 {
	osv, err := system.GetOSVersion()
	if err != nil {
		return false
	}

	// Native console is not available major version 10
	if osv.MajorVersion < 10 {
		return false
	}

	// Must have a late pre-release TP4 build of Windows Server 2016/Windows 10 TH2 or later
	if osv.Build < 10578 {
		return false
	}

	// Environment variable override
	if e := os.Getenv("USE_NATIVE_CONSOLE"); e != "" {
		if e == "1" {
			return true
		}
		return false
	}

	// Get the handle to stdout
	stdOutHandle, err := syscall.GetStdHandle(syscall.STD_OUTPUT_HANDLE)
	if err != nil {
		return false
	}

	// Get the console mode from the consoles stdout handle
	var mode uint32
	if err := syscall.GetConsoleMode(stdOutHandle, &mode); err != nil {
		return false
	}

	// Legacy mode does not have native ANSI emulation.
	// https://msdn.microsoft.com/en-us/library/windows/desktop/ms683167(v=vs.85).aspx
	const enableVirtualTerminalProcessing = 0x0004
	if mode&enableVirtualTerminalProcessing == 0 {
		return false
	}

	// TODO Windows (Post TP4). The native emulator still has issues which
	// mean it shouldn't be enabled for everyone. Change this next line to true
	// to change the default to "enable if available". In the meantime, users
	// can still try it out by using USE_NATIVE_CONSOLE env variable.
	return false
}
Exemple #11
0
func makeRaw(console syscall.Handle) (func(), error) {

	// Get old mode so that we can recover later
	var oldMode uint32
	if err := syscall.GetConsoleMode(console, &oldMode); err != nil {
		return nil, err
	}

	var newMode uint32 = uint32(int(oldMode) & ^ENABLE_ECHO_INPUT)
	if err := setConsoleMode(console, newMode); err != nil {
		return nil, err
	}

	return func() {
		setConsoleMode(console, oldMode)
	}, nil
}
func getPassword() (password string, err error) {
	var oldMode uint32

	err = syscall.GetConsoleMode(syscall.Stdin, &oldMode)
	if err != nil {
		return
	}

	var newMode uint32 = (oldMode &^ ENABLE_ECHO_INPUT)

	err = setConsoleMode(syscall.Stdin, newMode)
	defer setConsoleMode(syscall.Stdin, oldMode)
	if err != nil {
		return
	}

	return readline()
}
func read(f *os.File) (string, error) {
	handle := syscall.Handle(f.Fd())

	// Grab the old console mode so we can reset it. We defer the reset
	// right away because it doesn't matter (it is idempotent).
	var oldMode uint32
	if err := syscall.GetConsoleMode(handle, &oldMode); err != nil {
		return "", err
	}
	defer setConsoleMode(handle, oldMode)

	// The new mode is the old mode WITHOUT the echo input flag set.
	var newMode uint32 = uint32(int(oldMode) & ^ENABLE_ECHO_INPUT)
	if err := setConsoleMode(handle, newMode); err != nil {
		return "", err
	}

	return readline(f)
}
Exemple #14
0
func (ui terminalUI) AskForPassword(prompt string, args ...interface{}) (passwd string) {
	hStdin := syscall.Handle(os.Stdin.Fd())
	var originalMode uint32

	err := syscall.GetConsoleMode(hStdin, &originalMode)
	if err != nil {
		return
	}

	var newMode = uint32(originalMode &^ EnableEchoInput)

	err = setConsoleMode(hStdin, newMode)
	defer setConsoleMode(hStdin, originalMode)
	defer ui.Say("")

	if err != nil {
		return
	}

	return ui.Ask(prompt, args...)
}
Exemple #15
0
func (ui terminalUI) AskForPassword(prompt string) (passwd string) {
	hStdin := syscall.Handle(os.Stdin.Fd())
	var originalMode uint32

	err := syscall.GetConsoleMode(hStdin, &originalMode)
	if err != nil {
		return
	}

	var newMode uint32 = (originalMode &^ ENABLE_ECHO_INPUT)

	err = setConsoleMode(hStdin, newMode)
	defer setConsoleMode(hStdin, originalMode)
	defer ui.Say("")

	if err != nil {
		return
	}

	return ui.Ask(prompt)
}
// getColorableWriter will return a writer that is capable
// of interpreting ANSI escape codes for terminal colors.
func getColorableWriter() io.Writer {
	if strings.ToLower(os.Getenv("ConEmuANSI")) == "on" {
		// The ConEmu terminal is installed. Use it.
		return os.Stdout
	}

	const ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x0004

	h, err := syscall.GetStdHandle(syscall.STD_OUTPUT_HANDLE)
	if err != nil {
		return os.Stdout
	}
	var m uint32
	err = syscall.GetConsoleMode(h, &m)
	if err != nil {
		return os.Stdout
	}
	if m&ENABLE_VIRTUAL_TERMINAL_PROCESSING != 0 {
		return os.Stdout
	}
	return colorable.NewColorableStdout()
}
Exemple #17
0
// NewTerminal creates a terminal and sets it to raw input mode.
func NewTerminal() (*Terminal, error) {
	if inReader == nil {
		inReader = bufio.NewReader(os.Stdin)
	}
	term := &Terminal{In: os.Stdin, Out: os.Stdout}

	err := syscall.GetConsoleMode(syscall.Handle(term.In.Fd()), &term.origMode)
	if err != nil {
		return term, nil
	}
	mode := term.origMode
	term.isTerm = true

	// Set new mode flags.
	mode &^= (echoInputFlag | insertModeFlag | lineInputFlag | mouseInputFlag |
		processedInputFlag | windowInputFlag)

	ret, _, err := setConsoleMode.Call(term.In.Fd(), uintptr(mode))
	if ret == 0 {
		return nil, errors.NewStackError(err)
	}

	return term, nil
}
Exemple #18
0
// GetConsoleMode gets the console mode for given file descriptor
// See http://msdn.microsoft.com/en-us/library/windows/desktop/ms683167(v=vs.85).aspx.
func GetConsoleMode(handle uintptr) (mode uint32, err error) {
	err = syscall.GetConsoleMode(syscall.Handle(handle), &mode)
	return mode, err
}
Exemple #19
0
// IsTerminal returns true if the given file descriptor is a terminal.
func IsTerminal(fd FileDescriptor) (is bool) {
	var mode uint32
	is = syscall.GetConsoleMode(handle(fd), &mode) == nil
	return
}
Exemple #20
0
func GetConsoleMode(fileDesc uintptr) (uint32, error) {
	var mode uint32
	err := syscall.GetConsoleMode(syscall.Handle(fileDesc), &mode)
	return mode, err
}