diff --git a/src/Makefile.am b/src/Makefile.am index 0dad66f..6ac8528 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -34,7 +34,7 @@ check_PROGRAMS = testsuite #testsuite_CFLAGS = $(42sh_CFLAGS) #testsuite_CFLAGS += $(CRITERION_CFLAGS) -testsuite_SOURCES = ../tests/unit/lexer/lexer_tests.c \ +testsuite_SOURCES = ../tests/unit/utils/utils_tests.c \ ../tests/unit/expansion/expand.c \ ../tests/unit/expansion/parse_var.c \ @@ -42,7 +42,7 @@ testsuite_SOURCES = ../tests/unit/lexer/lexer_tests.c \ ../tests/unit/utils/args.c \ ../tests/unit/utils/hash_map.c \ ../tests/unit/utils/insert_into.c - + #../tests/unit/lexer/lexer_tests.c \ testsuite_CPPFLAGS = $(42sh_CPPFLAGS) testsuite_LDADD = $(42sh_LDADD) -lcriterion diff --git a/src/execution/execution_helpers.c b/src/execution/execution_helpers.c index edbe220..9e44dfa 100644 --- a/src/execution/execution_helpers.c +++ b/src/execution/execution_helpers.c @@ -125,9 +125,31 @@ static char **list_to_argv(struct list *command_list) static int try_builtin(char **argv, struct hash_map *vars); +static int exec_assignment(struct list *assignment_list, struct hash_map *vars) +{ + while (assignment_list != NULL) + { + if (!ast_is_assignment(assignment_list->data)) + { + fprintf(stderr, "list of assignements contains something else"); + return 1; + } + struct ast_assignment *assignment = + ast_get_assignment(assignment_list->data); + + char *key = strdup(assignment->name); + char *value = strdup(assignment->value); + hash_map_insert(vars, key, value, NULL); + + assignment_list = assignment_list->next; + } + return 0; +} + int exec_ast_command(struct ast_command *command, struct hash_map *vars) { (void)vars; + exec_assignment(command->assignments, vars); set_all_redir(command->redirections); if (!command || !(command->command)) @@ -255,52 +277,6 @@ void unset_all_redir(struct list *redir_list) } } -/* -int exec_ast_redir(struct ast_redir *redir, struct hash_map *vars) -{ - int fd_target = get_fd_target(redir); - int saved_fd = dup(fd_target); - int new_fd = -1, flags = 0, mode = 0644; - if (redir->type == AST_REDIR_TYPE_GREAT - || redir->type == AST_REDIR_TYPE_CLOBBER - || redir->type == AST_REDIR_TYPE_DGREAT - || redir->type == AST_REDIR_TYPE_LESS) - { - new_fd = open_redir_file(redir, &flags, &mode); - if (new_fd == -1) - { - perror("open"); - if (saved_fd != -1) - close(saved_fd); - return 1; - } - if (dup2(new_fd, fd_target) == -1) - { - perror("dup2"); - close(new_fd); - if (saved_fd != -1) - close(saved_fd); - return 1; - } - close(new_fd); - } - else if (redir->type == AST_REDIR_TYPE_GREATAND - || redir->type == AST_REDIR_TYPE_LESSAND) - { - new_fd = atoi(redir->filename); - if (dup2(new_fd, fd_target) == -1) - { - perror("dup2"); - if (saved_fd != -1) - close(saved_fd); - return 1; - } - } - int ret = execution(redir->child, vars); - handle_and_restore_fd(saved_fd, fd_target); - return ret; -} */ - // --- Builtins --- static int builtin_echo(char **argv) diff --git a/src/parser/grammar.c b/src/parser/grammar.c index d0258bf..57d7d20 100644 --- a/src/parser/grammar.c +++ b/src/parser/grammar.c @@ -151,6 +151,7 @@ bool grammar_init(void) // Simple command add_firsts(RULE_SIMPLE_COMMAND, first(RULE_PREFIX)); + add_first(RULE_SIMPLE_COMMAND, TOKEN_WORD); // Funcdec add_first(RULE_FUNCDEC, TOKEN_WORD); diff --git a/src/utils/ast/ast.c b/src/utils/ast/ast.c index 16db64e..95a2f15 100644 --- a/src/utils/ast/ast.c +++ b/src/utils/ast/ast.c @@ -39,7 +39,9 @@ void ast_free(struct ast **node) case AST_WORD: ast_free_word(ast_get_word(*node)); break; - + case AST_ASSIGNMENT: + ast_free_assignment(ast_get_assignment(*node)); + break; case AST_VOID: case AST_END: break;