Skip to content

Allow overriding readline completion in auto_prepend_file #5872

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

Conversation

TysonAndre
Copy link
Contributor

Currently, it's possible to override php -as 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.

@TysonAndre TysonAndre changed the title Allow overriding completion in auto_prepend_file Allow overriding readline completion in auto_prepend_file Jul 18, 2020
@TysonAndre TysonAndre force-pushed the readline-auto_prepend_file branch from 1b35ba4 to 5706ed4 Compare July 29, 2020 04:04
@TysonAndre
Copy link
Contributor Author

@cmb69 @nikic - I see you've contributed to readline, and it looks like readline hasn't been changed substantially since 2011 or so (e.g. 618b480 changed the handling of the readline callback)

I'd consider the current behavior of readline in auto_prepend_file to be a bug (see PR description and https://externals.io/message/111073)

What would you suggest for me to do to include changes to readline in php 8.0/8.1?

@cmb69
Copy link
Member

cmb69 commented Jul 29, 2020

This patch looks good to me; I suggest to merge it, if nobody objects.

However, it seems to me that rl_attempted_completion_function should be a module global instead of a true global.

@TysonAndre
Copy link
Contributor Author

However, it seems to me that rl_attempted_completion_function should be a module global instead of a true global.

  1. Readline is only ever used from an interactive CLI process, as far as I can tell. I can't imagine it being used in threaded mode, and anyone unexpectedly attempting to use it in embed (doubt it) probably got it working already

  2. More importantly, rl_attempted_completion_function is a C static variable defined by the readline C library used by php and linked against. http://web.mit.edu/gnu/doc/html/rlman_2.html mentions that C code must override that function pointer.

    So any attempt to make this safe in threaded mode would likely fail, and setting up mutexes would be something I'd consider unexpected overkill because of the way readline is usually used (e.g. no independent threaded php processes).

@johannes
Copy link
Member

Correct rl_attempted_completion_function is exported by the deadline library, not PHP.

Please not my note above about _readline_completion_cb. This should get a php prefix or similar.

For some context: This indeed should only be used in CLI SAPI. Historically the code lived in SAPI/cli but since many distributions build readline as shared. Therefore I split that code partly into ext/readline so this can dynamically be loaded. With other SAPIs this is disabled. (I remember some weirdness around cgi, as CLI was created out of CGI, but even there it should be disabled)

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 (See discussion in phpGH-5872).
I believe MINIT and RINIT will only run once when invoked with `php -a`.

Add documentation about the architecture of how php uses readline/libedit
@TysonAndre TysonAndre force-pushed the readline-auto_prepend_file branch from 5706ed4 to 0a2412b Compare July 29, 2020 19:13
@cmb69
Copy link
Member

cmb69 commented Jul 30, 2020

Thanks for explanaining! :)

@TysonAndre
Copy link
Contributor Author

Please not my note above about _readline_completion_cb. This should get a php prefix or similar.

Renamed to `php_readline_completion_cb and added a README with the information mentioned here. This will get merged if nobody objects. Missing information in the README could get amended in future PRs/commits.

@php-pulls php-pulls closed this in 97f10fc Aug 1, 2020
@TysonAndre
Copy link
Contributor Author

This patch looks good to me; I suggest to merge it, if nobody objects.

Merged and added a note to UPGRADING, this can be amended if there are issues with the wording or the section.

@carusogabriel carusogabriel added this to the PHP 8.0 milestone Aug 1, 2020
@TysonAndre TysonAndre deleted the readline-auto_prepend_file branch December 20, 2020 22:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants