-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathterm_open_posix.go
46 lines (38 loc) · 984 Bytes
/
term_open_posix.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
// +build !windows,!solaris
package term
import (
"os"
"github.com/pkg/term/termios"
"golang.org/x/sys/unix"
)
// Open opens an asynchronous communications port.
func Open(name string, options ...func(*Term) error) (*Term, error) {
fd, e := unix.Open(name, unix.O_NOCTTY|unix.O_CLOEXEC|unix.O_NDELAY|unix.O_RDWR, 0666)
if e != nil {
return nil, &os.PathError{
Op: "open",
Path: name,
Err: e,
}
}
orig, err := termios.Tcgetattr(uintptr(fd))
if err != nil {
unix.Close(fd)
return nil, err
}
t := Term{name: name, fd: fd, orig: *orig}
if err := t.SetOption(options...); err != nil {
unix.Close(fd)
return nil, err
}
if err := unix.SetNonblock(t.fd, false); err != nil {
unix.Close(fd)
return nil, err
}
return &t, nil
}
// Restore restores the state of the terminal captured at the point that
// the terminal was originally opened.
func (t *Term) Restore() error {
return termios.Tcsetattr(uintptr(t.fd), termios.TCIOFLUSH, &t.orig)
}