feat: add CLI argument parsing structure
This commit is contained in:
parent
0ddbb95bad
commit
d573990df8
18 changed files with 245 additions and 3 deletions
19
includes/cli.h
Normal file
19
includes/cli.h
Normal file
|
|
@ -0,0 +1,19 @@
|
||||||
|
#ifndef PING_CLI_H
|
||||||
|
#define PING_CLI_H
|
||||||
|
|
||||||
|
#include "ft_ping.h"
|
||||||
|
|
||||||
|
typedef int (*t_option_handler)(const char *arg, 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);
|
||||||
|
|
||||||
|
#endif
|
||||||
26
includes/ft_ping.h
Normal file
26
includes/ft_ping.h
Normal file
|
|
@ -0,0 +1,26 @@
|
||||||
|
#ifndef FT_PING
|
||||||
|
#define FT_PING
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
|
typedef struct s_ping_config
|
||||||
|
{
|
||||||
|
/* Target */
|
||||||
|
char *destination;
|
||||||
|
|
||||||
|
/* Options */
|
||||||
|
uint64_t count;
|
||||||
|
double interval;
|
||||||
|
uint8_t ttl;
|
||||||
|
size_t packet_size;
|
||||||
|
double timeout;
|
||||||
|
|
||||||
|
/* Flags */
|
||||||
|
uint8_t flags;
|
||||||
|
} t_ping_config;
|
||||||
|
|
||||||
|
int cli_parse_arguments(int argc, char **argv, t_ping_config *config);
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
||||||
15
includes/ft_ping_const.h
Normal file
15
includes/ft_ping_const.h
Normal file
|
|
@ -0,0 +1,15 @@
|
||||||
|
#ifndef FT_PING_CONST
|
||||||
|
#define FT_PING_CONST
|
||||||
|
|
||||||
|
/* Default configuration values */
|
||||||
|
#define DEFAULT_COUNT 0
|
||||||
|
#define DEFAULT_INTERVAL 1.0
|
||||||
|
#define DEFAULT_TTL 64
|
||||||
|
#define DEFAULT_PACKET_SIZE 56
|
||||||
|
#define DEFAULT_TIMEOUT 1.0
|
||||||
|
|
||||||
|
/* Maximum values */
|
||||||
|
#define MAX_PACKET_SIZE 65507
|
||||||
|
#define MAX_TTL 255
|
||||||
|
|
||||||
|
#endif
|
||||||
13
includes/ft_ping_flags.h
Normal file
13
includes/ft_ping_flags.h
Normal file
|
|
@ -0,0 +1,13 @@
|
||||||
|
#ifndef FT_PING_FLAGS
|
||||||
|
#define FT_PING_FLAGS
|
||||||
|
|
||||||
|
#define FLAG_VERBOSE (1 << 0)
|
||||||
|
#define FLAG_QUIET (1 << 1)
|
||||||
|
#define FLAG_FLOOD (1 << 2)
|
||||||
|
|
||||||
|
#define HAS_FLAG(flags, flag) ((flags) & (flag))
|
||||||
|
#define SET_FLAG(flags, flag) ((flags) |= (flag))
|
||||||
|
#define CLEAR_FLAG(flags, flag) ((flags) &= ~(flag))
|
||||||
|
#define TOGGLE_FLAG(flags, flag) ((flags) ^= (flag))
|
||||||
|
|
||||||
|
#endif
|
||||||
14
sources.mk
14
sources.mk
|
|
@ -1,6 +1,18 @@
|
||||||
|
|
||||||
SRC_DIR = srcs
|
SRC_DIR = srcs
|
||||||
SRCS = $(SRC_DIR)/main.c
|
SRCS = $(SRC_DIR)/main.c \
|
||||||
|
$(SRC_DIR)/cli/parse.c \
|
||||||
|
$(SRC_DIR)/cli/handlers/handle_count.c \
|
||||||
|
$(SRC_DIR)/cli/handlers/handle_flood.c \
|
||||||
|
$(SRC_DIR)/cli/handlers/handle_help.c \
|
||||||
|
$(SRC_DIR)/cli/handlers/handle_interval.c \
|
||||||
|
$(SRC_DIR)/cli/handlers/handle_quit.c \
|
||||||
|
$(SRC_DIR)/cli/handlers/handler_map.c \
|
||||||
|
$(SRC_DIR)/cli/handlers/handle_size.c \
|
||||||
|
$(SRC_DIR)/cli/handlers/handle_timeout.c \
|
||||||
|
$(SRC_DIR)/cli/handlers/handle_ttl.c \
|
||||||
|
$(SRC_DIR)/cli/handlers/handle_version.c \
|
||||||
|
$(SRC_DIR)/cli/handlers/hanlde_verbose.c \
|
||||||
|
|
||||||
TESTS_DIR = tests
|
TESTS_DIR = tests
|
||||||
TESTS= $(TESTS_DIR)/test_main.c
|
TESTS= $(TESTS_DIR)/test_main.c
|
||||||
|
|
|
||||||
9
srcs/cli/handlers/handle_count.c
Normal file
9
srcs/cli/handlers/handle_count.c
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
#include "ft_ping.h"
|
||||||
|
|
||||||
|
int
|
||||||
|
cli_handle_count(const char *arg, t_ping_config *config)
|
||||||
|
{
|
||||||
|
(void)arg;
|
||||||
|
(void)config;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
9
srcs/cli/handlers/handle_flood.c
Normal file
9
srcs/cli/handlers/handle_flood.c
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
#include "ft_ping.h"
|
||||||
|
|
||||||
|
int
|
||||||
|
cli_handle_flood(const char *arg, t_ping_config *config)
|
||||||
|
{
|
||||||
|
(void)arg;
|
||||||
|
(void)config;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
9
srcs/cli/handlers/handle_help.c
Normal file
9
srcs/cli/handlers/handle_help.c
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
#include "ft_ping.h"
|
||||||
|
|
||||||
|
int
|
||||||
|
cli_handle_help(const char *arg, t_ping_config *config)
|
||||||
|
{
|
||||||
|
(void)arg;
|
||||||
|
(void)config;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
9
srcs/cli/handlers/handle_interval.c
Normal file
9
srcs/cli/handlers/handle_interval.c
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
#include "ft_ping.h"
|
||||||
|
|
||||||
|
int
|
||||||
|
cli_handle_interval(const char *arg, t_ping_config *config)
|
||||||
|
{
|
||||||
|
(void)arg;
|
||||||
|
(void)config;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
9
srcs/cli/handlers/handle_quit.c
Normal file
9
srcs/cli/handlers/handle_quit.c
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
#include "ft_ping.h"
|
||||||
|
|
||||||
|
int
|
||||||
|
cli_handle_quiet(const char *arg, t_ping_config *config)
|
||||||
|
{
|
||||||
|
(void)arg;
|
||||||
|
(void)config;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
9
srcs/cli/handlers/handle_size.c
Normal file
9
srcs/cli/handlers/handle_size.c
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
#include "ft_ping.h"
|
||||||
|
|
||||||
|
int
|
||||||
|
cli_handle_size(const char *arg, t_ping_config *config)
|
||||||
|
{
|
||||||
|
(void)arg;
|
||||||
|
(void)config;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
9
srcs/cli/handlers/handle_timeout.c
Normal file
9
srcs/cli/handlers/handle_timeout.c
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
#include "ft_ping.h"
|
||||||
|
|
||||||
|
int
|
||||||
|
cli_handle_timeout(const char *arg, t_ping_config *config)
|
||||||
|
{
|
||||||
|
(void)arg;
|
||||||
|
(void)config;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
9
srcs/cli/handlers/handle_ttl.c
Normal file
9
srcs/cli/handlers/handle_ttl.c
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
#include "ft_ping.h"
|
||||||
|
|
||||||
|
int
|
||||||
|
cli_handle_ttl(const char *arg, t_ping_config *config)
|
||||||
|
{
|
||||||
|
(void)arg;
|
||||||
|
(void)config;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
9
srcs/cli/handlers/handle_version.c
Normal file
9
srcs/cli/handlers/handle_version.c
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
#include "ft_ping.h"
|
||||||
|
|
||||||
|
int
|
||||||
|
cli_handle_version(const char *arg, t_ping_config *config)
|
||||||
|
{
|
||||||
|
(void)arg;
|
||||||
|
(void)config;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
26
srcs/cli/handlers/handler_map.c
Normal file
26
srcs/cli/handlers/handler_map.c
Normal file
|
|
@ -0,0 +1,26 @@
|
||||||
|
|
||||||
|
#include "cli.h"
|
||||||
|
#include <getopt.h>
|
||||||
|
|
||||||
|
typedef struct s_option_descriptor
|
||||||
|
{
|
||||||
|
int short_opt;
|
||||||
|
const char *long_opt;
|
||||||
|
int has_arg;
|
||||||
|
t_option_handler handler;
|
||||||
|
const char *description;
|
||||||
|
} t_option_descriptor;
|
||||||
|
|
||||||
|
const t_option_descriptor g_options[] = {
|
||||||
|
{'h', "help", no_argument, cli_handle_help, "Display this help and exit"},
|
||||||
|
{'V', "version", no_argument, cli_handle_version, "Display version information and exit"},
|
||||||
|
{'v', "verbose", no_argument, cli_handle_verbose, "Verbose output"},
|
||||||
|
{'q', "quiet", no_argument, cli_handle_quiet, "Quiet mode (only show summary)"},
|
||||||
|
{'c', "count", required_argument, cli_handle_count, "Stop after sending N packets"},
|
||||||
|
{'i', "interval", required_argument, cli_handle_interval, "Wait N seconds between packets"},
|
||||||
|
{'t', "ttl", required_argument, cli_handle_ttl, "Set Time To Live"},
|
||||||
|
{'s', "size", required_argument, cli_handle_size, "Packet size in bytes"},
|
||||||
|
{'W', "timeout", required_argument, cli_handle_timeout, "Timeout for replies in seconds"},
|
||||||
|
{'f', "flood", no_argument, cli_handle_flood, "Flood mode"},
|
||||||
|
{0, NULL, 0, NULL, NULL}
|
||||||
|
};
|
||||||
9
srcs/cli/handlers/hanlde_verbose.c
Normal file
9
srcs/cli/handlers/hanlde_verbose.c
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
#include "ft_ping.h"
|
||||||
|
|
||||||
|
int
|
||||||
|
cli_handle_verbose(const char *arg, t_ping_config *config)
|
||||||
|
{
|
||||||
|
(void)arg;
|
||||||
|
(void)config;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
29
srcs/cli/parse.c
Normal file
29
srcs/cli/parse.c
Normal file
|
|
@ -0,0 +1,29 @@
|
||||||
|
#include <string.h>
|
||||||
|
#include <getopt.h>
|
||||||
|
#include "ft_ping.h"
|
||||||
|
#include "ft_ping_const.h"
|
||||||
|
|
||||||
|
/* Forward declatation */
|
||||||
|
static void init_config(t_ping_config *config);
|
||||||
|
/* ------------------- */
|
||||||
|
|
||||||
|
int
|
||||||
|
cli_parse_arguments(int argc, char **argv, t_ping_config *config)
|
||||||
|
{
|
||||||
|
(void)argc;
|
||||||
|
(void)argv;
|
||||||
|
init_config(config);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
init_config(t_ping_config *config)
|
||||||
|
{
|
||||||
|
memset(config, 0, sizeof(t_ping_config));
|
||||||
|
config->count = DEFAULT_COUNT;
|
||||||
|
config->interval = DEFAULT_INTERVAL;
|
||||||
|
config->ttl = DEFAULT_TTL;
|
||||||
|
config->packet_size = DEFAULT_PACKET_SIZE;
|
||||||
|
config->timeout = DEFAULT_TIMEOUT;
|
||||||
|
}
|
||||||
|
|
||||||
16
srcs/main.c
16
srcs/main.c
|
|
@ -1,6 +1,18 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include "ft_ping.h"
|
||||||
|
|
||||||
int
|
int
|
||||||
main(void)
|
main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
return 0;
|
t_ping_config config;
|
||||||
|
int ret;
|
||||||
|
|
||||||
|
ret = cli_parse_arguments(argc, argv, &config);
|
||||||
|
if (ret != 0)
|
||||||
|
return (ret < 0) ? EXIT_FAILURE : EXIT_SUCCESS;
|
||||||
|
|
||||||
|
printf("PING: Not yet implemented\n");
|
||||||
|
|
||||||
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue