-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtty_windows.go
79 lines (67 loc) · 1.61 KB
/
tty_windows.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package main
import (
"os"
"syscall"
"unsafe"
)
var (
kernel32 = syscall.MustLoadDLL("kernel32.dll")
procSetStdHandle = kernel32.MustFindProc("SetStdHandle")
)
func getStdHandle(h int) (fd syscall.Handle) {
r, _ := syscall.GetStdHandle(h)
syscall.CloseOnExec(r)
return r
}
func setStdHandle(stdhandle int32, handle syscall.Handle) error {
r0, _, e1 := syscall.Syscall(procSetStdHandle.Addr(), 2, uintptr(stdhandle), uintptr(handle), 0)
if r0 == 0 {
if e1 != 0 {
return error(e1)
}
return syscall.EINVAL
}
return nil
}
func isTerminal() bool {
f := syscall.MustLoadDLL("kernel32.dll").MustFindProc("GetConsoleMode")
var st uint32
r1, _, err := f.Call(uintptr(os.Stdin.Fd()), uintptr(unsafe.Pointer(&st)))
return r1 != 0 && err != nil
}
var stdout = os.Stdout
var stdin = os.Stdin
func startTerminal() error {
var err error
_stdin, err := os.Open("CONIN$")
if err != nil {
return err
}
_stdout, err := os.Open("CONOUT$")
if err != nil {
return err
}
stdin = os.Stdin
stdout = os.Stdout
os.Stdin = _stdin
os.Stdout = _stdout
syscall.Stdin = syscall.Handle(os.Stdin.Fd())
err = setStdHandle(syscall.STD_INPUT_HANDLE, syscall.Stdin)
if err != nil {
return err
}
syscall.Stdout = syscall.Handle(os.Stdout.Fd())
err = setStdHandle(syscall.STD_OUTPUT_HANDLE, syscall.Stdout)
if err != nil {
return err
}
return nil
}
func stopTerminal() {
os.Stdin = stdin
syscall.Stdin = syscall.Handle(os.Stdin.Fd())
setStdHandle(syscall.STD_INPUT_HANDLE, syscall.Stdin)
os.Stdout = stdout
syscall.Stdout = syscall.Handle(os.Stdout.Fd())
setStdHandle(syscall.STD_OUTPUT_HANDLE, syscall.Stdout)
}