From 603def159756ed07bfbb2335f32e1e56800b52e5 Mon Sep 17 00:00:00 2001 From: "Gu://em_" Date: Tue, 20 Jan 2026 19:25:55 +0100 Subject: [PATCH] fix: includes with relative path and memory leaks --- src/parser/parsing_utils.c | 57 +++++++++++++++++++++++++------------- 1 file changed, 38 insertions(+), 19 deletions(-) diff --git a/src/parser/parsing_utils.c b/src/parser/parsing_utils.c index 1841c0c..ea1b113 100644 --- a/src/parser/parsing_utils.c +++ b/src/parser/parsing_utils.c @@ -8,13 +8,13 @@ #include #include -#include "lexer/lexer.h" -#include "utils/ast/ast.h" +#include "../lexer/lexer.h" +#include "../utils/ast/ast.h" // === Static functions /* Returns true if c is a command terminator, false otherwise -*/ + */ static bool isterminator(struct token *token) { if (token == NULL) @@ -22,12 +22,12 @@ static bool isterminator(struct token *token) switch (token->type) { - case TOKEN_NEWLINE: - case TOKEN_SEMICOLON: - case TOKEN_EOF: - return true; - default: - return false; + case TOKEN_NEWLINE: + case TOKEN_SEMICOLON: + case TOKEN_EOF: + return true; + default: + return false; } } @@ -70,7 +70,7 @@ struct ast *parse_list(void) current_node = parse_and_or(); if (current_node == NULL) return NULL; - list_append(result_list, current_node); + result_list = list_append(result_list, current_node); // Following and_or commands token = PEEK_TOKEN(); @@ -82,11 +82,11 @@ struct ast *parse_list(void) current_node = parse_and_or(); if (current_node == NULL) { - //TODO free list - // There must be a function for that + // TODO free list + // There must be a function for that return NULL; } - list_append(result_list, current_node); + result_list = list_append(result_list, current_node); token = PEEK_TOKEN(); } } @@ -135,17 +135,37 @@ struct ast *parse_simple_command(void) while (token->type == TOKEN_WORD) { - token = POP_TOKEN(); - char* word = strdup(token->data); + token = pop_token(); + if (token == NULL) + { + // TODO free + return NULL; + } + char *word = strdup(token->data); + if (word == NULL) + { + // TODO free + puts("Internal error: Couldn't copy token content (is memory full " + "?)"); + return NULL; + } command_elements = list_append(command_elements, word); - token = PEEK_TOKEN(); + token = peek_token(); + if (token == NULL) + { + // TODO free + return NULL; + } } struct ast *result = ast_create_command(command_elements); + if (result == NULL) + { + // TODO free + } return result; } -// TODO check compliance with the grammar struct ast *parse_shell_command(void) { return parse_if_rule(); @@ -158,7 +178,7 @@ struct ast *parse_if_rule(void) if (token->type != TOKEN_IF) { puts("Internal error: expected a if rule but token has different " - "type"); + "type"); return NULL; } @@ -267,7 +287,6 @@ struct ast *parse_compound_list(void) token = POP_TOKEN(); } - struct ast *result = ast_create_list(result_list); return result; }