Index: asterisk.c =================================================================== RCS file: /usr/cvsroot/asterisk/asterisk.c,v retrieving revision 1.46 diff -u -r1.46 asterisk.c --- asterisk.c 3 Dec 2003 04:55:55 -0000 1.46 +++ asterisk.c 3 Dec 2003 18:02:38 -0000 @@ -167,6 +167,7 @@ int res; int max; fd_set rfds; + struct timeval tv, *ptv; if (gethostname(hostname, sizeof(hostname))) strncpy(hostname, "", sizeof(hostname)-1); @@ -179,11 +180,21 @@ max = con->fd; if (con->p[0] > max) max = con->p[0]; - res = ast_select(max + 1, &rfds, NULL, NULL, NULL); + if (option_exec) { + tv.tv_sec = 4; /* Wait max 4 sec for fds to become active */ + tv.tv_usec = 0; + ptv = &tv; + } else + ptv = NULL; + res = ast_select(max + 1, &rfds, NULL, NULL, ptv); if (res < 0) { ast_log(LOG_WARNING, "select returned < 0: %s\n", strerror(errno)); continue; } + if (res == 0) { + ast_log(LOG_WARNING, "Timeout on select.\n"); + break; + } if (FD_ISSET(con->fd, &rfds)) { res = read(con->fd, tmp, sizeof(tmp)); if (res < 1) { @@ -750,12 +761,14 @@ static int ast_el_read_char(EditLine *el, char *cp) { - int num_read=0; + int num_read=0; int lastpos=0; + int finished=0, i; fd_set rfds; int res; int max; char buf[512]; + struct timeval tv, *ptv; for (;;) { FD_ZERO(&rfds); @@ -765,14 +778,26 @@ FD_SET(STDIN_FILENO, &rfds); if (STDIN_FILENO > max) max = STDIN_FILENO; + ptv = NULL; + } else { + /* When we execute commands, specify a timeout waiting + * for data (results) from the Asterisk server */ + tv.tv_sec = 4; /* Wait max 4 sec for fds to become active */ + tv.tv_usec = 0; + ptv = &tv; } - res = ast_select(max+1, &rfds, NULL, NULL, NULL); + + res = ast_select(max+1, &rfds, NULL, NULL, ptv); if (res < 0) { if (errno == EINTR) continue; ast_log(LOG_ERROR, "select failed: %s\n", strerror(errno)); break; } + if (res == 0) { + ast_log(LOG_NOTICE, "Timeout on select\n"); + break; + } if (FD_ISSET(STDIN_FILENO, &rfds)) { num_read = read(STDIN_FILENO, cp, 1); @@ -789,12 +814,20 @@ quit_handler(0, 0, 0, 0); } + /* Search the buf for the end-of-data mark */ buf[res] = '\0'; + for (i = 0; ibuffer, ptr); fdprint(ast_consock, buf); - res = read(ast_consock, buf, sizeof(buf)); - buf[res] = '\0'; + mark_found = 0; + read_ptr = 0; + memset(buf, 0, sizeof(buf)); + while (1) { + res = read(ast_consock, (char *)(buf+read_ptr), sizeof(buf) - read_ptr); + if (res < 1) { + ast_log(LOG_ERROR, "Read from server failed.\n"); + return NULL; + } + read_ptr += res; + /* Remove the mark, if any */ + for (i=0; i 0) { snprintf(buf, sizeof(buf),"_COMMAND MATCHESARRAY \"%s\" \"%s\"", lf->buffer, ptr); fdprint(ast_consock, buf); - res = read(ast_consock, buf, sizeof(buf)); - buf[res] = '\0'; + mark_found = 0; + read_ptr = 0; + memset(buf, 0, sizeof(buf)); + while (1) { + res = read(ast_consock, (char *)(buf+read_ptr), sizeof(buf) - read_ptr); + if (res < 1) { + ast_log(LOG_ERROR, "Read from server failed.\n"); + return NULL; + } + read_ptr += res; + /* Remove the mark, if any */ + for (i=0; iusage); break; } - } else + } else ast_cli(fd, "No such command '%s' (type 'help' for help)\n", find_best(argv)); + /* Hack to mark the end of results */ + char mark[2] = {'\b', '\0'}; + write(fd, mark, 1); if (e) { ast_mutex_lock(&clilock); e->inuse--;