2020-11-23 15:51:54 -05:00
|
|
|
// Copyright 2019 The Go Authors. All rights reserved.
|
2017-04-17 18:08:24 -04:00
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2020-11-23 15:51:54 -05:00
|
|
|
package term
|
2017-04-17 18:08:24 -04:00
|
|
|
|
|
|
|
import (
|
2018-06-08 05:26:10 -04:00
|
|
|
"os"
|
2017-04-17 18:08:24 -04:00
|
|
|
|
2018-06-08 05:26:10 -04:00
|
|
|
"golang.org/x/sys/windows"
|
2017-04-17 18:08:24 -04:00
|
|
|
)
|
|
|
|
|
2020-11-23 15:51:54 -05:00
|
|
|
type state struct {
|
2017-04-17 18:08:24 -04:00
|
|
|
mode uint32
|
|
|
|
}
|
|
|
|
|
2020-11-23 15:51:54 -05:00
|
|
|
func isTerminal(fd int) bool {
|
2017-04-17 18:08:24 -04:00
|
|
|
var st uint32
|
2018-06-08 05:26:10 -04:00
|
|
|
err := windows.GetConsoleMode(windows.Handle(fd), &st)
|
|
|
|
return err == nil
|
2017-04-17 18:08:24 -04:00
|
|
|
}
|
|
|
|
|
2020-11-23 15:51:54 -05:00
|
|
|
func makeRaw(fd int) (*State, error) {
|
2017-04-17 18:08:24 -04:00
|
|
|
var st uint32
|
2018-06-08 05:26:10 -04:00
|
|
|
if err := windows.GetConsoleMode(windows.Handle(fd), &st); err != nil {
|
|
|
|
return nil, err
|
2017-04-17 18:08:24 -04:00
|
|
|
}
|
2018-06-08 05:26:10 -04:00
|
|
|
raw := st &^ (windows.ENABLE_ECHO_INPUT | windows.ENABLE_PROCESSED_INPUT | windows.ENABLE_LINE_INPUT | windows.ENABLE_PROCESSED_OUTPUT)
|
2024-10-12 15:46:04 -04:00
|
|
|
raw |= windows.ENABLE_VIRTUAL_TERMINAL_INPUT
|
2018-06-08 05:26:10 -04:00
|
|
|
if err := windows.SetConsoleMode(windows.Handle(fd), raw); err != nil {
|
|
|
|
return nil, err
|
2017-04-17 18:08:24 -04:00
|
|
|
}
|
2020-11-23 15:51:54 -05:00
|
|
|
return &State{state{st}}, nil
|
2017-04-17 18:08:24 -04:00
|
|
|
}
|
|
|
|
|
2020-11-23 15:51:54 -05:00
|
|
|
func getState(fd int) (*State, error) {
|
2017-04-17 18:08:24 -04:00
|
|
|
var st uint32
|
2018-06-08 05:26:10 -04:00
|
|
|
if err := windows.GetConsoleMode(windows.Handle(fd), &st); err != nil {
|
|
|
|
return nil, err
|
2017-04-17 18:08:24 -04:00
|
|
|
}
|
2020-11-23 15:51:54 -05:00
|
|
|
return &State{state{st}}, nil
|
2017-04-17 18:08:24 -04:00
|
|
|
}
|
|
|
|
|
2020-11-23 15:51:54 -05:00
|
|
|
func restore(fd int, state *State) error {
|
2018-06-08 05:26:10 -04:00
|
|
|
return windows.SetConsoleMode(windows.Handle(fd), state.mode)
|
2017-04-17 18:08:24 -04:00
|
|
|
}
|
|
|
|
|
2020-11-23 15:51:54 -05:00
|
|
|
func getSize(fd int) (width, height int, err error) {
|
2018-06-08 05:26:10 -04:00
|
|
|
var info windows.ConsoleScreenBufferInfo
|
|
|
|
if err := windows.GetConsoleScreenBufferInfo(windows.Handle(fd), &info); err != nil {
|
|
|
|
return 0, 0, err
|
2017-04-17 18:08:24 -04:00
|
|
|
}
|
2019-04-03 15:06:12 -04:00
|
|
|
return int(info.Window.Right - info.Window.Left + 1), int(info.Window.Bottom - info.Window.Top + 1), nil
|
2017-09-29 09:29:30 -04:00
|
|
|
}
|
|
|
|
|
2020-11-23 15:51:54 -05:00
|
|
|
func readPassword(fd int) ([]byte, error) {
|
2017-04-17 18:08:24 -04:00
|
|
|
var st uint32
|
2018-06-08 05:26:10 -04:00
|
|
|
if err := windows.GetConsoleMode(windows.Handle(fd), &st); err != nil {
|
|
|
|
return nil, err
|
2017-04-17 18:08:24 -04:00
|
|
|
}
|
|
|
|
old := st
|
|
|
|
|
2020-01-29 05:25:09 -05:00
|
|
|
st &^= (windows.ENABLE_ECHO_INPUT | windows.ENABLE_LINE_INPUT)
|
|
|
|
st |= (windows.ENABLE_PROCESSED_OUTPUT | windows.ENABLE_PROCESSED_INPUT)
|
2018-06-08 05:26:10 -04:00
|
|
|
if err := windows.SetConsoleMode(windows.Handle(fd), st); err != nil {
|
|
|
|
return nil, err
|
2017-04-17 18:08:24 -04:00
|
|
|
}
|
|
|
|
|
2018-06-08 05:26:10 -04:00
|
|
|
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
|
|
|
|
}
|
2017-04-17 18:08:24 -04:00
|
|
|
|
2018-06-08 05:26:10 -04:00
|
|
|
f := os.NewFile(uintptr(h), "stdin")
|
|
|
|
defer f.Close()
|
|
|
|
return readPasswordLine(f)
|
2017-04-17 18:08:24 -04:00
|
|
|
}
|