feat: fuckning working
This commit is contained in:
parent
56f0a979a7
commit
028d4312af
4 changed files with 28 additions and 49 deletions
|
|
@ -34,7 +34,7 @@ check_PROGRAMS = testsuite
|
||||||
#testsuite_CFLAGS = $(42sh_CFLAGS)
|
#testsuite_CFLAGS = $(42sh_CFLAGS)
|
||||||
#testsuite_CFLAGS += $(CRITERION_CFLAGS)
|
#testsuite_CFLAGS += $(CRITERION_CFLAGS)
|
||||||
|
|
||||||
testsuite_SOURCES = ../tests/unit/lexer/lexer_tests.c \
|
testsuite_SOURCES =
|
||||||
../tests/unit/utils/utils_tests.c \
|
../tests/unit/utils/utils_tests.c \
|
||||||
../tests/unit/expansion/expand.c \
|
../tests/unit/expansion/expand.c \
|
||||||
../tests/unit/expansion/parse_var.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/args.c \
|
||||||
../tests/unit/utils/hash_map.c \
|
../tests/unit/utils/hash_map.c \
|
||||||
../tests/unit/utils/insert_into.c
|
../tests/unit/utils/insert_into.c
|
||||||
|
#../tests/unit/lexer/lexer_tests.c \
|
||||||
testsuite_CPPFLAGS = $(42sh_CPPFLAGS)
|
testsuite_CPPFLAGS = $(42sh_CPPFLAGS)
|
||||||
|
|
||||||
testsuite_LDADD = $(42sh_LDADD) -lcriterion
|
testsuite_LDADD = $(42sh_LDADD) -lcriterion
|
||||||
|
|
|
||||||
|
|
@ -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 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)
|
int exec_ast_command(struct ast_command *command, struct hash_map *vars)
|
||||||
{
|
{
|
||||||
(void)vars;
|
(void)vars;
|
||||||
|
exec_assignment(command->assignments, vars);
|
||||||
set_all_redir(command->redirections);
|
set_all_redir(command->redirections);
|
||||||
|
|
||||||
if (!command || !(command->command))
|
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 ---
|
// --- Builtins ---
|
||||||
|
|
||||||
static int builtin_echo(char **argv)
|
static int builtin_echo(char **argv)
|
||||||
|
|
|
||||||
|
|
@ -151,6 +151,7 @@ bool grammar_init(void)
|
||||||
|
|
||||||
// Simple command
|
// Simple command
|
||||||
add_firsts(RULE_SIMPLE_COMMAND, first(RULE_PREFIX));
|
add_firsts(RULE_SIMPLE_COMMAND, first(RULE_PREFIX));
|
||||||
|
add_first(RULE_SIMPLE_COMMAND, TOKEN_WORD);
|
||||||
|
|
||||||
// Funcdec
|
// Funcdec
|
||||||
add_first(RULE_FUNCDEC, TOKEN_WORD);
|
add_first(RULE_FUNCDEC, TOKEN_WORD);
|
||||||
|
|
|
||||||
|
|
@ -39,7 +39,9 @@ void ast_free(struct ast **node)
|
||||||
case AST_WORD:
|
case AST_WORD:
|
||||||
ast_free_word(ast_get_word(*node));
|
ast_free_word(ast_get_word(*node));
|
||||||
break;
|
break;
|
||||||
|
case AST_ASSIGNMENT:
|
||||||
|
ast_free_assignment(ast_get_assignment(*node));
|
||||||
|
break;
|
||||||
case AST_VOID:
|
case AST_VOID:
|
||||||
case AST_END:
|
case AST_END:
|
||||||
break;
|
break;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue