Browse code

Use termios via CGO only on Linux

Signed-off-by: Yohei Ueda <yohei@jp.ibm.com>

Yohei Ueda authored on 2014/11/25 20:49:01
Showing 6 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,47 @@
0
+// +build linux,cgo
1
+
2
+package term
3
+
4
+import (
5
+	"syscall"
6
+	"unsafe"
7
+)
8
+
9
+// #include <termios.h>
10
+import "C"
11
+
12
+type Termios syscall.Termios
13
+
14
+// MakeRaw put the terminal connected to the given file descriptor into raw
15
+// mode and returns the previous state of the terminal so that it can be
16
+// restored.
17
+func MakeRaw(fd uintptr) (*State, error) {
18
+	var oldState State
19
+	if err := tcget(fd, &oldState.termios); err != 0 {
20
+		return nil, err
21
+	}
22
+
23
+	newState := oldState.termios
24
+
25
+	C.cfmakeraw((*C.struct_termios)(unsafe.Pointer(&newState)))
26
+	if err := tcset(fd, &newState); err != 0 {
27
+		return nil, err
28
+	}
29
+	return &oldState, nil
30
+}
31
+
32
+func tcget(fd uintptr, p *Termios) syscall.Errno {
33
+	ret, err := C.tcgetattr(C.int(fd), (*C.struct_termios)(unsafe.Pointer(p)))
34
+	if ret != 0 {
35
+		return err.(syscall.Errno)
36
+	}
37
+	return 0
38
+}
39
+
40
+func tcset(fd uintptr, p *Termios) syscall.Errno {
41
+	ret, err := C.tcsetattr(C.int(fd), C.TCSANOW, (*C.struct_termios)(unsafe.Pointer(p)))
42
+	if ret != 0 {
43
+		return err.(syscall.Errno)
44
+	}
45
+	return 0
46
+}
0 47
new file mode 100644
... ...
@@ -0,0 +1,19 @@
0
+// +build !windows
1
+// +build !linux !cgo
2
+
3
+package term
4
+
5
+import (
6
+	"syscall"
7
+	"unsafe"
8
+)
9
+
10
+func tcget(fd uintptr, p *Termios) syscall.Errno {
11
+	_, _, err := syscall.Syscall(syscall.SYS_IOCTL, fd, uintptr(getTermios), uintptr(unsafe.Pointer(p)))
12
+	return err
13
+}
14
+
15
+func tcset(fd uintptr, p *Termios) syscall.Errno {
16
+	_, _, err := syscall.Syscall(syscall.SYS_IOCTL, fd, setTermios, uintptr(unsafe.Pointer(p)))
17
+	return err
18
+}
0 19
deleted file mode 100644
... ...
@@ -1,47 +0,0 @@
1
-// +build !windows,cgo
2
-
3
-package term
4
-
5
-import (
6
-	"syscall"
7
-	"unsafe"
8
-)
9
-
10
-// #include <termios.h>
11
-import "C"
12
-
13
-type Termios syscall.Termios
14
-
15
-// MakeRaw put the terminal connected to the given file descriptor into raw
16
-// mode and returns the previous state of the terminal so that it can be
17
-// restored.
18
-func MakeRaw(fd uintptr) (*State, error) {
19
-	var oldState State
20
-	if err := tcget(fd, &oldState.termios); err != 0 {
21
-		return nil, err
22
-	}
23
-
24
-	newState := oldState.termios
25
-
26
-	C.cfmakeraw((*C.struct_termios)(unsafe.Pointer(&newState)))
27
-	if err := tcset(fd, &newState); err != 0 {
28
-		return nil, err
29
-	}
30
-	return &oldState, nil
31
-}
32
-
33
-func tcget(fd uintptr, p *Termios) syscall.Errno {
34
-	ret, err := C.tcgetattr(C.int(fd), (*C.struct_termios)(unsafe.Pointer(p)))
35
-	if ret != 0 {
36
-		return err.(syscall.Errno)
37
-	}
38
-	return 0
39
-}
40
-
41
-func tcset(fd uintptr, p *Termios) syscall.Errno {
42
-	ret, err := C.tcsetattr(C.int(fd), C.TCSANOW, (*C.struct_termios)(unsafe.Pointer(p)))
43
-	if ret != 0 {
44
-		return err.(syscall.Errno)
45
-	}
46
-	return 0
47
-}
48 1
deleted file mode 100644
... ...
@@ -1,18 +0,0 @@
1
-// +build !windows,!cgo
2
-
3
-package term
4
-
5
-import (
6
-	"syscall"
7
-	"unsafe"
8
-)
9
-
10
-func tcget(fd uintptr, p *Termios) syscall.Errno {
11
-	_, _, err := syscall.Syscall(syscall.SYS_IOCTL, fd, uintptr(getTermios), uintptr(unsafe.Pointer(p)))
12
-	return err
13
-}
14
-
15
-func tcset(fd uintptr, p *Termios) syscall.Errno {
16
-	_, _, err := syscall.Syscall(syscall.SYS_IOCTL, fd, setTermios, uintptr(unsafe.Pointer(p)))
17
-	return err
18
-}
... ...
@@ -1,5 +1,3 @@
1
-// +build !cgo
2
-
3 1
 package term
4 2
 
5 3
 import (
... ...
@@ -1,5 +1,3 @@
1
-// +build !cgo
2
-
3 1
 package term
4 2
 
5 3
 import (