Skip to content

Commit e23745e

Browse files
committed
Fix up naming, the input/output pipes are always confusing...
1 parent 9ea9bff commit e23745e

File tree

2 files changed

+31
-34
lines changed

2 files changed

+31
-34
lines changed

expect/conpty/conpty.go

Lines changed: 30 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//go:build windows
12
// +build windows
23

34
// Original copyright 2020 ActiveState Software. All rights reserved.
@@ -16,17 +17,17 @@ import (
1617

1718
// ConPty represents a windows pseudo console.
1819
type ConPty struct {
19-
hpCon windows.Handle
20-
pipeFdIn windows.Handle
21-
pipeFdOut windows.Handle
22-
pipe3 windows.Handle
23-
pipe4 windows.Handle
24-
consoleSize uintptr
25-
outputR *os.File
26-
outputW *os.File
27-
inputR *os.File
28-
inputW *os.File
29-
closed bool
20+
hpCon windows.Handle
21+
outPipePseudoConsoleSide windows.Handle
22+
outPipeOurSide windows.Handle
23+
inPipeOurSide windows.Handle
24+
inPipePseudoConsoleSide windows.Handle
25+
consoleSize uintptr
26+
outFilePseudoConsoleSide *os.File
27+
outFileOurSide *os.File
28+
inFilePseudoConsoleSide *os.File
29+
inFileOurSide *os.File
30+
closed bool
3031
}
3132

3233
// New returns a new ConPty pseudo terminal device
@@ -40,67 +41,62 @@ func New(columns int16, rows int16) (*ConPty, error) {
4041

4142
// Close closes the pseudo-terminal and cleans up all attached resources
4243
func (c *ConPty) Close() error {
43-
if (c.closed) {
44+
// Trying to close these pipes multiple times will result in an
45+
// access violation
46+
if c.closed {
4447
return nil
4548
}
4649

4750
err := closePseudoConsole(c.hpCon)
48-
c.outputR.Close()
49-
c.outputW.Close()
50-
c.inputR.Close()
51-
c.inputW.Close()
51+
c.outFilePseudoConsoleSide.Close()
52+
c.outFileOurSide.Close()
53+
c.inFilePseudoConsoleSide.Close()
54+
c.inFileOurSide.Close()
5255
c.closed = true
5356
return err
5457
}
5558

5659
// OutPipe returns the output pipe of the pseudo terminal
5760
func (c *ConPty) OutPipe() *os.File {
58-
return c.outputR
61+
return c.outFilePseudoConsoleSide
5962
}
6063

6164
func (c *ConPty) Reader() io.Reader {
62-
return c.outputW
65+
return c.outFileOurSide
6366
}
6467

6568
// InPipe returns input pipe of the pseudo terminal
6669
// Note: It is safer to use the Write method to prevent partially-written VT sequences
6770
// from corrupting the terminal
6871
func (c *ConPty) InPipe() *os.File {
69-
return c.inputR
72+
return c.inFilePseudoConsoleSide
7073
}
7174

7275
func (c *ConPty) WriteString(str string) (int, error) {
73-
return c.inputW.WriteString(str)
76+
return c.inFileOurSide.WriteString(str)
7477
}
7578

7679
func (c *ConPty) createPseudoConsoleAndPipes() error {
77-
// These are the readers/writers for "stdin", but we only need this to
78-
// successfully call CreatePseudoConsole. After, we can throw it away.
79-
var hPipeInW, hPipeInR windows.Handle
80-
8180
// Create the stdin pipe
82-
if err := windows.CreatePipe(&hPipeInR, &hPipeInW, nil, 0); err != nil {
81+
if err := windows.CreatePipe(&c.inPipePseudoConsoleSide, &c.inPipeOurSide, nil, 0); err != nil {
8382
return err
8483
}
8584

8685
// Create the stdout pipe
87-
if err := windows.CreatePipe(&c.pipeFdOut, &c.pipeFdIn, nil, 0); err != nil {
86+
if err := windows.CreatePipe(&c.outPipeOurSide, &c.outPipePseudoConsoleSide, nil, 0); err != nil {
8887
return err
8988
}
9089

9190
// Create the pty with our stdin/stdout
92-
if err := createPseudoConsole(c.consoleSize, hPipeInR, c.pipeFdIn, &c.hpCon); err != nil {
91+
if err := createPseudoConsole(c.consoleSize, c.inPipePseudoConsoleSide, c.outPipePseudoConsoleSide, &c.hpCon); err != nil {
9392
return fmt.Errorf("failed to create pseudo console: %d, %v", uintptr(c.hpCon), err)
9493
}
9594

96-
c.pipe3 = hPipeInR
97-
c.pipe4 = hPipeInW
98-
99-
c.outputR = os.NewFile(uintptr(c.pipeFdIn), "|0")
100-
c.outputW = os.NewFile(uintptr(c.pipeFdOut), "|1")
95+
c.outFilePseudoConsoleSide = os.NewFile(uintptr(c.outPipePseudoConsoleSide), "|0")
96+
c.outFileOurSide = os.NewFile(uintptr(c.outPipeOurSide), "|1")
10197

102-
c.inputR = os.NewFile(uintptr(c.pipe3), "|2")
103-
c.inputW = os.NewFile(uintptr(c.pipe4), "|3")
98+
c.inFilePseudoConsoleSide = os.NewFile(uintptr(c.inPipePseudoConsoleSide), "|2")
99+
c.inFileOurSide = os.NewFile(uintptr(c.inPipeOurSide), "|3")
104100
c.closed = false
105101

106102
return nil

expect/conpty/syscall.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//go:build windows
12
// +build windows
23

34
// Copyright 2020 ActiveState Software. All rights reserved.

0 commit comments

Comments
 (0)