diff options
author | mkaply%us.ibm.com <devnull@localhost> | 2005-04-27 19:02:53 +0000 |
---|---|---|
committer | mkaply%us.ibm.com <devnull@localhost> | 2005-04-27 19:02:53 +0000 |
commit | c63f1466f08b0ffe953821a49c433098fcc06612 (patch) | |
tree | 38a1dd36bdae4852bab1e17a8e6756ee17c29e60 | |
parent | 205296c791ccbf2d14fb1aa96b2db098f9ed4bef (diff) | |
download | nspr-hg-c63f1466f08b0ffe953821a49c433098fcc06612.tar.gz |
Bugzilla bug 265219: added support for redirecting stdin/stdout of a child
process. The patch is contributed by Davide Bresolin <davide@teamos2.it>.
r=mkaply,wtc, a=mkaply
-rw-r--r-- | pr/src/md/os2/os2misc.c | 100 |
1 files changed, 90 insertions, 10 deletions
diff --git a/pr/src/md/os2/os2misc.c b/pr/src/md/os2/os2misc.c index 4e9bb932..f155d6f1 100644 --- a/pr/src/md/os2/os2misc.c +++ b/pr/src/md/os2/os2misc.c @@ -252,6 +252,19 @@ PRProcess * _PR_CreateOS2Process( char pszObjectBuffer[CCHMAXPATH]; char *pszFormatResult = NULL; + /* + * Variables for DosExecPgm + */ + char szFailed[CCHMAXPATH]; + char *pszCmdLine = NULL; + RESULTCODES procInfo; + HFILE hStdIn = 0, + hStdOut = 0, + hStdErr = 0; + HFILE hStdInSave = -1, + hStdOutSave = -1, + hStdErrSave = -1; + proc = PR_NEW(PRProcess); if (!proc) { PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0); @@ -282,10 +295,6 @@ PRProcess * _PR_CreateOS2Process( goto errorExit; } - if (attr) { - PR_ASSERT(!"Not implemented"); - } - rc = DosQueryAppType(path, &ulAppType); if (rc != NO_ERROR) { char *pszDot = strrchr(path, '.'); @@ -341,18 +350,89 @@ PRProcess * _PR_CreateOS2Process( startData.ObjectBuffLen = CCHMAXPATH; startData.Environment = envBlock; - rc = DosStartSession(&startData, &ulAppType, &pid); + if (attr) { + /* On OS/2, there is really no way to pass file handles for stdin, + * stdout, and stderr to a new process. Instead, we can make it + * a child process and make the given file handles a copy of our + * stdin, stdout, and stderr. The child process then inherits + * ours, and we set ours back. Twisted and gross I know. If you + * know a better way, please use it. + */ + if (attr->stdinFd) { + hStdIn = 0; + DosDupHandle(hStdIn, &hStdInSave); + DosDupHandle((HFILE) attr->stdinFd->secret->md.osfd, &hStdIn); + } - if ((rc != NO_ERROR) && (rc != ERROR_SMG_START_IN_BACKGROUND)) { - PR_SetError(PR_UNKNOWN_ERROR, 0); - } + if (attr->stdoutFd) { + hStdOut = 1; + DosDupHandle(hStdOut, &hStdOutSave); + DosDupHandle((HFILE) attr->stdoutFd->secret->md.osfd, &hStdOut); + } + + if (attr->stderrFd) { + hStdErr = 2; + DosDupHandle(hStdErr, &hStdErrSave); + DosDupHandle((HFILE) attr->stderrFd->secret->md.osfd, &hStdErr); + } + /* + * Build up the Command Line for DosExecPgm + */ + pszCmdLine = PR_MALLOC(strlen(pszEXEName) + + strlen(startData.PgmInputs) + 3); + sprintf(pszCmdLine, "%s%c%s%c", pszEXEName, '\0', + startData.PgmInputs, '\0'); + rc = DosExecPgm(szFailed, + CCHMAXPATH, + EXEC_ASYNCRESULT, + pszCmdLine, + envBlock, + &procInfo, + pszEXEName); + PR_DELETE(pszCmdLine); + + /* Restore our old values. Hope this works */ + if (hStdInSave != -1) { + DosDupHandle(hStdInSave, &hStdIn); + DosClose(hStdInSave); + } + + if (hStdOutSave != -1) { + DosDupHandle(hStdOutSave, &hStdOut); + DosClose(hStdOutSave); + } + + if (hStdErrSave != -1) { + DosDupHandle(hStdErrSave, &hStdErr); + DosClose(hStdErrSave); + } + + if (rc != NO_ERROR) { + /* XXX what error code? */ + PR_SetError(PR_UNKNOWN_ERROR, rc); + goto errorExit; + } + + proc->md.pid = procInfo.codeTerminate; + } else { + /* + * If no STDIN/STDOUT redirection is not needed, use DosStartSession + * to create a new, independent session + */ + rc = DosStartSession(&startData, &ulAppType, &pid); + + if ((rc != NO_ERROR) && (rc != ERROR_SMG_START_IN_BACKGROUND)) { + PR_SetError(PR_UNKNOWN_ERROR, rc); + goto errorExit; + } - proc->md.pid = pid; + proc->md.pid = pid; + } if (pszFormatResult) { PR_DELETE(pszFormatResult); } - + PR_DELETE(cmdLine); if (newEnvp) { PR_DELETE(newEnvp); |