diff options
author | Wim <wim@42.be> | 2019-11-17 21:42:41 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-11-17 21:42:41 +0100 |
commit | d4253d7a55f7af862304fb65c461547055dd294a (patch) | |
tree | 8a8c5ed06841ea2bd3a24639dcd5462cfdbfea32 /vendor/github.com/shazow/ssh-chat/sshd/terminal/util_windows.go | |
parent | 0917dc876613fd71c9726a34bf0138b4f5121be9 (diff) | |
download | matterbridge-msglm-d4253d7a55f7af862304fb65c461547055dd294a.tar.gz matterbridge-msglm-d4253d7a55f7af862304fb65c461547055dd294a.tar.bz2 matterbridge-msglm-d4253d7a55f7af862304fb65c461547055dd294a.zip |
Update shazow/ssh-chat dependency (#947)
Diffstat (limited to 'vendor/github.com/shazow/ssh-chat/sshd/terminal/util_windows.go')
-rw-r--r-- | vendor/github.com/shazow/ssh-chat/sshd/terminal/util_windows.go | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/vendor/github.com/shazow/ssh-chat/sshd/terminal/util_windows.go b/vendor/github.com/shazow/ssh-chat/sshd/terminal/util_windows.go new file mode 100644 index 00000000..6cb8a950 --- /dev/null +++ b/vendor/github.com/shazow/ssh-chat/sshd/terminal/util_windows.go @@ -0,0 +1,103 @@ +// Copyright 2011 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// +build windows + +// Package terminal provides support functions for dealing with terminals, as +// commonly found on UNIX systems. +// +// Putting a terminal into raw mode is the most common requirement: +// +// oldState, err := terminal.MakeRaw(0) +// if err != nil { +// panic(err) +// } +// defer terminal.Restore(0, oldState) +package terminal + +import ( + "os" + + "golang.org/x/sys/windows" +) + +type State struct { + mode uint32 +} + +// IsTerminal returns whether the given file descriptor is a terminal. +func IsTerminal(fd int) bool { + var st uint32 + err := windows.GetConsoleMode(windows.Handle(fd), &st) + return err == nil +} + +// MakeRaw put the terminal connected to the given file descriptor into raw +// mode and returns the previous state of the terminal so that it can be +// restored. +func MakeRaw(fd int) (*State, error) { + var st uint32 + if err := windows.GetConsoleMode(windows.Handle(fd), &st); err != nil { + return nil, err + } + raw := st &^ (windows.ENABLE_ECHO_INPUT | windows.ENABLE_PROCESSED_INPUT | windows.ENABLE_LINE_INPUT | windows.ENABLE_PROCESSED_OUTPUT) + if err := windows.SetConsoleMode(windows.Handle(fd), raw); err != nil { + return nil, err + } + return &State{st}, nil +} + +// GetState returns the current state of a terminal which may be useful to +// restore the terminal after a signal. +func GetState(fd int) (*State, error) { + var st uint32 + if err := windows.GetConsoleMode(windows.Handle(fd), &st); err != nil { + return nil, err + } + return &State{st}, nil +} + +// Restore restores the terminal connected to the given file descriptor to a +// previous state. +func Restore(fd int, state *State) error { + return windows.SetConsoleMode(windows.Handle(fd), state.mode) +} + +// GetSize returns the dimensions of the given terminal. +func GetSize(fd int) (width, height int, err error) { + var info windows.ConsoleScreenBufferInfo + if err := windows.GetConsoleScreenBufferInfo(windows.Handle(fd), &info); err != nil { + return 0, 0, err + } + return int(info.Size.X), int(info.Size.Y), nil +} + +// ReadPassword reads a line of input from a terminal without local echo. This +// is commonly used for inputting passwords and other sensitive data. The slice +// returned does not include the \n. +func ReadPassword(fd int) ([]byte, error) { + var st uint32 + if err := windows.GetConsoleMode(windows.Handle(fd), &st); err != nil { + return nil, err + } + old := st + + st &^= (windows.ENABLE_ECHO_INPUT) + st |= (windows.ENABLE_PROCESSED_INPUT | windows.ENABLE_LINE_INPUT | windows.ENABLE_PROCESSED_OUTPUT) + if err := windows.SetConsoleMode(windows.Handle(fd), st); err != nil { + return nil, err + } + + defer windows.SetConsoleMode(windows.Handle(fd), old) + + var h windows.Handle + p, _ := windows.GetCurrentProcess() + if err := windows.DuplicateHandle(p, windows.Handle(fd), p, &h, 0, false, windows.DUPLICATE_SAME_ACCESS); err != nil { + return nil, err + } + + f := os.NewFile(uintptr(h), "stdin") + defer f.Close() + return readPasswordLine(f) +} |