From: Lukas Jiriste Date: Sat, 31 Aug 2024 10:00:44 +0000 (+0200) Subject: Simplify tokenization X-Git-Url: https://git.ljiriste.work/?a=commitdiff_plain;h=9f0aeecea853a5340499c225a4f5232fa1d5b58f;p=42%2Fminishell.git Simplify tokenization Because minishell should error out when unpaired quotes are encountered, the handling is unnecessary as is the input of char** instead of char*. --- diff --git a/inc/minishell.h b/inc/minishell.h index be6479a..92318d8 100644 --- a/inc/minishell.h +++ b/inc/minishell.h @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/02 13:22:57 by ljiriste #+# #+# */ -/* Updated: 2024/08/31 10:04:30 by ljiriste ### ########.fr */ +/* Updated: 2024/08/31 11:59:16 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -33,7 +33,7 @@ int add_conformant(t_vec *expanded, t_wildcard_info *info, char quote); int expand_dir(t_vec *expanded, t_wildcard_info *info); int expand_wildcards(char **input, const t_execution_env *env); -int tokenize(char **line, t_vec *tokens); +int tokenize(char *line, t_vec *tokens); int parse(t_vec *tokens, t_tree **parse_tree); int execute(t_tree *parse_tree, t_execution_env *env); diff --git a/src/input_handling.c b/src/input_handling.c index 554306e..5347db0 100644 --- a/src/input_handling.c +++ b/src/input_handling.c @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/03 09:00:00 by ljiriste #+# #+# */ -/* Updated: 2024/08/29 16:45:00 by ljiriste ### ########.fr */ +/* Updated: 2024/08/31 11:58:46 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -29,7 +29,7 @@ void handle_input(char **input, t_execution_env *env) g_last_signal = 0; return ; } - res = res || tokenize(input, &tokens); + res = res || tokenize(*input, &tokens); if (tokens.size == 0 && res == 0) { ft_vec_free(&tokens, free_token); diff --git a/src/tokenization.c b/src/tokenization.c index cc8791d..ccabffc 100644 --- a/src/tokenization.c +++ b/src/tokenization.c @@ -6,21 +6,13 @@ /* By: lnikolov -#ifndef NOLEAKS -# include // readline -# include // readline -# include // readline -#else // NOLEAKS -# include -#endif // NOLEAKS - static int is_operator_start(char *str, size_t size) { if (!str) @@ -130,40 +122,19 @@ const char *get_token_type(const char *str, char next) return (g_tokens[WORD]); } -#ifndef NOLEAKS - -char *continue_input(char *line, size_t *i) -{ - free(line); - *i = 0; - return (readline("> ")); -} - -#else //NOLEAKS - -char *continue_input(char *line, size_t *i) -{ - free(line); - *i = 0; - ft_printf("> "); - return (get_next_line(STDIN_FILENO)); -} - -#endif //NOLEAKS - -int handle_quote(t_vec *current_token, char **line, char quote_char, size_t *i) +int handle_quote(t_vec *current_token, char *line, char quote_char, size_t *i) { - if (ft_vec_append(current_token, line[0] + (*i)++) != success) + if (ft_vec_append(current_token, line + (*i)++) != success) return (1); - while (line[0][*i] != quote_char) + while (line[*i] != quote_char) { - if (!line[0][*i]) + if (!line[*i]) return (1); else - if (ft_vec_append(current_token, line[0] + (*i)++) != success) + if (ft_vec_append(current_token, line + (*i)++) != success) return (1); } - ft_vec_append(current_token, line[0] + (*i)++); + ft_vec_append(current_token, line + (*i)++); return (0); } @@ -230,7 +201,7 @@ void filter_assignment_word(t_vec *tokens) } } -int tokenize(char **line, t_vec *tokens) +int tokenize(char *line, t_vec *tokens) { t_vec current_token; t_token token; @@ -240,32 +211,32 @@ int tokenize(char **line, t_vec *tokens) ft_vec_init(¤t_token, sizeof(char)); res = 0; i = 0; - while (line[0][i] && res == 0) + while (line[i] && res == 0) { if (is_operator_start(current_token.vec, current_token.size) - && can_expand_operator(¤t_token, line[0][i])) - res = (ft_vec_append(¤t_token, line[0] + (i++)) != success); + && can_expand_operator(¤t_token, line[i])) + res = (ft_vec_append(¤t_token, line + (i++)) != success); else if (is_operator(¤t_token)) res = finish_token(tokens, ¤t_token, '\0'); - else if (line[0][i] == '\'') + else if (line[i] == '\'') res = handle_quote(¤t_token, line, '\'', &i); - else if (line[0][i] == '"' ) + else if (line[i] == '"' ) res = handle_quote(¤t_token, line, '"', &i); - else if (is_operator_start(line[0] + i, 1) || ft_isspace(line[0][i])) + else if (is_operator_start(line + i, 1) || ft_isspace(line[i])) { if (current_token.size > 0) - res = finish_token(tokens, ¤t_token, line[0][i]); - if (!ft_isspace(line[0][i])) - res = res || ft_vec_append(¤t_token, line[0] + i) + res = finish_token(tokens, ¤t_token, line[i]); + if (!ft_isspace(line[i])) + res = res || ft_vec_append(¤t_token, line + i) != success; ++i; } else if (current_token.size > 0) - res = ft_vec_append(¤t_token, line[0] + (i++)) != success; - else if (line[0][i] == '#') + res = ft_vec_append(¤t_token, line + (i++)) != success; + else if (line[i] == '#') break ; else - res = ft_vec_append(¤t_token, line[0] + (i++)) != success; + res = ft_vec_append(¤t_token, line + (i++)) != success; } if (current_token.size > 0 && !res) {