From: Rolf K. <lab...@us...> - 2005-06-12 11:30:37
|
Update of /cvsroot/opengtoolkit/pipe/c_source In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv10740/c_source Modified Files: pipes.c Log Message: Fix a broken std input pipe Index: pipes.c =================================================================== RCS file: /cvsroot/opengtoolkit/pipe/c_source/pipes.c,v retrieving revision 1.10 retrieving revision 1.11 diff -C2 -d -r1.10 -r1.11 *** pipes.c 7 Jun 2005 16:29:25 -0000 1.10 --- pipes.c 12 Jun 2005 11:30:19 -0000 1.11 *************** *** 160,164 **** { MgErr err = noErr; - uInt32 retErr = fdErr ? *fdErr : 0; #if defined(MSWin) SECURITY_ATTRIBUTES saAttr = { 0 }; --- 160,163 ---- *************** *** 170,177 **** return mgArgErr; - *fdIn = 0; - *fdOut = 0; - *fdErr = 0; - DebugBreaking(); --- 169,172 ---- *************** *** 182,185 **** --- 177,183 ---- if (mode == kWriteMode || mode == kReadWriteMode) { + if (!fdOut) + goto error; + /* Save the handle to the current STDOUT. */ hSaveStdout = GetStdHandle(STD_OUTPUT_HANDLE); *************** *** 189,193 **** goto error; ! /* Set a write handle to the pipe to be STDOUT. */ if (!SetStdHandle(STD_OUTPUT_HANDLE, hChildStdoutWr)) goto error; --- 187,191 ---- goto error; ! /* Set the write handle to the pipe to be STDOUT. */ if (!SetStdHandle(STD_OUTPUT_HANDLE, hChildStdoutWr)) goto error; *************** *** 195,199 **** /* Create noninheritable read handle and close the inheritable read handle. */ if (!DuplicateHandle(GetCurrentProcess(), hChildStdoutRd, ! GetCurrentProcess(), (HANDLE*)fdOut , 0, FALSE, DUPLICATE_SAME_ACCESS)) --- 193,197 ---- /* Create noninheritable read handle and close the inheritable read handle. */ if (!DuplicateHandle(GetCurrentProcess(), hChildStdoutRd, ! GetCurrentProcess(), (HANDLE*)fdOut, 0, FALSE, DUPLICATE_SAME_ACCESS)) *************** *** 205,208 **** --- 203,209 ---- if (mode == kReadMode || mode == kReadWriteMode) { + if (!fdIn) + goto error; + /* Save the handle to the current STDIN. */ hSaveStdin = GetStdHandle(STD_INPUT_HANDLE); *************** *** 212,216 **** goto error; ! /* Set a read handle to the pipe to be STDIN. */ if (!SetStdHandle(STD_INPUT_HANDLE, hChildStdinRd)) goto error; --- 213,217 ---- goto error; ! /* Set the read handle to the pipe to be STDIN. */ if (!SetStdHandle(STD_INPUT_HANDLE, hChildStdinRd)) goto error; *************** *** 223,250 **** goto error; CloseHandle(hChildStdinWr); ! hChildStdinRd = 0; } ! if (retErr) { ! /* 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 a read 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; } --- 224,254 ---- 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; ! } } *************** *** 272,276 **** } ! if (retErr) { CloseHandle(hChildStderrWr); --- 276,280 ---- } ! if (fdErr && *fdErr) { CloseHandle(hChildStderrWr); *************** *** 287,291 **** if (hChildStdoutWr) CloseHandle(hChildStdoutWr); ! if (*fdOut) CloseHandle(*(HANDLE*)fdOut); if (hSaveStdin) --- 291,295 ---- if (hChildStdoutWr) CloseHandle(hChildStdoutWr); ! if (fdOut && *fdOut) CloseHandle(*(HANDLE*)fdOut); if (hSaveStdin) *************** *** 295,299 **** if (hChildStdinWr) CloseHandle(hChildStdinWr); ! if (*fdIn) CloseHandle(*(HANDLE*)fdIn); if (hSaveStderr) --- 299,303 ---- if (hChildStdinWr) CloseHandle(hChildStdinWr); ! if (fdIn && *fdIn) CloseHandle(*(HANDLE*)fdIn); if (hSaveStderr) *************** *** 303,307 **** if (hChildStderrWr) CloseHandle(hChildStderrWr); ! if (*fdErr) CloseHandle(*(HANDLE*)fdErr); return dvOpenErr; --- 307,311 ---- if (hChildStderrWr) CloseHandle(hChildStderrWr); ! if (fdErr && *fdErr) CloseHandle(*(HANDLE*)fdErr); return dvOpenErr; *************** *** 353,362 **** #elif defined(Unix) if (close((pipe_t)fd) < 0) ! ..err = UnixToLVErr(errno); ! #else err = mgNotImplementd: #endif ! return err;} MgErr LibAPI PipeRead(uInt32 fd, uInt32 *bytesRead, LStrHandle data, uInt32 *eof) --- 357,366 ---- #elif defined(Unix) if (close((pipe_t)fd) < 0) ! err = UnixToLVErr(errno); #else err = mgNotImplementd: #endif ! return err; ! } MgErr LibAPI PipeRead(uInt32 fd, uInt32 *bytesRead, LStrHandle data, uInt32 *eof) *************** *** 366,371 **** #if defined(MSWin) DWORD ret; - #elif defined(Unix) - int32 len; #endif --- 370,373 ---- *************** *** 375,389 **** *eof = FALSE; ! err = NumericArrayResize(uB, 1, (UHandle*)&data, bytesReq); ! if (err) ! return err; ! ! /* We need to do this in LabVIEW 7.0 as otherwise the next ! NumericArrayResize() will optimize out and only copy as much ! characters as it sees to be in the array into the new handle ! if any. */ ! LStrLen(*data) = bytesReq; #if defined(MSWin) if (!PeekNamedPipe((HANDLE)fd, NULL, 0, NULL, bytesRead, NULL)) { --- 377,400 ---- *eof = FALSE; ! if (LStrLen(*data) < (int32)bytesReq) ! { ! err = NumericArrayResize(uB, 1, (UHandle*)&data, bytesReq); ! if (err) ! return err; + /* We need to do this in LabVIEW 7.0 as otherwise the next + NumericArrayResize() will optimize out and only copy as much + characters into the new handle as it believes to be in the + array based on this value. */ + LStrLen(*data) = bytesReq; + } #if defined(MSWin) + /* ReadFile blocks on any pipe not opened in overlapped mode and the anonymous + pipes used for standard handle redirection are always non-overlapping. We don't + want to block here, as for LabVIEW configurations with only one thread per + execution system we would block the ReadPipe function and can't read another + pipe. So we peek first at the pipe to see if there is anything available and if so + we invoke ReadFile to read the entire data instead of maybe only part of a byte- + stream sent from the other side.*/ if (!PeekNamedPipe((HANDLE)fd, NULL, 0, NULL, bytesRead, NULL)) { *************** *** 394,403 **** *bytesRead = 0; } - if (bytesReq < *bytesRead) - *bytesRead = bytesReq; if (*bytesRead) ! { ! if (!ReadFile((HANDLE)fd, LStrBuf(*data), *bytesRead, bytesRead, NULL)) { ret = GetLastError(); --- 405,413 ---- *bytesRead = 0; } if (*bytesRead) ! { ! /* There are some bytes to read, so try to read as much as possible now! */ ! if (!ReadFile((HANDLE)fd, LStrBuf(*data), bytesReq, bytesRead, NULL)) { ret = GetLastError(); *************** *** 406,442 **** err = Win32ToLVErr(ret); } ! else ! { ! if (*bytesRead) ! *eof = *bytesRead == 0; ! } ! } ! #elif defined(Unix) ! len = read((pipe_t)fd, LStrBuf(*data), *bytesRead) ! if (len < 0) { *eof = (errno == EEOF); if (!*eof) err = UnixToLVErr(errno); } - else - { - *bytesRead = len; - } - #else err = mgNotImplementd; #endif - - - if (bytesReq != *bytesRead) - { - if (err) - NumericArrayResize(uB, 1, &(UHandle)data, *bytesRead); - else - err = NumericArrayResize(uB, 1, &(UHandle)data, *bytesRead); - LStrLen(*data) = *bytesRead; - } return err; } --- 416,434 ---- err = Win32ToLVErr(ret); } ! } #elif defined(Unix) ! *bytesRead = read((pipe_t)fd, LStrBuf(*data), bytesReq) ! if (*bytesRead < 0) { *eof = (errno == EEOF); if (!*eof) + { err = UnixToLVErr(errno); + *bytesRead = 0; + } } #else err = mgNotImplementd; #endif return err; } *************** *** 445,473 **** { MgErr err = noErr; - int32 len; ! if (!fd || !data) return mgArgErr; - DebugBreaking(); - /* NULL terminate the LabVIEW string so that the API functions can use ! them a C strings */ ! len = LStrLen(*data); ! err = NumericArrayResize(uB, 1, (UHandle*)&data, len + 1); if (err) return err; ! LStrBuf(*data)[len]; #if defined(MSWin) ! if (!WriteFile((HANDLE)fd, LStrBuf(*data), len, bytesWritten, NULL) ) err = Win32ToLVErr(GetLastError()); #elif defined(Unix) ! len = write((pipe_t)fd, LStrBuf(*data), LStrLen(*data); ! if (len < 0) err = UnixToLVErr(errno); ! else ! *bytesWritten = len; #else err = mgNotImplementd: --- 437,463 ---- { MgErr err = noErr; ! if (!fd || !bytesWritten || !data) return mgArgErr; /* NULL terminate the LabVIEW string so that the API functions can use ! them as a C string */ ! *bytesWritten = LStrLen(*data); ! err = NumericArrayResize(uB, 1, (UHandle*)&data, *bytesWritten + 1); if (err) return err; ! LStrBuf(*data)[*bytesWritten] = 0; #if defined(MSWin) ! if (!WriteFile((HANDLE)fd, LStrBuf(*data), *bytesWritten, bytesWritten, NULL) ) err = Win32ToLVErr(GetLastError()); #elif defined(Unix) ! *bytesWritten = write((pipe_t)fd, LStrBuf(*data), *bytesWritten); ! if (*bytesWritten < 0) ! { ! *bytesWritten = 0; err = UnixToLVErr(errno); ! } #else err = mgNotImplementd: *************** *** 493,497 **** siStartInfo.cb = sizeof(STARTUPINFO); siStartInfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW; ! siStartInfo.wShowWindow = SW_HIDE; siStartInfo.hStdInput = stdIn; siStartInfo.hStdOutput = stdOut; --- 483,487 ---- siStartInfo.cb = sizeof(STARTUPINFO); siStartInfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW; ! siStartInfo.wShowWindow = (pid && *pid) ? SW_HIDE : SW_SHOW; siStartInfo.hStdInput = stdIn; siStartInfo.hStdOutput = stdOut; |