fixed crash if stdin is not a tty (pipe)

This commit is contained in:
Cheney 2016-02-17 14:08:55 +08:00
parent 07485bbd8f
commit 21acaf90fd
4 changed files with 16 additions and 2 deletions

View File

@ -84,6 +84,13 @@ func (o *Operation) ioloop() {
keepInSearchMode := false
keepInCompleteMode := false
r := o.t.ReadRune()
if r == 0 { // io.EOF
o.buf.Clean()
select {
case o.errchan <- io.EOF:
}
break
}
isUpdateHistory := true
if o.IsInCompleteSelectMode() {

View File

@ -67,8 +67,13 @@ func (t *Terminal) Readline() *Operation {
return NewOperation(t, t.cfg)
}
// return rune(0) if meet EOF
func (t *Terminal) ReadRune() rune {
return <-t.outchan
ch, ok := <-t.outchan
if !ok {
return rune(0)
}
return ch
}
func (t *Terminal) IsReading() bool {
@ -136,6 +141,7 @@ func (t *Terminal) ioloop() {
t.outchan <- r
}
}
close(t.outchan)
}
func (t *Terminal) Bell() {

View File

@ -10,6 +10,7 @@ import (
var (
StdinFd = int(uintptr(syscall.Stdin))
StdoutFd = int(uintptr(syscall.Stdout))
isWindows = false
)

View File

@ -18,7 +18,7 @@ type winsize struct {
func getWidth() int {
ws := &winsize{}
retCode, _, errno := syscall.Syscall(syscall.SYS_IOCTL,
uintptr(StdinFd),
uintptr(StdoutFd),
uintptr(syscall.TIOCGWINSZ),
uintptr(unsafe.Pointer(ws)))