aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/eval/redir.c12
-rw-r--r--src/lexer/lexer.c18
-rw-r--r--src/lexer/tok_lst.c2
-rw-r--r--src/main.c8
-rw-r--r--src/parser/parsed.c3
-rw-r--r--src/parser/parser.c144
6 files changed, 84 insertions, 103 deletions
diff --git a/src/eval/redir.c b/src/eval/redir.c
index 3e67c74..8ab214d 100644
--- a/src/eval/redir.c
+++ b/src/eval/redir.c
@@ -6,7 +6,7 @@
/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/06/15 11:05:34 by charles #+# #+# */
-/* Updated: 2020/08/27 17:06:45 by charles ### ########.fr */
+/* Updated: 2020/08/28 10:22:02 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -56,8 +56,8 @@ bool redir_extract(
}
if ((filename = preprocess_filename(&redirs->next, env)) == NULL)
{
- ft_lstdestroy((t_ftlst**)&redirs, free);
- ft_lstdestroy((t_ftlst**)&after, free);
+ /* ft_lstdestroy((t_ftlst**)&redirs, free); */
+ /* ft_lstdestroy((t_ftlst**)&after, free); */
return (false);
}
if ((redirs->tag == TAG_REDIR_IN
@@ -67,11 +67,11 @@ bool redir_extract(
|| (redirs->tag == TAG_REDIR_APPEND
&& !st_open_replace(&fds[FDS_WRITE], filename, O_WRONLY | O_CREAT | O_APPEND)))
{
- ft_lstdestroy((t_ftlst**)&redirs, free);
- ft_lstdestroy((t_ftlst**)&after, free);
+ /* ft_lstdestroy((t_ftlst**)&redirs, free); */
+ /* ft_lstdestroy((t_ftlst**)&after, free); */
return (false);
}
- ft_lstdestroy((t_ftlst**)&redirs, free);
+ /* ft_lstdestroy((t_ftlst**)&redirs, free); */
free(filename);
return (redir_extract(after, env, fds));
}
diff --git a/src/lexer/lexer.c b/src/lexer/lexer.c
index 7f49431..3c7328e 100644
--- a/src/lexer/lexer.c
+++ b/src/lexer/lexer.c
@@ -6,7 +6,7 @@
/* By: nahaddac <nahaddac@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/07/16 08:18:25 by nahaddac #+# #+# */
-/* Updated: 2020/08/27 20:45:59 by charles ### ########.fr */
+/* Updated: 2020/08/28 10:43:42 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -110,8 +110,8 @@ enum e_tok token_check_stick(t_tok_lst *tok)
i = ft_strlen(tok->content);
if (i > 0)
if (tok->content[i - 1] == ' ')
- return(tok->tag);
- return(tok->tag | TAG_STICK);
+ return (tok->tag);
+ return (tok->tag | TAG_STICK);
}
enum e_tok token_str_or_quote(t_tok_lst *tok)
@@ -119,22 +119,22 @@ enum e_tok token_str_or_quote(t_tok_lst *tok)
int i;
i = 0;
- while(tok->content[i] != '\0')
+ while (tok->content[i] != '\0')
{
- if(tok->content[i] == '\'')
+ if (tok->content[i] == '\'')
{
tok->tag = TAG_STR_SINGLE;
- return(token_check_stick(tok));
+ return (token_check_stick(tok));
}
- if(tok->content[i] == '"')
+ if (tok->content[i] == '"')
{
tok->tag = TAG_STR_DOUBLE;
- return(token_check_stick(tok));
+ return (token_check_stick(tok));
}
else
{
tok->tag = TAG_STR;
- return(token_check_stick(tok));
+ return (token_check_stick(tok));
}
i++;
}
diff --git a/src/lexer/tok_lst.c b/src/lexer/tok_lst.c
index a620aa5..a746794 100644
--- a/src/lexer/tok_lst.c
+++ b/src/lexer/tok_lst.c
@@ -59,7 +59,7 @@ t_tok_lst *tok_lst_last(t_tok_lst *tokens)
return ((t_tok_lst*)ft_lstlast((t_ftlst*)tokens));
}
-t_tok_lst *tok_lst_pop_front(t_tok_lst **tokens)
+t_tok_lst *tok_lst_uncons(t_tok_lst **tokens)
{
t_tok_lst *poped;
diff --git a/src/main.c b/src/main.c
index 41a4967..e7326de 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/08/27 20:44:44 by charles ### ########.fr */
+/* Updated: 2020/08/28 10:45:33 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -87,7 +87,7 @@ int main(int argc, char **argv, char **envp)
if (argc == 3 && ft_strcmp(argv[1], "-l") == 0)
{
- t_tok_lst *lex_out = lexer(ft_strdup(argv[2]));
+ t_tok_lst *lex_out = lexer(argv[2]);
if (lex_out == NULL)
return (1);
tok_lst_debug(lex_out);
@@ -95,12 +95,10 @@ int main(int argc, char **argv, char **envp)
}
else if (argc == 3 && ft_strcmp(argv[1], "-c") == 0)
{
- t_tok_lst *lex_out = lexer(ft_strdup(argv[2]));
+ t_tok_lst *lex_out = lexer(argv[2]);
if (lex_out == NULL)
return (1);
- /* ft_lstiter(lex_out, token_debug); */
-
t_parsed *parser_out = parse(lex_out);
if (parser_out == NULL || parser_out->syntax_error)
return (1);
diff --git a/src/parser/parsed.c b/src/parser/parsed.c
index 0c8eba6..4e6d6f2 100644
--- a/src/parser/parsed.c
+++ b/src/parser/parsed.c
@@ -6,7 +6,7 @@
/* By: charles <me@cacharle.xyz> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/08/27 20:27:42 by charles #+# #+# */
-/* Updated: 2020/08/27 20:31:11 by charles ### ########.fr */
+/* Updated: 2020/08/28 10:44:39 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -37,4 +37,3 @@ t_parsed *parsed_error(const char *format, ...)
va_end(ap);
return (err);
}
-
diff --git a/src/parser/parser.c b/src/parser/parser.c
index ae484ac..a0c76d2 100644
--- a/src/parser/parser.c
+++ b/src/parser/parser.c
@@ -6,7 +6,7 @@
/* By: nahaddac <nahaddac@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/06/17 18:09:04 by nahaddac #+# #+# */
-/* Updated: 2020/08/27 21:12:21 by charles ### ########.fr */
+/* Updated: 2020/08/28 10:40:03 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -17,27 +17,31 @@
#include "parser.h"
-t_parsed *parse_redir(t_tok_lst *input, t_tok_lst **redirs)
+/*
+** push redirection token
+** push while token is str and stick
+** push last str token
+*/
+
+t_parsed *parse_redir(t_tok_lst *input, t_tok_lst **redirs)
{
- tok_lst_push_back(redirs, tok_lst_pop_front(&input));
- /* input = input->next; */
+ tok_lst_push_back(redirs, tok_lst_uncons(&input));
if (input == NULL)
return (parsed_error("syntax error near unexpected token `newline'\n"));
- while(input != NULL
- && input->next != NULL
- && input->next->tag & TAG_IS_STR
- && input->tag & TAG_IS_STR && input->tag & TAG_STICK)
- {
- tok_lst_push_back(redirs, tok_lst_pop_front(&input));
- /* input = input->next; */
- }
- if (input != NULL && !(input->tag & TAG_IS_STR))
+ // FIXME big condition could be avoided with lexer not putting STICK if the next tokens isn't a string
+ while (input != NULL
+ && input->tag & TAG_IS_STR && input->tag & TAG_STICK
+ && input->next != NULL && input->next->tag & TAG_IS_STR)
+ tok_lst_push_back(redirs, tok_lst_uncons(&input));
+ if (input == NULL)
+ return (NULL);
+ if (!(input->tag & TAG_IS_STR))
return (parsed_error("syntax error near unexpected token `%s'\n", input->content));
- tok_lst_push_back(redirs, tok_lst_pop_front(&input));
+ tok_lst_push_back(redirs, tok_lst_uncons(&input));
return (parsed_new(NULL, input));
}
-t_parsed *parse_cmd(t_tok_lst *input)
+t_parsed *parse_cmd(t_tok_lst *input)
{
t_ast *ast;
t_parsed *tmp;
@@ -49,7 +53,7 @@ t_parsed *parse_cmd(t_tok_lst *input)
while (input != NULL)
{
if (input->tag & TAG_IS_STR)
- tok_lst_push_back(&ast->cmd_argv, tok_lst_pop_front(&input));
+ tok_lst_push_back(&ast->cmd_argv, tok_lst_uncons(&input));
else if (input->tag & TAG_IS_REDIR)
{
tmp = parse_redir(input, &ast->redirs);
@@ -60,95 +64,75 @@ t_parsed *parse_cmd(t_tok_lst *input)
else
break;
}
- return parsed_new(ast, input);
+ return (parsed_new(ast, input));
}
-// <cmd> ::= (<string> | <redir>)+
-// <op> ::= <expr> <sep> <op> | <expr>
-// <expr> ::= '(' <op> ')' | <cmd>
+/*
+** parse and operation (| ; && ||)
+*/
-t_parsed *parse_op(t_tok_lst *input)
+t_parsed *parse_op(t_tok_lst *input)
{
t_ast *ast;
- t_parsed *left_parsed;
- t_parsed *right_parsed;
- enum e_tok tag;
+ t_parsed *left;
+ t_parsed *right;
+ enum e_tok sep_tag;
- left_parsed = parse_expr(input);
- if (left_parsed == NULL || left_parsed->syntax_error)
- return left_parsed;
- input = left_parsed->rest;
+ left = parse_expr(input);
+ if (left == NULL || left->syntax_error)
+ return (left);
+ input = left->rest;
if (input == NULL || input->tag & TAG_PARENT_CLOSE)
- return parsed_new(left_parsed->ast, input);
-
- tag = input->tag;
- if (!(tag & TAG_IS_SEP))
+ return (parsed_new(left->ast, input));
+ sep_tag = input->tag;
+ if (!(sep_tag & TAG_IS_SEP))
return (parsed_error("syntax error near unexpected token `%s'\n", input->content));
- input = input->next;
+ ft_lstpop_front((t_ftlst**)&input, free);
if (input == NULL)
return (parsed_error("syntax error expected token\n"));
- right_parsed = parse_op(input);
- if (right_parsed == NULL || right_parsed->syntax_error)
- return right_parsed;
- ast = ast_new(AST_OP);
- ast->op.left = left_parsed->ast;
- ast->op.right = right_parsed->ast;
- ast->op.sep = tag;
- return parsed_new(ast, right_parsed->rest);
+ right = parse_op(input);
+ if (right == NULL || right->syntax_error)
+ return (right);
+ if ((ast = ast_new(AST_OP)) == NULL)
+ return (NULL);
+ ast->op.left = left->ast;
+ ast->op.right = right->ast;
+ ast->op.sep = sep_tag;
+ return (parsed_new(ast, right->rest));
}
-t_parsed *parse_expr(t_tok_lst *input)
+/*
+** try to parse parenthesis, if fail parse a command.
+** parenthesis can be followed by redirections
+*/
+
+t_parsed *parse_expr(t_tok_lst *input)
{
t_parsed *tmp;
- enum e_tok tag;
- t_ast *new_ast;
+ t_ast *ast;
- tag = input->tag;
- if (tag & TAG_PARENT_OPEN)
+ if (input->tag & TAG_PARENT_OPEN)
{
if (!(tmp = parse_op(input->next)) || tmp->syntax_error)
- return tmp;
+ return (tmp);
input = tmp->rest;
- if (input == NULL)
- return (parsed_error("syntax error expected token\n"));
- tag = input->tag;
- if (!(tag & TAG_PARENT_CLOSE))
+ if (input == NULL || !(input->tag & TAG_PARENT_CLOSE))
return (parsed_error("syntax error expected token\n"));
input = input->next;
- new_ast = ast_new(AST_PARENT);
- new_ast->parent_ast = tmp->ast;
- tmp->ast = new_ast;
- if (input == NULL)
- return tmp;
- // could reuse parse_redir instead
- tag = input->tag;
- while (tag & TAG_IS_REDIR)
- {
- while(input != NULL)
- {
- tag = input->tag;
- tok_lst_push_back(&tmp->ast->redirs, tok_lst_pop_front(&input));
- if (tag & TAG_IS_STR && tag & TAG_STICK)
- input = input->next;
- else if (tag & TAG_IS_REDIR)
- input = input->next;
- else
- break;
- }
- if (input == NULL)
- break;
- input = input->next;
- tag = input->tag;
- }
+ ast = ast_new(AST_PARENT);
+ ast->parent_ast = tmp->ast;
+ tmp->ast = ast;
+ while (input != NULL && input->tag & TAG_IS_REDIR)
+ parse_redir(input, &tmp->ast->redirs);
tmp->rest = input;
- return tmp;
+ return (tmp);
}
- return parse_cmd(input);
+ return (parse_cmd(input));
}
-t_parsed *parse(t_tok_lst *input)
+t_parsed *parse(t_tok_lst *input)
{
if (input == NULL)
- return NULL;
+ return (NULL);
return (parse_op(input));
}