From 40aac7eeca5cea233825d7472226743fb1f5ccdf Mon Sep 17 00:00:00 2001 From: Bob Polis Date: Sat, 18 Apr 2020 20:39:32 +0200 Subject: [PATCH] added option handling and more tests --- sciotest/main.cpp | 133 ++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 111 insertions(+), 22 deletions(-) diff --git a/sciotest/main.cpp b/sciotest/main.cpp index a28d80e..7affbec 100644 --- a/sciotest/main.cpp +++ b/sciotest/main.cpp @@ -6,37 +6,126 @@ // Copyright (c) 2020 SwiftCoder. All rights reserved. // +#include +#include +#include +#include #include #include -#include +#include +#include +#include #include using namespace std; -int main(int /*argc*/, const char * argv[]) { +void print_help() { + std::cout << "usage: sciotest [-h][-v][-t TEST_ID]\n"; + std::cout << " -h, --help show this help text and exit\n"; + std::cout << " -v, --version show version number and exit\n"; +} + +void print_version() { + std::cout << "sciotest version 1.0\n"; +} + +void test1() { + sc::io::fdostream chan {1}; + chan << "Please enter a line of text:\n"; + + string line; + sc::io::fdistream is {0}; + getline(is, line); + cout << line << '\n'; +} + +void test2() { + cout << "starting server\n"; + sc::io::socketstream server(AF_INET6, SOCK_STREAM); + sc::io::socket_address addr {"", 4242}; + server.make_server(addr); + for (;;) { + sc::io::socketstream client = server.accept(); + cerr << "connection from " << client.address() << '\n'; + client << "ready to echo...\n"; + string line; + while (getline(client, line)) { + client << line << '\n'; + } + } +} + +void test3() { + string passwd {sc::file_get_contents("/etc/passwd")}; + int fd = -1; + throw_if_min1(fd = ::open("passwd", O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR, S_IWUSR)); + sc::io::fdostream os {fd}; + os << passwd; + throw_if_min1(::close(fd)); +} + +void test4() { + int fd = -1; + throw_if_min1(fd = ::open("/etc/passwd", O_RDONLY)); + sc::io::fdistream is {fd}; + string line; + while (getline(is, line)) { + cout << line << '\n'; + } + throw_if_min1(::close(fd)); +} + +int main(int argc, const char * argv[]) { try { - sc::io::fdostream chan {1}; - chan << "Please enter a line of text:\n"; + struct option long_options[] = { + {"help", no_argument, nullptr, 'h'}, + {"test", required_argument, nullptr, 't'}, + {"version", no_argument, nullptr, 'v'}, + {nullptr, 0, nullptr, 0} + }; + int opt_char, option_index; + int test_id = 0; + while ((opt_char = getopt_long(argc, const_cast(argv), "ht:v", long_options, &option_index)) != -1) { + switch (opt_char) { + case 0: { + // handle long-only options here + std::string optname {long_options[option_index].name}; + break; + } + case 'h': + print_help(); + return EXIT_SUCCESS; + case 't': + test_id = atoi(optarg); + break; + case 'v': + print_version(); + return EXIT_SUCCESS; + case '?': + throw std::runtime_error("unrecognized option"); + } + } + if (optind == argc) { + // here when no file args + } + for (int i = optind; i < argc; ++i) { + try { + // process file argv[i] + } catch (const std::runtime_error& ex) { + std::cerr << "sciotest: " << ex.what() << endl; + } + } + if (test_id == 0) throw std::runtime_error("please provide a test id"); + switch (test_id) { + case 1: test1(); break; + case 2: test2(); break; + case 3: test3(); break; + case 4: test4(); break; + default: throw std::runtime_error("no such test"); + } - string line; - sc::io::fdistream is {0}; - getline(is, line); - cout << line << '\n'; - - cout << "starting server\n"; - sc::io::socketstream server(AF_INET6, SOCK_STREAM); - sc::io::socket_address addr {"", 4242}; - server.make_server(addr); - for (;;) { - sc::io::socketstream client = server.accept(); - cerr << "connection from " << client.address() << '\n'; - client << "ready to echo...\n"; - while (getline(client, line)) { - client << line; - } - } } catch (const exception& ex) { - cerr << su::basename(argv[0]) << ": " << ex.what() << '\n'; + cerr << "sciotest: " << ex.what() << '\n'; return EXIT_FAILURE; } return EXIT_SUCCESS;