Merge remote-tracking branch 'refs/remotes/origin/main'

This commit is contained in:
gazhonsepaskwa
2025-02-05 15:51:24 +01:00
7 changed files with 109 additions and 79 deletions

View File

@@ -6,7 +6,7 @@
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/28 09:01:33 by lderidde #+# #+# */
/* Updated: 2025/02/03 14:05:25 by lderidde ### ########.fr */
/* Updated: 2025/02/05 11:50:47 by lderidde ### ########.fr */
/* */
/* ************************************************************************** */
@@ -25,7 +25,7 @@
# define EXPRT_INV "not a valid identifier"
// void echo(char *msg, int flag);
int builtin_echo(char **arg, char **envp);
int builtin_echo(t_ast_n *node, char **envp);
int builtin_exit(char **arg, bool depth, t_ast_n *node);
int builtin_pwd(char **arg);
int builtin_env(char **arg, char **envp);

View File

@@ -6,7 +6,7 @@
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/27 11:25:25 by lderidde #+# #+# */
/* Updated: 2025/02/03 13:45:35 by lderidde ### ########.fr */
/* Updated: 2025/02/05 09:55:28 by lderidde ### ########.fr */
/* */
/* ************************************************************************** */

View File

@@ -6,7 +6,7 @@
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/24 08:23:27 by lderidde #+# #+# */
/* Updated: 2025/02/04 15:55:59 by lderidde ### ########.fr */
/* Updated: 2025/02/05 13:45:47 by lderidde ### ########.fr */
/* */
/* ************************************************************************** */
@@ -48,7 +48,8 @@ typedef struct s_ast_n
int fds[2];
int _stdout;
int _stdin;
int save_std;
int save_stdo;
int save_stdi;
t_redir *redir;
char *input;
char **files;

View File

