commit bb3e281739184a20a50338238803256d986d6dfe from: Aleksey Ryndin date: Mon Aug 14 10:24:10 2023 UTC Separate `command_line_arguments` module commit - 599e39ef74f251e93e4a45f35765e685abe960e2 commit + bb3e281739184a20a50338238803256d986d6dfe blob - 8e28b9616f39d3517678e609f64d5317b850e017 blob + 27e97471f0107832ede9ffbcb9d826feb13c9601 --- shared/not_null +++ shared/not_null @@ -1,6 +1,7 @@ /** Restricts a pointer or smart pointer to only hold non-null values. */ +#include #include blob - c1c6d79110769a612749d821eda6858d3825e565 blob + 8c37ddf4527c26aaa85a1c838d97f429b1423e38 --- vostokd/Makefile +++ vostokd/Makefile @@ -13,6 +13,8 @@ CXXFILES += ${.PATH}../shared/transport.cc HXXFILES += ${.PATH}../shared/transport.h CXXFILES += ${.PATH}../shared/error.cc HXXFILES += ${.PATH}../shared/error.h +CXXFILES += command_line_arguments.cc +HXXFILES += command_line_arguments.h vostokd: ${CXXFILES} ${HXXFILES} ${CXX} ${CXXFLAGS} ${CXXFILES} -o vostokd blob - 88c122fe1ed1029919a57118bf1df380b1c9f120 blob + 4cbc7c5fbc7b64cfe00d9d3b4456b945ddb8ee8e --- vostokd/vostokd.cc +++ vostokd/vostokd.cc @@ -1,13 +1,13 @@ /* Gemini server */ -#include -#include +#include "error.h" +#include "transport.h" +#include "command_line_arguments.h" -#include #include #include -#include #include +#include #include @@ -38,77 +38,6 @@ const auto non_gemini = cut_null("No proxying to non-G const auto gemini_scheme = cut_null("gemini://"); -struct command_line_arguments -{ - not_null m_addr{"127.0.0.1"}; - int m_port{1965}; - czstring m_cert_file{nullptr}; - czstring m_key_file{nullptr}; - - bool parse_command_line(int argc, char *argv[]) - { - int ch; - char *p = nullptr; - while ((ch = getopt(argc, argv, "a:p:c:k:")) != -1) { - switch (ch) { - case 'a': - m_addr = optarg; - break; - case 'p': - p = nullptr; - m_port = std::strtoul(optarg, &p, 10); - if (!m_port) - { - error::g_log << "Invalid PORT value: " << optarg << std::endl; - usage(argv[0]); - return false; - } - break; - case 'c': - m_cert_file = optarg; - break; - case 'k': - m_key_file = optarg; - break; - - default: - usage(argv[0]); - return false; - } - } - if (!m_cert_file) - { - error::g_log << "Invalid command line: -c option required" << std::endl; - usage(argv[0]); - return false; - } - if (!m_key_file) - { - error::g_log << "Invalid command line: -k option required" << std::endl; - usage(argv[0]); - return false; - } - return true; - } - - static void usage(const char *program) - { - const char *file_name = strrchr(program, '/'); - if (!file_name) - file_name = program; - else - ++file_name; - - error::g_log << "Usage: " << file_name << " [OPTS]" << std::endl << std::endl; - error::g_log << "OPTS may be: " << std::endl; - error::g_log << "\t-a ADDR : listening network address (127.0.0.1 by default)" << std::endl; - error::g_log << "\t-p PORT : TCP/IP port number (1965 by default)" << std::endl; - error::g_log << "\t-c FILE : Server certificate file [REQUIRED]" << std::endl; - error::g_log << "\t-k FILE : Server key file [REQUIRED]" << std::endl; - } -}; - - void client_thread(transport::accepted_context::value raw_value) { const transport::accepted_context ctx{raw_value}; blob - /dev/null blob + 7157976b86a0eabf00636cff867f8d832cb36960 (mode 644) --- /dev/null +++ vostokd/command_line_arguments.cc @@ -0,0 +1,77 @@ +/** Parse command line arguments */ + +#include "command_line_arguments.h" +#include "error.h" + +#include + + +namespace vostok +{ + + +bool command_line_arguments::parse_command_line(int argc, char *argv[]) +{ + int ch; + char *p = nullptr; + while ((ch = getopt(argc, argv, "a:p:c:k:")) != -1) { + switch (ch) { + case 'a': + m_addr = optarg; + break; + case 'p': + p = nullptr; + m_port = std::strtoul(optarg, &p, 10); + if (!m_port) + { + error::g_log << "Invalid PORT value: " << optarg << std::endl; + usage(argv[0]); + return false; + } + break; + case 'c': + m_cert_file = optarg; + break; + case 'k': + m_key_file = optarg; + break; + + default: + usage(argv[0]); + return false; + } + } + if (!m_cert_file) + { + error::g_log << "Invalid command line: -c option required" << std::endl; + usage(argv[0]); + return false; + } + if (!m_key_file) + { + error::g_log << "Invalid command line: -k option required" << std::endl; + usage(argv[0]); + return false; + } + return true; +} + + +void command_line_arguments::usage(const char *program) +{ + const char *file_name = strrchr(program, '/'); + if (!file_name) + file_name = program; + else + ++file_name; + + error::g_log << "Usage: " << file_name << " [OPTS]" << std::endl << std::endl; + error::g_log << "OPTS may be: " << std::endl; + error::g_log << "\t-a ADDR : listening network address (127.0.0.1 by default)" << std::endl; + error::g_log << "\t-p PORT : TCP/IP port number (1965 by default)" << std::endl; + error::g_log << "\t-c FILE : Server certificate file [REQUIRED]" << std::endl; + error::g_log << "\t-k FILE : Server key file [REQUIRED]" << std::endl; +} + + +} // namespace vostok blob - /dev/null blob + a05146429c24bd042d30740b1f5c8c48de36673a (mode 644) --- /dev/null +++ vostokd/command_line_arguments.h @@ -0,0 +1,27 @@ +/** Parse command line arguments */ + + +#pragma once + +#include +#include + + +namespace vostok +{ + + +struct command_line_arguments +{ + not_null m_addr{"127.0.0.1"}; + int m_port{1965}; + czstring m_cert_file{nullptr}; + czstring m_key_file{nullptr}; + + bool parse_command_line(int argc, char *argv[]); + + void usage(const char *program); +}; + + +} // namespace vostok