]> git.dkaiser.de - 42/minishell.git/commitdiff
Add basic command execution
authorDominik Kaiser <dkaiser@1-D-24.42heilbronn.de>
Mon, 21 Oct 2024 13:08:10 +0000 (15:08 +0200)
committerDominik Kaiser <dkaiser@1-D-24.42heilbronn.de>
Mon, 21 Oct 2024 13:08:10 +0000 (15:08 +0200)
Makefile
include/env.h
include/minishell.h
src/env.c
src/env_to_strlst.c [new file with mode: 0644]
src/execute_cmd.c [new file with mode: 0644]
src/interpreter.c
src/main.c

index 3ae13596e90cf5ecd5fae0f20b82136d0605e9ab..78286784886debec08e8ad1e8e72477095c25212 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -14,7 +14,7 @@ VPATH   := src
 SRC     := main.c debug_tools.c init.c signal_handling.c repl.c new_token.c \
            free_token.c new_node.c free_node.c tokenizer.c parser.c \
            parse_cmd.c collect_redirs.c print_ast.c interpreter.c env.c \
-           get_cmd_path.c
+           get_cmd_path.c env_to_strlst.c execute_cmd.c
 
 OBJ_DIR := _obj
 OBJ     := $(addprefix $(OBJ_DIR)/, $(SRC:%.c=%.o))
index f22ebddeb4b7d931670bfe4047ac217c090f6272..1e6941d08bd49279028aea61b374d727fd5a4dbf 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: chuhlig <chuhlig@student.42.fr>            +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/08/08 16:53:39 by dkaiser           #+#    #+#             */
-/*   Updated: 2024/10/17 17:01:27 by dkaiser          ###   ########.fr       */
+/*   Updated: 2024/10/21 14:57:24 by dkaiser          ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -22,3 +22,4 @@ typedef struct s_env
 void                           getenvlst(t_env **env, char **en);
 void                           free_envlst(t_env **env);
 char                           *env_get(t_env *env, char *name);
+char                           **env_to_strlst(t_env *env);
index c84fda36cbcc3d0cf44f60c3e070a735e9a12083..6282ea6f35078782ade61abb639b82636600b76e 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/06/22 17:14:49 by dkaiser           #+#    #+#             */
-/*   Updated: 2024/10/17 17:09:25 by dkaiser          ###   ########.fr       */
+/*   Updated: 2024/10/21 15:00:05 by dkaiser          ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -39,4 +39,5 @@ void                  print_ast(t_node *ast);
 
 int                            eval(t_node *node, t_env *env);
 char                   *get_cmd_path(char *cmd, t_env *env);
+int                            execute_cmd(t_cmd *cmd, t_env *env);
 #endif
index 77ba4831667bf3ee003aee80bf9dfff4376152fb..ed93ec03d12f7a8c3ffaa6e2b050276d6ed982f8 100644 (file)
--- a/src/env.c
+++ b/src/env.c
@@ -6,13 +6,14 @@
 /*   By: chuhlig <chuhlig@student.42.fr>            +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/10/17 14:31:07 by chuhlig           #+#    #+#             */
