#include "request.hpp" #include #include #include #include #include #include namespace { struct Event { char kind; std::int64_t first; std::int64_t second; }; class ScriptedPort final : public remaining::WaitPort { public: void push(Event event) { events_.push_back(event); } int wait(const remaining::Time &requested, remaining::Time &left, int &error) override { Event event{'F', EAGAIN, 0}; if (cursor_ < events_.size()) { event = events_[cursor_++]; } int result = -1; if (event.kind == 'S') { result = 0; error = 0; } else if (event.kind == 'I') { error = EINTR; left = remaining::to_time(static_cast(event.first)); } else if (event.kind == 'X') { error = EINTR; left = {event.first, event.second}; } else { error = static_cast(event.first); left = {-7, -7}; } std::cout << "W " << requested.sec << ' ' << requested.nsec << ' ' << result << ' ' << error << '\n'; return result; } private: std::vector events_; std::size_t cursor_ = 0; }; bool event_line(const std::string &line, Event &event) { std::istringstream input(line); std::string prefix; std::string extra; if (!(input >> prefix) || prefix != "E" || !(input >> event.kind)) return false; if (event.kind == 'S') return !(input >> extra); if (event.kind == 'I' || event.kind == 'F') { return bool(input >> event.first) && !(input >> extra); } if (event.kind == 'X') { return bool(input >> event.first >> event.second) && !(input >> extra); } return false; } } // namespace int main() { ScriptedPort port; remaining::Manager manager(port); remaining::Console console(manager); std::string line; while (std::getline(std::cin, line)) { if (line.size() >= 2 && line[0] == 'E' && line[1] == ' ') { Event event{}; if (!event_line(line, event)) return 2; port.push(event); continue; } std::string output; if (!console.execute(line, output)) return 2; if (!output.empty()) std::cout << "O " << output << '\n'; } return std::cin.bad() ? 2 : 0; }