Skip to content

Commit 33301d5

Browse files
mlocatiweltling
authored andcommittedOct 28, 2016
Add VT100 support for Windows
Fix function names prefix Use Unicode version of GetFinalPathNameByHandle Use EG(windows_version_info) instead of RtlGetVersion Use the specified handle_id instead of STD_OUTPUT_HANDLE Switch from stream name to stream resource Allow running tests capturing only stdout and/or stderr Add tests for stream_vt100_support function Export Win32 console functions Fix x64 build Use zend_long instead of long long, use GetConsole instead of GetFinalPathNameByHandleW to check if a handle is a valid console stream Always use zend_long on any platform Use _get_osfhandle to determine the standard handle Accept stream names Raise warnings in case of invalid stream parameter Return true if disabling VT100 support on a not-console/redirected stream or on old Windows versions Remove php_win32_console_os_supports_vt100 Differentiate stdin vs stdout/stderr Simplify setting flag Allow avoid piping STDIN Let stream_vt100_support accept only resources Fix run-tests Revert console flags in case of failure Simplify logic of stream_vt100_support when setting the flag Return true if succeeded, false otherwise Drop support for STDIN More comprehensive tests for stream_vt100_support Remove old tests Fix name of included file and use absolute paths Enable ENABLE_VIRTUAL_TERMINAL_PROCESSING on Windows by default Remove tests for stream_vt100_support Split stream_vt100_support into stream_isatty+sapi_windows_vt100_support Add tests for stream_isatty Add tests for sapi_windows_vt100_support Return null from stream_isatty is neither Windows nor Posix Fallback to S_ISCHR if neither Windows nor Posix Avoid defining argc since it's only used once Better comment about php_win32_console_fileno_is_console Use events instead of cNumberOfEvents Do not restore previous console mode We need to restore previous console mode on failing SetConsole calls only for STDIN Don't configure STDOUT/STDERR on Windows with PHP_CLI_WIN32_NO_CONSOLE
1 parent 946eb9b commit 33301d5

29 files changed

+2354
-20
lines changed
 

‎ext/standard/basic_functions.c

+15
Original file line numberDiff line numberDiff line change
@@ -2013,6 +2013,17 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_stream_supports_lock, 0, 0, 1)
20132013
ZEND_ARG_INFO(0, stream)
20142014
ZEND_END_ARG_INFO()
20152015

2016+
ZEND_BEGIN_ARG_INFO_EX(arginfo_stream_isatty, 0, 0, 1)
2017+
ZEND_ARG_INFO(0, stream)
2018+
ZEND_END_ARG_INFO()
2019+
2020+
#ifdef PHP_WIN32
2021+
ZEND_BEGIN_ARG_INFO_EX(arginfo_sapi_windows_vt100_support, 0, 0, 1)
2022+
ZEND_ARG_INFO(0, stream)
2023+
ZEND_ARG_INFO(0, enable)
2024+
ZEND_END_ARG_INFO()
2025+
#endif
2026+
20162027
ZEND_BEGIN_ARG_INFO_EX(arginfo_stream_select, 0, 0, 4)
20172028
ZEND_ARG_INFO(1, read_streams) /* ARRAY_INFO(1, read_streams, 1) */
20182029
ZEND_ARG_INFO(1, write_streams) /* ARRAY_INFO(1, write_streams, 1) */
@@ -3146,6 +3157,10 @@ const zend_function_entry basic_functions[] = { /* {{{ */
31463157
PHP_FE(stream_copy_to_stream, arginfo_stream_copy_to_stream)
31473158
PHP_FE(stream_get_contents, arginfo_stream_get_contents)
31483159
PHP_FE(stream_supports_lock, arginfo_stream_supports_lock)
3160+
PHP_FE(stream_isatty, arginfo_stream_isatty)
3161+
#ifdef PHP_WIN32
3162+
PHP_FE(sapi_windows_vt100_support, arginfo_sapi_windows_vt100_support)
3163+
#endif
31493164
PHP_FE(fgetcsv, arginfo_fgetcsv)
31503165
PHP_FE(fputcsv, arginfo_fputcsv)
31513166
PHP_FE(flock, arginfo_flock)

‎ext/standard/streamsfuncs.c

+114
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ typedef unsigned long long php_timeout_ull;
3737
#else
3838
#include "win32/select.h"
3939
#include "win32/sockets.h"
40+
#include "win32/console.h"
4041
typedef unsigned __int64 php_timeout_ull;
4142
#endif
4243

@@ -1569,6 +1570,119 @@ PHP_FUNCTION(stream_supports_lock)
15691570
RETURN_TRUE;
15701571
}
15711572

