-
Notifications
You must be signed in to change notification settings - Fork 0
/
tty.go
238 lines (219 loc) · 5.2 KB
/
tty.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package main
import (
"bytes"
"flag"
"fmt"
"net"
"os"
"os/exec"
"strings"
"time"
"github.com/abakum/menu"
"github.com/f1bonacc1/glippy"
"gopkg.in/ini.v1"
)
const (
KITTYINI = "kitty.ini"
)
// start hub4com then show menu for baud and delay
func tty(hphp ...string) {
if So == "" {
return
}
li.Println("COM"+So, "com0com", Cncb)
opts := []string{"--baud=75",
"--create-filter=escparse,com,parse",
"--create-filter=pinmap,com,pinmap:--rts=cts --dtr=dsr",
"--create-filter=linectl,com,lc:--br=local --lc=local",
"--add-filters=0:com",
"--create-filter=telnet,tcp,telnet:--comport=client",
"--create-filter=pinmap,tcp,pinmap:--rts=cts --dtr=dsr --break=break",
"--create-filter=linectl,tcp,lc:--br=remote --lc=remote",
}
if Crypt != "" {
opts = append(opts, Crypt)
}
Hub = exec.Command(Fns[HUB4COM], append(opts,
"--add-filters=1:tcp",
// "--use-driver=serial",
"--octs=off",
"--ito="+ITO,
"--ox="+XO,
"--ix="+XO,
"--write-limit="+LIMIT,
Cncb,
"--use-driver=tcp",
net.JoinHostPort(hphp[0], hphp[1]),
)...)
var bBuffer bytes.Buffer
Hub.Stdout = &bBuffer
Hub.Stderr = &bBuffer
Println(cmd("Start", Hub), Hub.Start())
defer kill()
for i := 0; i < 24; i++ {
s, er := bBuffer.ReadString('\n')
if er == nil {
FatalOr(s, strings.Contains(s, "ERROR"))
fmt.Print(s)
if s == "TCP(1): Connected\n" {
break
}
}
time.Sleep(time.Millisecond * 50)
}
// fmt.Print(bBuffer.String())
bBuffer.WriteTo(os.Stdout)
Hub.Stdout = os.Stdout
Hub.Stderr = os.Stderr
items := []menu.MenuFunc{func(index int, pressed rune) string {
if index == -1 {
return menu.SELECT
}
if Delay != DELAY {
return "Choose baud and seconds delay for Ctrl-F2 or commands from clipboard\n" +
"Выбери скорость и задержку в секундах для Ctrl-F2 или команд из буфера"
}
return "Choose baud and seconds delay for Ctrl-F2\n" +
"Выбери скорость и задержку в секундах для Ctrl-F2"
}}
items = append(items, func(index int, pressed rune) string {
return setDelay(index, pressed, DELAY)
})
items = append(items, func(index int, pressed rune) string {
return kiRun(index, pressed, "115200")
})
items = append(items, func(index int, pressed rune) string {
return setDelay(index, pressed, "0.2")
})
items = append(items, func(index int, pressed rune) string {
return kiRun(index, pressed, "38400")
})
items = append(items, func(index int, pressed rune) string {
return setDelay(index, pressed, "0.4")
})
items = append(items, func(index int, pressed rune) string {
return kiRun(index, pressed, "57600")
})
items = append(items, func(index int, pressed rune) string {
return setDelay(index, pressed, "0.6")
})
items = append(items, func(index int, pressed rune) string {
return setDelay(index, pressed, "0.7")
})
items = append(items, func(index int, pressed rune) string {
return setDelay(index, pressed, "0.08")
})
items = append(items, func(index int, pressed rune) string {
return kiRun(index, pressed, B9600)
})
menu.Menu(0, actual(flag.CommandLine, "b"), true, items...)
}
func setDelay(index int, pressed rune, d string) string {
r := rune('0' + index)
switch pressed {
case menu.MARKED:
if Delay == d {
return menu.MARK
}
case menu.ITEM:
return fmt.Sprintf("%c) %s", r, d)
case r:
Delay = d
return string(r)
}
return ""
}
func kiRun(index int, pressed rune, b string) string {
r := rune('0' + index)
switch pressed {
case menu.MARKED:
if Baud == b {
return menu.MARK
}
case menu.ITEM:
return fmt.Sprintf(`%c) %s`, r, b)
case r:
Baud = b
opts := []string{
"-sercfg",
Baud,
"-serial",
"COM" + So,
}
Println("cmdFromClipBoard", command(&opts))
ki := exec.Command(Fns[KITTY], opts...)
ltf.Println(cmd("Run", ki))
Println(cmd("Close", ki), ki.Run())
return string(r)
}
return ""
}
func kill() {
if Hub.Process != nil {
Println(cmd("Kill", Hub), Hub.Process.Kill())
}
}
func SetValue(section *ini.Section, key, val string) (set bool) {
set = section.Key(key).String() != val
if set {
ltf.Println(key, val)
section.Key(key).SetValue(val)
}
return
}
func command(opts *[]string) error {
ini.PrettyFormat = false
iniFile, err := ini.LoadSources(ini.LoadOptions{
IgnoreInlineComment: false,
}, Fns[KITTYINI])
if err != nil {
return err
}
section := iniFile.Section("KiTTY")
ok := SetValue(section, "commanddelay", Delay)
if ok {
err = iniFile.SaveTo(Fns[KITTYINI])
if err != nil {
return err
}
}
if Delay == DELAY {
return nil
}
text, err := glippy.Get()
if err != nil {
return err
}
if text == "" {
return fmt.Errorf("empty ClipBoard")
}
temp, err := os.CreateTemp("", "cmdFromClipBoard")
if err != nil {
return err
}
clip := temp.Name()
defer os.Remove(clip)
n, err := temp.WriteString(text)
if err != nil {
return err
}
if n != len(text) {
return fmt.Errorf("error write ClipBoard to %s", clip)
}
*opts = append(*opts,
"-cmd",
clip,
)
return nil
}
func ttyMenu(index int, pressed rune, hphp ...string) string {
r := rune('1' + index)
switch pressed {
case r:
tty(hphp...)
return string(r)
case menu.ITEM:
return fmt.Sprintf("%c) %s", r, strings.Join(hphp, ":"))
}
return ""
}