From 7265a6c340effec6a208480a364bee77b2ac828d Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Thu, 2 Feb 2017 15:45:55 +0100 Subject: [PATCH] cleaner main, added ft_lexer to encapsulate all phases of tokenization --- 42sh/Makefile | 1 + 42sh/includes/lexer.h | 6 ++++-- 42sh/src/exec/exec_command.c | 2 +- 42sh/src/job-control/process_free.c | 6 +----- 42sh/src/lexer/ft_lexer.c | 28 ++++++++++++++++++++++++++++ 42sh/src/lexer/ft_post_tokenize.c | 2 +- 42sh/src/lexer/ft_tokenize.c | 2 +- 42sh/src/main/main.c | 15 +++++---------- 42sh/src/parser/ft_parse.c | 2 +- 9 files changed, 43 insertions(+), 21 deletions(-) create mode 100644 42sh/src/lexer/ft_lexer.c diff --git a/42sh/Makefile b/42sh/Makefile index b20dd88f..8d286750 100644 --- a/42sh/Makefile +++ b/42sh/Makefile @@ -116,6 +116,7 @@ job-control/sigtstp_handler.c\ job-control/sigttin_handler.c\ job-control/sigttou_handler.c\ lexer/command_getoutput.c\ +lexer/ft_lexer.c\ lexer/ft_post_tokenize.c\ lexer/ft_tokenize.c\ lexer/get_lexer_state.c\ diff --git a/42sh/includes/lexer.h b/42sh/includes/lexer.h index 8efb7a2b..0b4f3424 100644 --- a/42sh/includes/lexer.h +++ b/42sh/includes/lexer.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/01 12:15:50 by jhalford #+# #+# */ -/* Updated: 2017/02/02 15:14:58 by jhalford ### ########.fr */ +/* Updated: 2017/02/02 15:36:09 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -73,9 +73,11 @@ typedef enum e_lexstate t_lexstate; extern int (*g_lexer[])(t_list **alst, char *str); -t_token *token_init(); +int ft_lexer(t_list **alst, char **str); int ft_tokenize(t_list **alst, char *str, t_lexstate state); int ft_post_tokenize(t_list **alst, char **str); + +t_token *token_init(); int token_append(t_token *token, char c, short int esc); void token_free(void *data, size_t size); int token_cmp_type(t_token *token, t_type *ref); diff --git a/42sh/src/exec/exec_command.c b/42sh/src/exec/exec_command.c index 035558a5..e0f5e71b 100644 --- a/42sh/src/exec/exec_command.c +++ b/42sh/src/exec/exec_command.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/14 17:28:14 by jhalford #+# #+# */ -/* Updated: 2017/02/02 14:49:22 by jhalford ### ########.fr */ +/* Updated: 2017/02/02 15:45:32 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/job-control/process_free.c b/42sh/src/job-control/process_free.c index 6910a7c2..f0f77c0a 100644 --- a/42sh/src/job-control/process_free.c +++ b/42sh/src/job-control/process_free.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/12 12:41:11 by jhalford #+# #+# */ -/* Updated: 2017/02/02 14:34:40 by jhalford ### ########.fr */ +/* Updated: 2017/02/02 15:45:25 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,12 +17,8 @@ void process_free(void *content, size_t content_size) t_process *p; (void)content_size; - DG("check 0"); p = content; - DG("check 1"); ft_strdel(&p->path); - DG("check 2"); ft_sstrfree(p->av); - DG("check 3"); free(p); } diff --git a/42sh/src/lexer/ft_lexer.c b/42sh/src/lexer/ft_lexer.c new file mode 100644 index 00000000..a0abdd90 --- /dev/null +++ b/42sh/src/lexer/ft_lexer.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lexer.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/02/02 15:30:59 by jhalford #+# #+# */ +/* Updated: 2017/02/02 15:42:24 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "lexer.h" + +int ft_lexer(t_list **alst, char **command) +{ + int ret; + + ret = 0; + if (!*command) + ret = 1; + else if (ft_tokenize(alst, *command, DEFAULT)) + ret = 1; + else if (ft_post_tokenize(alst, command)) + ret = 1; + ft_strdel(command); + return (ret); +} diff --git a/42sh/src/lexer/ft_post_tokenize.c b/42sh/src/lexer/ft_post_tokenize.c index f95e1cb0..f88d45b5 100644 --- a/42sh/src/lexer/ft_post_tokenize.c +++ b/42sh/src/lexer/ft_post_tokenize.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/11 16:11:11 by jhalford #+# #+# */ -/* Updated: 2017/02/02 15:22:34 by jhalford ### ########.fr */ +/* Updated: 2017/02/02 15:29:57 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/lexer/ft_tokenize.c b/42sh/src/lexer/ft_tokenize.c index 8abae3d3..a2334864 100644 --- a/42sh/src/lexer/ft_tokenize.c +++ b/42sh/src/lexer/ft_tokenize.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/10 13:37:11 by jhalford #+# #+# */ -/* Updated: 2017/02/02 14:55:43 by jhalford ### ########.fr */ +/* Updated: 2017/02/02 15:34:45 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/main/main.c b/42sh/src/main/main.c index 2df0292f..8fb8d026 100644 --- a/42sh/src/main/main.c +++ b/42sh/src/main/main.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/06 18:40:58 by jhalford #+# #+# */ -/* Updated: 2017/02/02 15:22:00 by jhalford ### ########.fr */ +/* Updated: 2017/02/02 15:45:17 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,23 +19,18 @@ int shell_single_command(char *command) token = NULL; ast = NULL; - if (!command) - return (0); + DG("{inv}{mag}got command '%s'", command); - if (ft_tokenize(&token, command, DEFAULT)) + if (ft_lexer(&token, &command) || !token) return (1); - if (ft_post_tokenize(&token, &command)) - return (1); - if (!token) - return (0); - ft_strdel(&command); + token_print(token); if (ft_parse(&ast, &token)) return (1); + btree_print(STDBUG, ast, &ft_putast); if (ft_exec(&ast)) return (1); - DG("after exec!"); return (0); } diff --git a/42sh/src/parser/ft_parse.c b/42sh/src/parser/ft_parse.c index d1a334c6..beb0cfaa 100644 --- a/42sh/src/parser/ft_parse.c +++ b/42sh/src/parser/ft_parse.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/30 17:14:58 by jhalford #+# #+# */ -/* Updated: 2017/01/31 16:10:41 by wescande ### ########.fr */ +/* Updated: 2017/02/02 15:45:33 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */