[php-src] master: cli: allow to change ~/.php_history with PHP_HISTFILE

From: Date: Mon, 22 Apr 2024 08:09:13 +0000
Subject: [php-src] master: cli: allow to change ~/.php_history with PHP_HISTFILE
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-128202@lists.php.net to get a copy of this message
Author: Laurent Arnoud (spk) Committer: Ilija Tovilo (iluuu1994) Date: 2024-04-22T10:05:47+02:00 Commit: https://github.com/php/php-src/commit/3f0b204f5a8af6d1d6c344cd0341b2083e170964 Raw diff: https://github.com/php/php-src/commit/3f0b204f5a8af6d1d6c344cd0341b2083e170964.diff cli: allow to change ~/.php_history with PHP_HISTFILE Closes GH-13313 Changed paths: M UPGRADING M ext/readline/readline_cli.c M sapi/cli/tests/017.phpt Diff: diff --git a/UPGRADING b/UPGRADING index bcd8282530336..b231fd7e8ef0d 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 4afe7be3f667c..7c92687088f85 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 81e5cc5547d5a..d233ebf41955a 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-- <?php include "skipif.inc"; @@ -11,9 +13,18 @@ if (readline_info('done') !== NULL) { ?> --FILE-- <?php -$php = getenv('TEST_PHP_EXECUTABLE_ESCAPED'); -$ini = getenv('TEST_PHP_EXTRA_ARGS'); -$descriptorspec = [['pipe', 'r'], STDOUT, STDERR]; +function runReplCodes($codes) { + $php = getenv('TEST_PHP_EXECUTABLE_ESCAPED'); + $ini = getenv('TEST_PHP_EXTRA_ARGS'); + $descriptorspec = [['pipe', 'r'], STDOUT, STDERR]; + 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); + } +} $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] = <<<EOT +echo 'Hello World'; +exit +EOT; +runReplCodes($last); +echo "\nDone\n"; + +$php_history_path = PHP_OS_FAMILY == 'Windows' ? getenv("USERPROFILE") : $php_history_tmp; +var_dump(file_exists($php_history_path)); +@unlink($php_history_tmp); ?> --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)

« previous php.cvs (#128202) next »