mirror of
https://github.com/genodelabs/genode.git
synced 2024-12-23 15:32:25 +00:00
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
This commit is contained in:
parent
26796c8f7a
commit
94881e757a
79
repos/os/src/test/terminal_expect_send/main.cc
Normal file
79
repos/os/src/test/terminal_expect_send/main.cc
Normal file
@ -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 <base/attached_rom_dataspace.h>
|
||||
#include <base/component.h>
|
||||
#include <terminal_session/connection.h>
|
||||
|
||||
using namespace Genode;
|
||||
|
||||
struct Main
|
||||
{
|
||||
enum { MAX_LINE_LENGTH = 512 };
|
||||
|
||||
using Line = String<MAX_LINE_LENGTH>;
|
||||
Terminal::Connection terminal;
|
||||
Signal_handler<Main> 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); }
|
3
repos/os/src/test/terminal_expect_send/target.mk
Normal file
3
repos/os/src/test/terminal_expect_send/target.mk
Normal file
@ -0,0 +1,3 @@
|
||||
TARGET = test-terminal_expect_send
|
||||
SRC_CC = main.cc
|
||||
LIBS = base
|
Loading…
Reference in New Issue
Block a user