fix: ast_redir and ast_assignment accorded for every part

This commit is contained in:
matteo 2026-01-29 18:46:11 +01:00
parent e0032dd991
commit 98d18eef7d
8 changed files with 39 additions and 22 deletions

View file

@ -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");

View file

@ -169,7 +169,6 @@ struct token *pop_token(struct lexer_context *ctx)
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.

View file

@ -6,8 +6,8 @@
#include <stdlib.h>
#include <string.h>
#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);

View file

@ -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);
}

View file

@ -1,3 +1,5 @@
#define _POSIX_C_SOURCE 200809L
#include "ast_assignment.h"
#include <stdlib.h>
@ -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);
}

View file

@ -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 */

View file

@ -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)

View file

@ -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.