aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDominik Kaiser2025-01-20 18:46:39 +0100
committerGitHub2025-01-20 18:46:39 +0100
commit9fa887da20e409c2f25fac44b57f999e508a30ea (patch)
tree7e3fee3edea18d0d81abddb89104858cfc0ec5e3 /src
parentaf9d1a9b39daaf1b86cf94ee629e06503d8ab6d4 (diff)
parent62845ce01f222fad126372c574c7ab084478adf0 (diff)
downloadminishell-9fa887da20e409c2f25fac44b57f999e508a30ea.tar.gz
minishell-9fa887da20e409c2f25fac44b57f999e508a30ea.zip
Merge pull request #30 from dpu-kaiser/merge_format_redir
Merge format redir
Diffstat (limited to 'src')
-rw-r--r--src/builtins_part_one.c144
-rw-r--r--src/builtins_part_three.c85
-rw-r--r--src/builtins_part_two.c5
-rw-r--r--src/collect_redirs.c51
-rw-r--r--src/create_files.c16
-rw-r--r--src/debug_tools.c6
-rw-r--r--src/env.c4
-rw-r--r--src/env_to_strlst.c29
-rw-r--r--src/execute_cmd.c4
-rw-r--r--src/format_string.c112
-rw-r--r--src/free_token.c7
-rw-r--r--src/handle_redir.c103
-rw-r--r--src/interpreter.c89
-rw-r--r--src/new_node.c5
-rw-r--r--src/parse_cmd.c13
-rw-r--r--src/parser.c4
-rw-r--r--src/repl.c6
-rw-r--r--src/signal_handling.c2
18 files changed, 410 insertions, 275 deletions
diff --git a/src/builtins_part_one.c b/src/builtins_part_one.c
index f3bcfc0..629cc87 100644
--- a/src/builtins_part_one.c
+++ b/src/builtins_part_one.c
@@ -6,60 +6,13 @@
/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/09 17:01:16 by chuhlig #+# #+# */
-/* Updated: 2025/01/14 19:51:59 by chuhlig ### ########.fr */
+/* Updated: 2025/01/20 17:05:19 by chuhlig ### ########.fr */
/* */
/* ************************************************************************** */
#include "env.h"
#include <stdio.h>
-int echo(char **av)
-{
- int i;
- int f;
-
- i = 1;
- f = 1;
- if (av[1] == NULL || av[1][0] == '\0')
- {
- write(1, "\n", 1);
- return (0);
- }
- if (ft_strncmp(av[1], "-n", 3) == 0)
- {
- i++;
- f = 0;
- }
- while (av[i])
- {
- write(1, av[i], ft_strlen(av[i]));
- i++;
- if (av[i])
- write(1, " ", 1);
- }
- if (f)
- write(1, "\n", 1);
- return (0);
-}
-
-void exit_shell(t_env **env, int exit_status)
-{
- free_envlst(env);
- exit(exit_status);
-}
-
-int builtin_exit(char **args, t_env **env)
-{
- int exit_status;
-
- 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)
{
t_env *current;
@@ -69,13 +22,19 @@ int unset(char **av, t_env **env)
i = 0;
while (av[++i])
{
- if (ft_strchr(av[i], '?'))
- return (1);
current = *env;
prev = NULL;
while (current)
{
- if (ft_strcmp(current->name, av[i]) == 0)
+ if ((!ft_strcmp(current->name, av[i])) && (!ft_strcmp("?", av[1])))
+ {
+ if (prev)
+ prev->next = current->next;
+ else
+ *env = current->next;
+ free_env_node(current);
+ break ;
+ }
{
if (prev)
prev->next = current->next;
@@ -95,6 +54,8 @@ t_env *check_existing(t_env *env, char *av)
{
while (env)
{
+ if (ft_strcmp("$", av) == 0)
+ return (NULL);
if (ft_strcmp(env->name, av) == 0)
return (env);
env = env->next;
@@ -102,51 +63,66 @@ t_env *check_existing(t_env *env, char *av)
return (NULL);
}
-int export(char **av, t_env **env)
+void export_export(char *av, t_env **env)
{
char *tmp;
t_env *current;
- int i;
current = NULL;
+ tmp = ft_strchr(av, '=');
+ *tmp = '\0';
+ current = check_existing(*env, av);
+ if (current)
+ free(current->value);
+ else
+ {
+ current = env_new(ft_strdup(av));
+ current->next = *env;
+ *env = current;
+ }
+ current->value = ft_strdup(tmp + 1);
+}
+
+int is_valid_identifier(char *str)
+{
+ int i;
+
i = 0;
- while (av[++i])
+ if (!ft_isalpha(str[0]) && str[0] != '_')
+ return (0);
+ while (str[i] && str[i] != '=')
{
- if ((ft_strchr(av[i], '=')))
- {
- tmp = ft_strchr(av[i], '=');
- *tmp = '\0';
- if (ft_strchr(av[i], '?'))
- return (1);
- current = check_existing(*env, av[i]);
- if (current)
- free(current->value);
- else
- {
- current = env_new(ft_strdup(av[i]));
- current->next = *env;
- *env = current;
- }
- current->value = ft_strdup(tmp + 1);
- }
- else
- return (1);
+ if (!ft_isalnum(str[i]) && str[i] != '_')
+ return (0);
+ i++;
}
- return (0);
+ return (1);
}
-void set_return_code(int return_code, t_env **env)
+int export(char **av, t_env **env, int f)
{
- t_env *cur;
+ char *equal_sign;
+ int i;
- cur = check_existing(*env, "?");
- if (cur)
- free(cur->value);
- else
+ i = 0;
+ while (av[++i])
{
- cur = env_new(ft_strdup("?"));
- cur->next = *env;
- *env = cur;
+ equal_sign = ft_strchr(av[i], '=');
+ if (equal_sign)
+ *equal_sign = '\0';
+ if (!is_valid_identifier(av[i]))
+ {
+ write(1, "Minishell $ export: not a valid identifier\n", 43);
+ if (equal_sign)
+ *equal_sign = '=';
+ f++;
+ continue ;
+ }
+ if (equal_sign)
+ {
+ *equal_sign = '=';
+ export_export(av[i], env);
+ }
}
- cur->value = ft_itoa(return_code);
+ return (check_flag(f));
}
diff --git a/src/builtins_part_three.c b/src/builtins_part_three.c
new file mode 100644
index 0000000..5f6fa31
--- /dev/null
+++ b/src/builtins_part_three.c
@@ -0,0 +1,85 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* builtins_part_three.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2025/01/18 18:29:24 by chuhlig #+# #+# */
+/* Updated: 2025/01/20 17:08:17 by chuhlig ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "env.h"
+
+void exit_shell(t_env **env, int exit_status)
+{
+ free_envlst(env);
+ exit(exit_status);
+}
+
+int builtin_exit(char **av, t_env **env)
+{
+ int exit_status;
+
+ if (av[1] && !av[2])
+ exit_status = ft_atoi(av[1]);
+ else if (av[2])
+ exit_status = 1;
+ else
+ exit_status = 0;
+ exit_shell(env, exit_status);
+ return (exit_status);
+}
+
+void set_return_code(int return_code, t_env **env)
+{
+ t_env *cur;
+
+ cur = check_existing(*env, "?");
+ if (cur)
+ free(cur->value);
+ else
+ {
+ cur = env_new(ft_strdup("?"));
+ cur->next = *env;
+ *env = cur;
+ }
+ cur->value = ft_itoa(return_code);
+}
+
+int echo(char **av)
+{
+ int i;
+ int f;
+
+ i = 1;
+ f = 1;
+ if (av[1] == NULL || av[1][0] == '\0')
+ {
+ write(1, "\n", 1);
+ return (0);
+ }
+ if (ft_strncmp(av[1], "-n", 3) == 0)
+ {
+ i++;
+ f = 0;
+ }
+ while (av[i])
+ {
+ write(1, av[i], ft_strlen(av[i]));
+ i++;
+ if (av[i])
+ write(1, " ", 1);
+ }
+ if (f)
+ write(1, "\n", 1);
+ return (0);
+}
+
+int check_flag(int f)
+{
+ if (f)
+ return (1);
+ return (0);
+}
diff --git a/src/builtins_part_two.c b/src/builtins_part_two.c
index 05d6943..9fabc81 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: 2025/01/14 19:31:17 by chuhlig ### ########.fr */
+/* Updated: 2025/01/18 18:57:12 by chuhlig ### ########.fr */
/* */
/* ************************************************************************** */
@@ -72,6 +72,7 @@ int cd(t_env **env, char **av)
}
if (chdir(current->value) == -1)
return (1);
+ update_pwd(env);
}
else
{
@@ -101,7 +102,7 @@ int ft_env(t_env *env)
{
while (env != NULL)
{
- if (strchr(env->name, '?'))
+ if (ft_strchr(env->name, '?'))
{
env = env->next;
continue ;
diff --git a/src/collect_redirs.c b/src/collect_redirs.c
index 350cf6b..837a16a 100644
--- a/src/collect_redirs.c
+++ b/src/collect_redirs.c
@@ -20,7 +20,8 @@ static t_redirection *set_redir(t_redirection *redir, int type, char *spec,
static int set_heredoc_data(t_token *cur, t_redirection *result,
t_env *env);
-t_redirection *collect_redirs(t_token **tokens, t_env *env, t_list **create_files)
+t_redirection *collect_redirs(t_token **tokens, t_env *env,
+ t_list **create_files)
{
t_redirection *result;
t_token *cur;
@@ -45,28 +46,6 @@ t_redirection *collect_redirs(t_token **tokens, t_env *env, t_list **create_file
return (result);
}
-static t_redirection *set_redir(t_redirection *redir, int type, char *spec,
- t_env *env)
-{
- t_redirection *result;
-
- redir->type = type;
- if (spec != NULL)
- redir->specifier = format_string(spec, env);
- else
- redir->specifier = spec;
- if (redir->type == OUTPUT_APPEND || redir->type == OUTPUT_OVERRIDE)
- {
- result = malloc(sizeof(t_redirection));
- if (!result)
- return (NULL);
- result->type = type;
- result->specifier = spec;
- return (result);
- }
- return (NULL);
-}
-
static void collect_and_check_redir(t_redirection *result, t_token **cur,
t_env *env, t_list **create_files)
{
@@ -89,7 +68,7 @@ static void collect_and_check_redir(t_redirection *result, t_token **cur,
ft_lstadd_back(create_files, ft_lstnew(set_redir(&result[1],
OUTPUT_APPEND, format_string(str, env), env)));
next_token = (*cur)->next;
- free_token_and_connect(*cur);
+ // free_token_and_connect(*cur);
if (next_token)
{
*cur = next_token->next;
@@ -99,6 +78,30 @@ static void collect_and_check_redir(t_redirection *result, t_token **cur,
*cur = NULL;
}
+static t_redirection *set_redir(t_redirection *redir, int type, char *spec,
+ t_env *env)
+{
+ t_redirection *result;
+
+ redir->type = type;
+ if (spec != NULL)
+ redir->specifier = format_string(spec, env, ft_atoi("0"));
+ else
+ redir->specifier = spec;
+ if (redir->type == OUTPUT_APPEND || redir->type == OUTPUT_OVERRIDE)
+ {
+ result = malloc(sizeof(t_redirection));
+ if (!result)
+ return (NULL);
+ result->type = type;
+ result->specifier = spec;
+ return (result);
+ }
+ return (NULL);
+}
+
+
+
static int set_heredoc_data(t_token *cur, t_redirection *result, t_env *env)
{
char *heredoc_data;
diff --git a/src/create_files.c b/src/create_files.c
index faee27f..8689f88 100644
--- a/src/create_files.c
+++ b/src/create_files.c
@@ -3,10 +3,10 @@
/* ::: :::::::: */
/* create_files.c :+: :+: :+: */
/* +:+ +:+ +:+ */
-/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */
+/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/16 16:23:51 by dkaiser #+# #+# */
-/* Updated: 2025/01/16 19:16:33 by dkaiser ### ########.fr */
+/* Updated: 2025/01/19 14:36:59 by chuhlig ### ########.fr */
/* */
/* ************************************************************************** */
@@ -22,10 +22,10 @@ void create_files(t_list *files)
{
dbg("Test");
if (files->content == NULL)
- continue;
+ continue ;
file = (t_redirection *)files->content;
if (access(file->specifier, F_OK) != -1 && access(file->specifier, W_OK) == -1)
- break;
+ break ;
if (file->type == OUTPUT_OVERRIDE)
{
fd = open(file->specifier, O_WRONLY | O_CREAT | O_TRUNC, 0644);
@@ -36,10 +36,10 @@ void create_files(t_list *files)
fd = open(file->specifier, O_WRONLY | O_CREAT | O_APPEND, 0644);
close(fd);
}
- /* if (files->next == NULL) */
- /* break; */
- /* if (((t_redirection *) files->next->content)->type == 0) */
- /* break; */
+ if (files->next == NULL)
+ break ;
+ if (((t_redirection *) files->next->content)->type == 0)
+ break ;
files = files->next;
}
}
diff --git a/src/debug_tools.c b/src/debug_tools.c
index de59703..6bee1b0 100644
--- a/src/debug_tools.c
+++ b/src/debug_tools.c
@@ -3,14 +3,16 @@
/* ::: :::::::: */
/* debug_tools.c :+: :+: :+: */
/* +:+ +:+ +:+ */
-/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */
+/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/24 15:34:14 by dkaiser #+# #+# */
-/* Updated: 2024/06/28 15:04:43 by dkaiser ### ########.fr */
+/* Updated: 2025/01/20 12:50:36 by chuhlig ### ########.fr */
/* */
/* ************************************************************************** */
#include "debug_tools.h"
+#include <stdio.h>
+#include <stdarg.h>
void dbg(char *msg)
{
diff --git a/src/env.c b/src/env.c
index 1972909..cc9cfae 100644
--- a/src/env.c
+++ b/src/env.c
@@ -6,7 +6,7 @@
/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/17 14:31:07 by chuhlig #+# #+# */
-/* Updated: 2025/01/14 14:44:34 by chuhlig ### ########.fr */
+/* Updated: 2025/01/20 15:05:49 by chuhlig ### ########.fr */
/* */
/* ************************************************************************** */
@@ -55,7 +55,7 @@ char *env_get(t_env *env, char *name)
{
while (env != NULL)
{
- if (!ft_strncmp(env->name, name, ft_strlen(name)))
+ if (!ft_strncmp(env->name, name, ft_strlen(env->name)))
return (env->value);
env = env->next;
}
diff --git a/src/env_to_strlst.c b/src/env_to_strlst.c
index a1ab7cc..5806d96 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: 2025/01/14 19:34:10 by chuhlig ### ########.fr */
+/* Updated: 2025/01/18 18:50:49 by chuhlig ### ########.fr */
/* */
/* ************************************************************************** */
@@ -15,25 +15,32 @@
static char *get_var_assign(t_env *cur);
-char **env_to_strlst(t_env *env)
+static int getsize(t_env *env)
{
int size;
t_env *cur;
- char **result;
- int i;
size = 0;
cur = env;
- while (cur != NULL)
+ while (cur)
{
- if (ft_strchr(cur->name, '?'))
- {
- cur = cur->next;
- continue ;
- }
- size++;
+ if (!ft_strchr(cur->name, '?'))
+ size++;
cur = cur->next;
}
+ return (size);
+}
+
+char **env_to_strlst(t_env *env)
+{
+ int size;
+ t_env *cur;
+ char **result;
+ int i;
+
+ size = 0;
+ cur = env;
+ size = getsize(env);
result = malloc(sizeof(char *) * (size + 1));
if (result == NULL)
return (NULL);
diff --git a/src/execute_cmd.c b/src/execute_cmd.c
index 8f5b541..4b84e12 100644
--- a/src/execute_cmd.c
+++ b/src/execute_cmd.c
@@ -6,7 +6,7 @@
/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/17 19:21:35 by chuhlig #+# #+# */
-/* Updated: 2025/01/16 18:38:00 by dkaiser ### ########.fr */
+/* Updated: 2025/01/20 15:43:41 by chuhlig ### ########.fr */
/* */
/* ************************************************************************** */
@@ -28,7 +28,7 @@ int is_builtin(char *cmd)
int execute_builtin(char **args, t_env **env)
{
if (ft_strcmp(args[0], "export") == 0)
- return (export(args, env));
+ return (export(args, env, ft_atoi("0")));
else if (ft_strcmp(args[0], "unset") == 0)
return (unset(args, env));
else if (ft_strcmp(args[0], "cd") == 0)
diff --git a/src/format_string.c b/src/format_string.c
index 5f31130..7e64039 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: 2025/01/14 18:06:17 by dkaiser ### ########.fr */
+/* Updated: 2025/01/19 20:24:05 by chuhlig ### ########.fr */
/* */
/* ************************************************************************** */
@@ -14,46 +14,18 @@
#include "libft.h"
#include "minishell.h"
-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);
-
-char *format_string(char *str, t_env *env)
+void append_var_exit_code(char **dst, t_env *env)
{
+ char *exit_code;
char *result;
- int pos;
- int start;
- int is_literal;
- pos = 0;
- start = 0;
- is_literal = 0;
- result = NULL;
- if (str == NULL)
- return (NULL);
- while (str[pos] != '\0')
+ exit_code = env_get(env, "?");
+ if (exit_code)
{
- if (str[pos] == '\'')
- {
- append_slice(&result, str, start, pos);
- start = pos + 1;
- is_literal = !is_literal;
- }
- if (str[pos] == '"' && !is_literal)
- {
- append_slice(&result, str, start, pos);
- start = pos + 1;
- }
- if (str[pos] == '$' && !is_literal)
- {
- append_slice(&result, str, start, pos);
- append_var(&result, str, &pos, env);
- start = pos;
- continue ;
- }
- pos++;
+ result = ft_strjoin(*dst, exit_code);
+ free(*dst);
+ *dst = result;
}
- append_slice(&result, str, start, pos);
- return (result);
}
static void append_slice(char **dst, char *src, int start, int end)
@@ -65,9 +37,7 @@ static void append_slice(char **dst, char *src, int start, int end)
if (*dst != NULL)
len = ft_strlen(*dst);
else
- {
len = 0;
- }
result = malloc(len + (end - start) + 1);
if (!result)
return ;
@@ -93,27 +63,63 @@ 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] != '$')
- {
+ while (ft_isalnum(src[*pos + i]) || src[*pos + i] == '_')
i++;
- }
- var = malloc(i + 1);
- if (var == NULL)
+ if (i == 0)
return ;
- var[i] = '\0';
- i--;
- while (i >= 0)
- {
- var[i] = src[*pos + i];
- i--;
- }
+ var = ft_substr(src, *pos, i);
value = env_get(env, var);
- if (value != NULL)
+ if (value)
{
result = ft_strjoin(*dst, value);
free(*dst);
*dst = result;
}
- *pos += ft_strlen(var);
+ *pos += i;
+ free(var);
+}
+
+static void handle_dollar_sign(char **result, char *str, int *pos, t_env *env)
+{
+ if (str[*pos + 1] == '?')
+ {
+ append_var_exit_code(result, env);
+ *pos += 2;
+ }
+ else if (ft_isalnum(str[*pos + 1]) || str[*pos + 1] == '_')
+ append_var(result, str, pos, env);
+ else
+ {
+ append_slice(result, str, *pos, *pos + 1);
+ (*pos)++;
+ }
+}
+
+char *format_string(char *str, t_env *env, int is_literal)
+{
+ char *result;
+ int pos;
+ int start;
+
+ pos = 0;
+ start = 0;
+ result = NULL;
+ if (!str)
+ return (NULL);
+ while (str[pos])
+ {
+ if (str[pos] == '\'' || (str[pos] == '\"' && !is_literal)
+ || (str[pos] == '$' && !is_literal))
+ {
+ append_slice(&result, str, start, pos);
+ if (str[pos] == '$')
+ handle_dollar_sign(&result, str, &pos, env);
+ else
+ is_literal ^= (str[pos++] == '\'');
+ start = pos;
+ continue ;
+ }
+ pos++;
+ }
+ return (append_slice(&result, str, start, pos), result);
}
diff --git a/src/free_token.c b/src/free_token.c
index 9b035ac..512ba23 100644
--- a/src/free_token.c
+++ b/src/free_token.c
@@ -3,14 +3,15 @@
/* ::: :::::::: */
/* free_token.c :+: :+: :+: */
/* +:+ +:+ +:+ */
-/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */
+/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/27 14:38:57 by dkaiser #+# #+# */
-/* Updated: 2024/08/02 14:23:56 by dkaiser ### ########.fr */
+/* Updated: 2025/01/20 12:49:48 by chuhlig ### ########.fr */
/* */
/* ************************************************************************** */
#include "token.h"
+#include "debug_tools.h"
void free_token(t_token *token)
{
@@ -19,6 +20,7 @@ void free_token(t_token *token)
if (token->next != NULL)
token->next->previous = NULL;
free(token);
+ token = NULL;
}
void free_token_and_connect(t_token *token)
@@ -28,6 +30,7 @@ void free_token_and_connect(t_token *token)
if (token->next != NULL)
token->next->previous = token->previous;
free(token);
+ token = NULL;
}
void free_tokens(t_token *tokens)
diff --git a/src/handle_redir.c b/src/handle_redir.c
new file mode 100644
index 0000000..e8a1010
--- /dev/null
+++ b/src/handle_redir.c
@@ -0,0 +1,103 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* handle_redir.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2025/01/18 18:34:51 by chuhlig #+# #+# */
+/* Updated: 2025/01/20 14:59:38 by chuhlig ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "minishell.h"
+
+int handle_input_redirection(t_redirection *redir)
+{
+ int fd;
+
+ if (redir->type == INPUT_FILE)
+ {
+ fd = open_file(redir->specifier, O_RDONLY, 0);
+ if (fd < 0)
+ return (-1);
+ dup2(fd, STDIN_FILENO);
+ close(fd);
+ }
+ else if (redir->type == INPUT_LIMITER)
+ {
+ fd = open_file("/tmp/heredoc_tmp", O_WRONLY | O_TRUNC, 0644);
+ if (fd < 0)
+ return (-1);
+ write(fd, redir->specifier, ft_strlen(redir->specifier));
+ close(fd);
+ fd = open_file("/tmp/heredoc_tmp", O_RDONLY, 0);
+ if (fd < 0)
+ return (-1);
+ dup2(fd, STDIN_FILENO);
+ close(fd);
+ }
+ return (0);
+}
+
+int handle_output_redirection(t_redirection *redir)
+{
+ int fd;
+
+ if (redir->type == OUTPUT_OVERRIDE)
+ {
+ fd = open_file(redir->specifier, O_WRONLY | O_TRUNC, 0644);
+ if (fd < 0)
+ return (-1);
+ dup2(fd, STDOUT_FILENO);
+ close(fd);
+ }
+ else if (redir->type == OUTPUT_APPEND)
+ {
+ fd = open_file(redir->specifier, O_WRONLY | O_APPEND, 0644);
+ if (fd < 0)
+ return (-1);
+ dup2(fd, STDOUT_FILENO);
+ close(fd);
+ }
+ return (0);
+}
+
+int handle_redirections(t_redirection *redirs)
+{
+ if (redirs[0].type == INPUT_FILE || redirs[0].type == INPUT_LIMITER)
+ {
+ if (handle_input_redirection(&redirs[0]) < 0)
+ return (-1);
+ }
+ if (redirs[1].type == OUTPUT_OVERRIDE || redirs[1].type == OUTPUT_APPEND)
+ {
+ if (handle_output_redirection(&redirs[1]) < 0)
+ return (-1);
+ }
+ return (0);
+}
+
+int handle_pipe_parent(int p[2], t_node *node, t_env **env)
+{
+ int original_stdin;
+ int result;
+
+ close(p[1]);
+ original_stdin = dup(STDIN_FILENO);
+ dup2(p[0], STDIN_FILENO);
+ result = eval_rec(node->content.pipe.right, env, p[0]);
+ dup2(original_stdin, STDIN_FILENO);
+ close(original_stdin);
+ close(p[0]);
+ return (result);
+}
+
+int handle_pipe_child(int p[2], t_node *node, t_env **env, int in_fd)
+{
+ close(p[0]);
+ dup2(in_fd, STDIN_FILENO);
+ dup2(p[1], STDOUT_FILENO);
+ close(p[1]);
+ exit(eval_rec(node->content.pipe.left, env, in_fd));
+}
diff --git a/src/interpreter.c b/src/interpreter.c
index c7fe67c..f31e965 100644
--- a/src/interpreter.c
+++ b/src/interpreter.c
@@ -6,100 +6,41 @@
/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/17 19:15:49 by chuhlig #+# #+# */
-/* Updated: 2025/01/16 18:44:39 by dkaiser ### ########.fr */
+/* Updated: 2025/01/20 12:48:49 by chuhlig ### ########.fr */
/* */
/* ************************************************************************** */
#include "debug_tools.h"
#include "minishell.h"
-int handle_redirections(t_redirection *redirs)
+int eval_rec(t_node *node, t_env **env, int in_fd);
+
+int open_file(char *path, int flags, int mode)
{
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)
- {
- fd = open("/tmp/heredoc_tmp", O_WRONLY | 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_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_APPEND, 0644);
- if (fd < 0)
- {
- perror("open");
- return (-1);
- }
- dup2(fd, STDOUT_FILENO);
- close(fd);
- }
- return (0);
+ fd = open(path, flags, mode);
+ if (fd < 0)
+ perror("open");
+ return (fd);
}
int eval_rec(t_node *node, t_env **env, int in_fd)
{
- pid_t pid;
int p[2];
+ pid_t pid;
int result;
- int original_stdin;
if (node->type == PIPE_NODE)
{
- pipe(p);
+ if (pipe(p) == -1)
+ return (perror("pipe"), EXIT_FAILURE);
pid = fork();
+ if (pid == -1)
+ return (perror("fork"), close(p[0]), close(p[1]), EXIT_FAILURE);
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]);
- original_stdin = dup(STDIN_FILENO);
- dup2(p[0], STDIN_FILENO);
- result = eval_rec(node->content.pipe.right, env, p[0]);
- dup2(original_stdin, STDIN_FILENO);
- close(original_stdin);
- }
+ handle_pipe_child(p, node, env, in_fd);
+ result = handle_pipe_parent(p, node, env);
}
else if (node->type == CMD_NODE)
result = execute_cmd(&node->content.cmd, env);
diff --git a/src/new_node.c b/src/new_node.c
index 83d9159..bbac154 100644
--- a/src/new_node.c
+++ b/src/new_node.c
@@ -3,10 +3,10 @@
/* ::: :::::::: */
/* new_node.c :+: :+: :+: */
/* +:+ +:+ +:+ */
-/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */
+/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/27 11:21:03 by dkaiser #+# #+# */
-/* Updated: 2025/01/16 18:25:54 by dkaiser ### ########.fr */
+/* Updated: 2025/01/19 19:01:01 by chuhlig ### ########.fr */
/* */
/* ************************************************************************** */
@@ -51,6 +51,7 @@ t_node *new_cmd_node(char **args, t_redirection redirs[2], t_list *create_files)
node->content.cmd.redirs[1] = redirs[1];
node->content.cmd.create_files = create_files;
free(redirs);
+ redirs = NULL;//1
return (node);
}
return (NULL);
diff --git a/src/parse_cmd.c b/src/parse_cmd.c
index 92dfd12..d13bf3f 100644
--- a/src/parse_cmd.c
+++ b/src/parse_cmd.c
@@ -6,7 +6,7 @@
/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/08 15:06:25 by dkaiser #+# #+# */
-/* Updated: 2025/01/16 19:06:03 by dkaiser ### ########.fr */
+/* Updated: 2025/01/20 12:44:44 by chuhlig ### ########.fr */
/* */
/* ************************************************************************** */
@@ -18,7 +18,7 @@ t_node *parse_cmd(t_token *tokens, t_env **env)
{
char **args;
t_redirection *redirs;
- t_list *create_files;
+ t_list *create_files;
create_files = NULL;
redirs = collect_redirs(&tokens, *env, &create_files);
@@ -36,6 +36,7 @@ t_node *parse_cmd(t_token *tokens, t_env **env)
static char **collect_args(t_token **tokens, t_env **env)
{
t_token *cur;
+ t_token *next;//2
char **result;
int i;
@@ -50,12 +51,14 @@ static char **collect_args(t_token **tokens, t_env **env)
i = 0;
while (cur != NULL && cur->type == STRING_TOKEN)
{
+ next = cur->next;//2
if (cur->previous)
free_token(cur->previous);
- result[i] = format_string(cur->content.string, *env);
+ result[i] = format_string(cur->content.string, *env, ft_atoi("0"));
i++;
- cur = cur->next;
+ // cur = cur->next;
+ cur = next;//2
}
result[i] = NULL;
return (result);
-}
+} \ No newline at end of file
diff --git a/src/parser.c b/src/parser.c
index 1375954..aef8d70 100644
--- a/src/parser.c
+++ b/src/parser.c
@@ -6,7 +6,7 @@
/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/29 15:53:29 by dkaiser #+# #+# */
-/* Updated: 2025/01/11 16:06:54 by chuhlig ### ########.fr */
+/* Updated: 2025/01/19 18:59:00 by chuhlig ### ########.fr */
/* */
/* ************************************************************************** */
@@ -40,6 +40,7 @@ static t_node *parse_statement(t_token *tokens, t_env **env)
if (left_side_tokens == NULL)
{
free_tokens(tokens);
+ tokens = NULL;//1
return (NULL);
}
else if (tokens != NULL)
@@ -70,6 +71,7 @@ t_token *split_at_first(t_token **tokens, int type)
if (result == split)
result = NULL;
free_token(split);
+ split = NULL;//1
return (result);
}
diff --git a/src/repl.c b/src/repl.c
index 5079ee8..15b0a80 100644
--- a/src/repl.c
+++ b/src/repl.c
@@ -6,7 +6,7 @@
/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/24 16:07:04 by dkaiser #+# #+# */
-/* Updated: 2025/01/14 15:39:58 by chuhlig ### ########.fr */
+/* Updated: 2025/01/20 12:45:00 by chuhlig ### ########.fr */
/* */
/* ************************************************************************** */
@@ -23,7 +23,7 @@ void repl(const char *prompt, t_env **env, int *promptflag)
while (1)
{
input = readline(prompt);
- if (input == NULL)
+ if (input == NULL)
{
if (*promptflag > 1)
(*promptflag)--;
@@ -41,3 +41,5 @@ void repl(const char *prompt, t_env **env, int *promptflag)
free(input);
}
}
+
+//echo hi | >./outfiles/outfile01 echo bye >./test_files/invalid_permission \ No newline at end of file
diff --git a/src/signal_handling.c b/src/signal_handling.c
index c6273d0..6c6ca1e 100644
--- a/src/signal_handling.c
+++ b/src/signal_handling.c
@@ -6,7 +6,7 @@
/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/24 15:08:43 by dkaiser #+# #+# */
-/* Updated: 2025/01/14 14:11:29 by chuhlig ### ########.fr */
+/* Updated: 2025/01/20 12:15:32 by chuhlig ### ########.fr */
/* */
/* ************************************************************************** */