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

@ -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);
}
@ -33,4 +51,4 @@ void ast_free_assignment(struct ast_assignment *assignment_data)
free(assignment_data->name);
free(assignment_data->value);
free(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 */
#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.