1573+
/* {{{ proto proto stream_isatty(resource stream)
1574+
Check if a stream is a TTY.
1575+
*/
1576+
PHP_FUNCTION(stream_isatty)
1577+
{
1578+
zval *zsrc;
1579+
php_stream *stream;
1580+
zend_long fileno;
1581+
1582+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &zsrc) == FAILURE) {
1583+
RETURN_FALSE;
1584+
}
1585+
1586+
php_stream_from_zval(stream, zsrc);
1587+
1588+
if (php_stream_can_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT) == SUCCESS) {
1589+
php_stream_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT, (void*)&fileno, 0);
1590+
}
1591+
else if (php_stream_can_cast(stream, PHP_STREAM_AS_FD) == SUCCESS) {
1592+
php_stream_cast(stream, PHP_STREAM_AS_FD, (void*)&fileno, 0);
1593+
}
1594+
else {
1595+
RETURN_FALSE;
1596+
}
1597+
1598+
#ifdef PHP_WIN32
1599+
/* Check if the Windows standard handle is redirected to file */
1600+
if (php_win32_console_fileno_is_console(fileno)) {
1601+
RETURN_TRUE;
1602+
}
1603+
else {
1604+
RETURN_FALSE;
1605+
}
1606+
#elif HAVE_POSIX
1607+
/* Check if the file descriptor identifier is a terminal */
1608+
if (isatty(fileno)) {
1609+
RETURN_TRUE;
1610+
}
1611+
else {
1612+
RETURN_FALSE;
1613+
}
1614+
#else
1615+
zend_stat_t stat;
1616+
if (zend_fstat(fileno, &stat) == 0) {
1617+
if ((stat.st_mode & /*S_IFMT*/0170000) == /*S_IFCHR*/0020000) {
1618+
RETURN_TRUE;
1619+
}
1620+
}
1621+
RETURN_NULL();
1622+
#endif
1623+
}
1624+
1625+
#ifdef PHP_WIN32
1626+
/* {{{ proto proto sapi_windows_vt100_support(resource stream[, bool enable])
1627+
Get or set VT100 support for the specified stream associated to an
1628+
output buffer of a Windows console.
1629+
*/
1630+
PHP_FUNCTION(sapi_windows_vt100_support)
1631+
{
1632+
zval *zsrc;
1633+
php_stream *stream;
1634+
zend_bool enable;
1635+
zend_long fileno;
1636+
1637+
int argc = ZEND_NUM_ARGS();
1638+
1639+
if (zend_parse_parameters(argc, "r|b", &zsrc, &enable) == FAILURE) {
1640+
RETURN_FALSE;
1641+
}
1642+
1643+
php_stream_from_zval(stream, zsrc);
1644+
1645+
if (php_stream_can_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT) == SUCCESS) {
1646+
php_stream_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT, (void*)&fileno, 0);
1647+
}
1648+
else if (php_stream_can_cast(stream, PHP_STREAM_AS_FD) == SUCCESS) {
1649+
php_stream_cast(stream, PHP_STREAM_AS_FD, (void*)&fileno, 0);
1650+
}
1651+
else {
1652+
zend_internal_type_error(
1653+
ZEND_ARG_USES_STRICT_TYPES(),
1654+
"%s() was not able to analyze the specified stream",
1655+
get_active_function_name()
1656+
);
1657+
RETURN_FALSE;
1658+
}
1659+
1660+
/* Check if the file descriptor is a console */
1661+
if (!php_win32_console_fileno_is_console(fileno)) {
1662+
RETURN_FALSE;
1663+
}
1664+
1665+
if (argc == 1) {
1666+
/* Check if the Windows standard handle has VT100 control codes enabled */
1667+
if (php_win32_console_fileno_has_vt100(fileno)) {
1668+
RETURN_TRUE;
1669+
}
1670+
else {
1671+
RETURN_FALSE;
1672+
}
1673+
}
1674+
else {
1675+
/* Enable/disable VT100 control codes support for the specified Windows standard handle */
1676+
if (php_win32_console_fileno_set_vt100(fileno, enable ? TRUE : FALSE)) {
1677+
RETURN_TRUE;
1678+
}
1679+
else {
1680+
RETURN_FALSE;
1681+
}
1682+
}
1683+
}
1684+
#endif
1685+
15721686
#ifdef HAVE_SHUTDOWN
15731687
/* {{{ proto int stream_socket_shutdown(resource stream, int how)
15741688
causes all or part of a full-duplex connection on the socket associated

‎ext/standard/streamsfuncs.h

+4
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ PHP_FUNCTION(stream_socket_shutdown);
6161
PHP_FUNCTION(stream_resolve_include_path);
6262
PHP_FUNCTION(stream_is_local);
6363
PHP_FUNCTION(stream_supports_lock);
64+
PHP_FUNCTION(stream_isatty);
65+
#ifdef PHP_WIN32
66+
PHP_FUNCTION(sapi_windows_vt100_support);
67+
#endif
6468

6569
#if HAVE_SOCKETPAIR
6670
PHP_FUNCTION(stream_socket_pair);

‎run-tests.php

+47-18
Original file line numberDiff line numberDiff line change
@@ -1057,7 +1057,7 @@ function error_report($testname, $logname, $tested)
10571057
}
10581058
}
10591059

1060-
function system_with_timeout($commandline, $env = null, $stdin = null)
1060+
function system_with_timeout($commandline, $env = null, $stdin = null, $captureStdIn = true, $captureStdOut = true, $captureStdErr = true)
10611061
{
10621062
global $leak_check, $cwd;
10631063

@@ -1068,21 +1068,29 @@ function system_with_timeout($commandline, $env = null, $stdin = null)
10681068
$bin_env[$key] = $value;
10691069
}
10701070

1071-
$proc = proc_open($commandline, array(
1072-
0 => array('pipe', 'r'),
1073-
1 => array('pipe', 'w'),
1074-
2 => array('pipe', 'w')
1075-
), $pipes, $cwd, $bin_env, array('suppress_errors' => true, 'binary_pipes' => true));
1071+
$descriptorspec = array();
1072+
if ($captureStdIn) {
1073+
$descriptorspec[0] = array('pipe', 'r');
1074+
}
1075+
if ($captureStdOut) {
1076+
$descriptorspec[1] = array('pipe', 'w');
1077+
}
1078+
if ($captureStdErr) {
1079+
$descriptorspec[2] = array('pipe', 'w');
1080+
}
1081+
$proc = proc_open($commandline, $descriptorspec, $pipes, $cwd, $bin_env, array('suppress_errors' => true, 'binary_pipes' => true));
10761082

10771083
if (!$proc) {
10781084
return false;
10791085
}
10801086

1081-
if (!is_null($stdin)) {
1082-
fwrite($pipes[0], $stdin);
1087+
if ($captureStdIn) {
1088+
if (!is_null($stdin)) {
1089+
fwrite($pipes[0], $stdin);
1090+
}
1091+
fclose($pipes[0]);
1092+
unset($pipes[0]);
10831093
}
1084-
fclose($pipes[0]);
1085-
unset($pipes[0]);
10861094

10871095
$timeout = $leak_check ? 300 : (isset($env['TEST_TIMEOUT']) ? $env['TEST_TIMEOUT'] : 60);
10881096

@@ -1102,7 +1110,13 @@ function system_with_timeout($commandline, $env = null, $stdin = null)
11021110
proc_terminate($proc, 9);
11031111
return $data;
11041112
} else if ($n > 0) {
1105-
$line = fread($pipes[1], 8192);
1113+
if ($captureStdOut) {
1114+
$line = fread($pipes[1], 8192);
1115+
} elseif ($captureStdErr) {
1116+
$line = fread($pipes[2], 8192);
1117+
} else {
1118+
$line = '';
1119+
}
11061120
if (strlen($line) == 0) {
11071121
/* EOF */
11081122
break;
@@ -1332,6 +1346,21 @@ function run_test($php, $file, $env)
13321346
return 'BORKED';
13331347
}
13341348

