aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authornass1pro <nass1pro@gmail.com>2020-09-14 16:21:24 +0200
committernass1pro <nass1pro@gmail.com>2020-09-14 16:21:24 +0200
commit7f08c8ba80240ece1f34caa4fb15e240301fff5e (patch)
tree92412156e94f69f0fd7240e7e5e131712a2c1a19 /src
parent5c5096d5c6d67686c1ad896cbb7a1b5386abf597 (diff)
downloadminishell-7f08c8ba80240ece1f34caa4fb15e240301fff5e.tar.gz
minishell-7f08c8ba80240ece1f34caa4fb15e240301fff5e.tar.bz2
minishell-7f08c8ba80240ece1f34caa4fb15e240301fff5e.zip
test error lexer quote
Diffstat (limited to 'src')
-rw-r--r--src/lexer/lexer.c15
-rw-r--r--src/lexer/trim.c40
-rw-r--r--src/main.c17
3 files changed, 52 insertions, 20 deletions
diff --git a/src/lexer/lexer.c b/src/lexer/lexer.c
index 79401f2..63e92db 100644
--- a/src/lexer/lexer.c
+++ b/src/lexer/lexer.c
@@ -6,11 +6,12 @@
/* By: nahaddac <nahaddac@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/07/16 08:18:25 by nahaddac #+# #+# */
-/* Updated: 2020/09/14 11:30:24 by nahaddac ### ########.fr */
+/* Updated: 2020/09/14 16:14:51 by nahaddac ### ########.fr */
/* */
/* ************************************************************************** */
#include "lexer.h"
+#include <stdio.h>
// len until meaningful character for non quoted str
int len_until_sep(char *input)
@@ -101,16 +102,20 @@ t_tok_lst *create_token_list(char *input, t_tok_lst **lst)
** \return The created tokens or NULL on error
*/
-t_tok_lst *lexer(char *input)
+int lexer(char *input, t_tok_lst **out)
{
t_tok_lst *lst;
+ int r;
+ r = 0;
if (!input)
- return (NULL);
+ return (1);
lst = NULL;
lst = create_token_list(input, &lst);
- lexer_trim(lst);
- return (lst);
+ r = lexer_trim(lst);
+ *out = lst;
+ return r;
+
}
/* int check_input_out(char *input) */
diff --git a/src/lexer/trim.c b/src/lexer/trim.c
index 351ad36..991cecc 100644
--- a/src/lexer/trim.c
+++ b/src/lexer/trim.c
@@ -6,12 +6,13 @@
/* By: nahaddac <nahaddac@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/07/16 08:18:36 by nahaddac #+# #+# */
-/* Updated: 2020/09/14 11:30:18 by nahaddac ### ########.fr */
+/* Updated: 2020/09/14 15:40:21 by nahaddac ### ########.fr */
/* */
/* ************************************************************************** */
#include "lexer.h"
+#include <stdio.h>
void del_space(char *str)
{
@@ -31,11 +32,13 @@ void del_space(char *str)
}
}
-void del_quote(char *str)
+int del_quote(char *str)
{
size_t i;
+ int nb_q;
i = 0;
+ nb_q = 1;
if (str[0] == '\'')
{
while (str[i++] != '\0')
@@ -48,7 +51,10 @@ void del_quote(char *str)
else if (str[i] == '\\')
i += 2;
if (str[i] == '\'')
+ {
+ nb_q++;
break ;
+ }
}
}
else if (str[0] == '"')
@@ -58,22 +64,39 @@ void del_quote(char *str)
if (str[i] == '\\')
i += 2;
if (str[i] == '"')
+ {
+ nb_q++;
break ;
+ }
}
}
- str[i] = '\0';
- ft_memmove(str, str + 1, ft_strlen(str + 1) + 1);
+ if (nb_q % 2 == 0)
+ {
+ str[i] = '\0';
+ if(!(ft_memmove(str, str + 1, ft_strlen(str + 1) + 1)))
+ return 1;
+ else
+ return 0;
+ }
+ else
+ return 2;
}
-void lexer_trim(t_tok_lst *tokens)
+int lexer_trim(t_tok_lst *tokens)
{
+ int r = 0;
while (tokens != NULL)
{
if (tokens->tag & (TAG_STR_DOUBLE | TAG_STR_SINGLE))
{
- del_quote(tokens->content);
- if (tokens->next == NULL)
- tokens->tag &= ~TAG_STICK;
+ r = del_quote(tokens->content);
+ if (r == 0)
+ {
+ if (tokens->next == NULL)
+ tokens->tag &= ~TAG_STICK;
+ }
+ else
+ return r;
}
else
{
@@ -83,4 +106,5 @@ void lexer_trim(t_tok_lst *tokens)
}
tokens = tokens->next;
}
+ return 0;
}
diff --git a/src/main.c b/src/main.c
index caa4462..daa33f3 100644
--- a/src/main.c
+++ b/src/main.c
@@ -6,7 +6,7 @@
/* By: cacharle <cacharle@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/28 11:45:44 by cacharle #+# #+# */
-/* Updated: 2020/09/13 16:11:32 by charles ### ########.fr */
+/* Updated: 2020/09/14 16:13:41 by nahaddac ### ########.fr */
/* */
/* ************************************************************************** */
@@ -47,7 +47,9 @@ int main(int argc, char **argv, char **envp)
{
t_path path;
t_env env;
+ int r;
+ r = 0;
env = env_from_array(envp);
char buf[PATH_MAX] = {0};
@@ -101,15 +103,15 @@ int main(int argc, char **argv, char **envp)
if (argc == 3 && ft_strcmp(argv[1], "-l") == 0)
{
- t_tok_lst *lex_out = lexer(argv[2]);
- if (lex_out == NULL)
- return (1);
+ t_tok_lst *lex_out;
+ r = lexer(argv[2], &lex_out);
tok_lst_debug(lex_out);
/* ft_lstiter((t_ftlst*)lex_out, token_debug); */
}
else if (argc == 3 && ft_strcmp(argv[1], "-c") == 0) // put in MINISHELL_TEST
{
- t_tok_lst *lex_out = lexer(argv[2]);
+ t_tok_lst *lex_out;
+ r = lexer(argv[2], &lex_out);
if (lex_out == NULL)
return (1);
@@ -144,11 +146,12 @@ int main(int argc, char **argv, char **envp)
print_prompt();
continue;
}
- t_tok_lst *lex_out = lexer(line);
+ t_tok_lst **lex_out = NULL;
+ r = lexer(line, lex_out);
if (lex_out == NULL)
return (1);
- t_parsed *parser_out = parse(lex_out);
+ t_parsed *parser_out = parse(*lex_out);
if (parser_out == NULL)
return (1);
if (parser_out->syntax_error)