--- asterisk.orig/asterisk.c	2003-12-06 16:08:23.000000000 -0600
+++ asterisk/asterisk.c	2003-12-08 13:03:48.000000000 -0600
@@ -167,6 +167,7 @@
 	int res;
 	int max;
 	fd_set rfds;
+	struct timeval tv, *ptv;
 	
 	if (gethostname(hostname, sizeof(hostname)))
 		strncpy(hostname, "<Unknown>", 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; i<res; i++)
+				if (buf[i] == '\b') {
+					buf[i] = '\0';
+					res = i;
+					finished = 1;
+					break;
+				}
 
 			if (!option_exec && !lastpos)
 				write(STDOUT_FILENO, "\r", 1);
 			write(STDOUT_FILENO, buf, res);
-			if ((buf[res-1] == '\n') || (buf[res-2] == '\n')) {
+			if (finished) {
 				*cp = CC_REFRESH;
 				return(1);
 			} else {
@@ -909,6 +942,7 @@
 	int retval = CC_ERROR;
 	char buf[1024];
 	int res;
+	int i, read_ptr, mark_found;
 
 	LineInfo *lf = (LineInfo *)el_line(el);
 
@@ -929,15 +963,55 @@
 	if (option_remote) {
 		snprintf(buf, sizeof(buf),"_COMMAND NUMMATCHES \"%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; i<read_ptr; i++)
+				if (buf[i] == '\b') {
+					res = i;
+					buf[i] = '\0';
+					mark_found = 1;
+					break;
+				}
+			if (mark_found)
+				break;
+		}
+		/* printf("NUM1=[%s]\n", buf); */
 		nummatches = atoi(buf);
 
 		if (nummatches > 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; i<read_ptr; i++)
+					if (buf[i] == '\b') {
+						res = i;
+						buf[i] = '\0';
+						mark_found = 1;
+						break;
+					}
+				if (mark_found)
+					break;
+			}
+			/* printf("MATCH1=[%s]\n", buf); */
 
 			matches = ast_el_strtoarr(buf);
 		} else
--- asterisk.orig/cli.c	2003-12-06 16:08:23.000000000 -0600
+++ asterisk/cli.c	2003-12-08 13:03:51.000000000 -0600
@@ -994,6 +994,7 @@
 	struct ast_cli_entry *e;
 	int x;
 	char *dup;
+	char mark[2];
 	x = AST_MAX_ARGS;
 	if ((dup = parse_args(s, &x, argv))) {
 		/* We need at least one entry, or ignore */
@@ -1009,8 +1010,12 @@
 					ast_cli(fd, e->usage);
 					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 */
+			mark[0] = '\b';
+			mark[1] = '\0';
+			write(fd, mark, 1);
 			if (e) {
 				ast_mutex_lock(&clilock);
 				e->inuse--;
