executo builtin fix?
This commit is contained in:
parent
029c3cbcb0
commit
7d32667e4d
4
Makefile
4
Makefile
@ -6,12 +6,12 @@
|
||||
# By: qmennen <qmennen@student.codam.nl> +#+ #
|
||||
# +#+ #
|
||||
# Created: 2024/10/15 11:48:46 by whaffman #+# #+# #
|
||||
# Updated: 2025/03/03 22:50:22 by whaffman ######## odam.nl #
|
||||
# Updated: 2025/03/04 16:05:38 by whaffman ######## odam.nl #
|
||||
# #
|
||||
# **************************************************************************** #
|
||||
|
||||
NAME = minishell
|
||||
# FLAGS=-DNOCOLORS -DNOBANNER -DNOPROMPT
|
||||
FLAGS=-DNOCOLORS -DNOBANNER -DNOPROMPT
|
||||
|
||||
SRC_PATH = src
|
||||
INC_PATH = inc
|
||||
|
||||
@ -6,7 +6,7 @@
|
||||
/* By: qmennen <qmennen@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* Created: 2025/02/05 12:36:08 by whaffman #+# #+# */
|
||||
/* Updated: 2025/02/26 17:26:27 by whaffman ######## odam.nl */
|
||||
/* Updated: 2025/03/04 15:25:16 by whaffman ######## odam.nl */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -60,7 +60,7 @@ typedef struct s_command
|
||||
{
|
||||
char *command;
|
||||
char **args;
|
||||
t_list *environment;
|
||||
// t_list *environment;
|
||||
t_list *redirect_in;
|
||||
t_list *redirect_out;
|
||||
int fd_in;
|
||||
|
||||
@ -6,7 +6,7 @@
|
||||
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* Created: 2025/02/20 11:33:05 by whaffman #+# #+# */
|
||||
/* Updated: 2025/02/26 15:46:29 by whaffman ######## odam.nl */
|
||||
/* Updated: 2025/03/04 16:16:02 by whaffman ######## odam.nl */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -34,5 +34,5 @@ int builtin_echo(t_minishell *msh, t_command *cmd)
|
||||
}
|
||||
if (!n_flag)
|
||||
printf("\n");
|
||||
return (SUCCESS);
|
||||
return (EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
@ -6,7 +6,7 @@
|
||||
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* Created: 2025/02/20 11:12:38 by whaffman #+# #+# */
|
||||
/* Updated: 2025/03/03 14:38:52 by whaffman ######## odam.nl */
|
||||
/* Updated: 2025/03/04 16:15:32 by whaffman ######## odam.nl */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -23,8 +23,8 @@ int builtin_router(t_minishell *msh, t_command *cmd)
|
||||
builtin_env,
|
||||
builtin_exit};
|
||||
|
||||
if (!is_builtin(cmd->args[0]))
|
||||
if (is_builtin(cmd->args[0]) < 0)
|
||||
return (FALSE);
|
||||
msh->exit_status = builtin_fn[is_builtin(cmd->args[0])](msh, cmd);
|
||||
return (TRUE);
|
||||
return (builtin_fn[is_builtin(cmd->args[0])](msh, cmd));
|
||||
|
||||
}
|
||||
|
||||
@ -6,7 +6,7 @@
|
||||
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* Created: 2025/02/20 11:03:33 by whaffman #+# #+# */
|
||||
/* Updated: 2025/02/26 14:42:49 by whaffman ######## odam.nl */
|
||||
/* Updated: 2025/03/04 15:45:38 by whaffman ######## odam.nl */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -25,5 +25,5 @@ int is_builtin(char *cmd)
|
||||
return (i);
|
||||
i++;
|
||||
}
|
||||
return (FALSE);
|
||||
return (-1);
|
||||
}
|
||||
|
||||
@ -6,7 +6,7 @@
|
||||
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* Created: 2025/02/05 16:21:39 by whaffman #+# #+# */
|
||||
/* Updated: 2025/02/26 16:08:49 by whaffman ######## odam.nl */
|
||||
/* Updated: 2025/03/04 15:43:53 by whaffman ######## odam.nl */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -16,6 +16,5 @@ void simple_builtins(t_minishell *msh)
|
||||
{
|
||||
if (msh->commands == NULL)
|
||||
return ;
|
||||
if (!builtin_router(msh, msh->commands->content))
|
||||
executor_execute_pipeline(msh);
|
||||
executor_execute_pipeline(msh);
|
||||
}
|
||||
|
||||
@ -1,55 +1,57 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* :::::::: */
|
||||
/* print_commands.c :+: :+: */
|
||||
/* +:+ */
|
||||
/* By: marvin <marvin@student.42.fr> +#+ */
|
||||
/* +#+ */
|
||||
/* Created: 2025/02/18 20:06:37 by qmennen #+# #+# */
|
||||
/* Updated: 2025/02/23 12:43:35 by Quinten ######## odam.nl */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
|
||||
void print_commands(void *param)
|
||||
{
|
||||
t_command *command;
|
||||
int i;
|
||||
|
||||
if (!DEBUG)
|
||||
return ;
|
||||
command = (t_command *)param;
|
||||
if (!command)
|
||||
return ;
|
||||
printf("command: %s\n", command->command);
|
||||
printf("-- Args: ");
|
||||
i = 0;
|
||||
while (command->args[i])
|
||||
printf("%s ", command->args[i++]);
|
||||
printf("\n");
|
||||
printf("-- Input redirects:\n");
|
||||
ft_lstiter(command->redirect_in, print_redirects);
|
||||
printf("-- Output redirects:\n");
|
||||
ft_lstiter(command->redirect_out, print_redirects);
|
||||
}
|
||||
|
||||
void print_redirects(void *param)
|
||||
{
|
||||
t_redirect *redirect;
|
||||
|
||||
redirect = (t_redirect *)param;
|
||||
if (!redirect)
|
||||
return ;
|
||||
printf(" Redirect %i value %s\n", redirect->type, redirect->value);
|
||||
}
|
||||
|
||||
void token_print(void *param)
|
||||
{
|
||||
t_token *token;
|
||||
|
||||
if (!DEBUG)
|
||||
return ;
|
||||
token = (t_token *)param;
|
||||
printf("token type %i, value %s\n", token->type, token->value);
|
||||
}
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* :::::::: */
|
||||
/* print_commands.c :+: :+: */
|
||||
/* +:+ */
|
||||
/* By: marvin <marvin@student.42.fr> +#+ */
|
||||
/* +#+ */
|
||||
/* Created: 2025/02/18 20:06:37 by qmennen #+# #+# */
|
||||
/* Updated: 2025/03/04 15:49:58 by whaffman ######## odam.nl */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
|
||||
void print_commands(void *param)
|
||||
{
|
||||
t_command *command;
|
||||
int i;
|
||||
|
||||
if (!DEBUG)
|
||||
return ;
|
||||
command = (t_command *)param;
|
||||
if (!command)
|
||||
return ;
|
||||
printf("command: %s\n", command->command);
|
||||
printf("-- Args: ");
|
||||
i = 0;
|
||||
while (command->args[i])
|
||||
printf("%s ", command->args[i++]);
|
||||
printf("\n");
|
||||
printf("-- Input redirects:\n");
|
||||
ft_lstiter(command->redirect_in, print_redirects);
|
||||
printf("-- Output redirects:\n");
|
||||
ft_lstiter(command->redirect_out, print_redirects);
|
||||
printf("-- fd_in: %i\n", command->fd_in);
|
||||
printf("-- fd_out: %i\n", command->fd_out);
|
||||
}
|
||||
|
||||
void print_redirects(void *param)
|
||||
{
|
||||
t_redirect *redirect;
|
||||
|
||||
redirect = (t_redirect *)param;
|
||||
if (!redirect)
|
||||
return ;
|
||||
printf(" Redirect %i value %s\n", redirect->type, redirect->value);
|
||||
}
|
||||
|
||||
void token_print(void *param)
|
||||
{
|
||||
t_token *token;
|
||||
|
||||
if (!DEBUG)
|
||||
return ;
|
||||
token = (t_token *)param;
|
||||
printf("token type %i, value %s\n", token->type, token->value);
|
||||
}
|
||||
|
||||
@ -6,32 +6,59 @@
|
||||
/* By: willem <willem@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* Created: 2025/02/12 21:25:02 by willem #+# #+# */
|
||||
/* Updated: 2025/02/26 17:42:46 by whaffman ######## odam.nl */
|
||||
/* Updated: 2025/03/04 16:03:29 by whaffman ######## odam.nl */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
|
||||
static int execute_builtin(t_minishell *msh, t_command *cmd)
|
||||
{
|
||||
int original_stdout;
|
||||
int original_stdin;
|
||||
int exit_status;
|
||||
|
||||
original_stdin = dup(STDIN_FILENO);
|
||||
original_stdout = dup(STDOUT_FILENO);
|
||||
dup2(cmd->fd_in, STDIN_FILENO);
|
||||
dup2(cmd->fd_out, STDOUT_FILENO);
|
||||
signal_init_child();
|
||||
exit_status = builtin_router(msh, cmd);
|
||||
signal_init_minishell();
|
||||
close(cmd->fd_in);
|
||||
close(cmd->fd_out);
|
||||
dup2(original_stdin, STDIN_FILENO);
|
||||
dup2(original_stdout, STDOUT_FILENO);
|
||||
close(original_stdin);
|
||||
close(original_stdout);
|
||||
return (exit_status);
|
||||
}
|
||||
|
||||
int executor_execute_pipeline(t_minishell *msh)
|
||||
{
|
||||
t_list *current;
|
||||
t_command *command;
|
||||
pid_t last_pid;
|
||||
int exit_status;
|
||||
|
||||
executor_create_pipes(msh);
|
||||
executor_create_redirects(msh);
|
||||
current = msh->commands;
|
||||
ft_lstiter(current, print_commands);
|
||||
last_pid = 0;
|
||||
while (current)
|
||||
{
|
||||
command = (t_command *)current->content;
|
||||
command->environment = msh->environment;
|
||||
last_pid = executor_fork(msh, command);
|
||||
if (is_builtin(((t_command *)current->content)->command) >= 0)
|
||||
msh->exit_status = execute_builtin(msh,
|
||||
(t_command *)current->content);
|
||||
else
|
||||
last_pid = executor_fork(msh, (t_command *)current->content);
|
||||
current = current->next;
|
||||
}
|
||||
waitpid(last_pid, &exit_status, 0);
|
||||
msh->exit_status = ((exit_status) & 0xff00) >> 8;
|
||||
if (last_pid != 0)
|
||||
{
|
||||
waitpid(last_pid, &exit_status, 0);
|
||||
msh->exit_status = ((exit_status) & 0xff00) >> 8;
|
||||
}
|
||||
signal_init_minishell();
|
||||
return (((exit_status) & 0xff00) >> 8);
|
||||
return (msh->exit_status);
|
||||
}
|
||||
|
||||
@ -6,7 +6,7 @@
|
||||
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* Created: 2025/02/04 16:19:22 by whaffman #+# #+# */
|
||||
/* Updated: 2025/03/03 14:25:02 by whaffman ######## odam.nl */
|
||||
/* Updated: 2025/03/04 16:08:26 by whaffman ######## odam.nl */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -23,7 +23,7 @@ static void main_loop(t_minishell *msh)
|
||||
msh->tokens = ft_parse_input(msh);
|
||||
ft_lstiter(msh->tokens, token_print);
|
||||
msh->commands = parser_get_commands(msh);
|
||||
simple_builtins(msh);
|
||||
executor_execute_pipeline(msh);
|
||||
free_minishell_line(msh);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,12 +1,12 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* :::::::: */
|
||||
/* parser_new_command.c :+: :+: */
|
||||
/* parser_alloc_command.c :+: :+: */
|
||||
/* +:+ */
|
||||
/* By: qmennen <qmennen@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* Created: 2025/02/11 16:18:21 by qmennen #+# #+# */
|
||||
/* Updated: 2025/02/26 16:14:31 by whaffman ######## odam.nl */
|
||||
/* Updated: 2025/03/04 15:41:59 by whaffman ######## odam.nl */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -22,7 +22,7 @@ t_command *parser_alloc_command(t_minishell *msh, char *cmd)
|
||||
command->fd_out = 1;
|
||||
command->redirect_in = NULL;
|
||||
command->redirect_out = NULL;
|
||||
command->environment = NULL;
|
||||
// command->environment = NULL;
|
||||
command->n_fds = 0;
|
||||
command->command = cmd;
|
||||
return (command);
|
||||
|
||||
@ -1,12 +1,12 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* parser_get_commands.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/02/11 14:06:02 by qmennen #+# #+# */
|
||||
/* Updated: 2025/02/27 16:07:54 by qmennen ### ########.fr */
|
||||
/* :::::::: */
|
||||
/* parser_get_commands.c :+: :+: */
|
||||
/* +:+ */
|
||||
/* By: qmennen <qmennen@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* Created: 2025/02/11 14:06:02 by qmennen #+# #+# */
|
||||
/* Updated: 2025/03/04 15:53:59 by whaffman ######## odam.nl */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -41,6 +41,5 @@ t_list *parser_get_commands(t_minishell *msh)
|
||||
if (current && ((t_token *)current->content)->type >= 3)
|
||||
current = current->next;
|
||||
}
|
||||
ft_lstiter(command_list, print_commands);
|
||||
return (command_list);
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user