aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/collect_redirs.c43
-rw-r--r--src/create_files.c58
-rw-r--r--src/error.c9
-rw-r--r--src/execute_cmd.c6
-rw-r--r--src/get_cmd_path.c6
-rw-r--r--src/new_node.c7
-rw-r--r--src/parse_cmd.c11
-rw-r--r--src/parser.c8
-rw-r--r--src/praise_the_norme.c30
-rw-r--r--src/repl.c6
10 files changed, 114 insertions, 70 deletions
diff --git a/src/collect_redirs.c b/src/collect_redirs.c
index 837a16a..f274053 100644
--- a/src/collect_redirs.c
+++ b/src/collect_redirs.c
@@ -6,7 +6,7 @@
/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/02 13:49:31 by dkaiser #+# #+# */
-/* Updated: 2025/01/20 13:03:47 by dkaiser ### ########.fr */
+/* Updated: 2025/01/20 18:39:24 by dkaiser ### ########.fr */
/* */
/* ************************************************************************** */
@@ -14,17 +14,18 @@
#include <stdlib.h>
static void collect_and_check_redir(t_redirection *result,
- t_token **cur, t_env *env, t_list **create_files);
+ t_token **cur, t_minidata *data, t_token **tokens);
static t_redirection *set_redir(t_redirection *redir, int type, char *spec,
t_env *env);
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_list **create_files)
{
t_redirection *result;
t_token *cur;
+ t_minidata data;
cur = *tokens;
result = malloc(sizeof(t_redirection) * 2);
@@ -32,10 +33,12 @@ t_redirection *collect_redirs(t_token **tokens, t_env *env,
return (free_tokens(*tokens), NULL);
set_redir(&result[0], 0, NULL, env);
set_redir(&result[1], 0, NULL, env);
- while (cur != NULL && cur->next != NULL)
+ data.create_files = create_files;
+ data.env = env;
+ while (cur != NULL)
{
if (cur->type == REDIR_TOKEN && cur->next->type == STRING_TOKEN)
- collect_and_check_redir(result, &cur, env, create_files);
+ collect_and_check_redir(result, &cur, &data, tokens);
else if (cur->type == REDIR_TOKEN)
return (free(result), NULL);
else
@@ -47,35 +50,27 @@ t_redirection *collect_redirs(t_token **tokens, t_env *env,
}
static void collect_and_check_redir(t_redirection *result, t_token **cur,
- t_env *env, t_list **create_files)
+ t_minidata *data, t_token **tokens)
{
- t_token *next_token;
char *str;
if ((*cur)->content.redir_type != INPUT_LIMITER)
str = ft_strdup((*cur)->next->content.string);
if ((*cur)->content.redir_type == INPUT_LIMITER)
{
- if (!set_heredoc_data(*cur, result, env))
+ if (!set_heredoc_data(*cur, result, data->env))
return ;
}
else if ((*cur)->content.redir_type == INPUT_FILE)
- set_redir(&result[0], INPUT_FILE, format_string(str, env), env);
+ q4fc(data->create_files, set_redir(&result[0], INPUT_FILE,
+ format_string(str, data->env, 0), data->env));
else if ((*cur)->content.redir_type == OUTPUT_OVERRIDE)
- ft_lstadd_back(create_files, ft_lstnew(set_redir(&result[1],
- OUTPUT_OVERRIDE, format_string(str, env), env)));
+ q4fc(data->create_files, set_redir(&result[1], OUTPUT_OVERRIDE,
+ format_string(str, data->env, 0), data->env));
else if ((*cur)->content.redir_type == OUTPUT_APPEND)
- 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);
- if (next_token)
- {
- *cur = next_token->next;
- free_token_and_connect(next_token);
- }
- else
- *cur = NULL;
+ q4fc(data->create_files, set_redir(&result[1], OUTPUT_APPEND,
+ format_string(str, data->env, 0), data->env));
+ i_love_the_norme(cur, tokens);
}
static t_redirection *set_redir(t_redirection *redir, int type, char *spec,
@@ -88,7 +83,7 @@ static t_redirection *set_redir(t_redirection *redir, int type, char *spec,
redir->specifier = format_string(spec, env, ft_atoi("0"));
else
redir->specifier = spec;
- if (redir->type == OUTPUT_APPEND || redir->type == OUTPUT_OVERRIDE)
+ if (redir->type != INPUT_LIMITER)
{
result = malloc(sizeof(t_redirection));
if (!result)
@@ -100,8 +95,6 @@ static t_redirection *set_redir(t_redirection *redir, int type, char *spec,
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 8689f88..eee2860 100644
--- a/src/create_files.c
+++ b/src/create_files.c
@@ -6,40 +6,56 @@
/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/16 16:23:51 by dkaiser #+# #+# */
-/* Updated: 2025/01/19 14:36:59 by chuhlig ### ########.fr */
+/* Updated: 2025/01/20 18:30:40 by dkaiser ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
+#include <sys/fcntl.h>
#include <unistd.h>
-void create_files(t_list *files)
+static int cant_write(char *filename);
+static void create_file(char *filename, int mode);
+
+int create_files(t_list *files)
{
t_redirection *file;
- int fd;
while (files)
{
- dbg("Test");
- if (files->content == NULL)
- continue ;
- file = (t_redirection *)files->content;
- if (access(file->specifier, F_OK) != -1 && access(file->specifier, W_OK) == -1)
- break ;
- if (file->type == OUTPUT_OVERRIDE)
- {
- fd = open(file->specifier, O_WRONLY | O_CREAT | O_TRUNC, 0644);
- close(fd);
- }
- else if (file->type == OUTPUT_APPEND)
+ if (files->content != NULL)
{
- fd = open(file->specifier, O_WRONLY | O_CREAT | O_APPEND, 0644);
- close(fd);
+ file = (t_redirection *)files->content;
+ if (file->type == INPUT_FILE && (access(file->specifier, F_OK) == -1
+ || access(file->specifier, R_OK) == -1))
+ return (EXIT_FAILURE);
+ if (cant_write(file->specifier))
+ break ;
+ if (file->type == OUTPUT_OVERRIDE)
+ create_file(file->specifier, O_TRUNC);
+ else if (file->type == OUTPUT_APPEND)
+ create_file(file->specifier, O_APPEND);
+ 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;
}
+ return (EXIT_SUCCESS);
+}
+
+static int cant_write(char *filename)
+{
+ return (access(filename, F_OK) != -1 && access(filename, W_OK) == -1);
+}
+
+static void create_file(char *filename, int mode)
+{
+ close(open(filename, O_WRONLY | O_CREAT | mode, 0644));
+}
+
+void q4fc(t_list **queue, t_redirection *redir)
+{
+ ft_lstadd_back(queue, ft_lstnew(redir));
}
diff --git a/src/error.c b/src/error.c
index 628200d..2ca60b2 100644
--- a/src/error.c
+++ b/src/error.c
@@ -6,7 +6,7 @@
/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/15 16:35:53 by dkaiser #+# #+# */
-/* Updated: 2025/01/15 16:36:18 by dkaiser ### ########.fr */
+/* Updated: 2025/01/20 18:12:40 by dkaiser ### ########.fr */
/* */
/* ************************************************************************** */
@@ -21,3 +21,10 @@ void *error(int err_code, char *err_text, int exit_code, int *ret_code)
*ret_code = exit_code;
return (NULL);
}
+
+void command_not_found_error(char *cmd)
+{
+ ft_printf("%s:", cmd);
+ ft_putstr_fd(" command not found", 2);
+ ft_printf("\n");
+}
diff --git a/src/execute_cmd.c b/src/execute_cmd.c
index 4b84e12..ab43032 100644
--- a/src/execute_cmd.c
+++ b/src/execute_cmd.c
@@ -51,8 +51,8 @@ int execute_cmd(t_cmd *cmd, t_env **env)
original_std[1] = dup(STDOUT_FILENO);
original_std[0] = dup(STDIN_FILENO);
- create_files(cmd->create_files);
- if (handle_redirections(cmd->redirs) == -1)
+ result = create_files(cmd->create_files);
+ if (result != EXIT_SUCCESS || handle_redirections(cmd->redirs) == -1)
{
establish_pipeline(original_std[0], original_std[1]);
return (EXIT_FAILURE);
@@ -63,6 +63,8 @@ int execute_cmd(t_cmd *cmd, t_env **env)
establish_pipeline(original_std[0], original_std[1]);
return (result);
}
+ if (result != EXIT_SUCCESS)
+ return (result);
return (exec_cmd(cmd, env, original_std, EXIT_SUCCESS));
}
diff --git a/src/get_cmd_path.c b/src/get_cmd_path.c
index e005af1..70043a7 100644
--- a/src/get_cmd_path.c
+++ b/src/get_cmd_path.c
@@ -6,7 +6,7 @@
/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/17 19:19:59 by chuhlig #+# #+# */
-/* Updated: 2025/01/20 12:52:00 by dkaiser ### ########.fr */
+/* Updated: 2025/01/20 18:12:33 by dkaiser ### ########.fr */
/* */
/* ************************************************************************** */
@@ -85,9 +85,7 @@ static char *find_in_path(char *cmd, t_env *env, int *return_code)
path++;
}
*return_code = 127;
- ft_printf("%s:", cmd);
- ft_putstr_fd(" command not found", 2);
- ft_printf("\n");
+ command_not_found_error(cmd);
return (NULL);
}
diff --git a/src/new_node.c b/src/new_node.c
index bbac154..b2ab7ea 100644
--- a/src/new_node.c
+++ b/src/new_node.c
@@ -6,7 +6,7 @@
/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/27 11:21:03 by dkaiser #+# #+# */
-/* Updated: 2025/01/19 19:01:01 by chuhlig ### ########.fr */
+/* Updated: 2025/01/20 17:59:01 by dkaiser ### ########.fr */
/* */
/* ************************************************************************** */
@@ -37,7 +37,8 @@ t_node *new_pipe_node(t_node *left, t_node *right)
return (node);
}
-t_node *new_cmd_node(char **args, t_redirection redirs[2], t_list *create_files)
+t_node *new_cmd_node(char **args, t_redirection redirs[2],
+ t_list *create_files)
{
t_node *node;
@@ -51,7 +52,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
+ redirs = NULL;
return (node);
}
return (NULL);
diff --git a/src/parse_cmd.c b/src/parse_cmd.c
index d13bf3f..b30d126 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/20 12:44:44 by chuhlig ### ########.fr */
+/* Updated: 2025/01/20 17:57:50 by dkaiser ### ########.fr */
/* */
/* ************************************************************************** */
@@ -36,9 +36,9 @@ 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;
+ t_token *next;
cur = *tokens;
i = 0;
@@ -51,14 +51,13 @@ static char **collect_args(t_token **tokens, t_env **env)
i = 0;
while (cur != NULL && cur->type == STRING_TOKEN)
{
- next = cur->next;//2
+ next = cur->next;
if (cur->previous)
free_token(cur->previous);
result[i] = format_string(cur->content.string, *env, ft_atoi("0"));
i++;
- // cur = cur->next;
- cur = next;//2
+ cur = next;
}
result[i] = NULL;
return (result);
-} \ No newline at end of file
+}
diff --git a/src/parser.c b/src/parser.c
index aef8d70..75f1c64 100644
--- a/src/parser.c
+++ b/src/parser.c
@@ -6,14 +6,14 @@
/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/29 15:53:29 by dkaiser #+# #+# */
-/* Updated: 2025/01/19 18:59:00 by chuhlig ### ########.fr */
+/* Updated: 2025/01/20 17:57:20 by dkaiser ### ########.fr */
/* */
/* ************************************************************************** */
+#include "env.h"
#include "libft.h"
#include "minishell.h"
#include "token.h"
-#include "env.h"
static t_token *find_token_by_type(t_token *tokens, int type);
t_token *split_at_first(t_token **tokens, int type);
@@ -40,7 +40,7 @@ static t_node *parse_statement(t_token *tokens, t_env **env)
if (left_side_tokens == NULL)
{
free_tokens(tokens);
- tokens = NULL;//1
+ tokens = NULL;
return (NULL);
}
else if (tokens != NULL)
@@ -71,7 +71,7 @@ t_token *split_at_first(t_token **tokens, int type)
if (result == split)
result = NULL;
free_token(split);
- split = NULL;//1
+ split = NULL;
return (result);
}
diff --git a/src/praise_the_norme.c b/src/praise_the_norme.c
new file mode 100644
index 0000000..a22843b
--- /dev/null
+++ b/src/praise_the_norme.c
@@ -0,0 +1,30 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* praise_the_norme.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2025/01/20 18:35:41 by dkaiser #+# #+# */
+/* Updated: 2025/01/20 18:39:31 by dkaiser ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "minishell.h"
+
+void i_love_the_norme(t_token **cur, t_token **tokens)
+{
+ t_token *next_token;
+
+ next_token = (*cur)->next;
+ free_token_and_connect(*cur);
+ if (next_token)
+ {
+ if (next_token->previous == NULL)
+ *tokens = next_token->next;
+ *cur = next_token->next;
+ free_token_and_connect(next_token);
+ }
+ else
+ *cur = NULL;
+}
diff --git a/src/repl.c b/src/repl.c
index 15b0a80..16c8e95 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/20 12:45:00 by chuhlig ### ########.fr */
+/* Updated: 2025/01/20 17:58:43 by dkaiser ### ########.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,5 +41,3 @@ 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