/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* m_loop_next.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_loop_next.h" #include "m_prompt.h" #include "s_struct.h" static char *m_counter_line_backslash(int32_t fd, uint8_t psx, char *line, t_msh *msh) { char *counter_line; if (fd == STDIN_FILENO) m_prompt_psx(psx, msh); get_next_line(fd, &counter_line); if (counter_line[0] != C_NULL) { line = ft_nrealloc(line, ft_strlen(line) + 1, ft_strlen(line) + ft_strlen(counter_line)); ft_memcpy(line + ft_strlen(line) - 1, counter_line, ft_strlen(counter_line) + 1); } ft_memdel((void*)&counter_line); return (line); } static char *m_counter_line_pipes(int32_t fd, uint8_t psx, char *line, t_msh *msh) { char *counter_line; if (fd == STDIN_FILENO) m_prompt_psx(psx, msh); get_next_line(fd, &counter_line); if (counter_line[0] != C_NULL) { line = ft_nrealloc(line, ft_strlen(line) + 1, ft_strlen(line) + ft_strlen(counter_line) + 1); ft_memcpy(line + ft_strlen(line), counter_line, ft_strlen(counter_line) + 1); } ft_memdel((void*)&counter_line); return (line); } static char *m_counter_line_quotes(int32_t fd, uint8_t psx, char *line, t_msh *msh) { char *counter_line; if (fd == STDIN_FILENO) m_prompt_psx(psx, msh); get_next_line(fd, &counter_line); if (counter_line[0] != C_NULL) { line = ft_nrealloc(line, ft_strlen(line) + 1, ft_strlen(line) + ft_strlen(counter_line) + 2); line[ft_strlen(line) + 1] = '\0'; line[ft_strlen(line)] = '\n'; ft_memcpy(line + ft_strlen(line), counter_line, ft_strlen(counter_line) + 1); } ft_memdel((void*)&counter_line); return (line); } char *m_check_multi_backslash(int32_t fd, char line[], t_msh *msh) { if (line[ft_strlen(line) - 1] == '\\') { line = m_counter_line_backslash(fd, 2, line, msh); line = m_check_multi_backslash(fd, line, msh); } return (line); } char *m_check_multi_pipe(int32_t fd, char line[], t_msh *msh) { char *pipe; if ((pipe = ft_strrchr(line, '|')) != NULL) { pipe += 1; while (*pipe != '\0') { if (ft_iswhitespace(*pipe) == FALSE) { return (line); } pipe++; } line = m_counter_line_pipes(fd, 2, line, msh); line = m_check_multi_backslash(fd, line, msh); line = m_check_multi_pipe(fd, line, msh); } return (line); } char *m_check_multi_and(int32_t fd, char line[], t_msh *msh) { char *and; if ((and = ft_strrchr(line, '&')) != NULL && *(and - 1) == '&') { and += 1; while (*and != '\0') { if (ft_iswhitespace(*and) == FALSE) { return (line); } and++; } line = m_counter_line_pipes(fd, 2, line, msh); line = m_check_multi_backslash(fd, line, msh); line = m_check_multi_pipe(fd, line, msh); line = m_check_multi_and(fd, line, msh); } return (line); } static t_bool m_check_missing_quotes(char line[]) { char *p; uint8_t mode; t_bool ret; ret = FALSE; p = line; mode = M_NO_MODE; while (*p != C_NULL) { if (*p == C_SQUOTE || *p == C_DQUOTE) { if (((p - line) == 0) || ((p - line) == 1 && *(p - 1) != C_BS) || ((p - line) > 1 && *(p - 1) == C_BS && *(p - 2) == C_BS)) mode = (*p == C_SQUOTE) ? (M_SQ_MODE) : (M_DQ_MODE); } p++; } return (ret); } char *m_check_multi_quotes(int32_t fd, char line[], t_msh *msh) { t_bool reparse; reparse = FALSE; reparse = m_check_missing_quotes(line); if (reparse == TRUE) { line = m_counter_line_quotes(fd, 2, line, msh); line = m_check_multi_backslash(fd, line, msh); line = m_check_multi_pipe(fd, line, msh); line = m_check_multi_and(fd, line, msh); line = m_check_multi_quotes(fd, line, msh); } return (line); }