From 71b89bc9ceb59f2603cf4b0635849269597a4823 Mon Sep 17 00:00:00 2001 From: Oxore Date: Tue, 30 Apr 2024 01:38:42 +0300 Subject: Impl --stop and --gdb options --- chardev.cpp | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 chardev.cpp (limited to 'chardev.cpp') diff --git a/chardev.cpp b/chardev.cpp new file mode 100644 index 0000000..1d587b9 --- /dev/null +++ b/chardev.cpp @@ -0,0 +1,50 @@ +/* SPDX-License-Identifier: Unlicense + */ + +#include "chardev.hpp" + +#include +#include + +static bool startsWith(const char* str, const char* pattern) +{ + const size_t pattern_len = strlen(pattern); + const size_t str_len = strlen(str); + if (str_len < pattern_len) { + return false; + } + return 0 == memcmp(str, pattern, pattern_len); +} + +static CharDev parseTcp(const char* path) +{ + // We need to make sure there is at least one more ':' delimiter in the + // string + const char* rest = strchr(path + (sizeof "tcp:") - 1, ':'); + if (rest == nullptr) { + return CharDev::Invalid(path); + } + const char* port_str; + // Find last ':' before string ends, i.e. when strchr returns NULL + do { + port_str = rest + 1; + rest = strchr(port_str, ':'); + } while (rest); + const unsigned long port = strtoul(port_str, nullptr, 0); + if (port <= UINT16_MAX) { + path += (sizeof "tcp:") - 1; + return CharDev::Tcp(path, port_str - 1 - path, port); + } + return CharDev::Invalid(path); +} + +CharDev CharDev::Parse(const char* path) +{ + if (0 == strcmp(path, "pty")) { + return CharDev::Pty(); + } + if (startsWith(path, "tcp:")) { + return parseTcp(path); + } + return CharDev::Invalid(path); +} -- cgit v1.2.3