init: parsing

This commit is contained in:
2025-11-12 01:18:35 +01:00
parent c6503f157a
commit 1757afbe44
2 changed files with 72 additions and 0 deletions

59
src/parsing.c Normal file
View File

@ -0,0 +1,59 @@
#include "parsing.h"
#include "print.h"
#include <stddef.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
static struct param parameters[] = {
{NULL, "?", OPTION, false},
{NULL, "v", OPTION, false},
{NULL, "f", OPTION, false},
{NULL, "n", OPTION, false},
{NULL, "w", ARGUMENT, NULL},
{NULL, "W",ARGUMENT, NULL},
{"ttl", NULL, ARGUMENT, "116"},
{NULL, NULL, NULL, NULL},
};
static struct param *get_parameter(struct param parameters[], const char *str)
{
size_t count = str[0] == '-' + str[1] == '-';
for (size_t i = 0; parameters[i].name || parameters[i].alias; i++)
{
if (count == 2 && !strcmp(parameters[i].name, str + count))
return parameters + i;
else if (count == 1 && !strcmp(parameters[i].alias, str + count))
return parameters + i;
}
return NULL;
}
int parsing(const char * const *av)
{
char *host;
for (size_t i = 0; av[i]; i++)
{
struct param *parameter = get_parameter(parameters, av[i]);
if (parameter)
{
if (parameter->type == OPTION)
parameter->value = (void*)true;
else
{
if (av[i] == NULL)
{
print_err("%s: requirement argument", av[i]);
return NULL;
}
parameter->value = (char*) av[++i];
}
}
else
{
host = (char*) av[i];
}
}
return 0;
}

13
src/parsing.h Normal file
View File

@ -0,0 +1,13 @@
#pragma once
typedef enum {
OPTION,
ARGUMENT,
} e_type;
struct param {
const char *name;
const char *alias;
e_type type;
void *value;
};