fix: parse_configuration is functional
This commit is contained in:
parent
27cf51c41d
commit
7d428eda79
2 changed files with 59 additions and 20 deletions
|
|
@ -6,11 +6,14 @@
|
|||
#include <string.h>
|
||||
|
||||
#include "../utils/string/string.h"
|
||||
#include "bits/getopt_ext.h"
|
||||
|
||||
#define ARG_VALID 0
|
||||
#define ARG_INVALID 1
|
||||
#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
|
||||
|
||||
|
|
@ -32,7 +35,8 @@ static void print_help(char *program_name)
|
|||
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)
|
||||
{
|
||||
if (strcmp(optarg, "start") == 0)
|
||||
|
|
@ -42,7 +46,18 @@ static int parse_daemon_arg(struct config *cfg)
|
|||
else if (strcmp(optarg, "restart") == 0)
|
||||
cfg->daemon = RESTART;
|
||||
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;
|
||||
}
|
||||
|
|
@ -60,7 +75,7 @@ static int handle_opt(char **argv, char opt, struct config *cfg)
|
|||
}
|
||||
|
||||
if (optarg == NULL)
|
||||
return ARG_INVALID;
|
||||
return ARG_NO_VALUE;
|
||||
|
||||
// Options with value
|
||||
switch (opt)
|
||||
|
|
@ -106,7 +121,7 @@ static int handle_opt(char **argv, char opt, struct config *cfg)
|
|||
|
||||
// Logs
|
||||
case 'l':
|
||||
return ARG_NOT_IMPLEMENTED;
|
||||
return parse_log_arg(cfg);
|
||||
|
||||
default:
|
||||
// TODO print full flags
|
||||
|
|
@ -117,23 +132,50 @@ static int handle_opt(char **argv, char opt, struct config *cfg)
|
|||
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
|
||||
|
||||
struct config *parse_configuration(int argc, char *argv[])
|
||||
{
|
||||
struct option options[] = { // Global
|
||||
{ "daemon", optional_argument, NULL, 'd' },
|
||||
{ "help", optional_argument, NULL, 'h' },
|
||||
{ "daemon", required_argument, NULL, 'd' },
|
||||
{ "help", no_argument, NULL, 'h' },
|
||||
// Vhosts
|
||||
{ "server_name", required_argument, NULL, 's' },
|
||||
{ "port", required_argument, NULL, 'p' },
|
||||
{ "ip", required_argument, NULL, 'i' },
|
||||
{ "root_dir", required_argument, NULL, 'r' },
|
||||
{ "defaut_file", optional_argument, NULL, 'D' },
|
||||
{ "defaut_file", required_argument, NULL, 'D' },
|
||||
// Logging
|
||||
{ "pid_file", required_argument, NULL, 'P' },
|
||||
{ "log_file", optional_argument, NULL, 'L' },
|
||||
{ "log", optional_argument, NULL, 'l' },
|
||||
{ "log_file", required_argument, NULL, 'L' },
|
||||
{ "log", required_argument, NULL, 'l' },
|
||||
// End
|
||||
{ NULL, 0, NULL, 0 }
|
||||
};
|
||||
|
|
@ -150,19 +192,14 @@ struct config *parse_configuration(int argc, char *argv[])
|
|||
}
|
||||
|
||||
char opt;
|
||||
// int optindex;
|
||||
while ((opt = getopt_long(argc, argv, "dhspirl:", options, NULL)) != -1)
|
||||
int optindex = 0;
|
||||
while ((opt = getopt_long(argc, argv, "dhspirDPLl", options, &optindex))
|
||||
!= -1)
|
||||
{
|
||||
int err = handle_opt(argv, opt, config);
|
||||
if (err != ARG_VALID)
|
||||
{
|
||||
if (err == ARG_INVALID)
|
||||
// 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]);
|
||||
|
||||
print_arg_error(err, argv, options, optindex);
|
||||
free(config->servers);
|
||||
free(config);
|
||||
return NULL;
|
||||
|
|
|
|||
|
|
@ -1,8 +1,10 @@
|
|||
#include <getopt.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "config/config.h"
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
// struct config *config = parse_configuration(argc, argv);
|
||||
parse_configuration(argc, argv);
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue