Update working directory of elvish-stub.

This commit is contained in:
Qi Xiao 2016-02-22 18:42:10 +01:00
parent 158b4b82a5
commit 657448acf3
3 changed files with 34 additions and 16 deletions

View File

@ -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)
}

View File

@ -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.

View File

@ -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;
}