Skip to content

add readline_list_history with libedit >= 3.1 #3824

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions ext/readline/config.m4
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions ext/readline/config.w32
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down
45 changes: 38 additions & 7 deletions ext/readline/readline.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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;
Expand All @@ -388,19 +389,49 @@ 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();
hs = history_get_history_state();
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);
}
}
}
}

#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)
Expand Down
4 changes: 2 additions & 2 deletions ext/readline/tests/readline_read_history_001.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ readline_read_history(): Basic test
--FILE--
<?php

$name = tempnam('/tmp', 'readline.tmp');
$name = tempnam(sys_get_temp_dir(), 'readline.tmp');

readline_add_history("foo\n");
readline_add_history("foo");

var_dump(readline_write_history($name));

Expand Down
2 changes: 0 additions & 2 deletions ext/readline/tests/readline_read_history_error_001.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ Pedro Manoel Evangelista <pedro.evangelista at gmail dot com>
<?php if (!extension_loaded("readline") || !function_exists('readline_read_history') || READLINE_LIB != "libedit") die("skip"); ?>
--FILE--
<?php
var_dump(readline_read_history());
var_dump(readline_read_history('nofile'));
?>
--EXPECT--
bool(false)
bool(false)