From d78384b4e98a19cb13300f59b877a2bac4028c9b Mon Sep 17 00:00:00 2001 From: kinou-p Date: Tue, 5 Apr 2022 02:43:05 +0200 Subject: [PATCH] Delete some printf --- includes/minishell.h | 6 +-- srcs/main.c | 10 ++--- srcs/pipe/pipe.c | 36 ++++++++++++++- srcs/set_cmd/set_cmd.c | 9 ++-- srcs/set_redirection/redirection.c | 72 +++++++++++++++++++++--------- srcs/set_redirection/utils.c | 9 ++-- 6 files changed, 101 insertions(+), 41 deletions(-) diff --git a/includes/minishell.h b/includes/minishell.h index d5a0bec..0b4e5cc 100644 --- a/includes/minishell.h +++ b/includes/minishell.h @@ -6,7 +6,7 @@ /* 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); //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); //uitls redirection int double_size(char **tab); -void print_double(char **tab); +void print_double_fd(char **tab, int fd); void free_double(char **tab); #endif \ No newline at end of file diff --git a/srcs/main.c b/srcs/main.c index 54a3f53..f0a8796 100644 --- a/srcs/main.c +++ b/srcs/main.c @@ -6,7 +6,7 @@ /* 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) { i = 0; - //ft_putstr_fd("\033[1;31m~$ \033[0m", 2); - /*input = get_next_line(0); + /*ft_putstr_fd("\033[1;31m~$ \033[0m", 0); + input = get_next_line(0); while (input[i] != '\n' && input[i]) i++; if (input[i] == '\n') @@ -45,7 +45,7 @@ void print_prompt(char **path) if (!ft_strcmp("exit", input) && input) { - dprintf(2, "exit\n"); + //dprintf(2, "exit\n"); rl_clear_history(); //free(input); exit_shell(cmd); @@ -99,7 +99,7 @@ void test(int num) void just_exit(int num) { num = 0; - printf("crtl d\n"); + printf("crtl \\n"); exit(0); } diff --git a/srcs/pipe/pipe.c b/srcs/pipe/pipe.c index a7a1cf5..2e99624 100644 --- a/srcs/pipe/pipe.c +++ b/srcs/pipe/pipe.c @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* pipe.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: apommier +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/04/02 18:51:31 by apommier #+# #+# */ +/* Updated: 2022/04/02 18:51:32 by apommier ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "../../includes/minishell.h" @@ -73,9 +85,23 @@ void execute(t_cmd *cmd, char **env) i = 0; tmpin = dup(0); tmpout= dup(1); - fdin=dup(tmpin); + 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); 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) { cmd->current_s_cmd->last = 1; @@ -92,7 +118,13 @@ void execute(t_cmd *cmd, char **env) cmd->current_s_cmd->last = 0; pipe(fdpipe); cmd->current_s_cmd->fd[0] = fdin; - cmd->current_s_cmd->fd[1] = fdpipe[1]; + 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]; fdin=fdpipe[0]; exec_cmd(cmd, env, fdpipe); close(cmd->current_s_cmd->fd[0]); diff --git a/srcs/set_cmd/set_cmd.c b/srcs/set_cmd/set_cmd.c index 38dddb0..1e64629 100644 --- a/srcs/set_cmd/set_cmd.c +++ b/srcs/set_cmd/set_cmd.c @@ -6,7 +6,7 @@ /* 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; char **split_line; @@ -29,7 +29,7 @@ t_s_cmd *set_s_cmd(char *line, t_cmd *cmd) s_cmd->outfile = 0; s_cmd->args = 0; //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, ' '); s_cmd->cmd = get_command(split_line, cmd->path); if (!s_cmd->cmd) @@ -39,7 +39,6 @@ t_s_cmd *set_s_cmd(char *line, t_cmd *cmd) free(s_cmd); return (0); } - s_cmd->nb_args = double_size(split_line); //set_redirection(s_cmd); s_cmd->args = split_line; @@ -54,7 +53,7 @@ t_cmd *split_cmd(t_cmd *cmd, char **cmds) i = 0; 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]) { printf("invalid command\n"); diff --git a/srcs/set_redirection/redirection.c b/srcs/set_redirection/redirection.c index 800be3b..b82f72e 100644 --- a/srcs/set_redirection/redirection.c +++ b/srcs/set_redirection/redirection.c @@ -6,7 +6,7 @@ /* 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++; while (str[start + i] == ' ') i++; - printf("srt= %s\n", &str[start + i]); + //printf("srt= %s\n", &str[start + i]); new = ft_strjoin(&str[start + i], 0); - 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= %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); i = 0; while (new[i] && new[i] != ' ' && new[i] != '>' && new[i] != '<') i++; @@ -65,9 +65,9 @@ char *get_word(char *str, int start, int end) return (new); } -void error_redirect(void) +void error_redirect(char *str) { - printf("ERROR: bad syntax\n"); + printf("error : %s\n", str); exit(1); } @@ -129,13 +129,11 @@ char *ft_input(char *line, t_s_cmd *cmd, int index) else cmd->in_type = 0; if (next == '<' || next == '>' || !next) - error_redirect(); + error_redirect("problem in ft_input"); line = set_input(line, cmd, i); return (line); } - - char *ft_output(char *line, t_s_cmd *cmd, int index) { int i; @@ -151,7 +149,7 @@ char *ft_output(char *line, t_s_cmd *cmd, int index) else cmd->in_type = 0; if (next == '<' || next == '>' || !next) - error_redirect(); + error_redirect("problem in ft_output"); line = set_output(line, cmd, i); return (line); } @@ -160,11 +158,12 @@ void set_file(char *file) { 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) close(fd); - if (fd == -1) - error_redirect(); } char **add_line(char **tab, char *line) @@ -179,7 +178,7 @@ char **add_line(char **tab, char *line) if (tab) size = double_size(tab); //printf("size= %d\n", size); - ret = ft_calloc(size + 2, sizeof(char*)); + ret = ft_calloc(size + 1, sizeof(char*)); if (!ret) { // if (tab) @@ -188,7 +187,7 @@ char **add_line(char **tab, char *line) } 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); i++; } @@ -199,40 +198,69 @@ char **add_line(char **tab, char *line) 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 **history; history = 0; input = 0; + //printf("-%s-\n", 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); - print_double(history); + //print_double(history); } //print_double(history); //free_double(history); free(input); free(cmd->infile); - cmd->infile = 0; + cmd->infile = set_heredoc(index, history); + //cmd->infile = 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; i = 0; - //printf("enter redirection\n"); + printf("enter redirection\n"); while (line[i]) { if(line[i] == '<') { line = ft_input(line, cmd, i); if (cmd->in_type == 1) - wait_prompt(cmd); + wait_prompt(cmd, index); i = 0; } else if(line[i] == '>') diff --git a/srcs/set_redirection/utils.c b/srcs/set_redirection/utils.c index 0d221b6..1dee7b1 100644 --- a/srcs/set_redirection/utils.c +++ b/srcs/set_redirection/utils.c @@ -6,7 +6,7 @@ /* 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; @@ -46,9 +46,10 @@ void print_double(char **tab) { 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++; } - dprintf(1 , "end double\n"); } } \ No newline at end of file