Skip to content

cli: allow to change ~/.php_history with PHP_HISTFILE #13964

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 1 commit 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
11 changes: 6 additions & 5 deletions ext/readline/readline_cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,12 @@ static int readline_shell_run(void) /* {{{ */
}

#ifndef PHP_WIN32
history_file = tilde_expand("~/.php_history");
#define PHP_HISTFILE_ENV "PHP_HISTFILE"
if (getenv(PHP_HISTFILE_ENV)) {
spprintf(&history_file, MAXPATHLEN, "%s", getenv(PHP_HISTFILE_ENV));
} else {
spprintf(&history_file, MAXPATHLEN, "%s/.php_history", getenv("HOME"));
}
#else
spprintf(&history_file, MAX_PATH, "%s/.php_history", getenv("USERPROFILE"));
#endif
Expand Down Expand Up @@ -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);
Expand Down
51 changes: 40 additions & 11 deletions sapi/cli/tests/017.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,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();

Expand Down Expand Up @@ -52,17 +61,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--
--------------
Expand Down Expand Up @@ -108,3 +126,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)