summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJozanLeClerc <bousset.rudy@gmail.com>2020-09-04 22:02:44 +0200
committerJozanLeClerc <bousset.rudy@gmail.com>2020-09-04 22:02:44 +0200
commit0e9cfacaf15bb0f4d8a7bd72bae56010d263b2c1 (patch)
tree129b3fbce4992e902e87358becf59e9be4be2851
parentNext time (diff)
download42-minishell-0e9cfacaf15bb0f4d8a7bd72bae56010d263b2c1.tar.gz
42-minishell-0e9cfacaf15bb0f4d8a7bd72bae56010d263b2c1.tar.bz2
42-minishell-0e9cfacaf15bb0f4d8a7bd72bae56010d263b2c1.tar.xz
42-minishell-0e9cfacaf15bb0f4d8a7bd72bae56010d263b2c1.tar.zst
42-minishell-0e9cfacaf15bb0f4d8a7bd72bae56010d263b2c1.zip
Time to escape chars
-rw-r--r--Makefile1
-rw-r--r--src/p_args.c32
-rw-r--r--src/p_args_len.c100
-rw-r--r--src/p_args_len.h20
-rw-r--r--src/p_args_next.c59
5 files changed, 146 insertions, 66 deletions
diff --git a/Makefile b/Makefile
index bf199f6..9d113ac 100644
--- a/Makefile
+++ b/Makefile
@@ -63,6 +63,7 @@ SRCS_NAME += s_lvars
SRCS_NAME += s_lpipes
SRCS_NAME += p_args
SRCS_NAME += p_args_next
+SRCS_NAME += p_args_len
SRCS_NAME += p_args_quotes
SRCS_NAME += p_line
SRCS_NAME += p_lcom
diff --git a/src/p_args.c b/src/p_args.c
index 72182fd..c25fc40 100644
--- a/src/p_args.c
+++ b/src/p_args.c
@@ -63,7 +63,7 @@ static char
return (ptr);
}
-static uint16_t
+static int32_t
p_skip_clean_arg(const char *head,
char **ptr,
uint16_t argc,
@@ -72,11 +72,18 @@ static uint16_t
while (*(*ptr) != C_NULL && ft_iswhitespace(*(*ptr)) == FALSE)
{
(*ptr)++;
- if ((*(*ptr) == C_SQUOTE || *(*ptr) == C_DQUOTE) && *(*(ptr - 1)) != '\\')
+ if ((*(*ptr) == C_SQUOTE || *(*ptr) == C_DQUOTE) &&
+ *(*ptr - 1) != C_BACKSLASH)
{
- (*ptr)++;
+ (*ptr) += 1;
return (p_count_args(head, *ptr, argc, start));
}
+ if (ft_iswhitespace(*(*ptr)) == TRUE && *(*ptr - 1) == C_BACKSLASH &&
+ *(*ptr - 2) != C_BACKSLASH)
+ {
+ (*ptr) += 1;
+ return (-1);
+ }
}
return (0);
}
@@ -84,7 +91,7 @@ static uint16_t
uint16_t
p_count_args(const char *head, char *ptr, uint16_t argc, size_t start[])
{
- static uint16_t preset = 512;
+ static int32_t preset = -1;
if (*ptr == C_NULL)
return (argc);
@@ -95,7 +102,8 @@ uint16_t
preset = argc;
if (*ptr != C_SQUOTE && *ptr != C_DQUOTE && *ptr != C_NULL)
{
- p_skip_clean_arg(head, &ptr, argc, start);
+ if (p_skip_clean_arg(head, &ptr, argc, start) == -1)
+ argc -= 1;
}
else if (*ptr == C_SQUOTE || *ptr == C_DQUOTE)
{
@@ -114,15 +122,15 @@ uint16_t
static char
**p_split_words_no_rdr(const char word[])
{
- char **words;
- char *ptr;
- size_t start[512];
- uint16_t argc;
- uint16_t to_del;
+ char **words;
+ char *ptr;
+ size_t start[512];
+ uint16_t argc;
+ uint16_t to_del;
ptr = (char*)word;
argc = p_count_args(word, ptr, 0, start);
- ft_printf("%hu\n", argc);
+ ft_printf("argc: %hu\n", argc);
if ((words = (char**)malloc((argc + 1) * sizeof(char*))) == NULL)
return (NULL);
words[argc] = NULL;
@@ -131,6 +139,8 @@ static char
p_del_alloced_words(words, to_del);
return (NULL);
}
+ p_print(words);
+ p_escape_chars(words);
p_args_quotes(words);
return (words);
}
diff --git a/src/p_args_len.c b/src/p_args_len.c
new file mode 100644
index 0000000..8bab9d7
--- /dev/null
+++ b/src/p_args_len.c
@@ -0,0 +1,100 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* p_args_len.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 <stddef.h>
+
+#include "d_define.h"
+
+static size_t
+ p_skip_delim_size(const char word[], char c, size_t end)
+{
+ end++;
+ if (word[end] == c)
+ return (end + 1);
+ while (word[end] != C_NULL && word[end] != c)
+ {
+ end++;
+ if (word[end] == c && c == C_DQUOTE)
+ {
+ if (word[end - 1] == C_BACKSLASH && word[end - 2] != C_BACKSLASH)
+ {
+ end++;
+ }
+ }
+ }
+ if (word[end] != C_NULL)
+ {
+ end++;
+ }
+ return (end);
+}
+
+static size_t
+ p_skip_unquote(const char word[], size_t end)
+{
+ while (word[end] != C_NULL && ft_iswhitespace(word[end]) == FALSE)
+ {
+ end++;
+ if (ft_iswhitespace(word[end]) == TRUE && end == 1 &&
+ word[end - 1] == C_BACKSLASH)
+ {
+ end++;
+ }
+ else if (ft_iswhitespace(word[end]) == TRUE && end > 1 &&
+ word[end - 1] == C_BACKSLASH && word[end - 2] != C_BACKSLASH)
+ {
+ end++;
+ }
+ }
+ return (end);
+}
+
+static size_t
+ p_skip_quote(const char word[], size_t end)
+{
+ while (word[end] != C_NULL && ft_iswhitespace(word[end]) == FALSE)
+ {
+ if (word[end] == C_SQUOTE || word[end] == C_DQUOTE)
+ {
+ end = p_skip_delim_size(word, word[end], end);
+ }
+ else
+ {
+ while (word[end] != C_NULL &&
+ ft_iswhitespace(word[end]) == FALSE)
+ {
+ end++;
+ }
+ }
+ }
+ return (end);
+}
+
+size_t
+ p_arg_len(const char word[], const size_t start)
+{
+ size_t end;
+
+ end = start;
+ if (word[start] != C_SQUOTE && word[start] != C_DQUOTE &&
+ word[start] != C_NULL)
+ {
+ end = p_skip_unquote(word, end);
+ }
+ else if (word[end] == C_SQUOTE || word[end] == C_DQUOTE)
+ {
+ end = p_skip_quote(word, end);
+ }
+ return (end);
+}
+
diff --git a/src/p_args_len.h b/src/p_args_len.h
new file mode 100644
index 0000000..093d8f7
--- /dev/null
+++ b/src/p_args_len.h
@@ -0,0 +1,20 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* p_args_len.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 P_ARGS_LEN_H
+#define P_ARGS_LEN_H
+
+#include <stddef.h>
+
+size_t p_arg_len(const char word[], const size_t start);
+
+#endif
diff --git a/src/p_args_next.c b/src/p_args_next.c
index 10ca37f..77f8ced 100644
--- a/src/p_args_next.c
+++ b/src/p_args_next.c
@@ -15,60 +15,7 @@
#include "d_define.h"
#include "p_args.h"
-
-static size_t
- p_skip_delim_size(const char word[], char c, size_t end)
-{
- end++;
- if (word[end] == c)
- return (end + 1);
- while (word[end] != C_NULL && word[end] != c)
- {
- end++;
- if (word[end] == c && c == C_DQUOTE)
- {
- if (word[end - 1] == C_BACKSLASH && word[end - 2] != C_BACKSLASH)
- {
- end++;
- }
- }
- }
- if (word[end] != C_NULL)
- {
- end++;
- }
- return (end);
-}
-
-static size_t
- p_arg_len(const char word[], const size_t start)
-{
- size_t end;
-
- end = start;
- if (word[start] != C_SQUOTE &&
- word[start] != C_DQUOTE &&
- word[start] != C_NULL)
- {
- while (word[end] != C_NULL && ft_iswhitespace(word[end]) == FALSE)
- end++;
- }
- else if (word[end] == C_SQUOTE || word[end] == C_DQUOTE)
- {
- while (word[end] != C_NULL && ft_iswhitespace(word[end]) == FALSE)
- {
- if (word[end] == C_SQUOTE || word[end] == C_DQUOTE)
- end = p_skip_delim_size(word, word[end], end);
- else
- {
- while (word[end] != C_NULL &&
- ft_iswhitespace(word[end]) == FALSE)
- end++;
- }
- }
- }
- return (end);
-}
+#include "p_args_len.h"
static char
*p_give_me_an_arg(char tmp[],
@@ -108,7 +55,9 @@ uint16_t
i = 0;
while (i < argc)
{
- if ((words[i] = ft_strdup(p_give_me_an_arg(tmp, word, i, start))) == NULL)
+ if ((words[i] = ft_strdup(
+ p_give_me_an_arg(tmp, word, i, start)
+ )) == NULL)
return (i);
i++;
}