feat: made an executer and commented print functions out for show
This commit is contained in:
parent
b196377066
commit
68efadf4ac
@ -6,7 +6,7 @@
|
|||||||
/* By: willem <willem@student.codam.nl> +#+ */
|
/* By: willem <willem@student.codam.nl> +#+ */
|
||||||
/* +#+ */
|
/* +#+ */
|
||||||
/* Created: 2025/02/08 17:06:07 by willem #+# #+# */
|
/* Created: 2025/02/08 17:06:07 by willem #+# #+# */
|
||||||
/* Updated: 2025/02/11 17:06:54 by whaffman ######## odam.nl */
|
/* Updated: 2025/02/12 20:24:20 by willem ######## odam.nl */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -16,5 +16,12 @@
|
|||||||
# include "minishell.h"
|
# include "minishell.h"
|
||||||
|
|
||||||
char *executor_absolute_path(t_list *env, char *cmd);
|
char *executor_absolute_path(t_list *env, char *cmd);
|
||||||
|
void executor_child(t_command *command);
|
||||||
|
int executor_fork(t_command *command);
|
||||||
|
void executor_create_pipes(t_minishell *minishell);
|
||||||
|
void executor_execute_pipeline(t_minishell *minishell);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#endif // EXECUTOR_H
|
#endif // EXECUTOR_H
|
||||||
|
|||||||
@ -6,7 +6,7 @@
|
|||||||
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||||
/* +#+ */
|
/* +#+ */
|
||||||
/* Created: 2025/02/05 12:36:08 by whaffman #+# #+# */
|
/* Created: 2025/02/05 12:36:08 by whaffman #+# #+# */
|
||||||
/* Updated: 2025/02/12 18:47:30 by willem ######## odam.nl */
|
/* Updated: 2025/02/12 20:35:09 by willem ######## odam.nl */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -50,9 +50,10 @@ typedef struct s_command
|
|||||||
{
|
{
|
||||||
char *command;
|
char *command;
|
||||||
char **args;
|
char **args;
|
||||||
char **environment;
|
t_list *environment;
|
||||||
int fd_in;
|
int fd_in;
|
||||||
int fd_out;
|
int fd_out;
|
||||||
|
int n_pipes;
|
||||||
} t_command;
|
} t_command;
|
||||||
|
|
||||||
typedef struct s_minishell
|
typedef struct s_minishell
|
||||||
|
|||||||
@ -12,19 +12,28 @@
|
|||||||
|
|
||||||
#include "minishell.h"
|
#include "minishell.h"
|
||||||
|
|
||||||
|
void executor_close_pipes(int n_pipes)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = 3;
|
||||||
|
while (i < n_pipes + 3)
|
||||||
|
{
|
||||||
|
close(i);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void executor_child(t_command *command)
|
void executor_child(t_command *command)
|
||||||
{
|
{
|
||||||
|
char *path;
|
||||||
if (command->fd_in != 0)
|
if (command->fd_in != 0)
|
||||||
{
|
|
||||||
dup2(command->fd_in, 0);
|
dup2(command->fd_in, 0);
|
||||||
close(command->fd_in);
|
|
||||||
}
|
|
||||||
if (command->fd_out != 1)
|
if (command->fd_out != 1)
|
||||||
{
|
|
||||||
dup2(command->fd_out, 1);
|
dup2(command->fd_out, 1);
|
||||||
close(command->fd_out);
|
executor_close_pipes(command->n_pipes);
|
||||||
}
|
path = executor_absolute_path(command->environment, command->command);
|
||||||
execve(command->command, command->args, command->environment);
|
execve(path, command->args, environment_get_arr(command->environment));
|
||||||
}
|
}
|
||||||
|
|
||||||
int executor_fork(t_command *command)
|
int executor_fork(t_command *command)
|
||||||
@ -35,6 +44,10 @@ int executor_fork(t_command *command)
|
|||||||
pid = fork();
|
pid = fork();
|
||||||
if (pid > 0)
|
if (pid > 0)
|
||||||
{
|
{
|
||||||
|
if(command->fd_in != 0)
|
||||||
|
close(command->fd_in);
|
||||||
|
if(command->fd_out != 1)
|
||||||
|
close(command->fd_out);
|
||||||
waitpid(pid, &status, 0);
|
waitpid(pid, &status, 0);
|
||||||
return (((status) & 0xff00) >> 8);
|
return (((status) & 0xff00) >> 8);
|
||||||
}
|
}
|
||||||
@ -45,18 +58,70 @@ int executor_fork(t_command *command)
|
|||||||
return (EXIT_FAILURE);
|
return (EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char **argv, char **envp)
|
void executer_create_pipes(t_minishell *minishell)
|
||||||
{
|
{
|
||||||
t_minishell minishell;
|
t_list *current;
|
||||||
t_command command;
|
t_command *command;
|
||||||
|
int fd[2];
|
||||||
|
int fd_in;
|
||||||
|
|
||||||
|
|
||||||
command.command = "/bin/ls";
|
|
||||||
command.args = (char *[]){"ls", "-l", NULL};
|
|
||||||
command.environment = envp;
|
|
||||||
command.fd_in = 0;
|
|
||||||
command.fd_out = 1;
|
|
||||||
|
|
||||||
printf("exit status: %d\n", executor_fork(&command));
|
fd_in = 0;
|
||||||
return (0);
|
current = minishell->commands;
|
||||||
|
while (current)
|
||||||
|
{
|
||||||
|
command = (t_command *)current->content;
|
||||||
|
if (current->next)
|
||||||
|
{
|
||||||
|
pipe(fd);
|
||||||
|
command->fd_out = fd[1];
|
||||||
|
}
|
||||||
|
else
|
||||||
|
command->fd_out = 1;
|
||||||
|
command->fd_in = fd_in;
|
||||||
|
command->n_pipes = (ft_lstsize(minishell->commands) - 1) * 2;
|
||||||
|
fd_in = fd[0];
|
||||||
|
current = current->next;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
void executor_execute_pipeline(t_minishell *minishell)
|
||||||
|
{
|
||||||
|
t_list *current;
|
||||||
|
t_command *command;
|
||||||
|
|
||||||
|
executer_create_pipes(minishell);
|
||||||
|
current = minishell->commands;
|
||||||
|
while (current)
|
||||||
|
{
|
||||||
|
command = (t_command *)current->content;
|
||||||
|
command->environment = minishell->environment;
|
||||||
|
executor_fork(command);
|
||||||
|
current = current->next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// int main(int argc, char **argv, char **envp)
|
||||||
|
// {
|
||||||
|
// t_minishell minishell;
|
||||||
|
// t_command command[3];
|
||||||
|
|
||||||
|
// minishell.commands = ft_lstnew(&command[0]);
|
||||||
|
// ft_lstadd_back(&minishell.commands, ft_lstnew(&command[1]));
|
||||||
|
// ft_lstadd_back(&minishell.commands, ft_lstnew(&command[2]));
|
||||||
|
|
||||||
|
// command[0].command = "/bin/ls";
|
||||||
|
// command[0].args = (char *[]){"ls", "-l", NULL};
|
||||||
|
// command[0].environment = envp;
|
||||||
|
|
||||||
|
// command[1].command = "/usr/bin/grep";
|
||||||
|
// command[1].args = (char *[]){"grep", "src", NULL};
|
||||||
|
// command[1].environment = envp;
|
||||||
|
|
||||||
|
// command[2].command = "/usr/bin/wc";
|
||||||
|
// command[2].args = (char *[]){"wc", "-l", NULL};
|
||||||
|
// command[2].environment = envp;
|
||||||
|
|
||||||
|
// executor_execute_pipeline(&minishell);
|
||||||
|
// return (0);
|
||||||
|
// }
|
||||||
16
src/main.c
16
src/main.c
@ -6,7 +6,7 @@
|
|||||||
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||||
/* +#+ */
|
/* +#+ */
|
||||||
/* Created: 2025/02/04 16:19:22 by whaffman #+# #+# */
|
/* Created: 2025/02/04 16:19:22 by whaffman #+# #+# */
|
||||||
/* Updated: 2025/02/12 12:54:01 by whaffman ######## odam.nl */
|
/* Updated: 2025/02/12 20:46:11 by willem ######## odam.nl */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -14,13 +14,13 @@
|
|||||||
#include "minishell.h"
|
#include "minishell.h"
|
||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
|
|
||||||
static void token_print(void *param)
|
// static void token_print(void *param)
|
||||||
{
|
// {
|
||||||
t_token *token;
|
// t_token *token;
|
||||||
|
|
||||||
token = (t_token *)param;
|
// token = (t_token *)param;
|
||||||
printf("token type %i, value %s\n", token->type, token->value);
|
// printf("token type %i, value %s\n", token->type, token->value);
|
||||||
}
|
// }
|
||||||
|
|
||||||
int main(int argc, char **argv, char **envp)
|
int main(int argc, char **argv, char **envp)
|
||||||
{
|
{
|
||||||
@ -37,7 +37,7 @@ int main(int argc, char **argv, char **envp)
|
|||||||
minishell->line = ft_prompt(minishell);
|
minishell->line = ft_prompt(minishell);
|
||||||
minishell->lexer = ft_lexer_new(minishell->line);
|
minishell->lexer = ft_lexer_new(minishell->line);
|
||||||
minishell->tokens = ft_parse_input(minishell->lexer);
|
minishell->tokens = ft_parse_input(minishell->lexer);
|
||||||
ft_lstiter(minishell->tokens, token_print);
|
//ft_lstiter(minishell->tokens, token_print);
|
||||||
minishell->commands = parser_get_commands(minishell->tokens);
|
minishell->commands = parser_get_commands(minishell->tokens);
|
||||||
simple_builtins(minishell);
|
simple_builtins(minishell);
|
||||||
free_minishell_line(minishell);
|
free_minishell_line(minishell);
|
||||||
|
|||||||
@ -1,33 +1,33 @@
|
|||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
/* */
|
/* */
|
||||||
/* ::: :::::::: */
|
/* :::::::: */
|
||||||
/* parser_get_commands.c :+: :+: :+: */
|
/* parser_get_commands.c :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ */
|
||||||
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
|
/* By: qmennen <qmennen@student.codam.nl> +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+ */
|
||||||
/* Created: 2025/02/11 14:06:02 by qmennen #+# #+# */
|
/* Created: 2025/02/11 14:06:02 by qmennen #+# #+# */
|
||||||
/* Updated: 2025/02/11 16:25:36 by qmennen ### ########.fr */
|
/* Updated: 2025/02/12 20:49:00 by willem ######## odam.nl */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "minishell.h"
|
#include "minishell.h"
|
||||||
|
|
||||||
static void print_cmds(void *content)
|
// static void print_cmds(void *content)
|
||||||
{
|
// {
|
||||||
t_command *cmd;
|
// t_command *cmd;
|
||||||
int i;
|
// int i;
|
||||||
|
|
||||||
cmd = (t_command *)content;
|
// cmd = (t_command *)content;
|
||||||
printf("cmd: %s\n", cmd->command);
|
// printf("cmd: %s\n", cmd->command);
|
||||||
printf("args: ");
|
// printf("args: ");
|
||||||
i = 0;
|
// i = 0;
|
||||||
while (cmd->args[i])
|
// while (cmd->args[i])
|
||||||
{
|
// {
|
||||||
printf("%s ", cmd->args[i]);
|
// printf("%s ", cmd->args[i]);
|
||||||
i++;
|
// i++;
|
||||||
}
|
// }
|
||||||
printf("\n");
|
// printf("\n");
|
||||||
}
|
// }
|
||||||
|
|
||||||
t_list *parser_get_commands(t_list *list)
|
t_list *parser_get_commands(t_list *list)
|
||||||
{
|
{
|
||||||
@ -51,6 +51,6 @@ t_list *parser_get_commands(t_list *list)
|
|||||||
if (current && ((t_token *)current->content)->type != T_WORD)
|
if (current && ((t_token *)current->content)->type != T_WORD)
|
||||||
current = current->next;
|
current = current->next;
|
||||||
}
|
}
|
||||||
ft_lstiter(command_list, print_cmds);
|
//ft_lstiter(command_list, print_cmds);
|
||||||
return (command_list);
|
return (command_list);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -6,7 +6,7 @@
|
|||||||
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||||
/* +#+ */
|
/* +#+ */
|
||||||
/* Created: 2025/02/05 16:21:39 by whaffman #+# #+# */
|
/* Created: 2025/02/05 16:21:39 by whaffman #+# #+# */
|
||||||
/* Updated: 2025/02/12 12:52:30 by whaffman ######## odam.nl */
|
/* Updated: 2025/02/12 20:25:23 by willem ######## odam.nl */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -81,8 +81,6 @@ void fork_execve(t_minishell *minishell, char *path, char **argv)
|
|||||||
|
|
||||||
void simple_builtins(t_minishell *minishell)
|
void simple_builtins(t_minishell *minishell)
|
||||||
{
|
{
|
||||||
char *path;
|
|
||||||
|
|
||||||
if (!minishell->tokens)
|
if (!minishell->tokens)
|
||||||
return ;
|
return ;
|
||||||
if (cmp_value(minishell->tokens, "clear"))
|
if (cmp_value(minishell->tokens, "clear"))
|
||||||
@ -100,13 +98,6 @@ void simple_builtins(t_minishell *minishell)
|
|||||||
environment_del(&(minishell->environment), ((t_token *)minishell->tokens->next->content)->value);
|
environment_del(&(minishell->environment), ((t_token *)minishell->tokens->next->content)->value);
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
path = executor_absolute_path(minishell->environment, ((t_token *)minishell->tokens->content)->value);
|
executor_execute_pipeline(minishell);
|
||||||
if (path == NULL)
|
|
||||||
printf("minishell: %s: command not found\n", ((t_token *)minishell->tokens->content)->value);
|
|
||||||
else
|
|
||||||
{
|
|
||||||
printf("found excutable: %s\n", path);
|
|
||||||
fork_execve(minishell, path, ft_split(((t_token *)minishell->tokens->content)->value, ' '));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user