-
Notifications
You must be signed in to change notification settings - Fork 7.8k
Implement stream_vt100_support user function #2103
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
Closed
Changes from all commits
Commits
Show all changes
41 commits
Select commit
Hold shift + click to select a range
a0e0d8d
Add VT100 support for Windows
mlocati 18c5c42
Fix function names prefix
mlocati be50062
Use Unicode version of GetFinalPathNameByHandle
mlocati e11f3d2
Use EG(windows_version_info) instead of RtlGetVersion
mlocati 6953d2f
Use the specified handle_id instead of STD_OUTPUT_HANDLE
mlocati bd93781
Switch from stream name to stream resource
mlocati 35bf19a
Allow running tests capturing only stdout and/or stderr
mlocati d9b1c99
Add tests for stream_vt100_support function
mlocati 2f05643
Export Win32 console functions
mlocati 694e207
Fix x64 build
mlocati 4f4ffd0
Use zend_long instead of long long, use GetConsole instead of GetFina…
mlocati 3fcdc64
Always use zend_long on any platform
mlocati 99bd01e
Use _get_osfhandle to determine the standard handle
mlocati 7b155f4
Accept stream names
mlocati 08e75a2
Raise warnings in case of invalid stream parameter
mlocati c10467e
Return true if disabling VT100 support on a not-console/redirected st…
mlocati dd85825
Remove php_win32_console_os_supports_vt100
mlocati a6e88f6
Differentiate stdin vs stdout/stderr
mlocati 1643715
Simplify setting flag
mlocati f86567e
Allow avoid piping STDIN
mlocati 658ff84
Let stream_vt100_support accept only resources
mlocati d9e0531
Fix run-tests
mlocati 157f8e8
Revert console flags in case of failure
mlocati 76411a4
Simplify logic of stream_vt100_support when setting the flag
mlocati 4d956f2
Drop support for STDIN
mlocati 7d348a0
More comprehensive tests for stream_vt100_support
mlocati 4588b50
Remove old tests
mlocati bf60a78
Fix name of included file and use absolute paths
mlocati 5d2ecf9
Enable ENABLE_VIRTUAL_TERMINAL_PROCESSING on Windows by default
mlocati 7581767
Remove tests for stream_vt100_support
mlocati d15063c
Split stream_vt100_support into stream_isatty+sapi_windows_vt100_support
mlocati 65ad779
Add tests for stream_isatty
mlocati 67e1eb7
Add tests for sapi_windows_vt100_support
mlocati f9ff470
Return null from stream_isatty is neither Windows nor Posix
mlocati 6df8c3c
Fallback to S_ISCHR if neither Windows nor Posix
mlocati 1081aa8
Avoid defining argc since it's only used once
mlocati e4aaa27
Better comment about php_win32_console_fileno_is_console
mlocati 2d062f3
Use events instead of cNumberOfEvents
mlocati d70a7f2
Do not restore previous console mode
mlocati c2988ea
Merge master into vt100-windows
mlocati d1772e6
Don't configure STDOUT/STDERR on Windows with PHP_CLI_WIN32_NO_CONSOLE
mlocati File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,6 +37,7 @@ typedef unsigned long long php_timeout_ull; | |
#else | ||
#include "win32/select.h" | ||
#include "win32/sockets.h" | ||
#include "win32/console.h" | ||
typedef unsigned __int64 php_timeout_ull; | ||
#endif | ||
|
||
|
@@ -1569,6 +1570,119 @@ PHP_FUNCTION(stream_supports_lock) | |
RETURN_TRUE; | ||
} | ||
|
||
/* {{{ proto proto stream_isatty(resource stream) | ||
Check if a stream is a TTY. | ||
*/ | ||
PHP_FUNCTION(stream_isatty) | ||
{ | ||
zval *zsrc; | ||
php_stream *stream; | ||
zend_long fileno; | ||
|
||
if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &zsrc) == FAILURE) { | ||
RETURN_FALSE; | ||
} | ||
|
||
php_stream_from_zval(stream, zsrc); | ||
|
||
if (php_stream_can_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT) == SUCCESS) { | ||
php_stream_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT, (void*)&fileno, 0); | ||
} | ||
else if (php_stream_can_cast(stream, PHP_STREAM_AS_FD) == SUCCESS) { | ||
php_stream_cast(stream, PHP_STREAM_AS_FD, (void*)&fileno, 0); | ||
} | ||
else { | ||
RETURN_FALSE; | ||
} | ||
|
||
#ifdef PHP_WIN32 | ||
/* Check if the Windows standard handle is redirected to file */ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. May be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Much better: e4aaa27 |
||
if (php_win32_console_fileno_is_console(fileno)) { | ||
RETURN_TRUE; | ||
} | ||
else { | ||
RETURN_FALSE; | ||
} | ||
#elif HAVE_POSIX | ||
/* Check if the file descriptor identifier is a terminal */ | ||
if (isatty(fileno)) { | ||
RETURN_TRUE; | ||
} | ||
else { | ||
RETURN_FALSE; | ||
} | ||
#else | ||
zend_stat_t stat; | ||
if (zend_fstat(fileno, &stat) == 0) { | ||
if ((stat.st_mode & /*S_IFMT*/0170000) == /*S_IFCHR*/0020000) { | ||
RETURN_TRUE; | ||
} | ||
} | ||
RETURN_NULL(); | ||
#endif | ||
} | ||
|
||
#ifdef PHP_WIN32 | ||
/* {{{ proto proto sapi_windows_vt100_support(resource stream[, bool enable]) | ||
Get or set VT100 support for the specified stream associated to an | ||
output buffer of a Windows console. | ||
*/ | ||
PHP_FUNCTION(sapi_windows_vt100_support) | ||
{ | ||
zval *zsrc; | ||
php_stream *stream; | ||
zend_bool enable; | ||
zend_long fileno; | ||
|
||
int argc = ZEND_NUM_ARGS(); | ||
|
||
if (zend_parse_parameters(argc, "r|b", &zsrc, &enable) == FAILURE) { | ||
RETURN_FALSE; | ||
} | ||
|
||
php_stream_from_zval(stream, zsrc); | ||
|
||
if (php_stream_can_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT) == SUCCESS) { | ||
php_stream_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT, (void*)&fileno, 0); | ||
} | ||
else if (php_stream_can_cast(stream, PHP_STREAM_AS_FD) == SUCCESS) { | ||
php_stream_cast(stream, PHP_STREAM_AS_FD, (void*)&fileno, 0); | ||
} | ||
else { | ||
zend_internal_type_error( | ||
ZEND_ARG_USES_STRICT_TYPES(), | ||
"%s() was not able to analyze the specified stream", | ||
get_active_function_name() | ||
); | ||
RETURN_FALSE; | ||
} | ||
|
||
/* Check if the file descriptor is a console */ | ||
if (!php_win32_console_fileno_is_console(fileno)) { | ||
RETURN_FALSE; | ||
} | ||
|
||
if (argc == 1) { | ||
/* Check if the Windows standard handle has VT100 control codes enabled */ | ||
if (php_win32_console_fileno_has_vt100(fileno)) { | ||
RETURN_TRUE; | ||
} | ||
else { | ||
RETURN_FALSE; | ||
} | ||
} | ||
else { | ||
/* Enable/disable VT100 control codes support for the specified Windows standard handle */ | ||
if (php_win32_console_fileno_set_vt100(fileno, enable ? TRUE : FALSE)) { | ||
RETURN_TRUE; | ||
} | ||
else { | ||
RETURN_FALSE; | ||
} | ||
} | ||
} | ||
#endif | ||
|
||
#ifdef HAVE_SHUTDOWN | ||
/* {{{ proto int stream_socket_shutdown(resource stream, int how) | ||
causes all or part of a full-duplex connection on the socket associated | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The void* cast will cause the stack corruption on 64-bit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 694e207