Skip to content

Commit

Permalink
fix: norm
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmitry Demirkylych committed Nov 7, 2024
1 parent 79d92f3 commit d0603ed
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 113 deletions.
Binary file removed .DS_Store
Binary file not shown.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ NAME = minishell

# Comands
COMPILER = cc
DFLAGS = -g3 -gdwarf-2 -fsanitize=address -fsanitize=undefined
DFLAGS = -g3 -gdwarf-2 #-fsanitize=address -fsanitize=undefined
CFLAGS = -Wall -Wextra -Werror $(DFLAGS) -g -O0
AR = ar rcs
RM = rm -rf
Expand Down
7 changes: 3 additions & 4 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: rmikhayl <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/05 13:23:26 by rmikhayl #+# #+# */
/* Updated: 2024/09/09 13:27:44 by dmdemirk ### ########.fr */
/* Updated: 2024/11/07 19:48:38 by dmdemirk ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -18,12 +18,12 @@
#include "exit_status.h"
#include "builtins.h"

int status_handler(int status, \
int status_handler(int status, \
t_loop_data *loop_data, t_token *token_head);
void process_ast_and_io(t_ms_data *data, \
t_loop_data *loop_data, t_token *tokens_head);
void main_loop(t_ms_data *data, t_loop_data *loop_data);
int main(int argc, char **argv, char **envp);
int main(int argc, char **argv, char **envp);

int status_handler(int status, \
t_loop_data *loop_data, t_token *tokens_head)
Expand All @@ -42,7 +42,6 @@ void process_ast_and_io(t_ms_data *data, \
int status;

status = execute_ast(loop_data->tree, data);
printf("execute ast exit status -> %d\n", status);
data->exit_status = status;
set_shell_var_handler(data);
if (status_handler(status, loop_data, tokens_head))
Expand Down
63 changes: 32 additions & 31 deletions src/pipe/pipe.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: dmdemirk <dmdemirk@student.42london.c +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/17 11:04:39 by dmdemirk #+# #+# */
/* Updated: 2024/06/26 14:47:46 by dmdemirk ### ########.fr */
/* Updated: 2024/11/07 19:48:14 by dmdemirk ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -20,41 +20,42 @@

int builtin_pipe(t_ast *node, t_ms_data *data);
pid_t execute_child(t_ast *node, t_ms_data *data, \
int fd[2], int direction);
int fd[2], int direction);

/**
- @brief execute pipe when | is found in the command
-
- @param node current node in the AST
- @param data minishell structure data
- @return int return status:
- - 0: success
- - 1: error
*/

int builtin_pipe(t_ast *node, t_ms_data *data)
static int setup_pipe_processes(t_ast *node, t_ms_data *data, \
pid_t *pid_1, pid_t *pid_2)
{
int fd[2];
pid_t pid_1;
pid_t pid_2;
int status_1;
int status_2;
int fd[2];

status_1 = 0;
status_2 = 0;
pid_2 = -1;
*pid_2 = -1;
if (pipe(fd) == -1)
ft_perror("pipe");
pid_1 = execute_child(node->left, data, fd, 0);
return (ft_perror("pipe"));
*pid_1 = execute_child(node->left, data, fd, 0);
if (node->right != NULL)
pid_2 = execute_child(node->right, data, fd, 1);
*pid_2 = execute_child(node->right, data, fd, 1);
else
{
close(fd[1]);
data->std_in = fd[0];
return (WAIT_NEXT_COMMAND);
}
close_fds(fd[0], fd[1]);
return (EXIT_SUCCESS);
}

int builtin_pipe(t_ast *node, t_ms_data *data)
{
pid_t pid_1;
pid_t pid_2;
int status_1;
int status_2;
int setup_result;

status_1 = 0;
status_2 = 0;
setup_result = setup_pipe_processes(node, data, &pid_1, &pid_2);
if (setup_result == WAIT_NEXT_COMMAND)
return (WAIT_NEXT_COMMAND);
if (pid_1 > 0 && waitpid(pid_1, &status_1, 0) == -1)
return (ft_perror("waitpid"));
if (pid_2 > 0)
Expand All @@ -67,16 +68,16 @@ int builtin_pipe(t_ast *node, t_ms_data *data)
}

