From 7eaf35ea4c9df1375fb91645ef9d35fe86dcaa81 Mon Sep 17 00:00:00 2001 From: "Nikita Tyukalov, ASUS, Linux" Date: Tue, 24 Mar 2026 21:04:22 +0300 Subject: [PATCH] Improved CLI experience --- main.c | 39 +++++++++++++++++++++++++++++++-------- utils.c | 9 +++++++-- utils.h | 8 ++++++++ 3 files changed, 46 insertions(+), 10 deletions(-) diff --git a/main.c b/main.c index 7718029..1b71334 100644 --- a/main.c +++ b/main.c @@ -19,6 +19,7 @@ static bool _to_work = true; static int _sig_fd = -1; static int _server_fd = -1; +static bool _is_server = false; /* * Private API @@ -68,7 +69,24 @@ bool parse_cli_args(int argc, char** argv) { required_args_present &= cli_arg_get("A") ? true : false; required_args_present &= cli_arg_get("P") ? true : false; required_args_present &= cli_arg_get("E") ? true : false; - return required_args_present; + if (!required_args_present) { + fprintf(stderr, "[!] Some required arguments are missing\n"); + return false; + } + const char *mode = cli_arg_get("M"); + if (strcmp(mode, "server") && strcmp(mode, "client")) { + fprintf(stderr, "[!] Unknown mode '%s'\n", mode); + return false; + } + _is_server = !strcmp(mode, "server"); + flag_verbose = cli_arg_get("V") ? 1 : 0; + if (flag_verbose) { + fprintf(stdout, "[I] Being verbose\n"); + fprintf(stdout, "[I] - Mode ------ %s\n", cli_arg_get("M")); + fprintf(stdout, "[I] - Address --- %s:%d\n", cli_arg_get("A"), atoi(cli_arg_get("P"))); + fprintf(stdout, "[I] - Extension - %s\n", cli_arg_get("E")); + } + return true; } // Initialize signals. @@ -167,6 +185,7 @@ static void on_fd_event(int fd, uint32_t events) { } int main(int argc, char **argv) { + // Initialization if (!parse_cli_args(argc, argv)) { print_usage_text(); return 1; @@ -178,12 +197,14 @@ int main(int argc, char **argv) { loop_deinit(); return 1; } - if (!server_init()) { - signals_deinit(); - loop_deinit(); - return 1; + if (_is_server) { + if (!server_init()) { + signals_deinit(); + loop_deinit(); + return 1; + } } - + // Loop while (_to_work) { if (!loop_wait()) { fprintf(stderr, "[!] loop_wait returns false, stopping...\n"); @@ -191,8 +212,10 @@ int main(int argc, char **argv) { } fprintf(stderr, "--- loop ---\n"); } - - server_close(); + // Termination + if (_is_server) { + server_close(); + } signals_deinit(); loop_deinit(); return 0; diff --git a/utils.c b/utils.c index b359648..60e4d42 100644 --- a/utils.c +++ b/utils.c @@ -22,16 +22,21 @@ typedef struct { */ static cli_arg_t _cli_args[MAX_CLI_ARGS]; +/* + * Public data + */ +int flag_verbose = 0; + /* * Public API */ void print_usage_text() { printf("ipoim -M -A
-P -C ...\n"); printf(" --- MANDATORY ARGUMENTS ---\n"); - printf(" -M - operation mode ('provider' or 'last-mile')\n"); + printf(" -M - operation mode ('server' or 'client')\n"); printf(" -A
- IPv4 to listen on/connect to\n"); printf(" -P - port to listen on/connect to\n"); - printf(" -E - extension to use, one of:\n"); + printf(" -E - extension to use, one of:\n"); printf(" * pipe - use STDIN and STDOUT\n"); printf(" --- OPTIONAL ARGUMENTS ---\n"); printf(" +V - be verbose\n"); diff --git a/utils.h b/utils.h index 4bad6aa..442d395 100644 --- a/utils.h +++ b/utils.h @@ -3,6 +3,14 @@ #include +/* + * Public data + */ +extern int flag_verbose; + +/* + * Public API + */ // Print usage text. void print_usage_text();