xref: /PHP-8.2/win32/console.c (revision aff36587)
1 /*
2    +----------------------------------------------------------------------+
3    | Copyright (c) The PHP Group                                          |
4    +----------------------------------------------------------------------+
5    | This source file is subject to version 3.01 of the PHP license,      |
6    | that is bundled with this package in the file LICENSE, and is        |
7    | available through the world-wide-web at the following url:           |
8    | https://www.php.net/license/3_01.txt                                 |
9    | If you did not receive a copy of the PHP license and are unable to   |
10    | obtain it through the world-wide-web, please send a note to          |
11    | license@php.net so we can mail you a copy immediately.               |
12    +----------------------------------------------------------------------+
13    | Author: Michele Locati <mlocati@gmail.com>                           |
14    +----------------------------------------------------------------------+
15  */
16 
17 #include "php.h"
18 #include "SAPI.h"
19 #include "win32/console.h"
20 
21 
php_win32_console_fileno_is_console(zend_long fileno)22 PHP_WINUTIL_API BOOL php_win32_console_fileno_is_console(zend_long fileno)
23 {/*{{{*/
24 	BOOL result = FALSE;
25 	HANDLE handle = (HANDLE) _get_osfhandle(fileno);
26 
27 	if (handle != INVALID_HANDLE_VALUE) {
28 		DWORD mode;
29 		if (GetConsoleMode(handle, &mode)) {
30 			result = TRUE;
31 		}
32 	}
33 	return result;
34 }/*}}}*/
35 
php_win32_console_fileno_has_vt100(zend_long fileno)36 PHP_WINUTIL_API BOOL php_win32_console_fileno_has_vt100(zend_long fileno)
37 {/*{{{*/
38 	BOOL result = FALSE;
39 	HANDLE handle = (HANDLE) _get_osfhandle(fileno);
40 
41 	if (handle != INVALID_HANDLE_VALUE) {
42 		DWORD events;
43 
44 		if (fileno != 0 && !GetNumberOfConsoleInputEvents(handle, &events)) {
45 			// Not STDIN
46 			DWORD mode;
47 
48 			if (GetConsoleMode(handle, &mode)) {
49 				if (mode & ENABLE_VIRTUAL_TERMINAL_PROCESSING) {
50 					result = TRUE;
51 				}
52 			}
53 		}
54 	}
55 	return result;
56 }/*}}}*/
57 
php_win32_console_fileno_set_vt100(zend_long fileno,BOOL enable)58 PHP_WINUTIL_API BOOL php_win32_console_fileno_set_vt100(zend_long fileno, BOOL enable)
59 {/*{{{*/
60 	BOOL result = FALSE;
61 	HANDLE handle = (HANDLE) _get_osfhandle(fileno);
62 
63 	if (handle != INVALID_HANDLE_VALUE) {
64 		DWORD events;
65 
66 		if (fileno != 0 && !GetNumberOfConsoleInputEvents(handle, &events)) {
67 			// Not STDIN
68 			DWORD mode;
69 
70 			if (GetConsoleMode(handle, &mode)) {
71 				DWORD newMode;
72 
73 				if (enable) {
74 					newMode = mode | ENABLE_VIRTUAL_TERMINAL_PROCESSING;
75 				}
76 				else {
77 					newMode = mode & ~ENABLE_VIRTUAL_TERMINAL_PROCESSING;
78 				}
79 				if (newMode == mode) {
80 					result = TRUE;
81 				}
82 				else {
83 					if (SetConsoleMode(handle, newMode)) {
84 						result = TRUE;
85 					}
86 				}
87 			}
88 		}
89 	}
90 	return result;
91 }/*}}}*/
92 
php_win32_console_is_own(void)93 PHP_WINUTIL_API BOOL php_win32_console_is_own(void)
94 {/*{{{*/
95 	if (!IsDebuggerPresent()) {
96 		CONSOLE_SCREEN_BUFFER_INFO csbi;
97 		DWORD pl[1];
98 		BOOL ret0 = FALSE, ret1 = FALSE;
99 
100 		if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi)) {
101 			ret0 = !csbi.dwCursorPosition.X && !csbi.dwCursorPosition.Y;
102 		}
103 
104 		ret1 = GetConsoleProcessList(pl, 1) == 1;
105 
106 		return ret0 && ret1;
107 	}
108 
109 	return FALSE;
110 }/*}}}*/
111 
php_win32_console_is_cli_sapi(void)112 PHP_WINUTIL_API BOOL php_win32_console_is_cli_sapi(void)
113 {/*{{{*/
114 	return strlen(sapi_module.name) >= sizeof("cli") - 1 && !strncmp(sapi_module.name, "cli", sizeof("cli") - 1);
115 }/*}}}*/
116 
117