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