Browse code

[pkg/term] use IoctlGetTermios/IoctlSetTermios from x/sys/unix

Use IoctlGetTermios/IoctlSetTermios from golang.org/x/sys/unix instead
of manually reimplementing them.

Signed-off-by: Tobias Klauser <tklauser@distanz.ch>

Tobias Klauser authored on 2017/07/17 17:36:52
Showing 1 changed files
... ...
@@ -1,8 +1,6 @@
1 1
 package term
2 2
 
3 3
 import (
4
-	"unsafe"
5
-
6 4
 	"golang.org/x/sys/unix"
7 5
 )
8 6
 
... ...
@@ -18,20 +16,21 @@ type Termios unix.Termios
18 18
 // mode and returns the previous state of the terminal so that it can be
19 19
 // restored.
20 20
 func MakeRaw(fd uintptr) (*State, error) {
21
-	var oldState State
22
-	if _, _, err := unix.Syscall(unix.SYS_IOCTL, fd, getTermios, uintptr(unsafe.Pointer(&oldState.termios))); err != 0 {
21
+	termios, err := unix.IoctlGetTermios(int(fd), getTermios)
22
+	if err != nil {
23 23
 		return nil, err
24 24
 	}
25 25
 
26
-	newState := oldState.termios
26
+	var oldState State
27
+	oldState.termios = Termios(*termios)
27 28
 
28
-	newState.Iflag &^= (unix.IGNBRK | unix.BRKINT | unix.PARMRK | unix.ISTRIP | unix.INLCR | unix.IGNCR | unix.ICRNL | unix.IXON)
29
-	newState.Oflag &^= unix.OPOST
30
-	newState.Lflag &^= (unix.ECHO | unix.ECHONL | unix.ICANON | unix.ISIG | unix.IEXTEN)
31
-	newState.Cflag &^= (unix.CSIZE | unix.PARENB)
32
-	newState.Cflag |= unix.CS8
29
+	termios.Iflag &^= (unix.IGNBRK | unix.BRKINT | unix.PARMRK | unix.ISTRIP | unix.INLCR | unix.IGNCR | unix.ICRNL | unix.IXON)
30
+	termios.Oflag &^= unix.OPOST
31
+	termios.Lflag &^= (unix.ECHO | unix.ECHONL | unix.ICANON | unix.ISIG | unix.IEXTEN)
32
+	termios.Cflag &^= (unix.CSIZE | unix.PARENB)
33
+	termios.Cflag |= unix.CS8
33 34
 
34
-	if _, _, err := unix.Syscall(unix.SYS_IOCTL, fd, setTermios, uintptr(unsafe.Pointer(&newState))); err != 0 {
35
+	if err := unix.IoctlSetTermios(int(fd), setTermios, termios); err != nil {
35 36
 		return nil, err
36 37
 	}
37 38
 	return &oldState, nil