-
Notifications
You must be signed in to change notification settings - Fork 11
/
console_windows.go
268 lines (212 loc) · 5.02 KB
/
console_windows.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package console
import (
"embed"
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"
"syscall"
"github.com/iamacarpet/go-winpty"
"github.com/runletapp/go-console/interfaces"
)
// Do the interface allocations only once for common
// Errno values.
const (
errnoERROR_IO_PENDING = 997
)
var (
errERROR_IO_PENDING error = syscall.Errno(errnoERROR_IO_PENDING)
)
//go:embed winpty/*
var winpty_deps embed.FS
// errnoErr returns common boxed Errno values, to prevent
// allocations at runtime.
func errnoErr(e syscall.Errno) error {
switch e {
case 0:
return nil
case errnoERROR_IO_PENDING:
return errERROR_IO_PENDING
}
// TODO: add more here, after collecting data on the common
// error values see on Windows. (perhaps when running
// all.bat?)
return e
}
var _ interfaces.Console = (*consoleWindows)(nil)
type consoleWindows struct {
initialCols int
initialRows int
file *winpty.WinPTY
cwd string
env []string
}
func newNative(cols int, rows int) (Console, error) {
return &consoleWindows{
initialCols: cols,
initialRows: rows,
file: nil,
cwd: ".",
env: os.Environ(),
}, nil
}
func (c *consoleWindows) Start(args []string) error {
dllDir, err := c.UnloadEmbeddedDeps()
if err != nil {
return err
}
opts := winpty.Options{
DLLPrefix: dllDir,
InitialCols: uint32(c.initialCols),
InitialRows: uint32(c.initialRows),
Command: strings.Join(args, " "),
Dir: c.cwd,
Env: c.env,
}
cmd, err := winpty.OpenWithOptions(opts)
if err != nil {
return err
}
c.file = cmd
return nil
}
func (c *consoleWindows) UnloadEmbeddedDeps() (string, error) {
executableName, err := os.Executable()
if err != nil {
return "", err
}
executableName = filepath.Base(executableName)
dllDir := filepath.Join(os.TempDir(), fmt.Sprintf("%s_winpty", executableName))
if err := os.MkdirAll(dllDir, 0755); err != nil {
return "", err
}
files := []string{"winpty.dll", "winpty-agent.exe"}
for _, file := range files {
filenameEmbedded := fmt.Sprintf("winpty/%s", file)
filenameDisk := path.Join(dllDir, file)
_, statErr := os.Stat(filenameDisk)
if statErr == nil {
// file is already there, skip it
continue
}
data, err := winpty_deps.ReadFile(filenameEmbedded)
if err != nil {
return "", err
}
if err := ioutil.WriteFile(path.Join(dllDir, file), data, 0644); err != nil {
return "", err
}
}
return dllDir, nil
}
func (c *consoleWindows) Read(b []byte) (int, error) {
if c.file == nil {
return 0, ErrProcessNotStarted
}
n, err := c.file.StdOut.Read(b)
return n, err
}
func (c *consoleWindows) Write(b []byte) (int, error) {
if c.file == nil {
return 0, ErrProcessNotStarted
}
return c.file.StdIn.Write(b)
}
func (c *consoleWindows) Close() error {
if c.file == nil {
return ErrProcessNotStarted
}
c.file.Close()
return nil
}
func (c *consoleWindows) SetSize(cols int, rows int) error {
c.initialRows = rows
c.initialCols = cols
if c.file == nil {
return nil
}
c.file.SetSize(uint32(c.initialCols), uint32(c.initialRows))
return nil
}
func (c *consoleWindows) GetSize() (int, int, error) {
return c.initialCols, c.initialRows, nil
}
// At this point, sys/windows does not yet contain the method GetProcessID
// this was copied and pasted from: https://github.com/golang/sys/blob/master/windows/zsyscall_windows.go#L2226
func (c *consoleWindows) getProcessIDFromHandle(process uintptr) (id uint32, err error) {
modkernel32 := syscall.NewLazyDLL("kernel32.dll")
procGetProcessId := modkernel32.NewProc("GetProcessId")
r0, _, e1 := syscall.Syscall(procGetProcessId.Addr(), 1, process, 0, 0)
id = uint32(r0)
if id == 0 {
if e1 != 0 {
err = errnoErr(e1)
} else {
err = syscall.EINVAL
}
}
return
}
func (c *consoleWindows) Wait() (*os.ProcessState, error) {
if c.file == nil {
return nil, ErrProcessNotStarted
}
handle := c.file.GetProcHandle()
pid, err := c.getProcessIDFromHandle(handle)
if err != nil {
return nil, err
}
proc, err := os.FindProcess(int(pid))
if err != nil {
return nil, err
}
return proc.Wait()
}
func (c *consoleWindows) SetCWD(cwd string) error {
c.cwd = cwd
return nil
}
func (c *consoleWindows) SetENV(environ []string) error {
c.env = append(os.Environ(), environ...)
return nil
}
func (c *consoleWindows) Pid() (int, error) {
if c.file == nil {
return 0, ErrProcessNotStarted
}
handle := c.file.GetProcHandle()
pid, err := c.getProcessIDFromHandle(handle)
return int(pid), err
}
func (c *consoleWindows) Kill() error {
if c.file == nil {
return ErrProcessNotStarted
}
handle := c.file.GetProcHandle()
pid, err := c.getProcessIDFromHandle(handle)
if err != nil {
return err
}
proc, err := os.FindProcess(int(pid))
if err != nil {
return err
}
return proc.Kill()
}
func (c *consoleWindows) Signal(sig os.Signal) error {
if c.file == nil {
return ErrProcessNotStarted
}
handle := c.file.GetProcHandle()
pid, err := c.getProcessIDFromHandle(handle)
if err != nil {
return err
}
proc, err := os.FindProcess(int(pid))
if err != nil {
return err
}
return proc.Signal(sig)
}