diff options
author | Egor Tensin <Egor.Tensin@gmail.com> | 2023-04-29 21:42:33 +0200 |
---|---|---|
committer | Egor Tensin <Egor.Tensin@gmail.com> | 2023-04-29 21:42:33 +0200 |
commit | 8b4ad891dce51baa3fb306c617f3368d24600e6d (patch) | |
tree | 065cbaee51791d3a35f15547a3c3ff77c97db83a | |
parent | make struct storage_settings_sqlite opaque (diff) | |
download | cimple-8b4ad891dce51baa3fb306c617f3368d24600e6d.tar.gz cimple-8b4ad891dce51baa3fb306c617f3368d24600e6d.zip |
dedupe command line routines
-rw-r--r-- | src/CMakeLists.txt | 3 | ||||
-rw-r--r-- | src/client_main.c | 21 | ||||
-rw-r--r-- | src/cmd_line.c | 34 | ||||
-rw-r--r-- | src/cmd_line.h | 17 | ||||
-rw-r--r-- | src/server_main.c | 26 | ||||
-rw-r--r-- | src/worker_main.c | 21 |
6 files changed, 69 insertions, 53 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 07fea51..30fb3fd 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -35,6 +35,7 @@ generate_sql_header(sqlite) add_my_executable(server server_main.c server.c ci_queue.c + cmd_line.c msg.c net.c signal.c @@ -47,11 +48,13 @@ target_link_libraries(server PRIVATE pthread sqlite3) target_include_directories(server PRIVATE "${CMAKE_CURRENT_BINARY_DIR}") add_my_executable(client client_main.c client.c + cmd_line.c msg.c net.c) add_my_executable(worker worker_main.c worker.c ci.c + cmd_line.c file.c git.c msg.c diff --git a/src/client_main.c b/src/client_main.c index dd6c8c5..4574e44 100644 --- a/src/client_main.c +++ b/src/client_main.c @@ -6,11 +6,10 @@ */ #include "client.h" +#include "cmd_line.h" #include "const.h" #include <getopt.h> -#include <stdio.h> -#include <stdlib.h> static struct settings default_settings() { @@ -18,14 +17,9 @@ static struct settings default_settings() return settings; } -static void print_usage(const char *argv0) +const char *get_usage_string() { - printf("usage: %s [-h|--help] [-V|--version] [-H|--host HOST] [-p|--port PORT]\n", argv0); -} - -static void print_version() -{ - printf("%s\n", VERSION); + return "[-h|--help] [-V|--version] [-H|--host HOST] [-p|--port PORT]"; } static int parse_settings(struct settings *settings, int argc, char *argv[]) @@ -45,12 +39,10 @@ static int parse_settings(struct settings *settings, int argc, char *argv[]) while ((opt = getopt_long(argc, argv, "hVH:p:", long_options, &longind)) != -1) { switch (opt) { case 'h': - print_usage(argv[0]); - exit(0); + exit_with_usage(0, argv[0]); break; case 'V': - print_version(); - exit(0); + exit_with_version(); break; case 'H': settings->host = optarg; @@ -59,8 +51,7 @@ static int parse_settings(struct settings *settings, int argc, char *argv[]) settings->port = optarg; break; default: - print_usage(argv[0]); - exit(1); + exit_with_usage(1, argv[0]); break; } } diff --git a/src/cmd_line.c b/src/cmd_line.c new file mode 100644 index 0000000..9527d6f --- /dev/null +++ b/src/cmd_line.c @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2023 Egor Tensin <Egor.Tensin@gmail.com> + * This file is part of the "cimple" project. + * For details, see https://github.com/egor-tensin/cimple. + * Distributed under the MIT License. + */ + +#include "cmd_line.h" + +#include <stdio.h> +#include <stdlib.h> + +void exit_with_usage(int ec, const char *argv0) +{ + FILE *dest = stdout; + if (ec) + dest = stderr; + + fprintf(dest, "usage: %s %s\n", argv0, get_usage_string()); + exit(ec); +} + +void exit_with_usage_err(const char *argv0, const char *msg) +{ + if (msg) + fprintf(stderr, "usage error: %s\n", msg); + exit_with_usage(1, argv0); +} + +void exit_with_version() +{ + printf("%s\n", VERSION); + exit(0); +} diff --git a/src/cmd_line.h b/src/cmd_line.h new file mode 100644 index 0000000..a661825 --- /dev/null +++ b/src/cmd_line.h @@ -0,0 +1,17 @@ +/* + * Copyright (c) 2023 Egor Tensin <Egor.Tensin@gmail.com> + * This file is part of the "cimple" project. + * For details, see https://github.com/egor-tensin/cimple. + * Distributed under the MIT License. + */ + +#ifndef __CMD_LINE_H__ +#define __CMD_LINE_H__ + +const char *get_usage_string(); + +void exit_with_usage(int ec, const char *argv0); +void exit_with_usage_err(const char *argv0, const char *msg); +void exit_with_version(); + +#endif diff --git a/src/server_main.c b/src/server_main.c index 4295b2d..02304b2 100644 --- a/src/server_main.c +++ b/src/server_main.c @@ -5,12 +5,11 @@ * Distributed under the MIT License. */ +#include "cmd_line.h" #include "const.h" #include "server.h" #include <getopt.h> -#include <stdio.h> -#include <stdlib.h> static struct settings default_settings() { @@ -18,28 +17,9 @@ static struct settings default_settings() return settings; } -static void exit_with_usage(int ec, const char *argv0) +const char *get_usage_string() { - FILE *dest = stdout; - if (ec) - dest = stderr; - - fprintf(dest, "usage: %s [-h|--help] [-V|--version] [-p|--port PORT] [-s|--sqlite PATH]\n", - argv0); - exit(ec); -} - -static void exit_with_usage_err(const char *argv0, const char *msg) -{ - if (msg) - fprintf(stderr, "usage error: %s\n", msg); - exit_with_usage(1, argv0); -} - -static void exit_with_version() -{ - printf("%s\n", VERSION); - exit(0); + return "[-h|--help] [-V|--version] [-p|--port PORT] [-s|--sqlite PATH]"; } static int parse_settings(struct settings *settings, int argc, char *argv[]) diff --git a/src/worker_main.c b/src/worker_main.c index 7c78f93..9e9a0b9 100644 --- a/src/worker_main.c +++ b/src/worker_main.c @@ -5,13 +5,12 @@ * Distributed under the MIT License. */ +#include "cmd_line.h" #include "const.h" #include "signal.h" #include "worker.h" #include <getopt.h> -#include <stdio.h> -#include <stdlib.h> static struct settings default_settings() { @@ -19,14 +18,9 @@ static struct settings default_settings() return settings; } -static void print_usage(const char *argv0) +const char *get_usage_string() { - printf("usage: %s [-h|--help] [-V|--version] [-H|--host HOST] [-p|--port PORT]\n", argv0); -} - -static void print_version() -{ - printf("%s\n", VERSION); + return "[-h|--help] [-V|--version] [-H|--host HOST] [-p|--port PORT]"; } static int parse_settings(struct settings *settings, int argc, char *argv[]) @@ -46,12 +40,10 @@ static int parse_settings(struct settings *settings, int argc, char *argv[]) while ((opt = getopt_long(argc, argv, "hVH:p:", long_options, &longind)) != -1) { switch (opt) { case 'h': - print_usage(argv[0]); - exit(0); + exit_with_usage(0, argv[0]); break; case 'V': - print_version(); - exit(0); + exit_with_version(); break; case 'H': settings->host = optarg; @@ -60,8 +52,7 @@ static int parse_settings(struct settings *settings, int argc, char *argv[]) settings->port = optarg; break; default: - print_usage(argv[0]); - exit(1); + exit_with_usage(1, argv[0]); break; } } |