mirror of
				https://gitea.com/Lydanne/buildx.git
				synced 2025-11-04 10:03:42 +08:00 
			
		
		
		
	vendor: update buildkit to v0.8
Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
This commit is contained in:
		
							
								
								
									
										989
									
								
								vendor/golang.org/x/crypto/ssh/terminal/terminal.go
									
									
									
										generated
									
									
										vendored
									
									
								
							
							
						
						
									
										989
									
								
								vendor/golang.org/x/crypto/ssh/terminal/terminal.go
									
									
									
										generated
									
									
										vendored
									
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							
							
								
								
									
										114
									
								
								vendor/golang.org/x/crypto/ssh/terminal/util.go
									
									
									
										generated
									
									
										vendored
									
									
								
							
							
						
						
									
										114
									
								
								vendor/golang.org/x/crypto/ssh/terminal/util.go
									
									
									
										generated
									
									
										vendored
									
									
								
							@@ -1,114 +0,0 @@
 | 
			
		||||
// 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 aix darwin dragonfly freebsd linux,!appengine netbsd openbsd
 | 
			
		||||
 | 
			
		||||
// 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 "golang.org/x/crypto/ssh/terminal"
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"golang.org/x/sys/unix"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// State contains the state of a terminal.
 | 
			
		||||
type State struct {
 | 
			
		||||
	termios unix.Termios
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// IsTerminal returns whether the given file descriptor is a terminal.
 | 
			
		||||
func IsTerminal(fd int) bool {
 | 
			
		||||
	_, err := unix.IoctlGetTermios(fd, ioctlReadTermios)
 | 
			
		||||
	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) {
 | 
			
		||||
	termios, err := unix.IoctlGetTermios(fd, ioctlReadTermios)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	oldState := State{termios: *termios}
 | 
			
		||||
 | 
			
		||||
	// This attempts to replicate the behaviour documented for cfmakeraw in
 | 
			
		||||
	// the termios(3) manpage.
 | 
			
		||||
	termios.Iflag &^= unix.IGNBRK | unix.BRKINT | unix.PARMRK | unix.ISTRIP | unix.INLCR | unix.IGNCR | unix.ICRNL | unix.IXON
 | 
			
		||||
	termios.Oflag &^= unix.OPOST
 | 
			
		||||
	termios.Lflag &^= unix.ECHO | unix.ECHONL | unix.ICANON | unix.ISIG | unix.IEXTEN
 | 
			
		||||
	termios.Cflag &^= unix.CSIZE | unix.PARENB
 | 
			
		||||
	termios.Cflag |= unix.CS8
 | 
			
		||||
	termios.Cc[unix.VMIN] = 1
 | 
			
		||||
	termios.Cc[unix.VTIME] = 0
 | 
			
		||||
	if err := unix.IoctlSetTermios(fd, ioctlWriteTermios, termios); err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return &oldState, 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) {
 | 
			
		||||
	termios, err := unix.IoctlGetTermios(fd, ioctlReadTermios)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return &State{termios: *termios}, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Restore restores the terminal connected to the given file descriptor to a
 | 
			
		||||
// previous state.
 | 
			
		||||
func Restore(fd int, state *State) error {
 | 
			
		||||
	return unix.IoctlSetTermios(fd, ioctlWriteTermios, &state.termios)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// GetSize returns the dimensions of the given terminal.
 | 
			
		||||
func GetSize(fd int) (width, height int, err error) {
 | 
			
		||||
	ws, err := unix.IoctlGetWinsize(fd, unix.TIOCGWINSZ)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return -1, -1, err
 | 
			
		||||
	}
 | 
			
		||||
	return int(ws.Col), int(ws.Row), nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// passwordReader is an io.Reader that reads from a specific file descriptor.
 | 
			
		||||
type passwordReader int
 | 
			
		||||
 | 
			
		||||
func (r passwordReader) Read(buf []byte) (int, error) {
 | 
			
		||||
	return unix.Read(int(r), buf)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 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) {
 | 
			
		||||
	termios, err := unix.IoctlGetTermios(fd, ioctlReadTermios)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	newState := *termios
 | 
			
		||||
	newState.Lflag &^= unix.ECHO
 | 
			
		||||
	newState.Lflag |= unix.ICANON | unix.ISIG
 | 
			
		||||
	newState.Iflag |= unix.ICRNL
 | 
			
		||||
	if err := unix.IoctlSetTermios(fd, ioctlWriteTermios, &newState); err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	defer unix.IoctlSetTermios(fd, ioctlWriteTermios, termios)
 | 
			
		||||
 | 
			
		||||
	return readPasswordLine(passwordReader(fd))
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										12
									
								
								vendor/golang.org/x/crypto/ssh/terminal/util_aix.go
									
									
									
										generated
									
									
										vendored
									
									
								
							
							
						
						
									
										12
									
								
								vendor/golang.org/x/crypto/ssh/terminal/util_aix.go
									
									
									
										generated
									
									
										vendored
									
									
								
							@@ -1,12 +0,0 @@
 | 
			
		||||
// Copyright 2018 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 aix
 | 
			
		||||
 | 
			
		||||
package terminal
 | 
			
		||||
 | 
			
		||||
import "golang.org/x/sys/unix"
 | 
			
		||||
 | 
			
		||||
const ioctlReadTermios = unix.TCGETS
 | 
			
		||||
const ioctlWriteTermios = unix.TCSETS
 | 
			
		||||
							
								
								
									
										12
									
								
								vendor/golang.org/x/crypto/ssh/terminal/util_bsd.go
									
									
									
										generated
									
									
										vendored
									
									
								
							
							
						
						
									
										12
									
								
								vendor/golang.org/x/crypto/ssh/terminal/util_bsd.go
									
									
									
										generated
									
									
										vendored
									
									
								
							@@ -1,12 +0,0 @@
 | 
			
		||||
// Copyright 2013 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 darwin dragonfly freebsd netbsd openbsd
 | 
			
		||||
 | 
			
		||||
package terminal
 | 
			
		||||
 | 
			
		||||
import "golang.org/x/sys/unix"
 | 
			
		||||
 | 
			
		||||
const ioctlReadTermios = unix.TIOCGETA
 | 
			
		||||
const ioctlWriteTermios = unix.TIOCSETA
 | 
			
		||||
							
								
								
									
										10
									
								
								vendor/golang.org/x/crypto/ssh/terminal/util_linux.go
									
									
									
										generated
									
									
										vendored
									
									
								
							
							
						
						
									
										10
									
								
								vendor/golang.org/x/crypto/ssh/terminal/util_linux.go
									
									
									
										generated
									
									
										vendored
									
									
								
							@@ -1,10 +0,0 @@
 | 
			
		||||
// Copyright 2013 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.
 | 
			
		||||
 | 
			
		||||
package terminal
 | 
			
		||||
 | 
			
		||||
import "golang.org/x/sys/unix"
 | 
			
		||||
 | 
			
		||||
const ioctlReadTermios = unix.TCGETS
 | 
			
		||||
const ioctlWriteTermios = unix.TCSETS
 | 
			
		||||
							
								
								
									
										58
									
								
								vendor/golang.org/x/crypto/ssh/terminal/util_plan9.go
									
									
									
										generated
									
									
										vendored
									
									
								
							
							
						
						
									
										58
									
								
								vendor/golang.org/x/crypto/ssh/terminal/util_plan9.go
									
									
									
										generated
									
									
										vendored
									
									
								
							@@ -1,58 +0,0 @@
 | 
			
		||||
// Copyright 2016 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.
 | 
			
		||||
 | 
			
		||||
// 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 (
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"runtime"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type State struct{}
 | 
			
		||||
 | 
			
		||||
// IsTerminal returns whether the given file descriptor is a terminal.
 | 
			
		||||
func IsTerminal(fd int) bool {
 | 
			
		||||
	return false
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 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) {
 | 
			
		||||
	return nil, fmt.Errorf("terminal: MakeRaw not implemented on %s/%s", runtime.GOOS, runtime.GOARCH)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 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) {
 | 
			
		||||
	return nil, fmt.Errorf("terminal: GetState not implemented on %s/%s", runtime.GOOS, runtime.GOARCH)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Restore restores the terminal connected to the given file descriptor to a
 | 
			
		||||
// previous state.
 | 
			
		||||
func Restore(fd int, state *State) error {
 | 
			
		||||
	return fmt.Errorf("terminal: Restore not implemented on %s/%s", runtime.GOOS, runtime.GOARCH)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// GetSize returns the dimensions of the given terminal.
 | 
			
		||||
func GetSize(fd int) (width, height int, err error) {
 | 
			
		||||
	return 0, 0, fmt.Errorf("terminal: GetSize not implemented on %s/%s", runtime.GOOS, runtime.GOARCH)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 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) {
 | 
			
		||||
	return nil, fmt.Errorf("terminal: ReadPassword not implemented on %s/%s", runtime.GOOS, runtime.GOARCH)
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										124
									
								
								vendor/golang.org/x/crypto/ssh/terminal/util_solaris.go
									
									
									
										generated
									
									
										vendored
									
									
								
							
							
						
						
									
										124
									
								
								vendor/golang.org/x/crypto/ssh/terminal/util_solaris.go
									
									
									
										generated
									
									
										vendored
									
									
								
							@@ -1,124 +0,0 @@
 | 
			
		||||
// Copyright 2015 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 solaris
 | 
			
		||||
 | 
			
		||||
package terminal // import "golang.org/x/crypto/ssh/terminal"
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"golang.org/x/sys/unix"
 | 
			
		||||
	"io"
 | 
			
		||||
	"syscall"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// State contains the state of a terminal.
 | 
			
		||||
type State struct {
 | 
			
		||||
	termios unix.Termios
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// IsTerminal returns whether the given file descriptor is a terminal.
 | 
			
		||||
func IsTerminal(fd int) bool {
 | 
			
		||||
	_, err := unix.IoctlGetTermio(fd, unix.TCGETA)
 | 
			
		||||
	return err == 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) {
 | 
			
		||||
	// see also: http://src.illumos.org/source/xref/illumos-gate/usr/src/lib/libast/common/uwin/getpass.c
 | 
			
		||||
	val, err := unix.IoctlGetTermios(fd, unix.TCGETS)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
	oldState := *val
 | 
			
		||||
 | 
			
		||||
	newState := oldState
 | 
			
		||||
	newState.Lflag &^= syscall.ECHO
 | 
			
		||||
	newState.Lflag |= syscall.ICANON | syscall.ISIG
 | 
			
		||||
	newState.Iflag |= syscall.ICRNL
 | 
			
		||||
	err = unix.IoctlSetTermios(fd, unix.TCSETS, &newState)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	defer unix.IoctlSetTermios(fd, unix.TCSETS, &oldState)
 | 
			
		||||
 | 
			
		||||
	var buf [16]byte
 | 
			
		||||
	var ret []byte
 | 
			
		||||
	for {
 | 
			
		||||
		n, err := syscall.Read(fd, buf[:])
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return nil, err
 | 
			
		||||
		}
 | 
			
		||||
		if n == 0 {
 | 
			
		||||
			if len(ret) == 0 {
 | 
			
		||||
				return nil, io.EOF
 | 
			
		||||
			}
 | 
			
		||||
			break
 | 
			
		||||
		}
 | 
			
		||||
		if buf[n-1] == '\n' {
 | 
			
		||||
			n--
 | 
			
		||||
		}
 | 
			
		||||
		ret = append(ret, buf[:n]...)
 | 
			
		||||
		if n < len(buf) {
 | 
			
		||||
			break
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return ret, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// MakeRaw puts 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.
 | 
			
		||||
// see http://cr.illumos.org/~webrev/andy_js/1060/
 | 
			
		||||
func MakeRaw(fd int) (*State, error) {
 | 
			
		||||
	termios, err := unix.IoctlGetTermios(fd, unix.TCGETS)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	oldState := State{termios: *termios}
 | 
			
		||||
 | 
			
		||||
	termios.Iflag &^= unix.IGNBRK | unix.BRKINT | unix.PARMRK | unix.ISTRIP | unix.INLCR | unix.IGNCR | unix.ICRNL | unix.IXON
 | 
			
		||||
	termios.Oflag &^= unix.OPOST
 | 
			
		||||
	termios.Lflag &^= unix.ECHO | unix.ECHONL | unix.ICANON | unix.ISIG | unix.IEXTEN
 | 
			
		||||
	termios.Cflag &^= unix.CSIZE | unix.PARENB
 | 
			
		||||
	termios.Cflag |= unix.CS8
 | 
			
		||||
	termios.Cc[unix.VMIN] = 1
 | 
			
		||||
	termios.Cc[unix.VTIME] = 0
 | 
			
		||||
 | 
			
		||||
	if err := unix.IoctlSetTermios(fd, unix.TCSETS, termios); err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return &oldState, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Restore restores the terminal connected to the given file descriptor to a
 | 
			
		||||
// previous state.
 | 
			
		||||
func Restore(fd int, oldState *State) error {
 | 
			
		||||
	return unix.IoctlSetTermios(fd, unix.TCSETS, &oldState.termios)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 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) {
 | 
			
		||||
	termios, err := unix.IoctlGetTermios(fd, unix.TCGETS)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return &State{termios: *termios}, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// GetSize returns the dimensions of the given terminal.
 | 
			
		||||
func GetSize(fd int) (width, height int, err error) {
 | 
			
		||||
	ws, err := unix.IoctlGetWinsize(fd, unix.TIOCGWINSZ)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return 0, 0, err
 | 
			
		||||
	}
 | 
			
		||||
	return int(ws.Col), int(ws.Row), nil
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										105
									
								
								vendor/golang.org/x/crypto/ssh/terminal/util_windows.go
									
									
									
										generated
									
									
										vendored
									
									
								
							
							
						
						
									
										105
									
								
								vendor/golang.org/x/crypto/ssh/terminal/util_windows.go
									
									
									
										generated
									
									
										vendored
									
									
								
							@@ -1,105 +0,0 @@
 | 
			
		||||
// 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 visible dimensions of the given terminal.
 | 
			
		||||
//
 | 
			
		||||
// These dimensions don't include any scrollback buffer height.
 | 
			
		||||
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.Window.Right - info.Window.Left + 1), int(info.Window.Bottom - info.Window.Top + 1), 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 | windows.ENABLE_LINE_INPUT)
 | 
			
		||||
	st |= (windows.ENABLE_PROCESSED_OUTPUT | windows.ENABLE_PROCESSED_INPUT)
 | 
			
		||||
	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)
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user