Skip to content

Adjust to line size limit. #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 4 additions & 2 deletions interpreter/cling/lib/MetaProcessor/MetaParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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
Expand Down
65 changes: 40 additions & 25 deletions interpreter/cling/lib/MetaProcessor/MetaProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,21 @@ 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;
cacheStd(STDERR_FILENO, stdoutName);
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);

}

Expand All @@ -56,12 +59,14 @@ namespace cling {
}

bool MetaProcessor::MaybeRedirectOutputRAII::cacheStd(int fd,
llvm::SmallVectorImpl<char>& file) {
llvm::SmallVectorImpl<char>& file) {

int ttyname_Result = ttyname_r(fd, const_cast<char*>(file.data()), file.capacity());
int ttyname_Result = ttyname_r(fd, const_cast<char*>(file.data()),
file.capacity());
while (ttyname_Result == ERANGE) {
file.reserve(16*file.capacity());
ttyname_Result = ttyname_r(fd, const_cast<char*>(file.data()), file.capacity());
ttyname_Result = ttyname_r(fd, const_cast<char*>(file.data()),
file.capacity());
}

if (ttyname_Result == 0) {
Expand All @@ -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<char>& prevFile,
std::string fileName,
FILE* file) {
llvm::SmallVectorImpl<char>& prevFile,
std::string fileName,
FILE* file) {
if (!fileName.empty()) {
//Cache prevous stdout.
if (!cacheStd(fd, prevFile)) {
Expand All @@ -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<char>& filename,
FILE* file) {
void MetaProcessor::MaybeRedirectOutputRAII::unredirect(
llvm::SmallVectorImpl<char>& 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;
}
Expand Down Expand Up @@ -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);
}
}

Expand Down