-/*   Updated: 2024/10/17 15:58:28 by dkaiser          ###   ########.fr       */
+/*   Updated: 2024/10/21 15:07:51 by dkaiser          ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
 #include "env.h"
 #include "get_next_line.h"
 #include "libft.h"
+#include <stdlib.h>
 
 void   getenvlst(t_env **env, char **en)
 {
@@ -51,7 +52,7 @@ void  free_envlst(t_env **env)
        }
 }
 
-char *env_get(t_env *env, char *name)
+char   *env_get(t_env *env, char *name)
 {
        while (env != NULL)
        {
diff --git a/src/env_to_strlst.c b/src/env_to_strlst.c
new file mode 100644 (file)
index 0000000..3a58df9
--- /dev/null
@@ -0,0 +1,58 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   env_to_strlst.c                                    :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2024/10/21 14:52:08 by dkaiser           #+#    #+#             */
+/*   Updated: 2024/10/21 15:07:33 by dkaiser          ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "libft.h"
+#include "minishell.h"
+
+static char    *get_var_assign(t_env *cur);
+
+char   **env_to_strlst(t_env *env)
+{
+       int             size;
+       t_env   *cur;
+       char    **result;
+       int             i;
+
+       size = 0;
+       cur = env;
+       while (cur != NULL)
+       {
+               size++;
+               cur = cur->next;
+       }
+       result = malloc(sizeof(char *) * (size + 1));
+       if (result == NULL)
+               return (NULL);
+       i = 0;
+       cur = env;
+       while (i < size)
+       {
+               result[i] = get_var_assign(cur);
+               cur = cur->next;
+               i++;
+       }
+       result[i] = NULL;
+       return (result);
+}
+
+static char    *get_var_assign(t_env *cur)
+{
+       char    *left_side;
+       char    *result;
+
+       left_side = ft_strjoin(cur->name, "=");
+       if (left_side == NULL)
+               return (NULL);
+       result = ft_strjoin(left_side, cur->value);
+       free(left_side);
+       return (result);
+}
diff --git a/src/execute_cmd.c b/src/execute_cmd.c
new file mode 100644 (file)
index 0000000..3730d4b
--- /dev/null
@@ -0,0 +1,38 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   execute_cmd.c                                      :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2024/10/21 13:58:56 by dkaiser           #+#    #+#             */
+/*   Updated: 2024/10/21 15:07:37 by dkaiser          ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "minishell.h"
+#include <stdlib.h>
+#include <sys/_types/_pid_t.h>
+#include <unistd.h>
+
+int    execute_cmd(t_cmd *cmd, t_env *env)
+{
+       int             result;
+       pid_t   pid;
+
+       pid = fork();
+       if (pid < 0)
+               return (EXIT_FAILURE);
+       if (pid == 0)
+       {
+               result = execve(cmd->args[0], cmd->args, env_to_strlst(env));
+               exit(result);
+       }
+       else
+       {
+               // only wait if cmd is on rightmost side of pipe?
+               // so in cmd1 | cmd2 | cmd3 | cmd4 only wait for cmd4
+               waitpid(pid, &result, 0);
+       }
+       return (result);
+}
index 0fa7ac1184b4586c51f723732eb72fa817fc5f01..10826447cd7614781754ef55e1cafb06fb2ae981 100644 (file)
@@ -6,11 +6,12 @@
 /*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/08/05 13:15:24 by dkaiser           #+#    #+#             */
-/*   Updated: 2024/10/17 17:05:51 by dkaiser          ###   ########.fr       */
+/*   Updated: 2024/10/21 15:07:27 by dkaiser          ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
 #include "minishell.h"
+#include <sys/wait.h>
 
 static int     eval_pipe(t_pipe *pipe, t_env *env);
 static int     eval_cmd(t_cmd *cmd, t_env *env);
@@ -38,6 +39,13 @@ static int   eval_pipe(t_pipe *pipe, t_env *env)
 
 static int     eval_cmd(t_cmd *cmd, t_env *env)
 {
-       printf("%s\n", get_cmd_path(cmd->args[0], env));
+       char    *cmd_path;
+
+       cmd_path = get_cmd_path(cmd->args[0], env);
+       if (cmd_path == NULL)
+               return (1);
+       free(cmd->args[0]);
+       cmd->args[0] = cmd_path;
+       execute_cmd(cmd, env);
        return (0);
 }
index e87bc99ac4fe73a2ad38a7ec79c1200098ccff64..c3e0ec797f8267b897829ed662be834eb6cf3853 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: chuhlig <chuhlig@student.42.fr>            +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/06/22 17:14:03 by dkaiser           #+#    #+#             */
-/*   Updated: 2024/10/17 17:01:45 by dkaiser          ###   ########.fr       */
+/*   Updated: 2024/10/21 15:02:56 by dkaiser          ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -16,6 +16,7 @@ int   main(int argc, char *argv[], char *envp[])
 {
        t_env   *env;
 
+       env = NULL;
        if (!argc && !argv)
                return (1);
        if (init())