Compare commits

...

2 Commits

Author SHA1 Message Date
Camille Chauvet
f5940ee99e fix: waitpid infinite loop 2023-03-13 19:29:20 +01:00
Camille Chauvet
ee3d535393 fix: quote verif is better 2023-03-13 19:28:46 +01:00
5 changed files with 49 additions and 25 deletions

View File

@ -11,7 +11,6 @@
/* ************************************************************************** */ /* ************************************************************************** */
#include "execution_private.h" #include "execution_private.h"
#include <unistd.h>
static int ft_own_cmd(t_data *data, t_cmd *cmd) static int ft_own_cmd(t_data *data, t_cmd *cmd)
{ {
@ -42,7 +41,7 @@ static int ft_own_cmd(t_data *data, t_cmd *cmd)
return (return_code); return (return_code);
} }
static bool ft_executor(t_cmd *cmd, char **env) static bool ft_executor(t_cmd *cmd, char **env, int fd)
{ {
if (cmd->fd_in[0] == -1 || cmd->fd_out[0] == -1 || cmd->executable == NULL) if (cmd->fd_in[0] == -1 || cmd->fd_out[0] == -1 || cmd->executable == NULL)
return (0); return (0);
@ -51,12 +50,14 @@ static bool ft_executor(t_cmd *cmd, char **env)
return (1); return (1);
if (cmd->pid == 0) if (cmd->pid == 0)
{ {
if (cmd->fd_in[1] != -1) /* if (cmd->fd_in[1] != -1) */
dup2(cmd->fd_in[1], 0); /* dup2(cmd->fd_in[1], 0); */
if (cmd->fd_out[1] != -1) /* if (cmd->fd_out[1] != -1) */
dup2(cmd->fd_out[1], 1); /* dup2(cmd->fd_out[1], 1); */
dup2(cmd->fd_in[0], 0); dup2(cmd->fd_in[0], 0);
dup2(cmd->fd_out[0], 1); dup2(cmd->fd_out[0], 1);
if (fd != -1)
close(fd);
ft_closer(cmd->fd_in); ft_closer(cmd->fd_in);
ft_closer(cmd->fd_out); ft_closer(cmd->fd_out);
execve(cmd->executable, cmd->args, env); execve(cmd->executable, cmd->args, env);
@ -66,7 +67,7 @@ static bool ft_executor(t_cmd *cmd, char **env)
return (0); return (0);
} }
static int ft_cmd_executor(t_data *data, t_cmd *cmd) static int ft_cmd_executor(t_data *data, t_cmd *cmd, int fd)
{ {
int exit_code; int exit_code;
char **env; char **env;
@ -74,6 +75,8 @@ static int ft_cmd_executor(t_data *data, t_cmd *cmd)
if (cmd->own_cmd == 1) if (cmd->own_cmd == 1)
{ {
exit_code = ft_own_cmd(data, cmd); exit_code = ft_own_cmd(data, cmd);
ft_closer(cmd->fd_in);
ft_closer(cmd->fd_out);
if (exit_code == -1) if (exit_code == -1)
return (1); return (1);
} }
@ -82,7 +85,9 @@ static int ft_cmd_executor(t_data *data, t_cmd *cmd)
env = env_to_strs(data->env); env = env_to_strs(data->env);
if (env == NULL) if (env == NULL)
return (1); return (1);
exit_code = ft_executor(cmd, env); exit_code = ft_executor(cmd, env, fd);
ft_closer(cmd->fd_in);
ft_closer(cmd->fd_out);
ft_freer_tab_ultimate(1, env); ft_freer_tab_ultimate(1, env);
if (exit_code == 1) if (exit_code == 1)
return (1); return (1);
@ -102,6 +107,8 @@ int ft_cmds_executor(t_data *data)
while (current != NULL) while (current != NULL)
{ {
content = current->content; content = current->content;
fds[0] = -1;
fds[1] = -1;
if (current->next != NULL) if (current->next != NULL)
{ {
if (pipe(fds) == -1) if (pipe(fds) == -1)
@ -109,10 +116,8 @@ int ft_cmds_executor(t_data *data)
ft_add_fd(content->fd_out, fds[1]); ft_add_fd(content->fd_out, fds[1]);
ft_add_fd(((t_cmd *) (current->next->content))->fd_in, fds[0]); ft_add_fd(((t_cmd *) (current->next->content))->fd_in, fds[0]);
} }
if (ft_cmd_executor(data, content)) if (ft_cmd_executor(data, content, fds[0]))
return (1); return (1);
ft_closer(content->fd_in);
ft_closer(content->fd_out);
current = current->next; current = current->next;
} }
return (0); return (0);

View File

@ -24,8 +24,10 @@ static char *ft_spacer_after(const char *str)
i = 1; i = 1;
while (out[i] != '\0') while (out[i] != '\0')
{ {
while (ft_is_in_quote(out, i)) while (ft_is_in_quote(out, i - 1))
i++; i++;
if (out[i] == '\0')
break ;
if (ft_is_in("><|", out[i - 1])) if (ft_is_in("><|", out[i - 1]))
{ {
while (out[i] == out[i - 1]) while (out[i] == out[i - 1])
@ -56,6 +58,8 @@ static char *ft_spacer_before(const char *str)
{ {
while (ft_is_in_quote(out, i)) while (ft_is_in_quote(out, i))
i++; i++;
if (out[i] == '\0')
break ;
if (ft_is_in("><|", out[i + 1])) if (ft_is_in("><|", out[i + 1]))
{ {
while (out[i] == ' ') while (out[i] == ' ')
@ -83,6 +87,8 @@ static void ft_space_simplifier(char *str)
{ {
if (ft_is_in_quote(str, i)) if (ft_is_in_quote(str, i))
i++; i++;
if (str[i] != '\0')
break ;
y = 0; y = 0;
while (str[y + i] == ' ') while (str[y + i] == ' ')
y++; y++;

View File

@ -84,7 +84,7 @@ static int ft_special_char_dub(const char *str)
} }
i = i + y; i = i + y;
} }
else else if (str[i] != '\0')
i++; i++;
} }
return (0); return (0);

View File

@ -1,11 +1,18 @@
#include "./utils.h" #include "./utils.h"
#include <stdio.h>
void ft_closer(int fds[2]) void ft_closer(int fds[2])
{ {
if (fds[0] > 2) if (fds[0] > 2)
{
//dprintf(2, "close(%d)\n", fds[0]);
close(fds[0]); close(fds[0]);
}
if (fds[1] > 2) if (fds[1] > 2)
{
//dprintf(2, "close(%d)\n", fds[1]);
close(fds[1]); close(fds[1]);
}
} }
void ft_add_fd(int fds[2], int fd) void ft_add_fd(int fds[2], int fd)

View File

@ -14,26 +14,32 @@
int ft_is_in_quote(const char *str, size_t n) int ft_is_in_quote(const char *str, size_t n)
{ {
size_t double_quoted;
size_t simple_quoted;
size_t i; size_t i;
double_quoted = 0;
simple_quoted = 0;
i = 0; i = 0;
while (str[i] != '\0' && i < n) while (str[i] != '\0' && i < n)
{ {
if (str[i] == '\'')
{
i++;
while (str[i] != '\'' && str[i] != '\0')
{
if (i == n)
return (1);
i++;
}
}
if (str[i] == '"') if (str[i] == '"')
{ {
if (simple_quoted == 0) i++;
double_quoted = !double_quoted; while (str[i] != '"' && str[i] != '\0')
} {
else if (str[i] == '\'') if (i == n)
{ return (2);
if (double_quoted == 0) i++;
simple_quoted = !simple_quoted; }
} }
i++; i++;
} }
return (simple_quoted + double_quoted * 2); return (0);
} }