diff options
author | JozanLeClerc <bousset.rudy@gmail.com> | 2020-08-24 18:05:34 +0200 |
---|---|---|
committer | JozanLeClerc <bousset.rudy@gmail.com> | 2020-08-24 18:05:34 +0200 |
commit | f10e352f8740009d1a93f90160fda7aeabf2a2dc (patch) | |
tree | d8c98fdad9cb985d05e63d0999c4c252c233b8c8 /src | |
parent | TODO update (diff) | |
download | 42-minishell-f10e352f8740009d1a93f90160fda7aeabf2a2dc.tar.gz 42-minishell-f10e352f8740009d1a93f90160fda7aeabf2a2dc.tar.bz2 42-minishell-f10e352f8740009d1a93f90160fda7aeabf2a2dc.tar.xz 42-minishell-f10e352f8740009d1a93f90160fda7aeabf2a2dc.tar.zst 42-minishell-f10e352f8740009d1a93f90160fda7aeabf2a2dc.zip |
Counter | and counter \
Diffstat (limited to 'src')
-rw-r--r-- | src/m_loop.c | 79 | ||||
-rw-r--r-- | src/m_loop_next.c | 95 | ||||
-rw-r--r-- | src/m_loop_next.h | 24 | ||||
-rw-r--r-- | src/m_prompt.c | 2 |
4 files changed, 128 insertions, 72 deletions
diff --git a/src/m_loop.c b/src/m_loop.c index 2521204..e174f30 100644 --- a/src/m_loop.c +++ b/src/m_loop.c @@ -16,67 +16,16 @@ #include <unistd.h> #include "e_line.h" +#include "m_loop_next.h" #include "m_prompt.h" #include "p_line.h" #include "s_line.h" - -static char - *strjoin_m(char *s1, - char *s2, - uint8_t mode) -{ - size_t i; - size_t j; - size_t size1; - size_t size2; - char *dst; - - i = -1; - j = -1; - size1 = ft_strlen(s1); - size2 = ft_strlen(s2) + 1; - if (!(dst = (char*)malloc((size1 + size2 + 2) * sizeof(char)))) - return (NULL); - while (++i < size1) - dst[i] = s1[i]; - (mode == 0) ? dst[i++] = '\n' : 0; - while (++j < size2) - dst[i + j] = s2[j]; - dst[i + j] = '\0'; - (mode == 0) ? ft_memdel((void*)&s1) : ft_memdel((void*)&s2); - return (dst); -} - -void - m_loop_cont(t_msh *msh, - char *line, - char *quote, - int8_t gnl) +static void + m_parse_and_run_line(char *line, t_msh *msh) { - char *prog; - char *buf; - char *fin; - - if (!(prog = ft_calloc(ft_strlen(line) + 1, sizeof(char))) || - !(buf = ft_strdup(line + (quote - line) + 1))) - return ; - ft_memcpy(prog, line, (quote - line)); - while (gnl > 0 && ((!(quote = ft_strrchr(buf, '\"'))) && - (!(quote = ft_strrchr(buf, '\''))))) - { - m_prompt_psx(2, msh); - gnl = get_next_line(STDIN_FILENO, &line); - buf = strjoin_m(buf, line, 0); - ft_memdel((void*)&line); - } - fin = ft_strtrim(buf, "\"'"); - ft_memdel((void*)&buf); - fin = (ft_strrchr(fin, '\'')) ? ft_strsubst(fin, "\'", "") : fin; - fin = strjoin_m(prog, fin, 1); - p_line(fin, msh); - ft_memdel((void*)&prog); - ft_memdel((void*)&fin); + p_line(line, msh); + ft_memdel((void*)&line); e_line(msh); s_line_clear(&msh->curr); } @@ -85,7 +34,6 @@ uint8_t m_loop(int32_t fd, t_msh *msh) { char *line; - char *quote; int8_t gnl; gnl = 1; @@ -96,21 +44,10 @@ uint8_t gnl = get_next_line(fd, &line); if (line[0] != '\0') { - if (!(quote = ft_strchr(line, '\'')) && !(quote = ft_strchr(line, '\"'))) - { - p_line(line, msh); - ft_memdel((void*)&line); - e_line(msh); - s_line_clear(&msh->curr); - } - else - { - m_loop_cont(msh, line, quote, 1); - if (line != NULL) - ft_memdel((void*)&line); - } + line = m_check_multi_backslash(fd, line, msh); + line = m_check_multi_pipe(fd, line, msh); + m_parse_and_run_line(line, msh); /* TODO: (null): Bad address on "msh ~> echo a > asd; cat < asd" but not on "msh ~> echo a > asd; cat asd" */ - /* TODO: GNL 25 leak on "msh ~> exit" | gl hf */ /* TODO: "msh ~> some command \": re GNL into ft_nrealloc */ /* TODO: a histfile would be nice */ } diff --git a/src/m_loop_next.c b/src/m_loop_next.c new file mode 100644 index 0000000..9aa7129 --- /dev/null +++ b/src/m_loop_next.c @@ -0,0 +1,95 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* m_loop_next.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include <libft.h> +#include <stdint.h> +#include <unistd.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] != 0) + { + 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] != 0) + { + 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); +} + +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++; + while (*pipe != '\0') + { + if (ft_isspace(*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); +} + diff --git a/src/m_loop_next.h b/src/m_loop_next.h new file mode 100644 index 0000000..56221e7 --- /dev/null +++ b/src/m_loop_next.h @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* m_loop_next.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef M_LOOP_NEXT_H +#define M_LOOP_NEXT_H + +#include <stdint.h> + +#include "s_struct.h" + +char *m_check_multi_backslash(int32_t fd, char *line, t_msh *msh); +char *m_check_multi_pipe(int32_t fd, char *line, t_msh *msh); + + +#endif diff --git a/src/m_prompt.c b/src/m_prompt.c index 887fbbe..ad3c957 100644 --- a/src/m_prompt.c +++ b/src/m_prompt.c @@ -94,6 +94,6 @@ void m_prompt_psx(uint8_t x, t_msh *msh) { - m_update_psx(1, msh); + m_update_psx(x, msh); ft_dprintf(STDERR_FILENO, "%s", msh->ps[x - 1]); } |