From 7fc3c0ba26088fae71f7a57a9195ee9bae71e2a0 Mon Sep 17 00:00:00 2001 From: Sachin Joshi Date: Wed, 18 Mar 2015 13:10:57 -0700 Subject: [PATCH] Fix panic with vi in busybox Following bugs are fixed: 1.Handle out of bound cursor movements: vi in busybox sets cursor to (999,999) expecting it to be set to right, bottom correctly. 2.Correctly determine redirected non-terminal file. Signed-off-by: Sachin Joshi --- term/winconsole/console_windows.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/term/winconsole/console_windows.go b/term/winconsole/console_windows.go index 2d0421f..859ee1a 100644 --- a/term/winconsole/console_windows.go +++ b/term/winconsole/console_windows.go @@ -265,6 +265,10 @@ func StdStreams() (stdOut io.Writer, stdErr io.Writer, stdIn io.ReadCloser) { func GetHandleInfo(in interface{}) (uintptr, bool) { var inFd uintptr var isTerminalIn bool + if file, ok := in.(*os.File); ok { + inFd = file.Fd() + isTerminalIn = IsTerminal(inFd) + } if tr, ok := in.(*terminalReader); ok { if file, ok := tr.wrappedReader.(*os.File); ok { inFd = file.Fd() @@ -678,14 +682,24 @@ func (term *WindowsTerminal) HandleOutputCommand(fd uintptr, command []byte) (n // [line;columnf // Moves the cursor to the specified position (coordinates). // If you do not specify a position, the cursor moves to the home position at the upper-left corner of the screen (line 0, column 0). + screenBufferInfo, err := GetConsoleScreenBufferInfo(uintptr(handle)) + if err != nil { + return len(command), err + } line, err := parseInt16OrDefault(parsedCommand.getParam(0), 1) if err != nil { return len(command), err } + if line > int16(screenBufferInfo.Window.Bottom) { + line = int16(screenBufferInfo.Window.Bottom) + } column, err := parseInt16OrDefault(parsedCommand.getParam(1), 1) if err != nil { return len(command), err } + if column > int16(screenBufferInfo.Window.Right) { + column = int16(screenBufferInfo.Window.Right) + } // The numbers are not 0 based, but 1 based r, err = setConsoleCursorPosition(uintptr(handle), false, int16(column-1), int16(line-1)) if !r {