/**
- @brief execute child process in the pipe context
- @param node current node in the AST
- @param data minishell structure data
- @param fd file descriptors
- @param direction direction of the pipe if 0 - node_left, if 1 - node_right
- @return pid_t return the process id
- @brief execute child process in the pipe context
- @param node current node in the AST
- @param data minishell structure data
- @param fd file descriptors
- @param direction direction of the pipe if 0 - node_left, if 1 - node_right
- @return pid_t return the process id
*/

pid_t execute_child(t_ast *node, t_ms_data *data, \
int fd[2], int direction)
int fd[2], int direction)
{
pid_t pid;
int status;
Expand Down
68 changes: 30 additions & 38 deletions src/redirection/redirect_in.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: dmdemirk <dmdemirk@student.42london.c +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/11 14:32:40 by dmdemirk #+# #+# */
/* Updated: 2024/07/11 14:53:27 by dmdemirk ### ########.fr */
/* Updated: 2024/11/07 19:40:50 by dmdemirk ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -19,52 +19,44 @@

int redirect_in(t_ast *node, t_ms_data *data);

/**
- @brief redirection input in context of executing AST
-
- @param node current node in the AST
- @param data minishell structure data
- @return status:
- 0: success
- 1: error
*/
static int handle_child_process(t_ast *node, t_ms_data *data)
{
int local_fd;
int status;

if (data->std_in == -1)
{
data->std_in = open_file(node->right, "<");
if (data->std_in == -1)
return (EXIT_FAILURE);
}
else
{
local_fd = open_file(node->right, "<");
if (local_fd == -1)
return (EXIT_FAILURE);
dup2(local_fd, STDIN_FILENO);
close(local_fd);
}
if (!node->left->args[0])
{
close(data->std_in);
exit(EXIT_SUCCESS);
}
status = execute_ast(node->left, data);
exit(status);
}

int redirect_in(t_ast *node, t_ms_data *data)
{
pid_t pid;
int status;
int local_fd;
int status;

pid = fork();
if (pid == -1)
return (EXIT_FAILURE);
if (pid == 0)
{
if (data->std_in == -1)
{
data->std_in = open_file(node->right, "<");
if (data->std_in == -1)
return (EXIT_FAILURE);
}
else
{
local_fd = open_file(node->right, "<");
if (local_fd == -1)
return (EXIT_FAILURE);
dup2(local_fd, STDIN_FILENO);
close(local_fd);
}
if (!node->left->args[0])
{
close(data->std_in);
exit(EXIT_SUCCESS);
}
else
{
status = execute_ast(node->left, data);
exit(status);
}
}
handle_child_process(node, data);
waitpid(pid, &status, 0);
return (WEXITSTATUS(status));
}
44 changes: 5 additions & 39 deletions src/redirection/redirect_out.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: dmdemirk <dmdemirk@student.42london.c +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/11 14:32:59 by dmdemirk #+# #+# */
/* Updated: 2024/09/09 13:39:13 by dmdemirk ### ########.fr */
/* Updated: 2024/11/07 19:41:36 by dmdemirk ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -30,40 +30,6 @@ int redirect_out(t_ast *node, t_ms_data *data);
- 1: error
*/

/*
int redirect_out(t_ast *node, t_ms_data *data)
{
pid_t pid;
int status;
int fd;
pid = fork();
if (pid == -1)
return (1);
if (pid == 0)
{
if (data->std_out == -1)
{
data->std_out = open_file(node->right, ">");
if (data->std_out == -1)
return (1);
}
else
{
fd = open_file(node->right, ">");
if (fd == -1)
return (1);
dup2(fd, STDOUT_FILENO);
close(fd);
}
execute_ast(node->left, data);
exit(0);
}
waitpid(pid, &status, 0);
return (WEXITSTATUS(status));
}
*/

static int open_and_redirect(t_ast *node, t_ms_data *data)
{
int fd;
Expand All @@ -89,7 +55,7 @@ int redirect_out(t_ast *node, t_ms_data *data)
{
pid_t pid;
int exec_status;
int status;
int status;

pid = fork();
if (pid == -1)
Expand All @@ -102,8 +68,8 @@ int redirect_out(t_ast *node, t_ms_data *data)
exit(exec_status);
}
if (waitpid(pid, &status, 0) == -1)
return (EXIT_FAILURE);
if (WIFSIGNALED(status))
return (128 + WTERMSIG(status));
return (EXIT_FAILURE);
if (WIFSIGNALED(status))
return (128 + WTERMSIG(status));
return (WEXITSTATUS(status));
}

0 comments on commit d0603ed

Please sign in to comment.