diff --git a/attach.c b/attach.c index 6a7713b..ece56db 100644 --- a/attach.c +++ b/attach.c @@ -34,9 +34,6 @@ static struct termios cur_term; /* 1 if the window size changed */ static int win_changed; -/* This hopefully moves to the bottom of the screen */ -#define EOS "\033[999H" - /* Restores the original terminal settings. */ static void restore_term(void) diff --git a/dtach.h b/dtach.h index 2ffd778..c7377df 100644 --- a/dtach.h +++ b/dtach.h @@ -116,8 +116,11 @@ struct packet */ #define BUFSIZE 4096 +/* This hopefully moves to the bottom of the screen */ +#define EOS "\033[999H" + int attach_main(int noerror); -int master_main(char **argv); +int master_main(char **argv, int waitattach); #ifdef sun #define BROKEN_MASTER diff --git a/main.c b/main.c index f7bc13d..6687c16 100644 --- a/main.c +++ b/main.c @@ -243,10 +243,10 @@ main(int argc, char **argv) return attach_main(0); } else if (mode == 'n') - return master_main(argv); + return master_main(argv, 0); else if (mode == 'c') { - if (master_main(argv) != 0) + if (master_main(argv, 1) != 0) return 1; return attach_main(0); } @@ -256,7 +256,7 @@ main(int argc, char **argv) ** socket. */ if (attach_main(1) != 0) { - if (master_main(argv) != 0) + if (master_main(argv, 1) != 0) return 1; return attach_main(0); } diff --git a/master.c b/master.c index 4b03fab..a46782d 100644 --- a/master.c +++ b/master.c @@ -124,6 +124,8 @@ init_pty(char **argv) { /* Child.. Execute the program. */ execvp(*argv, argv); + printf(EOS "\r\n%s: could not execute %s: %s\r\n", + progname, *argv, strerror(errno)); exit(127); } /* Parent.. Finish up and return */ @@ -394,7 +396,7 @@ client_activity(struct client *p) /* The master process - It watches over the pty process and the attached */ /* clients. */ static void -master_process(int s, char **argv) +master_process(int s, char **argv, int waitattach) { struct client *p, *next; fd_set readfds; @@ -440,11 +442,17 @@ master_process(int s, char **argv) /* Re-initialize the file descriptor set for select. */ FD_ZERO(&readfds); FD_SET(s, &readfds); - FD_SET(the_pty.fd, &readfds); - if (s > the_pty.fd) - highest_fd = s; - else - highest_fd = the_pty.fd; + highest_fd = s; + + if (clients && clients->attached) + waitattach = 0; + if (!waitattach) + { + FD_SET(the_pty.fd, &readfds); + if (the_pty.fd > highest_fd) + highest_fd = the_pty.fd; + } + for (p = clients; p; p = p->next) { FD_SET(p->fd, &readfds); @@ -458,10 +466,8 @@ master_process(int s, char **argv) if (errno == EINTR || errno == EAGAIN) continue; exit(1); - } - /* pty activity? */ - if (FD_ISSET(the_pty.fd, &readfds)) - pty_activity(s); + } + /* New client? */ if (FD_ISSET(s, &readfds)) control_activity(s); @@ -472,11 +478,14 @@ master_process(int s, char **argv) if (FD_ISSET(p->fd, &readfds)) client_activity(p); } + /* pty activity? */ + if (FD_ISSET(the_pty.fd, &readfds)) + pty_activity(s); } } int -master_main(char **argv) +master_main(char **argv, int waitattach) { int s; pid_t pid; @@ -503,7 +512,7 @@ master_main(char **argv) else if (pid == 0) { /* Child - this becomes the master */ - master_process(s, argv); + master_process(s, argv, waitattach); return 0; } /* Parent - just return. */