From 657448acf38c61c87a9e2e303751e57a98e2e3fb Mon Sep 17 00:00:00 2001 From: Qi Xiao Date: Mon, 22 Feb 2016 18:42:10 +0100 Subject: [PATCH] Update working directory of elvish-stub. --- eval/eval.go | 7 ++++++- stub/stub.go | 7 ++++++- stubimpl/main.c | 36 ++++++++++++++++++++++-------------- 3 files changed, 34 insertions(+), 16 deletions(-) diff --git a/eval/eval.go b/eval/eval.go index 957e7ab6..dc8e4251 100644 --- a/eval/eval.go +++ b/eval/eval.go @@ -177,7 +177,12 @@ func (ev *Evaler) EvalInteractive(text string, n *parse.Chunk) error { // XXX Should use fd of /dev/terminal instead of 0. if ev.Stub != nil && sys.IsATTY(0) { ev.Stub.SetTitle(summarize(text)) - err := sys.Tcsetpgrp(0, ev.Stub.Process().Pid) + dir, err := os.Getwd() + if err != nil { + dir = "/" + } + ev.Stub.Chdir(dir) + err = sys.Tcsetpgrp(0, ev.Stub.Process().Pid) if err != nil { fmt.Println("failed to put stub in foreground:", err) } diff --git a/stub/stub.go b/stub/stub.go index 50d5203a..48e9cbfa 100644 --- a/stub/stub.go +++ b/stub/stub.go @@ -96,7 +96,12 @@ func (stub *Stub) Terminate() { // SetTitle sets the title of the stub. func (stub *Stub) SetTitle(s string) { - stub.write.WriteString(s + "\n") + s = strings.TrimSpace(s) + fmt.Fprintf(stub.write, "t %d %s\n", len(s), s) +} + +func (stub *Stub) Chdir(dir string) { + fmt.Fprintf(stub.write, "d %d %s\n", len(dir), dir) } // Signals returns a channel into which signals sent to the stub are relayed. diff --git a/stubimpl/main.c b/stubimpl/main.c index 0c081a3f..4b3b2d99 100644 --- a/stubimpl/main.c +++ b/stubimpl/main.c @@ -22,9 +22,7 @@ void handler(int signum) { must(write(1, p, s+4-p) != -1, "write signum"); } -enum { ARGV0BUF = 32 }; - -char argv0buf[ARGV0BUF]; +enum { ARGV0MAX = 32 }; int main(int argc, char **argv) { int i; @@ -36,18 +34,28 @@ int main(int argc, char **argv) { must(write(1, "ok\n", 3) != -1, "write ok"); - while (1) { - if (fgets(argv0buf, ARGV0BUF, stdin) == NULL) { - if (feof(stdin)) { - exit(0); - } else { - exit(10); + char op; + int len; + char *buf; + int scanned; + while ((scanned = scanf(" %c%d ", &op, &len)) == 2) { + printf("op=%d, len=%d\n", op, len); + buf = malloc(len+1); + fgets(buf, len+1, stdin); + printf("buf=%s\n", buf); + if (op == 'd') { + // Change directory. + chdir(buf); + } else if (op == 't') { + if (len > ARGV0MAX) { + buf[ARGV0MAX] = '\0'; } + strcpy(argv[0], buf); } - int n = strlen(argv0buf); - if (n > 0 && argv0buf[n-1] == '\n') { - argv0buf[n-1] = '\0'; - } - strcpy(argv[0], argv0buf); + free(buf); } + if (scanned != EOF) { + return 1; + } + return 0; }