Merge pull request #1112 from fjl/fix-console-exit

cmd/geth: exit the console cleanly when interrupted
This commit is contained in:
Jeffrey Wilcke 2015-05-27 04:40:29 -07:00
commit e13f413ef5

View File

@ -22,6 +22,7 @@ import (
"fmt" "fmt"
"math/big" "math/big"
"os" "os"
"os/signal"
"path/filepath" "path/filepath"
"strings" "strings"
@ -47,7 +48,8 @@ type dumbterm struct{ r *bufio.Reader }
func (r dumbterm) Prompt(p string) (string, error) { func (r dumbterm) Prompt(p string) (string, error) {
fmt.Print(p) fmt.Print(p)
return r.r.ReadString('\n') line, err := r.r.ReadString('\n')
return strings.TrimSuffix(line, "\n"), err
} }
func (r dumbterm) PasswordPrompt(p string) (string, error) { func (r dumbterm) PasswordPrompt(p string) (string, error) {
@ -182,30 +184,52 @@ func (self *jsre) exec(filename string) error {
} }
func (self *jsre) interactive() { func (self *jsre) interactive() {
for { // Read input lines.
input, err := self.Prompt(self.ps1) prompt := make(chan string)
if err != nil { inputln := make(chan string)
break go func() {
} defer close(inputln)
if input == "" { for {
continue line, err := self.Prompt(<-prompt)
} if err != nil {
str += input + "\n" return
self.setIndent() }
if indentCount <= 0 { inputln <- line
if input == "exit" { }
break }()
// Wait for Ctrl-C, too.
sig := make(chan os.Signal, 1)
signal.Notify(sig, os.Interrupt)
defer func() {
if self.atexit != nil {
self.atexit()
}
self.re.Stop(false)
}()
for {
prompt <- self.ps1
select {
case <-sig:
fmt.Println("caught interrupt, exiting")
return
case input, ok := <-inputln:
if !ok || indentCount <= 0 && input == "exit" {
return
}
if input == "" {
continue
}
str += input + "\n"
self.setIndent()
if indentCount <= 0 {
hist := str[:len(str)-1]
self.AppendHistory(hist)
self.parseInput(str)
str = ""
} }
hist := str[:len(str)-1]
self.AppendHistory(hist)
self.parseInput(str)
str = ""
} }
} }
if self.atexit != nil {
self.atexit()
}
self.re.Stop(false)
} }
func (self *jsre) withHistory(op func(*os.File)) { func (self *jsre) withHistory(op func(*os.File)) {