Skip to content

Commit 055fd8a

Browse files
committed
cli: allow to change ~/.php_history with PHP_HISTFILE
1 parent 58c0202 commit 055fd8a

File tree

4 files changed

+48
-17
lines changed

4 files changed

+48
-17
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

build/Makefile.global

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ distclean: clean
129129
rm -f Makefile config.cache config.log config.status Makefile.objects Makefile.fragments libtool main/php_config.h main/internal_functions_cli.c main/internal_functions.c Zend/zend_dtrace_gen.h Zend/zend_dtrace_gen.h.bak Zend/zend_config.h
130130
rm -f main/build-defs.h scripts/phpize
131131
rm -f ext/date/lib/timelib_config.h ext/mbstring/libmbfl/config.h
132-
rm -f scripts/man1/phpize.1 scripts/php-config scripts/man1/php-config.1 sapi/cli/php.1 sapi/cgi/php-cgi.1 sapi/phpdbg/phpdbg.1 ext/phar/phar.1 ext/phar/phar.phar.1
132+
rm -f scripts/man1/phpize.1 scripts/php-config scripts/man1/php-config.1 sapi/cli/php.1 sapi/cli/tests/php_history sapi/cgi/php-cgi.1 sapi/phpdbg/phpdbg.1 ext/phar/phar.1 ext/phar/phar.phar.1
133133
rm -f sapi/fpm/php-fpm.conf sapi/fpm/init.d.php-fpm sapi/fpm/php-fpm.service sapi/fpm/php-fpm.8 sapi/fpm/status.html
134134
rm -f ext/phar/phar.phar ext/phar/phar.php
135135
if test "$(srcdir)" != "$(builddir)"; then \

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+
define("TMPDIR", __DIR__ . "/");
71+
$php_history_tmp = TMPDIR . "php_history";
72+
$php_history_path = PHP_OS_FAMILY == 'Windows' ? getenv("USERPROFILE") : $php_history_tmp;
73+
putenv('PHP_HISTFILE=' . $php_history_path);
74+
var_dump(file_exists($php_history_path));
75+
76+
$last[6] = <<<EOT
77+
echo 'Hello world';
78+
exit
79+
EOT;
80+
runReplCodes($last);
81+
echo "\nDone\n";
82+
83+
var_dump(file_exists($php_history_path));
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)