diff options
author | David Tardon <dtardon@redhat.com> | 2012-04-16 16:15:41 +0200 |
---|---|---|
committer | David Tardon <dtardon@redhat.com> | 2012-04-16 20:23:03 +0200 |
commit | df02932ed3fd6f15b2886e6e69ec04ee5b3ad72f (patch) | |
tree | 5d3cb163dfb10d8a06725c81bbcea92d03369331 /desktop | |
parent | f039cd3e484cda1b4d2fa6d6f3c3efb97d26d4da (diff) |
convert tabs to spaces
Diffstat (limited to 'desktop')
-rw-r--r-- | desktop/win32/source/guistdio/guistdio.inc | 447 |
1 files changed, 225 insertions, 222 deletions
diff --git a/desktop/win32/source/guistdio/guistdio.inc b/desktop/win32/source/guistdio/guistdio.inc index a62a0a62ef79..58b5bffcc448 100644 --- a/desktop/win32/source/guistdio/guistdio.inc +++ b/desktop/win32/source/guistdio/guistdio.inc @@ -1,7 +1,8 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ /************************************************************************* * * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * + * * Copyright 2000, 2010 Oracle and/or its affiliates. * * OpenOffice.org - a multi-platform office productivity suite @@ -49,60 +50,60 @@ DWORD passOutputToConsole(HANDLE readPipe, HANDLE console) { - BYTE aBuffer[1024]; - DWORD dwRead = 0; - HANDLE hReadPipe = readPipe; - DWORD dwWritten; - - //Indicates that we read an odd number of bytes. That is, we only read half of the last - //wchar_t - bool bIncompleteWchar = false; - //fprintf, fwprintf will both send char data without the terminating zero. - //fwprintf converts the unicode string first. - //We expect here to receive unicode without the terminating zero. - //unopkg and the extension manager code MUST - //use dp_misc::writeConsole instead of using fprintf, etc. - - DWORD dwToRead = sizeof(aBuffer); - BYTE * pBuffer = aBuffer; - while ( ReadFile( hReadPipe, pBuffer, dwToRead, &dwRead, NULL ) ) - { - //If the previous ReadFile call read an odd number of bytes, then the last one was - //put at the front of the buffer. We increase the number of read bytes by one to reflect - //that one byte. - if (bIncompleteWchar) - dwRead++; - //We must make sure that only complete wchar_t|s are written. WriteConsolse takes - //the number of wchar_t|s as argument. ReadFile, however, reads bytes. - bIncompleteWchar = dwRead % 2 ? true : false; - if (bIncompleteWchar) - { - //To test this case, give aBuffer a small odd size, e.g. aBuffer[3] - //The last byte, which is the incomplete wchar_t (half of it), will not be written. - (void) WriteConsoleW( console, aBuffer, - (dwRead - 1) / 2, &dwWritten, NULL ); - - //Move the last byte to the front of the buffer, so that it is the start of the - //next string - aBuffer[0] = aBuffer[dwRead - 1]; - - //Make sure that ReadFile does not overwrite the first byte the next time - dwToRead = sizeof(aBuffer) - 1; - pBuffer = aBuffer + 1; - - } - else - { //We have read an even number of bytes. Therefore, we do not put the last incomplete - //wchar_t at the front of the buffer. We will use the complete buffer the next time - //when ReadFile is called. - dwToRead = sizeof(aBuffer); - pBuffer = aBuffer; - (void) WriteConsoleW( console, - aBuffer, dwRead / 2, &dwWritten, NULL ); - } - } - - return 0; + BYTE aBuffer[1024]; + DWORD dwRead = 0; + HANDLE hReadPipe = readPipe; + DWORD dwWritten; + + //Indicates that we read an odd number of bytes. That is, we only read half of the last + //wchar_t + bool bIncompleteWchar = false; + //fprintf, fwprintf will both send char data without the terminating zero. + //fwprintf converts the unicode string first. + //We expect here to receive unicode without the terminating zero. + //unopkg and the extension manager code MUST + //use dp_misc::writeConsole instead of using fprintf, etc. + + DWORD dwToRead = sizeof(aBuffer); + BYTE * pBuffer = aBuffer; + while ( ReadFile( hReadPipe, pBuffer, dwToRead, &dwRead, NULL ) ) + { + //If the previous ReadFile call read an odd number of bytes, then the last one was + //put at the front of the buffer. We increase the number of read bytes by one to reflect + //that one byte. + if (bIncompleteWchar) + dwRead++; + //We must make sure that only complete wchar_t|s are written. WriteConsolse takes + //the number of wchar_t|s as argument. ReadFile, however, reads bytes. + bIncompleteWchar = dwRead % 2 ? true : false; + if (bIncompleteWchar) + { + //To test this case, give aBuffer a small odd size, e.g. aBuffer[3] + //The last byte, which is the incomplete wchar_t (half of it), will not be written. + (void) WriteConsoleW( console, aBuffer, + (dwRead - 1) / 2, &dwWritten, NULL ); + + //Move the last byte to the front of the buffer, so that it is the start of the + //next string + aBuffer[0] = aBuffer[dwRead - 1]; + + //Make sure that ReadFile does not overwrite the first byte the next time + dwToRead = sizeof(aBuffer) - 1; + pBuffer = aBuffer + 1; + + } + else + { //We have read an even number of bytes. Therefore, we do not put the last incomplete + //wchar_t at the front of the buffer. We will use the complete buffer the next time + //when ReadFile is called. + dwToRead = sizeof(aBuffer); + pBuffer = aBuffer; + (void) WriteConsoleW( console, + aBuffer, dwRead / 2, &dwWritten, NULL ); + } + } + + return 0; } #endif @@ -110,23 +111,23 @@ DWORD passOutputToConsole(HANDLE readPipe, HANDLE console) #ifdef UNOPKG DWORD WINAPI OutputThread( LPVOID pParam ) { - return passOutputToConsole((HANDLE)pParam, GetStdHandle( STD_OUTPUT_HANDLE )); + return passOutputToConsole((HANDLE)pParam, GetStdHandle( STD_OUTPUT_HANDLE )); } #else DWORD WINAPI OutputThread( LPVOID pParam ) { - BYTE aBuffer[256]; - DWORD dwRead = 0; - HANDLE hReadPipe = (HANDLE)pParam; - while ( ReadFile( hReadPipe, &aBuffer, sizeof(aBuffer), &dwRead, NULL ) ) - { - DWORD dwWritten; + BYTE aBuffer[256]; + DWORD dwRead = 0; + HANDLE hReadPipe = (HANDLE)pParam; + while ( ReadFile( hReadPipe, &aBuffer, sizeof(aBuffer), &dwRead, NULL ) ) + { + DWORD dwWritten; - (void) WriteFile( GetStdHandle( STD_OUTPUT_HANDLE ), aBuffer, dwRead, &dwWritten, NULL ); - } + (void) WriteFile( GetStdHandle( STD_OUTPUT_HANDLE ), aBuffer, dwRead, &dwWritten, NULL ); + } - return 0; + return 0; } #endif //--------------------------------------------------------------------------- @@ -136,24 +137,24 @@ DWORD WINAPI OutputThread( LPVOID pParam ) #ifdef UNOPKG DWORD WINAPI ErrorThread( LPVOID pParam ) { - return passOutputToConsole((HANDLE)pParam, GetStdHandle( STD_ERROR_HANDLE )); + return passOutputToConsole((HANDLE)pParam, GetStdHandle( STD_ERROR_HANDLE )); } #else DWORD WINAPI ErrorThread( LPVOID pParam ) { - BYTE aBuffer[256]; - DWORD dwRead = 0; - HANDLE hReadPipe = (HANDLE)pParam; + BYTE aBuffer[256]; + DWORD dwRead = 0; + HANDLE hReadPipe = (HANDLE)pParam; - while ( ReadFile( hReadPipe, &aBuffer, sizeof(aBuffer), &dwRead, NULL ) ) - { - DWORD dwWritten; + while ( ReadFile( hReadPipe, &aBuffer, sizeof(aBuffer), &dwRead, NULL ) ) + { + DWORD dwWritten; - (void) WriteFile( GetStdHandle( STD_ERROR_HANDLE ), aBuffer, dwRead, &dwWritten, NULL ); - } + (void) WriteFile( GetStdHandle( STD_ERROR_HANDLE ), aBuffer, dwRead, &dwWritten, NULL ); + } - return 0; + return 0; } #endif //--------------------------------------------------------------------------- @@ -163,8 +164,8 @@ DWORD WINAPI ErrorThread( LPVOID pParam ) DWORD WINAPI InputThread( LPVOID pParam ) { - DWORD dwRead = 0; - HANDLE hWritePipe = (HANDLE)pParam; + DWORD dwRead = 0; + HANDLE hWritePipe = (HANDLE)pParam; //We need to read in the complete input until we encounter a new line before //converting to Unicode. This is necessary because the input string can use @@ -179,16 +180,16 @@ DWORD WINAPI InputThread( LPVOID pParam ) char * mbBuff = new char[cNewLine]; WideCharToMultiByte( GetConsoleCP(), 0, L"\r\n", 2, mbBuff, cNewLine, NULL, NULL); - + const size_t dwBufferSize = 256; char* readBuf = (char*) malloc(dwBufferSize); int readAll = 0; size_t curBufSize = dwBufferSize; - + while ( ReadFile( GetStdHandle( STD_INPUT_HANDLE ), readBuf + readAll, curBufSize - readAll, &dwRead, NULL ) ) - { + { readAll += dwRead; int lastBufSize = curBufSize; //Grow the buffer if necessary @@ -197,8 +198,8 @@ DWORD WINAPI InputThread( LPVOID pParam ) curBufSize *= 2; readBuf = (char *) realloc(readBuf, curBufSize); } - - //If the buffer was filled completely then + + //If the buffer was filled completely then //there could be more input coming. But if we read from the console //and the console input fits exactly in the buffer, then the next //ReadFile would block until the users presses return, etc. @@ -222,30 +223,30 @@ DWORD WINAPI InputThread( LPVOID pParam ) MultiByteToWideChar( GetConsoleCP(), MB_PRECOMPOSED, readBuf, readAll, wideBuf, sizeWBuf); - DWORD dwWritten; + DWORD dwWritten; (void)WriteFile( hWritePipe, wideBuf, sizeWBuf * 2, &dwWritten, NULL ); delete[] wideBuf; readAll = 0; - } + } delete[] mbBuff; free(readBuf); - return 0; + return 0; } #else DWORD WINAPI InputThread( LPVOID pParam ) { - BYTE aBuffer[256]; - DWORD dwRead = 0; - HANDLE hWritePipe = (HANDLE)pParam; + BYTE aBuffer[256]; + DWORD dwRead = 0; + HANDLE hWritePipe = (HANDLE)pParam; - while ( ReadFile( GetStdHandle( STD_INPUT_HANDLE ), &aBuffer, sizeof(aBuffer), &dwRead, NULL ) ) - { - DWORD dwWritten; - (void) WriteFile( hWritePipe, aBuffer, dwRead, &dwWritten, NULL ); - } + while ( ReadFile( GetStdHandle( STD_INPUT_HANDLE ), &aBuffer, sizeof(aBuffer), &dwRead, NULL ) ) + { + DWORD dwWritten; + (void) WriteFile( hWritePipe, aBuffer, dwRead, &dwWritten, NULL ); + } - return 0; + return 0; } #endif @@ -255,14 +256,14 @@ DWORD WINAPI InputThread( LPVOID pParam ) DWORD WINAPI WaitForUIThread( LPVOID pParam ) { - HANDLE hProcess = (HANDLE)pParam; + HANDLE hProcess = (HANDLE)pParam; #ifndef UNOPKG - if ( !_tgetenv( TEXT("UNOPKG") ) ) - WaitForInputIdle( hProcess, INFINITE ); + if ( !_tgetenv( TEXT("UNOPKG") ) ) + WaitForInputIdle( hProcess, INFINITE ); #endif - return 0; + return 0; } @@ -270,14 +271,14 @@ DWORD WINAPI WaitForUIThread( LPVOID pParam ) // Ctrl-Break handler that terminates the child process if Ctrl-C was pressed //--------------------------------------------------------------------------- -HANDLE hTargetProcess = INVALID_HANDLE_VALUE; +HANDLE hTargetProcess = INVALID_HANDLE_VALUE; BOOL WINAPI CtrlBreakHandler( DWORD // control signal type ) { - TerminateProcess( hTargetProcess, 255 ); - return TRUE; + TerminateProcess( hTargetProcess, 255 ); + return TRUE; } @@ -291,159 +292,161 @@ int main( int, char ** ) int _tmain( int, _TCHAR ** ) #endif { - TCHAR szTargetFileName[MAX_PATH] = TEXT(""); - STARTUPINFO aStartupInfo; - PROCESS_INFORMATION aProcessInfo; + TCHAR szTargetFileName[MAX_PATH] = TEXT(""); + STARTUPINFO aStartupInfo; + PROCESS_INFORMATION aProcessInfo; + + ZeroMemory( &aStartupInfo, sizeof(aStartupInfo) ); + aStartupInfo.cb = sizeof(aStartupInfo); + aStartupInfo.dwFlags = STARTF_USESTDHANDLES; - ZeroMemory( &aStartupInfo, sizeof(aStartupInfo) ); - aStartupInfo.cb = sizeof(aStartupInfo); - aStartupInfo.dwFlags = STARTF_USESTDHANDLES; + // Create an output pipe where the write end is inheritable - // Create an output pipe where the write end is inheritable + HANDLE hOutputRead, hOutputWrite; - HANDLE hOutputRead, hOutputWrite; - - if ( CreatePipe( &hOutputRead, &hOutputWrite, NULL, 0 ) ) - { - HANDLE hTemp; + if ( CreatePipe( &hOutputRead, &hOutputWrite, NULL, 0 ) ) + { + HANDLE hTemp; - DuplicateHandle( GetCurrentProcess(), hOutputWrite, GetCurrentProcess(), &hTemp, 0, TRUE, DUPLICATE_SAME_ACCESS ); - CloseHandle( hOutputWrite ); - hOutputWrite = hTemp; + DuplicateHandle( GetCurrentProcess(), hOutputWrite, GetCurrentProcess(), &hTemp, 0, TRUE, DUPLICATE_SAME_ACCESS ); + CloseHandle( hOutputWrite ); + hOutputWrite = hTemp; - aStartupInfo.hStdOutput = hOutputWrite; - } + aStartupInfo.hStdOutput = hOutputWrite; + } - // Create an error pipe where the write end is inheritable + // Create an error pipe where the write end is inheritable - HANDLE hErrorRead, hErrorWrite; - - if ( CreatePipe( &hErrorRead, &hErrorWrite, NULL, 0 ) ) - { - HANDLE hTemp; + HANDLE hErrorRead, hErrorWrite; - DuplicateHandle( GetCurrentProcess(), hErrorWrite, GetCurrentProcess(), &hTemp, 0, TRUE, DUPLICATE_SAME_ACCESS ); - CloseHandle( hErrorWrite ); - hErrorWrite = hTemp; + if ( CreatePipe( &hErrorRead, &hErrorWrite, NULL, 0 ) ) + { + HANDLE hTemp; - aStartupInfo.hStdError = hErrorWrite; - } + DuplicateHandle( GetCurrentProcess(), hErrorWrite, GetCurrentProcess(), &hTemp, 0, TRUE, DUPLICATE_SAME_ACCESS ); + CloseHandle( hErrorWrite ); + hErrorWrite = hTemp; - // Create an input pipe where the read end is inheritable + aStartupInfo.hStdError = hErrorWrite; + } - HANDLE hInputRead, hInputWrite; + // Create an input pipe where the read end is inheritable - if ( CreatePipe( &hInputRead, &hInputWrite, NULL, 0 ) ) - { - HANDLE hTemp; + HANDLE hInputRead, hInputWrite; - DuplicateHandle( GetCurrentProcess(), hInputRead, GetCurrentProcess(), &hTemp, 0, TRUE, DUPLICATE_SAME_ACCESS ); - CloseHandle( hInputRead ); - hInputRead = hTemp; + if ( CreatePipe( &hInputRead, &hInputWrite, NULL, 0 ) ) + { + HANDLE hTemp; - aStartupInfo.hStdInput = hInputRead; - } + DuplicateHandle( GetCurrentProcess(), hInputRead, GetCurrentProcess(), &hTemp, 0, TRUE, DUPLICATE_SAME_ACCESS ); + CloseHandle( hInputRead ); + hInputRead = hTemp; - // Get image path with same name but with .exe extension + aStartupInfo.hStdInput = hInputRead; + } - TCHAR szModuleFileName[MAX_PATH]; + // Get image path with same name but with .exe extension - GetModuleFileName( NULL, szModuleFileName, MAX_PATH ); - _TCHAR *lpLastDot = _tcsrchr( szModuleFileName, '.' ); - if ( lpLastDot && 0 == _tcsicmp( lpLastDot, _T(".COM") ) ) - { - size_t len = lpLastDot - szModuleFileName; - _tcsncpy( szTargetFileName, szModuleFileName, len ); - _tcsncpy( szTargetFileName + len, _T(".EXE"), SAL_N_ELEMENTS(szTargetFileName) - len ); - } + TCHAR szModuleFileName[MAX_PATH]; - // Create process with same command line, environment and stdio handles which - // are directed to the created pipes + GetModuleFileName( NULL, szModuleFileName, MAX_PATH ); + _TCHAR *lpLastDot = _tcsrchr( szModuleFileName, '.' ); + if ( lpLastDot && 0 == _tcsicmp( lpLastDot, _T(".COM") ) ) + { + size_t len = lpLastDot - szModuleFileName; + _tcsncpy( szTargetFileName, szModuleFileName, len ); + _tcsncpy( szTargetFileName + len, _T(".EXE"), SAL_N_ELEMENTS(szTargetFileName) - len ); + } - BOOL fSuccess = CreateProcess( - szTargetFileName, - GetCommandLine(), - NULL, - NULL, - TRUE, - 0, - NULL, - NULL, - &aStartupInfo, - &aProcessInfo ); + // Create process with same command line, environment and stdio handles which + // are directed to the created pipes - if ( fSuccess ) - { - // These pipe ends are inherited by the child process and no longer used - CloseHandle( hOutputWrite ); - CloseHandle( hErrorWrite ); - CloseHandle( hInputRead ); + BOOL fSuccess = CreateProcess( + szTargetFileName, + GetCommandLine(), + NULL, + NULL, + TRUE, + 0, + NULL, + NULL, + &aStartupInfo, + &aProcessInfo ); - // Set the Ctrl-Break handler - hTargetProcess = aProcessInfo.hProcess; - SetConsoleCtrlHandler( CtrlBreakHandler, TRUE ); + if ( fSuccess ) + { + // These pipe ends are inherited by the child process and no longer used + CloseHandle( hOutputWrite ); + CloseHandle( hErrorWrite ); + CloseHandle( hInputRead ); - // Create threads that redirect remote pipe io to current process's console stdio + // Set the Ctrl-Break handler + hTargetProcess = aProcessInfo.hProcess; + SetConsoleCtrlHandler( CtrlBreakHandler, TRUE ); - DWORD dwOutputThreadId, dwErrorThreadId, dwInputThreadId; + // Create threads that redirect remote pipe io to current process's console stdio - HANDLE hOutputThread = CreateThread( NULL, 0, OutputThread, (LPVOID)hOutputRead, 0, &dwOutputThreadId ); - HANDLE hErrorThread = CreateThread( NULL, 0, OutputThread, (LPVOID)hErrorRead, 0, &dwErrorThreadId ); - HANDLE hInputThread = CreateThread( NULL, 0, InputThread, (LPVOID)hInputWrite, 0, &dwInputThreadId ); + DWORD dwOutputThreadId, dwErrorThreadId, dwInputThreadId; - // Create thread that wait until child process entered input idle + HANDLE hOutputThread = CreateThread( NULL, 0, OutputThread, (LPVOID)hOutputRead, 0, &dwOutputThreadId ); + HANDLE hErrorThread = CreateThread( NULL, 0, OutputThread, (LPVOID)hErrorRead, 0, &dwErrorThreadId ); + HANDLE hInputThread = CreateThread( NULL, 0, InputThread, (LPVOID)hInputWrite, 0, &dwInputThreadId ); - DWORD dwWaitForUIThreadId; - HANDLE hWaitForUIThread = CreateThread( NULL, 0, WaitForUIThread, (LPVOID)aProcessInfo.hProcess, 0, &dwWaitForUIThreadId ); + // Create thread that wait until child process entered input idle - DWORD dwWaitResult; - HANDLE hObjects[] = - { - hTargetProcess, - hWaitForUIThread, - hOutputThread, - hErrorThread - }; + DWORD dwWaitForUIThreadId; + HANDLE hWaitForUIThread = CreateThread( NULL, 0, WaitForUIThread, (LPVOID)aProcessInfo.hProcess, 0, &dwWaitForUIThreadId ); - #ifdef UNOPKG + DWORD dwWaitResult; + HANDLE hObjects[] = + { + hTargetProcess, + hWaitForUIThread, + hOutputThread, + hErrorThread + }; + + #ifdef UNOPKG dwWaitResult = WaitForMultipleObjects( SAL_N_ELEMENTS(hObjects), hObjects, TRUE, INFINITE ); - #else - bool bDetach = false; - int nOpenPipes = 2; - do - { - dwWaitResult = WaitForMultipleObjects( SAL_N_ELEMENTS(hObjects), hObjects, FALSE, INFINITE ); - - switch ( dwWaitResult ) - { - case WAIT_OBJECT_0: // The child process has terminated - case WAIT_OBJECT_0 + 1: // The child process entered input idle - bDetach = true; - break; - case WAIT_OBJECT_0 + 2: // The remote end of stdout pipe was closed - case WAIT_OBJECT_0 + 3: // The remote end of stderr pipe was closed - bDetach = --nOpenPipes <= 0; - break; - default: // Something went wrong - bDetach = true; - break; - } - } while( !bDetach ); - + #else + bool bDetach = false; + int nOpenPipes = 2; + do + { + dwWaitResult = WaitForMultipleObjects( SAL_N_ELEMENTS(hObjects), hObjects, FALSE, INFINITE ); + + switch ( dwWaitResult ) + { + case WAIT_OBJECT_0: // The child process has terminated + case WAIT_OBJECT_0 + 1: // The child process entered input idle + bDetach = true; + break; + case WAIT_OBJECT_0 + 2: // The remote end of stdout pipe was closed + case WAIT_OBJECT_0 + 3: // The remote end of stderr pipe was closed + bDetach = --nOpenPipes <= 0; + break; + default: // Something went wrong + bDetach = true; + break; + } + } while( !bDetach ); + #endif - CloseHandle( hOutputThread ); - CloseHandle( hErrorThread ); - CloseHandle( hInputThread ); - CloseHandle( hWaitForUIThread ); + CloseHandle( hOutputThread ); + CloseHandle( hErrorThread ); + CloseHandle( hInputThread ); + CloseHandle( hWaitForUIThread ); - DWORD dwExitCode = 0; - GetExitCodeProcess( aProcessInfo.hProcess, &dwExitCode ); - CloseHandle( aProcessInfo.hProcess ); - CloseHandle( aProcessInfo.hThread ); + DWORD dwExitCode = 0; + GetExitCodeProcess( aProcessInfo.hProcess, &dwExitCode ); + CloseHandle( aProcessInfo.hProcess ); + CloseHandle( aProcessInfo.hThread ); - return dwExitCode; - } + return dwExitCode; + } - return -1; + return -1; } + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |