forked from jupyter-xeus/xeus-cling
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxsystem.hpp
79 lines (68 loc) · 2.4 KB
/
xsystem.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/***********************************************************************************
* Copyright (c) 2016, Johan Mabille, Loic Gouarin, Sylvain Corlay, Wolf Vollprecht *
* Copyright (c) 2016, QuantStack *
* *
* Distributed under the terms of the BSD 3-Clause License. *
* *
* The full license is in the file LICENSE, distributed with this software. *
************************************************************************************/
#ifndef XCPP_SYSTEM_HPP
#define XCPP_SYSTEM_HPP
#include <cstdio>
#include "xeus-cling/xpreamble.hpp"
namespace xcpp
{
struct xsystem : xpreamble
{
const std::string spattern = R"(^\!)";
using xpreamble::pattern;
xsystem()
{
pattern = spattern;
}
void apply(const std::string& code, nl::json& kernel_res) override
{
std::regex re(spattern + R"((.*))");
std::smatch to_execute;
std::regex_search(code, to_execute, re);
int ret = 1;
// Redirection of stderr to stdout
std::string command = to_execute.str(1) + " 2>&1";
#if defined(WIN32)
FILE* shell_result = _popen(command.c_str(), "r");
#else
FILE* shell_result = popen(command.c_str(), "r");
#endif
if (shell_result)
{
char buff[512];
ret = 0;
while (fgets(buff, sizeof(buff), shell_result))
{
std::cout << buff;
}
#if defined(WIN32)
_pclose(shell_result);
#else
pclose(shell_result);
#endif
std::cout << std::flush;
kernel_res["status"] = "ok";
}
else
{
std::cerr << "Unable to execute the shell command\n";
std::cout << std::flush;
kernel_res["status"] = "error";
kernel_res["ename"] = "ename";
kernel_res["evalue"] = "evalue";
kernel_res["traceback"] = nl::json::array();
}
}
virtual xpreamble* clone() const override
{
return new xsystem(*this);
}
};
}
#endif