From 94881e757a9b8ff82188ae1696de8fd4b242700b Mon Sep 17 00:00:00 2001 From: Stefan Kalkowski Date: Thu, 4 Apr 2019 16:35:07 +0200 Subject: [PATCH] test-terminal_expect_send: expect-like helper tool A small terminal-client tool, which expects a specific line(-start), and then sends a specified line to the other side. Optionally, it prints all received lines to its LOG service. Ref #3278 --- .../os/src/test/terminal_expect_send/main.cc | 79 +++++++++++++++++++ .../src/test/terminal_expect_send/target.mk | 3 + 2 files changed, 82 insertions(+) create mode 100644 repos/os/src/test/terminal_expect_send/main.cc create mode 100644 repos/os/src/test/terminal_expect_send/target.mk diff --git a/repos/os/src/test/terminal_expect_send/main.cc b/repos/os/src/test/terminal_expect_send/main.cc new file mode 100644 index 000000000..49377d21d --- /dev/null +++ b/repos/os/src/test/terminal_expect_send/main.cc @@ -0,0 +1,79 @@ +/* + * \brief Send terminal output on specified input (like expect) + * \author Stefan Kalkowski + * \date 2019-04-03 + */ + +/* + * Copyright (C) 2019 Genode Labs GmbH + * + * This file is part of the Genode OS framework, which is distributed + * under the terms of the GNU Affero General Public License version 3. + */ + +/* Genode includes */ +#include +#include +#include + +using namespace Genode; + +struct Main +{ + enum { MAX_LINE_LENGTH = 512 }; + + using Line = String; + Terminal::Connection terminal; + Signal_handler
read_avail; + unsigned line_idx = 0; + char line[MAX_LINE_LENGTH]; + char read_buffer[MAX_LINE_LENGTH]; + Line expect {}; + Line send {}; + bool verbose { false }; + + void handle_read_avail() + { + unsigned num_bytes = terminal.read(read_buffer, sizeof(read_buffer)); + + for (unsigned i = 0; i < num_bytes; i++) { + + /* copy over all characters other than line-end */ + if (read_buffer[i] != '\n' && + read_buffer[i] != '\r') { + line[line_idx++] = read_buffer[i]; + line[line_idx] = 0; + } + + /* check for expected line-start string, if found send line */ + if (expect.valid() && expect == line) { + terminal.write(send.string(), send.length()-1); + terminal.write("\r\n", 2); + } + + /* check for line end */ + if (read_buffer[i] == '\n') { + if (verbose) log(Cstring(line)); + line_idx = 0; + } + } + } + + Main(Env &env) : terminal(env), + read_avail(env.ep(), *this, &Main::handle_read_avail) + { + terminal.read_avail_sigh(read_avail); + + try { + Genode::Attached_rom_dataspace config { env, "config" }; + + verbose = config.xml().attribute_value("verbose", false); + config.xml().attribute("expect").value(line, sizeof(line)); + expect = Line(line); + config.xml().attribute("send").value(line, sizeof(line)); + send = Line(line); + } catch (...) { warning("No config data available"); } + } +}; + +void Component::construct(Env &env) { static Main main(env); } diff --git a/repos/os/src/test/terminal_expect_send/target.mk b/repos/os/src/test/terminal_expect_send/target.mk new file mode 100644 index 000000000..6414b5d64 --- /dev/null +++ b/repos/os/src/test/terminal_expect_send/target.mk @@ -0,0 +1,3 @@ +TARGET = test-terminal_expect_send +SRC_CC = main.cc +LIBS = base