2026-01-16 20:05:49 +01:00
|
|
|
// === Includes
|
2026-01-12 19:30:11 +00:00
|
|
|
#include <stdio.h>
|
2026-01-12 22:27:26 +01:00
|
|
|
#include <stdlib.h>
|
2026-01-12 19:30:11 +00:00
|
|
|
|
2026-01-12 22:27:26 +01:00
|
|
|
#include "execution/execution.h"
|
|
|
|
|
#include "io_backend/io_backend.h"
|
2026-01-20 20:32:59 +01:00
|
|
|
#include "lexer/lexer.h"
|
2026-01-12 22:27:26 +01:00
|
|
|
#include "parser/parser.h"
|
2026-01-12 19:30:11 +00:00
|
|
|
#include "utils/args/args.h"
|
2026-01-18 01:36:04 +00:00
|
|
|
#include "utils/vars/vars.h"
|
2026-01-12 19:30:11 +00:00
|
|
|
|
2026-01-16 20:05:49 +01:00
|
|
|
// === Error codes
|
|
|
|
|
|
|
|
|
|
#define SUCCESS 0
|
|
|
|
|
#define ERR_INPUT_PROCESSING 2
|
|
|
|
|
#define ERR_MALLOC 3
|
|
|
|
|
#define ERR_GENERIC 4
|
|
|
|
|
|
|
|
|
|
// === Functions
|
2026-01-12 22:27:26 +01:00
|
|
|
|
2026-01-24 13:20:29 +01:00
|
|
|
/* @brief: frees the hash map.
|
|
|
|
|
* @return: always ERR_INPUT_PROCESSING.
|
|
|
|
|
*/
|
|
|
|
|
static int err_input(struct hash_map **vars)
|
|
|
|
|
{
|
|
|
|
|
hash_map_free(vars);
|
|
|
|
|
return ERR_INPUT_PROCESSING;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-24 12:55:11 +01:00
|
|
|
static int main_loop(struct lexer_context *ctx, struct args_options *options,
|
|
|
|
|
struct hash_map *vars)
|
|
|
|
|
{
|
|
|
|
|
int return_code = SUCCESS;
|
|
|
|
|
// Retrieve and build first AST
|
|
|
|
|
struct ast *command_ast = get_ast(ctx);
|
|
|
|
|
|
|
|
|
|
if (options->pretty_print)
|
|
|
|
|
{
|
|
|
|
|
ast_print_dot(command_ast);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Main parse-execute loop
|
|
|
|
|
while (command_ast != NULL && command_ast->type != AST_END)
|
|
|
|
|
{
|
|
|
|
|
if (command_ast->type != AST_VOID)
|
|
|
|
|
{
|
|
|
|
|
// Execute AST
|
|
|
|
|
return_code = execution(command_ast, vars);
|
|
|
|
|
|
|
|
|
|
// set $? variable
|
|
|
|
|
set_var_int(vars, "?", return_code);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ast_free(&command_ast);
|
|
|
|
|
|
|
|
|
|
// Retrieve and build next AST
|
|
|
|
|
command_ast = get_ast(ctx);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (command_ast == NULL)
|
2026-01-24 13:20:29 +01:00
|
|
|
return err_input(&vars);
|
2026-01-24 12:55:11 +01:00
|
|
|
|
|
|
|
|
ast_free(&command_ast);
|
|
|
|
|
|
|
|
|
|
return return_code;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-07 17:38:54 +01:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
|
{
|
2026-01-23 17:05:56 +00:00
|
|
|
struct hash_map *vars = vars_init();
|
|
|
|
|
if (vars == NULL)
|
|
|
|
|
{
|
|
|
|
|
fprintf(stderr, "Error: Failed to initialize variables hash map\n");
|
|
|
|
|
return ERR_MALLOC;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-12 22:27:26 +01:00
|
|
|
// Create the options struct (with argument handler)
|
2026-01-12 19:30:11 +00:00
|
|
|
struct args_options options;
|
2026-01-23 17:05:56 +00:00
|
|
|
int return_code = args_handler(argc, argv, &options, vars);
|
2026-01-16 20:05:49 +01:00
|
|
|
if (return_code != 0)
|
2026-01-12 19:30:11 +00:00
|
|
|
{
|
|
|
|
|
print_usage(stderr, argv[0]);
|
2026-01-24 13:20:29 +01:00
|
|
|
return err_input(&vars);
|
2026-01-12 19:30:11 +00:00
|
|
|
}
|
2026-01-12 22:27:26 +01:00
|
|
|
// args_print(&options);
|
|
|
|
|
|
2026-01-18 01:36:04 +00:00
|
|
|
// Initialize variables hash map
|
|
|
|
|
|
2026-01-12 22:27:26 +01:00
|
|
|
// Create the IO-Backend context struct
|
2026-01-24 12:55:11 +01:00
|
|
|
struct iob_context io_context = { 0 };
|
2026-01-12 22:27:26 +01:00
|
|
|
|
|
|
|
|
// Convert args_options to iob_context
|
2026-01-24 12:55:11 +01:00
|
|
|
return_code = iob_config_from_args(&options, &io_context);
|
2026-01-16 20:05:49 +01:00
|
|
|
if (return_code != 0)
|
2026-01-12 22:27:26 +01:00
|
|
|
{
|
|
|
|
|
fprintf(stderr,
|
|
|
|
|
"Error: Failed to configure IO Backend from arguments\n");
|
2026-01-24 13:20:29 +01:00
|
|
|
return err_input(&vars);
|
2026-01-12 22:27:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Init IO Backend (with the context struct)
|
2026-01-24 12:55:11 +01:00
|
|
|
return_code = iob_init(&io_context);
|
2026-01-16 20:05:49 +01:00
|
|
|
if (return_code != 0)
|
2026-01-12 22:27:26 +01:00
|
|
|
{
|
|
|
|
|
fprintf(stderr,
|
2026-01-16 20:05:49 +01:00
|
|
|
"Error: IO Backend initialization failed with code %d\n",
|
|
|
|
|
return_code);
|
2026-01-24 13:20:29 +01:00
|
|
|
return err_input(&vars);
|
2026-01-12 22:27:26 +01:00
|
|
|
}
|
|
|
|
|
|
2026-01-20 20:32:59 +01:00
|
|
|
// init lexer context
|
2026-01-24 12:55:11 +01:00
|
|
|
struct lexer_context ctx = { 0 };
|
2026-01-20 20:32:59 +01:00
|
|
|
|
2026-01-24 12:55:11 +01:00
|
|
|
return_code = main_loop(&ctx, &options,vars);
|
2026-01-12 22:27:26 +01:00
|
|
|
|
2026-01-24 12:55:11 +01:00
|
|
|
// === free
|
2026-01-20 20:32:59 +01:00
|
|
|
|
2026-01-21 15:14:22 +01:00
|
|
|
hash_map_free(&vars);
|
|
|
|
|
|
2026-01-16 20:05:49 +01:00
|
|
|
return return_code;
|
2026-01-07 17:38:54 +01:00
|
|
|
}
|