feat(compiling en fait): Execution
This commit is contained in:
parent
08803bd591
commit
6ca10b673a
4 changed files with 15 additions and 20 deletions
|
|
@ -42,16 +42,15 @@ static int set_all_redir(struct list *redir_list)
|
||||||
{
|
{
|
||||||
while (redir_list)
|
while (redir_list)
|
||||||
{
|
{
|
||||||
struct ast_redir *redir = (struct ast_redir *)redir_list->data;
|
struct ast *redir_node = (struct ast *)redir_list->data;
|
||||||
|
struct ast_redir *redir = ast_get_redir(redir_node);
|
||||||
int target_fd;
|
int target_fd;
|
||||||
|
|
||||||
if (redir->io_number != -1)
|
if (redir->io_number != -1)
|
||||||
{
|
{
|
||||||
target_fd = redir->io_number;
|
target_fd = redir->io_number;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// assign target_fd depending on redir type
|
|
||||||
if (redir->type == AST_REDIR_TYPE_LESS
|
if (redir->type == AST_REDIR_TYPE_LESS
|
||||||
|| redir->type == AST_REDIR_TYPE_LESSGREAT
|
|| redir->type == AST_REDIR_TYPE_LESSGREAT
|
||||||
|| redir->type == AST_REDIR_TYPE_LESSAND)
|
|| redir->type == AST_REDIR_TYPE_LESSAND)
|
||||||
|
|
@ -63,7 +62,6 @@ static int set_all_redir(struct list *redir_list)
|
||||||
target_fd = 1;
|
target_fd = 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
redir->saved_fd = dup(target_fd);
|
redir->saved_fd = dup(target_fd);
|
||||||
int new_fd = -1;
|
int new_fd = -1;
|
||||||
int flags = 0;
|
int flags = 0;
|
||||||
|
|
@ -130,8 +128,7 @@ static int try_builtin(char **argv, struct hash_map *vars);
|
||||||
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;
|
||||||
set_all_redir(command->redirections ? command->redirections->children
|
set_all_redir(command->redirections);
|
||||||
: NULL);
|
|
||||||
|
|
||||||
if (!command || !(command->command))
|
if (!command || !(command->command))
|
||||||
{
|
{
|
||||||
|
|
@ -142,8 +139,7 @@ int exec_ast_command(struct ast_command *command, struct hash_map *vars)
|
||||||
if (!argv || !(argv[0]))
|
if (!argv || !(argv[0]))
|
||||||
{
|
{
|
||||||
free(argv);
|
free(argv);
|
||||||
unset_all_redir(command->redirections ? command->redirections->children
|
unset_all_redir(command->redirections);
|
||||||
: NULL);
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -151,8 +147,7 @@ int exec_ast_command(struct ast_command *command, struct hash_map *vars)
|
||||||
if (builtin_ret != -1)
|
if (builtin_ret != -1)
|
||||||
{
|
{
|
||||||
free(argv);
|
free(argv);
|
||||||
unset_all_redir(command->redirections ? command->redirections->children
|
unset_all_redir(command->redirections);
|
||||||
: NULL);
|
|
||||||
return builtin_ret;
|
return builtin_ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -161,8 +156,7 @@ int exec_ast_command(struct ast_command *command, struct hash_map *vars)
|
||||||
{
|
{
|
||||||
perror("fork");
|
perror("fork");
|
||||||
free(argv);
|
free(argv);
|
||||||
unset_all_redir(command->redirections ? command->redirections->children
|
unset_all_redir(command->redirections);
|
||||||
: NULL);
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -170,16 +164,14 @@ int exec_ast_command(struct ast_command *command, struct hash_map *vars)
|
||||||
{
|
{
|
||||||
execvp(argv[0], argv);
|
execvp(argv[0], argv);
|
||||||
perror("execvp");
|
perror("execvp");
|
||||||
unset_all_redir(command->redirections ? command->redirections->children
|
unset_all_redir(command->redirections);
|
||||||
: NULL);
|
|
||||||
_exit(127);
|
_exit(127);
|
||||||
}
|
}
|
||||||
|
|
||||||
int status = 0;
|
int status = 0;
|
||||||
waitpid(pid, &status, 0);
|
waitpid(pid, &status, 0);
|
||||||
free(argv);
|
free(argv);
|
||||||
unset_all_redir(command->redirections ? command->redirections->children
|
unset_all_redir(command->redirections);
|
||||||
: NULL);
|
|
||||||
|
|
||||||
if (WIFEXITED(status))
|
if (WIFEXITED(status))
|
||||||
{
|
{
|
||||||
|
|
@ -233,7 +225,8 @@ void unset_all_redir(struct list *redir_list)
|
||||||
{
|
{
|
||||||
while (redir_list)
|
while (redir_list)
|
||||||
{
|
{
|
||||||
struct ast_redir *redir = (struct ast_redir *)redir_list->data;
|
struct ast *redir_node = (struct ast *)redir_list->data;
|
||||||
|
struct ast_redir *redir = ast_get_redir(redir_node);
|
||||||
int target_fd;
|
int target_fd;
|
||||||
if (redir->io_number != -1)
|
if (redir->io_number != -1)
|
||||||
{
|
{
|
||||||
|
|
@ -258,7 +251,6 @@ void unset_all_redir(struct list *redir_list)
|
||||||
close(redir->saved_fd);
|
close(redir->saved_fd);
|
||||||
redir->saved_fd = -1;
|
redir->saved_fd = -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
redir_list = redir_list->next;
|
redir_list = redir_list->next;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,8 @@ libutils_a_SOURCES = \
|
||||||
ast/ast_neg.c \
|
ast/ast_neg.c \
|
||||||
ast/ast_pipe.c \
|
ast/ast_pipe.c \
|
||||||
args/args.c \
|
args/args.c \
|
||||||
vars/vars.c
|
vars/vars.c \
|
||||||
|
ast/ast_assignment.c
|
||||||
|
|
||||||
libutils_a_CPPFLAGS = -I$(top_srcdir)/src
|
libutils_a_CPPFLAGS = -I$(top_srcdir)/src
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@
|
||||||
#include "ast_assignment.h"
|
#include "ast_assignment.h"
|
||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
bool ast_is_assignment(struct ast *node)
|
bool ast_is_assignment(struct ast *node)
|
||||||
{
|
{
|
||||||
|
|
@ -40,7 +41,7 @@ struct ast *ast_create_assignment(char *assignment)
|
||||||
if (!assignment_data)
|
if (!assignment_data)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
init_assignments(assignement_data);
|
init_assignments(assignment_data, assignment);
|
||||||
return ast_create(AST_ASSIGNMENT, assignment_data);
|
return ast_create(AST_ASSIGNMENT, assignment_data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,7 @@ struct ast *ast_create_command(struct list *command, struct list *redirections,
|
||||||
|
|
||||||
command_data->command = command;
|
command_data->command = command;
|
||||||
command_data->redirections = redirections;
|
command_data->redirections = redirections;
|
||||||
|
command_data->assignments = assignments;
|
||||||
|
|
||||||
return ast_create(AST_CMD, command_data);
|
return ast_create(AST_CMD, command_data);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue