Delete some printf

This commit is contained in:
kinou-p 2022-04-05 02:43:05 +02:00
parent cd750a160f
commit d78384b4e9
6 changed files with 101 additions and 41 deletions

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/09 22:33:49 by apommier #+# #+# */ /* Created: 2022/03/09 22:33:49 by apommier #+# #+# */
/* Updated: 2022/04/01 15:37:03 by apommier ### ########.fr */ /* Updated: 2022/04/03 19:22:42 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -77,12 +77,12 @@ void free_cmd(t_cmd *cmd);
void exit_shell(t_cmd *cmd); void exit_shell(t_cmd *cmd);
//redirection.c set redirection and input good //redirection.c set redirection and input good
char *set_redirection(t_s_cmd *cmd, char *line); char *set_redirection(t_s_cmd *cmd, char *line, int index);
char next_space(char *str, int i); char next_space(char *str, int i);
//uitls redirection //uitls redirection
int double_size(char **tab); int double_size(char **tab);
void print_double(char **tab); void print_double_fd(char **tab, int fd);
void free_double(char **tab); void free_double(char **tab);
#endif #endif

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/06 13:27:11 by apommier #+# #+# */ /* Created: 2022/03/06 13:27:11 by apommier #+# #+# */
/* Updated: 2022/04/01 17:30:02 by apommier ### ########.fr */ /* Updated: 2022/04/04 21:12:38 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -27,8 +27,8 @@ void print_prompt(char **path)
while (1) while (1)
{ {
i = 0; i = 0;
//ft_putstr_fd("\033[1;31m~$ \033[0m", 2); /*ft_putstr_fd("\033[1;31m~$ \033[0m", 0);
/*input = get_next_line(0); input = get_next_line(0);
while (input[i] != '\n' && input[i]) while (input[i] != '\n' && input[i])
i++; i++;
if (input[i] == '\n') if (input[i] == '\n')
@ -45,7 +45,7 @@ void print_prompt(char **path)
if (!ft_strcmp("exit", input) && input) if (!ft_strcmp("exit", input) && input)
{ {
dprintf(2, "exit\n"); //dprintf(2, "exit\n");
rl_clear_history(); rl_clear_history();
//free(input); //free(input);
exit_shell(cmd); exit_shell(cmd);
@ -99,7 +99,7 @@ void test(int num)
void just_exit(int num) void just_exit(int num)
{ {
num = 0; num = 0;
printf("crtl d\n"); printf("crtl \\n");
exit(0); exit(0);
} }

View File

@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* pipe.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/04/02 18:51:31 by apommier #+# #+# */
/* Updated: 2022/04/02 18:51:32 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/minishell.h" #include "../../includes/minishell.h"
@ -73,9 +85,23 @@ void execute(t_cmd *cmd, char **env)
i = 0; i = 0;
tmpin = dup(0); tmpin = dup(0);
tmpout= dup(1); tmpout= dup(1);
if (cmd->current_s_cmd->infile)
{
fdin = open(cmd->current_s_cmd->infile, O_RDWR);
if (fdin < 0)
printf("open error\n");
}
else
fdin=dup(tmpin); fdin=dup(tmpin);
while(cmd->current_s_cmd) while(cmd->current_s_cmd)
{ {
fdout = 0;
if (i > 0 && cmd->current_s_cmd->infile)
{
fdin = open(cmd->current_s_cmd->infile, O_RDWR);
if (fdin < 0)
printf("open error\n");
}
if (i == cmd->nb_s_cmd - 1) if (i == cmd->nb_s_cmd - 1)
{ {
cmd->current_s_cmd->last = 1; cmd->current_s_cmd->last = 1;
@ -92,6 +118,12 @@ void execute(t_cmd *cmd, char **env)
cmd->current_s_cmd->last = 0; cmd->current_s_cmd->last = 0;
pipe(fdpipe); pipe(fdpipe);
cmd->current_s_cmd->fd[0] = fdin; cmd->current_s_cmd->fd[0] = fdin;
if (cmd->current_s_cmd->outfile)
{
fdout = open(cmd->current_s_cmd->outfile, O_RDWR | O_CREAT | O_APPEND, 0666);
cmd->current_s_cmd->fd[1] = fdout;
}
else
cmd->current_s_cmd->fd[1] = fdpipe[1]; cmd->current_s_cmd->fd[1] = fdpipe[1];
fdin=fdpipe[0]; fdin=fdpipe[0];
exec_cmd(cmd, env, fdpipe); exec_cmd(cmd, env, fdpipe);

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/08 15:19:42 by apommier #+# #+# */ /* Created: 2022/03/08 15:19:42 by apommier #+# #+# */
/* Updated: 2022/03/31 16:31:40 by apommier ### ########.fr */ /* Updated: 2022/04/03 18:19:55 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -14,7 +14,7 @@
t_s_cmd *set_s_cmd(char *line, t_cmd *cmd) t_s_cmd *set_s_cmd(char *line, t_cmd *cmd, int index)
{ {
t_s_cmd *s_cmd; t_s_cmd *s_cmd;
char **split_line; char **split_line;
@ -29,7 +29,7 @@ t_s_cmd *set_s_cmd(char *line, t_cmd *cmd)
s_cmd->outfile = 0; s_cmd->outfile = 0;
s_cmd->args = 0; s_cmd->args = 0;
//printf("before redirect\n"); //printf("before redirect\n");
line = set_redirection(s_cmd, line);//SET REDIRECTION line = set_redirection(s_cmd, line, index);//SET REDIRECTION
split_line = ft_split(line, ' '); split_line = ft_split(line, ' ');
s_cmd->cmd = get_command(split_line, cmd->path); s_cmd->cmd = get_command(split_line, cmd->path);
if (!s_cmd->cmd) if (!s_cmd->cmd)
@ -39,7 +39,6 @@ t_s_cmd *set_s_cmd(char *line, t_cmd *cmd)
free(s_cmd); free(s_cmd);
return (0); return (0);
} }
s_cmd->nb_args = double_size(split_line); s_cmd->nb_args = double_size(split_line);
//set_redirection(s_cmd); //set_redirection(s_cmd);
s_cmd->args = split_line; s_cmd->args = split_line;
@ -54,7 +53,7 @@ t_cmd *split_cmd(t_cmd *cmd, char **cmds)
i = 0; i = 0;
while (cmds[i]) while (cmds[i])
{ {
cmd->s_cmds[i] = set_s_cmd(cmds[i], cmd); cmd->s_cmds[i] = set_s_cmd(cmds[i], cmd, i);
if (!cmd->s_cmds[i]) if (!cmd->s_cmds[i])
{ {
printf("invalid command\n"); printf("invalid command\n");

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/09 15:18:58 by apommier #+# #+# */ /* Created: 2022/03/09 15:18:58 by apommier #+# #+# */
/* Updated: 2022/04/01 17:36:46 by apommier ### ########.fr */ /* Updated: 2022/04/03 21:59:51 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -49,10 +49,10 @@ char *get_word(char *str, int start, int end)
i++; i++;
while (str[start + i] == ' ') while (str[start + i] == ' ')
i++; i++;
printf("srt= %s\n", &str[start + i]); //printf("srt= %s\n", &str[start + i]);
new = ft_strjoin(&str[start + i], 0); new = ft_strjoin(&str[start + i], 0);
printf("char= %c\n",new[end - start - i]); //printf("char= %c\n",new[end - start - i]);
printf("char str= %s end=%d start=%d out=%d\n", &new[0], end, start, end - start - i - 1); //printf("char str= %s end=%d start=%d out=%d\n", &new[0], end, start, end - start - i - 1);
i = 0; i = 0;
while (new[i] && new[i] != ' ' && new[i] != '>' && new[i] != '<') while (new[i] && new[i] != ' ' && new[i] != '>' && new[i] != '<')
i++; i++;
@ -65,9 +65,9 @@ char *get_word(char *str, int start, int end)
return (new); return (new);
} }
void error_redirect(void) void error_redirect(char *str)
{ {
printf("ERROR: bad syntax\n"); printf("error : %s\n", str);
exit(1); exit(1);
} }
@ -129,13 +129,11 @@ char *ft_input(char *line, t_s_cmd *cmd, int index)
else else
cmd->in_type = 0; cmd->in_type = 0;
if (next == '<' || next == '>' || !next) if (next == '<' || next == '>' || !next)
error_redirect(); error_redirect("problem in ft_input");
line = set_input(line, cmd, i); line = set_input(line, cmd, i);
return (line); return (line);
} }
char *ft_output(char *line, t_s_cmd *cmd, int index) char *ft_output(char *line, t_s_cmd *cmd, int index)
{ {
int i; int i;
@ -151,7 +149,7 @@ char *ft_output(char *line, t_s_cmd *cmd, int index)
else else
cmd->in_type = 0; cmd->in_type = 0;
if (next == '<' || next == '>' || !next) if (next == '<' || next == '>' || !next)
error_redirect(); error_redirect("problem in ft_output");
line = set_output(line, cmd, i); line = set_output(line, cmd, i);
return (line); return (line);
} }
@ -160,11 +158,12 @@ void set_file(char *file)
{ {
int fd; int fd;
fd = open(file, O_CREAT | O_TRUNC); printf("setfile= %s\n", file);
fd = open(file, O_TRUNC | O_CREAT, 0644);
if (fd == -1)
error_redirect("can't set file");
if (fd) if (fd)
close(fd); close(fd);
if (fd == -1)
error_redirect();
} }
char **add_line(char **tab, char *line) char **add_line(char **tab, char *line)
@ -179,7 +178,7 @@ char **add_line(char **tab, char *line)
if (tab) if (tab)
size = double_size(tab); size = double_size(tab);
//printf("size= %d\n", size); //printf("size= %d\n", size);
ret = ft_calloc(size + 2, sizeof(char*)); ret = ft_calloc(size + 1, sizeof(char*));
if (!ret) if (!ret)
{ {
// if (tab) // if (tab)
@ -188,7 +187,7 @@ char **add_line(char **tab, char *line)
} }
while (tab && tab[i]) while (tab && tab[i])
{ {
printf("tab[i]= %s\n", tab[i]); //printf("tab[i]= %s\n", tab[i]);
ret[i] = ft_strjoin(tab[i], 0); ret[i] = ft_strjoin(tab[i], 0);
i++; i++;
} }
@ -199,40 +198,69 @@ char **add_line(char **tab, char *line)
return(ret); return(ret);
} }
void wait_prompt(t_s_cmd *cmd) char *set_heredoc(int index, char **in)
{
char *nbr_file;
char *file_name;
int fd;
int i;
i = 0;
//print_double_fd(in, 1);
if (index)
{
nbr_file = ft_itoa(index + 1);
file_name = ft_strjoin(".heredoc", nbr_file);
}
else
file_name = ft_strjoin(".heredoc", 0);
fd = open(file_name, O_RDWR | O_CREAT | O_TRUNC, 0666);
if (fd > 0)
print_double_fd(in, fd);
close(fd);
return (file_name);
}
void wait_prompt(t_s_cmd *cmd, int index)
{ {
char *input; char *input;
char **history; char **history;
history = 0; history = 0;
input = 0; input = 0;
//printf("-%s-\n", cmd->infile);
while (ft_strcmp(input, cmd->infile)) while (ft_strcmp(input, cmd->infile))
{ {
input = readline("> "); ft_putstr_fd("> ", 0);
input = get_next_line(0);
//printf("input = -%s-\n", input);
input[ft_strlen(input) - 1] = 0;
//printf("input2 = -%s-\n", input);
history = add_line(history, input); history = add_line(history, input);
print_double(history); //print_double(history);
} }
//print_double(history); //print_double(history);
//free_double(history); //free_double(history);
free(input); free(input);
free(cmd->infile); free(cmd->infile);
cmd->infile = 0; cmd->infile = set_heredoc(index, history);
//cmd->infile = 0;
cmd->in_type = 0; cmd->in_type = 0;
} }
char *set_redirection(t_s_cmd *cmd, char *line) char *set_redirection(t_s_cmd *cmd, char *line, int index)
{ {
int i; int i;
i = 0; i = 0;
//printf("enter redirection\n"); printf("enter redirection\n");
while (line[i]) while (line[i])
{ {
if(line[i] == '<') if(line[i] == '<')
{ {
line = ft_input(line, cmd, i); line = ft_input(line, cmd, i);
if (cmd->in_type == 1) if (cmd->in_type == 1)
wait_prompt(cmd); wait_prompt(cmd, index);
i = 0; i = 0;
} }
else if(line[i] == '>') else if(line[i] == '>')

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/11 16:35:37 by apommier #+# #+# */ /* Created: 2022/03/11 16:35:37 by apommier #+# #+# */
/* Updated: 2022/03/11 23:28:49 by apommier ### ########.fr */ /* Updated: 2022/04/03 20:20:14 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -37,7 +37,7 @@ void free_double(char **tab)
} }
} }
void print_double(char **tab) void print_double_fd(char **tab, int fd)
{ {
int i; int i;
@ -46,9 +46,10 @@ void print_double(char **tab)
{ {
while (tab[i]) while (tab[i])
{ {
dprintf(1 , "%d -%s-\n", i, tab[i]); ft_putstr_fd(tab[i], fd);
if (tab[i + 1])
ft_putstr_fd("\n", fd);
i++; i++;
} }
dprintf(1 , "end double\n");
} }
} }