]> git.dkaiser.de - 42/minishell.git/commitdiff
uiuiuiui
authorChristopher Uhlig <chuhlig@3-G-7.42heilbronn.de>
Wed, 22 Jan 2025 15:50:05 +0000 (16:50 +0100)
committerChristopher Uhlig <chuhlig@3-G-7.42heilbronn.de>
Wed, 22 Jan 2025 15:50:05 +0000 (16:50 +0100)
src/collect_redirs.c
src/env_tools.c
src/execute_cmd.c
src/format_string.c
src/free_node.c
src/free_token.c
src/get_cmd_path.c
src/interpreter.c
src/new_node.c
src/repl.c

index 67ab8f855592e11298c8c1bf6fcc470a2b73c1cc..d40cee4c75b6b19cf81dfdc6fc1cf9cdc526f20b 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: chuhlig <chuhlig@student.42.fr>            +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/08/02 13:49:31 by dkaiser           #+#    #+#             */
-/*   Updated: 2025/01/21 20:19:48 by chuhlig          ###   ########.fr       */
+/*   Updated: 2025/01/22 16:08:25 by chuhlig          ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
index f1c37487629d9ea3d50cb81eeaec4ed845867ebe..b2e7873a70ed10af0ec7683ed1a610c1a9c970ac 100644 (file)
@@ -6,16 +6,16 @@
 /*   By: chuhlig <chuhlig@student.42.fr>            +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2025/01/15 16:14:31 by dkaiser           #+#    #+#             */
