Skip to content

Commit

Permalink
patch the last error and end of the norm
Browse files Browse the repository at this point in the history
  • Loading branch information
Reaster0 committed May 30, 2021
1 parent 5054cbc commit df5814c
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 74 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# By: earnaud <[email protected]> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2021/01/06 14:15:36 by earnaud #+# #+# #
# Updated: 2021/05/28 15:32:47 by earnaud ### ########.fr #
# Updated: 2021/05/30 23:31:26 by earnaud ### ########.fr #
# #
# **************************************************************************** #

Expand All @@ -31,6 +31,7 @@ FILES = args_tree.c \
update_shell.c \
free.c \
cmd_util.c \
cmd_util2.c \
pars_command2.c \
$(addprefix builtin/, \
cd.c \
Expand Down
15 changes: 9 additions & 6 deletions includes/minishell.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: earnaud <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/03/30 17:20:33 by vfurmane #+# #+# */
/* Updated: 2021/05/30 20:15:54 by earnaud ### ########.fr */
/* Updated: 2021/05/30 23:30:12 by earnaud ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -116,7 +116,8 @@ int ft_init_args_tree(t_config *shell_c, char *const buffer);

int ft_handle_command(t_cmd *cmd, t_config *shell_c,
int pipefd[2]);
int ft_recursiv_command(t_cmd *cmd, t_config *shell_c,int *pipe_fshell);
int ft_recursiv_command(t_cmd *cmd, t_config *shell_c,
int *pipe_fshell);

int ft_parse_envp(char **envp, t_config *shell_c);
char *ft_replace_with_env(t_config *shell_c, const char *str,
Expand All @@ -133,7 +134,8 @@ void free_shell(t_config *shell_c);
void free_neo(char **str);
void free_neo_content(char **str);
int free_all_cmd(t_cmd *cmd);
void free_neocont_neo_shell(char **args, char **line, t_config *shell_c);
void free_neocont_neo_shell(char **args, char **line,
t_config *shell_c);

int ft_arglen(t_config *shell_c, const char *cmd);

Expand All @@ -160,7 +162,8 @@ int ft_write_pipe(const char *key, const char *value1,
int ft_route_file_from(const char *file_name,
t_config *shell_c);
int ft_route_command(t_config *shell_c, char **argv,
t_cmd *cmd);
t_cmd *cmd);
void switch_case_pipe_to(t_cmd *cmd, int std_out);

char *ft_skip_spaces(const char *cmd, int *i);
int skip_quotes(const char *str, int *i);
Expand All @@ -170,11 +173,11 @@ char **ft_split_cmd(t_config *shell_c, const char *cmd);
t_cmd *ft_cmdlast(t_cmd *cmd);
void ft_cmdadd_back(t_cmd **acmd, t_cmd *new);
char *ft_skip_cmd(const char *cmd, int *i);
t_cmd *ft_new_cmd(t_config *shell_c, char *const buffer, int *error);
t_cmd *ft_new_cmd(t_config *shell_c, char *const buffer,
int *error);
char *ft_strinstr_quotes(const char *str, const char *sep);
int where_to_cut(char *const buffer);


int ft_update_env(t_config *shell_c);
int ft_del_env(t_config *shell_c, char *str);
int ft_add_env(t_config *shell_c, const char *str);
Expand Down
92 changes: 33 additions & 59 deletions srcs/cmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,57 +6,12 @@
/* By: earnaud <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/03/31 18:42:16 by vfurmane #+# #+# */
/* Updated: 2021/05/30 20:07:13 by earnaud ### ########.fr */
/* Updated: 2021/05/30 23:29:47 by earnaud ### ########.fr */
/* */
/* ************************************************************************** */

#include "minishell.h"

int where_to_cut(char *const buffer)
{
int i;

i = 0;
if (!buffer)
return (0);
while (buffer[i])
{
if (ft_strchr(";| ", buffer[i]))
return (i);
i++;
}
return (i);
}

void switch_case_pipe_to(t_cmd *cmd, int std_out)
{
if (cmd->separator == PIPE)
{
pipe(cmd->fd);
if (cmd->from_to == BRACKET_TO)
{
dup2(cmd->file_to, STDOUT_FILENO);
close(cmd->file_to);
close(cmd->fd[1]);
}
else
{
dup2(cmd->fd[1], STDOUT_FILENO);
close(cmd->fd[1]);
}
}
else
{
if (cmd->file_to)
{
dup2(cmd->file_to, STDOUT_FILENO);
close(cmd->file_to);
}
else
dup2(std_out, STDOUT_FILENO);
}
}

void switch_case_dup(t_cmd *cmd, int pipe_in, int std_out)
{
switch_case_pipe_to(cmd, std_out);
Expand All @@ -75,7 +30,8 @@ void switch_case_dup(t_cmd *cmd, int pipe_in, int std_out)

int main_process(t_cmd *cmd, t_config *shell_c, int *pipe_fshell)
{
int ret;
int ret;

switch_case_dup(cmd, pipe_fshell[0], pipe_fshell[1]);
close(cmd->file_to);
close(cmd->file_from);
Expand All @@ -89,12 +45,8 @@ int main_process(t_cmd *cmd, t_config *shell_c, int *pipe_fshell)
return (WEXITSTATUS(ret));
}

int ft_recursiv_command(t_cmd *cmd, t_config *shell_c, int *pipe_fshell)
int first_setup(t_cmd *cmd, t_config *shell_c, int *pipe_fshell, int count[0])
{
int count[4];
char **args;
t_cmd *cmd_next;

if (!cmd)
{
if (pipe_fshell[3])
Expand All @@ -105,19 +57,41 @@ int ft_recursiv_command(t_cmd *cmd, t_config *shell_c, int *pipe_fshell)
count[0] = fork();
if (count[0] == -1)
return (ft_stderr_message("could not fork: ", strerror(errno),
NULL, -1));
NULL, -1));
else if (count[0] != CHILD_PROCESS)
return (main_process(cmd, shell_c, pipe_fshell));
return (main_process(cmd, shell_c, pipe_fshell));
return (-42);
}

t_cmd *set_next_value(t_cmd *cmd, int *pipe_fshell,
int *count)
{
t_cmd *cmd_next;

switch_case_dup(cmd, pipe_fshell[0], pipe_fshell[1]);
free(cmd->str);
cmd_next = cmd->next;
count[3] = cmd->fd[0];
free(cmd);
return (cmd_next);
}

int ft_recursiv_command(t_cmd *cmd, t_config *shell_c, int *pipe_fshell)
{
int count[4];
char **args;
t_cmd *cmd_next;
int ret;

ret = first_setup(cmd, shell_c, pipe_fshell, count);
if (ret != -42)
return (ret);
args = ft_split_cmd(shell_c, cmd->str);
if (args == NULL)
return (-1);
if (args[0] != NULL)
{
switch_case_dup(cmd, pipe_fshell[0], pipe_fshell[1]);
free(cmd->str);
cmd_next = cmd->next;
count[3] = cmd->fd[0];
free(cmd);
cmd_next = set_next_value(cmd, pipe_fshell, count);
count[2] = ft_route_command(shell_c, args, cmd_next);
free_neo(args);
pipe_fshell[0] = count[3];
Expand Down
58 changes: 58 additions & 0 deletions srcs/cmd_util2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cmd_util2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: earnaud <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/05/30 23:28:01 by earnaud #+# #+# */
/* Updated: 2021/05/30 23:30:46 by earnaud ### ########.fr */
/* */
/* ************************************************************************** */

#include "minishell.h"

int where_to_cut(char *const buffer)
{
int i;

i = 0;
if (!buffer)
return (0);
while (buffer[i])
{
if (ft_strchr(";| ", buffer[i]))
return (i);
i++;
}
return (i);
}

void switch_case_pipe_to(t_cmd *cmd, int std_out)
{
if (cmd->separator == PIPE)
{
pipe(cmd->fd);
if (cmd->from_to == BRACKET_TO)
{
dup2(cmd->file_to, STDOUT_FILENO);
close(cmd->file_to);
close(cmd->fd[1]);
}
else
{
dup2(cmd->fd[1], STDOUT_FILENO);
close(cmd->fd[1]);
}
}
else
{
if (cmd->file_to)
{
dup2(cmd->file_to, STDOUT_FILENO);
close(cmd->file_to);
}
else
dup2(std_out, STDOUT_FILENO);
}
}
4 changes: 2 additions & 2 deletions srcs/free.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
/* By: earnaud <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/05/18 14:56:49 by earnaud #+# #+# */
/* Updated: 2021/05/28 11:35:19 by earnaud ### ########.fr */
/* Updated: 2021/05/30 23:02:23 by earnaud ### ########.fr */
/* */
/* ************************************************************************** */

#include "minishell.h"

int free_all_cmd(t_cmd *cmd)
int free_all_cmd(t_cmd *cmd)
{
t_cmd *temp;

Expand Down
5 changes: 3 additions & 2 deletions srcs/pars_command.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: earnaud <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/05/13 14:34:59 by earnaud #+# #+# */
/* Updated: 2021/05/28 15:32:23 by earnaud ### ########.fr */
/* Updated: 2021/05/30 22:36:51 by earnaud ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -84,7 +84,8 @@ int buffer_fix_loop(char *const buffer, char *str, int *count)
while (buffer[count[0]] && !ft_strchr(" <>|;", buffer[count[0]]))
count[0]++;
}
str[count[1]++] = buffer[count[0]++];
if (!ft_strchr(";|", buffer[count[0]]))
str[count[1]++] = buffer[count[0]++];
}
return (1);
}
Expand Down
5 changes: 3 additions & 2 deletions srcs/router.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: earnaud <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/04/01 11:52:44 by vfurmane #+# #+# */
/* Updated: 2021/05/30 20:11:47 by earnaud ### ########.fr */
/* Updated: 2021/05/30 23:02:46 by earnaud ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -59,7 +59,8 @@ static int ft_search_and_execute_file(t_config *shell_c,
return (127);
}

static int ft_search_and_execute_command(t_config *shell_c, char **argv, t_cmd *cmd)
static int ft_search_and_execute_command(t_config *shell_c,
char **argv, t_cmd *cmd)
{
if (ft_getenv(shell_c->envp_list, "PATH") == NULL)
{
Expand Down
4 changes: 2 additions & 2 deletions srcs/split_args.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: earnaud <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/05/12 09:38:43 by vfurmane #+# #+# */
/* Updated: 2021/05/27 12:03:23 by earnaud ### ########.fr */
/* Updated: 2021/05/30 20:58:58 by earnaud ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -45,7 +45,7 @@ char *ft_cmd_argdup(t_config *shell_c, const char *cmd)
if (arg.str == NULL)
return (NULL);
i = 0;
while (cmd[i] && (cmd[i] != ' ' || arg.quote != '\0'))
while (cmd[i] && ((cmd[i] != ' ' && cmd[i] != '|') || arg.quote != '\0'))
{
if (ft_set_backslash(&arg, cmd, &i))
continue ;
Expand Down

0 comments on commit df5814c

Please sign in to comment.