|
| 1 | +//go:build windows |
| 2 | +// +build windows |
| 3 | + |
| 4 | +// Original copyright 2020 ActiveState Software. All rights reserved. |
| 5 | +// Use of this source code is governed by a BSD-style |
| 6 | +// license that can be found in the LICENSE file |
| 7 | + |
| 8 | +package conpty |
| 9 | + |
| 10 | +import ( |
| 11 | +"fmt" |
| 12 | +"io" |
| 13 | +"os" |
| 14 | + |
| 15 | +"golang.org/x/sys/windows" |
| 16 | +) |
| 17 | + |
| 18 | +// ConPty represents a windows pseudo console. |
| 19 | +typeConPtystruct { |
| 20 | +hpCon windows.Handle |
| 21 | +outPipePseudoConsoleSide windows.Handle |
| 22 | +outPipeOurSide windows.Handle |
| 23 | +inPipeOurSide windows.Handle |
| 24 | +inPipePseudoConsoleSide windows.Handle |
| 25 | +consoleSizeuintptr |
| 26 | +outFilePseudoConsoleSide*os.File |
| 27 | +outFileOurSide*os.File |
| 28 | +inFilePseudoConsoleSide*os.File |
| 29 | +inFileOurSide*os.File |
| 30 | +closedbool |
| 31 | +} |
| 32 | + |
| 33 | +// New returns a new ConPty pseudo terminal device |
| 34 | +funcNew(columnsint16,rowsint16) (*ConPty,error) { |
| 35 | +c:=&ConPty{ |
| 36 | +consoleSize:uintptr(columns)+ (uintptr(rows)<<16), |
| 37 | +} |
| 38 | + |
| 39 | +returnc,c.createPseudoConsoleAndPipes() |
| 40 | +} |
| 41 | + |
| 42 | +// Close closes the pseudo-terminal and cleans up all attached resources |
| 43 | +func (c*ConPty)Close()error { |
| 44 | +// Trying to close these pipes multiple times will result in an |
| 45 | +// access violation |
| 46 | +ifc.closed { |
| 47 | +returnnil |
| 48 | +} |
| 49 | + |
| 50 | +err:=closePseudoConsole(c.hpCon) |
| 51 | +c.outFilePseudoConsoleSide.Close() |
| 52 | +c.outFileOurSide.Close() |
| 53 | +c.inFilePseudoConsoleSide.Close() |
| 54 | +c.inFileOurSide.Close() |
| 55 | +c.closed=true |
| 56 | +returnerr |
| 57 | +} |
| 58 | + |
| 59 | +// OutPipe returns the output pipe of the pseudo terminal |
| 60 | +func (c*ConPty)OutPipe()*os.File { |
| 61 | +returnc.outFilePseudoConsoleSide |
| 62 | +} |
| 63 | + |
| 64 | +func (c*ConPty)Reader() io.Reader { |
| 65 | +returnc.outFileOurSide |
| 66 | +} |
| 67 | + |
| 68 | +// InPipe returns input pipe of the pseudo terminal |
| 69 | +// Note: It is safer to use the Write method to prevent partially-written VT sequences |
| 70 | +// from corrupting the terminal |
| 71 | +func (c*ConPty)InPipe()*os.File { |
| 72 | +returnc.inFilePseudoConsoleSide |
| 73 | +} |
| 74 | + |
| 75 | +func (c*ConPty)WriteString(strstring) (int,error) { |
| 76 | +returnc.inFileOurSide.WriteString(str) |
| 77 | +} |
| 78 | + |
| 79 | +func (c*ConPty)createPseudoConsoleAndPipes()error { |
| 80 | +// Create the stdin pipe |
| 81 | +iferr:=windows.CreatePipe(&c.inPipePseudoConsoleSide,&c.inPipeOurSide,nil,0);err!=nil { |
| 82 | +returnerr |
| 83 | +} |
| 84 | + |
| 85 | +// Create the stdout pipe |
| 86 | +iferr:=windows.CreatePipe(&c.outPipeOurSide,&c.outPipePseudoConsoleSide,nil,0);err!=nil { |
| 87 | +returnerr |
| 88 | +} |
| 89 | + |
| 90 | +// Create the pty with our stdin/stdout |
| 91 | +iferr:=createPseudoConsole(c.consoleSize,c.inPipePseudoConsoleSide,c.outPipePseudoConsoleSide,&c.hpCon);err!=nil { |
| 92 | +returnfmt.Errorf("failed to create pseudo console: %d, %v",uintptr(c.hpCon),err) |
| 93 | +} |
| 94 | + |
| 95 | +c.outFilePseudoConsoleSide=os.NewFile(uintptr(c.outPipePseudoConsoleSide),"|0") |
| 96 | +c.outFileOurSide=os.NewFile(uintptr(c.outPipeOurSide),"|1") |
| 97 | + |
| 98 | +c.inFilePseudoConsoleSide=os.NewFile(uintptr(c.inPipePseudoConsoleSide),"|2") |
| 99 | +c.inFileOurSide=os.NewFile(uintptr(c.inPipeOurSide),"|3") |
| 100 | +c.closed=false |
| 101 | + |
| 102 | +returnnil |
| 103 | +} |
| 104 | + |
| 105 | +func (c*ConPty)Resize(colsuint16,rowsuint16)error { |
| 106 | +returnresizePseudoConsole(c.hpCon,uintptr(cols)+(uintptr(rows)<<16)) |
| 107 | +} |