From 3f0b204f5a8af6d1d6c344cd0341b2083e170964 Mon Sep 17 00:00:00 2001 From: Laurent Arnoud Date: Mon, 8 Apr 2024 11:51:24 +0200 Subject: [PATCH] cli: allow to change ~/.php_history with PHP_HISTFILE Closes GH-13313 --- UPGRADING | 3 +++ ext/readline/readline_cli.c | 11 ++++---- sapi/cli/tests/017.phpt | 53 +++++++++++++++++++++++++++++-------- 3 files changed, 51 insertions(+), 16 deletions(-) diff --git a/UPGRADING b/UPGRADING index bcd82825303..b231fd7e8ef 100644 --- a/UPGRADING +++ b/UPGRADING @@ -246,6 +246,9 @@ PHP 8.4 UPGRADE NOTES . Added constant POSIX_SC_CHILD_MAX . Added constant POSIX_SC_CLK_TCK +- Readfile: + . Added ability to change .php_history path through PHP_HISTFILE env variable. + - Reflection: . ReflectionAttribute now contains a $name property to improve the debugging experience. diff --git a/ext/readline/readline_cli.c b/ext/readline/readline_cli.c index 4afe7be3f66..7c92687088f 100644 --- a/ext/readline/readline_cli.c +++ b/ext/readline/readline_cli.c @@ -617,7 +617,12 @@ static int readline_shell_run(void) /* {{{ */ } #ifndef PHP_WIN32 - history_file = tilde_expand("~/.php_history"); + const char *histfile_env_name = "PHP_HISTFILE"; + if (getenv(histfile_env_name)) { + spprintf(&history_file, MAXPATHLEN, "%s", getenv(histfile_env_name)); + } else { + spprintf(&history_file, MAXPATHLEN, "%s/.php_history", getenv("HOME")); + } #else spprintf(&history_file, MAX_PATH, "%s/.php_history", getenv("USERPROFILE")); #endif @@ -717,11 +722,7 @@ static int readline_shell_run(void) /* {{{ */ php_last_char = '\0'; } -#ifdef PHP_WIN32 efree(history_file); -#else - free(history_file); -#endif efree(code); zend_string_release_ex(prompt, 0); return EG(exit_status); diff --git a/sapi/cli/tests/017.phpt b/sapi/cli/tests/017.phpt index 81e5cc5547d..d233ebf4195 100644 --- a/sapi/cli/tests/017.phpt +++ b/sapi/cli/tests/017.phpt @@ -2,6 +2,8 @@ CLI -a and libedit --EXTENSIONS-- readline +--ENV-- +PHP_HISTFILE= --SKIPIF-- --FILE-- $code) { + echo "\n--------------\nSnippet no. $key:\n--------------\n"; + $proc = proc_open("$php $ini -a", $descriptorspec, $pipes); + fwrite($pipes[0], $code); + fclose($pipes[0]); + proc_close($proc); + } +} $codes = array(); @@ -52,17 +63,26 @@ function a_function_with_some_name() { a_function_w ); EOT; -foreach ($codes as $key => $code) { - echo "\n--------------\nSnippet no. $key:\n--------------\n"; - $proc = proc_open("$php $ini -a", $descriptorspec, $pipes); - fwrite($pipes[0], $code); - fclose($pipes[0]); - proc_close($proc); -} - +runReplCodes($codes); echo "\nDone\n"; + $dir = PHP_OS_FAMILY == 'Windows' ? getenv("USERPROFILE") : getenv("HOME"); var_dump(file_exists($dir . '/.php_history')); + +$php_history_tmp = sprintf('%s%s%s', sys_get_temp_dir(), DIRECTORY_SEPARATOR, 'php_history'); +putenv('PHP_HISTFILE=' . $php_history_tmp); +var_dump(file_exists($php_history_tmp)); + +$last[6] = << --EXPECT-- -------------- @@ -108,3 +128,14 @@ Parse error: Unmatched ')' in php shell code on line 1 Done bool(true) +bool(false) + +-------------- +Snippet no. 6: +-------------- +Interactive shell + +Hello World + +Done +bool(true)