fix: parse_configuration is functional

This commit is contained in:
Guillem George 2025-11-18 20:25:46 +01:00
parent 27cf51c41d
commit 7d428eda79
2 changed files with 59 additions and 20 deletions

View file

@ -6,11 +6,14 @@
#include <string.h> #include <string.h>
#include "../utils/string/string.h" #include "../utils/string/string.h"
#include "bits/getopt_ext.h"
#define ARG_VALID 0 #define ARG_VALID 0
#define ARG_INVALID 1 #define ARG_INVALID 1
#define ARG_HELP 2 #define ARG_HELP 2
#define ARG_NOT_IMPLEMENTED 2 #define ARG_NOT_IMPLEMENTED 3
#define ARG_NO_VALUE 4
#define ARG_INVALID_VALUE 5
// == Static functions // == Static functions
@ -32,7 +35,8 @@ static void print_help(char *program_name)
puts(""); puts("");
} }
// Just to reduce the number of lines inside handle_opt to not // Just to reduce the number of lines inside handle_opt to not overflow the
// limit imposed by school
static int parse_daemon_arg(struct config *cfg) static int parse_daemon_arg(struct config *cfg)
{ {
if (strcmp(optarg, "start") == 0) if (strcmp(optarg, "start") == 0)
@ -42,7 +46,18 @@ static int parse_daemon_arg(struct config *cfg)
else if (strcmp(optarg, "restart") == 0) else if (strcmp(optarg, "restart") == 0)
cfg->daemon = RESTART; cfg->daemon = RESTART;
else else
return ARG_INVALID; return ARG_INVALID_VALUE;
return ARG_VALID;
}
// Same as for parse_daemon_arg
static int parse_log_arg(struct config *cfg)
{
if (strcmp(optarg, "true") == 0)
cfg->daemon = true;
else
cfg->daemon = false;
return ARG_VALID; return ARG_VALID;
} }
@ -60,7 +75,7 @@ static int handle_opt(char **argv, char opt, struct config *cfg)
} }
if (optarg == NULL) if (optarg == NULL)
return ARG_INVALID; return ARG_NO_VALUE;
// Options with value // Options with value
switch (opt) switch (opt)
@ -106,7 +121,7 @@ static int handle_opt(char **argv, char opt, struct config *cfg)
// Logs // Logs
case 'l': case 'l':
return ARG_NOT_IMPLEMENTED; return parse_log_arg(cfg);
default: default:
// TODO print full flags // TODO print full flags
@ -117,23 +132,50 @@ static int handle_opt(char **argv, char opt, struct config *cfg)
return ARG_VALID; return ARG_VALID;
} }
// Prints the corresponding message to the invalid given argument
static void print_arg_error(int err, char **argv, struct option options[],
int optindex)
{
switch (err)
{
case ARG_INVALID:
printf("%s: Invalid argument '--%s'\n", argv[0],
options[optindex].name);
break;
case ARG_NOT_IMPLEMENTED:
printf("%s: This function is not implemented\n", argv[0]);
break;
case ARG_NO_VALUE:
printf("%s: You must specify a value for '--%s'\n", argv[0],
options[optindex].name);
break;
case ARG_INVALID_VALUE:
printf("%s: Invalid value for '--%s'\n", argv[0],
options[optindex].name);
break;
}
}
// == Main functions // == Main functions
struct config *parse_configuration(int argc, char *argv[]) struct config *parse_configuration(int argc, char *argv[])
{ {
struct option options[] = { // Global struct option options[] = { // Global
{ "daemon", optional_argument, NULL, 'd' }, { "daemon", required_argument, NULL, 'd' },
{ "help", optional_argument, NULL, 'h' }, { "help", no_argument, NULL, 'h' },
// Vhosts // Vhosts
{ "server_name", required_argument, NULL, 's' }, { "server_name", required_argument, NULL, 's' },
{ "port", required_argument, NULL, 'p' }, { "port", required_argument, NULL, 'p' },
{ "ip", required_argument, NULL, 'i' }, { "ip", required_argument, NULL, 'i' },
{ "root_dir", required_argument, NULL, 'r' }, { "root_dir", required_argument, NULL, 'r' },
{ "defaut_file", optional_argument, NULL, 'D' }, { "defaut_file", required_argument, NULL, 'D' },
// Logging // Logging
{ "pid_file", required_argument, NULL, 'P' }, { "pid_file", required_argument, NULL, 'P' },
{ "log_file", optional_argument, NULL, 'L' }, { "log_file", required_argument, NULL, 'L' },
{ "log", optional_argument, NULL, 'l' }, { "log", required_argument, NULL, 'l' },
// End // End
{ NULL, 0, NULL, 0 } { NULL, 0, NULL, 0 }
}; };
@ -150,19 +192,14 @@ struct config *parse_configuration(int argc, char *argv[])
} }
char opt; char opt;
// int optindex; int optindex = 0;
while ((opt = getopt_long(argc, argv, "dhspirl:", options, NULL)) != -1) while ((opt = getopt_long(argc, argv, "dhspirDPLl", options, &optindex))
!= -1)
{ {
int err = handle_opt(argv, opt, config); int err = handle_opt(argv, opt, config);
if (err != ARG_VALID) if (err != ARG_VALID)
{ {
if (err == ARG_INVALID) print_arg_error(err, argv, options, optindex);
// printf("%s: Invalid argument '%s'\n", argv[0],
// argv[optindex]);
printf("%s: Invalid argument\n", argv[0]);
if (err == ARG_NOT_IMPLEMENTED)
printf("%s: This function is not implemented\n", argv[0]);
free(config->servers); free(config->servers);
free(config); free(config);
return NULL; return NULL;

View file

@ -1,8 +1,10 @@
#include <getopt.h>
#include <stdio.h>
#include "config/config.h" #include "config/config.h"
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
// struct config *config = parse_configuration(argc, argv);
parse_configuration(argc, argv); parse_configuration(argc, argv);
return 0; return 0;
} }