From: Rolf K. <lab...@us...> - 2005-06-13 08:49:15
|
Update of /cvsroot/opengtoolkit/pipe/c_source In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv32406/c_source Modified Files: pipes.c Log Message: Made proper forwarding of default standard error if no standard error redirection was specified. Changed hide window flag manipulation. Improved error handling. Index: pipes.c =================================================================== RCS file: /cvsroot/opengtoolkit/pipe/c_source/pipes.c,v retrieving revision 1.12 retrieving revision 1.13 diff -C2 -d -r1.12 -r1.13 *** pipes.c 12 Jun 2005 15:52:59 -0000 1.12 --- pipes.c 13 Jun 2005 08:48:35 -0000 1.13 *************** *** 159,164 **** MgErr LibAPI PipeOpenCmd(CStr cmd, uInt16 mode, uInt32 *fdIn, uInt32 *fdOut, uInt32 *fdErr, uInt32 *processID) { ! MgErr err = noErr; #if defined(MSWin) SECURITY_ATTRIBUTES saAttr = { 0 }; HANDLE hSaveStdout = 0L, hChildStdoutRd = 0L, hChildStdoutWr = 0L; --- 159,165 ---- MgErr LibAPI PipeOpenCmd(CStr cmd, uInt16 mode, uInt32 *fdIn, uInt32 *fdOut, uInt32 *fdErr, uInt32 *processID) { ! MgErr err = dvOpenErr; #if defined(MSWin) + BOOL ret; SECURITY_ATTRIBUTES saAttr = { 0 }; HANDLE hSaveStdout = 0L, hChildStdoutRd = 0L, hChildStdoutWr = 0L; *************** *** 166,170 **** HANDLE hSaveStderr = 0L, hChildStderrRd = 0L, hChildStderrWr = 0L; ! if (mode > kReadWriteMode) return mgArgErr; --- 167,173 ---- HANDLE hSaveStderr = 0L, hChildStderrRd = 0L, hChildStderrWr = 0L; ! if ((mode > kReadWriteMode) || ! ((mode == kReadMode || mode == kReadWriteMode) && !fdIn) || ! ((mode == kWriteMode || mode == kReadWriteMode) && !fdOut)) return mgArgErr; *************** *** 175,313 **** saAttr.bInheritHandle = TRUE; - if (mode == kWriteMode || mode == kReadWriteMode) - { - if (!fdOut) - goto error; - - /* Save the handle to the current STDOUT. */ - hSaveStdout = GetStdHandle(STD_OUTPUT_HANDLE); - - /* Create a pipe for the child process's STDOUT. */ - if (!CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0)) - goto error; - - /* Set the write handle to the pipe to be STDOUT. */ - if (!SetStdHandle(STD_OUTPUT_HANDLE, hChildStdoutWr)) - goto error; - - /* Create noninheritable read handle and close the inheritable read handle. */ - if (!DuplicateHandle(GetCurrentProcess(), hChildStdoutRd, - GetCurrentProcess(), (HANDLE*)fdOut, 0, - FALSE, - DUPLICATE_SAME_ACCESS)) - goto error; - CloseHandle(hChildStdoutRd); - hChildStdoutRd = 0; - } - if (mode == kReadMode || mode == kReadWriteMode) { - if (!fdIn) - goto error; - /* Save the handle to the current STDIN. */ hSaveStdin = GetStdHandle(STD_INPUT_HANDLE); /* Create a pipe for the child process's STDIN. */ ! if (!CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0)) ! goto error; ! ! /* Set the read handle to the pipe to be STDIN. */ ! if (!SetStdHandle(STD_INPUT_HANDLE, hChildStdinRd)) ! goto error; ! ! /* Duplicate the write handle to the pipe so it is not inherited. */ ! if (!DuplicateHandle(GetCurrentProcess(), hChildStdinWr, ! GetCurrentProcess(), (HANDLE*)fdIn, 0, ! FALSE, ! DUPLICATE_SAME_ACCESS)) ! goto error; ! CloseHandle(hChildStdinWr); ! hChildStdinWr = 0; } ! if (fdErr) { ! if (*fdErr) { ! /* Save the handle to the current STDERR. */ ! hSaveStderr = GetStdHandle(STD_ERROR_HANDLE); ! /* Create a pipe for the child process's STDERR. */ ! if (!CreatePipe(&hChildStderrRd, &hChildStderrWr, &saAttr, 0)) ! goto error; ! /* Set the write handle to the pipe to be STDERR. */ ! if (!SetStdHandle(STD_ERROR_HANDLE, hChildStderrWr)) ! goto error; ! /* Duplicate the read handle to the pipe so it is not inherited. */ ! if (!DuplicateHandle(GetCurrentProcess(), hChildStderrRd, ! GetCurrentProcess(), (HANDLE*)fdErr, 0, ! FALSE, ! DUPLICATE_SAME_ACCESS)) ! goto error; CloseHandle(hChildStderrRd); - hChildStderrRd = 0; } } /* Now create the child process. */ err = CreateChildProcess(cmd, hChildStdinRd, hChildStdoutWr, hChildStderrWr, processID); - if (err) - goto error; /* After process creation, restore the saved STDIN. STDOUT, and STDERR. Also close the intermediate handle. */ if (mode == kReadMode || mode == kReadWriteMode) { ! CloseHandle(hChildStdinRd); ! hChildStdinRd = 0; ! if (!SetStdHandle(STD_INPUT_HANDLE, hSaveStdin)) ! goto error; } if (mode == kWriteMode || mode == kReadWriteMode) { ! CloseHandle(hChildStdoutWr); ! hChildStdoutWr = 0; ! if (!SetStdHandle(STD_OUTPUT_HANDLE, hSaveStdout)) ! goto error; } if (fdErr && *fdErr) { ! CloseHandle(hChildStderrWr); ! hChildStderrWr = 0; ! if (!SetStdHandle(STD_ERROR_HANDLE, hSaveStderr)) ! goto error; } - return noErr; - error: - if (hSaveStdout) - CloseHandle(hSaveStdout); - if (hChildStdoutRd) - CloseHandle(hChildStdoutRd); - if (hChildStdoutWr) - CloseHandle(hChildStdoutWr); - if (fdOut && *fdOut) - CloseHandle(*(HANDLE*)fdOut); - if (hSaveStdin) - CloseHandle(hSaveStdin); - if (hChildStdinRd) - CloseHandle(hChildStdinRd); - if (hChildStdinWr) - CloseHandle(hChildStdinWr); - if (fdIn && *fdIn) - CloseHandle(*(HANDLE*)fdIn); - if (hSaveStderr) - CloseHandle(hSaveStderr); - if (hChildStderrRd) - CloseHandle(hChildStderrRd); - if (hChildStderrWr) - CloseHandle(hChildStderrWr); - if (fdErr && *fdErr) - CloseHandle(*(HANDLE*)fdErr); - return dvOpenErr; #elif defined(Unix) char *pmode; --- 178,296 ---- saAttr.bInheritHandle = TRUE; if (mode == kReadMode || mode == kReadWriteMode) { /* Save the handle to the current STDIN. */ hSaveStdin = GetStdHandle(STD_INPUT_HANDLE); /* Create a pipe for the child process's STDIN. */ ! ret = CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0); ! if (ret) ! { ! /* Set the read handle to the pipe to be STDIN. */ ! ret = SetStdHandle(STD_INPUT_HANDLE, hChildStdinRd); ! if (ret) ! { ! /* Duplicate the write handle to the pipe so it is not inherited. */ ! ret = DuplicateHandle(GetCurrentProcess(), hChildStdinWr, ! GetCurrentProcess(), (HANDLE*)fdIn, 0, ! FALSE, DUPLICATE_SAME_ACCESS); ! } ! CloseHandle(hChildStdinWr); ! } ! ! if (!ret) ! goto out; } ! if (mode == kWriteMode || mode == kReadWriteMode) { ! /* Save the handle to the current STDOUT. */ ! hSaveStdout = GetStdHandle(STD_OUTPUT_HANDLE); ! ! /* Create a pipe for the child process's STDOUT. */ ! ret = CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0); ! if (ret) { ! /* Set the write handle to the pipe to be STDOUT. */ ! ret = SetStdHandle(STD_OUTPUT_HANDLE, hChildStdoutWr); ! if (ret) ! { ! /* Create noninheritable read handle and close the inheritable read handle. */ ! ret = DuplicateHandle(GetCurrentProcess(), hChildStdoutRd, ! GetCurrentProcess(), (HANDLE*)fdOut, 0, ! FALSE, DUPLICATE_SAME_ACCESS); ! } ! CloseHandle(hChildStdoutRd); ! } ! if (!ret) ! goto out; ! } ! if (fdErr && *fdErr) ! { ! /* Save the handle to the current STDERR. */ ! hSaveStderr = GetStdHandle(STD_ERROR_HANDLE); ! /* Create a pipe for the child process's STDERR. */ ! ret = CreatePipe(&hChildStderrRd, &hChildStderrWr, &saAttr, 0); ! if (ret) ! { ! /* Set the write handle to the pipe to be STDERR. */ ! ret = SetStdHandle(STD_ERROR_HANDLE, hChildStderrWr); ! if (ret) ! { ! /* Duplicate the read handle to the pipe so it is not inherited. */ ! ret = DuplicateHandle(GetCurrentProcess(), hChildStderrRd, ! GetCurrentProcess(), (HANDLE*)fdErr, 0, ! FALSE, DUPLICATE_SAME_ACCESS); ! } CloseHandle(hChildStderrRd); } + + if (!ret) + goto out; + } + else + { + hChildStderrWr = GetStdHandle(STD_ERROR_HANDLE); } /* Now create the child process. */ err = CreateChildProcess(cmd, hChildStdinRd, hChildStdoutWr, hChildStderrWr, processID); + out: /* After process creation, restore the saved STDIN. STDOUT, and STDERR. Also close the intermediate handle. */ if (mode == kReadMode || mode == kReadWriteMode) { ! if (hChildStdinRd) ! CloseHandle(hChildStdinRd); ! SetStdHandle(STD_INPUT_HANDLE, hSaveStdin); } if (mode == kWriteMode || mode == kReadWriteMode) { ! if (hChildStdoutWr) ! CloseHandle(hChildStdoutWr); ! SetStdHandle(STD_OUTPUT_HANDLE, hSaveStdout); } if (fdErr && *fdErr) { ! if (hChildStderrWr) ! CloseHandle(hChildStderrWr); ! SetStdHandle(STD_ERROR_HANDLE, hSaveStderr); ! } ! ! if (err) ! { ! if (fdIn && *fdIn) ! CloseHandle(*(HANDLE*)fdIn); ! if (fdOut && *fdOut) ! CloseHandle(*(HANDLE*)fdOut); ! if (fdErr && *fdErr) ! CloseHandle(*(HANDLE*)fdErr); } #elif defined(Unix) char *pmode; *************** *** 474,478 **** { PROCESS_INFORMATION piProcInfo; ! STARTUPINFO siStartInfo; BOOL bFuncRetn = FALSE; --- 457,461 ---- { PROCESS_INFORMATION piProcInfo; ! STARTUPINFOA siStartInfo; BOOL bFuncRetn = FALSE; *************** *** 483,488 **** ZeroMemory( &siStartInfo, sizeof(STARTUPINFO) ); siStartInfo.cb = sizeof(STARTUPINFO); ! siStartInfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW; ! siStartInfo.wShowWindow = (pid && *pid) ? SW_HIDE : SW_SHOW; siStartInfo.hStdInput = stdIn; siStartInfo.hStdOutput = stdOut; --- 466,473 ---- ZeroMemory( &siStartInfo, sizeof(STARTUPINFO) ); siStartInfo.cb = sizeof(STARTUPINFO); ! siStartInfo.dwFlags = STARTF_USESTDHANDLES; ! if (pid && *pid) ! siStartInfo.dwFlags |= STARTF_USESHOWWINDOW; ! siStartInfo.wShowWindow = SW_HIDE; siStartInfo.hStdInput = stdIn; siStartInfo.hStdOutput = stdOut; *************** *** 502,516 **** if (bFuncRetn == 0) - { return mFullErr; ! } ! else ! { ! if (pid) ! *pid = piProcInfo.dwProcessId; ! CloseHandle(piProcInfo.hProcess); ! CloseHandle(piProcInfo.hThread); ! return noErr; ! } } --- 487,497 ---- if (bFuncRetn == 0) return mFullErr; ! ! if (pid) ! *pid = piProcInfo.dwProcessId; ! CloseHandle(piProcInfo.hProcess); ! CloseHandle(piProcInfo.hThread); ! return noErr; } |