summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJozanLeClerc <bousset.rudy@gmail.com>2020-09-01 18:28:12 +0200
committerJozanLeClerc <bousset.rudy@gmail.com>2020-09-01 18:28:12 +0200
commit70e3b4133b89e4e77a67139ccaa37322e2507e49 (patch)
treef288fbaad300075c16b6a5385d860b142fa59ba9
parentCool (diff)
download42-minishell-70e3b4133b89e4e77a67139ccaa37322e2507e49.tar.gz
42-minishell-70e3b4133b89e4e77a67139ccaa37322e2507e49.tar.bz2
42-minishell-70e3b4133b89e4e77a67139ccaa37322e2507e49.tar.xz
42-minishell-70e3b4133b89e4e77a67139ccaa37322e2507e49.tar.zst
42-minishell-70e3b4133b89e4e77a67139ccaa37322e2507e49.zip
Added quotes multiline
Diffstat (limited to '')
-rw-r--r--libft/Makefile1
-rw-r--r--libft/include/libft.h1
-rw-r--r--libft/src/ft_isspace.c1
-rw-r--r--libft/src/ft_iswhitespace.c25
-rw-r--r--src/d_define.h9
-rw-r--r--src/m_loop.c1
-rw-r--r--src/m_loop_next.c61
-rw-r--r--src/m_loop_next.h1
-rw-r--r--src/p_args.c70
-rw-r--r--src/p_lcom_next.c33
-rw-r--r--src/p_line.c4
11 files changed, 166 insertions, 41 deletions
diff --git a/libft/Makefile b/libft/Makefile
index 6ada2e1..431ec0b 100644
--- a/libft/Makefile
+++ b/libft/Makefile
@@ -78,6 +78,7 @@ SRCS_NAME += ft_putnbr_base.c
SRCS_NAME += ft_strcat.c
SRCS_NAME += ft_strcmp.c
SRCS_NAME += ft_isspace.c
+SRCS_NAME += ft_iswhitespace.c
SRCS_NAME += ft_sqrt.c
SRCS_NAME += ft_intlen.c
SRCS_NAME += ft_intlen_base.c
diff --git a/libft/include/libft.h b/libft/include/libft.h
index 96615fd..68b8d9b 100644
--- a/libft/include/libft.h
+++ b/libft/include/libft.h
@@ -137,6 +137,7 @@ uint8_t ft_uintlen(unsigned long n);
uint8_t ft_uintlen_base(unsigned long n, char *base);
int ft_memcmp(const void *s1, const void *s2, size_t n);
t_bool ft_isspace(int c);
+t_bool ft_iswhitespace(int c);
t_bool ft_ischarset(const char *charset, int c);
t_bool ft_isupper(int c);
t_bool ft_islower(int c);
diff --git a/libft/src/ft_isspace.c b/libft/src/ft_isspace.c
index b9b653f..8454c7c 100644
--- a/libft/src/ft_isspace.c
+++ b/libft/src/ft_isspace.c
@@ -11,7 +11,6 @@
/* ************************************************************************** */
#include <libft.h>
-#include <inttypes.h>
t_bool
ft_isspace(int c)
diff --git a/libft/src/ft_iswhitespace.c b/libft/src/ft_iswhitespace.c
new file mode 100644
index 0000000..3bb5868
--- /dev/null
+++ b/libft/src/ft_iswhitespace.c
@@ -0,0 +1,25 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_iswhitespace.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/02/14 17:06:40 by rbousset #+# #+# */
+/* Updated: 2020/02/14 17:06:40 by rbousset ### ########lyon.fr */
+/* */
+/* ************************************************************************** */
+
+#include <libft.h>
+
+t_bool
+ ft_iswhitespace(int c)
+{
+ if (c == '\t' ||
+ c == '\v' ||
+ c == '\f' ||
+ c == '\r' ||
+ c == ' ')
+ return (TRUE);
+ return (FALSE);
+}
diff --git a/src/d_define.h b/src/d_define.h
index 62cfe7c..66cc619 100644
--- a/src/d_define.h
+++ b/src/d_define.h
@@ -62,6 +62,15 @@
#define FT_FAIL_HOME_NOT_SET "HOME not set"
/*
+** ====== UTILS ======
+*/
+
+#define C_SQUOTE '\''
+#define C_DQUOTE '"'
+#define C_BACKSLASH '\\'
+#define C_NULL '\000'
+
+/*
** ====== FILES ======
*/
diff --git a/src/m_loop.c b/src/m_loop.c
index 1faa2cf..17668a5 100644
--- a/src/m_loop.c
+++ b/src/m_loop.c
@@ -87,6 +87,7 @@ uint8_t
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);
if (fd == STDIN_FILENO)
m_handle_hist(line, msh);
m_parse_and_run_line(line, msh);
diff --git a/src/m_loop_next.c b/src/m_loop_next.c
index 1a8d867..22eadba 100644
--- a/src/m_loop_next.c
+++ b/src/m_loop_next.c
@@ -14,6 +14,7 @@
#include <stdint.h>
#include <unistd.h>
+#include "d_define.h"
#include "m_prompt.h"
#include "s_struct.h"
@@ -25,7 +26,7 @@ static char
if (fd == STDIN_FILENO)
m_prompt_psx(psx, msh);
get_next_line(fd, &counter_line);
- if (counter_line[0] != 0)
+ if (counter_line[0] != C_NULL)
{
line = ft_nrealloc(line,
ft_strlen(line) + 1,
@@ -46,7 +47,7 @@ static char
if (fd == STDIN_FILENO)
m_prompt_psx(psx, msh);
get_next_line(fd, &counter_line);
- if (counter_line[0] != 0)
+ if (counter_line[0] != C_NULL)
{
line = ft_nrealloc(line,
ft_strlen(line) + 1,
@@ -59,6 +60,30 @@ 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_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);
+ ft_printf("[%s]\n", line);
+ return (line);
+}
+
char
*m_check_multi_backslash(int32_t fd, char line[], t_msh *msh)
{
@@ -80,7 +105,7 @@ char
pipe += 1;
while (*pipe != '\0')
{
- if (ft_isspace(*pipe) == FALSE)
+ if (ft_iswhitespace(*pipe) == FALSE)
{
return (line);
}
@@ -103,7 +128,7 @@ char
and += 1;
while (*and != '\0')
{
- if (ft_isspace(*and) == FALSE)
+ if (ft_iswhitespace(*and) == FALSE)
{
return (line);
}
@@ -116,3 +141,31 @@ char
}
return (line);
}
+
+char
+ *m_check_multi_quotes(int32_t fd, char line[], t_msh *msh)
+{
+ char *ptr;
+ size_t q[2];
+
+ q[0] = 0;
+ q[1] = 0;
+ ptr = line;
+ while (*ptr != C_NULL)
+ {
+ if (*ptr == C_SQUOTE && *(ptr - 1) != C_BACKSLASH)
+ q[0] += 1;
+ else if (*ptr == C_DQUOTE && *(ptr - 1) != C_BACKSLASH)
+ q[1] += 1;
+ ptr++;
+ }
+ if (q[0] % 2 == 1 || q[1] % 2 == 1)
+ {
+ 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);
+}
diff --git a/src/m_loop_next.h b/src/m_loop_next.h
index 225e2ed..b4ca62c 100644
--- a/src/m_loop_next.h
+++ b/src/m_loop_next.h
@@ -20,6 +20,7 @@
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);
#endif
diff --git a/src/p_args.c b/src/p_args.c
index d8ca029..1cab59d 100644
--- a/src/p_args.c
+++ b/src/p_args.c
@@ -14,6 +14,8 @@
#include <stdint.h>
#include <stdlib.h>
+#include "d_define.h"
+
/* ================= */
/* TODO: DELETE THIS */
/* ================= */
@@ -35,14 +37,80 @@ p_print(char *words[])
/* TODO: DELETE ABOVE */
/* ================== */
+static void
+ p_skip_delim(char *ptr, char c)
+{
+ ptr++;
+ while (*ptr != C_NULL && *ptr != c)
+ {
+ ptr++;
+ if (*ptr == c && *(ptr - 1) == C_BACKSLASH)
+ ptr++;
+ }
+ if (*ptr != C_NULL)
+ ptr++;
+}
+
+static uint16_t
+ p_count_args(char *ptr, uint16_t argc)
+{
+ if (*ptr == C_NULL)
+ return (argc);
+ while (*ptr != C_NULL && ft_iswhitespace(*ptr) == TRUE)
+ ptr++;
+ if (*ptr != C_SQUOTE && *ptr != C_DQUOTE)
+ {
+ while (*ptr != C_NULL && ft_iswhitespace(*ptr) == FALSE)
+ {
+ ptr++;
+ if ((*ptr == C_SQUOTE || *ptr == C_DQUOTE) &&
+ *(ptr - 1) != C_BACKSLASH)
+ {
+ ptr++;
+ return (p_count_args(ptr, argc));
+ }
+ }
+ }
+ else if (*ptr == C_SQUOTE)
+ {
+ p_skip_delim(ptr, C_SQUOTE);
+ }
+ else if (*ptr == C_DQUOTE)
+ {
+ p_skip_delim(ptr, C_DQUOTE);
+ }
+ return (p_count_args(ptr, argc + 1));
+}
+
+static char
+ **p_split_words_no_rdr(const char word[])
+{
+ char **words;
+ char *ptr;
+ uint16_t argc;
+
+ ptr = (char*)word;
+ argc = p_count_args(ptr, 0);
+ ft_printf("%hu\n", argc);
+ exit(0);
+ if ((words = (char**)malloc((argc + 1) * sizeof(char*))) == NULL)
+ return (NULL);
+ words[argc] = NULL;
+ return (words);
+}
+
+
char
**p_split_args(const char word[], int8_t redir)
{
- char **words;
+ char **words;
+ words = NULL;
ft_printf("word at start: [%s]\n", word);
if (redir == 0)
{
+ if ((words = p_split_words_no_rdr(word)) == NULL)
+ return (NULL);
p_print(words);
return (words);
}
diff --git a/src/p_lcom_next.c b/src/p_lcom_next.c
index 28ad821..435a710 100644
--- a/src/p_lcom_next.c
+++ b/src/p_lcom_next.c
@@ -72,39 +72,6 @@ char
}
char
- **p_subst_args(const char word[],
- int8_t redir)
-{
- char **words;
-
- return (words);
- /* char **words; */
- /* char *subst; */
- /* size_t i; */
-
- /* if (redir == 0) */
- /* { */
- /* if (!(words = ft_split(word, ' '))) */
- /* return (NULL); */
- /* return (words); */
- /* } */
- /* i = 0; */
- /* while (word[i] && ft_ischarset("<>", word[i]) == FALSE) */
- /* i++; */
- /* while (redir > 0 && ft_isdigit(word[i]) == TRUE) */
- /* i--; */
- /* if (!(subst = ft_substr(word, 0, i))) */
- /* return (NULL); */
- /* if (!(words = ft_split(subst, ' '))) */
- /* { */
- /* ft_memdel((void*)&subst); */
- /* return (NULL); */
- /* } */
- /* ft_memdel((void*)&subst); */
- /* return (words); */
-}
-
-char
**p_subst_home(char *words[],
t_msh *msh)
{
diff --git a/src/p_line.c b/src/p_line.c
index ec148e3..0224bbb 100644
--- a/src/p_line.c
+++ b/src/p_line.c
@@ -45,7 +45,7 @@ static void
ptr = line;
while (*ptr != '\0')
{
- if (ft_isspace(*ptr) == TRUE)
+ if (ft_iswhitespace(*ptr) == TRUE)
{
*ptr = ' ';
}
@@ -61,7 +61,7 @@ static t_bool
ptr = line;
while (*ptr != '\0')
{
- if (ft_isspace(*ptr) == FALSE)
+ if (ft_iswhitespace(*ptr) == FALSE)
{
return (FALSE);
}