aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristopher Uhlig2024-10-25 20:44:14 +0200
committerChristopher Uhlig2024-10-25 20:44:14 +0200
commiteafa035f9a72961280f4b1bb8f405f40000d3212 (patch)
tree3566b9bcfde24d59afc7591179647f393184cfaf
parentca4acea03cda19c2a0f0fd168d3c8fd418d71e04 (diff)
parent15d8385f8ecf30e1ca74025b12fed7e45349b706 (diff)
downloadminishell-eafa035f9a72961280f4b1bb8f405f40000d3212.tar.gz
minishell-eafa035f9a72961280f4b1bb8f405f40000d3212.zip
resolved merge conflicts
-rw-r--r--Makefile3
-rw-r--r--include/env.h9
-rw-r--r--include/minishell.h6
-rw-r--r--src/env.c5
-rw-r--r--src/env_to_strlst.c58
-rw-r--r--src/execute_cmd.c59
-rw-r--r--src/get_cmd_path.c82
-rw-r--r--src/interpreter.c55
-rw-r--r--src/main.c2
9 files changed, 214 insertions, 65 deletions
diff --git a/Makefile b/Makefile
index 283db1a..7828678 100644
--- a/Makefile
+++ b/Makefile
@@ -13,7 +13,8 @@ HEADERS = -I include -I $(LIB_DIR)/libft
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
+ parse_cmd.c collect_redirs.c print_ast.c interpreter.c env.c \
+ get_cmd_path.c env_to_strlst.c execute_cmd.c
OBJ_DIR := _obj
OBJ := $(addprefix $(OBJ_DIR)/, $(SRC:%.c=%.o))
diff --git a/include/env.h b/include/env.h
index 3bf6bd6..a35bec3 100644
--- a/include/env.h
+++ b/include/env.h
@@ -6,7 +6,7 @@
/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/08 16:53:39 by dkaiser #+# #+# */
-/* Updated: 2024/10/17 17:08:01 by chuhlig ### ########.fr */
+/* Updated: 2024/10/25 19:53:38 by chuhlig ### ########.fr */
/* */
/* ************************************************************************** */
@@ -19,6 +19,7 @@ typedef struct s_env
struct s_env *next;
} t_env;
-void getenvlst(t_env **env, char **en);
-void free_envlst(t_env **env);
-char *env_get(t_env *env, char *name);
+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); \ No newline at end of file
diff --git a/include/minishell.h b/include/minishell.h
index 7b6226e..b2a3845 100644
--- a/include/minishell.h
+++ b/include/minishell.h
@@ -6,7 +6,7 @@
/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/22 17:14:49 by dkaiser #+# #+# */
-/* Updated: 2024/10/22 16:52:26 by chuhlig ### ########.fr */
+/* Updated: 2024/10/25 16:10:21 by chuhlig ### ########.fr */
/* */
/* ************************************************************************** */
@@ -37,5 +37,7 @@ t_redirection *collect_redirs(t_token **tokens);
void print_ast(t_node *ast);
-int eval(t_node *node, t_env **env);
+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
diff --git a/src/env.c b/src/env.c
index ca07f9f..3110965 100644
--- 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/25 15:48:56 by chuhlig ### ########.fr */
+/* Updated: 2024/10/25 19:17:54 by chuhlig ### ########.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
index 0000000..3a58df9
--- /dev/null
+++ b/src/env_to_strlst.c
@@ -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
index 0000000..fa7677f
--- /dev/null
+++ b/src/execute_cmd.c
@@ -0,0 +1,59 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* execute_cmd.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2024/10/21 13:58:56 by dkaiser #+# #+# */
+/* Updated: 2024/10/25 13:31:16 by dkaiser ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "minishell.h"
+#include <stdlib.h>
+#include <sys/_types/_pid_t.h>
+#include <sys/_types/_s_ifmt.h>
+#include <sys/fcntl.h>
+#include <unistd.h>
+#include <fcntl.h>
+
+int execute_cmd(t_cmd *cmd, t_env *env)
+{
+ int result;
+ char *cmd_path;
+ int fd;
+
+ cmd_path = get_cmd_path(cmd->args[0], env);
+ cmd->args[0] = cmd_path;
+
+ if (cmd->redirs[0].type == INPUT_FILE)
+ {
+ fd = open(cmd->redirs[0].specifier, O_RDONLY);
+ if (fd < 0)
+ return (EXIT_FAILURE);
+ dup2(fd, STDIN_FILENO);
+ }
+ else if (cmd->redirs[0].type == INPUT_LIMITER)
+ {
+ dbg("INPUT_LIMITER");
+ }
+ if (cmd->redirs[1].type == OUTPUT_APPEND)
+ {
+ dbg("OUTPUT_APPEND");
+ fd = open(cmd->redirs[1].specifier, O_WRONLY | O_CREAT | O_APPEND);
+ if (fd < 0)
+ return (EXIT_FAILURE);
+ dup2(fd, STDOUT_FILENO);
+ }
+ else if (cmd->redirs[1].type == OUTPUT_OVERRIDE)
+ {
+ fd = open(cmd->redirs[1].specifier, O_WRONLY | O_CREAT | O_TRUNC);
+ if (fd < 0)
+ return (EXIT_FAILURE);
+ dup2(fd, STDOUT_FILENO);
+ dbg("OUTPUT_OVERRIDE");
+ }
+ result = execve(cmd->args[0], cmd->args, env_to_strlst(env));
+ return (result);
+}
diff --git a/src/get_cmd_path.c b/src/get_cmd_path.c
new file mode 100644
index 0000000..1ecf393
--- /dev/null
+++ b/src/get_cmd_path.c
@@ -0,0 +1,82 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* get_cmd_path.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2024/10/17 16:45:47 by dkaiser #+# #+# */
+/* Updated: 2024/10/17 17:11:27 by dkaiser ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "libft.h"
+#include "minishell.h"
+
+static char *get_absolute_cmd_path(char *cmd, t_env *env);
+static char *find_in_path(char *cmd, t_env *env);
+char **get_split_path(t_env *env);
+
+char *get_cmd_path(char *cmd, t_env *env)
+{
+ if (cmd[0] == '/')
+ return (ft_strdup(cmd));
+ else if (ft_strchr(cmd, '/'))
+ return (get_absolute_cmd_path(cmd, env));
+ else
+ return (find_in_path(cmd, env));
+}
+
+static char *get_absolute_cmd_path(char *cmd, t_env *env)
+{
+ char *cur_dir;
+ char *result;
+
+ cur_dir = ft_strjoin(env_get(env, "PWD"), "/");
+ if (!cur_dir)
+ return (NULL);
+ result = ft_strjoin(cur_dir, cmd);
+ free(cur_dir);
+ if (!result)
+ return (NULL);
+ if (access(result, X_OK) == -1)
+ {
+ free(result);
+ return (NULL);
+ }
+ return (result);
+}
+
+static char *find_in_path(char *cmd, t_env *env)
+{
+ char *cur_path;
+ char *cmd_path;
+ char **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 (NULL);
+}
+
+char **get_split_path(t_env *env)
+{
+ char *path;
+
+ path = env_get(env, "PATH");
+ return (ft_split(path, ':'));
+}
diff --git a/src/interpreter.c b/src/interpreter.c
index 13f10ed..e69de29 100644
--- a/src/interpreter.c
+++ b/src/interpreter.c
@@ -1,55 +0,0 @@
-/* ************************************************************************** */
-/* */
-/* ::: :::::::: */
-/* interpreter.c :+: :+: :+: */
-/* +:+ +:+ +:+ */
-/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
-/* +#+#+#+#+#+ +#+ */
-/* Created: 2024/08/05 13:15:24 by dkaiser #+# #+# */
-/* Updated: 2024/10/25 15:46:53 by chuhlig ### ########.fr */
-/* */
-/* ************************************************************************** */
-
-#include "minishell.h"
-#include <stdio.h>
-
-static int eval_pipe(t_pipe *pipe, t_env *env);
-static int eval_cmd(t_cmd *cmd, t_env **env);
-
-int eval(t_node *node, t_env **env)
-{
- if (node->type == PIPE_NODE)
- return (eval_pipe(&node->content.pipe, *env));
- else if (node->type == CMD_NODE)
- return (eval_cmd(&node->content.cmd, env));
- else
- {
- panic(UNREACHABLE);
- return (-1);
- }
-}
-
-static int eval_pipe(t_pipe *pipe, t_env *env)
-{
- dbg("TODO: PIPE");
- eval_cmd(&pipe->left->content.cmd, &env);
- eval_cmd(&pipe->right->content.cmd, &env);
- return (0);
-}
-
-static int eval_cmd(t_cmd *cmd, t_env **env)
-{
- if (ft_strncmp(cmd->args[0], "pwd", 4) == 0)
- pwd(*env);
- else if (ft_strncmp(cmd->args[0], "echo", 5) == 0)
- echo(cmd->args);
- else if (ft_strncmp(cmd->args[0], "env", 4) == 0)
- ft_env(*env);
- else if (ft_strncmp(cmd->args[0], "cd", 3) == 0)
- cd(env, cmd->args);
- else if (ft_strncmp(cmd->args[0], "unset", 6) == 0)
- unset(cmd->args, env);
- else if (ft_strncmp(cmd->args[0], "export", 7) == 0)
- export(cmd->args, env);
- return (0);
-}
diff --git a/src/main.c b/src/main.c
index 3672fdd..18530bb 100644
--- a/src/main.c
+++ b/src/main.c
@@ -6,7 +6,7 @@
/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/22 17:14:03 by dkaiser #+# #+# */
-/* Updated: 2024/10/25 15:53:10 by chuhlig ### ########.fr */
+/* Updated: 2024/10/25 16:06:32 by chuhlig ### ########.fr */
/* */
/* ************************************************************************** */