aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/lexer.h56
-rw-r--r--include/ms_glob.h1
-rw-r--r--include/parse.h4
-rw-r--r--src/lexer/lexer.c178
-rw-r--r--src/lexer/lexer_utils.c75
-rw-r--r--src/lexer/main.c15
-rw-r--r--src/main.c22
-rw-r--r--src/ms_glob.c1
8 files changed, 294 insertions, 58 deletions
diff --git a/include/lexer.h b/include/lexer.h
index c56244f..90f9353 100644
--- a/include/lexer.h
+++ b/include/lexer.h
@@ -1,29 +1,43 @@
-#include "minishell.h"
+#ifndef LEXER_H
+# define LEXER_H
-// just to make iterpolation compile
-//////////////////////////////////////
-enum e_token_tag
+# include <stdlib.h>
+# include "libft_lst.h"
+# include "libft_str.h"
+# include "minishell.h"
+
+enum e_token_tag
{
- LTAG_STR = 1 << 0,
- LTAG_STR_SINGLE = 1 << 1,
- LTAG_STR_DOUBLE = 1 << 2,
- LTAG_STICK = 1 << 3,
+ LTAG_AND = 1 << 0,
+ LTAG_END = 1 << 1,
+ LTAG_OR = 1 << 2,
+ LTAG_PIPE = 1 << 3,
+ LTAG_REDIR_IN = 1 << 4,
+ LTAG_REDIR_OUT = 1 << 5,
+ LTAG_REDIR_APPEND = 1 << 6,
+ LTAG_PARENT_OPEN = 1 << 7,
+ LTAG_PARENT_CLOSE = 1 << 8,
+ LTAG_STR = 1 << 9,
+ LTAG_STR_DOUBLE = 1 << 10,
+ LTAG_STR_SINGLE = 1 << 11,
+ LTAG_STICK = 1 << 12,
};
typedef struct
{
- enum e_token_tag tag;
- char *content;
-} t_token;
+ enum e_token_tag tag;
+ char *content;
+} t_token;
+
+t_ftlst *lexer(char *input);
+enum e_token_tag ret_token(char *input, int i);
+enum e_token_tag ret_token_sep_redir_append(char *input, int i);
+
+int lexer_sep(char input);
+int lexer_verif_entre_cote(char *input, int i);
+int lexe_space(char *input);
-t_token *token_new(enum e_token_tag tag, char *content);
-void token_destroy(t_token *token);
-//////////////////////////////////////
+t_token *token_new(enum e_token_tag tag, char *content);
+void token_destroy(t_token *token);
-char **lexer(char *input);
-int lexer_sep(char input);
-int lexer_verif_entre_cote(char *input, int i);
-int lexer_verif_entre_cote22(char *input, int i);
-int lexer_count_nb_element(char *input);
-int lexer_count_len_element(char *input, int i);
-char **lexer_malloc_len_elem(char *input, int i, char **out);
+#endif
diff --git a/include/ms_glob.h b/include/ms_glob.h
index 2eb16f1..5b5c932 100644
--- a/include/ms_glob.h
+++ b/include/ms_glob.h
@@ -19,6 +19,7 @@
# include "libft_str.h"
# include "libft_vec.h"
# include "utils.h"
+# include <limits.h>
struct s_glob_param
{
diff --git a/include/parse.h b/include/parse.h
index e864f9a..ec95ef9 100644
--- a/include/parse.h
+++ b/include/parse.h
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/28 09:00:00 by cacharle #+# #+# */
-/* Updated: 2020/06/09 10:55:09 by charles ### ########.fr */
+/* Updated: 2020/06/13 11:59:47 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -37,7 +37,7 @@
** lexer.c
*/
-char **lexer(char *input);
+// char **lexer(char *input);
/*
** parse.c
diff --git a/src/lexer/lexer.c b/src/lexer/lexer.c
index 7332b86..92cd34a 100644
--- a/src/lexer/lexer.c
+++ b/src/lexer/lexer.c
@@ -1,19 +1,177 @@
-
#include "lexer.h"
+int len_is_not_sep(char *input)
+{
+ int i;
+
+ i = -1;
+ while(input[++i])
+ {
+ if (lexer_sep(input[i]))
+ {
+ if (input[i + 1] == ' ')
+ while(input[++i] == ' ')
+ ;
+ return(i);
+ }
+ if (input[i] == '\'' || input[i] == '"')
+ return(i);
+ if (input[i] == ' ')
+ {
+ while(input[++i] == ' ')
+ ;
+ return (i);
+ }
+ }
+ return(i);
+}
+
+int check_input(char *input)
+{
+ int i;
+
+ i = 0;
+ if (lexer_sep(input[i]))
+ {
+ while(input[i] == input[i + 1])
+ i++;
+ i += lexe_space(&input[i + 1]);
+ return (i + 1);
+ }
+ if (input[i] == 39 || input[i] == '"')
+ return(lexer_verif_entre_cote(input, i));
+ if (input[i] == ' ')
+ {
+ while(input[++i] == ' ')
+ ;
+ return (i);
+ }
+ return(len_is_not_sep(&input[i]));
+}
+
+
+int check_input_out(char *input)
+{
+ int i;
+ int j;
+
+ i = 0;
+ while(input[i] != '\0')
+ {
+ j = 0;
+ j += len_is_not_sep(&input[i]);
+ if (j != 0)
+ return(j);
+ i += j;
+ j = check_input(&input[i]);
+ return(j);
+ }
+ return(0);
+}
+
+t_token *lexer_lst_token_str(char *input, int i, int j)
+{
+ t_token *lst_token;
+
+ if (!(lst_token = malloc(sizeof(t_token) * 1)))
+ return (NULL);
+ lst_token->tag = 0;
+ lst_token->content = NULL;
+ if (!(lst_token->content = malloc(sizeof(char) * j + 1)))
+ return(0);
+ if (!(ft_strlcpy(lst_token->content, &input[i], j + 1)))
+ {
+ free(lst_token);
+ return(0);
+ }
+ //printf("%s-\n", lst_token->content);
+ return (lst_token);
+}
+
+enum e_token_tag token_verif_stick(t_token *lst_token)
+{
+ int i;
+
+ i = ft_strlen(lst_token->content);
+
+ if (lst_token->content[i - 1] == ' ')
+ return(lst_token->tag);
+ return(lst_token->tag | LTAG_STICK);
+}
-char **lexer(char *input)
+enum e_token_tag token_str_or_cote(t_token *lst_token)
{
- char **out;
int i;
+ i = 0;
+ while(lst_token->content[i] != '\0')
+ {
+ if(lst_token->content[i] == '\'')
+ {
+ lst_token->tag = LTAG_STR_SINGLE;
+ return(token_verif_stick(lst_token));
+ }
+ if(lst_token->content[i] == '"')
+ {
+ lst_token->tag = LTAG_STR_DOUBLE;
+ return(token_verif_stick(lst_token));
+ }
+ else
+ {
+ lst_token->tag = LTAG_STR;
+ return(token_verif_stick(lst_token));
+ }
+ i++;
+ }
+ return(0);
+}
+
+t_token *push_token_enum_and_trim(t_token *lst_token)
+{
+ enum e_token_tag tk;
+
+ tk = ret_token(lst_token->content, 0);
+ if (tk == 0)
+ {
+ lst_token->tag = token_str_or_cote(lst_token);
+ }
+ /* printf("%s-, %d\n",lst_token->content, (int)lst_token->tag); */
+ return (lst_token);
+}
+
+static t_ftlst *create_token_list(char *input, t_ftlst **lst)
+{
+ t_token *lst_token;
+ t_ftlst *new;
+ int i;
+ int j;
+
+ i = 0;
+ while (i < (int)ft_strlen(input))
+ {
+ j = 0;
+ j += check_input(&input[i]);
+ lst_token = lexer_lst_token_str(input,i,j);
+ lst_token = push_token_enum_and_trim(lst_token);
+ new = ft_lstnew((void *) lst_token);
+ ft_lstpush_back(lst, new);
+ i += j;
+ }
+ return (*lst);
+}
+
+t_ftlst *lexer(char *input)
+{
+ t_ftlst **lst;
+ /* int i; */
+
if (!input)
return (0);
- i = lexer_count_nb_element(input);
- if (!(out = malloc(sizeof(char *) * i + 1)))
- return (0);
- out[i + 1] = NULL;
- out = lexer_malloc_len_elem(input,i, out);
- return(out);
-
+ lst = malloc(sizeof(t_ftlst *) * 1);
+ if (!lst)
+ return(0);
+ *lst = create_token_list(input, lst);
+ /* i = ft_lstsize(*lst); */
+ free(lst);
+ return (0);
}
diff --git a/src/lexer/lexer_utils.c b/src/lexer/lexer_utils.c
index f028113..986db50 100644
--- a/src/lexer/lexer_utils.c
+++ b/src/lexer/lexer_utils.c
@@ -1,13 +1,42 @@
-
#include "lexer.h"
-int lexer_sep(char input)
+enum e_token_tag ret_token_sep_redir_append(char *input, int i)
+{
+ if (input[i + 1] == '>')
+ return(LTAG_REDIR_APPEND);
+ return (LTAG_REDIR_OUT);
+
+}
+
+enum e_token_tag ret_token(char *input, int i)
+{
+ if (input[i] == ';')
+ return(LTAG_AND);
+ if (input[i] == '&')
+ return(LTAG_END);
+ if (input[i] == '|' && input[i + 1] == '|')
+ return(LTAG_OR);
+ if(input[i] == '|')
+ return(LTAG_PIPE);
+ if (input[i] == '>')
+ return(ret_token_sep_redir_append(input,i));
+ if (input[i] == '<')
+ return(LTAG_REDIR_IN);
+ if (input[i] == '(')
+ return(LTAG_PARENT_OPEN);
+ if (input[i] == ')')
+ return(LTAG_PARENT_CLOSE);
+ return(0);
+
+}
+
+int lexer_sep(char input)
{
char *sep;
int i;
i = 0;
- sep = ";&<>|";
+ sep = ";&|><()";
while(sep[i] != '\0')
{
if(sep[i] == input)
@@ -17,12 +46,40 @@ int lexer_sep(char input)
return (0);
}
-int lexer_verif_entre_cote(char *input, int i)
+int lexe_space(char *input)
{
- while((input[++i] != '"' || input[i] == '\'') && (input[i] != '\0'))
- ;
- i++;
- if(input[i] == '"' || input[i] == '\'')
- return(lexer_verif_entre_cote(input, i));
+ int i;
+
+ i=0;
+ while(input[i] == ' ')
+ i++;
return(i);
}
+
+static int lex_verif_simple_cote(char *input, int i)
+{
+ i++;
+ while(input[i] != '\0')
+ {
+ ++i;
+ if(input[i] == '\'')
+ break;
+ }
+ if (input[i + 1] == ' ')
+ while(input[i + 1] == ' ')
+ i++;
+ return(i + 1);
+}
+
+int lexer_verif_entre_cote(char *input, int i)
+{
+ if(input[i] == '\'')
+ return(lex_verif_simple_cote(input, i));
+ i++;
+ while(input[i] != '"' && (input[i] != '\0'))
+ ++i;
+ if (input[i + 1] == ' ')
+ while(input[i + 1] == ' ')
+ i++;
+ return(i + 1);
+}
diff --git a/src/lexer/main.c b/src/lexer/main.c
deleted file mode 100644
index b341e93..0000000
--- a/src/lexer/main.c
+++ /dev/null
@@ -1,15 +0,0 @@
-
-#include "lexer.h"
-
-/* int main(void) */
-/* { */
-/* char **out; */
-/* int i = -1; */
-/* */
-/* out = lexer("\"echo\" \"* login_x\"<<AUTHORS; echo && ; salut;\"echo\"\"* login_x\""); */
-/* //out = lexer("echo * login_x << AUTHORS&&cd Desktop"); */
-/* while(out[++i]) */
-/* printf("%s\n", out[i]);; */
-/* exit(0); */
-/* return (0); */
-/* } */
diff --git a/src/main.c b/src/main.c
index 42c2bd9..5dac612 100644
--- a/src/main.c
+++ b/src/main.c
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/28 11:45:44 by cacharle #+# #+# */
-/* Updated: 2020/06/12 11:57:48 by charles ### ########.fr */
+/* Updated: 2020/06/13 11:56:27 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -123,3 +123,23 @@ int main(int argc, char **argv, char **envp)
ft_vecdestroy(env, free);
return (0);
}
+
+
+/////////////////////////////////////////////////////////////////////////////////////////
+// lexer main
+/////////////////////////////////////////////////////////////////////////////////////////
+
+#include "lexer.h"
+
+/* int main(void) */
+/* { */
+/* char **out; */
+/* int i = -1; */
+/* */
+/* out = lexer("\"echo\" \"* login_x\"<<AUTHORS; echo && ; salut;\"echo\"\"* login_x\""); */
+/* //out = lexer("echo * login_x << AUTHORS&&cd Desktop"); */
+/* while(out[++i]) */
+/* printf("%s\n", out[i]);; */
+/* exit(0); */
+/* return (0); */
+/* } */
diff --git a/src/ms_glob.c b/src/ms_glob.c
index 61dd93b..39c537c 100644
--- a/src/ms_glob.c
+++ b/src/ms_glob.c
@@ -11,6 +11,7 @@
/* ************************************************************************** */
#include "ms_glob.h"
+#include <limits.h>
/*
** \brief Match vector start size