fix: ast_redir and ast_assignment accorded for every part
This commit is contained in:
parent
51158ec4f9
commit
79fb96e5e2
8 changed files with 39 additions and 22 deletions
|
|
@ -354,7 +354,7 @@ static int builtin_cd(char **argv, struct hash_map *vars)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// char *pwd = getcwd("", "");
|
// char *pwd = getcwd("", "");
|
||||||
char *pwd = get_var_or_env(pwd, "PWD");
|
char *pwd = get_var_or_env(vars, "PWD");
|
||||||
if (chdir(path) != 0)
|
if (chdir(path) != 0)
|
||||||
{
|
{
|
||||||
perror("cd");
|
perror("cd");
|
||||||
|
|
|
||||||
|
|
@ -87,8 +87,8 @@ static void update_flags(char *stream, ssize_t i, struct token_info *info)
|
||||||
{
|
{
|
||||||
if (i == 0)
|
if (i == 0)
|
||||||
{
|
{
|
||||||
perror("Syntax error: word start with a '='");
|
perror("Syntax error: word start with a '='");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
info->has_equal = true;
|
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
|
// Usefull to know if we are inside a quote or double quote
|
||||||
enum lexing_mode lexing_mode = LEXER_NORMAL;
|
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;
|
char *stream = ctx->end_previous_token;
|
||||||
ssize_t i = 0;
|
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
|
// Usefull to know if we are inside a quote or double quote
|
||||||
enum lexing_mode lexing_mode = LEXER_NORMAL;
|
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;
|
char *stream = ctx->end_previous_token;
|
||||||
ssize_t i = 0;
|
ssize_t i = 0;
|
||||||
|
|
||||||
|
|
||||||
if (ctx->current_token != NULL && ctx->current_token->type == TOKEN_EOF)
|
if (ctx->current_token != NULL && ctx->current_token->type == TOKEN_EOF)
|
||||||
{
|
{
|
||||||
// we reached end of input, frees all the token still allocated.
|
// we reached end of input, frees all the token still allocated.
|
||||||
|
|
|
||||||
|
|
@ -6,8 +6,8 @@
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include "grammar_basic.h"
|
|
||||||
#include "grammar.h"
|
#include "grammar.h"
|
||||||
|
#include "grammar_basic.h"
|
||||||
|
|
||||||
static enum ast_redir_type redir_tok_to_ast_type(enum token_type tok_type)
|
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)
|
if (token->type == TOKEN_ASSIGNMENT_WORD)
|
||||||
{
|
{
|
||||||
token = POP_TOKEN();
|
token = POP_TOKEN();
|
||||||
return ast_create_assignment_word(token->data);
|
return ast_create_assignment(token->data);
|
||||||
}
|
}
|
||||||
else if (is_first(*token, RULE_REDIRECTION))
|
else if (is_first(*token, RULE_REDIRECTION))
|
||||||
return parse_redirection(ctx);
|
return parse_redirection(ctx);
|
||||||
|
|
|
||||||
|
|
@ -198,7 +198,7 @@ struct ast *parse_simple_command(struct lexer_context *ctx)
|
||||||
{
|
{
|
||||||
return err_simple_command(command_elements, redirections);
|
return err_simple_command(command_elements, redirections);
|
||||||
}
|
}
|
||||||
if (prefix->type == AST_ASSIGNEMENT)
|
if (prefix->type == AST_ASSIGNMENT)
|
||||||
{
|
{
|
||||||
assignments = list_append(assignments, prefix);
|
assignments = list_append(assignments, prefix);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,5 @@
|
||||||
|
#define _POSIX_C_SOURCE 200809L
|
||||||
|
|
||||||
#include "ast_assignment.h"
|
#include "ast_assignment.h"
|
||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
@ -14,15 +16,31 @@ struct ast_assignment *ast_get_assignment(struct ast *node)
|
||||||
return (struct ast_assignment *)node->data;
|
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)
|
if (!assignment_data)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
assignment_data->name = name;
|
init_assignments(assignement_data);
|
||||||
assignment_data->value = value;
|
|
||||||
|
|
||||||
return ast_create(AST_ASSIGNMENT, assignment_data);
|
return ast_create(AST_ASSIGNMENT, assignment_data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,7 @@ struct ast_assignment
|
||||||
|
|
||||||
bool ast_is_assignment(struct ast *node);
|
bool ast_is_assignment(struct ast *node);
|
||||||
struct ast_assignment *ast_get_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);
|
void ast_free_assignment(struct ast_assignment *assignment_data);
|
||||||
|
|
||||||
#endif /* ! AST_ASSIGNMENT_H */
|
#endif /* ! AST_ASSIGNMENT_H */
|
||||||
|
|
@ -5,8 +5,8 @@
|
||||||
|
|
||||||
#include "../lists/lists.h"
|
#include "../lists/lists.h"
|
||||||
|
|
||||||
struct ast *ast_create_command(struct list *command,
|
struct ast *ast_create_command(struct list *command, struct list *redirections,
|
||||||
struct list *redirections, struct ast_list *assignments)
|
struct ast_list *assignments)
|
||||||
{
|
{
|
||||||
struct ast_command *command_data = malloc(sizeof(struct ast_command));
|
struct ast_command *command_data = malloc(sizeof(struct ast_command));
|
||||||
if (!command_data)
|
if (!command_data)
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,7 @@ struct ast_command
|
||||||
{
|
{
|
||||||
struct list *command; // A list of words (char*)
|
struct list *command; // A list of words (char*)
|
||||||
struct ast_list *redirections; // A list of ASTs, all ast_redir
|
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.
|
* Creates a new AST node representing a command.
|
||||||
*/
|
*/
|
||||||
struct ast *ast_create_command(struct list *command,
|
struct ast *ast_create_command(struct list *command, struct list *redirections,
|
||||||
struct list *redirections, struct ast_list *assignments);
|
struct list *assignments);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* @brief: frees the given ast_command and sets the pointer to NULL.
|
* @brief: frees the given ast_command and sets the pointer to NULL.
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue