Repository navigation
Add Io\Terminal native terminal primitives - #23941
prateekbhujel wants to merge 54 commits into
Conversation
TimWolla
left a comment
There was a problem hiding this comment.
I'm only remarking the first occurrence of any given point. Please carefully check if the same suggestion would apply elsewhere for everything.
DanielEScherzer
left a comment
There was a problem hiding this comment.
some initial thoughts
|
FWIW the timespec functions may be useful to extract into another utility file in the future. poll has Also, perhaps it might be useful to split the Unix/Windows backends into their own files, if it gets unwieldy. |
|
Yeah, both make sense. I kept them local for now since the terminal implementation is still fairly self-contained, but if the timespec helpers start being reused elsewhere, pulling them into a shared utility would be cleaner. Same for the platform code, if the Unix/Windows sides keep growing, splitting them out would probably make the implementation easier to maintain. Thanks for pointing it out. |
There was a problem hiding this comment.
I would drop the terminal_ from all test file names, that's redundant with the directory name.
There was a problem hiding this comment.
Done, dropped the terminal_ prefix across all test files in the directory.
| --SKIPIF-- | ||
| <?php | ||
| if (!class_exists(\Io\Terminal\Terminal::class)) die('skip Io\Terminal\Terminal not available'); | ||
| if (!class_exists(\Time\Duration::class)) die('skip Time\Duration not available'); |
There was a problem hiding this comment.
Time\Duration is guaranteed to be available. And my understanding is that the terminal class is as well.
There was a problem hiding this comment.
Removed the redundant class checks from the --SKIPIF-- sections.
| #ifdef PHP_WIN32 | ||
| typedef struct php_io_terminal_identity { | ||
| php_io_terminal_native_stream stream; | ||
| bool is_console; | ||
| bool is_non_tty_stream; | ||
| zend_ulong stream_res_id; | ||
| php_stream *php_stream_ptr; | ||
| } php_io_terminal_identity; | ||
| #else | ||
| typedef struct php_io_terminal_identity { | ||
| php_io_terminal_native_stream stream; | ||
| dev_t dev; | ||
| bool has_dev; | ||
| pid_t sid; | ||
| bool has_sid; | ||
| bool is_non_tty_stream; | ||
| zend_ulong stream_res_id; | ||
| php_stream *php_stream_ptr; | ||
| } php_io_terminal_identity; | ||
| #endif |
There was a problem hiding this comment.
For readability I would suggest to only wrap the condition around the Unix/Windows differences and keep the common fields outside of the condition.
There was a problem hiding this comment.
Cleaned up: moved the common fields outside the #ifdef PHP_WIN32 block, and did the same for the other platform conditionals in the file.
| #define PHP_IO_TERMINAL_READ_EOF (-2) | ||
| #define PHP_IO_TERMINAL_READ_ERROR (-1) | ||
| #define PHP_IO_TERMINAL_READ_TIMEOUT 0 | ||
| #define PHP_IO_TERMINAL_READ_SUCCESS 1 | ||
| #define PHP_IO_TERMINAL_READ_RESIZE 2 |
There was a problem hiding this comment.
C doesn't allow switching on function pointers (and SIG_DFL/SIG_IGN expand to pointer casts), but I simplified this to a boolean check (handler != SIG_DFL && handler != SIG_IGN).
| if (handler == NULL) { | ||
| return false; | ||
| } | ||
| if (handler == SIG_DFL) { | ||
| return false; | ||
| } | ||
| if (handler == SIG_IGN) { | ||
| return false; | ||
| } | ||
| return true; |
There was a problem hiding this comment.
switch() should be more readable.
There was a problem hiding this comment.
C doesn't allow switching on function pointers (and SIG_DFL/SIG_IGN expand to pointer casts), but I simplified this to a one-line boolean check (handler != NULL && handler != SIG_DFL && handler != SIG_IGN).
| unsigned char key; | ||
| int read_result = php_io_terminal_read_byte(fd, stream, poll_ctx, &key, timeout, true, | ||
| resize_handler_installed ? &resize_generation : NULL); | ||
| if (read_result == PHP_IO_TERMINAL_READ_RESIZE) { |
There was a problem hiding this comment.
Switched to switch (read_result).
| RETURN_THROWS(); | ||
| } | ||
|
|
||
| zend_update_property_long(php_io_terminal_terminal_size_ce, Z_OBJ_P(ZEND_THIS), "cols", sizeof("cols") - 1, cols); |
There was a problem hiding this comment.
| zend_update_property_long(php_io_terminal_terminal_size_ce, Z_OBJ_P(ZEND_THIS), "cols", sizeof("cols") - 1, cols); | |
| zend_update_property_long(php_io_terminal_terminal_size_ce, Z_OBJ_P(ZEND_THIS), "cols", strlen("cols"), cols); |
In new code prefer strlen().
There was a problem hiding this comment.
Updated to strlen(), including the exception registration in MINIT.
…cret timeouts - Fix cancellation inside readSecret() escape parsing: - Abort on Ctrl+C, Ctrl+D, Escape, Enter inside CSI and SS3 sequences via terminal restoration path. - Distinguish cancellation, EOF, internal 25ms sequence timeout (which ends sequence skipping and continues secret input), and overall secret timeout. - Discard valid CSI (e.g. arrow keys) and SS3 (e.g. F1) sequences without exiting or emitting garbage. - Enforce one monotonic read deadline in readKey(): - Cap sequence timeout and UTF-8 continuation reads by remaining overall budget. - Zero timeout executes non-blocking read without blocking wait even if sequenceTimeout is specified. - Incomplete escape sequences return raw byte strings; incomplete UTF-8 sequences retain pending bytes across calls and return null on timeout. - Adopt null for absence: - Update Terminal::getSize() return type to ?TerminalSize. - Update Terminal::readKey() return type to Key|string|null. - Keep Terminal::restoreMode() return type as bool. - Add optional overall timeout to Terminal::readSecret(?Time\Duration $timeout = null): - Returns null on timeout, '' on empty submission (immediate Enter). - Throws TerminalException on cancellation/failure. - Throws ValueError on negative duration. - Introduce mockable boundaries: - Add Io\Terminal\TerminalInterface and Io\Terminal\ModeTokenInterface. - Make native Terminal implement TerminalInterface and ModeToken implement ModeTokenInterface while remaining final. - Have native restoreMode(?ModeTokenInterface $mode = null) validate native ModeToken, rejecting foreign tokens with ValueError. - Add comprehensive regression and acceptance tests.
…m handling - Refactor public API to final Terminal and ModeToken classes under Nicolas's shared lease model - Implement explicit mode restoration via restoreMode(ModeToken $mode): void with transactional rollback - Implement bounded readiness and overall sequence deadlines for ordinary streams on POSIX and Windows - Unify byte-stream readSecret cancellation (Escape, Ctrl+C, Ctrl+D) and SS3/CSI controls across platforms - Harden CRLF lookahead across secret and line reading, preserving data CR before CRLF and at EOF - Isolate test seam hooks to debug/test builds (ZEND_DEBUG || PHP_TERMINAL_TEST_BUILD) - Add regression PHPT test suite covering fork isolation, failure injection, output flags, and streams
Drop the readLine() suppression and the EIO-to-EOF conversion from fe88929; they were broader than the CI failure required and could hide real read errors.
… raw mode ISIG checks
Mark enableRawMode() with NoDiscard so accidental disposal of the lease is visible. Document token destruction, cover the warning and explicit discard, and remove two setup calls that discarded their tokens.
…ensions() Avoid repeating the Terminal namespace in the size value object and use getDimensions() on Terminal. Also cover separate wrappers reading across a split CRLF secret delimiter.
Align the public Dimensions property and constructor parameter with the RFC terminology, and update tests.
Subclass TerminalException for EOF on readKey and readSecret while preserving readLine contracts and cancellation semantics.
d0a8440 to
c7c1ca0
Compare
Implements the Io\Terminal RFC for PHP 8.7. It adds terminal dimensions, raw-mode leases, and methods to read a key, a line, or a password on POSIX and Windows. Applications can also pass ordinary PHP streams to script input in tests.
Raw-mode leases are shared by wrappers for the same terminal. Releasing the last lease restores the settings saved by the first. Libraries that already decode their own input can use just the lease and dimensions APIs.
Dimensions exposes $columns and $rows. For key and secret reads, callers can catch EofException separately from other terminal errors. readLine() returns a final unterminated line, then null at EOF.