From 79fb96e5e29ca62064b969c309c479ad72810e48 Mon Sep 17 00:00:00 2001 From: matteo Date: Thu, 29 Jan 2026 18:46:11 +0100 Subject: [PATCH] fix: ast_redir and ast_assignment accorded for every part --- src/execution/execution_helpers.c | 2 +- src/lexer/lexer.c | 9 ++++----- src/parser/grammar_advanced.c | 4 ++-- src/parser/grammar_basic.c | 2 +- src/utils/ast/ast_assignment.c | 30 ++++++++++++++++++++++++------ src/utils/ast/ast_assignment.h | 4 ++-- src/utils/ast/ast_command.c | 4 ++-- src/utils/ast/ast_command.h | 6 +++--- 8 files changed, 39 insertions(+), 22 deletions(-) diff --git a/src/execution/execution_helpers.c b/src/execution/execution_helpers.c index aba6dc7..cffcdb0 100644 --- a/src/execution/execution_helpers.c +++ b/src/execution/execution_helpers.c @@ -354,7 +354,7 @@ static int builtin_cd(char **argv, struct hash_map *vars) } } // char *pwd = getcwd("", ""); - char *pwd = get_var_or_env(pwd, "PWD"); + char *pwd = get_var_or_env(vars, "PWD"); if (chdir(path) != 0) { perror("cd"); diff --git a/src/lexer/lexer.c b/src/lexer/lexer.c index db4e066..fd5764d 100644 --- a/src/lexer/lexer.c +++ b/src/lexer/lexer.c @@ -87,8 +87,8 @@ static void update_flags(char *stream, ssize_t i, struct token_info *info) { if (i == 0) { - perror("Syntax error: word start with a '='"); - return; + perror("Syntax error: word start with a '='"); + return; } else info->has_equal = true; @@ -105,7 +105,7 @@ struct token *peek_token(struct lexer_context *ctx) // Usefull to know if we are inside a quote or double quote enum lexing_mode lexing_mode = LEXER_NORMAL; - struct token_info info = {true, 0}; + struct token_info info = { true, 0 }; char *stream = ctx->end_previous_token; ssize_t i = 0; @@ -165,11 +165,10 @@ struct token *pop_token(struct lexer_context *ctx) // Usefull to know if we are inside a quote or double quote enum lexing_mode lexing_mode = LEXER_NORMAL; - struct token_info info = {true, 0}; + struct token_info info = { true, 0 }; char *stream = ctx->end_previous_token; ssize_t i = 0; - if (ctx->current_token != NULL && ctx->current_token->type == TOKEN_EOF) { // we reached end of input, frees all the token still allocated. diff --git a/src/parser/grammar_advanced.c b/src/parser/grammar_advanced.c index 713fd99..d57e70e 100644 --- a/src/parser/grammar_advanced.c +++ b/src/parser/grammar_advanced.c @@ -6,8 +6,8 @@ #include #include -#include "grammar_basic.h" #include "grammar.h" +#include "grammar_basic.h" static enum ast_redir_type redir_tok_to_ast_type(enum token_type tok_type) { @@ -71,7 +71,7 @@ struct ast *parse_prefix(struct lexer_context *ctx) if (token->type == TOKEN_ASSIGNMENT_WORD) { token = POP_TOKEN(); - return ast_create_assignment_word(token->data); + return ast_create_assignment(token->data); } else if (is_first(*token, RULE_REDIRECTION)) return parse_redirection(ctx); diff --git a/src/parser/grammar_basic.c b/src/parser/grammar_basic.c index b979b6e..598c70a 100644 --- a/src/parser/grammar_basic.c +++ b/src/parser/grammar_basic.c @@ -198,7 +198,7 @@ struct ast *parse_simple_command(struct lexer_context *ctx) { return err_simple_command(command_elements, redirections); } - if (prefix->type == AST_ASSIGNEMENT) + if (prefix->type == AST_ASSIGNMENT) { assignments = list_append(assignments, prefix); } diff --git a/src/utils/ast/ast_assignment.c b/src/utils/ast/ast_assignment.c index a2564c2..a839e9e 100644 --- a/src/utils/ast/ast_assignment.c +++ b/src/utils/ast/ast_assignment.c @@ -1,3 +1,5 @@ +#define _POSIX_C_SOURCE 200809L + #include "ast_assignment.h" #include @@ -14,15 +16,31 @@ struct ast_assignment *ast_get_assignment(struct ast *node) return (struct ast_assignment *)node->data; } -struct ast *ast_create_assignment(char *name, char *value) +/* @brief: splits the assignement 'name=value' into 2 parts, + * and fills the fields of ast_assignment with it. + */ +static void init_assignments(struct ast_assignment *ast_assignment, + char *assignment) { - struct ast_assignment *assignment_data = malloc(sizeof(struct ast_assignment)); + if (assignment == NULL) + return; + char *split_pos = strchr(assignment, '='); + if (split_pos == NULL) + return; + + *split_pos = '\0'; + ast_assignment->name = strdup(assignment); + ast_assignment->value = strdup(split_pos + 1); +} + +struct ast *ast_create_assignment(char *assignment) +{ + struct ast_assignment *assignment_data = + calloc(1, sizeof(struct ast_assignment)); if (!assignment_data) return NULL; - assignment_data->name = name; - assignment_data->value = value; - + init_assignments(assignement_data); return ast_create(AST_ASSIGNMENT, assignment_data); } @@ -33,4 +51,4 @@ void ast_free_assignment(struct ast_assignment *assignment_data) free(assignment_data->name); free(assignment_data->value); free(assignment_data); -} \ No newline at end of file +} diff --git a/src/utils/ast/ast_assignment.h b/src/utils/ast/ast_assignment.h index c958c2d..5be56c8 100644 --- a/src/utils/ast/ast_assignment.h +++ b/src/utils/ast/ast_assignment.h @@ -11,7 +11,7 @@ struct ast_assignment bool ast_is_assignment(struct ast *node); struct ast_assignment *ast_get_assignment(struct ast *node); -struct ast *ast_create_assignment(char *name, char *value); +struct ast *ast_create_assignment(char *assignment); void ast_free_assignment(struct ast_assignment *assignment_data); -#endif /* ! AST_ASSIGNMENT_H */ \ No newline at end of file +#endif /* ! AST_ASSIGNMENT_H */ diff --git a/src/utils/ast/ast_command.c b/src/utils/ast/ast_command.c index d7e148d..3865e0e 100644 --- a/src/utils/ast/ast_command.c +++ b/src/utils/ast/ast_command.c @@ -5,8 +5,8 @@ #include "../lists/lists.h" -struct ast *ast_create_command(struct list *command, - struct list *redirections, struct ast_list *assignments) +struct ast *ast_create_command(struct list *command, struct list *redirections, + struct ast_list *assignments) { struct ast_command *command_data = malloc(sizeof(struct ast_command)); if (!command_data) diff --git a/src/utils/ast/ast_command.h b/src/utils/ast/ast_command.h index 9322ac7..75c8b2e 100644 --- a/src/utils/ast/ast_command.h +++ b/src/utils/ast/ast_command.h @@ -8,7 +8,7 @@ struct ast_command { struct list *command; // A list of words (char*) struct ast_list *redirections; // A list of ASTs, all ast_redir - struct ast_list *assignments; // A list of ASTs, all ast_assignment + struct list *assignments; // A list of ASTs, all ast_assignment }; /** @@ -25,8 +25,8 @@ struct ast_command *ast_get_command(struct ast *node); /** * Creates a new AST node representing a command. */ -struct ast *ast_create_command(struct list *command, - struct list *redirections, struct ast_list *assignments); +struct ast *ast_create_command(struct list *command, struct list *redirections, + struct list *assignments); /* * @brief: frees the given ast_command and sets the pointer to NULL.