console.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. #include "php.h"
  17. #include "SAPI.h"
  18. #include "win32/console.h"
  19. PHP_WINUTIL_API BOOL php_win32_console_fileno_is_console(zend_long fileno)
  20. {/*{{{*/
  21. BOOL result = FALSE;
  22. HANDLE handle = (HANDLE) _get_osfhandle(fileno);
  23. if (handle != INVALID_HANDLE_VALUE) {
  24. DWORD mode;
  25. if (GetConsoleMode(handle, &mode)) {
  26. result = TRUE;
  27. }
  28. }
  29. return result;
  30. }/*}}}*/
  31. PHP_WINUTIL_API BOOL php_win32_console_fileno_has_vt100(zend_long fileno)
  32. {/*{{{*/
  33. BOOL result = FALSE;
  34. HANDLE handle = (HANDLE) _get_osfhandle(fileno);
  35. if (handle != INVALID_HANDLE_VALUE) {
  36. DWORD events;
  37. if (fileno != 0 && !GetNumberOfConsoleInputEvents(handle, &events)) {
  38. // Not STDIN
  39. DWORD mode;
  40. if (GetConsoleMode(handle, &mode)) {
  41. if (mode & ENABLE_VIRTUAL_TERMINAL_PROCESSING) {
  42. result = TRUE;
  43. }
  44. }
  45. }
  46. }
  47. return result;
  48. }/*}}}*/
  49. PHP_WINUTIL_API BOOL php_win32_console_fileno_set_vt100(zend_long fileno, BOOL enable)
  50. {/*{{{*/
  51. BOOL result = FALSE;
  52. HANDLE handle = (HANDLE) _get_osfhandle(fileno);
  53. if (handle != INVALID_HANDLE_VALUE) {
  54. DWORD events;
  55. if (fileno != 0 && !GetNumberOfConsoleInputEvents(handle, &events)) {
  56. // Not STDIN
  57. DWORD mode;
  58. if (GetConsoleMode(handle, &mode)) {
  59. DWORD newMode;
  60. if (enable) {
  61. newMode = mode | ENABLE_VIRTUAL_TERMINAL_PROCESSING;
  62. }
  63. else {
  64. newMode = mode & ~ENABLE_VIRTUAL_TERMINAL_PROCESSING;
  65. }
  66. if (newMode == mode) {
  67. result = TRUE;
  68. }
  69. else {
  70. if (SetConsoleMode(handle, newMode)) {
  71. result = TRUE;
  72. }
  73. }
  74. }
  75. }
  76. }
  77. return result;
  78. }/*}}}*/
  79. PHP_WINUTIL_API BOOL php_win32_console_is_own(void)
  80. {/*{{{*/
  81. if (!IsDebuggerPresent()) {
  82. CONSOLE_SCREEN_BUFFER_INFO csbi;
  83. DWORD pl[1];
  84. BOOL ret0 = FALSE, ret1 = FALSE;
  85. if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi)) {
  86. ret0 = !csbi.dwCursorPosition.X && !csbi.dwCursorPosition.Y;
  87. }
  88. ret1 = GetConsoleProcessList(pl, 1) == 1;
  89. return ret0 && ret1;
  90. }
  91. return FALSE;
  92. }/*}}}*/
  93. PHP_WINUTIL_API BOOL php_win32_console_is_cli_sapi(void)
  94. {/*{{{*/
  95. return strlen(sapi_module.name) >= sizeof("cli") - 1 && !strncmp(sapi_module.name, "cli", sizeof("cli") - 1);
  96. }/*}}}*/