From 52c8f8e97202c22b3da2b56a9d8dbe111642f2a0 Mon Sep 17 00:00:00 2001 From: Derek Parker Date: Fri, 27 Feb 2015 15:02:46 -0600 Subject: [PATCH] Remove readline dep --- client/cli/cli.go | 57 ++++++++++++++------------ goreadline/goreadline.go | 86 ---------------------------------------- 2 files changed, 31 insertions(+), 112 deletions(-) delete mode 100644 goreadline/goreadline.go diff --git a/client/cli/cli.go b/client/cli/cli.go index fad2d3ba..bf421032 100644 --- a/client/cli/cli.go +++ b/client/cli/cli.go @@ -11,17 +11,20 @@ import ( sys "golang.org/x/sys/unix" "github.com/derekparker/delve/command" - "github.com/derekparker/delve/goreadline" "github.com/derekparker/delve/proctl" + + "github.com/peterh/liner" ) const historyFile string = ".dbg_history" func Run(run bool, pid int, args []string) { var ( - dbp *proctl.DebuggedProcess - err error + dbp *proctl.DebuggedProcess + err error + line = liner.NewLiner() ) + defer line.Close() switch { case run: @@ -60,14 +63,17 @@ func Run(run bool, pid int, args []string) { }() cmds := command.DebugCommands() - goreadline.LoadHistoryFromFile(historyFile) + if f, err := os.Open(historyFile); err == nil { + line.ReadHistory(f) + f.Close() + } fmt.Println("Type 'help' for list of commands.") for { - cmdstr, err := promptForInput() + cmdstr, err := promptForInput(line) if err != nil { if err == io.EOF { - handleExit(dbp, 0) + handleExit(dbp, line, 0) } die(1, "Prompt for input failed.\n") } @@ -75,7 +81,7 @@ func Run(run bool, pid int, args []string) { cmdstr, args := parseCommand(cmdstr) if cmdstr == "exit" { - handleExit(dbp, 0) + handleExit(dbp, line, 0) } cmd := cmds.Find(cmdstr) @@ -86,18 +92,17 @@ func Run(run bool, pid int, args []string) { } } -func handleExit(dbp *proctl.DebuggedProcess, status int) { - errno := goreadline.WriteHistoryToFile(historyFile) - if errno != 0 { - fmt.Println("readline:", errno) +func handleExit(dbp *proctl.DebuggedProcess, line *liner.State, status int) { + if f, err := os.Open(historyFile); err == nil { + line.WriteHistory(f) + f.Close() } - prompt := "Would you like to kill the process? [y/n]" - answerp := goreadline.ReadLine(&prompt) - if answerp == nil { + answer, err := line.Prompt("Would you like to kill the process? [y/n]") + if err != nil { die(2, io.EOF) } - answer := strings.TrimSuffix(*answerp, "\n") + answer = strings.TrimSuffix(answer, "\n") for _, bp := range dbp.HWBreakPoints { if bp == nil { @@ -115,7 +120,7 @@ func handleExit(dbp *proctl.DebuggedProcess, status int) { } fmt.Println("Detaching from process...") - err := sys.PtraceDetach(dbp.Process.Pid) + err = sys.PtraceDetach(dbp.Process.Pid) if err != nil { die(2, "Could not detach", err) } @@ -143,16 +148,16 @@ func parseCommand(cmdstr string) (string, []string) { return vals[0], vals[1:] } -func promptForInput() (string, error) { - prompt := "(dlv) " - linep := goreadline.ReadLine(&prompt) - if linep == nil { - return "", io.EOF - } - line := strings.TrimSuffix(*linep, "\n") - if line != "" { - goreadline.AddHistory(line) +func promptForInput(line *liner.State) (string, error) { + l, err := line.Prompt("(dlv) ") + if err != nil { + return "", err } - return line, nil + l = strings.TrimSuffix(l, "\n") + if l != "" { + line.AppendHistory(l) + } + + return l, nil } diff --git a/goreadline/goreadline.go b/goreadline/goreadline.go deleted file mode 100644 index 0a1d5e55..00000000 --- a/goreadline/goreadline.go +++ /dev/null @@ -1,86 +0,0 @@ -package goreadline - -// sniped from https://github.com/rocaltair/goreadline - -/* -#include -#include -#include -#include -#cgo LDFLAGS: -lreadline -*/ -import "C" -import ( - sys "golang.org/x/sys/unix" - "os" - "os/signal" - "unsafe" -) - -func init() { - C.rl_catch_sigwinch = 0 - c := make(chan os.Signal, 1) - signal.Notify(c, sys.SIGWINCH) - go func() { - for sig := range c { - switch sig { - case sys.SIGWINCH: - Resize() - default: - - } - } - }() -} - -func Resize() { - C.rl_resize_terminal() -} - -func ReadLine(prompt *string) *string { - var cPrompt *C.char - if prompt != nil { - cPrompt = C.CString(*prompt) - } - cLine := C.readline(cPrompt) - if cPrompt != nil { - C.free(unsafe.Pointer(cPrompt)) - } - if cLine == nil { - return nil - } - - line := C.GoString(cLine) - C.free(unsafe.Pointer(cLine)) - return &line -} - -func AddHistory(line string) { - cLine := C.CString(line) - C.add_history(cLine) - C.free(unsafe.Pointer(cLine)) -} - -func ClearHistory() { - C.clear_history() -} - -func WriteHistoryToFile(fileName string) int { - cFileName := C.CString(fileName) - err := C.write_history(cFileName) - C.free(unsafe.Pointer(cFileName)) - return int(err) -} - -func LoadHistoryFromFile(fileName string) { - cFileName := C.CString(fileName) - C.read_history(cFileName) - C.free(unsafe.Pointer(cFileName)) -} - -func TruncateHistoryFile(fileName string, left int) { - cFileName := C.CString(fileName) - cLeft := C.int(left) - C.history_truncate_file(cFileName, cLeft) - C.free(unsafe.Pointer(cFileName)) -}