-/*   Updated: 2025/01/22 00:01:03 by chuhlig          ###   ########.fr       */
+/*   Updated: 2025/01/22 14:13:02 by chuhlig          ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
 #include "minishell.h"
 
-// char        **get_split_path(t_env *env)
-// {
-//     char    *path;
+char   **get_split_path(t_env *env)
+{
+       char    *path;
 
-//     path = env_get(env, "PATH");
-//     return (ft_split(path, ':'));
-// }
+       path = env_get(env, "PATH");
+       return (ft_split(path, ':'));
+}
index 012391fb46cfeaff1891769482b81ae1c2e390cd..2179515f3314c2989dec75572ce3a4b32b528a23 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: chuhlig <chuhlig@student.42.fr>            +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/12/17 19:21:35 by chuhlig           #+#    #+#             */
-/*   Updated: 2025/01/21 23:52:38 by chuhlig          ###   ########.fr       */
+/*   Updated: 2025/01/22 16:29:34 by chuhlig          ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
 static void    establish_pipeline(int original_stdin, int original_stdout);
 static int     exec_cmd(t_cmd *cmd, t_env **env, int original_std[2], int result);
 
+int    invalid_input(char *cmd)
+{
+       ft_putstr_fd("minishell: ", STDERR_FILENO);
+       ft_putstr_fd(cmd, STDERR_FILENO);
+       ft_putstr_fd(": command not found\n", STDERR_FILENO);
+       return (127);
+}
+
 int    is_builtin(char *cmd)
 {
-       return ((ft_strcmp(cmd, "export") == 0) || (ft_strcmp(cmd, "unset") == 0)
+       if ((ft_strcmp(cmd, "export") == 0) || (ft_strcmp(cmd, "unset") == 0)
                || (ft_strcmp(cmd, "cd") == 0) || (ft_strcmp(cmd, "exit") == 0)
                || (ft_strcmp(cmd, "echo") == 0) || (ft_strcmp(cmd, "pwd") == 0)
-               || (ft_strcmp(cmd, "env") == 0));
+               || (ft_strcmp(cmd, "env") == 0))
+               return (1);
+       return(invalid_input(cmd));
 }
 
 int    execute_builtin(char **args, t_env **env)
@@ -96,7 +106,7 @@ static int   exec_cmd(t_cmd *cmd, t_env **env, int original_std[2], int result)
                cmd_path = get_cmd_path(cmd->args[i], *env, &result);
                if (cmd_path != NULL)
                        execve(cmd_path, &(cmd->args[i]), env_to_strlst(*env));
-               free(cmd_path);
+               // free(cmd_path);
                exit(result);
        }
        waitpid(pid, &status, 0);
index 775a3dafb0b7e699cdd602e80a2e63b924074e9d..ccc70846c09d985e719fd865a1ae4984b0fa883b 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: chuhlig <chuhlig@student.42.fr>            +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/12/17 19:30:11 by chuhlig           #+#    #+#             */
-/*   Updated: 2025/01/21 23:25:20 by chuhlig          ###   ########.fr       */
+/*   Updated: 2025/01/22 14:31:29 by chuhlig          ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -52,6 +52,8 @@ static void   append_slice(char **dst, char *src, int start, int end)
        if (*dst != NULL)
                free(*dst);
        *dst = result;
+       // free(src);
+       // src = *dst;
 }
 
 static void    append_var(char **dst, char *src, int *pos, t_env *env)
index d866727179c5e99d8c60a74f8d935fba62b884d0..733fe1c6c8b4364776b9fb45fcebaed4432d822a 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: chuhlig <chuhlig@student.42.fr>            +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/06/27 11:41:46 by dkaiser           #+#    #+#             */
-/*   Updated: 2025/01/22 01:53:34 by chuhlig          ###   ########.fr       */
+/*   Updated: 2025/01/22 16:00:36 by chuhlig          ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -28,84 +28,84 @@ static void free_file(void *arg);
 //             panic(UNREACHABLE);
 //     free(node);
 // }
-void   free_redirections(t_redirection redirs[2])
-{
-    for (int i = 0; i < 2; i++)
-    {
-        if (redirs[i].specifier)
-            free(redirs[i].specifier);
-    }
-}
 
-void   free_node(t_node *node)
-{
-    if (!node)
-        return;
+// static void free_pipe_node(t_node *node)
+// {
+//     free_node(node->content.pipe.left);
+//     free_node(node->content.pipe.right);
+// }
+
+// static void free_cmd_node(t_node *node)
+// {
+//     int     i;
+
+//     i = 0;
+//     while (node->content.cmd.args != NULL && node->content.cmd.args[i] != NULL)
+//     {
+//             free(node->content.cmd.args[i]);
+//             i++;
+//     }
+//     free(node->content.cmd.args);
+//     if (node->content.cmd.redirs[0].type != 0
+//             && node->content.cmd.redirs[0].specifier != NULL)
+//             free(node->content.cmd.redirs[0].specifier);
+//     if (node->content.cmd.redirs[1].type != 0
+//             && node->content.cmd.redirs[0].specifier != NULL)
+//             free(node->content.cmd.redirs[1].specifier);
+//     if (node->content.cmd.create_files != NULL)
+//             ft_lstclear(&node->content.cmd.create_files, free_file);
+// }
+
+// static void free_file(void *arg)
+// {
+//     t_redirection   *file;
 
+//     file = (t_redirection *)arg;
+//     free(file->specifier);
+//     free(file);
+// }
+
+void free_node(t_node *node)
+{
     if (node->type == PIPE_NODE)
-    {
-        free_node(node->content.pipe.left);
-        free_node(node->content.pipe.right);
-    }
+        free_pipe_node(node);
     else if (node->type == CMD_NODE)
-    {
-        if (node->content.cmd.args)
-        {
-            for (int i = 0; node->content.cmd.args[i]; i++)
-                free(node->content.cmd.args[i]);
-            free(node->content.cmd.args);
-        }
-        free_redirections(node->content.cmd.redirs);
-        // Assuming create_files is a list of dynamically allocated strings
-        t_list *current = node->content.cmd.create_files;
-        t_list *next;
-        while (current)
-        {
-            next = current->next;
-            free(current->content);
-            free(current);
-            current = next;
-        }
-    }
+        free_cmd_node(node);
     else if (node->type == STRING_NODE)
-    {
         free(node->content.string);
-    }
     free(node);
 }
 
-static void    free_pipe_node(t_node *node)
+static void free_pipe_node(t_node *node)
 {
-       free_node(node->content.pipe.left);
-       free_node(node->content.pipe.right);
+    free_node(node->content.pipe.left);
+    free_node(node->content.pipe.right);
 }
 
-static void    free_cmd_node(t_node *node)
+static void free_cmd_node(t_node *node)
 {
-       int     i;
+    int i;
 
-       i = 0;
-       while (node->content.cmd.args != NULL && node->content.cmd.args[i] != NULL)
-       {
-               free(node->content.cmd.args[i]);
-               i++;
-       }
-       free(node->content.cmd.args);
-       if (node->content.cmd.redirs[0].type != 0
-               && node->content.cmd.redirs[0].specifier != NULL)
-               free(node->content.cmd.redirs[0].specifier);
-       if (node->content.cmd.redirs[1].type != 0
-               && node->content.cmd.redirs[0].specifier != NULL)
-               free(node->content.cmd.redirs[1].specifier);
-       if (node->content.cmd.create_files != NULL)
-               ft_lstclear(&node->content.cmd.create_files, free_file);
+    i = 0;
+    while (node->content.cmd.args != NULL && node->content.cmd.args[i] != NULL)
+    {
+        free(node->content.cmd.args[i]);
+        i++;
+    }
+    free(node->content.cmd.args);
+    if (node->content.cmd.redirs[0].type != 0 && node->content.cmd.redirs[0].specifier != NULL)
+        free(node->content.cmd.redirs[0].specifier);
+    if (node->content.cmd.redirs[1].type != 0 && node->content.cmd.redirs[1].specifier != NULL)
+        free(node->content.cmd.redirs[1].specifier);
+    if (node->content.cmd.create_files != NULL)
+        ft_lstclear(&node->content.cmd.create_files, free_file);
 }
 
-static void    free_file(void *arg)
+static void free_file(void *arg)
 {
-       t_redirection   *file;
+    t_redirection *file;
 
-       file = (t_redirection *)arg;
-       free(file->specifier);
-       free(file);
-}
+    file = (t_redirection *)arg;
+    free(file->specifier);
+    free(file);
+}
\ No newline at end of file
index 64278a8050a4bb977413d4ad95cf9f00a76bd6eb..66ab1dd359d530de66e55fd5b41cf1a99c023b0f 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: chuhlig <chuhlig@student.42.fr>            +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/06/27 14:38:57 by dkaiser           #+#    #+#             */
-/*   Updated: 2025/01/22 00:07:58 by chuhlig          ###   ########.fr       */
+/*   Updated: 2025/01/22 15:56:38 by chuhlig          ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -19,8 +19,11 @@ void free_token(t_token *token)
                token->previous->next = NULL;
        if (token->next != NULL)
                token->next->previous = NULL;
-    // if (token->type == STRING_TOKEN && token->content.string != NULL)
-    //     free(token->content.string); // Ensure content is freed
+       // if (token->previous == NULL && token->next == NULL)
+       // {
+       //      if (token->type == STRING_TOKEN && token->content.string != NULL)
+       //              free(token->content.string); // Ensure content is freed
+       // }
        free(token);//maybe free token
        token = NULL;
 }
@@ -35,23 +38,23 @@ void        free_token_and_connect(t_token *token)
        token = NULL;
 }
 
-// void        free_tokens(t_token *tokens)
-// {
-//     while (tokens->next != NULL)
-//     {
-//             tokens = tokens->next;
-//             free_token(tokens->previous);
-//     }
-//     free_token(tokens);
-// }
-void free_tokens(t_token *tokens)
+void   free_tokens(t_token *tokens)
 {
-    t_token *tmp;
-
-    while (tokens)
-    {
-        tmp = tokens;
-        tokens = tokens->next;
-        free_token(tmp); // Ensure each token is freed
-    }
+       while (tokens->next != NULL)
+       {
+               tokens = tokens->next;
+               free_token(tokens->previous);
+       }
+       free_token(tokens);
 }
+// void free_tokens(t_token *tokens)
+// {
+//     t_token *tmp;
+
+//     while (tokens)
+//     {
+//         tmp = tokens;
+//         tokens = tokens->next;
+//         free_token(tmp); // Ensure each token is freed
+//     }
+// }
index 713c397bc6b732f2376763ebcb083e0cbe3c3ffa..01f24b1b902f236593ebfcf2fbded520b2594e9f 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: chuhlig <chuhlig@student.42.fr>            +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/12/17 19:19:59 by chuhlig           #+#    #+#             */
-/*   Updated: 2025/01/22 00:01:48 by chuhlig          ###   ########.fr       */
+/*   Updated: 2025/01/22 16:17:27 by chuhlig          ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -61,92 +61,77 @@ static char *get_absolute_cmd_path(char *cmd, t_env *env, int *return_code)
        return (result);
 }
 
-void free_split_path(char **path)
+static char    *find_in_path(char *cmd, t_env *env, int *return_code)
 {
-    char **tmp = path;
-    while (*tmp)
-    {
-        free(*tmp);
-        tmp++;
-    }
-    free(path);
-}
+       char    *cur_path;
+       char    *cmd_path;
+       char    **path;
 
-char **get_split_path(t_env *env)
-{
-    char *path_env;
-    char **split_path;
 
-    path_env = env_get(env, "PATH");
-    if (!path_env)
-        return (NULL);
-    split_path = ft_split(path_env, ':');
-    free(path_env);
-    return (split_path);
+       path = get_split_path(env);
+       cmd_path = NULL;
+       while (*path)
+       {
+               if (cmd_path)
+                       free(cmd_path);
+               cur_path = ft_strjoin(*path, "/");
+               if (!cur_path)
+                       return (NULL);
+               cmd_path = ft_strjoin(cur_path, cmd);
+               free(cur_path);
+               if (!cmd_path)
+                       return (NULL);
+               if (access(cmd_path, X_OK) != -1)
+                       return (cmd_path);
+               path++;
+       }
+       *return_code = 127;
+       free(cmd_path);
+       command_not_found_error(cmd);
+       return (NULL);
 }
 
-static char    *find_in_path(char *cmd, t_env *env, int *return_code)
-{
-    char       *cur_path;
-    char       *cmd_path;
-    char       **path;
-    char       **path_start; // To keep track of the start of the path array
-
-    path = get_split_path(env);
-    path_start = path; // Save the start of the path array
-    cmd_path = NULL;
-    while (*path)
-    {
-        if (cmd_path)
-            free(cmd_path);
-        cur_path = ft_strjoin(*path, "/");
-        if (!cur_path)
-        {
-            free_split_path(path_start); // Free the entire path array
-            return (NULL);
-        }
-        cmd_path = ft_strjoin(cur_path, cmd);
-        free(cur_path);
-        if (!cmd_path)
-        {
-            free_split_path(path_start); // Free the entire path array
-            return (NULL);
-        }
-        if (access(cmd_path, X_OK) != -1)
-        {
-            free_split_path(path_start); // Free the entire path array
-            return (cmd_path);
-        }
-        path++;
-    }
-    free_split_path(path_start); // Free the entire path array
-    *return_code = 127;
-    command_not_found_error(cmd);
-    return (NULL);
-}
+// static char *get_simple_cmd_path(char *cmd, int *return_code)
+// {
+//     char    *result;
 
+//     result = ft_strdup(cmd);
+//     if (!result)
+//             return (NULL);
+//     if (access(result, F_OK) == -1)
+//     {
+//             free(result);
+//             return (error(ENOENT, cmd, 127, return_code));
+//     }
+//     if (access(result, X_OK) == -1)
+//     {
+//             free(result);
+//             return (error(EACCES, cmd, 126, return_code));
+//     }
+//     if (is_directory(cmd))
+//     {
+//             free(result);
+//             return (error(EISDIR, cmd, 126, return_code));
+//     }
+//     return (result);
+// }
 static char    *get_simple_cmd_path(char *cmd, int *return_code)
 {
        char    *result;
 
        result = ft_strdup(cmd);
-       if (!result)
-               return (NULL);
        if (access(result, F_OK) == -1)
        {
                free(result);
                return (error(ENOENT, cmd, 127, return_code));
        }
-       if (access(result, X_OK) == -1)
+       else if (access(result, X_OK) == -1)
        {
                free(result);
                return (error(EACCES, cmd, 126, return_code));
        }
        if (is_directory(cmd))
-       {
-               free(result);
                return (error(EISDIR, cmd, 126, return_code));
-       }
        return (result);
 }
 
index 11f06203da46cda7a207b47836cc3c19e3b64337..979a3cfc69bb31359efe06c62347b71d090eabf8 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: chuhlig <chuhlig@student.42.fr>            +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/12/17 19:15:49 by chuhlig           #+#    #+#             */
-/*   Updated: 2025/01/21 16:35:38 by chuhlig          ###   ########.fr       */
+/*   Updated: 2025/01/22 15:20:18 by chuhlig          ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
index b2ab7ea7965732a8550f1192b6958fba05c9364c..21f8d9ffa6c274f55e9d270f05a5afe7886264c9 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: chuhlig <chuhlig@student.42.fr>            +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/06/27 11:21:03 by dkaiser           #+#    #+#             */
-/*   Updated: 2025/01/20 17:59:01 by dkaiser          ###   ########.fr       */
+/*   Updated: 2025/01/22 15:15:31 by chuhlig          ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -51,7 +51,7 @@ t_node        *new_cmd_node(char **args, t_redirection redirs[2],
                node->content.cmd.redirs[0] = redirs[0];
                node->content.cmd.redirs[1] = redirs[1];
                node->content.cmd.create_files = create_files;
-               free(redirs);
+               // free(redirs);
                redirs = NULL;
                return (node);
        }
index 2dcd16d23bc531b5e8656739fc87fc4d88ca97f6..859dde37e65b81e62cd1cedd3aebd3cfe6791ca5 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: chuhlig <chuhlig@student.42.fr>            +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/06/24 16:07:04 by dkaiser           #+#    #+#             */
-/*   Updated: 2025/01/21 21:29:16 by chuhlig          ###   ########.fr       */
+/*   Updated: 2025/01/22 15:22:00 by chuhlig          ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -16,7 +16,8 @@
 void   free_repl(char *input, t_node *ast)
 {
        free(input);
-       free_node(ast);
+       if(ast)
+               free_node(ast);
 }
 
 void   repl(const char *prompt, t_env **env, int *promptflag)