@@ -6,7 +6,7 @@
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/24 14:31:56 by lderidde #+# #+# */
/* Updated: 2025/02/03 13:13:38 by lderidde ### ########.fr */
/* Updated: 2025/02/05 12:00:26 by lderidde ### ########.fr */
/* */
/* ************************************************************************** */
@@ -64,44 +64,56 @@ int extractenv(char *str, char **envp)
return (i);
}
static void echo_print(char **arg, int j, char **envp)
int print_exit(char *arg, t_ast_n *node)
{
if (arg && ft_strncmp(arg, "$?", 2) == 0)
{
ft_fprintf(1, "%d", node->msh->ex_code);
return (1);
}
return (0);
}
static void echo_print(t_ast_n *node, int j, char **envp)
{
int i;
while (arg[j])
while (node->args[j])
{
i = 0;
while (arg[j][i])
while (node->args[j][i])
{
if (arg[j][i] == '$')
if (print_exit(node->args[j], node))
break ;
if (node->args[j][i] == '$')
{
if (!arg[j][i + 1] || arg[j][i + 1] == ' ')
ft_put_c(arg[j][i++]);
if (!node->args[j][i + 1] || node->args[j][i + 1] == ' ')
ft_put_c(node->args[j][i++]);
else
i += extractenv(&arg[j][i], envp);
i += extractenv(&node->args[j][i], envp);
}
else
ft_put_c(arg[j][i++]);
ft_put_c(node->args[j][i++]);
}
j++;
if (arg[j])
if (node->args[j])
ft_put_c(' ');
}
}
int builtin_echo(char **arg, char **envp)
int builtin_echo(t_ast_n *node, char **envp)
{
int i;
bool flag;
flag = false;
i = 1;
while (arg[i] && is_silent(arg[i]))
while (node->args[i] && is_silent(node->args[i]))
{
flag = true;
i++;
}
echo_print(arg, i, envp);
echo_print(node, i, envp);
if (!flag)
ft_printf("\n");
return (0);

View File

@@ -6,16 +6,17 @@
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/27 11:22:33 by lderidde #+# #+# */
/* Updated: 2025/02/05 09:09:56 by lderidde ### ########.fr */
/* Updated: 2025/02/05 15:39:07 by lderidde ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/minishell.h"
#include <stdlib.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>
// #include <stdlib.h>
// #include <fcntl.h>
// #include <sys/wait.h>
// #include <time.h>
// #include <unistd.h>
void handle_file(t_ast_n *node, int check, int i)
{
@@ -89,12 +90,21 @@ void here_doc(t_ast_n *node, int i)
perror("fork");
}
void save_stds(t_ast_n *node)
{
ft_fprintf(2, "save fds\nstate: %d", node->state);
node->save_stdo = dup(STDOUT_FILENO);
node->save_stdi = dup(STDIN_FILENO);
}
void handle_redir(t_ast_n *node)
{
int i;
i = -1;
while (node->redir[++i] && node->redir[i] != _NR)
if (node->state == _PLINE || node->redir[0] != _NR)
save_stds(node);
while (node->state != _PLINE && node->redir[++i] && node->redir[i] != _NR)
{
if (node->redir[i] == _RED_L)
handle_file(node, 1, i);
@@ -104,8 +114,6 @@ void handle_redir(t_ast_n *node)
handle_file(node, 3, i);
else if (node->redir[i] == _RED_DL)
here_doc(node, i);
// if (node->redir[i] != _RED_DL)
// free_null_ptr(node->input);
if (node->redir[i] == _RED_L)
{
dup2(node->_stdin, STDIN_FILENO);
@@ -140,34 +148,27 @@ int is_builtin(char *str)
return (0);
}
void reset_redir(void)
void reset_redir(t_ast_n *node)
{
int fd;
char *tty;
tty = ttyname(STDERR_FILENO);
fd = open(tty, O_WRONLY);
if (dup2(fd, STDOUT_FILENO) == -1)
printf("error\n");
close(fd);
tty = ttyname(STDERR_FILENO);
fd = open(tty, O_RDONLY);
if (dup2(fd, STDIN_FILENO) == -1)
printf("error\n");
close(fd);
if (node->state == _PLINE || node->redir[0] != _NR)
{
dup2(node->save_stdo, STDOUT_FILENO);
close(node->save_stdo);
dup2(node->save_stdi, STDIN_FILENO);
close(node->save_stdi);
}
}
int exec_builtin(t_ast_n *node)
{
int ret;
handle_redir(node);
if (ft_strncmp(node->cmd, "exit", 4) == 0)
ret = builtin_exit(node->args, false, node);
else if (ft_strncmp(node->cmd, "pwd", 3) == 0)
ret = builtin_pwd(node->args);
else if (ft_strncmp(node->cmd, "echo", 4) == 0)
ret = builtin_echo(node->args, node->msh->env);
ret = builtin_echo(node, node->msh->env);
else if (ft_strncmp(node->cmd, "env", 3) == 0)
ret = builtin_env(node->args, node->msh->env);
else if (ft_strncmp(node->cmd, "unset", 5) == 0)
@@ -229,7 +230,6 @@ int exec(t_ast_n *node)
{
char *path;
handle_redir(node);
path = find_path(node->cmd, node->msh->env);
if (!path)
return_error(node->cmd, "command not found", 127);
@@ -246,6 +246,7 @@ int exec_scmd(t_ast_n *node)
pid_t pid;
int status;
// handle_redir(node);
if (is_builtin(node->cmd))
return (exec_builtin(node));
else
@@ -279,6 +280,7 @@ void exec_pcmd(t_ast_n *pcmd)
{
int ret;
handle_redir(pcmd);
if (is_builtin(pcmd->cmd))
{
ret = exec_builtin(pcmd);
@@ -297,6 +299,8 @@ void exec_pchild(int *pipes, int index, t_ast_n *pcmd, int cmds)
dup2(pipes[1], STDOUT_FILENO);
close(pipes[0]);
close(pipes[1]);
// if (pcmd->state == _CMD)
// handle_redir(pcmd);
if (pcmd->state == _CMD)
exec_pcmd(pcmd);
else if (pcmd->state == _SUBSH)
@@ -312,7 +316,7 @@ int end_pline(pid_t last_pid, t_ast_n **pline)
waitpid(last_pid, &status, 0);
while (++i < count_cmds(pline) - 1)
wait(NULL);
reset_redir();
reset_redir(pline[0]->parent);
if (WIFEXITED(status))
return (WEXITSTATUS(status));
else
@@ -329,7 +333,8 @@ int exec_pline(t_ast_n **pline)
i = -1;
while (pline[++i])
{
/*ft_fprintf(2, "test\n");*/
if (i == 0)
handle_redir(pline[0]->parent);
pipe(pline[i]->fds);
pid = fork();
if (pid == 0)
@@ -353,6 +358,7 @@ int exec_shcmd(t_ast_n *node)
pid_t pid;
int status;
// handle_redir(node);
if (is_builtin(node->cmd))
return (exec_builtin(node));
else
@@ -378,50 +384,56 @@ int exec_subsh(t_ast_n *node);
int execute_shcommand(t_ast_n *node)
{
int status;
// int status;
if (node->state == _CMD)
return (exec_shcmd(node));
handle_redir(node);
if (node->state == _CMD)
node->msh->ex_code = exec_shcmd(node);
else if (node->state == _AND)
{
status = execute_shcommand(node->left);
if (status == 0)
return (execute_shcommand(node->right));
return (status);
node->msh->ex_code = execute_shcommand(node->left);
if (node->msh->ex_code == 0)
node->msh->ex_code = execute_shcommand(node->right);
// return (node->msh->ex_code);
}
else if (node->state == _OR)
{
status = execute_shcommand(node->left);
if (status != 0)
return (execute_shcommand(node->right));
return (status);
node->msh->ex_code = execute_shcommand(node->left);
if (node->msh->ex_code != 0)
node->msh->ex_code = execute_shcommand(node->right);
// return (node->msh->ex_code);
}
else if (node->state == _PLINE)
return (exec_pline(node->pline));
node->msh->ex_code = exec_pline(node->pline);
else if (node->state == _SUBSH)
return (exec_subsh(node->left));
return (0);
node->msh->ex_code = exec_subsh(node->left);
if (node->state == _CMD)
reset_redir(node);
return (node->msh->ex_code);
}
int exec_subsh(t_ast_n *node)
{
int status;
pid_t pid;
int ret;
pid = fork();
if (pid == 0)
{
// handle_redir(node);
exit (execute_shcommand(node));
handle_redir(node->parent);
// node->save_std = dup(STDOUT_FILENO);
ret = execute_shcommand(node);
reset_redir(node->parent);
exit(ret);
}
else if (pid > 0)
{
// reset_redir(node->parent);
waitpid(pid, &status, 0);
reset_redir();
if (WIFEXITED(status) && node->parent->state != _PLINE)
return (WEXITSTATUS(status));
else if (WIFEXITED(status) && node->parent->state == _PLINE)
exit (WEXITSTATUS(status));
else
return (1);
}
@@ -434,28 +446,31 @@ int exec_subsh(t_ast_n *node)
int execute_command(t_ast_n *node)
{
int status;
// int status;
if (node->state == _CMD)
status = exec_scmd(node);
handle_redir(node);
if (node->state == _CMD)
node->msh->ex_code = exec_scmd(node);
else if (node->state == _AND)
{
status = execute_command(node->left);
if (status == 0)
status = execute_command(node->right);
/*return (status);*/
node->msh->ex_code = execute_command(node->left);
if (node->msh->ex_code == 0)
node->msh->ex_code = execute_command(node->right);
// return (node->msh->ex_code);
}
else if (node->state == _OR)
{
status = execute_command(node->left);
if (status != 0)
status = execute_command(node->right);
/*return (status);*/
node->msh->ex_code = execute_command(node->left);
if (node->msh->ex_code != 0)
node->msh->ex_code = execute_command(node->right);
// return (node->msh->ex_code);
}
else if (node->state == _PLINE)
status = exec_pline(node->pline);
node->msh->ex_code = exec_pline(node->pline);
else if (node->state == _SUBSH)
status = exec_subsh(node->left);
reset_redir();
return (status);
node->msh->ex_code = exec_subsh(node->left);
if (node->state == _CMD)
reset_redir(node);
return (node->msh->ex_code);
}

View File

@@ -6,7 +6,7 @@
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/27 14:16:52 by lderidde #+# #+# */
/* Updated: 2025/02/05 09:09:35 by lderidde ### ########.fr */
/* Updated: 2025/02/05 13:27:18 by lderidde ### ########.fr */
/* */
/* ************************************************************************** */

View File

@@ -6,7 +6,7 @@
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/24 08:22:16 by lderidde #+# #+# */
/* Updated: 2025/02/03 15:44:56 by lderidde ### ########.fr */
/* Updated: 2025/02/05 14:04:12 by lderidde ### ########.fr */
/* */
/* ************************************************************************** */
@@ -27,6 +27,8 @@ t_ast_n *create_ast_n(t_node *lst, t_ast_n *parent, t_msh *msh)
node->parent = parent;
node->_stdout = 1;
node->_stdin = 0;
node->save_stdo = 1;
node->save_stdi = 0;
if (node->state == _AND || node->state == _OR)
create_and_or(node, lst, token, msh);
else if (node->state == _SUBSH)