From 5136141dbe3cf492dffdd601702f64cf80e7a225 Mon Sep 17 00:00:00 2001 From: Remi Collet Date: Wed, 13 Feb 2019 16:00:22 +0100 Subject: [PATCH 1/3] add readline_list_history with libedit >= 3.1 and mingweditline --- ext/readline/config.m4 | 8 ++++++++ ext/readline/config.w32 | 1 + ext/readline/readline.c | 44 ++++++++++++++++++++++++++++++++++------- 3 files changed, 46 insertions(+), 7 deletions(-) diff --git a/ext/readline/config.m4 b/ext/readline/config.m4 index 3995bd7587f5a..2e0bb5c503a33 100644 --- a/ext/readline/config.m4 +++ b/ext/readline/config.m4 @@ -74,6 +74,7 @@ if test "$PHP_READLINE" && test "$PHP_READLINE" != "no"; then -L$READLINE_DIR/$PHP_LIBDIR $PHP_READLINE_LIBS ]) + AC_DEFINE(HAVE_HISTORY_LIST, 1, [ ]) AC_DEFINE(HAVE_LIBREADLINE, 1, [ ]) elif test "$PHP_LIBEDIT" != "no"; then @@ -128,6 +129,13 @@ elif test "$PHP_LIBEDIT" != "no"; then -L$READLINE_DIR/$PHP_LIBDIR $PHP_READLINE_LIBS ]) + PHP_CHECK_LIBRARY(edit, history_list, + [ + AC_DEFINE(HAVE_HISTORY_LIST, 1, [ ]) + ],[],[ + -L$READLINE_DIR/$PHP_LIBDIR $PHP_READLINE_LIBS + ]) + AC_DEFINE(HAVE_LIBEDIT, 1, [ ]) fi diff --git a/ext/readline/config.w32 b/ext/readline/config.w32 index 9e21e6bb54051..640f2e31077ad 100644 --- a/ext/readline/config.w32 +++ b/ext/readline/config.w32 @@ -9,6 +9,7 @@ if (PHP_READLINE != "no") { EXTENSION("readline", "readline.c readline_cli.c"); ADD_FLAG("CFLAGS_READLINE", "/D HAVE_LIBEDIT"); ADD_FLAG("CFLAGS_READLINE", "/D HAVE_RL_COMPLETION_MATCHES"); + ADD_FLAG("CFLAGS_READLINE", "/D HAVE_HISTORY_LIST"); } else { WARNING("readline not enabled; libraries and headers not found"); } diff --git a/ext/readline/readline.c b/ext/readline/readline.c index bd443ebcd6a2a..da10fd1903a95 100644 --- a/ext/readline/readline.c +++ b/ext/readline/readline.c @@ -45,7 +45,7 @@ PHP_FUNCTION(readline); PHP_FUNCTION(readline_add_history); PHP_FUNCTION(readline_info); PHP_FUNCTION(readline_clear_history); -#ifndef HAVE_LIBEDIT +#ifdef HAVE_HISTORY_LIST PHP_FUNCTION(readline_list_history); #endif PHP_FUNCTION(readline_read_history); @@ -90,7 +90,7 @@ ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO(arginfo_readline_clear_history, 0) ZEND_END_ARG_INFO() -#ifndef HAVE_LIBEDIT +#ifdef HAVE_HISTORY_LIST ZEND_BEGIN_ARG_INFO(arginfo_readline_list_history, 0) ZEND_END_ARG_INFO() #endif @@ -135,7 +135,7 @@ static const zend_function_entry php_readline_functions[] = { PHP_FE(readline_info, arginfo_readline_info) PHP_FE(readline_add_history, arginfo_readline_add_history) PHP_FE(readline_clear_history, arginfo_readline_clear_history) -#ifndef HAVE_LIBEDIT +#ifdef HAVE_HISTORY_LIST PHP_FE(readline_list_history, arginfo_readline_list_history) #endif PHP_FE(readline_read_history, arginfo_readline_read_history) @@ -377,9 +377,10 @@ PHP_FUNCTION(readline_clear_history) } /* }}} */ + +#ifdef HAVE_HISTORY_LIST /* {{{ proto array readline_list_history(void) Lists the history */ -#ifndef HAVE_LIBEDIT PHP_FUNCTION(readline_list_history) { HIST_ENTRY **history; @@ -388,19 +389,48 @@ PHP_FUNCTION(readline_list_history) return; } + array_init(return_value); + +#if defined(HAVE_LIBEDIT) && defined(PHP_WIN32) /* Winedit on Windows */ history = history_list(); - array_init(return_value); + if (history) { + int i, n = history_length(); + for (i = 0; i < n; i++) { + add_next_index_string(return_value, history[i]->line); + } + } + +#elif defined(HAVE_LIBEDIT) /* libedit */ + { + HISTORY_STATE *hs; + int i; + + using_history(); + history = history_list(); + hs = history_get_history_state(); + + if (history && hs) { + for (i = 0; i < hs->length; i++) { + add_next_index_string(return_value, history[i]->line); + } + } + } + +#else /* readline */ + history = history_list(); if (history) { int i; for (i = 0; history[i]; i++) { - add_next_index_string(return_value,history[i]->line); + add_next_index_string(return_value, history[i]->line); } } -} #endif +} /* }}} */ +#endif + /* {{{ proto bool readline_read_history([string filename]) Reads the history */ PHP_FUNCTION(readline_read_history) From c77348ca51e8a78d6a077630b734209efafecc63 Mon Sep 17 00:00:00 2001 From: Remi Collet Date: Thu, 14 Feb 2019 15:30:21 +0100 Subject: [PATCH 2/3] sanity check --- ext/readline/readline.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/ext/readline/readline.c b/ext/readline/readline.c index da10fd1903a95..d61f8019b23a9 100644 --- a/ext/readline/readline.c +++ b/ext/readline/readline.c @@ -407,12 +407,13 @@ PHP_FUNCTION(readline_list_history) int i; using_history(); - history = history_list(); hs = history_get_history_state(); - - if (history && hs) { - for (i = 0; i < hs->length; i++) { - add_next_index_string(return_value, history[i]->line); + if (hs && hs->length) { + history = history_list(); + if (history) { + for (i = 0; i < hs->length; i++) { + add_next_index_string(return_value, history[i]->line); + } } } } From 66a9e12cec9d9f9325e82878c23e9946d701ac94 Mon Sep 17 00:00:00 2001 From: Remi Collet Date: Thu, 14 Feb 2019 16:54:04 +0100 Subject: [PATCH 3/3] fix test for windows (sys_get_temp_dir) fix test for libedit which preserve \n fix error test as default file may exists --- ext/readline/tests/readline_read_history_001.phpt | 4 ++-- ext/readline/tests/readline_read_history_error_001.phpt | 2 -- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/ext/readline/tests/readline_read_history_001.phpt b/ext/readline/tests/readline_read_history_001.phpt index fcdb1ae325e83..6cabaf369b727 100644 --- a/ext/readline/tests/readline_read_history_001.phpt +++ b/ext/readline/tests/readline_read_history_001.phpt @@ -5,9 +5,9 @@ readline_read_history(): Basic test --FILE-- --FILE-- --EXPECT-- bool(false) -bool(false)