summaryrefslogtreecommitdiffstats
path: root/src/m_loop_next.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/m_loop_next.c')
-rw-r--r--src/m_loop_next.c149
1 files changed, 143 insertions, 6 deletions
diff --git a/src/m_loop_next.c b/src/m_loop_next.c
index 6c0888f..e088cda 100644
--- a/src/m_loop_next.c
+++ b/src/m_loop_next.c
@@ -14,9 +14,12 @@
#include <stdint.h>
#include <unistd.h>
+#include "d_define.h"
+#include "m_loop_next.h"
#include "m_prompt.h"
#include "s_struct.h"
#include "c_init.h"
+#include "u_utils.h"
static char
*m_counter_line_backslash(int32_t fd, uint8_t psx, char *line, t_msh *msh)
@@ -24,7 +27,7 @@ static char
char *counter_line;
c_gnl(fd, &counter_line, psx, msh);
- if (counter_line[0] != 0)
+ if (counter_line[0] != C_NUL)
{
line = ft_nrealloc(line,
ft_strlen(line) + 1,
@@ -43,7 +46,7 @@ static char
char *counter_line;
c_gnl(fd, &counter_line, psx, msh);
- if (counter_line[0] != 0)
+ if (counter_line[0] != C_NUL)
{
line = ft_nrealloc(line,
ft_strlen(line) + 1,
@@ -56,8 +59,31 @@ static char
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)
+ *m_check_multi_backslash(int32_t fd, char line[], t_msh *msh)
{
if (line[ft_strlen(line) - 1] == '\\')
{
@@ -68,16 +94,16 @@ char
}
char
- *m_check_multi_pipe(int32_t fd, char *line, t_msh *msh)
+ *m_check_multi_pipe(int32_t fd, char line[], t_msh *msh)
{
char *pipe;
if ((pipe = ft_strrchr(line, '|')) != NULL)
{
- pipe++;
+ pipe += 1;
while (*pipe != '\0')
{
- if (ft_isspace(*pipe) == FALSE)
+ if (ft_iswhitespace(*pipe) == FALSE)
{
return (line);
}
@@ -90,3 +116,114 @@ char
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_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);
+}