aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorChristopher Uhlig2025-01-14 18:18:58 +0100
committerChristopher Uhlig2025-01-14 18:18:58 +0100
commitda80405b345b58f9e1bd83039b1df3771b5ca193 (patch)
treed8ddb9fa078fa353dc55992dd9609a0c798e56ad /src
parent8c37f835ba29bcecc1d779edb396d97b18667026 (diff)
downloadminishell-da80405b345b58f9e1bd83039b1df3771b5ca193.tar.gz
minishell-da80405b345b58f9e1bd83039b1df3771b5ca193.zip
update for exit bit norm removed staic stuff added prompt flag
Diffstat (limited to 'src')
-rw-r--r--src/builtins_part_one.c58
-rw-r--r--src/builtins_part_two.c27
-rw-r--r--src/collect_redirs.c132
-rw-r--r--src/env.c23
-rw-r--r--src/env_to_strlst.c4
-rw-r--r--src/execute_cmd.c155
-rw-r--r--src/format_string.c16
-rw-r--r--src/init.c4
-rw-r--r--src/interpreter.c473
-rw-r--r--src/main.c10
-rw-r--r--src/repl.c36
-rw-r--r--src/signal_handling.c4
-rw-r--r--src/tokenizer.c24
13 files changed, 266 insertions, 700 deletions
diff --git a/src/builtins_part_one.c b/src/builtins_part_one.c
index d64bb54..d8bdaa9 100644
--- a/src/builtins_part_one.c
+++ b/src/builtins_part_one.c
@@ -6,7 +6,7 @@
/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/09 17:01:16 by chuhlig #+# #+# */
-/* Updated: 2025/01/10 14:36:55 by chuhlig ### ########.fr */
+/* Updated: 2025/01/14 16:51:05 by chuhlig ### ########.fr */
/* */
/* ************************************************************************** */
@@ -41,43 +41,22 @@ int echo(char **av)
return (0);
}
-int pwd(t_env *env)
+void exit_shell(t_env **env, int exit_status)
{
- while (env)
- {
- if (ft_strncmp(env->name, "PWD", 4) == 0)
- {
- ft_printf("%s\n", env->value);
- break ;
- }
- env = env->next;
- }
- return (0);
+ free_envlst(env);
+ exit(exit_status);
}
-int ft_env(t_env *env)
+int builtin_exit(char **args, t_env **env)
{
- while (env != NULL)
- {
- printf("%s", env->name);
- printf("=%s\n", env->value);
- env = env->next;
- }
- return (0);
-}
-
-// int exit(char *av)
-// {
-// freenode free toke free sequence stop repl free env;
-// clear history;
-// }
-////
+ int exit_status;
-void free_env_node(t_env *node)
-{
- free(node->name);
- free(node->value);
- free(node);
+ if (args[1])
+ exit_status = ft_atoi(args[1]);
+ else
+ exit_status = 0;
+ exit_shell(env, exit_status);
+ return (exit_status);
}
int unset(char **av, t_env **env)
@@ -109,17 +88,6 @@ int unset(char **av, t_env **env)
return (0);
}
-t_env *env_new(char *name)
-{
- t_env *result;
-
- result = malloc(sizeof(t_env));
- if (!result)
- return (NULL);
- result->name = name;
- return (result);
-}
-
t_env *check_existing(t_env *env, char *av)
{
while (env)
@@ -158,4 +126,4 @@ int export(char **av, t_env **env)
}
}
return (0);
-} \ No newline at end of file
+}
diff --git a/src/builtins_part_two.c b/src/builtins_part_two.c
index f54e04f..5af6c68 100644
--- a/src/builtins_part_two.c
+++ b/src/builtins_part_two.c
@@ -6,7 +6,7 @@
/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/25 20:52:16 by chuhlig #+# #+# */
-/* Updated: 2024/12/20 18:53:03 by chuhlig ### ########.fr */
+/* Updated: 2025/01/14 14:26:39 by chuhlig ### ########.fr */
/* */
/* ************************************************************************** */
@@ -82,3 +82,28 @@ int cd(t_env **env, char **av)
}
return (0);
}
+
+int pwd(t_env *env)
+{
+ while (env)
+ {
+ if (ft_strncmp(env->name, "PWD", 4) == 0)
+ {
+ ft_printf("%s\n", env->value);
+ break ;
+ }
+ env = env->next;
+ }
+ return (0);
+}
+
+int ft_env(t_env *env)
+{
+ while (env != NULL)
+ {
+ printf("%s", env->name);
+ printf("=%s\n", env->value);
+ env = env->next;
+ }
+ return (0);
+}
diff --git a/src/collect_redirs.c b/src/collect_redirs.c
index 4b7b955..84ebd71 100644
--- a/src/collect_redirs.c
+++ b/src/collect_redirs.c
@@ -6,50 +6,14 @@
/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/02 13:49:31 by dkaiser #+# #+# */
-/* Updated: 2025/01/11 11:43:13 by chuhlig ### ########.fr */
+/* Updated: 2025/01/14 16:55:20 by chuhlig ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
-// static t_token *collect_redir(t_token **tokens, t_redirection *result,
-// t_token *cur);
-// static void collect_and_check_redir(t_token **tokens, t_redirection *result,
-// t_token **cur);
static void collect_and_check_redir(t_redirection *result, t_token **cur);
static void set_redir(t_redirection *redir, int type, char *specifier);
-// static int is_output_redir(int i);
-
-// static char *read_heredoc(char *delimiter)
-// {
-// char *line;
-// char *result;
-// size_t len;
-
-// result = NULL;
-// while (1)
-// {
-// line = readline("> ");
-// if (!line || ft_strcmp(line, delimiter) == 0)
-// {
-// free(line);
-// break ;
-// }
-// if (result)
-// len = ft_strlen(result);
-// else
-// len = 0;
-// result = realloc(result, len + ft_strlen(line) + 2);
-// if (!result)
-// return (NULL);
-// ft_strcpy(result + len, line);
-// strcat(result, "\n");
-// free(line);
-// }
-// return (result);
-// }
-
-//v2.0
static char *read_heredoc(char *delimiter)
{
@@ -74,8 +38,7 @@ static char *read_heredoc(char *delimiter)
if (!temp)
{
perror("malloc");
- free(result);
- return (NULL);
+ return (free(result), NULL);
}
if (result)
{
@@ -83,9 +46,7 @@ static char *read_heredoc(char *delimiter)
free(result);
}
else
- {
temp[0] = '\0';
- }
ft_strcat(temp, line);
ft_strcat(temp, "\n");
result = temp;
@@ -109,7 +70,7 @@ t_redirection *collect_redirs(t_token **tokens)
while (cur != NULL && cur->next != NULL)
{
if (cur->type == REDIR_TOKEN && cur->next->type == STRING_TOKEN)
- collect_and_check_redir(result, &cur);// her is diff
+ collect_and_check_redir(result, &cur);
else if (cur->type == REDIR_TOKEN)
return (free(result), NULL);
else
@@ -120,62 +81,12 @@ t_redirection *collect_redirs(t_token **tokens)
return (result);
}
-// static void collect_and_check_redir(t_token **tokens, t_redirection *result,
-// t_token **cur)
-// {
-// int is_redir_only;
-
-// is_redir_only = 0;
-// if ((*cur)->previous == NULL && (*cur)->next->next == NULL)
-// is_redir_only = 1;
-// *cur = collect_redir(tokens, result, *cur);
-// if (is_redir_only)
-// *tokens = NULL;
-// }
-
-// static t_token *collect_redir(t_token **tokens, t_redirection *result,
-// t_token *cur)
-// {
-// set_redir(&result[is_output_redir(cur->content.redir_type)],
-// cur->content.redir_type, cur->next->content.string);
-// cur = cur->next;
-// free_token_and_connect(cur->previous);
-// if (cur->next != NULL)
-// {
-// if (cur->previous == NULL)
-// *tokens = cur->next;
-// cur = cur->next;
-// free_token_and_connect(cur->previous);
-// }
-// else
-// {
-// free_token(cur);
-// return (NULL);
-// }
-// return (cur);
-// }
-
static void set_redir(t_redirection *redir, int type, char *specifier)
{
redir->type = type;
redir->specifier = specifier;
}
-// static int is_output_redir(int i)
-// {
-// if (i & (INPUT_FILE | INPUT_LIMITER))
-// return (0);
-// else if (i & (OUTPUT_APPEND | OUTPUT_OVERRIDE))
-// return (1);
-// else
-// {
-// panic(UNREACHABLE);
-// return (-1);
-// }
-// }
-
-//2.0
-
static void collect_and_check_redir(t_redirection *result, t_token **cur)
{
char *heredoc_data;
@@ -184,7 +95,6 @@ static void collect_and_check_redir(t_redirection *result, t_token **cur)
heredoc_data = NULL;
if ((*cur)->content.redir_type == INPUT_LIMITER)
{
- // Handle Here Document (<<)
heredoc_data = read_heredoc((*cur)->next->content.string);
if (!heredoc_data)
{
@@ -194,35 +104,21 @@ static void collect_and_check_redir(t_redirection *result, t_token **cur)
set_redir(&result[0], INPUT_LIMITER, heredoc_data);
}
else if ((*cur)->content.redir_type == INPUT_FILE)
- {
- // Handle Input File (<)
- set_redir(&result[0], INPUT_FILE, ft_strdup((*cur)->next->content.string));
- }
+ set_redir(&result[0], INPUT_FILE,
+ ft_strdup((*cur)->next->content.string));
else if ((*cur)->content.redir_type == OUTPUT_OVERRIDE)
- {
- // Handle Output File Overwrite (>)
- set_redir(&result[1], OUTPUT_OVERRIDE, ft_strdup((*cur)->next->content.string));
- }
+ set_redir(&result[1], OUTPUT_OVERRIDE,
+ ft_strdup((*cur)->next->content.string));
else if ((*cur)->content.redir_type == OUTPUT_APPEND)
- {
- // Handle Output File Append (>>)
- set_redir(&result[1], OUTPUT_APPEND, ft_strdup((*cur)->next->content.string));
- }
- else
- {
- // Handle unexpected cases
- printf("Unknown redirection type encountered\n");
- }
- // Advance the token pointer to skip the redirection token and its argument
+ set_redir(&result[1], OUTPUT_APPEND,
+ ft_strdup((*cur)->next->content.string));
next_token = (*cur)->next;
- free_token_and_connect(*cur); // Free the current redirection token
+ free_token_and_connect(*cur);
if (next_token)
{
- *cur = next_token->next; // Move to the next token after the argument
- free_token_and_connect(next_token); // Free the argument token
+ *cur = next_token->next;
+ free_token_and_connect(next_token);
}
else
- {
- *cur = NULL; // No more tokens
- }
-} \ No newline at end of file
+ *cur = NULL;
+}
diff --git a/src/env.c b/src/env.c
index 0213209..1972909 100644
--- a/src/env.c
+++ b/src/env.c
@@ -6,11 +6,12 @@
/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/17 14:31:07 by chuhlig #+# #+# */
-/* Updated: 2024/12/17 19:36:14 by chuhlig ### ########.fr */
+/* Updated: 2025/01/14 14:44:34 by chuhlig ### ########.fr */
/* */
/* ************************************************************************** */
#include "env.h"
+#include "minishell.h"
#include <stdlib.h>
void getenvlst(t_env **env, char **en)
@@ -59,4 +60,22 @@ char *env_get(t_env *env, char *name)
env = env->next;
}
return (NULL);
-} \ No newline at end of file
+}
+
+t_env *env_new(char *name)
+{
+ t_env *result;
+
+ result = malloc(sizeof(t_env));
+ if (!result)
+ return (NULL);
+ result->name = name;
+ return (result);
+}
+
+void free_env_node(t_env *node)
+{
+ free(node->name);
+ free(node->value);
+ free(node);
+}
diff --git a/src/env_to_strlst.c b/src/env_to_strlst.c
index c4c98c3..2031c24 100644
--- a/src/env_to_strlst.c
+++ b/src/env_to_strlst.c
@@ -6,7 +6,7 @@
/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/17 19:22:28 by chuhlig #+# #+# */
-/* Updated: 2024/12/17 19:22:36 by chuhlig ### ########.fr */
+/* Updated: 2025/01/14 14:10:49 by chuhlig ### ########.fr */
/* */
/* ************************************************************************** */
@@ -55,4 +55,4 @@ static char *get_var_assign(t_env *cur)
result = ft_strjoin(left_side, cur->value);
free(left_side);
return (result);
-} \ No newline at end of file
+}
diff --git a/src/execute_cmd.c b/src/execute_cmd.c
index 7f93c5a..b9faf71 100644
--- a/src/execute_cmd.c
+++ b/src/execute_cmd.c
@@ -6,81 +6,96 @@
/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/17 19:21:35 by chuhlig #+# #+# */
-/* Updated: 2025/01/09 16:07:01 by chuhlig ### ########.fr */
+/* Updated: 2025/01/14 16:56:24 by chuhlig ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
-#include <fcntl.h>
-#include <stdlib.h>
-#include <sys/_types/_pid_t.h>
-#include <sys/_types/_s_ifmt.h>
-#include <sys/fcntl.h>
-#include <unistd.h>
-// static void format_args(char **args, t_env *env);
+int is_builtin(char *cmd)
+{
+ return ((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));
+}
-// int execute_cmd(t_cmd *cmd, t_env **env)
-// {
-// int result;
-// char *cmd_path;
-// int fd;
+int execute_builtin(char **args, t_env **env)
+{
+ if (ft_strcmp(args[0], "export") == 0)
+ return (export(args, env));
+ else if (ft_strcmp(args[0], "unset") == 0)
+ return (unset(args, env));
+ else if (ft_strcmp(args[0], "cd") == 0)
+ return (cd(env, args));
+ else if (ft_strcmp(args[0], "echo") == 0)
+ return (echo(args));
+ else if (ft_strcmp(args[0], "pwd") == 0)
+ return (pwd(*env));
+ else if (ft_strcmp(args[0], "env") == 0)
+ return (ft_env(*env));
+ else if (ft_strcmp(args[0], "exit") == 0)
+ return (builtin_exit(args, env));
+ return (1);
+}
-// if (ft_strncmp(cmd->args[0], "pwd", 4) == 0)
-// return (pwd(*env));
-// else if (ft_strncmp(cmd->args[0], "echo", 5) == 0)
-// return (echo(cmd->args));
-// else if (ft_strncmp(cmd->args[0], "env", 4) == 0)
-// return (ft_env(*env));
-// else if (ft_strncmp(cmd->args[0], "cd", 3) == 0)
-// return (cd(env, cmd->args));
-// else if (ft_strncmp(cmd->args[0], "unset", 6) == 0)
-// return (unset(cmd->args, env));
-// else if (ft_strncmp(cmd->args[0], "export", 7) == 0)
-// return (export(cmd->args, env));
-// format_args(cmd->args, *env);
-// 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, 644);
-// 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, 644);
-// 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);
-// }
+int execute_cmd(t_cmd *cmd, t_env **env)
+{
+ char *cmd_path;
+ pid_t pid;
+ int status;
+ int original_stdout;
+ int original_stdin;
+ int result;
-// static void format_args(char **args, t_env *env)
-// {
-// char *formatted;
-
-// while (*args != NULL)
-// {
-// formatted = format_string(*args, env);
-// /* free(*args); */
-// *args = formatted;
-// args++;
-// }
-// } \ No newline at end of file
+ original_stdout = dup(STDOUT_FILENO);
+ original_stdin = dup(STDIN_FILENO);
+ if (handle_redirections(cmd->redirs) == -1)
+ {
+ dup2(original_stdout, STDOUT_FILENO);
+ dup2(original_stdin, STDIN_FILENO);
+ close(original_stdout);
+ close(original_stdin);
+ return (EXIT_FAILURE);
+ }
+ if (is_builtin(cmd->args[0]))
+ {
+ result = execute_builtin(cmd->args, env);
+ dup2(original_stdout, STDOUT_FILENO);
+ dup2(original_stdin, STDIN_FILENO);
+ close(original_stdout);
+ close(original_stdin);
+ return (result);
+ }
+ pid = fork();
+ if (pid == -1)
+ {
+ perror("fork");
+ dup2(original_stdout, STDOUT_FILENO);
+ dup2(original_stdin, STDIN_FILENO);
+ close(original_stdout);
+ close(original_stdin);
+ return (EXIT_FAILURE);
+ }
+ if (pid == 0)
+ {
+ cmd_path = get_cmd_path(cmd->args[0], *env);
+ if (!cmd_path)
+ {
+ printf("command not found\n");
+ exit(EXIT_FAILURE);
+ }
+ execve(cmd_path, cmd->args, env_to_strlst(*env));
+ perror("execve");
+ exit(EXIT_FAILURE);
+ }
+ waitpid(pid, &status, 0);
+ dup2(original_stdout, STDOUT_FILENO);
+ dup2(original_stdin, STDIN_FILENO);
+ close(original_stdout);
+ close(original_stdin);
+ return (WEXITSTATUS(status));
+}
diff --git a/src/format_string.c b/src/format_string.c
index bd7f703..7168ea0 100644
--- a/src/format_string.c
+++ b/src/format_string.c
@@ -6,7 +6,7 @@
/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/17 19:30:11 by chuhlig #+# #+# */
-/* Updated: 2024/12/17 19:31:54 by chuhlig ### ########.fr */
+/* Updated: 2025/01/14 14:21:36 by chuhlig ### ########.fr */
/* */
/* ************************************************************************** */
@@ -17,12 +17,6 @@
static void append_slice(char **dst, char *src, int start, int end);
static void append_var(char **dst, char *src, int *pos, t_env *env);
-enum e_format_mode
-{
- LITERAL = 1,
- VARIABLE = 2,
-};
-
char *format_string(char *str, t_env *env)
{
char *result;
@@ -42,14 +36,14 @@ char *format_string(char *str, t_env *env)
{
append_slice(&result, str, start, pos);
start = pos + 1;
- mode ^= LITERAL;
+ mode ^= 1;
}
- if (str[pos] == '"' && !(mode & LITERAL))
+ if (str[pos] == '"' && !(mode & 1))
{
append_slice(&result, str, start, pos);
start = pos + 1;
}
- if (str[pos] == '$' && !(mode & LITERAL))
+ if (str[pos] == '$' && !(mode & 1))
{
append_slice(&result, str, start, pos);
append_var(&result, str, &pos, env);
@@ -100,7 +94,7 @@ static void append_var(char **dst, char *src, int *pos, t_env *env)
i = 0;
*pos += 1;
while (src[*pos + i] != '\0' && src[*pos + i] != '\'' && src[*pos
- + i] != '"' && src[*pos + i] != '$')
+ + i] != '"' && src[*pos + i] != '$')
{
i++;
}
diff --git a/src/init.c b/src/init.c
index fd2c8ac..f41e64a 100644
--- a/src/init.c
+++ b/src/init.c
@@ -3,10 +3,10 @@
/* ::: :::::::: */
/* init.c :+: :+: :+: */
/* +:+ +:+ +:+ */
-/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */
+/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/24 15:02:24 by dkaiser #+# #+# */
-/* Updated: 2024/06/24 15:25:57 by dkaiser ### ########.fr */
+/* Updated: 2025/01/13 17:45:46 by chuhlig ### ########.fr */
/* */
/* ************************************************************************** */
diff --git a/src/interpreter.c b/src/interpreter.c
index 33d945c..af1eb82 100644
--- a/src/interpreter.c
+++ b/src/interpreter.c
@@ -6,326 +6,73 @@
/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/17 19:15:49 by chuhlig #+# #+# */
-/* Updated: 2025/01/11 12:52:57 by chuhlig ### ########.fr */
+/* Updated: 2025/01/14 16:59:45 by chuhlig ### ########.fr */
/* */
/* ************************************************************************** */
#include "debug_tools.h"
#include "minishell.h"
-#include <stdlib.h>
-#include <sys/_types/_pid_t.h>
-#include <sys/cdefs.h>
-#include <sys/wait.h>
-#include <unistd.h>
-#include <sys/fcntl.h>
-#include <sys/types.h>
-
-// static int eval_rec(t_node *node, t_env **env, int in_fd);
-
-// int eval(t_node *node, t_env **env)
-// {
-// pid_t pid;
-// int result;
-
-// if (node == NULL)
-// return (EXIT_FAILURE);
-// result = 0;
-// pid = fork();
-// if (pid < 0)
-// {
-// return (EXIT_FAILURE);
-// }
-// if (pid == 0)
-// {
-// result = eval_rec(node, env, STDIN_FILENO);
-// exit(result);
-// }
-// else
-// {
-// waitpid(pid, &result, 0);
-// }
-// return (result);
-// }
-
-// static int eval_rec(t_node *node, t_env **env, int in_fd)
-// {
-// pid_t pid;
-// int result;
-// int p[2];
-
-// result = pipe(p);
-// if (result == -1)
-// return (EXIT_FAILURE);
-// if (node->type == PIPE_NODE)
-// {
-// pid = fork();
-// if (pid < 0)
-// {
-// return (EXIT_FAILURE);
-// }
-// if (pid == 0)
-// {
-// close(p[0]);
-// dup2(in_fd, STDIN_FILENO);
-// dup2(p[1], STDOUT_FILENO);
-// result = execute_cmd(&node->content.pipe.left->content.cmd, env);
-// exit(result);
-// }
-// else
-// {
-// close(p[1]);
-// dup2(p[0], STDIN_FILENO);
-// result = eval_rec(node->content.pipe.right, env, p[0]);
-// }
-// }
-// else if (node->type == CMD_NODE)
-// {
-// result = execute_cmd(&node->content.cmd, env);
-// }
-// else
-// {
-// panic(UNREACHABLE);
-// return (EXIT_FAILURE);
-// }
-// return (result);
-// }
-
-
-//old interpreter
-// #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);
-// }
-
-//v2.0
-
-int is_builtin(char *cmd)
+int handle_redirections(t_redirection *redirs)
{
- return ((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));
-}
+ int fd;
-int execute_builtin(char **args, t_env **env)
-{
- if (ft_strcmp(args[0], "export") == 0)
- return (export(args, env));
- else if (ft_strcmp(args[0], "unset") == 0)
- return (unset(args, env));
- else if (ft_strcmp(args[0], "cd") == 0)
- return (cd(env, args));
- else if (ft_strcmp(args[0], "exit") == 0)
- return (EXIT_SUCCESS);
- else if (ft_strcmp(args[0], "echo") == 0)
- return (echo(args));
- else if (ft_strcmp(args[0], "pwd") == 0)
- return (pwd(*env));
- else if (ft_strcmp(args[0], "env") == 0)
- return (ft_env(*env));
- return (1);
-}
-// v1.0
-
-// static int handle_redirections(t_redirection *redirs)
-// {
-// int fd;
-// int pipe_fds[2];
-
-// if (redirs[0].type == INPUT_LIMITER)
-// {
-// if (pipe(pipe_fds) == -1)
-// {
-// perror("pipe");
-// return (-1);
-// }
-// write(pipe_fds[1], redirs[0].specifier, strlen(redirs[0].specifier));
-// close(pipe_fds[1]);
-// dup2(pipe_fds[0], STDIN_FILENO);
-// close(pipe_fds[0]);
-// }
-// else if (redirs[0].type == INPUT_FILE)
-// {
-// fd = open(redirs[0].specifier, O_RDONLY);
-// if (fd == -1)
-// {
-// perror(redirs[0].specifier);
-// return (-1);
-// }
-// dup2(fd, STDIN_FILENO);
-// close(fd);
-// }
-// if (redirs[1].type == OUTPUT_OVERRIDE)
-// {
-// fd = open(redirs[1].specifier, O_WRONLY | O_CREAT | O_TRUNC, 0644);
-// if (fd == -1)
-// {
-// perror(redirs[1].specifier);
-// return (-1);
-// }
-// dup2(fd, STDOUT_FILENO);
-// close(fd);
-// }
-// else if (redirs[1].type == OUTPUT_APPEND)
-// {
-// fd = open(redirs[1].specifier, O_WRONLY | O_CREAT | O_APPEND, 0644);
-// if (fd == -1)
-// {
-// perror(redirs[1].specifier);
-// return (-1);
-// }
-// dup2(fd, STDOUT_FILENO);
-// close(fd);
-// }
-// return (0);
-// }
-//v 3.0
-static int handle_redirections(t_redirection *redirs)
-{
- int fd;
-
- if (redirs[0].type == INPUT_FILE)
- {
- fd = open(redirs[0].specifier, O_RDONLY);
- if (fd < 0)
- {
- perror("open");
- return (-1);
- }
- dup2(fd, STDIN_FILENO);
- close(fd);
- }
- else if (redirs[0].type == INPUT_LIMITER)
- {
- // Handle here document (<<)
- // Assuming heredoc_data is stored in redirs[0].specifier
- fd = open("/tmp/heredoc_tmp", O_WRONLY | O_CREAT | O_TRUNC, 0644);
- if (fd < 0)
- {
- perror("open");
- return (-1);
- }
- write(fd, redirs[0].specifier, strlen(redirs[0].specifier));
- close(fd);
- fd = open("/tmp/heredoc_tmp", O_RDONLY);
- if (fd < 0)
- {
- perror("open");
- return (-1);
- }
- dup2(fd, STDIN_FILENO);
- close(fd);
- }
- if (redirs[1].type == OUTPUT_OVERRIDE)
- {
- fd = open(redirs[1].specifier, O_WRONLY | O_CREAT | O_TRUNC, 0644);
- if (fd < 0)
- {
- perror("open");
- return (-1);
- }
- dup2(fd, STDOUT_FILENO);
- close(fd);
- }
- else if (redirs[1].type == OUTPUT_APPEND)
- {
- fd = open(redirs[1].specifier, O_WRONLY | O_CREAT | O_APPEND, 0644);
- if (fd < 0)
- {
- perror("open");
- return (-1);
- }
- dup2(fd, STDOUT_FILENO);
- close(fd);
- }
- return (0);
+ if (redirs[0].type == INPUT_FILE)
+ {
+ fd = open(redirs[0].specifier, O_RDONLY);
+ if (fd < 0)
+ {
+ perror(redirs[0].specifier);
+ return (-1);
+ }
+ dup2(fd, STDIN_FILENO);
+ close(fd);
+ }
+ else if (redirs[0].type == INPUT_LIMITER)
+ {
+ fd = open("/tmp/heredoc_tmp", O_WRONLY | O_CREAT | O_TRUNC, 0644);
+ if (fd < 0)
+ {
+ perror("open");
+ return (-1);
+ }
+ write(fd, redirs[0].specifier, ft_strlen(redirs[0].specifier));
+ close(fd);
+ fd = open("/tmp/heredoc_tmp", O_RDONLY);
+ if (fd < 0)
+ {
+ perror("open");
+ return (-1);
+ }
+ dup2(fd, STDIN_FILENO);
+ close(fd);
+ }
+ if (redirs[1].type == OUTPUT_OVERRIDE)
+ {
+ fd = open(redirs[1].specifier, O_WRONLY | O_CREAT | O_TRUNC, 0644);
+ if (fd < 0)
+ {
+ perror("open");
+ return (-1);
+ }
+ dup2(fd, STDOUT_FILENO);
+ close(fd);
+ }
+ else if (redirs[1].type == OUTPUT_APPEND)
+ {
+ fd = open(redirs[1].specifier, O_WRONLY | O_CREAT | O_APPEND, 0644);
+ if (fd < 0)
+ {
+ perror("open");
+ return (-1);
+ }
+ dup2(fd, STDOUT_FILENO);
+ close(fd);
+ }
+ return (0);
}
-//v 2.0
-// static int eval_rec(t_node *node, t_env **env, int in_fd)
-// {
-// pid_t pid;
-// int p[2];
-// int result;
-// int status;
-
-// if (node->type == PIPE_NODE)
-// {
-// pipe(p);
-// pid = fork();
-// if (pid == 0)
-// {
-// close(p[0]);
-// dup2(in_fd, STDIN_FILENO);
-// dup2(p[1], STDOUT_FILENO);
-// result = eval_rec(node->content.pipe.left, env, in_fd);
-// exit(result);
-// }
-// else
-// {
-// close(p[1]);
-// dup2(p[0], STDIN_FILENO);
-// result = eval_rec(node->content.pipe.right, env, p[0]);
-// waitpid(pid, &status, 0);
-// }
-// }
-// else if (node->type == CMD_NODE)
-// {
-// result = execute_cmd(&node->content.cmd, env);
-// }
-// else
-// {
-// panic("UNREACHABLE");
-// result = EXIT_FAILURE;
-// }
-// return (result);
-// }
-
-//v 3.0
-static int eval_rec(t_node *node, t_env **env, int in_fd)
+int eval_rec(t_node *node, t_env **env, int in_fd)
{
pid_t pid;
int p[2];
@@ -357,117 +104,13 @@ static int eval_rec(t_node *node, t_env **env, int in_fd)
}
}
else if (node->type == CMD_NODE)
- {
result = execute_cmd(&node->content.cmd, env);
- }
else
- {
- printf("Handling unknown node type\n");
- panic("UNREACHABLE");
result = EXIT_FAILURE;
- }
return (result);
}
-int eval(t_node *node, t_env **env)
+int eval(t_node *node, t_env **env)
{
return (eval_rec(node, env, STDIN_FILENO));
}
-//v was auch immmer
-
-// int execute_cmd(t_cmd *cmd, t_env **env)
-// {
-// char *cmd_path;
-// pid_t pid;
-// int status;
-
-// if (handle_redirections(cmd->redirs) == -1)
-// {
-// return (EXIT_FAILURE);
-// }
-// if (is_builtin(cmd->args[0]))
-// {
-// return (execute_builtin(cmd->args, env));
-// }
-// pid = fork();
-// if (pid == -1)
-// {
-// perror("fork");
-// return (EXIT_FAILURE);
-// }
-// if (pid == 0)
-// {
-// cmd_path = get_cmd_path(cmd->args[0], *env);
-// if (!cmd_path)
-// {
-// printf("%s: command not found\n", cmd->args[0]);
-// return (EXIT_FAILURE);
-// }
-// execve(cmd_path, cmd->args, env_to_strlst(*env));
-// perror("execve");
-// exit(EXIT_FAILURE);
-// }
-// waitpid(pid, &status, 0);
-// return (WEXITSTATUS(status));
-// }
-int execute_cmd(t_cmd *cmd, t_env **env)
-{
- char *cmd_path;
- pid_t pid;
- int status;
- int original_stdout;
- int original_stdin;
-
- original_stdout = dup(STDOUT_FILENO);
- original_stdin = dup(STDIN_FILENO);
-
- if (handle_redirections(cmd->redirs) == -1)
- {
- dup2(original_stdout, STDOUT_FILENO);
- dup2(original_stdin, STDIN_FILENO);
- close(original_stdout);
- close(original_stdin);
- return (EXIT_FAILURE);
- }
-
- if (is_builtin(cmd->args[0]))
- {
- int result = execute_builtin(cmd->args, env);
- dup2(original_stdout, STDOUT_FILENO);
- dup2(original_stdin, STDIN_FILENO);
- close(original_stdout);
- close(original_stdin);
- return (result);
- }
-
- pid = fork();
- if (pid == -1)
- {
- perror("fork");
- dup2(original_stdout, STDOUT_FILENO);
- dup2(original_stdin, STDIN_FILENO);
- close(original_stdout);
- close(original_stdin);
- return (EXIT_FAILURE);
- }
- if (pid == 0)
- {
- cmd_path = get_cmd_path(cmd->args[0], *env);
- if (!cmd_path)
- {
- printf("%s: command not found\n", cmd->args[0]);
- exit(EXIT_FAILURE);
- }
- execve(cmd_path, cmd->args, env_to_strlst(*env));
- perror("execve");
- exit(EXIT_FAILURE);
- }
- waitpid(pid, &status, 0);
-
- dup2(original_stdout, STDOUT_FILENO);
- dup2(original_stdin, STDIN_FILENO);
- close(original_stdout);
- close(original_stdin);
-
- return (WEXITSTATUS(status));
-} \ No newline at end of file
diff --git a/src/main.c b/src/main.c
index a53760c..8abc4c9 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/12/17 19:26:42 by chuhlig ### ########.fr */
+/* Updated: 2025/01/14 15:29:20 by chuhlig ### ########.fr */
/* */
/* ************************************************************************** */
@@ -14,13 +14,17 @@
int main(int argc, char *argv[], char *envp[])
{
- t_env *env;
+ t_env *env;
+ static int promptflag;
env = NULL;
+ promptflag = 0;
if (!argc && !argv)
return (1);
if (init())
return (1);
getenvlst(&env, envp);
- repl("Minishell $ ", &env);
+ repl("Minishell $ ", &env, &promptflag);
+ free_envlst(&env);
+ return (0);
}
diff --git a/src/repl.c b/src/repl.c
index 7ff80a8..7eb6c0d 100644
--- a/src/repl.c
+++ b/src/repl.c
@@ -6,56 +6,38 @@
/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/24 16:07:04 by dkaiser #+# #+# */
-/* Updated: 2025/01/11 16:01:44 by chuhlig ### ########.fr */
+/* Updated: 2025/01/14 15:39:58 by chuhlig ### ########.fr */
/* */
/* ************************************************************************** */
#include "../include/minishell.h"
#include "token.h"
-t_token *reverse_token_list(t_token *head)
-{
- t_token *prev;
- t_token *current;
- t_token *next;
-
- prev = NULL;
- current = head;
- next = NULL;
- while (current != NULL)
- {
- next = current->previous;
- current->next = prev;
- current->previous = next;
- prev = current;
- current = next;
- }
- return (prev);
-}
-
-void repl(const char *prompt, t_env **env)
+void repl(const char *prompt, t_env **env, int *promptflag)
{
char *input;
t_token *token_list;
t_list *lines;
+ (*promptflag)++;
while (1)
{
input = readline(prompt);
if (input == NULL)
- return ;
+ {
+ if (*promptflag > 1)
+ (*promptflag)--;
+ printf("exit\n");
+ break ;
+ }
if (input[0] == '\0')
continue ;
add_history(input);
token_list = NULL;
tokenizer(input, &token_list, '\0');
- token_list = reverse_token_list(token_list);
lines = parse(token_list, env);
if (lines)
- {
- print_ast(lines->content);
eval(lines->content, env);
- }
free(input);
}
}
diff --git a/src/signal_handling.c b/src/signal_handling.c
index a19fa94..c6273d0 100644
--- a/src/signal_handling.c
+++ b/src/signal_handling.c
@@ -3,10 +3,10 @@
/* ::: :::::::: */
/* signal_handling.c :+: :+: :+: */
/* +:+ +:+ +:+ */
-/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */
+/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/24 15:08:43 by dkaiser #+# #+# */
-/* Updated: 2024/06/25 13:33:26 by dkaiser ### ########.fr */
+/* Updated: 2025/01/14 14:11:29 by chuhlig ### ########.fr */
/* */
/* ************************************************************************** */
diff --git a/src/tokenizer.c b/src/tokenizer.c
index 26edd2e..6d16f1d 100644
--- a/src/tokenizer.c
+++ b/src/tokenizer.c
@@ -6,13 +6,33 @@
/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/28 20:55:50 by chuhlig #+# #+# */
-/* Updated: 2025/01/11 15:22:07 by chuhlig ### ########.fr */
+/* Updated: 2025/01/14 15:58:41 by chuhlig ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
#include "token.h"
+t_token *reverse_token_list(t_token *head)
+{
+ t_token *prev;
+ t_token *current;
+ t_token *next;
+
+ prev = NULL;
+ current = head;
+ next = NULL;
+ while (current != NULL)
+ {
+ next = current->previous;
+ current->next = prev;
+ current->previous = next;
+ prev = current;
+ current = next;
+ }
+ return (prev);
+}
+
void print_token(t_token *token)
{
if (DEBUG)
@@ -77,7 +97,6 @@ void handle_special_chars(char *s, int *i, int *start, t_token **token_list)
*token_list = new_token(PIPE_TOKEN, *token_list, NULL);
else if (s[*i] == '\n')
*token_list = new_token(NEWLINE_TOKEN, *token_list, NULL);
- print_token(*token_list);
if (s[*i] == '<' && s[*i + 1] == '<')
(*i)++;
if (s[*i] == '>' && s[*i + 1] == '>')
@@ -111,4 +130,5 @@ void tokenizer(char *s, t_token **token_list, char quote_check)
pos = i + 1;
}
}
+ *token_list = reverse_token_list(*token_list);
}