Simplify tokenization
authorLukas Jiriste <ljiriste@student.42prague.com>
Sat, 31 Aug 2024 10:00:44 +0000 (12:00 +0200)
committerLukas Jiriste <ljiriste@student.42prague.com>
Sat, 31 Aug 2024 11:38:20 +0000 (13:38 +0200)
Because minishell should error out when unpaired quotes are encountered,
the handling is unnecessary as is the input of char** instead of char*.

inc/minishell.h
src/input_handling.c
src/tokenization.c

index be6479a2f1b7ca4ea9bacb6308155da55d7c233d..92318d82191437d38309946a50a4177610a47b99 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   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);
 
index 554306e9d7dcfdadb9d00eb2a8c8f95c63886649..5347db0a80e3d238afc02eb24c70c2daba78c6f4 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   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);
index cc8791d84d1c88834d4255ba730aa51224bc363c..ccabffc91d2e5ecdd3b1cd793c40508003ab23de 100644 (file)
@@ -6,21 +6,13 @@
 /*   By: lnikolov <lnikolov@student.42prague.com    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/06/21 16:34:43 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/08/30 14:28:42 by lnikolov         ###   ########.fr       */
+/*   Updated: 2024/08/31 11:58:17 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
 #include "minishell.h"
 #include <stdlib.h>
 
-#ifndef NOLEAKS
-# include <stdio.h>                            // readline
-# include <readline/readline.h>        // readline
-# include <readline/history.h> // readline
-#else // NOLEAKS
-# include <unistd.h>
-#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(&current_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(&current_token, line[0][i]))
-                       res = (ft_vec_append(&current_token, line[0] + (i++)) != success);
+                       && can_expand_operator(&current_token, line[i]))
+                       res = (ft_vec_append(&current_token, line + (i++)) != success);
                else if (is_operator(&current_token))
                        res = finish_token(tokens, &current_token, '\0');
-               else if (line[0][i] == '\'')
+               else if (line[i] == '\'')
                        res = handle_quote(&current_token, line, '\'', &i);
-               else if (line[0][i] == '"' )
+               else if (line[i] == '"' )
                        res = handle_quote(&current_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, &current_token, line[0][i]);
-                       if (!ft_isspace(line[0][i]))
-                               res = res || ft_vec_append(&current_token, line[0] + i)
+                               res = finish_token(tokens, &current_token, line[i]);
+                       if (!ft_isspace(line[i]))
+                               res = res || ft_vec_append(&current_token, line + i)
                                        != success;
                        ++i;
                }
                else if (current_token.size > 0)
-                       res = ft_vec_append(&current_token, line[0] + (i++)) != success;
-               else if (line[0][i] == '#')
+                       res = ft_vec_append(&current_token, line + (i++)) != success;
+               else if (line[i] == '#')
                        break ;
                else
-                       res = ft_vec_append(&current_token, line[0] + (i++)) != success;
+                       res = ft_vec_append(&current_token, line + (i++)) != success;
        }
        if (current_token.size > 0 && !res)
        {