summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJozanLeClerc <bousset.rudy@gmail.com>2020-09-08 20:26:51 +0200
committerJozanLeClerc <bousset.rudy@gmail.com>2020-09-08 20:26:51 +0200
commit74766656e5b605785e0e9622eb7f10d20a5467e3 (patch)
treed3818db14279a2e9649e314b3af4e819c61228e2 /src
parentPretty bav (diff)
download42-minishell-74766656e5b605785e0e9622eb7f10d20a5467e3.tar.gz
42-minishell-74766656e5b605785e0e9622eb7f10d20a5467e3.tar.bz2
42-minishell-74766656e5b605785e0e9622eb7f10d20a5467e3.tar.xz
42-minishell-74766656e5b605785e0e9622eb7f10d20a5467e3.tar.zst
42-minishell-74766656e5b605785e0e9622eb7f10d20a5467e3.zip
I hate myself and I want to die
Diffstat (limited to 'src')
-rw-r--r--src/d_define.h9
-rw-r--r--src/p_args.c47
-rw-r--r--src/p_split.c271
-rw-r--r--src/u_parse.c55
-rw-r--r--src/u_parse.h21
-rw-r--r--src/u_utils.c2
-rw-r--r--src/u_utils.h2
7 files changed, 252 insertions, 155 deletions
diff --git a/src/d_define.h b/src/d_define.h
index 5c10526..8e73668 100644
--- a/src/d_define.h
+++ b/src/d_define.h
@@ -66,11 +66,14 @@
** ====== UTILS ======
*/
-#define C_SQUOTE 0x27
-#define C_DQUOTE 0x22
-#define C_BACKS 0x5c
#define C_NUL 0x00
#define C_SUB 0x1a
+#define C_DQUOTE 0x22
+#define C_SQUOTE 0x27
+#define C_AMP 0x26
+#define C_SEMIC 0x3b
+#define C_BACKS 0x5c
+#define C_PIPE 0x7c
/*
** ====== FILES ======
diff --git a/src/p_args.c b/src/p_args.c
index 78cfc06..a4b3d02 100644
--- a/src/p_args.c
+++ b/src/p_args.c
@@ -18,6 +18,7 @@
#include "p_args.h"
#include "p_args_next.h"
#include "p_args_escape.h"
+#include "u_parse.h"
#include "u_utils.h"
static void
@@ -33,48 +34,6 @@ static void
}
}
-static t_quote_mode
- p_meet_dquote(char *head, char *ptr, t_quote_mode mode)
-{
- if (mode == Q_NONE)
- {
- if (u_is_not_escaped(head, ptr) == TRUE)
- {
- return (Q_DOUBLE);
- }
- else
- {
- return (Q_NONE);
- }
- }
- else if (mode == Q_DOUBLE && u_is_not_escaped(head, ptr) == TRUE)
- {
- return (Q_NONE);
- }
- return (mode);
-}
-
-static t_quote_mode
- p_meet_squote(char *head, char *ptr, t_quote_mode mode)
-{
- if (mode == Q_NONE)
- {
- if (u_is_not_escaped(head, ptr) == TRUE)
- {
- return (Q_SINGLE);
- }
- else
- {
- return (Q_NONE);
- }
- }
- else if (mode == Q_SINGLE)
- {
- return (Q_NONE);
- }
- return (mode);
-}
-
static t_bool
p_meet_whitespace(char *head, char *ptr, t_quote_mode mode)
{
@@ -110,9 +69,9 @@ static uint16_t
if (*ptr == C_BACKS)
p_meet_bs(ptr, mode);
if (*ptr == C_DQUOTE)
- mode = p_meet_dquote((char*)word, ptr, mode);
+ mode = u_meet_dquote((char*)word, ptr, mode);
else if (*ptr == C_SQUOTE)
- mode = p_meet_squote((char*)word, ptr, mode);
+ mode = u_meet_squote((char*)word, ptr, mode);
if (ft_iswhitespace(*ptr) &&
p_meet_whitespace((char*)word, ptr, mode) == TRUE)
{
diff --git a/src/p_split.c b/src/p_split.c
index 998fda5..f3f3e27 100644
--- a/src/p_split.c
+++ b/src/p_split.c
@@ -12,134 +12,193 @@
#include <libft.h>
#include <stdlib.h>
+#include <limits.h>
+#include "d_define.h"
+#include "u_parse.h"
#include "u_utils.h"
-static size_t
- p_count_semi_words(const char line[])
-{
- size_t count;
- size_t i;
+/* static size_t */
+/* p_count_semi_words(const char line[]) */
+/* { */
+/* size_t count; */
+/* size_t i; */
- count = 0;
- i = 0;
- while (line[i] != '\0')
- {
- if (line[i] == ';' && line[i + 1] != ';' && line[i + 1] != '\0')
- count += 1;
- i++;
- }
- return (count);
-}
+/* count = 0; */
+/* i = 0; */
+/* while (line[i] != '\0') */
+/* { */
+/* if (line[i] == ';' && line[i + 1] != ';' && line[i + 1] != '\0' && */
+/* u_is_not_escaped(line, line + i) == TRUE) */
+/* count += 1; */
+/* i++; */
+/* } */
+/* return (count); */
+/* } */
-static size_t
- p_count_and_or_words(const char line[], const char c)
-{
- size_t count;
- size_t i;
+/* static size_t */
+/* p_count_and_or_words(const char line[], const char c) */
+/* { */
+/* size_t count; */
+/* size_t i; */
- count = 0;
- i = 0;
- while (line[i] != '\0')
- {
- if (line[i] == c && line[i + 1] == c &&
- line[i + 2] != c && line[i + 2] != '\0')
- count += 1;
- i++;
- }
- return (count);
-}
+/* count = 0; */
+/* i = 0; */
+/* while (line[i] != '\0') */
+/* { */
+/* if (line[i] == c && line[i + 1] == c && */
+/* line[i + 2] != c && line[i + 2] != '\0') */
+/* count += 1; */
+/* i++; */
+/* } */
+/* return (count); */
+/* } */
+
+/* static char */
+/* *p_get_first_occur(const char *line_ptr) */
+/* { */
+/* char *ptr[3]; */
+/* size_t len[3]; */
+
+/* ptr[0] = ft_strnstr(line_ptr, ";", ft_strlen(line_ptr) + 1); */
+/* ptr[1] = ft_strnstr(line_ptr, "&&", ft_strlen(line_ptr) + 1); */
+/* ptr[2] = ft_strnstr(line_ptr, "||", ft_strlen(line_ptr) + 1); */
+/* len[0] = ft_strlen(ptr[0]); */
+/* len[1] = ft_strlen(ptr[1]); */
+/* len[2] = ft_strlen(ptr[2]); */
+/* if (len[0] > len[1] && len[0] > len[2]) */
+/* return (ptr[0]); */
+/* else if (len[1] > len[0] && len[1] > len[2]) */
+/* return (ptr[1]); */
+/* else if (len[2] > len[0] && len[2] > len[1]) */
+/* return (ptr[2]); */
+/* return ((char*)line_ptr); */
+/* } */
+
+/* static char */
+/* **p_split_destroy(char **words, */
+/* size_t i) */
+/* { */
+/* while (i > 0) */
+/* { */
+/* ft_memdel((void*)&words[i]); */
+/* } */
+/* ft_memdel((void*)&words); */
+/* return (NULL); */
+/* } */
-static char
- *p_get_first_occur(const char *line_ptr)
+/* static char */
+/* **p_split_to_stuff(const char line[], */
+/* const size_t count) */
+/* { */
+/* char **words; */
+/* char *line_ptr; */
+/* char *need_ptr; */
+/* size_t i; */
+/* char c; */
+
+/* if ((words = (char**)malloc((count + 1) * sizeof(char*))) == NULL) */
+/* return (NULL); */
+/* line_ptr = (char*)line; */
+/* i = 0; */
+/* while (i < count) */
+/* { */
+/* need_ptr = p_get_first_occur(line_ptr); */
+/* c = need_ptr[0]; */
+/* need_ptr += (c == ';') ? (1) : (0); */
+/* need_ptr += (c == '&') ? (2) : (0); */
+/* need_ptr += (c == '|') ? (2) : (0); */
+/* if (need_ptr - line_ptr == 0) */
+/* { */
+/* if ((words[i] = (char*)malloc(((ft_strlen(line_ptr) + 2) * */
+/* sizeof(char)))) == NULL) */
+/* return (p_split_destroy(words, i)); */
+/* ft_memcpy(words[i], line_ptr, ft_strlen(line_ptr)); */
+/* words[i][ft_strlen(line_ptr)] = ';'; */
+/* words[i][ft_strlen(line_ptr) + 1] = '\0'; */
+/* } */
+/* else */
+/* { */
+/* if ((words[i] = (char*)malloc(((need_ptr - line_ptr) + 1) * */
+/* sizeof(char))) == NULL) */
+/* return (p_split_destroy(words, i)); */
+/* ft_memcpy(words[i], line_ptr, (need_ptr - line_ptr) - 1); */
+/* words[i][(need_ptr - line_ptr) - ((c == ';') ? (1) : (2))] = c; */
+/* words[i][(need_ptr - line_ptr) - ((c == ';') ? (0) : (1))] = '\0'; */
+/* line_ptr = need_ptr; */
+/* } */
+/* i++; */
+/* } */
+/* words[i] = NULL; */
+/* return (words); */
+/* } */
+
+/* char */
+/* **p_split_line(const char line[]) */
+/* { */
+/* char **words; */
+/* size_t count; */
+
+/* count = p_count_semi_words(line); */
+/* count += p_count_and_or_words(line, '&'); */
+/* count += p_count_and_or_words(line, '|'); */
+/* count += 1; */
+/* if ((words = p_split_to_stuff(line, count)) == NULL) */
+/* return (NULL); */
+/* return (words); */
+/* } */
+
+typedef struct s_split
{
- char *ptr[3];
- size_t len[3];
-
- ptr[0] = ft_strnstr(line_ptr, ";", ft_strlen(line_ptr) + 1);
- ptr[1] = ft_strnstr(line_ptr, "&&", ft_strlen(line_ptr) + 1);
- ptr[2] = ft_strnstr(line_ptr, "||", ft_strlen(line_ptr) + 1);
- len[0] = ft_strlen(ptr[0]);
- len[1] = ft_strlen(ptr[1]);
- len[2] = ft_strlen(ptr[2]);
- if (len[0] > len[1] && len[0] > len[2])
- return (ptr[0]);
- else if (len[1] > len[0] && len[1] > len[2])
- return (ptr[1]);
- else if (len[2] > len[0] && len[2] > len[1])
- return (ptr[2]);
- return ((char*)line_ptr);
-}
+ size_t pos[ARG_MAX / 2];
+ int8_t nextif[ARG_MAX / 2];
+ int16_t count;
+} t_split;
-static char
- **p_split_destroy(char **words,
- size_t i)
+static void
+ p_meet_splitter(char *ptr, char line[], t_split sp, t_quote_mode mode)
{
- while (i > 0)
+ const char c = *ptr;
+
+ if (c == C_SEMIC && *ptr + 1 != C_SEMIC && *ptr + 1 != C_NUL &&
+ u_is_not_escaped(line, ptr) == TRUE)
{
- ft_memdel((void*)&words[i]);
+ sp.pos[sp.count] = (ptr - line);
}
- ft_memdel((void*)&words);
- return (NULL);
}
-static char
- **p_split_to_stuff(const char line[],
- const size_t count)
+static t_split
+ p_fill_sp(const char line[])
{
- char **words;
- char *line_ptr;
- char *need_ptr;
- size_t i;
- char c;
-
- if ((words = (char**)malloc((count + 1) * sizeof(char*))) == NULL)
- return (NULL);
- line_ptr = (char*)line;
- i = 0;
- while (i < count)
+ t_split sp;
+ char *ptr;
+ t_quote_mode mode;
+
+ sp.pos[0] = 0;
+ sp.nextif[0] = 0;
+ sp.count = 0;
+ mode = Q_NONE;
+ ptr = line;
+ while (*ptr != C_NUL)
{
- need_ptr = p_get_first_occur(line_ptr);
- c = need_ptr[0];
- need_ptr += (c == ';') ? (1) : (0);
- need_ptr += (c == '&') ? (2) : (0);
- need_ptr += (c == '|') ? (2) : (0);
- if (need_ptr - line_ptr == 0)
- {
- if ((words[i] = (char*)malloc(((ft_strlen(line_ptr) + 2) *
- sizeof(char)))) == NULL)
- return (p_split_destroy(words, i));
- ft_memcpy(words[i], line_ptr, ft_strlen(line_ptr));
- words[i][ft_strlen(line_ptr)] = ';';
- words[i][ft_strlen(line_ptr) + 1] = '\0';
- }
- else
- {
- if ((words[i] = (char*)malloc(((need_ptr - line_ptr) + 1) *
- sizeof(char))) == NULL)
- return (p_split_destroy(words, i));
- ft_memcpy(words[i], line_ptr, (need_ptr - line_ptr) - 1);
- words[i][(need_ptr - line_ptr) - ((c == ';') ? (1) : (2))] = c;
- words[i][(need_ptr - line_ptr) - ((c == ';') ? (0) : (1))] = '\0';
- line_ptr = need_ptr;
- }
- i++;
+ if (*ptr == C_SQUOTE)
+ mode = u_meet_squote(ptr, line, mode);
+ else if (*ptr == C_DQUOTE)
+ mode = u_meet_dquote(ptr, line, mode);
+ else if (*ptr == C_SEMIC || *ptr == C_AMP || *ptr == C_PIPE)
+ p_meet_splitter(ptr, line, sp, mode);
+ ptr++;
}
- words[i] = NULL;
- return (words);
+ return (sp);
}
char
**p_split_line(const char line[])
{
+ t_split sp;
char **words;
- size_t count;
-
- count = p_count_semi_words(line);
- count += p_count_and_or_words(line, '&');
- count += p_count_and_or_words(line, '|');
- count += 1;
- if ((words = p_split_to_stuff(line, count)) == NULL)
- return (NULL);
+
+ words = NULL;
+ sp = p_fill_sp(line);
return (words);
}
diff --git a/src/u_parse.c b/src/u_parse.c
new file mode 100644
index 0000000..ade8dd7
--- /dev/null
+++ b/src/u_parse.c
@@ -0,0 +1,55 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* u_parse.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 "d_define.h"
+
+t_quote_mode
+ u_meet_dquote(char *head, char *ptr, t_quote_mode mode)
+{
+ if (mode == Q_NONE)
+ {
+ if (u_is_not_escaped(head, ptr) == TRUE)
+ {
+ return (Q_DOUBLE);
+ }
+ else
+ {
+ return (Q_NONE);
+ }
+ }
+ else if (mode == Q_DOUBLE && u_is_not_escaped(head, ptr) == TRUE)
+ {
+ return (Q_NONE);
+ }
+ return (mode);
+}
+
+t_quote_mode
+ u_meet_squote(char *head, char *ptr, t_quote_mode mode)
+{
+ if (mode == Q_NONE)
+ {
+ if (u_is_not_escaped(head, ptr) == TRUE)
+ {
+ return (Q_SINGLE);
+ }
+ else
+ {
+ return (Q_NONE);
+ }
+ }
+ else if (mode == Q_SINGLE)
+ {
+ return (Q_NONE);
+ }
+ return (mode);
+}
diff --git a/src/u_parse.h b/src/u_parse.h
new file mode 100644
index 0000000..631468e
--- /dev/null
+++ b/src/u_parse.h
@@ -0,0 +1,21 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* u_parse.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 U_PARSE_H
+#define U_PARSE_H
+
+#include "d_define.h"
+
+t_quote_mode u_meet_dquote(char *head, char *ptr, t_quote_mode mode);
+t_quote_mode u_meet_squote(char *head, char *ptr, t_quote_mode mode);
+
+#endif
diff --git a/src/u_utils.c b/src/u_utils.c
index 7f75c4d..dd1634b 100644
--- a/src/u_utils.c
+++ b/src/u_utils.c
@@ -20,7 +20,7 @@
#include "s_struct.h"
t_bool
- u_is_not_escaped(char *head, char *ptr)
+ u_is_not_escaped(const char *head, const char *ptr)
{
if (((ptr - head) == 0) ||
((ptr - head) >= 1 && *(ptr - 1) != C_BACKS) ||
diff --git a/src/u_utils.h b/src/u_utils.h
index 2aa2c72..12606b7 100644
--- a/src/u_utils.h
+++ b/src/u_utils.h
@@ -18,7 +18,7 @@
#include "s_struct.h"
-t_bool u_is_not_escaped(char *head, char *ptr);
+t_bool u_is_not_escaped(const char *head, const char *ptr);
void u_eof_fd(int32_t fd);
uint64_t u_builtins_get_argc(const char *args[]);
char **u_get_env_var_names(t_msh *msh);