refactor(cli): merge ft_ping.h and ping.h and rename CLI related enums

This commit is contained in:
lohhiiccc 2026-03-16 08:26:40 -05:00
parent e7f4caf342
commit d4dfc554f0
12 changed files with 35 additions and 48 deletions

View file

@ -1,7 +0,0 @@
#ifndef CLI_INTERNAL_ARG_HANDLER
#define CLI_INTERNAL_ARG_HANDLER
#include "cli.h"
#include "internal/ping/cli_handlers.h"
#endif

View file

@ -3,7 +3,7 @@
#include <getopt.h> #include <getopt.h>
#define CLI_OPTIONS_LIST \ #define PING_OPTIONS_LIST \
X( \ X( \
'h', \ 'h', \
"help", \ "help", \
@ -103,14 +103,14 @@
#undef X #undef X
#define X(short_opt, long_opt, has_arg, handler, arg_type, desc) + 1 #define X(short_opt, long_opt, has_arg, handler, arg_type, desc) + 1
enum { CLI_OPT_LEN = (0 CLI_OPTIONS_LIST) }; enum { PING_OPT_LEN = (0 PING_OPTIONS_LIST) };
#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) \
+ (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 = (1 CLI_OPTIONS_LIST) }; enum { PING_OPTSTR_LEN = (1 PING_OPTIONS_LIST) };
/* +1 for ':' to disable getopt error messages */ /* +1 for ':' to disable getopt error messages */
#endif #endif

View file

@ -4,7 +4,7 @@
#include <stddef.h> #include <stddef.h>
#include <netinet/in.h> #include <netinet/in.h>
#include "icmp.h" #include "icmp.h"
#include "ping/ft_ping.h" #include "ping/ping.h"
#include "internal/ping/ping_state.h" #include "internal/ping/ping_state.h"
void ping_output_start(const struct ping_config *config, void ping_output_start(const struct ping_config *config,

View file

@ -2,7 +2,7 @@
#define PING_CLI_PARSE_UTILS_H #define PING_CLI_PARSE_UTILS_H
#include <netinet/in.h> #include <netinet/in.h>
#include "ping/ft_ping.h" #include "ping/ping.h"
int cli_parse_inet_addr(const char *s, struct in_addr *out); int cli_parse_inet_addr(const char *s, struct in_addr *out);
int cli_parse_destinations(int argc, char **argv, int first_arg, int cli_parse_destinations(int argc, char **argv, int first_arg,

View file

@ -6,7 +6,7 @@
#include <signal.h> #include <signal.h>
#include <time.h> #include <time.h>
#include "icmp.h" #include "icmp.h"
#include "ping/ft_ping.h" #include "ping/ping.h"
#include "internal/ping/stats.h" #include "internal/ping/stats.h"
#include "internal/ping/tracker.h" #include "internal/ping/tracker.h"

View file

@ -2,7 +2,7 @@
#define PING_CLI_H #define PING_CLI_H
#include "../cli.h" #include "../cli.h"
#include "ping/ft_ping.h" #include "ping/ping.h"
enum cli_code cli_parse_arguments(int argc, char **argv, enum cli_code cli_parse_arguments(int argc, char **argv,
struct ping_config *config); struct ping_config *config);

View file

@ -1,26 +0,0 @@
#ifndef FT_PING
#define FT_PING
#include <stdint.h>
#include <stddef.h>
#include <netinet/in.h>
struct ping_config
{
/* Targets */
struct in_addr *destinations;
size_t nb_destinations;
/* Options */
uint64_t count;
double interval;
uint8_t ttl;
size_t packet_size;
double timeout;
double deadline;
/* Flags */
uint8_t flags;
};
#endif

View file

@ -1,7 +1,27 @@
#ifndef PING_H #ifndef PING_H
#define PING_H #define PING_H
#include "ping/ft_ping.h" #include <stdint.h>
#include <stddef.h>
#include <netinet/in.h>
struct ping_config
{
/* Targets */
struct in_addr *destinations;
size_t nb_destinations;
/* Options */
uint64_t count;
double interval;
uint8_t ttl;
size_t packet_size;
double timeout;
double deadline;
/* Flags */
uint8_t flags;
};
int ping_run(const struct ping_config *config); int ping_run(const struct ping_config *config);

View file

@ -6,7 +6,7 @@
{ short_opt, long_opt, has_arg, handler, arg_type, desc }, { short_opt, long_opt, has_arg, handler, arg_type, desc },
const struct option_descriptor g_options[] = { const struct option_descriptor g_options[] = {
CLI_OPTIONS_LIST PING_OPTIONS_LIST
{0, NULL, 0, NULL, OPT_ARG_NONE, NULL } {0, NULL, 0, NULL, OPT_ARG_NONE, NULL }
}; };

View file

@ -14,7 +14,7 @@ print_help(void)
printf("Usage: ft_ping [options] <destination>\n\n"); printf("Usage: ft_ping [options] <destination>\n\n");
printf("Options:\n"); printf("Options:\n");
for (size_t i = 0; i < CLI_OPT_LEN; ++i) for (size_t i = 0; i < PING_OPT_LEN; ++i)
{ {
const struct option_descriptor *opt = &g_options[i]; const struct option_descriptor *opt = &g_options[i];
const char *argstr = option_arg_type_to_str(opt->arg_type); const char *argstr = option_arg_type_to_str(opt->arg_type);

View file

@ -1,6 +1,7 @@
#include <string.h> #include <string.h>
#include <getopt.h> #include <getopt.h>
#include "cli.h"
#include "ping/cli.h" #include "ping/cli.h"
#include "ping/ft_ping_const.h" #include "ping/ft_ping_const.h"
#include "internal/ping/cli_handlers.h" #include "internal/ping/cli_handlers.h"
@ -14,14 +15,14 @@ static void init_config(struct ping_config *config);
enum cli_code enum cli_code
cli_parse_arguments(int argc, char **argv, struct ping_config *config) cli_parse_arguments(int argc, char **argv, struct ping_config *config)
{ {
char opt_str[CLI_OPT_STR_LEN]; char opt_str[PING_OPTSTR_LEN];
struct option long_opts[CLI_OPT_LEN + 1]; struct option long_opts[PING_OPT_LEN + 1];
enum cli_code ret; enum cli_code ret;
init_config(config); init_config(config);
ret = cli_parse(argc, argv, config, g_options, CLI_OPT_LEN, ret = cli_parse(argc, argv, config, g_options, PING_OPT_LEN,
opt_str, long_opts); opt_str, long_opts);
if (CLI_SUCCESS != ret) if (CLI_ERROR == ret)
return ret; return ret;
return cli_parse_destinations(argc, argv, optind, config); return cli_parse_destinations(argc, argv, optind, config);
} }

View file

@ -2,7 +2,6 @@
#include <unistd.h> #include <unistd.h>
#include "compiler.h" #include "compiler.h"
#include "ping/ft_ping_flags.h" #include "ping/ft_ping_flags.h"
#include "internal/ping/scheduler.h" #include "internal/ping/scheduler.h"