@@ -62,7 +62,6 @@ typedef struct consoleEvent {
62
62
consoleEvent * head = NULL ;
63
63
consoleEvent * tail = NULL ;
64
64
65
- BOOL isRedirected = FALSE;
66
65
BOOL bRet = FALSE;
67
66
BOOL bNoScrollRegion = FALSE;
68
67
BOOL bStartup = TRUE;
@@ -86,6 +85,7 @@ DWORD hostThreadId = 0;
86
85
DWORD childProcessId = 0 ;
87
86
DWORD dwStatus = 0 ;
88
87
DWORD currentLine = 0 ;
88
+ DWORD lastLineLength = 0 ;
89
89
90
90
UINT cp = 0 ;
91
91
@@ -141,41 +141,46 @@ void SendKeyStroke(HANDLE hInput, int keyStroke, char character)
141
141
}
142
142
143
143
// VT output routines
144
- void SendClearScreen (HANDLE hInput ) {
144
+ void SendLF (HANDLE hInput ) {
145
145
DWORD wr = 0 ;
146
146
147
- WriteFile (hInput , "\033[2J" , 4 , & wr , NULL );
147
+ if (bUseAnsiEmulation )
148
+ WriteFile (hInput , "\n" , 1 , & wr , NULL );
148
149
}
149
150
150
- void SendClearScreenFromCursor (HANDLE hInput ) {
151
+ void SendClearScreen (HANDLE hInput ) {
151
152
DWORD wr = 0 ;
152
153
153
- WriteFile (hInput , "\033[1J" , 4 , & wr , NULL );
154
+ if (bUseAnsiEmulation )
155
+ WriteFile (hInput , "\033[2J" , 4 , & wr , NULL );
154
156
}
155
157
156
- void SendCRLF (HANDLE hInput ) {
157
-
158
+ void SendClearScreenFromCursor (HANDLE hInput ) {
158
159
DWORD wr = 0 ;
159
160
160
- WriteFile (hInput , "\n" , 2 , & wr , NULL );
161
+ if (bUseAnsiEmulation )
162
+ WriteFile (hInput , "\033[1J" , 4 , & wr , NULL );
161
163
}
162
164
163
165
void SendHideCursor (HANDLE hInput ) {
164
166
DWORD wr = 0 ;
165
167
166
- WriteFile (hInput , "\033[?25l" , 6 , & wr , NULL );
168
+ if (bUseAnsiEmulation )
169
+ WriteFile (hInput , "\033[?25l" , 6 , & wr , NULL );
167
170
}
168
171
169
172
void SendShowCursor (HANDLE hInput ) {
170
173
DWORD wr = 0 ;
171
174
172
- WriteFile (hInput , "\033[?25h" , 6 , & wr , NULL );
175
+ if (bUseAnsiEmulation )
176
+ WriteFile (hInput , "\033[?25h" , 6 , & wr , NULL );
173
177
}
174
178
175
179
void SendCursorPositionRequest (HANDLE hInput ) {
176
180
DWORD wr = 0 ;
177
181
178
- WriteFile (hInput , "\033[6n" , 4 , & wr , NULL );
182
+ if (bUseAnsiEmulation )
183
+ WriteFile (hInput , "\033[6n" , 4 , & wr , NULL );
179
184
}
180
185
181
186
void SendSetCursor (HANDLE hInput , int X , int Y ) {
@@ -428,8 +433,6 @@ void SizeWindow(HANDLE hInput) {
428
433
429
434
DWORD WINAPI MonitorChild (_In_ LPVOID lpParameter ) {
430
435
WaitForSingleObject (child , INFINITE );
431
- if (isRedirected )
432
- CloseHandle (pipe_in );
433
436
PostThreadMessage (hostThreadId , WM_APPEXIT , 0 , 0 );
434
437
return 0 ;
435
438
}
@@ -519,7 +522,7 @@ DWORD ProcessEvent(void *p) {
519
522
readRect .Right = LOWORD (idChild );
520
523
521
524
// Detect a "cls" (Windows).
522
- if (!bStartup &&
525
+ if (!bStartup &&
523
526
(readRect .Top == consoleInfo .srWindow .Top || readRect .Top == nextConsoleInfo .srWindow .Top ))
524
527
{
525
528
BOOL isClearCommand = FALSE;
@@ -555,15 +558,15 @@ DWORD ProcessEvent(void *p) {
555
558
556
559
if (bufferSize > MAX_EXPECTED_BUFFER_SIZE ) {
557
560
558
- if (!bStartup ) {
561
+ if (!bStartup ) {
559
562
SendClearScreen (pipe_out );
560
563
ViewPortY = 0 ;
561
564
lastViewPortY = 0 ;
562
565
}
563
566
564
567
return ERROR_SUCCESS ;
565
568
}
566
-
569
+
567
570
// Create the screen scrape buffer
568
571
CHAR_INFO * pBuffer = (PCHAR_INFO )malloc (sizeof (CHAR_INFO ) * bufferSize );
569
572
@@ -586,6 +589,10 @@ DWORD ProcessEvent(void *p) {
586
589
return dwError ;
587
590
}
588
591
592
+ if (readRect .Top > currentLine )
593
+ for (SHORT n = currentLine ; n < readRect .Top ; n ++ )
594
+ SendLF (pipe_out );
595
+
589
596
// Set cursor location based on the reported location from the message.
590
597
CalculateAndSetCursor (pipe_out , ViewPortY , viewPortHeight , readRect .Left ,
591
598
readRect .Top );
@@ -594,6 +601,7 @@ DWORD ProcessEvent(void *p) {
594
601
SendBuffer (pipe_out , pBuffer , bufferSize );
595
602
596
603
lastViewPortY = ViewPortY ;
604
+ lastLineLength = readRect .Left ;
597
605
598
606
free (pBuffer );
599
607
@@ -1062,8 +1070,6 @@ int wmain(int ac, wchar_t **av) {
1062
1070
1063
1071
cp = GetConsoleCP ();
1064
1072
1065
- isRedirected = !GetConsoleMode (pipe_in , & dwMode );
1066
-
1067
1073
ZeroMemory (& inputSi , sizeof (STARTUPINFO ));
1068
1074
GetStartupInfo (& inputSi );
1069
1075
@@ -1086,13 +1092,10 @@ int wmain(int ac, wchar_t **av) {
1086
1092
hostThreadId = GetCurrentThreadId ();
1087
1093
hostProcessId = GetCurrentProcessId ();
1088
1094
1089
- if (isRedirected )
1090
- {
1091
- InitializeCriticalSection (& criticalSection );
1095
+ InitializeCriticalSection (& criticalSection );
1092
1096
1093
- hEventHook = SetWinEventHook (EVENT_CONSOLE_CARET , EVENT_CONSOLE_LAYOUT , NULL ,
1094
- ConsoleEventProc , 0 , 0 , WINEVENT_OUTOFCONTEXT );
1095
- }
1097
+ hEventHook = SetWinEventHook (EVENT_CONSOLE_CARET , EVENT_CONSOLE_LAYOUT , NULL ,
1098
+ ConsoleEventProc , 0 , 0 , WINEVENT_OUTOFCONTEXT );
1096
1099
1097
1100
memset (& si , 0 , sizeof (STARTUPINFO ));
1098
1101
memset (& pi , 0 , sizeof (PROCESS_INFORMATION ));
@@ -1101,11 +1104,8 @@ int wmain(int ac, wchar_t **av) {
1101
1104
si .cb = sizeof (STARTUPINFO );
1102
1105
si .dwFlags = 0 ;
1103
1106
1104
- if (isRedirected )
1105
- {
1106
- /* disable inheritance on pipe_in*/
1107
- GOTO_CLEANUP_ON_FALSE (SetHandleInformation (pipe_in , HANDLE_FLAG_INHERIT , 0 ));
1108
- }
1107
+ /* disable inheritance on pipe_in*/
1108
+ GOTO_CLEANUP_ON_FALSE (SetHandleInformation (pipe_in , HANDLE_FLAG_INHERIT , 0 ));
1109
1109
1110
1110
/*TODO - pick this up from system32*/
1111
1111
cmd [0 ] = L'\0' ;
0 commit comments