Debian Codegraph @

vt_other.go

raw
1 //go:build plan9 || nacl || windows
2 // +build plan9 nacl windows
3
4 package terminal
5
6 import (
7 "bufio"
8 "bytes"
9 "errors"
10 "io"
11 "os"
12 "os/exec"
13 "unicode"
14 "unicode/utf8"
15 )
16
17 // VT represents the virtual terminal emulator.
18 type VT struct {
19 dest *State
20 rc io.ReadCloser
21 br *bufio.Reader
22 pty *os.File
23 }
24
25 // Start initializes a virtual terminal emulator with the target state
26 // and a new pty file by starting the *exec.Command. The returned
27 // *os.File is the pty file.
28 func Start(state *State, cmd *exec.Cmd) (*VT, *os.File, error) {
29 return nil, nil, errors.New("Unsupported operating system")
30 }
31
32 // Create initializes a virtual terminal emulator with the target state
33 // and io.ReadCloser input.
34 func Create(state *State, rc io.ReadCloser) (*VT, error) {
35 t := &VT{
36 dest: state,
37 rc: rc,
38 }
39 t.init()
40 return t, nil
41 }
42
43 func (t *VT) init() {
44 t.br = bufio.NewReader(t.rc)
45 t.dest.numlock = true
46 t.dest.state = t.dest.parse
47 t.dest.cur.attr.fg = DefaultFG
48 t.dest.cur.attr.bg = DefaultBG
49 t.Resize(80, 24)
50 t.dest.reset()
51 }
52
53 // File returns the pty file.
54 func (t *VT) File() *os.File {
55 return t.pty
56 }
57
58 // Write parses input and writes terminal changes to state.
59 func (t *VT) Write(p []byte) (int, error) {
60 var written int
61 r := bytes.NewReader(p)
62 t.dest.lock()
63 defer t.dest.unlock()
64 for {
65 c, sz, err := r.ReadRune()
66 if err != nil {
67 if err == io.EOF {
68 break
69 }
70 return written, err
71 }
72 written += sz
73 if c == unicode.ReplacementChar && sz == 1 {
74 if r.Len() == 0 {
75 // not enough bytes for a full rune
76 return written - 1, nil
77 }
78 t.dest.logln("invalid utf8 sequence")
79 continue
80 }
81 t.dest.put(c)
82 }
83 return written, nil
84 }
85
86 // Close closes the pty or io.ReadCloser.
87 func (t *VT) Close() error {
88 return t.rc.Close()
89 }
90
91 // Parse blocks on read on pty or io.ReadCloser, then parses sequences until
92 // buffer empties. State is locked as soon as first rune is read, and unlocked
93 // when buffer is empty.
94 // TODO: add tests for expected blocking behavior
95 func (t *VT) Parse() error {
96 var locked bool
97 defer func() {
98 if locked {
99 t.dest.unlock()
100 }
101 }()
102 for {
103 c, sz, err := t.br.ReadRune()
104 if err != nil {
105 return err
106 }
107 if c == unicode.ReplacementChar && sz == 1 {
108 t.dest.logln("invalid utf8 sequence")
109 break
110 }
111 if !locked {
112 t.dest.lock()
113 locked = true
114 }
115
116 // put rune for parsing and update state
117 t.dest.put(c)
118
119 // break if our buffer is empty, or if buffer contains an
120 // incomplete rune.
121 n := t.br.Buffered()
122 if n == 0 || (n < 4 && !fullRuneBuffered(t.br)) {
123 break
124 }
125 }
126 return nil
127 }
128
129 func fullRuneBuffered(br *bufio.Reader) bool {
130 n := br.Buffered()
131 buf, err := br.Peek(n)
132 if err != nil {
133 return false
134 }
135 return utf8.FullRune(buf)
136 }
137
138 // Resize reports new size to pty and updates state.
139 func (t *VT) Resize(cols, rows int) {
140 t.dest.lock()
141 defer t.dest.unlock()
142 _ = t.dest.resize(cols, rows)
143 t.ptyResize()
144 }