--- asterisk.c.ORIG Thu Jul 1 22:21:25 2004 +++ asterisk.c Fri Jul 2 22:37:41 2004 @@ -844,8 +844,19 @@ struct pollfd fds[2]; int res; int max; + int tmout = -1; char buf[512]; + /* + * If we're doing a -x (execute command from shell), then make + * the poll timeout 2 seconds (arbitrarily chosen) so if the remote + * server doesn't tell us nicely when a command is done, + * we can bail out gracefully. + */ + + if (option_exec) + tmout = 2; + for (;;) { max = 1; fds[0].fd = ast_consock; @@ -855,7 +866,7 @@ fds[1].events = POLLIN; max++; } - res = poll(fds, max, -1); + res = poll(fds, max, tmout); if (res < 0) { if (errno == EINTR) continue; @@ -863,6 +874,10 @@ break; } + /* timed out in shell mode...done with output */ + if (option_exec && res == 0) + break; + if (!option_exec && fds[1].revents) { num_read = read(STDIN_FILENO, cp, 1); if (num_read < 1) { @@ -898,16 +913,34 @@ } } - buf[res] = '\0'; + if (res > 0) { + /* we have something from the remote server */ + buf[res] = '\0'; - if (!option_exec && !lastpos) - write(STDOUT_FILENO, "\r", 1); - write(STDOUT_FILENO, buf, res); - if ((buf[res-1] == '\n') || (buf[res-2] == '\n')) { - *cp = CC_REFRESH; - return(1); - } else { - lastpos = 1; + /* overwrite the prompt if first time through */ + if (!option_exec && !lastpos) { + lastpos = 1; + write(STDOUT_FILENO, "\r", 1); + } + + write(STDOUT_FILENO, buf, res); + + /* unless this is option_exec where we'd + * like to keep reading until there's nothing + * more to read, we'll return and refresh + * the prompt if the buffer ends with a + * newline. This provides faster interactive + * response than a timeout on poll would, and + * if we didn't finish reading everything, + * then the poll will indicate more to read + * next time through... + */ + if (!option_exec && + ((buf[res-1] == '\n') || + (res > 1 && buf[res-2] == '\n'))) { + *cp = CC_REFRESH; + return(1); + } } } }