/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* p_redirs_heredoc.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: rbousset +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ /* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ /* */ /* ************************************************************************** */ #include #include #include #include "d_define.h" #include "m_prompt.h" #include "s_struct.h" static void p_skip_tabs(char *line[], const int8_t redir) { char *ptr; if (redir != -3) return ; ptr = *line; while (ptr[0] != C_NUL && ptr[0] == C_HT) { (void)ft_memmove(ptr, ptr + 1, (ft_strlen(ptr) + 1) * sizeof(char)); } } static void p_append_line(char *heredoc[], const char line[]) { char *ptr; ptr = *heredoc; ft_strlcpy(ptr + ft_strlen(ptr), line, ft_strlen(line) + 1); ptr[ft_strlen(ptr) + 1] = C_NUL; ptr[ft_strlen(ptr)] = C_LF; } char *p_get_heredoc(const char path[], const int8_t redir, t_msh *msh) { char *heredoc; char *line; int8_t gnl; if ((heredoc = ft_strdup("")) == NULL) return (NULL); gnl = 1; while (gnl > 0) { if (msh->fd == STDIN_FILENO) m_prompt_psx(2, msh); gnl = get_next_line(msh->fd, &line); p_skip_tabs(&line, redir); if (ft_strncmp(path, line, ft_strlen(path) + 1) == 0) break ; if ((heredoc = ft_nrealloc(heredoc, ft_strlen(heredoc) + 1, ft_strlen(heredoc) + ft_strlen(line) + 2)) == NULL) break ; p_append_line(&heredoc, line); ft_memdel((void*)&line); } if (gnl > 0) ft_memdel((void*)&line); return (heredoc); }