fix: updated main to a parse-execute loop and changed error codes from an enum to macros
This commit is contained in:
parent
a4fcce530c
commit
f475f4d4ec
1 changed files with 35 additions and 23 deletions
58
src/main.c
58
src/main.c
|
|
@ -1,4 +1,5 @@
|
||||||
// All the includes
|
// === Includes
|
||||||
|
#include <cstddef>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
|
@ -7,22 +8,24 @@
|
||||||
#include "parser/parser.h"
|
#include "parser/parser.h"
|
||||||
#include "utils/args/args.h"
|
#include "utils/args/args.h"
|
||||||
|
|
||||||
enum return_codes
|
// === Error codes
|
||||||
{
|
|
||||||
SUCCESS = 0,
|
#define SUCCESS 0
|
||||||
ERR_IO_BACKEND = 2,
|
#define ERR_INPUT_PROCESSING 2
|
||||||
ERR_MALLOC = 3
|
#define ERR_MALLOC 3
|
||||||
};
|
#define ERR_GENERIC 4
|
||||||
|
|
||||||
|
// === Functions
|
||||||
|
|
||||||
int main(int argc, char **argv)
|
int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
// Create the options struct (with argument handler)
|
// Create the options struct (with argument handler)
|
||||||
struct args_options options;
|
struct args_options options;
|
||||||
int r = args_handler(argc, argv, &options);
|
int return_code = args_handler(argc, argv, &options);
|
||||||
if (r != 0)
|
if (return_code != 0)
|
||||||
{
|
{
|
||||||
print_usage(stderr, argv[0]);
|
print_usage(stderr, argv[0]);
|
||||||
return ERR_IO_BACKEND;
|
return ERR_INPUT_PROCESSING;
|
||||||
}
|
}
|
||||||
// args_print(&options);
|
// args_print(&options);
|
||||||
|
|
||||||
|
|
@ -36,33 +39,42 @@ int main(int argc, char **argv)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Convert args_options to iob_context
|
// Convert args_options to iob_context
|
||||||
r = iob_config_from_args(&options, io_context);
|
return_code = iob_config_from_args(&options, io_context);
|
||||||
if (r != 0)
|
if (return_code != 0)
|
||||||
{
|
{
|
||||||
fprintf(stderr,
|
fprintf(stderr,
|
||||||
"Error: Failed to configure IO Backend from arguments\n");
|
"Error: Failed to configure IO Backend from arguments\n");
|
||||||
free(io_context);
|
free(io_context);
|
||||||
return ERR_IO_BACKEND;
|
return ERR_INPUT_PROCESSING;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Init IO Backend (with the context struct)
|
// Init IO Backend (with the context struct)
|
||||||
r = iob_init(io_context);
|
return_code = iob_init(io_context);
|
||||||
if (r != 0)
|
if (return_code != 0)
|
||||||
{
|
{
|
||||||
fprintf(stderr,
|
fprintf(stderr,
|
||||||
"Error: IO Backend initialization failed with code %d\n", r);
|
"Error: IO Backend initialization failed with code %d\n",
|
||||||
|
return_code);
|
||||||
free(io_context);
|
free(io_context);
|
||||||
return ERR_IO_BACKEND;
|
return ERR_INPUT_PROCESSING;
|
||||||
}
|
}
|
||||||
|
|
||||||
free(io_context);
|
free(io_context);
|
||||||
|
|
||||||
// Call the parser to get the AST
|
// Retrieve and build first AST
|
||||||
struct ast *command_ast = get_ast(); // We'll pass the options later
|
struct ast *command_ast = get_ast();
|
||||||
|
|
||||||
// Call the executor with the AST
|
// Main parse-execute loop
|
||||||
r = execution(command_ast);
|
while (command_ast != NULL && command_ast->type != AST_END)
|
||||||
|
{
|
||||||
|
// Execute AST
|
||||||
|
return_code = execution(command_ast);
|
||||||
|
|
||||||
// Return the execution return code (last command executed)
|
// Retrieve and build next AST
|
||||||
return r;
|
command_ast = get_ast();
|
||||||
|
}
|
||||||
|
if (command_ast == NULL)
|
||||||
|
return ERR_INPUT_PROCESSING;
|
||||||
|
|
||||||
|
return return_code;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue