Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

195 lines
4.0 KiB

  1. #include "std.h"
  2. #define FOREGROUND_BLACK 0
  3. #define FOREGROUND_CYAN FOREGROUND_BLUE | FOREGROUND_GREEN
  4. #define FOREGROUND_MAGENTA FOREGROUND_BLUE | FOREGROUND_RED
  5. #define FOREGROUND_YELLOW FOREGROUND_GREEN | FOREGROUND_RED
  6. #define FOREGROUND_WHITE FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED
  7. #define BACKGROUND_BLACK 0
  8. #define BACKGROUND_CYAN BACKGROUND_BLUE | BACKGROUND_GREEN
  9. #define BACKGROUND_MAGENTA BACKGROUND_BLUE | BACKGROUND_RED
  10. #define BACKGROUND_YELLOW BACKGROUND_GREEN | BACKGROUND_RED
  11. #define BACKGROUND_WHITE BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED
  12. #define TERMINAL_ROWS 25
  13. #define TERMINAL_COLUMNS 80
  14. HANDLE termpCreateTerminalBuffer();
  15. PTERMINAL termpCreateTerminal();
  16. void termpDestroyTerminal(PTERMINAL pTerminal);
  17. PTERMINAL termpCreateTerminal()
  18. {
  19. PTERMINAL pResult;
  20. pResult = malloc(
  21. sizeof(TERMINAL));
  22. if (pResult!=NULL)
  23. {
  24. ZeroMemory(
  25. pResult,
  26. sizeof(TERMINAL));
  27. pResult->hInput = INVALID_HANDLE_VALUE;
  28. pResult->hOutput = INVALID_HANDLE_VALUE;
  29. pResult->hSavedBuffer = INVALID_HANDLE_VALUE;
  30. pResult->hNewBuffer = INVALID_HANDLE_VALUE;
  31. pResult->wAttributes = FOREGROUND_WHITE;
  32. pResult->fInverse = FALSE;
  33. pResult->fBold = FALSE;
  34. pResult->fEscapeValid = FALSE;
  35. pResult->fEscapeInvalid = TRUE;
  36. pResult->wEscapeParamCount = 0;
  37. pResult->chEscapeCommand = 0;
  38. pResult->chEscapeFirstChar = 0;
  39. pResult->pTxProc = NULL;
  40. }
  41. else
  42. {
  43. SetLastError(
  44. ERROR_NOT_ENOUGH_MEMORY);
  45. }
  46. return pResult;
  47. }
  48. void termpDestroyTerminal(PTERMINAL pTerminal)
  49. {
  50. if (pTerminal!=NULL)
  51. {
  52. free(pTerminal);
  53. }
  54. }
  55. PTERMINAL termInitialize(TERMTXPROC pTxProc)
  56. {
  57. PTERMINAL pResult;
  58. BOOL fResult;
  59. pResult = termpCreateTerminal();
  60. if (pResult==NULL)
  61. {
  62. goto Error;
  63. }
  64. pResult->hSavedBuffer = GetStdHandle(
  65. STD_OUTPUT_HANDLE);
  66. if (pResult->hSavedBuffer==INVALID_HANDLE_VALUE)
  67. {
  68. goto Error;
  69. }
  70. pResult->hNewBuffer = termpCreateTerminalBuffer();
  71. if (pResult->hNewBuffer==INVALID_HANDLE_VALUE)
  72. {
  73. goto Error;
  74. }
  75. fResult = SetConsoleActiveScreenBuffer(
  76. pResult->hNewBuffer);
  77. if (!fResult)
  78. {
  79. goto Error;
  80. }
  81. return pResult;
  82. Error:
  83. termFinalize(pResult);
  84. return NULL;
  85. }
  86. void termFinalize(PTERMINAL pTerminal)
  87. {
  88. if (pTerminal!=NULL)
  89. {
  90. if (pTerminal->hSavedBuffer!=INVALID_HANDLE_VALUE)
  91. {
  92. SetConsoleActiveScreenBuffer(
  93. pTerminal->hSavedBuffer);
  94. }
  95. if (pTerminal->hNewBuffer!=INVALID_HANDLE_VALUE)
  96. {
  97. CloseHandle(
  98. pTerminal->hNewBuffer);
  99. }
  100. free(pTerminal);
  101. }
  102. }
  103. HANDLE termpCreateTerminalBuffer()
  104. {
  105. HANDLE hNewBuffer;
  106. BOOL bResult;
  107. COORD Coord;
  108. hNewBuffer = CreateConsoleScreenBuffer(
  109. GENERIC_READ | GENERIC_WRITE,
  110. 0, // No sharing allowed
  111. NULL, // No inheritance allowed
  112. CONSOLE_TEXTMODE_BUFFER, // The only supported value
  113. NULL); // Reserved, must be null
  114. if (hNewBuffer==INVALID_HANDLE_VALUE)
  115. {
  116. goto Error;
  117. }
  118. Coord.Y = TERMINAL_ROWS;
  119. Coord.X = TERMINAL_COLUMNS;
  120. bResult = SetConsoleScreenBufferSize(
  121. hNewBuffer,
  122. Coord);
  123. if (!bResult)
  124. {
  125. goto Error;
  126. }
  127. bResult = SetConsoleTextAttribute(
  128. hNewBuffer,
  129. FOREGROUND_WHITE);
  130. if (!bResult)
  131. {
  132. goto Error;
  133. }
  134. Coord.X = 0;
  135. Coord.Y = 0;
  136. bResult = SetConsoleCursorPosition(
  137. hNewBuffer,
  138. Coord);
  139. if (!bResult)
  140. {
  141. goto Error;
  142. }
  143. return hNewBuffer;
  144. Error:
  145. if (hNewBuffer!=INVALID_HANDLE_VALUE)
  146. {
  147. CloseHandle(hNewBuffer);
  148. }
  149. return INVALID_HANDLE_VALUE;
  150. }