xref: /PHP-7.3/win32/console.c (revision 8d3f8ca1)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 7                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1997-2018 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 "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 
112 /*
113  * Local variables:
114  * tab-width: 4
115  * c-basic-offset: 4
116  * End:
117  * vim600: sw=4 ts=4 fdm=marker
118  * vim<600: sw=4 ts=4
119  */
120