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> +#+ #
|
# By: qmennen <qmennen@student.codam.nl> +#+ #
|
||||||
# +#+ #
|
# +#+ #
|
||||||
# Created: 2024/10/15 11:48:46 by whaffman #+# #+# #
|
# 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
|
NAME = minishell
|
||||||
# FLAGS=-DNOCOLORS -DNOBANNER -DNOPROMPT
|
FLAGS=-DNOCOLORS -DNOBANNER -DNOPROMPT
|
||||||
|
|
||||||
SRC_PATH = src
|
SRC_PATH = src
|
||||||
INC_PATH = inc
|
INC_PATH = inc
|
||||||
|
|||||||
@ -6,7 +6,7 @@
|
|||||||
/* By: qmennen <qmennen@student.codam.nl> +#+ */
|
/* By: qmennen <qmennen@student.codam.nl> +#+ */
|
||||||
/* +#+ */
|
/* +#+ */
|
||||||
/* Created: 2025/02/05 12:36:08 by whaffman #+# #+# */
|
/* 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 *command;
|
||||||
char **args;
|
char **args;
|
||||||
t_list *environment;
|
// t_list *environment;
|
||||||
t_list *redirect_in;
|
t_list *redirect_in;
|
||||||
t_list *redirect_out;
|
t_list *redirect_out;
|
||||||
int fd_in;
|
int fd_in;
|
||||||
|
|||||||
@ -6,7 +6,7 @@
|
|||||||
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||||
/* +#+ */
|
/* +#+ */
|
||||||
/* Created: 2025/02/20 11:33:05 by whaffman #+# #+# */
|
/* 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)
|
if (!n_flag)
|
||||||
printf("\n");
|
printf("\n");
|
||||||
return (SUCCESS);
|
return (EXIT_SUCCESS);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -6,7 +6,7 @@
|
|||||||
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||||
/* +#+ */
|
/* +#+ */
|
||||||
/* Created: 2025/02/20 11:12:38 by whaffman #+# #+# */
|
/* 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_env,
|
||||||
builtin_exit};
|
builtin_exit};
|
||||||
|
|
||||||
if (!is_builtin(cmd->args[0]))
|
if (is_builtin(cmd->args[0]) < 0)
|
||||||
return (FALSE);
|
return (FALSE);
|
||||||
msh->exit_status = builtin_fn[is_builtin(cmd->args[0])](msh, cmd);
|
return (builtin_fn[is_builtin(cmd->args[0])](msh, cmd));
|
||||||
return (TRUE);
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -6,7 +6,7 @@
|
|||||||
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||||
/* +#+ */
|
/* +#+ */
|
||||||
/* Created: 2025/02/20 11:03:33 by whaffman #+# #+# */
|
/* 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);
|
return (i);
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
return (FALSE);
|
return (-1);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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/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)
|
if (msh->commands == NULL)
|
||||||
return ;
|
return ;
|
||||||
if (!builtin_router(msh, msh->commands->content))
|
executor_execute_pipeline(msh);
|
||||||
executor_execute_pipeline(msh);
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,55 +1,57 @@
|
|||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
/* */
|
/* */
|
||||||
/* :::::::: */
|
/* :::::::: */
|
||||||
/* print_commands.c :+: :+: */
|
/* print_commands.c :+: :+: */
|
||||||
/* +:+ */
|
/* +:+ */
|
||||||
/* By: marvin <marvin@student.42.fr> +#+ */
|
/* By: marvin <marvin@student.42.fr> +#+ */
|
||||||
/* +#+ */
|
/* +#+ */
|
||||||
/* Created: 2025/02/18 20:06:37 by qmennen #+# #+# */
|
/* Created: 2025/02/18 20:06:37 by qmennen #+# #+# */
|
||||||
/* Updated: 2025/02/23 12:43:35 by Quinten ######## odam.nl */
|
/* Updated: 2025/03/04 15:49:58 by whaffman ######## odam.nl */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "minishell.h"
|
#include "minishell.h"
|
||||||
|
|
||||||
void print_commands(void *param)
|
void print_commands(void *param)
|
||||||
{
|
{
|
||||||
t_command *command;
|
t_command *command;
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
if (!DEBUG)
|
if (!DEBUG)
|
||||||
return ;
|
return ;
|
||||||
command = (t_command *)param;
|
command = (t_command *)param;
|
||||||
if (!command)
|
if (!command)
|
||||||
return ;
|
return ;
|
||||||
printf("command: %s\n", command->command);
|
printf("command: %s\n", command->command);
|
||||||
printf("-- Args: ");
|
printf("-- Args: ");
|
||||||
i = 0;
|
i = 0;
|
||||||
while (command->args[i])
|
while (command->args[i])
|
||||||
printf("%s ", command->args[i++]);
|
printf("%s ", command->args[i++]);
|
||||||
printf("\n");
|
printf("\n");
|
||||||
printf("-- Input redirects:\n");
|
printf("-- Input redirects:\n");
|
||||||
ft_lstiter(command->redirect_in, print_redirects);
|
ft_lstiter(command->redirect_in, print_redirects);
|
||||||
printf("-- Output redirects:\n");
|
printf("-- Output redirects:\n");
|
||||||
ft_lstiter(command->redirect_out, print_redirects);
|
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;
|
void print_redirects(void *param)
|
||||||
|
{
|
||||||
redirect = (t_redirect *)param;
|
t_redirect *redirect;
|
||||||
if (!redirect)
|
|
||||||
return ;
|
redirect = (t_redirect *)param;
|
||||||
printf(" Redirect %i value %s\n", redirect->type, redirect->value);
|
if (!redirect)
|
||||||
}
|
return ;
|
||||||
|
printf(" Redirect %i value %s\n", redirect->type, redirect->value);
|
||||||
void token_print(void *param)
|
}
|
||||||
{
|
|
||||||
t_token *token;
|
void token_print(void *param)
|
||||||
|
{
|
||||||
if (!DEBUG)
|
t_token *token;
|
||||||
return ;
|
|
||||||
token = (t_token *)param;
|
if (!DEBUG)
|
||||||
printf("token type %i, value %s\n", token->type, token->value);
|
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> +#+ */
|
/* By: willem <willem@student.codam.nl> +#+ */
|
||||||
/* +#+ */
|
/* +#+ */
|
||||||
/* Created: 2025/02/12 21:25:02 by willem #+# #+# */
|
/* 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"
|
#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)
|
int executor_execute_pipeline(t_minishell *msh)
|
||||||
{
|
{
|
||||||
t_list *current;
|
t_list *current;
|
||||||
t_command *command;
|
|
||||||
pid_t last_pid;
|
pid_t last_pid;
|
||||||
int exit_status;
|
int exit_status;
|
||||||
|
|
||||||
executor_create_pipes(msh);
|
executor_create_pipes(msh);
|
||||||
executor_create_redirects(msh);
|
executor_create_redirects(msh);
|
||||||
current = msh->commands;
|
current = msh->commands;
|
||||||
|
ft_lstiter(current, print_commands);
|
||||||
last_pid = 0;
|
last_pid = 0;
|
||||||
while (current)
|
while (current)
|
||||||
{
|
{
|
||||||
command = (t_command *)current->content;
|
if (is_builtin(((t_command *)current->content)->command) >= 0)
|
||||||
command->environment = msh->environment;
|
msh->exit_status = execute_builtin(msh,
|
||||||
last_pid = executor_fork(msh, command);
|
(t_command *)current->content);
|
||||||
|
else
|
||||||
|
last_pid = executor_fork(msh, (t_command *)current->content);
|
||||||
current = current->next;
|
current = current->next;
|
||||||
}
|
}
|
||||||
waitpid(last_pid, &exit_status, 0);
|
if (last_pid != 0)
|
||||||
msh->exit_status = ((exit_status) & 0xff00) >> 8;
|
{
|
||||||
|
waitpid(last_pid, &exit_status, 0);
|
||||||
|
msh->exit_status = ((exit_status) & 0xff00) >> 8;
|
||||||
|
}
|
||||||
signal_init_minishell();
|
signal_init_minishell();
|
||||||
return (((exit_status) & 0xff00) >> 8);
|
return (msh->exit_status);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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/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);
|
msh->tokens = ft_parse_input(msh);
|
||||||
ft_lstiter(msh->tokens, token_print);
|
ft_lstiter(msh->tokens, token_print);
|
||||||
msh->commands = parser_get_commands(msh);
|
msh->commands = parser_get_commands(msh);
|
||||||
simple_builtins(msh);
|
executor_execute_pipeline(msh);
|
||||||
free_minishell_line(msh);
|
free_minishell_line(msh);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,12 +1,12 @@
|
|||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
/* */
|
/* */
|
||||||
/* :::::::: */
|
/* :::::::: */
|
||||||
/* parser_new_command.c :+: :+: */
|
/* parser_alloc_command.c :+: :+: */
|
||||||
/* +:+ */
|
/* +:+ */
|
||||||
/* By: qmennen <qmennen@student.codam.nl> +#+ */
|
/* By: qmennen <qmennen@student.codam.nl> +#+ */
|
||||||
/* +#+ */
|
/* +#+ */
|
||||||
/* Created: 2025/02/11 16:18:21 by qmennen #+# #+# */
|
/* 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->fd_out = 1;
|
||||||
command->redirect_in = NULL;
|
command->redirect_in = NULL;
|
||||||
command->redirect_out = NULL;
|
command->redirect_out = NULL;
|
||||||
command->environment = NULL;
|
// command->environment = NULL;
|
||||||
command->n_fds = 0;
|
command->n_fds = 0;
|
||||||
command->command = cmd;
|
command->command = cmd;
|
||||||
return (command);
|
return (command);
|
||||||
|
|||||||
@ -1,12 +1,12 @@
|
|||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
/* */
|
/* */
|
||||||
/* ::: :::::::: */
|
/* :::::::: */
|
||||||
/* 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/27 16:07:54 by qmennen ### ########.fr */
|
/* 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)
|
if (current && ((t_token *)current->content)->type >= 3)
|
||||||
current = current->next;
|
current = current->next;
|
||||||
}
|
}
|
||||||
ft_lstiter(command_list, print_commands);
|
|
||||||
return (command_list);
|
return (command_list);
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user