| 1 | package workbench |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "io" |
| 6 | "os" |
| 7 | "os/exec" |
| 8 | "strings" |
| 9 | "sync" |
| 10 | "syscall" |
| 11 | "unicode/utf16" |
| 12 | "unsafe" |
| 13 | |
| 14 | "golang.org/x/sys/windows" |
| 15 | ) |
| 16 | |
| 17 | |
| 18 | |
| 19 | type windowsTerminal struct { |
| 20 | input, output *os.File |
| 21 | console windows.Handle |
| 22 | process *os.Process |
| 23 | once sync.Once |
| 24 | consoleOnce sync.Once |
| 25 | consoleMu sync.Mutex |
| 26 | done chan struct{} |
| 27 | waitErr error |
| 28 | } |
| 29 | |
| 30 | func startTerminal(cmd *exec.Cmd, cols, rows uint16) (terminalPTY, error) { |
| 31 | inputRead, inputWrite, err := os.Pipe() |
| 32 | if err != nil { |
| 33 | return nil, err |
| 34 | } |
| 35 | defer inputRead.Close() |
| 36 | outputRead, outputWrite, err := os.Pipe() |
| 37 | if err != nil { |
| 38 | inputWrite.Close() |
| 39 | return nil, err |
| 40 | } |
| 41 | defer outputWrite.Close() |
| 42 | p := &windowsTerminal{input: inputWrite, output: outputRead, done: make(chan struct{})} |
| 43 | success := false |
| 44 | defer func() { |
| 45 | if !success { |
| 46 | p.Close() |
| 47 | } |
| 48 | }() |
| 49 | if err = windows.CreatePseudoConsole(windows.Coord{X: int16(cols), Y: int16(rows)}, windows.Handle(inputRead.Fd()), windows.Handle(outputWrite.Fd()), 0, &p.console); err != nil { |
| 50 | return nil, err |
| 51 | } |
| 52 | attrs, err := windows.NewProcThreadAttributeList(1) |
| 53 | if err != nil { |
| 54 | return nil, err |
| 55 | } |
| 56 | defer attrs.Delete() |
| 57 | |
| 58 | |
| 59 | update := windows.NewLazySystemDLL("kernel32.dll").NewProc("UpdateProcThreadAttribute") |
| 60 | if ok, _, callErr := update.Call(uintptr(unsafe.Pointer(attrs.List())), 0, windows.PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE, uintptr(p.console), unsafe.Sizeof(p.console), 0, 0); ok == 0 { |
| 61 | return nil, callErr |
| 62 | } |
| 63 | startup := windows.StartupInfoEx{} |
| 64 | startup.Cb = uint32(unsafe.Sizeof(startup)) |
| 65 | |
| 66 | |
| 67 | startup.Flags = windows.STARTF_USESTDHANDLES |
| 68 | startup.ProcThreadAttributeList = attrs.List() |
| 69 | app, err := windows.UTF16PtrFromString(cmd.Path) |
| 70 | if err != nil { |
| 71 | return nil, err |
| 72 | } |
| 73 | parts := make([]string, len(cmd.Args)) |
| 74 | for i, arg := range cmd.Args { |
| 75 | parts[i] = syscall.EscapeArg(arg) |
| 76 | } |
| 77 | commandLine, err := windows.UTF16PtrFromString(strings.Join(parts, " ")) |
| 78 | if err != nil { |
| 79 | return nil, err |
| 80 | } |
| 81 | var directory *uint16 |
| 82 | if cmd.Dir != "" { |
| 83 | directory, err = windows.UTF16PtrFromString(cmd.Dir) |
| 84 | if err != nil { |
| 85 | return nil, err |
| 86 | } |
| 87 | } |
| 88 | env := cmd.Env |
| 89 | if env == nil { |
| 90 | env = os.Environ() |
| 91 | } |
| 92 | for _, entry := range env { |
| 93 | if strings.ContainsRune(entry, 0) { |
| 94 | return nil, errors.New("invalid terminal environment") |
| 95 | } |
| 96 | } |
| 97 | block := utf16.Encode([]rune(strings.Join(env, "\x00") + "\x00\x00")) |
| 98 | var info windows.ProcessInformation |
| 99 | err = windows.CreateProcess(app, commandLine, nil, nil, false, windows.EXTENDED_STARTUPINFO_PRESENT|windows.CREATE_UNICODE_ENVIRONMENT, &block[0], directory, &startup.StartupInfo, &info) |
| 100 | if err != nil { |
| 101 | return nil, err |
| 102 | } |
| 103 | defer windows.CloseHandle(info.Thread) |
| 104 | defer windows.CloseHandle(info.Process) |
| 105 | p.process, err = os.FindProcess(int(info.ProcessId)) |
| 106 | if err != nil { |
| 107 | windows.TerminateProcess(info.Process, 1) |
| 108 | return nil, err |
| 109 | } |
| 110 | success = true |
| 111 | go func() { |
| 112 | _, p.waitErr = p.process.Wait() |
| 113 | p.closeConsole() |
| 114 | close(p.done) |
| 115 | }() |
| 116 | return p, nil |
| 117 | } |
| 118 | func (p *windowsTerminal) Read(b []byte) (int, error) { return p.output.Read(b) } |
| 119 | func (p *windowsTerminal) Write(b []byte) (int, error) { return p.input.Write(b) } |
| 120 | func (p *windowsTerminal) Resize(cols, rows uint16) error { |
| 121 | p.consoleMu.Lock() |
| 122 | defer p.consoleMu.Unlock() |
| 123 | if p.console == 0 { |
| 124 | return os.ErrClosed |
| 125 | } |
| 126 | return windows.ResizePseudoConsole(p.console, windows.Coord{X: int16(cols), Y: int16(rows)}) |
| 127 | } |
| 128 | func (p *windowsTerminal) Wait() error { <-p.done; return p.waitErr } |
| 129 | func (p *windowsTerminal) Kill() error { return p.process.Kill() } |
| 130 | func (p *windowsTerminal) closeConsole() { |
| 131 | p.consoleOnce.Do(func() { |
| 132 | p.consoleMu.Lock() |
| 133 | defer p.consoleMu.Unlock() |
| 134 | if p.console != 0 { |
| 135 | windows.ClosePseudoConsole(p.console) |
| 136 | p.console = 0 |
| 137 | } |
| 138 | }) |
| 139 | } |
| 140 | func (p *windowsTerminal) Close() error { |
| 141 | p.once.Do(func() { |
| 142 | p.input.Close() |
| 143 | |
| 144 | |
| 145 | |
| 146 | drained := make(chan struct{}) |
| 147 | go func() { |
| 148 | _, _ = io.Copy(io.Discard, p.output) |
| 149 | close(drained) |
| 150 | }() |
| 151 | p.closeConsole() |
| 152 | p.output.Close() |
| 153 | <-drained |
| 154 | }) |
| 155 | return nil |
| 156 | } |