2626#include "php_readline.h"
2727#include "readline_cli.h"
2828
29- #if HAVE_LIBREADLINE || HAVE_LIBEDIT
30-
3129#ifndef HAVE_RL_COMPLETION_MATCHES
3230#define rl_completion_matches completion_matches
3331#endif
3432
35- #ifdef HAVE_LIBEDIT
3633#include <editline/readline.h>
37- #else
38- #include <readline/readline.h>
39- #include <readline/history.h>
40- #endif
4134
4235PHP_FUNCTION (readline );
4336PHP_FUNCTION (readline_add_history );
4437PHP_FUNCTION (readline_info );
4538PHP_FUNCTION (readline_clear_history );
46- #ifndef HAVE_LIBEDIT
39+ #ifdef HAVE_HISTORY_LIST
4740PHP_FUNCTION (readline_list_history );
4841#endif
4942PHP_FUNCTION (readline_read_history );
@@ -88,7 +81,7 @@ ZEND_END_ARG_INFO()
8881ZEND_BEGIN_ARG_INFO (arginfo_readline_clear_history , 0 )
8982ZEND_END_ARG_INFO ()
9083
91- #ifndef HAVE_LIBEDIT
84+ #ifdef HAVE_HISTORY_LIST
9285ZEND_BEGIN_ARG_INFO (arginfo_readline_list_history , 0 )
9386ZEND_END_ARG_INFO ()
9487#endif
@@ -133,7 +126,7 @@ static const zend_function_entry php_readline_functions[] = {
133126 PHP_FE (readline_info , arginfo_readline_info )
134127 PHP_FE (readline_add_history , arginfo_readline_add_history )
135128 PHP_FE (readline_clear_history , arginfo_readline_clear_history )
136- #ifndef HAVE_LIBEDIT
129+ #ifdef HAVE_HISTORY_LIST
137130 PHP_FE (readline_list_history , arginfo_readline_list_history )
138131#endif
139132 PHP_FE (readline_read_history , arginfo_readline_read_history )
@@ -170,10 +163,8 @@ ZEND_GET_MODULE(readline)
170163
171164PHP_MINIT_FUNCTION (readline )
172165{
173- #if HAVE_LIBREADLINE
174- /* libedit don't need this call which set the tty in cooked mode */
175- using_history ();
176- #endif
166+ /* libedit don't need "using_history" call which set the tty in cooked mode */
167+
177168 ZVAL_UNDEF (& _readline_completion );
178169#if HAVE_RL_CALLBACK_READ_CHAR
179170 ZVAL_UNDEF (& _prepped_callback );
@@ -254,21 +245,6 @@ PHP_FUNCTION(readline_info)
254245#ifndef PHP_WIN32
255246 add_assoc_long (return_value ,"end" ,rl_end );
256247#endif
257- #ifdef HAVE_LIBREADLINE
258- add_assoc_long (return_value ,"mark" ,rl_mark );
259- add_assoc_long (return_value ,"done" ,rl_done );
260- add_assoc_long (return_value ,"pending_input" ,rl_pending_input );
261- add_assoc_string (return_value ,"prompt" ,SAFE_STRING (rl_prompt ));
262- add_assoc_string (return_value ,"terminal_name" ,(char * )SAFE_STRING (rl_terminal_name ));
263- add_assoc_str (return_value , "completion_append_character" ,
264- rl_completion_append_character == 0
265- ? ZSTR_EMPTY_ALLOC ()
266- : ZSTR_CHAR (rl_completion_append_character ));
267- add_assoc_bool (return_value ,"completion_suppress_append" ,rl_completion_suppress_append );
268- #endif
269- #if HAVE_ERASE_EMPTY_LINE
270- add_assoc_long (return_value ,"erase_empty_line" ,rl_erase_empty_line );
271- #endif
272248#ifndef PHP_WIN32
273249 add_assoc_string (return_value ,"library_version" ,(char * )SAFE_STRING (rl_library_version ));
274250#endif
@@ -289,51 +265,6 @@ PHP_FUNCTION(readline_info)
289265 } else if (!strcasecmp (what , "end" )) {
290266 RETVAL_LONG (rl_end );
291267#endif
292- #ifdef HAVE_LIBREADLINE
293- } else if (!strcasecmp (what , "mark" )) {
294- RETVAL_LONG (rl_mark );
295- } else if (!strcasecmp (what , "done" )) {
296- oldval = rl_done ;
297- if (value ) {
298- convert_to_long_ex (value );
299- rl_done = Z_LVAL_P (value );
300- }
301- RETVAL_LONG (oldval );
302- } else if (!strcasecmp (what , "pending_input" )) {
303- oldval = rl_pending_input ;
304- if (value ) {
305- convert_to_string_ex (value );
306- rl_pending_input = Z_STRVAL_P (value )[0 ];
307- }
308- RETVAL_LONG (oldval );
309- } else if (!strcasecmp (what , "prompt" )) {
310- RETVAL_STRING (SAFE_STRING (rl_prompt ));
311- } else if (!strcasecmp (what , "terminal_name" )) {
312- RETVAL_STRING ((char * )SAFE_STRING (rl_terminal_name ));
313- } else if (!strcasecmp (what , "completion_suppress_append" )) {
314- oldval = rl_completion_suppress_append ;
315- if (value ) {
316- rl_completion_suppress_append = zend_is_true (value );
317- }
318- RETVAL_BOOL (oldval );
319- } else if (!strcasecmp (what , "completion_append_character" )) {
320- oldval = rl_completion_append_character ;
321- if (value ) {
322- convert_to_string_ex (value )
323- rl_completion_append_character = (int )Z_STRVAL_P (value )[0 ];
324- }
325- RETVAL_INTERNED_STR (
326- oldval == 0 ? ZSTR_EMPTY_ALLOC () : ZSTR_CHAR (oldval ));
327- #endif
328- #if HAVE_ERASE_EMPTY_LINE
329- } else if (!strcasecmp (what , "erase_empty_line" )) {
330- oldval = rl_erase_empty_line ;
331- if (value ) {
332- convert_to_long_ex (value );
333- rl_erase_empty_line = Z_LVAL_P (value );
334- }
335- RETVAL_LONG (oldval );
336- #endif
337268#ifndef PHP_WIN32
338269 } else if (!strcasecmp (what ,"library_version" )) {
339270 RETVAL_STRING ((char * )SAFE_STRING (rl_library_version ));
@@ -383,41 +314,44 @@ PHP_FUNCTION(readline_clear_history)
383314 return ;
384315 }
385316
386- #if HAVE_LIBEDIT
387317 /* clear_history is the only function where rl_initialize
388318 is not call to ensure correct allocation */
389319 using_history ();
390- #endif
391320 clear_history ();
392321
393322 RETURN_TRUE ;
394323}
395324
396325/* }}} */
326+
327+ #ifdef HAVE_HISTORY_LIST
397328/* {{{ proto array readline_list_history(void)
398329 Lists the history */
399- #ifndef HAVE_LIBEDIT
400330PHP_FUNCTION (readline_list_history )
401331{
402332 HIST_ENTRY * * history ;
333+ HISTORY_STATE * hs ;
403334
404335 if (zend_parse_parameters_none () == FAILURE ) {
405336 return ;
406337 }
407338
339+ using_history ();
408340 history = history_list ();
409-
341+ hs = history_get_history_state ();
410342 array_init (return_value );
411343
412- if (history ) {
344+ if (history && hs ) {
413345 int i ;
414- for (i = 0 ; history [i ]; i ++ ) {
415- add_next_index_string (return_value ,history [i ]-> line );
346+ for (i = 0 ; i < hs -> length ; i ++ ) {
347+ //printf("==> %s\n", history[i]->line);
348+ add_next_index_string (return_value , history [i ]-> line );
416349 }
417350 }
418351}
419- #endif
420352/* }}} */
353+ #endif
354+
421355/* {{{ proto bool readline_read_history([string filename])
422356 Reads the history */
423357PHP_FUNCTION (readline_read_history )
@@ -638,11 +572,9 @@ PHP_FUNCTION(readline_callback_handler_remove)
638572 Ask readline to redraw the display */
639573PHP_FUNCTION (readline_redisplay )
640574{
641- #if HAVE_LIBEDIT
642575 /* seems libedit doesn't take care of rl_initialize in rl_redisplay
643576 * see bug #72538 */
644577 using_history ();
645- #endif
646578 rl_redisplay ();
647579}
648580/* }}} */
@@ -660,5 +592,3 @@ PHP_FUNCTION(readline_on_new_line)
660592
661593#endif
662594
663-
664- #endif /* HAVE_LIBREADLINE */
0 commit comments