Skip to content

Commit 1b9dfc5

Browse files
committed
cli: allow to change ~/.php_history with PHP_HISTFILE
1 parent af5db45 commit 1b9dfc5

File tree

3 files changed

+55
-5
lines changed

3 files changed

+55
-5
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ config.h.in
126126
/ext/phar/phar.php
127127
/pear/install-pear-nozlib.phar
128128
/sapi/cgi/php-cgi
129+
/sapi/cli/tests/php_history
129130
/sapi/fpm/php-fpm
130131
/sapi/phpdbg/phpdbg
131132
/scripts/php-config

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);
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
--TEST--
2+
CLI -a and libedit
3+
--EXTENSIONS--
4+
readline
5+
--SKIPIF--
6+
<?php
7+
include "skipif.inc";
8+
if (readline_info('done') !== NULL) {
9+
die ("skip need readline support using libedit");
10+
}
11+
?>
12+
--FILE--
13+
<?php
14+
define("TMPDIR", __DIR__ . "/");
15+
$php_history_path = TMPDIR . "php_history";
16+
putenv('PHP_HISTFILE=' . $php_history_path);
17+
$php = getenv('TEST_PHP_EXECUTABLE_ESCAPED');
18+
$ini = getenv('TEST_PHP_EXTRA_ARGS');
19+
$descriptorspec = [['pipe', 'r'], STDOUT, STDERR];
20+
21+
$codes = array();
22+
23+
$codes[1] = <<<EOT
24+
echo 'Hello world';
25+
exit
26+
EOT;
27+
28+
foreach ($codes as $key => $code) {
29+
echo "\n--------------\nSnippet no. $key:\n--------------\n";
30+
$proc = proc_open("$php $ini -a", $descriptorspec, $pipes);
31+
fwrite($pipes[0], $code);
32+
fclose($pipes[0]);
33+
proc_close($proc);
34+
}
35+
36+
echo "\nDone\n";
37+
var_dump(file_exists($php_history_path));
38+
?>
39+
--EXPECT--
40+
--------------
41+
Snippet no. 1:
42+
--------------
43+
Interactive shell
44+
45+
Hello world
46+
47+
Done
48+
bool(true)

0 commit comments

Comments
 (0)