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
|
// === 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)
|
int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
struct hash_map *vars = vars_init();
|
struct hash_map *vars = vars_init();
|
||||||
|
|
@ -40,74 +78,36 @@ int main(int argc, char **argv)
|
||||||
// Initialize variables hash map
|
// Initialize variables hash map
|
||||||
|
|
||||||
// Create the IO-Backend context struct
|
// Create the IO-Backend context struct
|
||||||
struct iob_context *io_context = malloc(sizeof(struct iob_context));
|
struct iob_context io_context = { 0 };
|
||||||
if (io_context == NULL)
|
|
||||||
{
|
|
||||||
fprintf(stderr,
|
|
||||||
"Error: Memory allocation failed for IO Backend context\n");
|
|
||||||
return ERR_MALLOC;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Convert args_options to iob_context
|
// 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)
|
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);
|
|
||||||
return ERR_INPUT_PROCESSING;
|
return ERR_INPUT_PROCESSING;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Init IO Backend (with the context struct)
|
// Init IO Backend (with the context struct)
|
||||||
return_code = iob_init(io_context);
|
return_code = iob_init(&io_context);
|
||||||
if (return_code != 0)
|
if (return_code != 0)
|
||||||
{
|
{
|
||||||
fprintf(stderr,
|
fprintf(stderr,
|
||||||
"Error: IO Backend initialization failed with code %d\n",
|
"Error: IO Backend initialization failed with code %d\n",
|
||||||
return_code);
|
return_code);
|
||||||
free(io_context);
|
|
||||||
return ERR_INPUT_PROCESSING;
|
return ERR_INPUT_PROCESSING;
|
||||||
}
|
}
|
||||||
|
|
||||||
free(io_context);
|
|
||||||
|
|
||||||
// init lexer context
|
// init lexer context
|
||||||
struct lexer_context *ctx = calloc(1, sizeof(struct lexer_context));
|
struct lexer_context ctx = { 0 };
|
||||||
|
|
||||||
// Retrieve and build first AST
|
return_code = main_loop(&ctx, &options,vars);
|
||||||
struct ast *command_ast = get_ast(ctx);
|
|
||||||
|
|
||||||
if (options.pretty_print)
|
// === free
|
||||||
{
|
|
||||||
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);
|
|
||||||
|
|
||||||
hash_map_free(&vars);
|
hash_map_free(&vars);
|
||||||
|
|
||||||
if (command_ast == NULL)
|
|
||||||
return ERR_INPUT_PROCESSING;
|
|
||||||
|
|
||||||
ast_free(&command_ast);
|
|
||||||
|
|
||||||
return return_code;
|
return return_code;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,63 +4,6 @@
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
struct list *list_prepend(struct list *list, void *value)
|
|
||||||
{
|
|
||||||
struct list *new_elt = malloc(sizeof(struct list));
|
|
||||||
if (new_elt == NULL)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
new_elt->next = list;
|
|
||||||
new_elt->data = value;
|
|
||||||
|
|
||||||
return new_elt;
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t list_length(struct list *list)
|
|
||||||
{
|
|
||||||
size_t len = 0;
|
|
||||||
while (list != NULL)
|
|
||||||
{
|
|
||||||
len++;
|
|
||||||
list = list->next;
|
|
||||||
}
|
|
||||||
return len;
|
|
||||||
}
|
|
||||||
|
|
||||||
void list_print(struct list *list)
|
|
||||||
{
|
|
||||||
if (list == NULL)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
while (list != NULL)
|
|
||||||
{
|
|
||||||
if (list->next != NULL)
|
|
||||||
{
|
|
||||||
printf("%p ", list->data);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
printf("%p\n", list->data);
|
|
||||||
}
|
|
||||||
list = list->next;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void list_destroy(struct list *list)
|
|
||||||
{
|
|
||||||
struct list *elt = list;
|
|
||||||
struct list *next_elt;
|
|
||||||
while (elt != NULL)
|
|
||||||
{
|
|
||||||
next_elt = elt->next;
|
|
||||||
free(elt);
|
|
||||||
elt = next_elt;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
struct list *list_append(struct list *list, void *value)
|
struct list *list_append(struct list *list, void *value)
|
||||||
{
|
{
|
||||||
if (list == NULL)
|
if (list == NULL)
|
||||||
|
|
@ -93,36 +36,6 @@ struct list *list_append(struct list *list, void *value)
|
||||||
*******************
|
*******************
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
struct list *list_insert(struct list *list, void *value, size_t index)
|
|
||||||
{
|
|
||||||
if (list == NULL || index == 0)
|
|
||||||
{
|
|
||||||
struct list *new_elt = malloc(sizeof(struct list));
|
|
||||||
new_elt->data = value;
|
|
||||||
new_elt->next = list;
|
|
||||||
return new_elt;
|
|
||||||
}
|
|
||||||
|
|
||||||
struct list *elt = list;
|
|
||||||
|
|
||||||
for (size_t i = 0; i < index - 1; i++)
|
|
||||||
{
|
|
||||||
if (elt->next == NULL)
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
elt = elt->next;
|
|
||||||
}
|
|
||||||
|
|
||||||
struct list *new_elt = malloc(sizeof(struct list));
|
|
||||||
new_elt->data = value;
|
|
||||||
new_elt->next = elt->next;
|
|
||||||
elt->next = new_elt;
|
|
||||||
|
|
||||||
return list;
|
|
||||||
}
|
|
||||||
|
|
||||||
struct list *list_remove(struct list *list, size_t index)
|
struct list *list_remove(struct list *list, size_t index)
|
||||||
{
|
{
|
||||||
struct list *elt = list;
|
struct list *elt = list;
|
||||||
|
|
@ -204,40 +117,6 @@ static void swap_next(struct list *elt)
|
||||||
elt->next->data = elt->data;
|
elt->next->data = elt->data;
|
||||||
elt->data = c;
|
elt->data = c;
|
||||||
}
|
}
|
||||||
struct list *list_sort(struct list *list)
|
|
||||||
{
|
|
||||||
// Bubble sort go !
|
|
||||||
if (list == NULL)
|
|
||||||
{
|
|
||||||
return list;
|
|
||||||
}
|
|
||||||
struct list *elt = list;
|
|
||||||
int len = 0;
|
|
||||||
while (elt->next != NULL)
|
|
||||||
{
|
|
||||||
if (elt->data > elt->next->data)
|
|
||||||
{
|
|
||||||
swap_next(elt);
|
|
||||||
}
|
|
||||||
elt = elt->next;
|
|
||||||
len++;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int i = 1; i < len; i++)
|
|
||||||
{
|
|
||||||
elt = list;
|
|
||||||
while (elt->next != NULL)
|
|
||||||
{
|
|
||||||
if (elt->data > elt->next->data)
|
|
||||||
{
|
|
||||||
swap_next(elt);
|
|
||||||
}
|
|
||||||
elt = elt->next;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return list;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Old proto
|
// Old proto
|
||||||
// WARNING no malloc/free allowed (moulinette issue)
|
// WARNING no malloc/free allowed (moulinette issue)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue