From: Vadim Zeitlin Date: Wed, 3 Jul 2013 00:33:04 +0000 (+0000) Subject: Don't ignore child process output if it exits with -1 exit code. X-Git-Url: https://git.saurik.com/wxWidgets.git/commitdiff_plain/d1c063b90f4d09656f7b9b42dbad8e8f59c752b5 Don't ignore child process output if it exits with -1 exit code. While this code is used by us if the program couldn't be launched at all, it doesn't mean that it didn't run as -1 could also be returned by the child process to indicate an error after outputting something, so we should still read its output in this case. Closes #15205. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@74352 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- diff --git a/docs/changes.txt b/docs/changes.txt index ca2dc13101..d72694e1a3 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -575,6 +575,7 @@ All: wxEVT_COMMAND_MENU_SELECTED is now wxEVT_MENU (but the old name remains available for compatibility) (Catalin Raceanu). - Fix wxExecute() implementation under Unix (Rob Bresalier). +- Also fix reading output from children exiting with -1 () (Jonathan Dagresta). - Add wxEvtHandler::CallAfter() method for asynchronous method calls. - Add support for symlinks to wxFileName (David Hart). - Add wxDIR_NO_FOLLOW flag for wxDir traversal (David Hart). diff --git a/samples/exec/exec.cpp b/samples/exec/exec.cpp index d72c89a7ad..ada37bb7ac 100644 --- a/samples/exec/exec.cpp +++ b/samples/exec/exec.cpp @@ -962,11 +962,8 @@ void MyFrame::OnExecWithRedirect(wxCommandEvent& WXUNUSED(event)) wxLogStatus("Command \"%s\" terminated after %ldms; exit code %d.", cmd, sw.Time(), code); - if ( code != -1 ) - { - ShowOutput(cmd, output, wxT("Output")); - ShowOutput(cmd, errors, wxT("Errors")); - } + ShowOutput(cmd, output, wxT("Output")); + ShowOutput(cmd, errors, wxT("Errors")); } else // async exec { diff --git a/src/common/utilscmn.cpp b/src/common/utilscmn.cpp index a451976b17..f5ba00045b 100644 --- a/src/common/utilscmn.cpp +++ b/src/common/utilscmn.cpp @@ -618,13 +618,15 @@ bool wxGetEnvMap(wxEnvVariableHashMap *map) // wxExecute // ---------------------------------------------------------------------------- -// wxDoExecuteWithCapture() helper: reads an entire stream into one array +// wxDoExecuteWithCapture() helper: reads an entire stream into one array if +// the stream is non-NULL (it doesn't do anything if it's NULL). // // returns true if ok, false if error #if wxUSE_STREAMS static bool ReadAll(wxInputStream *is, wxArrayString& output) { - wxCHECK_MSG( is, false, wxT("NULL stream in wxExecute()?") ); + if ( !is ) + return true; // the stream could be already at EOF or in wxSTREAM_BROKEN_PIPE state is->Reset(); @@ -671,17 +673,16 @@ static long wxDoExecuteWithCapture(const wxString& command, long rc = wxExecute(command, wxEXEC_SYNC | flags, process, env); #if wxUSE_STREAMS - if ( rc != -1 ) + // Notice that while -1 indicates an error exit code for us, a program + // exiting with this code could still have written something to its stdout + // and, especially, stderr, so we still need to read from them. + if ( !ReadAll(process->GetInputStream(), output) ) + rc = -1; + + if ( error ) { - if ( !ReadAll(process->GetInputStream(), output) ) + if ( !ReadAll(process->GetErrorStream(), *error) ) rc = -1; - - if ( error ) - { - if ( !ReadAll(process->GetErrorStream(), *error) ) - rc = -1; - } - } #else wxUnusedVar(output);