Skip to content

Commit 609429d

Browse files
committed
cli: allow to change ~/.php_history with PHP_HISTFILE
1 parent 7192379 commit 609429d

File tree

2 files changed

+46
-16
lines changed

2 files changed

+46
-16
lines changed

ext/readline/readline_cli.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -617,7 +617,12 @@ static int readline_shell_run(void) /* {{{ */
617617
}
618618

619619
#ifndef PHP_WIN32
620-
history_file = tilde_expand("~/.php_history");
620+
#define PHP_HISTFILE_ENV "PHP_HISTFILE"
621+
if (getenv(PHP_HISTFILE_ENV)) {
622+
spprintf(&history_file, MAXPATHLEN, "%s", getenv(PHP_HISTFILE_ENV));
623+
} else {
624+
spprintf(&history_file, MAXPATHLEN, "%s/.php_history", getenv("HOME"));
625+
}
621626
#else
622627
spprintf(&history_file, MAX_PATH, "%s/.php_history", getenv("USERPROFILE"));
623628
#endif
@@ -717,11 +722,7 @@ static int readline_shell_run(void) /* {{{ */
717722

718723
php_last_char = '\0';
719724
}
720-
#ifdef PHP_WIN32
721725
efree(history_file);
722-
#else
723-
free(history_file);
724-
#endif
725726
efree(code);
726727
zend_string_release_ex(prompt, 0);
727728
return EG(exit_status);

sapi/cli/tests/017.phpt

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,18 @@ if (readline_info('done') !== NULL) {
1111
?>
1212
--FILE--
1313
<?php
14-
$php = getenv('TEST_PHP_EXECUTABLE_ESCAPED');
15-
$ini = getenv('TEST_PHP_EXTRA_ARGS');
16-
$descriptorspec = [['pipe', 'r'], STDOUT, STDERR];
14+
function runReplCodes($codes) {
15+
$php = getenv('TEST_PHP_EXECUTABLE_ESCAPED');
16+
$ini = getenv('TEST_PHP_EXTRA_ARGS');
17+
$descriptorspec = [['pipe', 'r'], STDOUT, STDERR];
18+
foreach ($codes as $key => $code) {
19+
echo "\n--------------\nSnippet no. $key:\n--------------\n";
20+
$proc = proc_open("$php $ini -a", $descriptorspec, $pipes);
21+
fwrite($pipes[0], $code);
22+
fclose($pipes[0]);
23+
proc_close($proc);
24+
}
25+
}
1726

1827
$codes = array();
1928

@@ -52,17 +61,26 @@ function a_function_with_some_name() {
5261
a_function_w );
5362
EOT;
5463

55-
foreach ($codes as $key => $code) {
56-
echo "\n--------------\nSnippet no. $key:\n--------------\n";
57-
$proc = proc_open("$php $ini -a", $descriptorspec, $pipes);
58-
fwrite($pipes[0], $code);
59-
fclose($pipes[0]);
60-
proc_close($proc);
61-
}
62-
64+
runReplCodes($codes);
6365
echo "\nDone\n";
66+
6467
$dir = PHP_OS_FAMILY == 'Windows' ? getenv("USERPROFILE") : getenv("HOME");
6568
var_dump(file_exists($dir . '/.php_history'));
69+
70+
$php_history_tmp = sprintf('%s%s%s', sys_get_temp_dir(), DIRECTORY_SEPARATOR, 'php_history');
71+
putenv('PHP_HISTFILE=' . $php_history_tmp);
72+
var_dump(file_exists($php_history_tmp));
73+
74+
$last[6] = <<<EOT
75+
echo 'Hello World';
76+
exit
77+
EOT;
78+
runReplCodes($last);
79+
echo "\nDone\n";
80+
81+
$php_history_path = PHP_OS_FAMILY == 'Windows' ? getenv("USERPROFILE") : $php_history_tmp;
82+
var_dump(file_exists($php_history_path));
83+
@unlink($php_history_tmp);
6684
?>
6785
--EXPECT--
6886
--------------
@@ -108,3 +126,14 @@ Parse error: Unmatched ')' in php shell code on line 1
108126

109127
Done
110128
bool(true)
129+
bool(false)
130+
131+
--------------
132+
Snippet no. 6:
133+
--------------
134+
Interactive shell
135+
136+
Hello World
137+
138+
Done
139+
bool(true)

0 commit comments

Comments
 (0)