fix: clang tidy + made static what could be instead of malloc
This commit is contained in:
parent
de9173a71f
commit
71fadb1b4a
2 changed files with 44 additions and 165 deletions
88
src/main.c
88
src/main.c
|
|
@ -18,6 +18,44 @@
|
|||
|
||||
// === Functions
|
||||
|
||||
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)
|
||||
return ERR_INPUT_PROCESSING;
|
||||
|
||||
ast_free(&command_ast);
|
||||
|
||||
return return_code;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
struct hash_map *vars = vars_init();
|
||||
|
|
@ -40,74 +78,36 @@ int main(int argc, char **argv)
|
|||
// Initialize variables hash map
|
||||
|
||||
// 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;
|
||||
}
|
||||
struct iob_context io_context = { 0 };
|
||||
|
||||
// Convert args_options to iob_context
|
||||
return_code = iob_config_from_args(&options, io_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);
|
||||
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);
|
||||
|
||||
// init lexer context
|
||||
struct lexer_context *ctx = calloc(1, sizeof(struct lexer_context));
|
||||
struct lexer_context ctx = { 0 };
|
||||
|
||||
// Retrieve and build first AST
|
||||
struct ast *command_ast = get_ast(ctx);
|
||||
return_code = main_loop(&ctx, &options,vars);
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
destroy_lexer_context(&ctx);
|
||||
// === free
|
||||
|
||||
hash_map_free(&vars);
|
||||
|
||||
if (command_ast == NULL)
|
||||
return ERR_INPUT_PROCESSING;
|
||||
|
||||
ast_free(&command_ast);
|
||||
|
||||
return return_code;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue