Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 17 additions & 12 deletions lib/MetaProcessor/MetaProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -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
}

Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions lib/UserInterface/textinput/TerminalDisplayUnix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down