Skip to content

Commit a4017d4

Browse files
committed
Add sapi_windows_console_size()
This function is supposed to retrieve the width and height of a console window on Windows. It expects a single stream argument, and if that is a console, returns its width and height as two element array (aka. pair). On failure the function returns false.
1 parent 2b80b2e commit a4017d4

File tree

5 files changed

+66
-1
lines changed

5 files changed

+66
-1
lines changed

ext/standard/basic_functions.stub.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3569,6 +3569,9 @@ function stream_isatty($stream): bool {}
35693569
#ifdef PHP_WIN32
35703570
/** @param resource $stream */
35713571
function sapi_windows_vt100_support($stream, ?bool $enable = null): bool {}
3572+
3573+
/** @param resource $stream */
3574+
function sapi_windows_console_size($stream): array|bool {}
35723575
#endif
35733576

35743577
/** @param resource $stream */

ext/standard/basic_functions_arginfo.h

Lines changed: 7 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ext/standard/streamsfuncs.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1774,6 +1774,48 @@ PHP_FUNCTION(sapi_windows_vt100_support)
17741774
}
17751775
}
17761776
}
1777+
1778+
PHP_FUNCTION(sapi_windows_console_size)
1779+
{
1780+
zval *zsrc;
1781+
php_stream *stream;
1782+
zend_long fileno;
1783+
int width, height;
1784+
zval ztmp;
1785+
1786+
ZEND_PARSE_PARAMETERS_START(1, 1)
1787+
Z_PARAM_RESOURCE(zsrc)
1788+
ZEND_PARSE_PARAMETERS_END();
1789+
1790+
php_stream_from_zval(stream, zsrc);
1791+
1792+
/* get the fd.
1793+
* NB: Most other code will NOT use the PHP_STREAM_CAST_INTERNAL flag when casting.
1794+
* It is only used here so that the buffered data warning is not displayed.
1795+
*/
1796+
if (php_stream_can_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT | PHP_STREAM_CAST_INTERNAL) == SUCCESS) {
1797+
php_stream_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT | PHP_STREAM_CAST_INTERNAL, (void*)&fileno, 0);
1798+
} else if (php_stream_can_cast(stream, PHP_STREAM_AS_FD | PHP_STREAM_CAST_INTERNAL) == SUCCESS) {
1799+
php_stream_cast(stream, PHP_STREAM_AS_FD | PHP_STREAM_CAST_INTERNAL, (void*)&fileno, 0);
1800+
} else {
1801+
php_error_docref(NULL, E_WARNING, "not able to analyze the specified stream");
1802+
RETURN_FALSE;
1803+
}
1804+
1805+
if (!php_win32_console_fileno_is_console(fileno)) {
1806+
RETURN_FALSE;
1807+
}
1808+
1809+
if (!php_win32_console_size(fileno, &width, &height)) {
1810+
RETURN_FALSE;
1811+
}
1812+
1813+
array_init(return_value);
1814+
ZVAL_LONG(&ztmp, width);
1815+
zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &ztmp);
1816+
ZVAL_LONG(&ztmp, height);
1817+
zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &ztmp);
1818+
}
17771819
#endif
17781820

17791821
#ifdef HAVE_SHUTDOWN

win32/console.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,14 @@ PHP_WINUTIL_API BOOL php_win32_console_is_cli_sapi(void)
114114
return strlen(sapi_module.name) >= sizeof("cli") - 1 && !strncmp(sapi_module.name, "cli", sizeof("cli") - 1);
115115
}/*}}}*/
116116

117+
PHP_WINUTIL_API BOOL php_win32_console_size(zend_long fileno, int *width, int *height)
118+
{
119+
HANDLE handle = (HANDLE) _get_osfhandle(fileno);
120+
CONSOLE_SCREEN_BUFFER_INFO csbi;
121+
if (!GetConsoleScreenBufferInfo(handle, &csbi)) {
122+
return 0;
123+
}
124+
*width = csbi.srWindow.Right - csbi.srWindow.Left + 1;
125+
*height = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
126+
return 1;
127+
}

win32/console.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,7 @@ PHP_WINUTIL_API BOOL php_win32_console_is_own(void);
6060
/* Check whether the current SAPI is run on console. */
6161
PHP_WINUTIL_API BOOL php_win32_console_is_cli_sapi(void);
6262

63+
/* Gets the width and height of a console. */
64+
PHP_WINUTIL_API BOOL php_win32_console_size(zend_long fileno, int *width, int *height);
65+
6366
#endif

0 commit comments

Comments
 (0)