feat(cli): refactor CLI move utilities, and improve build/version metadata
- Move CLI internal headers to `includes/internal/cli/` - Split CLI handler and parsing declarations across dedicated internal headers - Move handler map to `option_map.c` and properly update references - Relocate CLI utility source and test files from `utils` to `parse_utils` - Refactor `cli.h` to only expose the public interface, move internal typedefs/functions out - Update build system: add conditional git commit detection
This commit is contained in:
parent
18211e441c
commit
f6ca69f795
39 changed files with 224 additions and 164 deletions
7
Makefile
7
Makefile
|
|
@ -19,6 +19,12 @@ VERSION := 0.0.1
|
||||||
BUILD_DATE := $(shell git log -1 --format=%cd --date=format:'%Y-%m-%d %H:%M:%S' 2>/dev/null || date -u '+%Y-%m-%d %H:%M:%S')
|
BUILD_DATE := $(shell git log -1 --format=%cd --date=format:'%Y-%m-%d %H:%M:%S' 2>/dev/null || date -u '+%Y-%m-%d %H:%M:%S')
|
||||||
GIT_COMMIT := $(shell git rev-parse --short HEAD 2>/dev/null || echo "nogit")
|
GIT_COMMIT := $(shell git rev-parse --short HEAD 2>/dev/null || echo "nogit")
|
||||||
|
|
||||||
|
ifeq ($(GIT_COMMIT),nogit)
|
||||||
|
HAS_GIT_COMMIT=0
|
||||||
|
else
|
||||||
|
HAS_GIT_COMMIT=1
|
||||||
|
endif
|
||||||
|
|
||||||
.DEFAULT_GOAL := all
|
.DEFAULT_GOAL := all
|
||||||
MAKEFLAGS += --no-print-directory
|
MAKEFLAGS += --no-print-directory
|
||||||
|
|
||||||
|
|
@ -52,6 +58,7 @@ update-version-header:
|
||||||
echo "#define PING_VERSION \"$(VERSION)\"" >> $$NEW_HEADER; \
|
echo "#define PING_VERSION \"$(VERSION)\"" >> $$NEW_HEADER; \
|
||||||
echo "#define PING_BUILD_DATE \"$(BUILD_DATE)\"" >> $$NEW_HEADER; \
|
echo "#define PING_BUILD_DATE \"$(BUILD_DATE)\"" >> $$NEW_HEADER; \
|
||||||
echo "#define PING_GIT_COMMIT \"$(GIT_COMMIT)\"" >> $$NEW_HEADER; \
|
echo "#define PING_GIT_COMMIT \"$(GIT_COMMIT)\"" >> $$NEW_HEADER; \
|
||||||
|
echo "#define PING_HAS_GIT_COMMIT $(HAS_GIT_COMMIT)" >> $$NEW_HEADER; \
|
||||||
echo "" >> $$NEW_HEADER; \
|
echo "" >> $$NEW_HEADER; \
|
||||||
echo "typedef struct s_prog_name" >> $$NEW_HEADER; \
|
echo "typedef struct s_prog_name" >> $$NEW_HEADER; \
|
||||||
echo "{" >> $$NEW_HEADER; \
|
echo "{" >> $$NEW_HEADER; \
|
||||||
|
|
|
||||||
|
|
@ -9,48 +9,7 @@ enum e_cli_code {
|
||||||
CLI_ERROR = 1
|
CLI_ERROR = 1
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
typedef int (*t_option_handler)(const char *arg, t_ping_config *config);
|
|
||||||
|
|
||||||
typedef enum e_option_arg_type
|
|
||||||
{
|
|
||||||
OPT_ARG_NONE = 0,
|
|
||||||
OPT_ARG_INT,
|
|
||||||
OPT_ARG_UINT,
|
|
||||||
OPT_ARG_SECONDS,
|
|
||||||
OPT_ARG_BYTES,
|
|
||||||
OPT_ARG_TTL,
|
|
||||||
OPT_ARG_STRING
|
|
||||||
} t_option_arg_type;
|
|
||||||
|
|
||||||
typedef struct s_option_descriptor
|
|
||||||
{
|
|
||||||
char short_opt;
|
|
||||||
const char *long_opt;
|
|
||||||
int has_arg;
|
|
||||||
t_option_handler handler;
|
|
||||||
t_option_arg_type arg_type;
|
|
||||||
const char *description;
|
|
||||||
} t_option_descriptor;
|
|
||||||
|
|
||||||
extern const t_option_descriptor g_options[];
|
|
||||||
|
|
||||||
enum e_cli_code
|
enum e_cli_code
|
||||||
cli_parse_arguments(int argc, char **argv, t_ping_config *config);
|
cli_parse_arguments(int argc, char **argv, t_ping_config *config);
|
||||||
|
|
||||||
int cli_handle_count(const char *arg, t_ping_config *config);
|
|
||||||
int cli_handle_flood(const char *arg, t_ping_config *config);
|
|
||||||
int cli_handle_help(const char *arg, t_ping_config *config);
|
|
||||||
int cli_handle_interval(const char *arg, t_ping_config *config);
|
|
||||||
int cli_handle_quiet(const char *arg, t_ping_config *config);
|
|
||||||
int cli_handle_size(const char *arg, t_ping_config *config);
|
|
||||||
int cli_handle_timeout(const char *arg, t_ping_config *config);
|
|
||||||
int cli_handle_ttl(const char *arg, t_ping_config *config);
|
|
||||||
int cli_handle_version(const char *arg, t_ping_config *config);
|
|
||||||
int cli_handle_verbose(const char *arg, t_ping_config *config);
|
|
||||||
|
|
||||||
int cli_parse_uint64(const char *s, uint64_t *out);
|
|
||||||
int cli_parse_float(const char *s, float *out);
|
|
||||||
const char *get_optstr(void);
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
44
includes/internal/cli/arg_handler.h
Normal file
44
includes/internal/cli/arg_handler.h
Normal file
|
|
@ -0,0 +1,44 @@
|
||||||
|
#ifndef CLI_INTERNAL_ARG_HANDLER
|
||||||
|
#define CLI_INTERNAL_ARG_HANDLER
|
||||||
|
|
||||||
|
#include "ft_ping.h"
|
||||||
|
|
||||||
|
typedef int (*t_option_handler)(const char *arg, t_ping_config *config);
|
||||||
|
|
||||||
|
typedef enum e_option_arg_type
|
||||||
|
{
|
||||||
|
OPT_ARG_NONE = 0,
|
||||||
|
OPT_ARG_INT,
|
||||||
|
OPT_ARG_UINT,
|
||||||
|
OPT_ARG_SECONDS,
|
||||||
|
OPT_ARG_BYTES,
|
||||||
|
OPT_ARG_TTL,
|
||||||
|
OPT_ARG_STRING
|
||||||
|
} t_option_arg_type;
|
||||||
|
|
||||||
|
typedef struct s_option_descriptor
|
||||||
|
{
|
||||||
|
char short_opt;
|
||||||
|
const char *long_opt;
|
||||||
|
int has_arg;
|
||||||
|
t_option_handler handler;
|
||||||
|
t_option_arg_type arg_type;
|
||||||
|
const char *description;
|
||||||
|
} t_option_descriptor;
|
||||||
|
|
||||||
|
extern const t_option_descriptor g_options[];
|
||||||
|
|
||||||
|
const char *cli_get_optstr(void);
|
||||||
|
|
||||||
|
int cli_handle_count(const char *arg, t_ping_config *config);
|
||||||
|
int cli_handle_flood(const char *arg, t_ping_config *config);
|
||||||
|
int cli_handle_help(const char *arg, t_ping_config *config);
|
||||||
|
int cli_handle_interval(const char *arg, t_ping_config *config);
|
||||||
|
int cli_handle_quiet(const char *arg, t_ping_config *config);
|
||||||
|
int cli_handle_size(const char *arg, t_ping_config *config);
|
||||||
|
int cli_handle_timeout(const char *arg, t_ping_config *config);
|
||||||
|
int cli_handle_ttl(const char *arg, t_ping_config *config);
|
||||||
|
int cli_handle_version(const char *arg, t_ping_config *config);
|
||||||
|
int cli_handle_verbose(const char *arg, t_ping_config *config);
|
||||||
|
|
||||||
|
#endif
|
||||||
7
includes/internal/cli/messages.h
Normal file
7
includes/internal/cli/messages.h
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
#ifndef CLI_INTERNAL_MESSAGES
|
||||||
|
#define CLI_INTERNAL_MESSAGES
|
||||||
|
|
||||||
|
void print_help(void);
|
||||||
|
void print_version(void);
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
@ -93,6 +93,7 @@ enum { CLI_OPT_LEN = (0 CLI_OPTIONS_LIST) };
|
||||||
+ (1 * (has_arg == no_argument) \
|
+ (1 * (has_arg == no_argument) \
|
||||||
+ (2 * (has_arg == required_argument) \
|
+ (2 * (has_arg == required_argument) \
|
||||||
+ (3 * (has_arg == optional_argument))))
|
+ (3 * (has_arg == optional_argument))))
|
||||||
enum { CLI_OPT_STR_LEN = (0 CLI_OPTIONS_LIST) };
|
enum { CLI_OPT_STR_LEN = (1 CLI_OPTIONS_LIST) };
|
||||||
|
/* +1 for ':' to disable getopt error messages */
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
9
includes/internal/cli/parse_utils.h
Normal file
9
includes/internal/cli/parse_utils.h
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
#ifndef CLI_INTERNAL_PARSE_UTILS
|
||||||
|
#define CLI_INTERNAL_PARSE_UTILS
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
int cli_parse_uint64(const char *s, uint64_t *out);
|
||||||
|
int cli_parse_float(const char *s, float *out);
|
||||||
|
|
||||||
|
#endif
|
||||||
15
sources.mk
15
sources.mk
|
|
@ -7,16 +7,17 @@ SRCS = $(SRC_DIR)/main.c \
|
||||||
$(SRC_DIR)/cli/handlers/handle_help.c \
|
$(SRC_DIR)/cli/handlers/handle_help.c \
|
||||||
$(SRC_DIR)/cli/handlers/handle_interval.c \
|
$(SRC_DIR)/cli/handlers/handle_interval.c \
|
||||||
$(SRC_DIR)/cli/handlers/handle_quiet.c \
|
$(SRC_DIR)/cli/handlers/handle_quiet.c \
|
||||||
$(SRC_DIR)/cli/handlers/handler_map.c \
|
$(SRC_DIR)/cli/handlers/option_map.c \
|
||||||
$(SRC_DIR)/cli/handlers/handle_size.c \
|
$(SRC_DIR)/cli/handlers/handle_size.c \
|
||||||
$(SRC_DIR)/cli/handlers/handle_timeout.c \
|
$(SRC_DIR)/cli/handlers/handle_timeout.c \
|
||||||
$(SRC_DIR)/cli/handlers/handle_ttl.c \
|
$(SRC_DIR)/cli/handlers/handle_ttl.c \
|
||||||
$(SRC_DIR)/cli/handlers/handle_version.c \
|
$(SRC_DIR)/cli/handlers/handle_version.c \
|
||||||
$(SRC_DIR)/cli/handlers/handle_verbose.c \
|
$(SRC_DIR)/cli/handlers/handle_verbose.c \
|
||||||
$(SRC_DIR)/cli/utils/parse_int.c \
|
$(SRC_DIR)/cli/parse_utils/parse_int.c \
|
||||||
$(SRC_DIR)/cli/utils/parse_float.c \
|
$(SRC_DIR)/cli/parse_utils/parse_float.c \
|
||||||
$(SRC_DIR)/cli/utils/get_optstr.c \
|
$(SRC_DIR)/cli/parse_utils/get_optstr.c \
|
||||||
|
$(SRC_DIR)/cli/messages/help.c \
|
||||||
|
$(SRC_DIR)/cli/messages/version.c \
|
||||||
|
|
||||||
TESTS_DIR = tests
|
TESTS_DIR = tests
|
||||||
TESTS = $(TESTS_DIR)/test_main.c \
|
TESTS = $(TESTS_DIR)/test_main.c \
|
||||||
|
|
@ -31,6 +32,6 @@ TESTS = $(TESTS_DIR)/test_main.c \
|
||||||
$(TESTS_DIR)/cli/handlers/test_handle_ttl.c \
|
$(TESTS_DIR)/cli/handlers/test_handle_ttl.c \
|
||||||
$(TESTS_DIR)/cli/handlers/test_handle_version.c \
|
$(TESTS_DIR)/cli/handlers/test_handle_version.c \
|
||||||
$(TESTS_DIR)/cli/handlers/test_handle_verbose.c \
|
$(TESTS_DIR)/cli/handlers/test_handle_verbose.c \
|
||||||
$(TESTS_DIR)/cli/utils/test_parse_int.c \
|
$(TESTS_DIR)/cli/parse_utils/test_parse_int.c \
|
||||||
$(TESTS_DIR)/cli/utils/test_parse_float.c \
|
$(TESTS_DIR)/cli/parse_utils/test_parse_float.c \
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,9 @@
|
||||||
#include <stdint.h>
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
#include "cli.h"
|
#include "cli.h"
|
||||||
|
#include "internal/cli/arg_handler.h"
|
||||||
|
#include "internal/cli/parse_utils.h"
|
||||||
|
|
||||||
int
|
int
|
||||||
cli_handle_count(const char *arg, t_ping_config *config)
|
cli_handle_count(const char *arg, t_ping_config *config)
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,4 @@
|
||||||
#include "cli.h"
|
#include "cli.h"
|
||||||
#include "ft_ping.h"
|
|
||||||
#include "ft_ping_flags.h"
|
#include "ft_ping_flags.h"
|
||||||
|
|
||||||
int
|
int
|
||||||
|
|
|
||||||
|
|
@ -1,52 +1,14 @@
|
||||||
#include <stddef.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <getopt.h>
|
|
||||||
|
|
||||||
#include "cli.h"
|
#include "cli.h"
|
||||||
#include "cli_opt.h"
|
#include "internal/cli/arg_handler.h"
|
||||||
#include "ft_ping.h"
|
#include "internal/cli/messages.h"
|
||||||
|
|
||||||
/* Forward declarations */
|
|
||||||
static const char * option_arg_type_to_str(t_option_arg_type type);
|
|
||||||
/* -------------------- */
|
|
||||||
|
|
||||||
int
|
int
|
||||||
cli_handle_help(const char *arg, t_ping_config *config)
|
cli_handle_help(const char *arg, t_ping_config *config)
|
||||||
{
|
{
|
||||||
(void)config;
|
|
||||||
(void)arg;
|
(void)arg;
|
||||||
|
(void)config;
|
||||||
|
|
||||||
printf("ft_ping - Send ICMP ECHO_REQUEST to network hosts\n");
|
print_help();
|
||||||
printf("Usage: ft_ping [options] <destination>\n\n");
|
|
||||||
printf("Options:\n");
|
|
||||||
|
|
||||||
for (size_t i = 0; i < CLI_OPT_LEN; ++i)
|
|
||||||
{
|
|
||||||
const t_option_descriptor *opt = &g_options[i];
|
|
||||||
const char *argstr = option_arg_type_to_str(opt->arg_type);
|
|
||||||
|
|
||||||
printf(" -%c, --%-10s %-8s %s\n",
|
|
||||||
opt->short_opt,
|
|
||||||
opt->long_opt,
|
|
||||||
argstr[0] ? argstr : "",
|
|
||||||
opt->description
|
|
||||||
);
|
|
||||||
}
|
|
||||||
return CLI_EXIT_SUCCESS;
|
return CLI_EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
static const char *
|
|
||||||
option_arg_type_to_str(t_option_arg_type type)
|
|
||||||
{
|
|
||||||
switch (type)
|
|
||||||
{
|
|
||||||
case OPT_ARG_NONE: return "";
|
|
||||||
case OPT_ARG_INT: return "<NUM>";
|
|
||||||
case OPT_ARG_UINT: return "<NUM>";
|
|
||||||
case OPT_ARG_SECONDS: return "<SEC>";
|
|
||||||
case OPT_ARG_BYTES: return "<SIZE>";
|
|
||||||
case OPT_ARG_TTL: return "<TTL>";
|
|
||||||
case OPT_ARG_STRING: return "<STR>";
|
|
||||||
default: return "<ARG>";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
#include "cli.h"
|
#include "cli.h"
|
||||||
#include "ft_ping.h"
|
#include "internal/cli/arg_handler.h"
|
||||||
|
#include "internal/cli/parse_utils.h"
|
||||||
|
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,3 @@
|
||||||
#include "ft_ping.h"
|
|
||||||
#include "cli.h"
|
#include "cli.h"
|
||||||
#include "ft_ping_flags.h"
|
#include "ft_ping_flags.h"
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
#include "cli.h"
|
#include "cli.h"
|
||||||
#include "ft_ping.h"
|
#include "internal/cli/arg_handler.h"
|
||||||
|
#include "internal/cli/parse_utils.h"
|
||||||
|
|
||||||
#include <stdint.h>
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
#include "cli.h"
|
#include "cli.h"
|
||||||
#include "ft_ping.h"
|
#include "internal/cli/arg_handler.h"
|
||||||
|
#include "internal/cli/parse_utils.h"
|
||||||
|
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
#include "cli.h"
|
#include "cli.h"
|
||||||
#include "ft_ping.h"
|
#include "internal/cli/arg_handler.h"
|
||||||
|
#include "internal/cli/parse_utils.h"
|
||||||
|
|
||||||
#include <stdint.h>
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,6 @@
|
||||||
#include "cli.h"
|
#include "cli.h"
|
||||||
#include "ft_ping.h"
|
#include "internal/cli/arg_handler.h"
|
||||||
#include "version_gen.h"
|
#include "internal/cli/messages.h"
|
||||||
|
|
||||||
#include <stdio.h>
|
|
||||||
|
|
||||||
int
|
int
|
||||||
cli_handle_version(const char *arg, t_ping_config *config)
|
cli_handle_version(const char *arg, t_ping_config *config)
|
||||||
|
|
@ -10,14 +8,6 @@ cli_handle_version(const char *arg, t_ping_config *config)
|
||||||
(void)arg;
|
(void)arg;
|
||||||
(void)config;
|
(void)config;
|
||||||
|
|
||||||
printf("%s version %s\n", g_prog_name.name, PING_VERSION);
|
print_version();
|
||||||
printf("Copyright (C) 2026 lohhiicccc\n");
|
|
||||||
printf("License GPLv3+:");
|
|
||||||
printf("GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>\n");
|
|
||||||
printf("This is free software: you are free to change and redistribute it.\n");
|
|
||||||
printf("There is NO WARRANTY, to the extent permitted by law.\n");
|
|
||||||
printf("\n");
|
|
||||||
printf("Build: %s (commit %s)\n", PING_BUILD_DATE, PING_GIT_COMMIT);
|
|
||||||
|
|
||||||
return CLI_EXIT_SUCCESS;
|
return CLI_EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,5 @@
|
||||||
#include <getopt.h>
|
#include "internal/cli/arg_handler.h"
|
||||||
#include <stddef.h>
|
#include "internal/cli/options.h"
|
||||||
|
|
||||||
#include "cli.h"
|
|
||||||
#include "cli_opt.h"
|
|
||||||
|
|
||||||
#undef X
|
#undef X
|
||||||
#define X(short_opt, long_opt, has_arg, handler, arg_type, desc) \
|
#define X(short_opt, long_opt, has_arg, handler, arg_type, desc) \
|
||||||
0
src/cli/messages/error.c
Normal file
0
src/cli/messages/error.c
Normal file
45
src/cli/messages/help.c
Normal file
45
src/cli/messages/help.c
Normal file
|
|
@ -0,0 +1,45 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#include "internal/cli/arg_handler.h"
|
||||||
|
#include "internal/cli/options.h"
|
||||||
|
|
||||||
|
/* Forward declarations */
|
||||||
|
static inline const char * option_arg_type_to_str(t_option_arg_type type);
|
||||||
|
/* -------------------- */
|
||||||
|
|
||||||
|
void
|
||||||
|
print_help(void)
|
||||||
|
{
|
||||||
|
printf("ft_ping - Send ICMP ECHO_REQUEST to network hosts\n");
|
||||||
|
printf("Usage: ft_ping [options] <destination>\n\n");
|
||||||
|
printf("Options:\n");
|
||||||
|
|
||||||
|
for (size_t i = 0; i < CLI_OPT_LEN; ++i)
|
||||||
|
{
|
||||||
|
const t_option_descriptor *opt = &g_options[i];
|
||||||
|
const char *argstr = option_arg_type_to_str(opt->arg_type);
|
||||||
|
|
||||||
|
printf(" -%c, --%-10s %-8s %s\n",
|
||||||
|
opt->short_opt,
|
||||||
|
opt->long_opt,
|
||||||
|
argstr[0] ? argstr : "",
|
||||||
|
opt->description
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline const char *
|
||||||
|
option_arg_type_to_str(t_option_arg_type type)
|
||||||
|
{
|
||||||
|
switch (type)
|
||||||
|
{
|
||||||
|
case OPT_ARG_NONE: return "";
|
||||||
|
case OPT_ARG_INT: return "<NUM>";
|
||||||
|
case OPT_ARG_UINT: return "<NUM>";
|
||||||
|
case OPT_ARG_SECONDS: return "<SEC>";
|
||||||
|
case OPT_ARG_BYTES: return "<SIZE>";
|
||||||
|
case OPT_ARG_TTL: return "<TTL>";
|
||||||
|
case OPT_ARG_STRING: return "<STR>";
|
||||||
|
default: return "<ARG>";
|
||||||
|
}
|
||||||
|
}
|
||||||
17
src/cli/messages/version.c
Normal file
17
src/cli/messages/version.c
Normal file
|
|
@ -0,0 +1,17 @@
|
||||||
|
#include "version_gen.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
void
|
||||||
|
print_version(void)
|
||||||
|
{
|
||||||
|
printf("%s version %s\n", g_prog_name.name, PING_VERSION);
|
||||||
|
printf("Copyright (C) 2026 lohhiicccc\n");
|
||||||
|
printf("License GPLv3+:");
|
||||||
|
printf("GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>\n");
|
||||||
|
printf("This is free software: you are free to change and redistribute it.\n");
|
||||||
|
printf("There is NO WARRANTY, to the extent permitted by law.\n");
|
||||||
|
printf("\n");
|
||||||
|
#if PING_HAS_GIT_COMMIT
|
||||||
|
printf("Build: %s (commit %s)\n", PING_BUILD_DATE, PING_GIT_COMMIT);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
@ -3,10 +3,17 @@
|
||||||
#include <getopt.h>
|
#include <getopt.h>
|
||||||
|
|
||||||
#include "cli.h"
|
#include "cli.h"
|
||||||
#include "cli_opt.h"
|
#include "internal/cli/arg_handler.h"
|
||||||
#include "ft_ping.h"
|
#include "internal/cli/options.h"
|
||||||
#include "ft_ping_const.h"
|
#include "ft_ping_const.h"
|
||||||
|
|
||||||
|
/* Map -? to -h to support help shortcut */
|
||||||
|
#define HANDLE_QUESTION_MARK(opt) \
|
||||||
|
do { \
|
||||||
|
if (opt == '?' && (optopt) == '?') \
|
||||||
|
opt = 'h'; \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
/* Forward declatation */
|
/* Forward declatation */
|
||||||
static void init_config(t_ping_config *config);
|
static void init_config(t_ping_config *config);
|
||||||
static void build_long_options(struct option *long_opts, size_t nb_opts,
|
static void build_long_options(struct option *long_opts, size_t nb_opts,
|
||||||
|
|
@ -24,8 +31,9 @@ cli_parse_arguments(int argc, char **argv, t_ping_config *config)
|
||||||
|
|
||||||
init_config(config);
|
init_config(config);
|
||||||
build_long_options(long_opts, CLI_OPT_LEN, g_options);
|
build_long_options(long_opts, CLI_OPT_LEN, g_options);
|
||||||
while (-1 != (opt = getopt_long(argc, argv, get_optstr(), long_opts, NULL)))
|
while (-1 != (opt = getopt_long(argc, argv, cli_get_optstr(), long_opts, NULL)))
|
||||||
{
|
{
|
||||||
|
HANDLE_QUESTION_MARK(opt);
|
||||||
res = handle_one_option(opt, optarg, config);
|
res = handle_one_option(opt, optarg, config);
|
||||||
if (0 != res)
|
if (0 != res)
|
||||||
return res;
|
return res;
|
||||||
|
|
|
||||||
40
src/cli/parse_utils/get_optstr.c
Normal file
40
src/cli/parse_utils/get_optstr.c
Normal file
|
|
@ -0,0 +1,40 @@
|
||||||
|
#include "internal/cli/arg_handler.h"
|
||||||
|
#include "internal/cli/options.h"
|
||||||
|
|
||||||
|
/* Forward declarations */
|
||||||
|
static void init_opt_str(char *str);
|
||||||
|
/* -------------------- */
|
||||||
|
|
||||||
|
const char *
|
||||||
|
cli_get_optstr(void)
|
||||||
|
{
|
||||||
|
/* Static string to act like a global */
|
||||||
|
static char opt_str[CLI_OPT_STR_LEN] = "";
|
||||||
|
|
||||||
|
/* Lazy init */
|
||||||
|
if ('\0' == *opt_str)
|
||||||
|
init_opt_str(opt_str);
|
||||||
|
|
||||||
|
return opt_str;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
init_opt_str(char *dest)
|
||||||
|
{
|
||||||
|
/* Mute default error messages */
|
||||||
|
dest[0] = ':';
|
||||||
|
|
||||||
|
size_t str_i = 1;
|
||||||
|
for (size_t opt_i = 0; opt_i < CLI_OPT_LEN; ++opt_i)
|
||||||
|
{
|
||||||
|
dest[str_i++] = g_options[opt_i].short_opt;
|
||||||
|
switch (g_options[opt_i].has_arg)
|
||||||
|
{
|
||||||
|
case optional_argument: dest[str_i++] = ':'; /* fall through */
|
||||||
|
case required_argument: dest[str_i++] = ':'; break;
|
||||||
|
default: break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
dest[str_i] = '\0';
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
@ -1,6 +1,5 @@
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <inttypes.h>
|
#include <inttypes.h>
|
||||||
#include <stdint.h>
|
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
|
||||||
int
|
int
|
||||||
|
|
@ -1,36 +0,0 @@
|
||||||
#include "cli.h"
|
|
||||||
#include "cli_opt.h"
|
|
||||||
#include <stddef.h>
|
|
||||||
|
|
||||||
/* Forward declarations */
|
|
||||||
static void init_opt_str(char *str);
|
|
||||||
/* -------------------- */
|
|
||||||
|
|
||||||
const char *
|
|
||||||
get_optstr(void)
|
|
||||||
{
|
|
||||||
static char opt_str[CLI_OPT_STR_LEN] = "";
|
|
||||||
|
|
||||||
if ('\0' == *opt_str)
|
|
||||||
init_opt_str(opt_str);
|
|
||||||
|
|
||||||
return opt_str;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
init_opt_str(char *opt_str)
|
|
||||||
{
|
|
||||||
size_t str_i = 0;
|
|
||||||
for (size_t opt_i = 0; opt_i < CLI_OPT_LEN; ++opt_i)
|
|
||||||
{
|
|
||||||
opt_str[str_i++] = g_options[opt_i].short_opt;
|
|
||||||
switch (g_options[opt_i].has_arg)
|
|
||||||
{
|
|
||||||
case optional_argument: opt_str[str_i++] = ':'; /* fall through */
|
|
||||||
case required_argument: opt_str[str_i++] = ':'; break;
|
|
||||||
default: break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
opt_str[str_i] = '\0';
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
@ -4,7 +4,6 @@
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include "cli.h"
|
#include "cli.h"
|
||||||
#include "ft_ping.h"
|
|
||||||
#include "version_gen.h"
|
#include "version_gen.h"
|
||||||
|
|
||||||
/* Forward declarations */
|
/* Forward declarations */
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
#include <criterion/criterion.h>
|
#include <criterion/criterion.h>
|
||||||
#include "cli.h"
|
#include "cli.h"
|
||||||
|
#include "internal/arg_handler.h"
|
||||||
|
|
||||||
/* Test 1: basic test */
|
/* Test 1: basic test */
|
||||||
Test(cli_handle_count, valid_number)
|
Test(cli_handle_count, valid_number)
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
#include "cli.h"
|
|
||||||
#include "ft_ping_flags.h"
|
|
||||||
#include <criterion/criterion.h>
|
#include <criterion/criterion.h>
|
||||||
|
#include "cli.h"
|
||||||
|
#include "internal/arg_handler.h"
|
||||||
|
#include "ft_ping_flags.h"
|
||||||
|
|
||||||
/* Test 1: test flag*/
|
/* Test 1: test flag*/
|
||||||
Test(handle_flood, flag_test)
|
Test(handle_flood, flag_test)
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
#include <criterion/criterion.h>
|
#include <criterion/criterion.h>
|
||||||
#include "cli.h"
|
#include "cli.h"
|
||||||
|
#include "internal/arg_handler.h"
|
||||||
|
|
||||||
/* Test 1: exit code test */
|
/* Test 1: exit code test */
|
||||||
Test(handle_help, exit_code)
|
Test(handle_help, exit_code)
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
#include <criterion/criterion.h>
|
#include <criterion/criterion.h>
|
||||||
#include "cli.h"
|
#include "cli.h"
|
||||||
|
#include "internal/arg_handler.h"
|
||||||
|
|
||||||
/* Test 1: basic test */
|
/* Test 1: basic test */
|
||||||
Test(cli_handle_interval, valid_number)
|
Test(cli_handle_interval, valid_number)
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
#include "cli.h"
|
#include "cli.h"
|
||||||
#include "ft_ping_flags.h"
|
#include "ft_ping_flags.h"
|
||||||
|
#include "internal/arg_handler.h"
|
||||||
#include <criterion/criterion.h>
|
#include <criterion/criterion.h>
|
||||||
|
|
||||||
/* Test 1: test flag*/
|
/* Test 1: test flag*/
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
#include <criterion/criterion.h>
|
#include <criterion/criterion.h>
|
||||||
#include "cli.h"
|
#include "cli.h"
|
||||||
|
#include "internal/arg_handler.h"
|
||||||
|
|
||||||
/* Test 1: basic test */
|
/* Test 1: basic test */
|
||||||
Test(cli_handle_size, valid_number)
|
Test(cli_handle_size, valid_number)
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
#include <criterion/criterion.h>
|
#include <criterion/criterion.h>
|
||||||
#include "cli.h"
|
#include "cli.h"
|
||||||
|
#include "internal/arg_handler.h"
|
||||||
|
|
||||||
/* Test 1: basic test */
|
/* Test 1: basic test */
|
||||||
Test(cli_handle_timeout, valid_number)
|
Test(cli_handle_timeout, valid_number)
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
#include <criterion/criterion.h>
|
#include <criterion/criterion.h>
|
||||||
#include "cli.h"
|
#include "cli.h"
|
||||||
|
#include "internal/arg_handler.h"
|
||||||
|
|
||||||
/* Test 1: basic test */
|
/* Test 1: basic test */
|
||||||
Test(cli_handle_ttl, valid_number)
|
Test(cli_handle_ttl, valid_number)
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
#include "cli.h"
|
#include "cli.h"
|
||||||
#include "ft_ping_flags.h"
|
#include "ft_ping_flags.h"
|
||||||
|
#include "internal/arg_handler.h"
|
||||||
#include <criterion/criterion.h>
|
#include <criterion/criterion.h>
|
||||||
|
|
||||||
/* Test 1: test flag*/
|
/* Test 1: test flag*/
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
#include <criterion/criterion.h>
|
#include <criterion/criterion.h>
|
||||||
#include "cli.h"
|
#include "cli.h"
|
||||||
|
#include "internal/arg_handler.h"
|
||||||
|
|
||||||
/* Test 1: exit code test */
|
/* Test 1: exit code test */
|
||||||
Test(handle_version, exit_code)
|
Test(handle_version, exit_code)
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
#include "cli.h"
|
#include "internal/arg_handler.h"
|
||||||
#include <criterion/criterion.h>
|
#include <criterion/criterion.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
#include <criterion/criterion.h>
|
#include <criterion/criterion.h>
|
||||||
#include "cli.h"
|
#include "internal/parse_utils.h"
|
||||||
|
|
||||||
/* Test 1: Valid simple number */
|
/* Test 1: Valid simple number */
|
||||||
Test(parse_float, valid_number)
|
Test(parse_float, valid_number)
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
#include <criterion/criterion.h>
|
#include <criterion/criterion.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include "cli.h"
|
#include "internal/parse_utils.h"
|
||||||
|
|
||||||
/* Test 1: Valid simple number */
|
/* Test 1: Valid simple number */
|
||||||
Test(parse_uint64, valid_number)
|
Test(parse_uint64, valid_number)
|
||||||
Loading…
Add table
Reference in a new issue