From ea3195deefdc728b7fe8268bfeab82dc85415111 Mon Sep 17 00:00:00 2001 From: Christopher Uhlig Date: Fri, 9 Aug 2024 11:45:22 +0200 Subject: [PATCH] shorten version with bugg --- src/tokenizer.c | 187 ++++++++++++++++++++++++++++-------------------- 1 file changed, 110 insertions(+), 77 deletions(-) diff --git a/src/tokenizer.c b/src/tokenizer.c index 40d54c8..fd4fefd 100644 --- a/src/tokenizer.c +++ b/src/tokenizer.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/28 20:55:50 by chuhlig #+# #+# */ -/* Updated: 2024/08/08 18:50:53 by chuhlig ### ########.fr */ +/* Updated: 2024/08/09 11:43:51 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,35 +15,30 @@ void print_token(t_token *token) { - if (DEBUG) + if (token->type == STRING_TOKEN) { - if (token->type == STRING_TOKEN) - { - printf("STRING_TOKEN: %s\n", token->content.string); - } - else if (token->type == REDIR_TOKEN) - { - printf("REDIR_TOKEN: %d\n", token->content.redir_type); - } - else if (token->type == PIPE_TOKEN) - { - printf("PIPE_TOKEN\n"); - } - else if (token->type == NEWLINE_TOKEN) - { - printf("NEWLINE_TOKEN\n"); - } + printf("STRING_TOKEN: %s\n", token->content.string); + } + else if (token->type == REDIR_TOKEN) + { + printf("REDIR_TOKEN: %d\n", token->content.redir_type); + } + else if (token->type == PIPE_TOKEN) + { + printf("PIPE_TOKEN\n"); + } + else if (token->type == NEWLINE_TOKEN) + { + printf("NEWLINE_TOKEN\n"); } } - -void conditional_print(char *string, int start_of_string, int i, - t_token **token_list) +void conditional_print(char *string, int start_of_string, int i, t_token **token_list) { char *line; int len; - len = i - start_of_string + 1; + len = i - start_of_string; if (len > 0) { line = (char *)malloc(len + 1); @@ -63,79 +58,117 @@ void conditional_print(char *string, int start_of_string, int i, } } +void handle_special_chars(char *s, int *i, int *start, t_token **token_list) +{ + conditional_print(s, *start, *i, token_list); // Pass correct boundaries + if (s[*i] == '<' && s[*i + 1] == '<') + *token_list = new_redir_token(INPUT_LIMITER, *token_list, NULL); + else if (s[*i] == '>' && s[*i + 1] == '>') + *token_list = new_redir_token(OUTPUT_APPEND, *token_list, NULL); + else if (s[*i] == '<') + *token_list = new_redir_token(INPUT_FILE, *token_list, NULL); + else if (s[*i] == '>') + *token_list = new_redir_token(OUTPUT_OVERRIDE, *token_list, NULL); + else if (s[*i] == '|') + *token_list = new_token(PIPE_TOKEN, *token_list, NULL); + else if (s[*i] == '\n') + *token_list = new_token(NEWLINE_TOKEN, *token_list, NULL); + print_token(*token_list); + if (s[*i] == '<' || s[*i] == '>') + (*i)++; + *start = *i + 1; +} + void tokenizer(char *s, t_token **token_list) { - char *quotes; char quote_check; - int start_of_string; - int ignore_space; + int pos; int i; + int f; - quotes = "\"\'"; - quote_check = '\0'; - start_of_string = 0; - ignore_space = 0; - i = 0; - if (!s || !*s) - return ; - while (s[i]) + f = 0; + i = -1; + pos = 0; + while (s[++i]) { - if (!ignore_space && (s[i] == '|' || s[i] == '\n' || s[i] == '<' || s[i] == '>')) - { - conditional_print(s, start_of_string, i - 1, token_list); - if ((s[i] == '<' || s[i] == '>') && s[i + 1] == s[i]) - { - if (s[i] == '<') - *token_list = new_redir_token(INPUT_LIMITER, *token_list, NULL); - else - *token_list = new_redir_token(OUTPUT_APPEND, *token_list, NULL); - i++; - } - else - { - if (s[i] == '<') - *token_list = new_redir_token(INPUT_FILE, *token_list, NULL); - else if (s[i] == '|') - *token_list = new_token(PIPE_TOKEN, *token_list, NULL); - else if (s[i] == '\n') - *token_list = new_token(NEWLINE_TOKEN, *token_list, NULL); - else - *token_list = new_redir_token(OUTPUT_OVERRIDE, *token_list, NULL); - } - print_token(*token_list); - start_of_string = i + 1; - } - else if (ignore_space && s[i] == quote_check) - { - quote_check = '\0'; - ignore_space = 0; - } - else if (!ignore_space && ft_strchr(quotes, s[i])) + if (!f && ft_strchr("|<>\\n", s[i])) + handle_special_chars(s, &i, &pos, token_list); + else if (f && s[i] == quote_check) + f = 0; + else if (!f && ft_strchr("\'\"", s[i])) { + f = 1; quote_check = s[i]; - ignore_space = 1; } - else if ((!ignore_space && (s[i] == ' ' || s[i] == '\t')) || i == ft_strlen(s) - 1) + if ((!f && (s[i] == ' ' || s[i] == '\t')) || s[i + 1] == '\0') { - conditional_print(s, start_of_string, i, token_list); - start_of_string = i + 1; + conditional_print(s, pos, i, token_list); + pos = i + 1; } - i++; } } +// Minishell $ echo "Hello World"|grep 'Hello|cat -e +// STRING_TOKEN: echo +// STRING_TOKEN: "Hello World" +// PIPE_TOKEN +// STRING_TOKEN: grep +// STRING_TOKEN: 'Hello|cat -e +// Minishell $ -// Minishell $ |abc|cba +// Minishell $ Minishell $ echo "Hello World"|grep 'Hello|cat -e +// STRING_TOKEN: echo +// STRING_TOKEN: "Hello World" +// PIPE_TOKEN +// STRING_TOKEN: grep +// STRING_TOKEN: 'Hello|cat -e +// Minishell $ +// STRING_TOKEN: Mi +// STRING_TOKEN: Mi +// STRING_TOKEN: ishell +// STRING_TOKEN: $ +// STRING_TOKEN: echo +// STRING_TOKEN: "Hello World" +// PIPE_TOKEN +// STRING_TOKEN: grep +// STRING_TOKEN: 'Hello|cat -e +// STRING_TOKEN: echo +// STRING_TOKEN: "Hello World" +// PIPE_TOKEN +// STRING_TOKEN: grep +// STRING_TOKEN: 'Hello // PIPE_TOKEN -// STRING_TOKEN: abc +// STRING_TOKEN: cat +// STRING_TOKEN: -e +// Mi +// STRING_TOKEN: -e +// Mi +// STRING_TOKEN: ishell +// STRING_TOKEN: $ +// Minishell $ echo "Hello World"|grep 'Hello|cat -e +// STRING_TOKEN: echo +// STRING_TOKEN: "Hello World" // PIPE_TOKEN -// STRING_TOKEN: cba -// Minishell $ ||abc a||cba +// STRING_TOKEN: grep +// STRING_TOKEN: 'Hello|cat -e +// Minishell $ echo "Hello World"|grep 'Hello|cat -e +// STRING_TOKEN: echo +// STRING_TOKEN: "Hello World" // PIPE_TOKEN +// STRING_TOKEN: grep +// STRING_TOKEN: 'Hello|cat -e +// Minishell $ echo "Hello World"|grep 'Hello|cat -e +// STRING_TOKEN: echo +// STRING_TOKEN: "Hello World" // PIPE_TOKEN -// STRING_TOKEN: abc -// STRING_TOKEN: a +// STRING_TOKEN: grep +// STRING_TOKEN: 'Hello|cat -e +// Minishell $ echo "Hello World"|grep 'Hello'|cat -e +// STRING_TOKEN: echo +// STRING_TOKEN: "Hello World" // PIPE_TOKEN +// STRING_TOKEN: grep +// STRING_TOKEN: 'Hello' // PIPE_TOKEN -// STRING_TOKEN: cba -// Minishell $ \ No newline at end of file +// STRING_TOKEN: cat +// STRING_TOKEN: -e \ No newline at end of file -- 2.47.2