/** * Reads stdin looking for a string, and coalesces that buffer until stdin * is calm for the passed in number of seconds. */ #include #include #include #include #include #include #include #include #include #include using sdeventplus::Clock; using sdeventplus::ClockId; using sdeventplus::Event; using sdeventplus::source::IO; constexpr auto clockId = ClockId::RealTime; using Timer = sdeventplus::utility::Timer; int main(int argc, char* argv[]) { if (argc != 2) { fprintf(stderr, "Usage: %s [seconds]\n", argv[0]); return 1; } std::chrono::seconds delay(std::stoul(argv[1])); auto event = Event::get_default(); std::string content; auto timerCb = [&](Timer&) { printf("%s", content.c_str()); content.clear(); }; Timer timer(event, std::move(timerCb)); auto ioCb = [&](IO&, int fd, uint32_t) { std::array buffer; ssize_t bytes = read(fd, buffer.data(), 4096); if (bytes <= 0) { printf("%s", content.c_str()); event.exit(bytes < 0); return; } content.append(buffer.data(), bytes); timer.restartOnce(delay); }; IO ioSource(event, STDIN_FILENO, EPOLLIN, std::move(ioCb)); return event.loop(); }