From db129989986a532dedc1f1496ad5be312bb8f7a5 Mon Sep 17 00:00:00 2001
From: krakjoe <joe.watkins@live.co.uk>
Date: Wed, 27 Nov 2013 17:32:15 +0000
Subject: [PATCH] fix remote mode when readline is enabled

---
 phpdbg.c       | 20 +++++++++-----------
 phpdbg_cmd.c   | 23 +++++++++++++++++------
 phpdbg_utils.c |  2 +-
 3 files changed, 27 insertions(+), 18 deletions(-)

diff --git a/phpdbg.c b/phpdbg.c
index 687237b90a..d536f9d801 100644
--- a/phpdbg.c
+++ b/phpdbg.c
@@ -589,11 +589,11 @@ int phpdbg_open_socket(short port) /* {{{ */
 	return fd;
 } /* }}} */
 
-int phpdbg_open_sockets(short listen[2], FILE* streams[2]) /* {{{ */
+int phpdbg_open_sockets(int listen[2], FILE* streams[2]) /* {{{ */
 {
 	int sockets[2] = {
-		phpdbg_open_socket(listen[0]),
-		phpdbg_open_socket(listen[1])
+		phpdbg_open_socket((short)listen[0]),
+		phpdbg_open_socket((short)listen[1])
 	};
 	int accepted[2] = {-1, -1};
 	
@@ -614,11 +614,11 @@ int phpdbg_open_sockets(short listen[2], FILE* streams[2]) /* {{{ */
 
         memset(&address, 0, size);
         accepted[0] = accept(
-        	sockets[0], &address, &size);
+        	sockets[0], (struct sockaddr *) &address, &size);
         
         memset(&address, 0, size);
         accepted[1] = accept(
-        	sockets[1], &address, &size);
+        	sockets[1], (struct sockaddr *) &address, &size);
 	}
 	
 	streams[0] = fdopen(accepted[0], "r");
@@ -648,7 +648,7 @@ int main(int argc, char **argv) /* {{{ */
 	int run = 0;
 	int step = 0;
 	char *bp_tmp_file;
-	short listen[2];
+	int listen[2];
 	FILE* streams[2] = {NULL, NULL};
 
 #ifdef ZTS
@@ -803,7 +803,6 @@ phpdbg_main:
 	if (!cleaning && 
 		(listen[0] && listen[1])) {
 		phpdbg_open_sockets(listen, streams);
-		/* now is a sensible time to announce listen settings on the console */
 	}
 	
 	phpdbg->ini_defaults = phpdbg_ini_defaults;
@@ -842,6 +841,9 @@ phpdbg_main:
 #endif
 
 		PG(modules_activated) = 0;
+		
+		/* set flags from command line */
+		PHPDBG_G(flags) = flags;
 
 		/* setup io here */
 		if (streams[0] && streams[1]) {
@@ -850,7 +852,6 @@ phpdbg_main:
 			PHPDBG_G(io)[PHPDBG_STDIN] = streams[0];
 			PHPDBG_G(io)[PHPDBG_STDOUT] = streams[1];
 			PHPDBG_G(io)[PHPDBG_STDERR] = stderr;
-			
 			signal(SIGPIPE, SIG_IGN);
 		} else {
 			/* local console */
@@ -876,9 +877,6 @@ phpdbg_main:
 			free(oplog_file);
 		}
 
-		/* set flags from command line */
-		PHPDBG_G(flags) = flags;
-
 		/* set default colors */
 		phpdbg_set_color_ex(PHPDBG_COLOR_PROMPT,  PHPDBG_STRL("white-bold") TSRMLS_CC);
 		phpdbg_set_color_ex(PHPDBG_COLOR_ERROR,   PHPDBG_STRL("red-bold") TSRMLS_CC);
diff --git a/phpdbg_cmd.c b/phpdbg_cmd.c
index 27641a695e..c4ba06e5db 100644
--- a/phpdbg_cmd.c
+++ b/phpdbg_cmd.c
@@ -220,11 +220,13 @@ PHPDBG_API phpdbg_input_t *phpdbg_read_input(char *buffered TSRMLS_DC) /* {{{ */
 	if (!(PHPDBG_G(flags) & PHPDBG_IS_QUITTING)) {
 		if (buffered == NULL) {
 		
-			fflush(PHPDBG_G(io)[PHPDBG_STDOUT]);
+			if ((PHPDBG_G(flags) & PHPDBG_IS_REMOTE)) {	
+				fflush(PHPDBG_G(io)[PHPDBG_STDOUT]);
+			}
 			
 #ifndef HAVE_LIBREADLINE
 			char buf[PHPDBG_MAX_CMD];
-			if (!phpdbg_write(phpdbg_get_prompt(TSRMLS_C)) ||
+			if ((!(PHPDBG_G(flags) & PHPDBG_IS_REMOTE) && !phpdbg_write(phpdbg_get_prompt(TSRMLS_C))) ||
 				!fgets(buf, PHPDBG_MAX_CMD, PHPDBG_G(io)[PHPDBG_STDIN])) {
 				/* the user has gone away */
 				phpdbg_error("Failed to read console !");
@@ -232,10 +234,16 @@ PHPDBG_API phpdbg_input_t *phpdbg_read_input(char *buffered TSRMLS_DC) /* {{{ */
 				zend_bailout();
 				return NULL;
 			}
-
+			
 			cmd = buf;
 #else
-			cmd = readline(phpdbg_get_prompt(TSRMLS_C));
+			if ((PHPDBG_G(flags) & PHPDBG_IS_REMOTE)) {
+				char buf[PHPDBG_MAX_CMD];
+				if (fgets(buf, PHPDBG_MAX_CMD, PHPDBG_G(io)[PHPDBG_STDIN])) {
+					cmd = buf;
+				} else cmd = NULL;
+			} else cmd = readline(phpdbg_get_prompt(TSRMLS_C));
+			
 			if (!cmd) {
 				/* the user has gone away */
 				phpdbg_error("Failed to read console !");
@@ -244,7 +252,9 @@ PHPDBG_API phpdbg_input_t *phpdbg_read_input(char *buffered TSRMLS_DC) /* {{{ */
 				return NULL;
 			}
 
-			add_history(cmd);
+			if (!(PHPDBG_G(flags) & PHPDBG_IS_REMOTE)) {
+				add_history(cmd);
+			}
 #endif
 		} else cmd = buffered;
 
@@ -275,7 +285,8 @@ PHPDBG_API phpdbg_input_t *phpdbg_read_input(char *buffered TSRMLS_DC) /* {{{ */
 #endif
 
 #ifdef HAVE_LIBREADLINE
-		if (!buffered && cmd) {
+		if (!buffered && cmd && 
+			!(PHPDBG_G(flags) & PHPDBG_IS_REMOTE)) {
 			free(cmd);
 		}
 #endif
diff --git a/phpdbg_utils.c b/phpdbg_utils.c
index f18bbb55fa..33ead6034c 100644
--- a/phpdbg_utils.c
+++ b/phpdbg_utils.c
@@ -299,7 +299,7 @@ PHPDBG_API void phpdbg_set_prompt(const char *prompt TSRMLS_DC) /* {{{ */
 } /* }}} */
 
 PHPDBG_API const char *phpdbg_get_prompt(TSRMLS_D) /* {{{ */
-{
+{	
 	/* find cached prompt */
 	if (PHPDBG_G(prompt)[1]) {
 		return PHPDBG_G(prompt)[1];
-- 
2.40.0