瀏覽代碼

ssh/terminal: add GetState and make ReadPassword work in raw mode.

GetState is useful for restoring the terminal in a signal handler.

R=golang-dev, rsc
CC=golang-dev
https://golang.org/cl/6990043
Adam Langley 13 年之前
父節點
當前提交
03be8f3bf0
共有 1 個文件被更改,包括 13 次插入0 次删除
  1. 13 0
      ssh/terminal/util.go

+ 13 - 0
ssh/terminal/util.go

@@ -53,6 +53,17 @@ func MakeRaw(fd int) (*State, error) {
 	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) {
+	var oldState State
+	if _, _, err := syscall.Syscall6(syscall.SYS_IOCTL, uintptr(fd), uintptr(syscall.TCGETS), uintptr(unsafe.Pointer(&oldState.termios)), 0, 0, 0); err != 0 {
+		return nil, err
+	}
+
+	return &oldState, nil
+}
+
 // Restore restores the terminal connected to the given file descriptor to a
 // previous state.
 func Restore(fd int, state *State) error {
@@ -81,6 +92,8 @@ func ReadPassword(fd int) ([]byte, error) {
 
 	newState := oldState
 	newState.Lflag &^= syscall.ECHO
+	newState.Lflag |= syscall.ICANON | syscall.ISIG
+	newState.Iflag |= syscall.ICRNL
 	if _, _, err := syscall.Syscall6(syscall.SYS_IOCTL, uintptr(fd), uintptr(syscall.TCSETS), uintptr(unsafe.Pointer(&newState)), 0, 0, 0); err != 0 {
 		return nil, err
 	}