summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJozanLeClerc <bousset.rudy@gmail.com>2020-10-01 19:14:01 +0200
committerJozanLeClerc <bousset.rudy@gmail.com>2020-10-01 19:14:01 +0200
commit426399f98b6cf68c2eee448de49ea091ac7a0562 (patch)
treee68f2f1ed1fca3bd54d7525d81f29e1bd6836703
parentRenormed m_loop (diff)
download42-minishell-426399f98b6cf68c2eee448de49ea091ac7a0562.tar.gz
42-minishell-426399f98b6cf68c2eee448de49ea091ac7a0562.tar.bz2
42-minishell-426399f98b6cf68c2eee448de49ea091ac7a0562.tar.xz
42-minishell-426399f98b6cf68c2eee448de49ea091ac7a0562.tar.zst
42-minishell-426399f98b6cf68c2eee448de49ea091ac7a0562.zip
Normed m_loop
Diffstat (limited to '')
-rw-r--r--Makefile4
-rw-r--r--src/m_loop.c2
-rw-r--r--src/m_loop_counter.c80
-rw-r--r--src/m_loop_counter.h24
-rw-r--r--src/m_loop_multis.c111
-rw-r--r--src/m_loop_multis.h23
-rw-r--r--src/m_loop_next.c176
-rw-r--r--src/m_loop_next.h10
8 files changed, 251 insertions, 179 deletions
diff --git a/Makefile b/Makefile
index 380520b..ff72886 100644
--- a/Makefile
+++ b/Makefile
@@ -1,4 +1,4 @@
-default: msan
+default: all
#==============================================================================#
#--------------------------------- SHELL --------------------------------------#
#==============================================================================#
@@ -54,6 +54,8 @@ SRCS_NAME += m_comm
SRCS_NAME += m_funptr
SRCS_NAME += m_init
SRCS_NAME += m_loop
+SRCS_NAME += m_loop_counter
+SRCS_NAME += m_loop_multis
SRCS_NAME += m_loop_next
SRCS_NAME += m_minishell
SRCS_NAME += m_mshrc
diff --git a/src/m_loop.c b/src/m_loop.c
index fe4a4b8..5509ccc 100644
--- a/src/m_loop.c
+++ b/src/m_loop.c
@@ -19,7 +19,7 @@
#include "d_define.h"
#include "e_line.h"
-#include "m_loop_next.h"
+#include "m_loop_multis.h"
#include "m_prompt.h"
#include "p_line.h"
#include "s_com.h"
diff --git a/src/m_loop_counter.c b/src/m_loop_counter.c
new file mode 100644
index 0000000..55a3b27
--- /dev/null
+++ b/src/m_loop_counter.c
@@ -0,0 +1,80 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* m_loop_counter.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"
+
+char *m_counter_line_backslash(int32_t fd, uint8_t psx, char *l, 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_NUL)
+ {
+ l = ft_nrealloc(l,
+ ft_strlen(l) + 1,
+ ft_strlen(l) + ft_strlen(counter_line));
+ ft_memcpy(l + ft_strlen(l) - 1,
+ counter_line,
+ ft_strlen(counter_line) + 1);
+ }
+ ft_memdel((void*)&counter_line);
+ return (l);
+}
+
+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_NUL)
+ {
+ 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_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_NUL)
+ {
+ 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);
+}
diff --git a/src/m_loop_counter.h b/src/m_loop_counter.h
new file mode 100644
index 0000000..3bf04ab
--- /dev/null
+++ b/src/m_loop_counter.h
@@ -0,0 +1,24 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* m_loop_counter.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 FT_M_LOOP_COUNTER_H
+# define FT_M_LOOP_COUNTER_H
+
+# include <stdint.h>
+
+# include "s_struct.h"
+
+char *m_counter_line_backslash(int32_t fd, uint8_t psx, char *l, t_msh *msh);
+char *m_counter_line_pipes(int32_t fd, uint8_t psx, char *line, t_msh *msh);
+char *m_counter_line_quotes(int32_t fd, uint8_t psx, char *line, t_msh *msh);
+
+#endif
diff --git a/src/m_loop_multis.c b/src/m_loop_multis.c
new file mode 100644
index 0000000..c4438d3
--- /dev/null
+++ b/src/m_loop_multis.c
@@ -0,0 +1,111 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* m_loop_multis.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 "m_loop_counter.h"
+#include "m_loop_next.h"
+#include "u_utils.h"
+
+static t_bool m_check_dquote(char **ptr, char line[])
+{
+ if (((*ptr - line) == 0) ||
+ ((*ptr - line) == 1 && *(*ptr - 1) == C_BACKS) ||
+ ((*ptr - line) > 1 && *(*ptr - 1) == C_BACKS && *(*ptr - 2) != C_BACKS))
+ {
+ return (FALSE);
+ }
+ else
+ {
+ return (TRUE);
+ }
+}
+
+static t_bool m_find_next_quote(char **ptr, char line[], t_quote_mode mode)
+{
+ char c;
+
+ if (mode == Q_NONE)
+ return (FALSE);
+ c = (mode == Q_SINGLE) ? (C_SQUOTE) : (C_DQUOTE);
+ (*ptr) += 1;
+ while (**ptr != C_NUL)
+ {
+ if (**ptr == c && c == C_DQUOTE)
+ {
+ if (m_check_dquote(ptr, line) == FALSE)
+ {
+ (*ptr)++;
+ if (**ptr == C_NUL)
+ break ;
+ }
+ else
+ return (FALSE);
+ }
+ else if (**ptr == c && c == C_SQUOTE)
+ return (FALSE);
+ (*ptr)++;
+ }
+ return (TRUE);
+}
+
+static t_bool m_check_missing_quotes(char line[])
+{
+ char *ptr;
+ t_quote_mode mode;
+
+ ptr = line;
+ mode = Q_NONE;
+ while (*ptr != C_NUL)
+ {
+ if (*ptr == C_SQUOTE || *ptr == C_DQUOTE)
+ {
+ if (u_is_not_escaped(line, ptr) == TRUE)
+ {
+ mode = (*ptr == C_SQUOTE) ? (Q_SINGLE) : (Q_DOUBLE);
+ if (m_find_next_quote(&ptr, line, mode) == FALSE)
+ mode = Q_NONE;
+ else
+ return (TRUE);
+ }
+ }
+ ptr++;
+ }
+ return (FALSE);
+}
+
+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);
+}
+
+char *m_check_multis(int32_t fd, char line[], t_msh *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);
+}
diff --git a/src/m_loop_multis.h b/src/m_loop_multis.h
new file mode 100644
index 0000000..33ba191
--- /dev/null
+++ b/src/m_loop_multis.h
@@ -0,0 +1,23 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* m_loop_multis.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 FT_M_LOOP_MULTIS_H
+# define FT_M_LOOP_MULTIS_H
+
+# include <stdint.h>
+
+# include "s_struct.h"
+
+char *m_check_multi_quotes(int32_t fd, char line[], t_msh *msh);
+char *m_check_multis(int32_t fd, char line[], t_msh *msh);
+
+#endif
diff --git a/src/m_loop_next.c b/src/m_loop_next.c
index b312c0b..a31e850 100644
--- a/src/m_loop_next.c
+++ b/src/m_loop_next.c
@@ -15,78 +15,12 @@
#include <unistd.h>
#include "d_define.h"
-#include "m_loop_next.h"
-#include "m_prompt.h"
+#include "m_loop_counter.h"
+#include "m_loop_multis.h"
#include "s_struct.h"
#include "u_utils.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_NUL)
- {
- 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_NUL)
- {
- 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_NUL)
- {
- 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)
+char *m_check_multi_backslash(int32_t fd, char line[], t_msh *msh)
{
if (line[ft_strlen(line) - 1] == '\\')
{
@@ -96,8 +30,7 @@ char
return (line);
}
-char
- *m_check_multi_pipe(int32_t fd, char line[], t_msh *msh)
+char *m_check_multi_pipe(int32_t fd, char line[], t_msh *msh)
{
char *pipe;
@@ -119,8 +52,7 @@ char
return (line);
}
-char
- *m_check_multi_and(int32_t fd, char line[], t_msh *msh)
+char *m_check_multi_and(int32_t fd, char line[], t_msh *msh)
{
char *and;
@@ -142,101 +74,3 @@ char
}
return (line);
}
-
-static t_bool
- m_check_dquote(char **ptr, char line[])
-{
- if (((*ptr - line) == 0) ||
- ((*ptr - line) == 1 && *(*ptr - 1) == C_BACKS) ||
- ((*ptr - line) > 1 && *(*ptr - 1) == C_BACKS && *(*ptr - 2) != C_BACKS))
- {
- return (FALSE);
- }
- else
- {
- return (TRUE);
- }
-}
-
-static t_bool
- m_find_next_quote(char **ptr, char line[], t_quote_mode mode)
-{
- char c;
-
- if (mode == Q_NONE)
- return (FALSE);
- c = (mode == Q_SINGLE) ? (C_SQUOTE) : (C_DQUOTE);
- (*ptr) += 1;
- while (**ptr != C_NUL)
- {
- if (**ptr == c && c == C_DQUOTE)
- {
- if (m_check_dquote(ptr, line) == FALSE)
- {
- (*ptr)++;
- if (**ptr == C_NUL)
- break ;
- }
- else
- return (FALSE);
- }
- else if (**ptr == c && c == C_SQUOTE)
- return (FALSE);
- (*ptr)++;
- }
- return (TRUE);
-}
-
-static t_bool
- m_check_missing_quotes(char line[])
-{
- char *ptr;
- t_quote_mode mode;
-
- ptr = line;
- mode = Q_NONE;
- while (*ptr != C_NUL)
- {
- if (*ptr == C_SQUOTE || *ptr == C_DQUOTE)
- {
- if (u_is_not_escaped(line, ptr) == TRUE)
- {
- mode = (*ptr == C_SQUOTE) ? (Q_SINGLE) : (Q_DOUBLE);
- if (m_find_next_quote(&ptr, line, mode) == FALSE)
- mode = Q_NONE;
- else
- return (TRUE);
- }
- }
- ptr++;
- }
- return (FALSE);
-}
-
-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);
-}
-
-char *m_check_multis(int32_t fd, char line[], t_msh *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);
-}
diff --git a/src/m_loop_next.h b/src/m_loop_next.h
index b400e76..d6d1c08 100644
--- a/src/m_loop_next.h
+++ b/src/m_loop_next.h
@@ -10,17 +10,15 @@
/* */
/* ************************************************************************** */
-#ifndef M_LOOP_NEXT_H
-#define M_LOOP_NEXT_H
+#ifndef FT_M_LOOP_NEXT_H
+# define FT_M_LOOP_NEXT_H
-#include <stdint.h>
+# include <stdint.h>
-#include "s_struct.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);
char *m_check_multi_and(int32_t fd, char line[], t_msh *msh);
-char *m_check_multi_quotes(int32_t fd, char line[], t_msh *msh);
-char *m_check_multis(int32_t fd, char line[], t_msh *msh);
#endif