From 1dae3a81011d386206a6eb02d220da415e512716 Mon Sep 17 00:00:00 2001 From: Frederich Munch Date: Tue, 7 Feb 2017 16:04:59 -0500 Subject: [PATCH 1/2] Revert "Must flush stdout, else a "lazy" stdout will not flush before prompt." This reverts commit bd6cbaf3b69f1bf52055c4d7071d9174b97c7a9d. --- lib/UserInterface/textinput/TerminalDisplayUnix.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/UserInterface/textinput/TerminalDisplayUnix.cpp b/lib/UserInterface/textinput/TerminalDisplayUnix.cpp index 5db2ea6e79..291788c5c3 100644 --- a/lib/UserInterface/textinput/TerminalDisplayUnix.cpp +++ b/lib/UserInterface/textinput/TerminalDisplayUnix.cpp @@ -124,8 +124,8 @@ namespace textinput { } } -// Don't rely on flushing here. -#define SYNC_OUT(fd) if (fd==STDOUT_FILENO) { ::fflush(stdout); } +// Don't rely on flushing here. REALLY, try to fix it elsewhere! +#define SYNC_OUT(fd) /*if (fd==STDOUT_FILENO) { ::fflush(stdout); } }*/ TerminalDisplayUnix::~TerminalDisplayUnix() { Detach(); From ed37482d5174345cd3333a3c1f503bf99a48c793 Mon Sep 17 00:00:00 2001 From: Frederich Munch Date: Tue, 7 Feb 2017 20:00:54 -0500 Subject: [PATCH 2/2] Fix stdout being reopened without line buffering if redirection occurs before output has been written. --- lib/MetaProcessor/MetaProcessor.cpp | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/lib/MetaProcessor/MetaProcessor.cpp b/lib/MetaProcessor/MetaProcessor.cpp index a9fea4f801..77373895a6 100644 --- a/lib/MetaProcessor/MetaProcessor.cpp +++ b/lib/MetaProcessor/MetaProcessor.cpp @@ -135,22 +135,18 @@ namespace cling { } // Restore stdstream from backup and close the backup - void close(int oldfd, int newfd) { + void close(int &oldfd, int newfd) { assert((newfd == STDOUT_FILENO || newfd == STDERR_FILENO) && "Not std FD"); assert(oldfd == m_Bak[newfd == STDERR_FILENO] && "Not backup FD"); if (oldfd != kInvalidFD) { dup2(oldfd, newfd, "RedirectOutput::close"); ::close(oldfd); + oldfd = kInvalidFD; } } - void reset(int oldfd, int newfd, FILE *F) { - fflush(F); - dup2(oldfd, newfd, "RedirectOutput::reset"); - } - int restore(int FD, FILE *F, MetaProcessor::RedirectionScope Flag, - int bakFD) { + int &bakFD) { // If no backup, we have never redirected the file, so nothing to restore if (bakFD != kInvalidFD) { // Find the last redirect for the scope, and restore redirection to it @@ -165,9 +161,10 @@ namespace cling { } // No redirection for this scope, restore to backup - reset(bakFD, FD, F); + fflush(F); + close(bakFD, FD); } - return bakFD; + return kInvalidFD; } public: @@ -187,6 +184,12 @@ namespace cling { // State 2, was tty to begin with, then redirected to stderr and back. if (m_TTY == 2) ::freopen("CON", "w", stdout); +#else + // If redirection took place without writing anything to the terminal + // beforehand (--nologo) then the dup2 relinking stdout will have caused + // it to be re-opened without line buffering. + if (m_TTY) + ::setvbuf(stdout, NULL, _IOLBF, BUFSIZ); #endif } @@ -254,10 +257,12 @@ namespace cling { return; if (toBackup) { - if (m_Bak[0] != kInvalidFD) - reset(m_Bak[0], STDOUT_FILENO, stdout); + if (m_Bak[0] != kInvalidFD) { + fflush(stdout); + dup2(m_Bak[0], STDOUT_FILENO, "RedirectOutput::resetStdOut"); + } } else if (m_CurStdOut != kInvalidFD) - dup2(m_CurStdOut, STDOUT_FILENO, "RedirectOutput::reset"); + dup2(m_CurStdOut, STDOUT_FILENO, "RedirectOutput::resetStdOut"); } bool empty() const {