1349+
if (isset($section_text['CAPTURE_STDIO'])) {
1350+
$captureStdIn = stripos($section_text['CAPTURE_STDIO'], 'STDIN') !== false;
1351+
$captureStdOut = stripos($section_text['CAPTURE_STDIO'], 'STDOUT') !== false;
1352+
$captureStdErr = stripos($section_text['CAPTURE_STDIO'], 'STDERR') !== false;
1353+
} else {
1354+
$captureStdIn = true;
1355+
$captureStdOut = true;
1356+
$captureStdErr = true;
1357+
}
1358+
if ($captureStdOut && $captureStdErr) {
1359+
$cmdRedirect = ' 2>&1';
1360+
} else {
1361+
$cmdRedirect = '';
1362+
}
1363+
13351364
$tested = trim($section_text['TEST']);
13361365

13371366
/* For GET/POST/PUT tests, check if cgi sapi is available and if it is, use it. */
@@ -1740,7 +1769,7 @@ function run_test($php, $file, $env)
17401769
}
17411770

17421771
save_text($tmp_post, $request);
1743-
$cmd = "$php $pass_options $ini_settings -f \"$test_file\" 2>&1 < \"$tmp_post\"";
1772+
$cmd = "$php $pass_options $ini_settings -f \"$test_file\"$cmdRedirect < \"$tmp_post\"";
17441773

17451774
} elseif (array_key_exists('PUT', $section_text) && !empty($section_text['PUT'])) {
17461775

@@ -1774,7 +1803,7 @@ function run_test($php, $file, $env)
17741803
}
17751804

