42sh/src/main.c

80 lines
1.9 KiB
C
Raw Normal View History

// === Includes
#include <stdio.h>
#include <stdlib.h>
#include "execution/execution.h"
#include "io_backend/io_backend.h"
#include "parser/parser.h"
#include "utils/args/args.h"
// === Error codes
#define SUCCESS 0
#define ERR_INPUT_PROCESSING 2
#define ERR_MALLOC 3
#define ERR_GENERIC 4
// === Functions
2026-01-07 17:38:54 +01:00
int main(int argc, char **argv)
{
// Create the options struct (with argument handler)
struct args_options options;
int return_code = args_handler(argc, argv, &options);
if (return_code != 0)
{
print_usage(stderr, argv[0]);
return ERR_INPUT_PROCESSING;
}
// args_print(&options);
// Create the IO-Backend context struct
struct iob_context *io_context = malloc(sizeof(struct iob_context));
if (io_context == NULL)
{
fprintf(stderr,
"Error: Memory allocation failed for IO Backend context\n");
return ERR_MALLOC;
}
// Convert args_options to iob_context
return_code = iob_config_from_args(&options, io_context);
if (return_code != 0)
{
fprintf(stderr,
"Error: Failed to configure IO Backend from arguments\n");
free(io_context);
return ERR_INPUT_PROCESSING;
}
// Init IO Backend (with the context struct)
return_code = iob_init(io_context);
if (return_code != 0)
{
fprintf(stderr,
"Error: IO Backend initialization failed with code %d\n",
return_code);
free(io_context);
return ERR_INPUT_PROCESSING;
}
free(io_context);
// Retrieve and build first AST
struct ast *command_ast = get_ast();
// Main parse-execute loop
while (command_ast != NULL && command_ast->type != AST_END)
{
// Execute AST
return_code = execution(command_ast);
// Retrieve and build next AST
command_ast = get_ast();
}
if (command_ast == NULL)
return ERR_INPUT_PROCESSING;
return return_code;
2026-01-07 17:38:54 +01:00
}