terminal.go 2.4 KB
Newer Older
D
Dan Mace 已提交
1 2 3
package terminal

import (
4
	"errors"
D
Dan Mace 已提交
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
	"fmt"
	"io"
	"os"
	"os/signal"
	"strings"

	"github.com/peterh/liner"
	sys "golang.org/x/sys/unix"

	"github.com/derekparker/delve/proctl"
	"github.com/derekparker/delve/service"
)

const historyFile string = ".dbg_history"

type Term struct {
	client service.Client
	prompt string
	line   *liner.State
}

func New(client service.Client) *Term {
	return &Term{
		prompt: "(dlv) ",
		line:   liner.NewLiner(),
		client: client,
	}
}

func (t *Term) Run() (error, int) {
	defer t.line.Close()

	// Send the debugger a halt command on SIGINT
	ch := make(chan os.Signal)
	signal.Notify(ch, sys.SIGINT)
	go func() {
		for range ch {
			_, err := t.client.Halt()
			if err != nil {
				fmt.Println(err)
			}
		}
	}()

	cmds := DebugCommands(t.client)
	f, err := os.Open(historyFile)
	if err != nil {
		f, _ = os.Create(historyFile)
	}
	t.line.ReadHistory(f)
	f.Close()
	fmt.Println("Type 'help' for list of commands.")

	var status int

	for {
		cmdstr, err := t.promptForInput()
		if err != nil {
			if err == io.EOF {
				err, status = handleExit(t.client, t)
			}
66
			err, status = errors.New("Prompt for input failed.\n"), 1
D
Dan Mace 已提交
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
			break
		}

		cmdstr, args := parseCommand(cmdstr)
		if cmdstr == "exit" {
			err, status = handleExit(t.client, t)
			break
		}

		cmd := cmds.Find(cmdstr)
		if err := cmd(t.client, args...); err != nil {
			switch err.(type) {
			case proctl.ProcessExitedError:
				pe := err.(proctl.ProcessExitedError)
				fmt.Fprintf(os.Stderr, "Process exited with status %d\n", pe.Status)
			default:
				fmt.Fprintf(os.Stderr, "Command failed: %s\n", err)
			}
		}
	}

	return nil, status
}

D
Derek Parker 已提交
91 92 93 94 95 96 97 98 99 100 101 102 103 104
func (t *Term) promptForInput() (string, error) {
	l, err := t.line.Prompt(t.prompt)
	if err != nil {
		return "", err
	}

	l = strings.TrimSuffix(l, "\n")
	if l != "" {
		t.line.AppendHistory(l)
	}

	return l, nil
}

D
Dan Mace 已提交
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
func handleExit(client service.Client, t *Term) (error, int) {
	if f, err := os.OpenFile(historyFile, os.O_RDWR, 0666); err == nil {
		_, err := t.line.WriteHistory(f)
		if err != nil {
			fmt.Println("readline history error: ", err)
		}
		f.Close()
	}

	answer, err := t.line.Prompt("Would you like to kill the process? [y/n] ")
	if err != nil {
		return io.EOF, 2
	}
	answer = strings.TrimSuffix(answer, "\n")

	kill := (answer == "y")
	err = client.Detach(kill)
	if err != nil {
		return err, 1
	}
	return nil, 0
}

func parseCommand(cmdstr string) (string, []string) {
	vals := strings.Split(cmdstr, " ")
	return vals[0], vals[1:]
}