aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/debug.c2
-rw-r--r--src/eval/cmd.c5
-rw-r--r--src/lexer/lexer.c8
-rw-r--r--src/main.c29
-rw-r--r--src/parser/parser.c53
-rw-r--r--src/preprocess.c11
-rw-r--r--src/utils.c10
7 files changed, 80 insertions, 38 deletions
diff --git a/src/debug.c b/src/debug.c
index 42c34eb..9743e5a 100644
--- a/src/debug.c
+++ b/src/debug.c
@@ -6,7 +6,7 @@
/* By: charles <me@cacharle.xyz> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/09/16 15:58:35 by charles #+# #+# */
-/* Updated: 2020/10/06 13:14:44 by cacharle ### ########.fr */
+/* Updated: 2020/10/08 11:37:57 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdio.h>
diff --git a/src/eval/cmd.c b/src/eval/cmd.c
index 242c7a5..c786a7b 100644
--- a/src/eval/cmd.c
+++ b/src/eval/cmd.c
@@ -6,7 +6,7 @@
/* By: charles <charles@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/06/14 10:41:31 by charles #+# #+# */
-/* Updated: 2020/10/07 18:12:00 by cacharle ### ########.fr */
+/* Updated: 2020/10/08 10:15:07 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -43,7 +43,10 @@ int eval_cmd(int fds[2], t_env env, t_ast *ast)
/* printf("%p\n", ast->cmd_argv); */
/* ast->cmd_argv = NULL; */
if (argv[0] == NULL)
+ {
+ ft_split_destroy(argv);
return (0);
+ }
param.builtin = builtin_search_func(argv[0]);
if (param.builtin != NULL && !param.builtin->forked)
{
diff --git a/src/lexer/lexer.c b/src/lexer/lexer.c
index 5e0600d..466e360 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/09/17 13:19:40 by nahaddac ### ########.fr */
+/* Updated: 2020/10/08 09:21:55 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -71,7 +71,7 @@ int tok_len(char *input)
** get the number of character for the current token
** create a token from a substring in input
** assign a tag to the token
-** return all token
+** return all token
*/
t_tok_lst *create_token_list(char *input, t_tok_lst **lst)
@@ -90,8 +90,10 @@ t_tok_lst *create_token_list(char *input, t_tok_lst **lst)
tok->tag = tok_assign_tag(tok->content);
if (tok->tag == 0)
tok->tag = tok_assign_str(tok);
- if (!ft_isblank(tok->content[0])) // ?
+ if (!ft_isblank(tok->content[0]))
tok_lst_push_back(lst, tok);
+ else
+ tok_lst_destroy(&tok, free);
i += j;
}
return (*lst);
diff --git a/src/main.c b/src/main.c
index 05fc0be..c00c6ff 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/10/07 16:01:29 by cacharle ### ########.fr */
+/* Updated: 2020/10/08 11:36:46 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -21,6 +21,7 @@
#include "eval.h"
int debug_lexer(char *input);
+int debug_parser(char *input);
/*
** TODO
@@ -40,10 +41,17 @@ int execute(t_env env, char *input)
status = lexer(input, &lexer_out);
if (status != 0)
+ {
+ tok_lst_destroy(&lexer_out, free);
return (status);
+ }
parser_out = parse(lexer_out);
if (parser_out == NULL || parser_out->syntax_error)
+ {
+ ast_destroy(parser_out->ast);
+ free(parser_out);
return (2);
+ }
fds[0] = FD_NONE;
fds[1] = FD_NONE;
status = eval(fds, env, parser_out->ast);
@@ -103,7 +111,6 @@ int main(int argc, char **argv, char **envp)
int main(int argc, char **argv, char **envp)
{
- int status;
t_env env;
if ((env = env_from_array(envp)) == NULL)
@@ -111,16 +118,14 @@ int main(int argc, char **argv, char **envp)
setup(argv[0], env);
g_state.last_status = 0;
if (argc == 3 && ft_strcmp(argv[1], "-l") == 0)
- return (debug_lexer(argv[2]));
- if (argc == 3 && ft_strcmp(argv[1], "-p") == 0)
- return (debug_parser(argv[2]));
- if (argc == 3 && ft_strcmp(argv[1], "-c") == 0)
- {
- status = execute(env, argv[2]);
- ft_vecdestroy(env, free);
- return (status);
- }
- repl(env);
+ g_state.last_status = debug_lexer(argv[2]);
+ else if (argc == 3 && ft_strcmp(argv[1], "-p") == 0)
+ g_state.last_status = debug_parser(argv[2]);
+ else if (argc == 3 && ft_strcmp(argv[1], "-c") == 0)
+ g_state.last_status = execute(env, argv[2]);
+ else
+ repl(env);
+ ft_vecdestroy(env, free);
return (g_state.last_status);
}
diff --git a/src/parser/parser.c b/src/parser/parser.c
index 6ab82be..25e9531 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/10/07 16:43:08 by cacharle ### ########.fr */
+/* Updated: 2020/10/08 12:29:52 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -47,7 +47,11 @@ t_parsed *parse_redir(t_tok_lst *input, t_tok_lst **redirs)
if (input == NULL)
return (parsed_error("syntax error near unexpected token `newline'\n"));
if (!(input->tag & TAG_IS_STR))
- return (parsed_error("syntax error near unexpected token `%s'\n", input->content));
+ {
+ t_parsed *ret = parsed_error("syntax error near unexpected token `%s'\n", input->content);
+ tok_lst_destroy(&input, free);
+ return (ret);
+ }
tok_lst_push_back(redirs, tok_lst_uncons(&input));
return (parsed_new(NULL, input));
}
@@ -58,7 +62,11 @@ t_parsed *parse_cmd(t_tok_lst *input)
t_parsed *tmp;
if (input->tag & TAG_IS_SEP || input->tag == TAG_PIPE)
- return (parsed_error("syntax error near unexpected token `%s'\n", input->content));
+ {
+ t_parsed *ret = parsed_error("syntax error near unexpected token `%s'\n", input->content);
+ tok_lst_destroy(&input, free);
+ return (ret);
+ }
if ((ast = ast_new(AST_CMD)) == NULL)
return (NULL);
while (input != NULL)
@@ -69,7 +77,11 @@ t_parsed *parse_cmd(t_tok_lst *input)
{
tmp = parse_redir(input, &ast->redirs);
if (tmp == NULL || tmp->syntax_error)
+ {
+ tok_lst_destroy(&tmp->rest, free);
+ ast_destroy(ast);
return (tmp);
+ }
input = tmp->rest;
free(tmp);
}
@@ -89,33 +101,32 @@ t_parsed *parse_pipeline(t_tok_lst *input)
if (expr == NULL || expr->syntax_error ||
expr->rest == NULL || expr->rest->tag != TAG_PIPE)
return (expr);
- if (expr->rest->next == NULL)
- return (parsed_error("syntax error expected token\n"));
ft_lstpop_front(&expr->rest, free);
+ if (expr->rest == NULL)
+ {
+ ast_destroy(expr->ast);
+ free(expr);
+ /* tok_lst_destroy(expr->rest, free); */
+ return (parsed_error("syntax error expected token\n"));
+ }
tail = parse_pipeline(expr->rest);
if (tail == NULL || tail->syntax_error)
+ {
+ ast_destroy(expr->ast);
+ free(expr);
return (tail);
+ }
expr_ast = expr->ast;
free(expr);
t_ast *pipeline_ast;
if (tail->ast->tag == AST_CMD || tail->ast->tag == AST_PARENT)
{
pipeline_ast = ast_new(AST_PIPELINE);
- if ((pipeline_ast->pipeline = ft_lstnew(tail->ast)) == NULL)
- {
- /* ast_destroy(expr_ast); */
- /* ft_lstdestroy(&tail, NULL); */
- return (NULL);
- }
+ try(pipeline_ast->pipeline = ft_lstnew(tail->ast));
}
else
pipeline_ast = tail->ast;
- if (ft_lstpush_front_node(&pipeline_ast->pipeline, expr_ast) == NULL)
- {
- /* ast_destroy(expr_ast); */
- /* ft_lstdestroy(&tail, NULL); */
- return (NULL);
- }
+ try(ft_lstpush_front_node(&pipeline_ast->pipeline, expr_ast));
tail->ast = pipeline_ast;
return (tail);
}
@@ -145,10 +156,18 @@ t_parsed *parse_op(t_tok_lst *input)
if (input == NULL && sep_tag == TAG_END)
return (left);
if (input == NULL)
+ {
+ ast_destroy(left->ast);
+ free(left);
return (parsed_error("syntax error expected token\n"));
+ }
right = parse_op(input);
if (right == NULL || right->syntax_error)
+ {
+ ast_destroy(left->ast);
+ free(left);
return (right);
+ }
if ((ast = ast_new(AST_OP)) == NULL)
return (NULL);
ast->op.left = left->ast;
diff --git a/src/preprocess.c b/src/preprocess.c
index 6204dc2..7f37a97 100644
--- a/src/preprocess.c
+++ b/src/preprocess.c
@@ -6,7 +6,7 @@
/* By: charles <charles@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/04/03 08:58:49 by charles #+# #+# */
-/* Updated: 2020/10/07 16:50:42 by cacharle ### ########.fr */
+/* Updated: 2020/10/08 09:58:48 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -118,6 +118,7 @@ size_t interpolate(char *str, size_t i, t_tok_lst **curr_addr, enum e_tok prev_t
if (curr->tag & TAG_STR_DOUBLE)
{
curr->content = ft_strjoin3(before, match, after);
+ free(before);
return i + ft_strlen(match);
}
if (curr->tag & TAG_STR)
@@ -139,6 +140,8 @@ size_t interpolate(char *str, size_t i, t_tok_lst **curr_addr, enum e_tok prev_t
else if (fields->next == NULL)
{
curr->content = ft_strjoin3(before, fields->content, after);
+ free(before);
+ tok_lst_destroy(&fields, free);
return i + len;
}
else
@@ -146,13 +149,15 @@ size_t interpolate(char *str, size_t i, t_tok_lst **curr_addr, enum e_tok prev_t
last = tok_lst_last(fields);
last->tag = curr->tag;
curr->tag = TAG_STR;
- curr->content = ft_strjoin(before, fields->content);
- last->content = ft_strjoin(last->content, after);
+ curr->content = ft_strjoinf_snd(before, fields->content);
+ last->content = ft_strjoinf_fst(last->content, after);
+ free(before);
t_tok_lst *tmp = curr->next;
curr->next = fields->next;
(*curr_addr) = last;
(*curr_addr)->next = tmp;
+ free(fields);
return len;
}
}
diff --git a/src/utils.c b/src/utils.c
index b9231c5..b816a3f 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -6,7 +6,7 @@
/* By: cacharle <cacharle@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/28 11:56:31 by cacharle #+# #+# */
-/* Updated: 2020/09/15 17:42:00 by charles ### ########.fr */
+/* Updated: 2020/10/08 11:31:05 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -35,3 +35,11 @@ void print_prompt(void)
ft_putstr_fd(cwd, STDERR_FILENO);
ft_putstr_fd("\033[0m$ ", STDERR_FILENO);
}
+
+void try(void *ptr)
+{
+ if (ptr != NULL)
+ return ;
+ ft_putendl_fd(STDERR_FILENO, "minishell: fatal error");
+ exit(3);
+}