diff --git a/src/execution/execution.c b/src/execution/execution.c index 855fd37..0066681 100644 --- a/src/execution/execution.c +++ b/src/execution/execution.c @@ -12,14 +12,14 @@ * @brief converts a linked list of command arguments to an argv array. Don't * forget to free the result * - * @param cmd_list Linked list of command arguments + * @param command_list Linked list of command arguments * @return char** Array of command arguments suitable for execvp. Terminated by * NULL */ -static char **list_to_argv(struct list *cmd_list) +static char **list_to_argv(struct list *command_list) { size_t len = 0; - struct list *cur = cmd_list; + struct list *cur = command_list; while (cur) { @@ -32,7 +32,7 @@ static char **list_to_argv(struct list *cmd_list) { return NULL; } - cur = cmd_list; + cur = command_list; for (size_t i = 0; i < len; i++) { @@ -45,19 +45,19 @@ static char **list_to_argv(struct list *cmd_list) } /** - * @brief Executes a command represented by an ast_cmd structure + * @brief Executes a command represented by an ast_command structure * - * @param cmd The command to execute + * @param command The command to execute * @return int The exit status of the command */ -static int exec_command(struct ast_cmd *cmd) +static int exec_command(struct ast_command *command) { - if (!cmd || !(cmd->cmd)) + if (!command || !(command->command)) { return -1; } - char **argv = list_to_argv(cmd->cmd); + char **argv = list_to_argv(command->command); if (!argv || !(argv[0])) { @@ -112,8 +112,8 @@ int execution(struct ast *ast) return 0; } case AST_CMD: { - struct ast_cmd *cmd = ast_get_cmd(ast); - return exec_command(cmd); // It's recursive + struct ast_command *command = ast_get_command(ast); + return exec_command(command); // It's recursive } case AST_IF: { struct ast_if *if_node = ast_get_if(ast); diff --git a/src/expansion/expansion.c b/src/expansion/expansion.c index 406c00b..fe28dd0 100644 --- a/src/expansion/expansion.c +++ b/src/expansion/expansion.c @@ -14,15 +14,15 @@ // return iter - start; // } -struct ast_cmd *expand(struct ast_cmd *cmd) +struct ast_command *expand(struct ast_command *command) { - if (cmd == NULL) + if (command == NULL) return NULL; bool in_quotes = false; char *str; size_t len; - struct list *l = cmd->cmd; + struct list *l = command->command; while (l != NULL) { @@ -69,19 +69,19 @@ struct ast_cmd *expand(struct ast_cmd *cmd) l = l->next; } - return cmd; + return command; } // int main() // { // printf("Expansion module test\n"); -// struct ast_cmd ast_cmd; +// struct ast_command ast_command; // // char str[] = "echo Hello $?"; // char str[] = "echo Hello $AE86"; -// ast_cmd.cmd = list_append(NULL, str); +// ast_command.command = list_append(NULL, str); -// struct ast_cmd *cmd2 = expand(&ast_cmd); -// printf("cmd2: %s\n", (char *)cmd2->cmd->data); +// struct ast_command *command2 = expand(&ast_command); +// printf("command2: %s\n", (char *)command2->command->data); // return 0; // } diff --git a/src/io_backend/io_backend.c b/src/io_backend/io_backend.c index eef080a..c3c38e3 100644 --- a/src/io_backend/io_backend.c +++ b/src/io_backend/io_backend.c @@ -42,7 +42,7 @@ int iob_init(struct iob_context *ctx) return 0; case IOB_MODE_CMD: - if (context.args != NULL) + if (context.args == NULL) return IOB_ERROR_BAD_ARG; state = IOB_STATE_READY; return 0; @@ -121,7 +121,7 @@ int iob_config_from_args(struct args_options *args, struct iob_context *ctx) case INPUT_CMD: ctx->mode = IOB_MODE_CMD; - ctx->args = NULL; + ctx->args = (char *)args->input_source; break; default: diff --git a/src/utils/ast/ast.c b/src/utils/ast/ast.c index e04e170..2d0ce84 100644 --- a/src/utils/ast/ast.c +++ b/src/utils/ast/ast.c @@ -2,25 +2,26 @@ #include -void ast_free(struct ast *node) +void ast_free(struct ast **node) { - if (node == NULL) + if (*node == NULL) return; // ast void does not need to be freed. - if (ast_is_if(node)) - ast_free_if(ast_get_if(node)); - else if (ast_is_command(node)) - ast_free_command(ast_get_command(node)); - else if (ast_is_list(node)) - ast_free_list(ast_get_list(node)); + if (ast_is_if(*node)) + ast_free_if(ast_get_if(*node)); + else if (ast_is_command(*node)) + ast_free_command(ast_get_command(*node)); + else if (ast_is_list(*node)) + ast_free_list(ast_get_list(*node)); - free(node); + free(*node); + *node = NULL; } struct ast *ast_create(enum ast_type type, void *data) { struct ast *node = malloc(sizeof(struct ast)); - if (!node) + if (node == NULL) return NULL; node->type = type; diff --git a/src/utils/ast/ast.h b/src/utils/ast/ast.h index 79eeff3..8ac85a0 100644 --- a/src/utils/ast/ast.h +++ b/src/utils/ast/ast.h @@ -7,15 +7,4 @@ #include "utils/ast/ast_list.h" #include "utils/ast/ast_void.h" -/* @brief: returns an ast* with corresponding data and type. - * - * @note: this function should only be called by ast_create_[TYPE] functions. - */ -struct ast *ast_create(enum ast_type type, void *data); - -/* @brief: frees the given ast. If ast is NULL, does nothing. - * - */ -void ast_free(struct ast *node); - #endif /* ! AST_H */ diff --git a/src/utils/ast/ast_base.h b/src/utils/ast/ast_base.h index 955ccdc..432eb2c 100644 --- a/src/utils/ast/ast_base.h +++ b/src/utils/ast/ast_base.h @@ -8,6 +8,7 @@ enum ast_type AST_END, AST_LIST, AST_IF, + AST_VOID, AST_CMD }; @@ -24,4 +25,15 @@ struct ast void *data; }; +/* @brief: returns an ast* with corresponding data and type. + * + * @note: this function should only be called by ast_create_[TYPE] functions. + */ +struct ast *ast_create(enum ast_type type, void *data); + +/* @brief: frees the given ast. If ast is NULL, does nothing. + * + */ +void ast_free(struct ast **node); + #endif /* ! AST_BASE_H */ diff --git a/src/utils/ast/ast_command.c b/src/utils/ast/ast_command.c index 6ad8fcc..920b1ff 100644 --- a/src/utils/ast/ast_command.c +++ b/src/utils/ast/ast_command.c @@ -30,9 +30,10 @@ bool ast_is_command(struct ast *node) return node->type == AST_CMD; } -void ast_free_command(struct ast_command **command_data) +void ast_free_command(struct ast_command *command_data) { - list_deep_destroy((*command_data)->command); - free(*command_data); - *command_data = NULL; + if (command_data == NULL) + return; + list_deep_destroy(command_data->command); + free(command_data); } diff --git a/src/utils/ast/ast_command.h b/src/utils/ast/ast_command.h index 0e442a5..08c62ba 100644 --- a/src/utils/ast/ast_command.h +++ b/src/utils/ast/ast_command.h @@ -30,6 +30,6 @@ struct ast *ast_create_command(struct list *command); /* * @brief: frees the given ast_command and sets the pointer to NULL. */ -void ast_free_command(struct ast_command **command_data); +void ast_free_command(struct ast_command *command_data); #endif /* ! AST_COMMAND_H */ diff --git a/src/utils/ast/ast_if.c b/src/utils/ast/ast_if.c index 242d5fd..6a25a9c 100644 --- a/src/utils/ast/ast_if.c +++ b/src/utils/ast/ast_if.c @@ -31,15 +31,14 @@ bool ast_is_if(struct ast *node) return node->type == AST_IF; } -void ast_free_if(struct ast_if **if_data) +void ast_free_if(struct ast_if *if_data) { - if (if_data == NULL || *if_data == NULL) + if (if_data == NULL) return; - ast_free((*if_data)->condition); - ast_free((*if_data)->then_clause); - ast_free((*if_data)->else_clause); + ast_free(&if_data->condition); + ast_free(&if_data->then_clause); + ast_free(&if_data->else_clause); - free(*if_data); - *if_data = NULL; + free(if_data); } diff --git a/src/utils/ast/ast_if.h b/src/utils/ast/ast_if.h index 2036eb7..51c1844 100644 --- a/src/utils/ast/ast_if.h +++ b/src/utils/ast/ast_if.h @@ -31,6 +31,6 @@ struct ast *ast_create_if(struct ast *condition, struct ast *then_clause, /* * @brief: frees the given ast_if and sets the pointer to NULL. */ -void ast_free_if(struct ast_if **if_data); +void ast_free_if(struct ast_if *if_data); #endif /* ! AST_IF_H */ diff --git a/src/utils/ast/ast_list.c b/src/utils/ast/ast_list.c index fae1f1a..95e7047 100644 --- a/src/utils/ast/ast_list.c +++ b/src/utils/ast/ast_list.c @@ -1,12 +1,14 @@ #include "utils/ast/ast.h" +#include + struct ast *ast_create_list(struct list *list) { struct ast_list *ast_list = malloc(sizeof(struct ast_list)); if (ast_list == NULL) return NULL; - ast_list->list = list; + ast_list->children = list; return ast_create(AST_LIST, ast_list); } @@ -22,12 +24,25 @@ bool ast_is_list(struct ast *node) return node->type == AST_LIST; } -void ast_free_list(struct ast_list **ast_list) +void ast_free_list(struct ast_list *ast_list) { - if (*ast_list == NULL || ast_list == NULL) + if (ast_list == NULL) return; - list_deep_destroy((*ast_list)->children); - free(*ast_list); - *ast_list = NULL; + list_deep_destroy(ast_list->children); + free(ast_list); +} + +void ast_list_deep_destroy(struct list *l) +{ + struct list *elt = l; + struct list *next_elt; + while (elt != NULL) + { + next_elt = elt->next; + + ast_free(elt->data); + free(elt); + elt = next_elt; + } } diff --git a/src/utils/ast/ast_list.h b/src/utils/ast/ast_list.h index 6f1cbd9..591b045 100644 --- a/src/utils/ast/ast_list.h +++ b/src/utils/ast/ast_list.h @@ -11,6 +11,14 @@ struct ast_list struct list *children; // A list of ASTs (ast*) }; +/* +** Release the memory used by the AST list and its AST children +** Does nothing if `list` is `NULL`. +* +* @warning: this function should NEVER be used on a list containing +* anything else than ASTs. +*/ +void ast_list_deep_destroy(struct list *l); /** * Creates a new AST node representing a list of ASTs