/
termios.go
77 lines (63 loc) · 1.76 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
68
69
70
71
72
73
74
75
76
77
// Copyright 2015-2017 the u-root Authors. All rights reserved
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package termios implements basic termios operations including getting
// a tty struct, termio struct, a winsize struct, and setting raw mode.
// To get a TTY, call termios.New.
// To get a Termios, call tty.Get(); to set it, call tty.Set(*Termios)
// To set raw mode and then restore, one can do:
// tty := termios.NewTTY()
// restorer, err := tty.Raw()
// do things
// tty.Set(restorer)
package termios
type (
// TTY is an os-independent version of the combined info in termios and window size structs.
// It is used to get/set info to the termios functions as well as marshal/unmarshal data
// in JSON format for dump and loading.
TTY struct {
Ispeed int
Ospeed int
Row int
Col int
CC map[string]uint8
Opts map[string]bool
}
)
// Raw sets the tty into raw mode.
func (t *TTYIO) Raw() (*Termios, error) {
restorer, err := t.Get()
if err != nil {
return nil, err
}
raw := MakeRaw(restorer)
if err := t.Set(raw); err != nil {
return nil, err
}
return restorer, nil
}
// Serial configure the serial TTY at given baudrate with ECHO and character conversion (CRNL, ERASE, KILL)
func (t *TTYIO) Serial(baud int) (*Termios, error) {
restorer, err := t.Get()
if err != nil {
return nil, err
}
serial := MakeSerialDefault(restorer)
if baud != 0 {
if err := t.Set(serial); err != nil {
return nil, err
}
serial, err = MakeSerialBaud(serial, baud)
if err != nil {
return restorer, err
}
}
err = t.Set(serial)
return restorer, err
}
func (t *TTYIO) Read(b []byte) (int, error) {
return t.f.Read(b)
}
func (t *TTYIO) Write(b []byte) (int, error) {
return t.f.Write(b)
}