@@ -62,7 +62,6 @@ typedef struct consoleEvent {
6262consoleEvent * head = NULL ;
6363consoleEvent * tail = NULL ;
6464
65- BOOL isRedirected = FALSE;
6665BOOL bRet = FALSE;
6766BOOL bNoScrollRegion = FALSE;
6867BOOL bStartup = TRUE;
@@ -86,6 +85,7 @@ DWORD hostThreadId = 0;
8685DWORD childProcessId = 0 ;
8786DWORD dwStatus = 0 ;
8887DWORD currentLine = 0 ;
88+ DWORD lastLineLength = 0 ;
8989
9090UINT cp = 0 ;
9191
@@ -141,41 +141,46 @@ void SendKeyStroke(HANDLE hInput, int keyStroke, char character)
141141}
142142
143143// VT output routines
144- void SendClearScreen (HANDLE hInput ) {
144+ void SendLF (HANDLE hInput ) {
145145 DWORD wr = 0 ;
146146
147- WriteFile (hInput , "\033[2J" , 4 , & wr , NULL );
147+ if (bUseAnsiEmulation )
148+ WriteFile (hInput , "\n" , 1 , & wr , NULL );
148149}
149150
150- void SendClearScreenFromCursor (HANDLE hInput ) {
151+ void SendClearScreen (HANDLE hInput ) {
151152 DWORD wr = 0 ;
152153
153- WriteFile (hInput , "\033[1J" , 4 , & wr , NULL );
154+ if (bUseAnsiEmulation )
155+ WriteFile (hInput , "\033[2J" , 4 , & wr , NULL );
154156}
155157
156- void SendCRLF (HANDLE hInput ) {
157-
158+ void SendClearScreenFromCursor (HANDLE hInput ) {
158159 DWORD wr = 0 ;
159160
160- WriteFile (hInput , "\n" , 2 , & wr , NULL );
161+ if (bUseAnsiEmulation )
162+ WriteFile (hInput , "\033[1J" , 4 , & wr , NULL );
161163}
162164
163165void SendHideCursor (HANDLE hInput ) {
164166 DWORD wr = 0 ;
165167
166- WriteFile (hInput , "\033[?25l" , 6 , & wr , NULL );
168+ if (bUseAnsiEmulation )
169+ WriteFile (hInput , "\033[?25l" , 6 , & wr , NULL );
167170}
168171
169172void SendShowCursor (HANDLE hInput ) {
170173 DWORD wr = 0 ;
171174
172- WriteFile (hInput , "\033[?25h" , 6 , & wr , NULL );
175+ if (bUseAnsiEmulation )
176+ WriteFile (hInput , "\033[?25h" , 6 , & wr , NULL );
173177}
174178
175179void SendCursorPositionRequest (HANDLE hInput ) {
176180 DWORD wr = 0 ;
177181
178- WriteFile (hInput , "\033[6n" , 4 , & wr , NULL );
182+ if (bUseAnsiEmulation )
183+ WriteFile (hInput , "\033[6n" , 4 , & wr , NULL );
179184}
180185
181186void SendSetCursor (HANDLE hInput , int X , int Y ) {
@@ -428,8 +433,6 @@ void SizeWindow(HANDLE hInput) {
428433
429434DWORD WINAPI MonitorChild (_In_ LPVOID lpParameter ) {
430435 WaitForSingleObject (child , INFINITE );
431- if (isRedirected )
432- CloseHandle (pipe_in );
433436 PostThreadMessage (hostThreadId , WM_APPEXIT , 0 , 0 );
434437 return 0 ;
435438}
@@ -519,7 +522,7 @@ DWORD ProcessEvent(void *p) {
519522 readRect .Right = LOWORD (idChild );
520523
521524 // Detect a "cls" (Windows).
522- if (!bStartup &&
525+ if (!bStartup &&
523526 (readRect .Top == consoleInfo .srWindow .Top || readRect .Top == nextConsoleInfo .srWindow .Top ))
524527 {
525528 BOOL isClearCommand = FALSE;
@@ -555,15 +558,15 @@ DWORD ProcessEvent(void *p) {
555558
556559 if (bufferSize > MAX_EXPECTED_BUFFER_SIZE ) {
557560
558- if (!bStartup ) {
561+ if (!bStartup ) {
559562 SendClearScreen (pipe_out );
560563 ViewPortY = 0 ;
561564 lastViewPortY = 0 ;
562565 }
563566
564567 return ERROR_SUCCESS ;
565568 }
566-
569+
567570 // Create the screen scrape buffer
568571 CHAR_INFO * pBuffer = (PCHAR_INFO )malloc (sizeof (CHAR_INFO ) * bufferSize );
569572
@@ -586,6 +589,10 @@ DWORD ProcessEvent(void *p) {
586589 return dwError ;
587590 }
588591
592+ if (readRect .Top > currentLine )
593+ for (SHORT n = currentLine ; n < readRect .Top ; n ++ )
594+ SendLF (pipe_out );
595+
589596 // Set cursor location based on the reported location from the message.
590597 CalculateAndSetCursor (pipe_out , ViewPortY , viewPortHeight , readRect .Left ,
591598 readRect .Top );
@@ -594,6 +601,7 @@ DWORD ProcessEvent(void *p) {
594601 SendBuffer (pipe_out , pBuffer , bufferSize );
595602
596603 lastViewPortY = ViewPortY ;
604+ lastLineLength = readRect .Left ;
597605
598606 free (pBuffer );
599607
@@ -1062,8 +1070,6 @@ int wmain(int ac, wchar_t **av) {
10621070
10631071 cp = GetConsoleCP ();
10641072
1065- isRedirected = !GetConsoleMode (pipe_in , & dwMode );
1066-
10671073 ZeroMemory (& inputSi , sizeof (STARTUPINFO ));
10681074 GetStartupInfo (& inputSi );
10691075
@@ -1086,13 +1092,10 @@ int wmain(int ac, wchar_t **av) {
10861092 hostThreadId = GetCurrentThreadId ();
10871093 hostProcessId = GetCurrentProcessId ();
10881094
1089- if (isRedirected )
1090- {
1091- InitializeCriticalSection (& criticalSection );
1095+ InitializeCriticalSection (& criticalSection );
10921096
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 );
10961099
10971100 memset (& si , 0 , sizeof (STARTUPINFO ));
10981101 memset (& pi , 0 , sizeof (PROCESS_INFORMATION ));
@@ -1101,11 +1104,8 @@ int wmain(int ac, wchar_t **av) {
11011104 si .cb = sizeof (STARTUPINFO );
11021105 si .dwFlags = 0 ;
11031106
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 ));
11091109
11101110 /*TODO - pick this up from system32*/
11111111 cmd [0 ] = L'\0' ;
0 commit comments