17761805
save_text($tmp_post, $request);
1777-
$cmd = "$php $pass_options $ini_settings -f \"$test_file\" 2>&1 < \"$tmp_post\"";
1806+
$cmd = "$php $pass_options $ini_settings -f \"$test_file\"$cmdRedirect < \"$tmp_post\"";
17781807

17791808
} else if (array_key_exists('POST', $section_text) && !empty($section_text['POST'])) {
17801809

@@ -1791,7 +1820,7 @@ function run_test($php, $file, $env)
17911820
$env['CONTENT_LENGTH'] = $content_length;
17921821
}
17931822

1794-
$cmd = "$php $pass_options $ini_settings -f \"$test_file\" 2>&1 < \"$tmp_post\"";
1823+
$cmd = "$php $pass_options $ini_settings -f \"$test_file\"$cmdRedirect < \"$tmp_post\"";
17951824

17961825
} else if (array_key_exists('GZIP_POST', $section_text) && !empty($section_text['GZIP_POST'])) {
17971826

@@ -1806,7 +1835,7 @@ function run_test($php, $file, $env)
18061835
$env['CONTENT_TYPE'] = 'application/x-www-form-urlencoded';
18071836
$env['CONTENT_LENGTH'] = $content_length;
18081837

1809-
$cmd = "$php $pass_options $ini_settings -f \"$test_file\" 2>&1 < \"$tmp_post\"";
1838+
$cmd = "$php $pass_options $ini_settings -f \"$test_file\"$cmdRedirect < \"$tmp_post\"";
18101839

18111840
} else if (array_key_exists('DEFLATE_POST', $section_text) && !empty($section_text['DEFLATE_POST'])) {
18121841
$post = trim($section_text['DEFLATE_POST']);
@@ -1819,15 +1848,15 @@ function run_test($php, $file, $env)
18191848
$env['CONTENT_TYPE'] = 'application/x-www-form-urlencoded';
18201849
$env['CONTENT_LENGTH'] = $content_length;
18211850

1822-
$cmd = "$php $pass_options $ini_settings -f \"$test_file\" 2>&1 < \"$tmp_post\"";
1851+
$cmd = "$php $pass_options $ini_settings -f \"$test_file\"$cmdRedirect < \"$tmp_post\"";
18231852

18241853
} else {
18251854

18261855
$env['REQUEST_METHOD'] = 'GET';
18271856
$env['CONTENT_TYPE'] = '';
18281857
$env['CONTENT_LENGTH'] = '';
18291858

1830-
$cmd = "$php $pass_options $ini_settings -f \"$test_file\" $args 2>&1";
1859+
$cmd = "$php $pass_options $ini_settings -f \"$test_file\" $args$cmdRedirect";
18311860
}
18321861

18331862
if ($leak_check) {
@@ -1863,7 +1892,7 @@ function run_test($php, $file, $env)
18631892

18641893
junit_start_timer($shortname);
18651894

1866-
$out = system_with_timeout($cmd, $env, isset($section_text['STDIN']) ? $section_text['STDIN'] : null);
1895+
$out = system_with_timeout($cmd, $env, isset($section_text['STDIN']) ? $section_text['STDIN'] : null, $captureStdIn, $captureStdOut, $captureStdErr);
18671896

18681897
junit_finish_timer($shortname);
18691898

‎sapi/cli/php_cli.c

+9
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#ifdef PHP_WIN32
3939
#include "win32/time.h"
4040
#include "win32/signal.h"
41+
#include "win32/console.h"
4142
#include <process.h>
4243
#include <shellapi.h>
4344
#endif
@@ -243,6 +244,9 @@ static void print_extensions(void) /* {{{ */
243244
#ifndef STDOUT_FILENO
244245
#define STDOUT_FILENO 1
245246
#endif
247+
#ifndef STDERR_FILENO
248+
#define STDERR_FILENO 2
249+
#endif
246250

247251
static inline int sapi_cli_select(int fd)
248252
{
@@ -1208,6 +1212,11 @@ int main(int argc, char *argv[])
12081212
*/
12091213
argv = save_ps_args(argc, argv);
12101214

1215+
#if defined(PHP_WIN32) && !defined(PHP_CLI_WIN32_NO_CONSOLE)
1216+
php_win32_console_fileno_set_vt100(STDOUT_FILENO, TRUE);
1217+
php_win32_console_fileno_set_vt100(STDERR_FILENO, TRUE);
1218+
#endif
1219+
12111220
cli_sapi_module.additional_functions = additional_functions;
12121221

12131222
#if defined(PHP_WIN32) && defined(_DEBUG) && defined(PHP_WIN32_DEBUG_HEAP)

0 commit comments

Comments
 (0)
Failed to load comments.