「🏗️」 wip: parsing kinda working
This commit is contained in:
2
Makefile
2
Makefile
@ -17,7 +17,7 @@ DEPS = $(SRCS:$(SRCDIR)/%.c=$(OBJSDIR)/%.d)
|
|||||||
# $(info OBJ $(OBJS))
|
# $(info OBJ $(OBJS))
|
||||||
# $(info SRC $(SRCS))
|
# $(info SRC $(SRCS))
|
||||||
|
|
||||||
FLAGS = -Wall -Werror -Wextra -MMD -MP -g
|
FLAGS = -MMD -MP -g
|
||||||
|
|
||||||
RED = \033[0;31m
|
RED = \033[0;31m
|
||||||
GREEN = \033[0;32m
|
GREEN = \033[0;32m
|
||||||
|
@ -1,28 +1,48 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
typedef struct {
|
typedef struct {
|
||||||
char *name;
|
char *name;
|
||||||
char opt;
|
char opt;
|
||||||
char *doc;
|
char *doc;
|
||||||
|
uint8_t arg_nb;
|
||||||
|
uint8_t grp;
|
||||||
} options_t;
|
} options_t;
|
||||||
|
|
||||||
static options_t options[] = {
|
static options_t options[] = {
|
||||||
{
|
#define GRP 0 // AKA random useless sh*t
|
||||||
"help",
|
{"usage", 'u', "give a short usage message", 0, GRP},
|
||||||
'?',
|
{"help", '?', "give this help list", 0, GRP},
|
||||||
"give this help list"
|
{"version", 'V', "print program version", 0, GRP},
|
||||||
},
|
#undef GRP
|
||||||
{
|
#define GRP 1 // AKA mandatory
|
||||||
"verbose",
|
{"verbose", 'v', "verbose output", 0, GRP},
|
||||||
'v',
|
#undef GRP
|
||||||
"verbose output"
|
#define GRP 2 // AKA bonus
|
||||||
}
|
{"count", 'c', "stop after sending NUMBER packets (--count=NUMBER)", 1,
|
||||||
|
GRP},
|
||||||
|
{"size", 's', "send NUMBER data octets (--size=NUMBER)", 1, GRP},
|
||||||
|
{"timeout", 'w', "stop after N seconds (--timeout=N)", 1, GRP}
|
||||||
|
#undef GRP
|
||||||
};
|
};
|
||||||
|
|
||||||
#define OPT_NB 2
|
#define OPT_NB 5
|
||||||
|
|
||||||
#define FT_PING_V 0.1
|
#define FT_PING_V 0.1
|
||||||
|
|
||||||
|
#define OPT_WHILE \
|
||||||
|
int i = 0; \
|
||||||
|
while (i < OPT_NB)
|
||||||
|
|
||||||
|
#define OPT_HANDLE(i) \
|
||||||
|
if (options[i].arg_nb == 1) { \
|
||||||
|
if (!(av + 1) || !*(av + 1)) { \
|
||||||
|
return EX_USAGE; \
|
||||||
|
} \
|
||||||
|
args->arg[i] = *(av + 1); \
|
||||||
|
} \
|
||||||
|
args->opts[i] = true;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Can be used to print the help message if no host (or addr) are
|
* @brief Can be used to print the help message if no host (or addr) are
|
||||||
* provided
|
* provided
|
||||||
|
@ -1,9 +1,13 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <help.h>
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
bool verbose;
|
bool opts[OPT_NB];
|
||||||
|
char *arg[OPT_NB];
|
||||||
} args_t;
|
} args_t;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
29
src/help.c
29
src/help.c
@ -4,18 +4,35 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
void print_help(void) {
|
void print_help(void) {
|
||||||
uint16_t i = 0;
|
|
||||||
|
|
||||||
printf("Usage: ping [OPTION...] HOST ...\n");
|
printf("Usage: ping [OPTION...] HOST ...\n");
|
||||||
printf("Send ICMP ECHO_REQUEST packets to network hosts.\n\n");
|
printf("Send ICMP ECHO_REQUEST packets to network hosts.\n\n");
|
||||||
|
|
||||||
while (i < OPT_NB) {
|
printf(" All the mandatory options : \n");
|
||||||
printf(" -%c, --%s:\t %s\n", options[i].opt, options[i].name, options[i].doc);
|
OPT_WHILE {
|
||||||
|
if (options[i].grp == 1)
|
||||||
|
printf(" -%c, --%s:\t %s\n", options[i].opt, options[i].name,
|
||||||
|
options[i].doc);
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
printf("\nMandatory or optional arguments to long options are also mandatory "
|
i = 0;
|
||||||
|
printf(" All the bonuses options : \n");
|
||||||
|
while (i < OPT_NB) {
|
||||||
|
if (options[i].grp == 2)
|
||||||
|
printf(" -%c, --%s:\t %s\n", options[i].opt, options[i].name,
|
||||||
|
options[i].doc);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
printf(" All the prints and help function : \n");
|
||||||
|
while (i < OPT_NB) {
|
||||||
|
if (options[i].grp == 0)
|
||||||
|
printf(" -%c, --%s:\t %s\n", options[i].opt, options[i].name,
|
||||||
|
options[i].doc);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
printf(
|
||||||
|
"\nMandatory or optional arguments to long options are also mandatory "
|
||||||
"or optional\nfor any corresponding short options.\n\n");
|
"or optional\nfor any corresponding short options.\n\n");
|
||||||
printf("Report bugs to <noreply@adjoly.fr> (pls don't).\n");
|
printf("Report bugs to <noreply@adjoly.fr> :D.\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
void print_no_host(char *av) {
|
void print_no_host(char *av) {
|
||||||
|
13
src/main.c
13
src/main.c
@ -6,6 +6,17 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
void init_args_t(args_t *args) {
|
||||||
|
OPT_WHILE {
|
||||||
|
args->opts[i] = false;
|
||||||
|
args->arg[i] = NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void handle_options(args_t *args) {
|
||||||
|
// TODO: need to make this function D:
|
||||||
|
}
|
||||||
|
|
||||||
int main(int ac, char **av) {
|
int main(int ac, char **av) {
|
||||||
args_t args;
|
args_t args;
|
||||||
if (ac > 1) {
|
if (ac > 1) {
|
||||||
@ -15,7 +26,7 @@ int main(int ac, char **av) {
|
|||||||
else if (ret != EXIT_SUCCESS)
|
else if (ret != EXIT_SUCCESS)
|
||||||
return ret;
|
return ret;
|
||||||
else
|
else
|
||||||
return EXIT_SUCCESS;
|
handle_options(&args);
|
||||||
} else {
|
} else {
|
||||||
print_no_host(*av);
|
print_no_host(*av);
|
||||||
return EX_USAGE;
|
return EX_USAGE;
|
||||||
|
@ -4,35 +4,47 @@
|
|||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
#include <sysexits.h>
|
#include <sysexits.h>
|
||||||
|
|
||||||
|
int handle_long_args(char **av, args_t *args) {
|
||||||
|
OPT_WHILE {
|
||||||
|
if (strncmp((*av) + 2, options[i].name, strlen(options[i].name)) == 0) {
|
||||||
|
OPT_HANDLE(i)
|
||||||
|
}
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return EXIT_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
int opt_parse(char **av, args_t *args) {
|
int opt_parse(char **av, args_t *args) {
|
||||||
char *exec_name = *av;
|
char *exec_name = *av;
|
||||||
|
|
||||||
(void)args;
|
|
||||||
while (*av != NULL) {
|
while (*av != NULL) {
|
||||||
char *a = *av;
|
char *a = *av;
|
||||||
|
|
||||||
if (*a == '-') {
|
if (*a == '-') {
|
||||||
switch ((char)*(a + 1)) {
|
char opt = *(a + 1);
|
||||||
case '-':
|
if (!opt) {
|
||||||
break;
|
|
||||||
case '?':
|
|
||||||
print_help();
|
|
||||||
return -1;
|
|
||||||
case 'v':
|
|
||||||
printf("verboseeee");
|
|
||||||
break;
|
|
||||||
};
|
|
||||||
} else {
|
|
||||||
|
|
||||||
}
|
|
||||||
av++;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (av) {
|
|
||||||
print_no_host(exec_name);
|
print_no_host(exec_name);
|
||||||
return EX_USAGE;
|
return EX_USAGE;
|
||||||
|
} else if (opt == '-') {
|
||||||
|
int ret = handle_long_args(av, args);
|
||||||
|
if (ret == EX_USAGE) {
|
||||||
|
print_no_host(exec_name);
|
||||||
|
return EX_USAGE;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
OPT_WHILE {
|
||||||
|
if (opt == options[i].opt) {
|
||||||
|
OPT_HANDLE(i)
|
||||||
|
}
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
}
|
||||||
|
av++;
|
||||||
}
|
}
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user