MCPcopy Index your code
hub / github.com/coder/coder / newConPty

Function newConPty

pty/pty_windows.go:49–99  ·  view source on GitHub ↗

newConPty creates a PTY backed by a Windows PseudoConsole (ConPTY). This should only be used when a process will be attached via Start().

(opt ...Option)

Source from the content-addressed store, hash-verified

47// newConPty creates a PTY backed by a Windows PseudoConsole (ConPTY). This
48// should only be used when a process will be attached via Start().
49func newConPty(opt ...Option) (*ptyWindows, error) {
50 var opts ptyOptions
51 for _, o := range opt {
52 o(&opts)
53 }
54
55 pty := &ptyWindows{
56 opts: opts,
57 }
58
59 var err error
60 pty.inputRead, pty.inputWrite, err = os.Pipe()
61 if err != nil {
62 return nil, err
63 }
64 pty.outputRead, pty.outputWrite, err = os.Pipe()
65 if err != nil {
66 _ = pty.inputRead.Close()
67 _ = pty.inputWrite.Close()
68 return nil, err
69 }
70
71 // Default dimensions
72 width, height := 80, 80
73 if opts.sshReq != nil {
74 if w := opts.sshReq.Window.Width; w > 0 && w <= 65535 {
75 width = w
76 }
77 if h := opts.sshReq.Window.Height; h > 0 && h <= 65535 {
78 height = h
79 }
80 }
81
82 consoleSize := uintptr(width) + (uintptr(height) << 16)
83
84 ret, _, err := procCreatePseudoConsole.Call(
85 consoleSize,
86 uintptr(pty.inputRead.Fd()),
87 uintptr(pty.outputWrite.Fd()),
88 0,
89 uintptr(unsafe.Pointer(&pty.console)),
90 )
91 // CreatePseudoConsole returns S_OK on success, as per:
92 // https://learn.microsoft.com/en-us/windows/console/createpseudoconsole
93 if windows.Handle(ret) != windows.S_OK {
94 _ = pty.Close()
95 return nil, xerrors.Errorf("create pseudo console (%d): %w", int32(ret), err)
96 }
97
98 return pty, nil
99}
100
101type ptyWindows struct {
102 opts ptyOptions

Callers 1

startPtyFunction · 0.85

Calls 4

CloseMethod · 0.95
CallMethod · 0.80
CloseMethod · 0.65
ErrorfMethod · 0.45

Tested by

no test coverage detected