forked from u-root/u-root
-
Notifications
You must be signed in to change notification settings - Fork 2
/
termios.go
67 lines (53 loc) · 1.59 KB
/
termios.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
// +build !windows,!plan9
// Copyright 2015 go-termios Author. All Rights Reserved.
// https://github.com/go-termios/termios
// Author: John Lenton <chipaca@github.com>
package sys
import (
"unsafe"
"golang.org/x/sys/unix"
)
// Termios represents terminal attributes.
type Termios unix.Termios
// NewTermiosFromFd extracts the terminal attribute of the given file
// descriptor.
func NewTermiosFromFd(fd int) (*Termios, error) {
var term Termios
if err := Ioctl(fd, getAttrIOCTL, uintptr(unsafe.Pointer(&term))); err != nil {
return nil, err
}
return &term, nil
}
// ApplyToFd applies term to the given file descriptor.
func (term *Termios) ApplyToFd(fd int) error {
return Ioctl(fd, setAttrNowIOCTL, uintptr(unsafe.Pointer(term)))
}
// Copy returns a copy of term.
func (term *Termios) Copy() *Termios {
v := *term
return &v
}
// SetVTime sets the timeout in deciseconds for noncanonical read.
func (term *Termios) SetVTime(v uint8) {
term.Cc[unix.VTIME] = v
}
// SetVMin sets the minimal number of characters for noncanonical read.
func (term *Termios) SetVMin(v uint8) {
term.Cc[unix.VMIN] = v
}
// SetICanon sets the canonical flag.
func (term *Termios) SetICanon(v bool) {
setFlag(&term.Lflag, unix.ICANON, v)
}
// SetEcho sets the echo flag.
func (term *Termios) SetEcho(v bool) {
setFlag(&term.Lflag, unix.ECHO, v)
}
// SetICRNL sets the CRNL iflag bit
func (term *Termios) SetICRNL(v bool) {
setFlag(&term.Iflag, unix.ICRNL, v)
}
// FlushInput discards data written to a file descriptor but not read.
func FlushInput(fd int) error {
return Ioctl(fd, flushIOCTL, uintptr(unix.TCIFLUSH))
}