feat(cli): parse numeric option arguments and validate values
This commit is contained in:
parent
7304ec95f3
commit
ec3a3b8721
12 changed files with 118 additions and 13 deletions
|
|
@ -45,4 +45,7 @@ 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_version(const char *arg, t_ping_config *config);
|
||||||
int cli_handle_verbose(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);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
|
|
@ -13,6 +13,8 @@ SRCS = $(SRC_DIR)/main.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/utils/parse_float.c
|
||||||
|
|
||||||
TESTS_DIR = tests
|
TESTS_DIR = tests
|
||||||
TESTS= $(TESTS_DIR)/test_main.c
|
TESTS= $(TESTS_DIR)/test_main.c
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,22 @@
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
#include "cli.h"
|
#include "cli.h"
|
||||||
#include "ft_ping.h"
|
#include "ft_ping.h"
|
||||||
|
|
||||||
int
|
int
|
||||||
cli_handle_count(const char *arg, t_ping_config *config)
|
cli_handle_count(const char *arg, t_ping_config *config)
|
||||||
{
|
{
|
||||||
(void)arg;
|
uint64_t count;
|
||||||
(void)config;
|
|
||||||
|
if (0 != cli_parse_uint64(arg, &count))
|
||||||
|
{
|
||||||
|
dprintf(STDERR_FILENO, "Invalide count\n");
|
||||||
|
return CLI_ERROR;
|
||||||
|
}
|
||||||
|
|
||||||
|
config->count = count;
|
||||||
return CLI_SUCCESS;
|
return CLI_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,10 @@
|
||||||
#include "cli.h"
|
|
||||||
#include "ft_ping.h"
|
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <getopt.h>
|
#include <getopt.h>
|
||||||
|
|
||||||
|
#include "cli.h"
|
||||||
|
#include "ft_ping.h"
|
||||||
|
|
||||||
/* Forward declarations */
|
/* Forward declarations */
|
||||||
static const char * option_arg_type_to_str(t_option_arg_type type);
|
static const char * option_arg_type_to_str(t_option_arg_type type);
|
||||||
/* -------------------- */
|
/* -------------------- */
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,20 @@
|
||||||
#include "cli.h"
|
#include "cli.h"
|
||||||
#include "ft_ping.h"
|
#include "ft_ping.h"
|
||||||
|
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
int
|
int
|
||||||
cli_handle_interval(const char *arg, t_ping_config *config)
|
cli_handle_interval(const char *arg, t_ping_config *config)
|
||||||
{
|
{
|
||||||
(void)arg;
|
float interval;
|
||||||
(void)config;
|
|
||||||
|
if (0 != cli_parse_float(arg, &interval))
|
||||||
|
{
|
||||||
|
dprintf(STDERR_FILENO, "Invalide interval\n");
|
||||||
|
return CLI_ERROR;
|
||||||
|
}
|
||||||
|
|
||||||
|
config->interval = interval;
|
||||||
return CLI_SUCCESS;
|
return CLI_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,21 @@
|
||||||
#include "cli.h"
|
#include "cli.h"
|
||||||
#include "ft_ping.h"
|
#include "ft_ping.h"
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
int
|
int
|
||||||
cli_handle_size(const char *arg, t_ping_config *config)
|
cli_handle_size(const char *arg, t_ping_config *config)
|
||||||
{
|
{
|
||||||
(void)arg;
|
uint64_t size;
|
||||||
(void)config;
|
|
||||||
|
if (0 != cli_parse_uint64(arg, &size) || size > (65535 - 8))
|
||||||
|
{
|
||||||
|
dprintf(STDERR_FILENO, "Invalide size\n");
|
||||||
|
return CLI_ERROR;
|
||||||
|
}
|
||||||
|
|
||||||
|
config->count = size;
|
||||||
return CLI_SUCCESS;
|
return CLI_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,20 @@
|
||||||
#include "cli.h"
|
#include "cli.h"
|
||||||
#include "ft_ping.h"
|
#include "ft_ping.h"
|
||||||
|
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
int
|
int
|
||||||
cli_handle_timeout(const char *arg, t_ping_config *config)
|
cli_handle_timeout(const char *arg, t_ping_config *config)
|
||||||
{
|
{
|
||||||
(void)arg;
|
float TO;
|
||||||
(void)config;
|
|
||||||
|
if (0 != cli_parse_float(arg, &TO))
|
||||||
|
{
|
||||||
|
dprintf(STDERR_FILENO, "Invalide timeout\n");
|
||||||
|
return CLI_ERROR;
|
||||||
|
}
|
||||||
|
|
||||||
|
config->timeout = TO;
|
||||||
return CLI_SUCCESS;
|
return CLI_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,21 @@
|
||||||
#include "cli.h"
|
#include "cli.h"
|
||||||
#include "ft_ping.h"
|
#include "ft_ping.h"
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
int
|
int
|
||||||
cli_handle_ttl(const char *arg, t_ping_config *config)
|
cli_handle_ttl(const char *arg, t_ping_config *config)
|
||||||
{
|
{
|
||||||
(void)arg;
|
uint64_t ttl;
|
||||||
(void)config;
|
|
||||||
|
if (0 != cli_parse_uint64(arg, &ttl) || ttl > 255)
|
||||||
|
{
|
||||||
|
dprintf(STDERR_FILENO, "Invalide ttl\n");
|
||||||
|
return CLI_ERROR;
|
||||||
|
}
|
||||||
|
|
||||||
|
config->ttl = (uint8_t)ttl;
|
||||||
return CLI_SUCCESS;
|
return CLI_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
|
#include <getopt.h>
|
||||||
|
|
||||||
#include "cli.h"
|
#include "cli.h"
|
||||||
#include <getopt.h>
|
|
||||||
|
|
||||||
const t_option_descriptor g_options[] = {
|
const t_option_descriptor g_options[] = {
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <getopt.h>
|
#include <getopt.h>
|
||||||
|
|
||||||
#include "cli.h"
|
#include "cli.h"
|
||||||
#include "ft_ping.h"
|
#include "ft_ping.h"
|
||||||
#include "ft_ping_const.h"
|
#include "ft_ping_const.h"
|
||||||
|
|
|
||||||
22
src/cli/utils/parse_float.c
Normal file
22
src/cli/utils/parse_float.c
Normal file
|
|
@ -0,0 +1,22 @@
|
||||||
|
#include <errno.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
int
|
||||||
|
cli_parse_float(const char *s, float *out)
|
||||||
|
{
|
||||||
|
char *end;
|
||||||
|
|
||||||
|
if (NULL == s || '\0' == *s || '-' == *s)
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
errno = 0;
|
||||||
|
float v = strtof(s, &end);
|
||||||
|
|
||||||
|
if (s == end || ERANGE == errno || '\0' != *end || 0 == isfinite(v))
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
*out = v;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
22
src/cli/utils/parse_int.c
Normal file
22
src/cli/utils/parse_int.c
Normal file
|
|
@ -0,0 +1,22 @@
|
||||||
|
#include <errno.h>
|
||||||
|
#include <inttypes.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
|
int
|
||||||
|
cli_parse_uint64(const char *s, uint64_t *out)
|
||||||
|
{
|
||||||
|
char *end;
|
||||||
|
|
||||||
|
if ( NULL == s || '\0' == *s || '-' == *s)
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
errno = 0;
|
||||||
|
const uintmax_t v =(uintmax_t)strtoumax(s, &end, 10);
|
||||||
|
|
||||||
|
if (s == end || ERANGE == errno || '\0' != *end || v > UINT64_MAX)
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
*out = (uint64_t)v;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Loading…
Add table
Reference in a new issue