Skip to content

Commit 5706ed4

Browse files
committed
Allow overriding completion in auto_prepend_file
Currently, it's possible to override `php -a`s completion functionality to provide an alternative to the C implementation, with `readline_completion_function()`. However, that surprisingly gets overridden when called from `auto_prepend_file`, because those scripts get run before the interactive shell is started. I believe that not overriding it would be more consistent with what happens when you override the completion function **after** the interactive shell. CLI is the only built-in API that uses this. I believe MINIT and RINIT will only run once when invoked with `php -a`.
1 parent 99ee73e commit 5706ed4

File tree

3 files changed

+12
-2
lines changed

3 files changed

+12
-2
lines changed

ext/readline/readline.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ static void _readline_long_zval(zval *ret, long l)
437437
ZVAL_LONG(ret, l);
438438
}
439439

440-
static char **_readline_completion_cb(const char *text, int start, int end)
440+
char **_readline_completion_cb(const char *text, int start, int end)
441441
{
442442
zval params[3];
443443
char **matches = NULL;

ext/readline/readline_cli.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,14 @@ static int readline_shell_run(void) /* {{{ */
603603
#else
604604
spprintf(&history_file, MAX_PATH, "%s/.php_history", getenv("USERPROFILE"));
605605
#endif
606-
rl_attempted_completion_function = cli_code_completion;
606+
/* Install the default completion function for 'php -a'.
607+
*
608+
* But if readline_completion_function() was called by PHP code prior to the shell starting
609+
* (e.g. with 'php -d auto_prepend_file=prepend.php -a'),
610+
* then use that instead of PHP's default. */
611+
if (rl_attempted_completion_function != _readline_completion_cb) {
612+
rl_attempted_completion_function = cli_code_completion;
613+
}
607614
#ifndef PHP_WIN32
608615
rl_special_prefixes = "$";
609616
#endif

ext/readline/readline_cli.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,7 @@ extern PHP_MINIT_FUNCTION(cli_readline);
3434
extern PHP_MSHUTDOWN_FUNCTION(cli_readline);
3535
extern PHP_MINFO_FUNCTION(cli_readline);
3636

37+
/* Defined by ext/readline/readline.c */
38+
char **_readline_completion_cb(const char *text, int start, int end);
39+
3740
ZEND_EXTERN_MODULE_GLOBALS(cli_readline)

0 commit comments

Comments
 (0)