terminal redirection

wxProcess *process = new wxProcess(this); // by giving the process this frame, we are notified when it dies


this is a bit tricky (and took 5 hours to figure out). It turns out that many systems act differently when output is redirected to a file than to a terminal. For example “ls” gives different results than “ls > t” and looking at “t”.

This is an issue for us, because the way the buffering works makes many printf statements get buffered so nothing shows up. The workaround is the program called “script” which is used to write down a script of terminal commands to a file. We can invoke it with the -c option to run a command, and act like a terminal. We then tell it to send its file to /dev/null and then capture its output through standard output redirection.

Finally, we must invoke the process with wxEXEC_MAKE_GROUP_LEADER so we can later kill it with wxKILL_CHILDREN, which causes everything to close correctly.

wxString processStr = wxT("script -c ") + m_current_dir + m_text_ctrls->Item(index)->GetValue() + (wxT(" /dev/null"));
int pid = int(wxExecute(processStr, wxEXEC_ASYNC | wxEXEC_MAKE_GROUP_LEADER, process));