Commit c85808ec authored by Bednárek David RNDr. Ph.D.'s avatar Bednárek David RNDr. Ph.D.
Browse files

-z redirects also ckrt_printf

parent 96ddffb0
......@@ -2,6 +2,7 @@
#include <cstdio>
#include <cstdarg>
#include <iostream>
#if defined _WIN32 || defined __CYGWIN__
#ifdef __GNUC__
......@@ -17,15 +18,36 @@
#endif
#endif
std::ostream* ckrt_out = &std::cout;
extern "C" {
int DLL_PUBLIC ckrt_printf(const char* s, ...)
{
static std::vector<char> buffer;
va_list va;
va_start(va, s);
int rv = vprintf(s, va);
va_list va2;
va_copy(va2, va);
auto rv2 = std::vsnprintf(nullptr, 0, s, va2);
va_end(va2);
if (rv2 < 0) // vsnprintf failed
{
std::vsnprintf(nullptr, 0, s, va);
}
else
{
auto rsize = rv2 + 1; // ending zero
if (rsize >= buffer.size())
buffer.resize(rsize);
auto rv = std::vsnprintf(buffer.data(), buffer.size(), s, va);
if (rv == rv2)
{
ckrt_out->write(buffer.data(), rv);
ckrt_out->flush();
}
}
va_end(va);
fflush(stdout);
return rv;
return rv2;
}
int DLL_PUBLIC ckrt_scanf(const char* s, ...)
......@@ -148,6 +170,7 @@ namespace cecko {
os << "========== starting main() ==========\n";
os.flush(); // flush before running unsafe code
ckrt_out = &os;
std::vector<llvm::GenericValue> Args(2);
Args[0].IntVal = llvm::APInt(32, argc);
......@@ -155,6 +178,7 @@ namespace cecko {
auto GV = EE->runFunction(fnc, Args);
mainrv = GV.IntVal.getSExtValue();
ckrt_out = &std::cout;
// hopefully we destroy JIT here
}
os << "\n========== main() returned " << mainrv << " ==========\n";
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment