diff --git a/interpreter/cling/include/cling/MetaProcessor/MetaProcessor.h b/interpreter/cling/include/cling/MetaProcessor/MetaProcessor.h index 059db06ecdb36..aa63e5804fea2 100644 --- a/interpreter/cling/include/cling/MetaProcessor/MetaProcessor.h +++ b/interpreter/cling/include/cling/MetaProcessor/MetaProcessor.h @@ -174,12 +174,18 @@ namespace cling { ///\brief Set the stdout and stderr stream to the appropriate file. /// - ///\param [in] file - The file for teh redirection + ///\param [in] file - The file for the redirection. ///\param [in] stream - Which stream to redirect: stdout, stderr or both. ///\param [in] append - Write in append mode. /// void setStdStream(llvm::StringRef file, RedirectionScope stream, bool append); + private: + ///\brief Set a stream to a file + /// + ///\param [in] file - The file for the redirection. + void setFileStream(std::string& fileStorage, llvm::StringRef file, + bool append); }; } // end namespace cling diff --git a/interpreter/cling/lib/MetaProcessor/MetaParser.cpp b/interpreter/cling/lib/MetaProcessor/MetaParser.cpp index bfa62244a179b..03322e672c97a 100644 --- a/interpreter/cling/lib/MetaProcessor/MetaParser.cpp +++ b/interpreter/cling/lib/MetaProcessor/MetaParser.cpp @@ -159,7 +159,8 @@ namespace cling { stream = MetaProcessor::kSTDERR; // Wrong constant_FD, do not redirect. } else if (constant_FD != 1) { - llvm::errs() << "cling::MetaParser::isRedirectCommand(): invalid file descriptor number " << constant_FD << "\n"; + llvm::errs() << "cling::MetaParser::isRedirectCommand():" + << "invalid file descriptor number " << constant_FD <<"\n"; return true; } consumeToken(); @@ -301,7 +302,8 @@ namespace cling { } else { consumeAnyStringToken(tok::eof); const Token& lastStringToken = getCurTok(); - if (lastStringToken.is(tok::raw_ident) && lastStringToken.getLength()) { + if (lastStringToken.is(tok::raw_ident) + && lastStringToken.getLength()) { int level = 0; if (!lastStringToken.getIdent().getAsInteger(10, level) && level >= 0) { //TODO: process .O XXX diff --git a/interpreter/cling/lib/MetaProcessor/MetaProcessor.cpp b/interpreter/cling/lib/MetaProcessor/MetaProcessor.cpp index 27ffc428b7821..b75963a1d5f6e 100644 --- a/interpreter/cling/lib/MetaProcessor/MetaProcessor.cpp +++ b/interpreter/cling/lib/MetaProcessor/MetaProcessor.cpp @@ -34,10 +34,12 @@ using namespace clang; namespace cling { - MetaProcessor::MaybeRedirectOutputRAII::MaybeRedirectOutputRAII(MetaProcessor* p) + MetaProcessor::MaybeRedirectOutputRAII::MaybeRedirectOutputRAII( + MetaProcessor* p) :m_MetaProcessor(p), m_PrevStdoutFileName(""), m_PrevStderrFileName("") { //Empty file acts as a flag. - redirect(STDOUT_FILENO, m_PrevStdoutFileName, m_MetaProcessor->m_FileOut, stdout); + redirect(STDOUT_FILENO, m_PrevStdoutFileName, m_MetaProcessor->m_FileOut, + stdout); // Deal with the case 2>&1 and 2&>1 if (strcmp(m_MetaProcessor->m_FileErr.c_str(), "_IO_2_1_stdout_") == 0) { SmallString<1024> stdoutName; @@ -45,7 +47,8 @@ namespace cling { m_MetaProcessor->m_FileErr = stdoutName.c_str(); } //Empty file acts as a flag. - redirect(STDERR_FILENO, m_PrevStderrFileName, m_MetaProcessor->m_FileErr, stderr); + redirect(STDERR_FILENO, m_PrevStderrFileName, m_MetaProcessor->m_FileErr, + stderr); } @@ -56,12 +59,14 @@ namespace cling { } bool MetaProcessor::MaybeRedirectOutputRAII::cacheStd(int fd, - llvm::SmallVectorImpl& file) { + llvm::SmallVectorImpl& file) { - int ttyname_Result = ttyname_r(fd, const_cast(file.data()), file.capacity()); + int ttyname_Result = ttyname_r(fd, const_cast(file.data()), + file.capacity()); while (ttyname_Result == ERANGE) { file.reserve(16*file.capacity()); - ttyname_Result = ttyname_r(fd, const_cast(file.data()), file.capacity()); + ttyname_Result = ttyname_r(fd, const_cast(file.data()), + file.capacity()); } if (ttyname_Result == 0) { @@ -72,22 +77,25 @@ namespace cling { } else if (ttyname_Result == ENOTTY) { llvm::errs() << "File descriptor does not refer to a terminal device."; } else if (ttyname_Result == EAGAIN) { - llvm::errs() << "The device driver was in use by another process, or the driver" - << "was unable to carry out the request due to an outstanding command in progress."; + llvm::errs() << "The device driver was in use by another process, or the" + << "driver was unable to carry out the request due to an" + << "outstanding command in progress."; } else if (ttyname_Result == EINTR) { llvm::errs() << "The function was interrupted by a signal."; } else if (ttyname_Result == ENOSYS) { - llvm::errs() << "The ttyname_r() function isn't implemented for the filesystem specified by filedes."; + llvm::errs() << "The ttyname_r() function isn't implemented for the" + << "filesystem specified by filedes."; } else if (ttyname_Result == EPERM) { - llvm::errs() << "The process doesn't have sufficient permission to carry out the requested command."; + llvm::errs() << "The process doesn't have sufficient permission to carry" + << "out the requested command."; } return false; } void MetaProcessor::MaybeRedirectOutputRAII::redirect(int fd, - llvm::SmallVectorImpl& prevFile, - std::string fileName, - FILE* file) { + llvm::SmallVectorImpl& prevFile, + std::string fileName, + FILE* file) { if (!fileName.empty()) { //Cache prevous stdout. if (!cacheStd(fd, prevFile)) { @@ -96,20 +104,22 @@ namespace cling { } FILE * redirectionFile = freopen(fileName.c_str(), "a", file); if (!redirectionFile) { - llvm::errs() << "cling::MetaProcessor Error: The file path is not valid."; + llvm::errs()<<"cling::MetaProcessor Error: The file path is not valid."; } else { file = redirectionFile; } } } - void MetaProcessor::MaybeRedirectOutputRAII::unredirect(llvm::SmallVectorImpl& filename, - FILE* file) { + void MetaProcessor::MaybeRedirectOutputRAII::unredirect( + llvm::SmallVectorImpl& filename, + FILE* file) { //Switch back to standard output after line is processed. if (!filename.empty()) { FILE* redirectionFile = freopen(filename.data(), "w", file); if (!redirectionFile) { - llvm::errs() << "cling::MetaProcessor Error: The file path is not valid."; + llvm::errs() << "cling::MetaProcessor Error: " + << "The file path is not valid."; } else { file = redirectionFile; } @@ -309,20 +319,25 @@ namespace cling { return ret; } + void MetaProcessor::setFileStream(std::string& fileStorage, + llvm::StringRef file, bool append) { + fileStorage = file; + if (!append && !fileStorage.empty()) { + if (fopen(fileStorage.c_str(), "w")) { + llvm::errs() << "cling::MetaProcessor Error: The file path is not" + << " valid."; + } + } + } + void MetaProcessor::setStdStream(llvm::StringRef file, RedirectionScope stream, bool append) { if (stream & kSTDOUT) { - m_FileOut = file; - if (!append && !m_FileOut.empty()) { - FILE* f = fopen(m_FileOut.c_str(), "w"); - } + setFileStream(m_FileOut, file, append); } if (stream & kSTDERR) { - m_FileErr = file; - if (!append && !m_FileErr.empty()) { - FILE* f = fopen(m_FileErr.c_str(), "w"); - } + setFileStream(m_FileErr, file, append); } }