aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDominik Kaiser2025-01-20 18:41:55 +0100
committerDominik Kaiser2025-01-20 18:41:55 +0100
commit4e249399780b53dad6fa1bf6db8a7c6d32ee2d13 (patch)
tree21ec55386702455222d320cbf270f3c2065ccc3a
parenteceed405102e019b1f001751de60f5534be0a0ef (diff)
downloadminishell-4e249399780b53dad6fa1bf6db8a7c6d32ee2d13.tar.gz
minishell-4e249399780b53dad6fa1bf6db8a7c6d32ee2d13.zip
I love the norme sooooooooooo much!!!!
-rw-r--r--Makefile3
-rw-r--r--include/debug_tools.h8
-rw-r--r--include/minishell.h21
-rw-r--r--src/collect_redirs.c30
-rw-r--r--src/create_files.c61
-rw-r--r--src/error.c9
-rw-r--r--src/get_cmd_path.c6
-rw-r--r--src/new_node.c7
-rw-r--r--src/parse_cmd.c15
-rw-r--r--src/parser.c8
-rw-r--r--src/praise_the_norme.c30
-rw-r--r--src/repl.c6
12 files changed, 117 insertions, 87 deletions
diff --git a/Makefile b/Makefile
index 9cfa670..b3a18f3 100644
--- a/Makefile
+++ b/Makefile
@@ -20,7 +20,8 @@ SRC := main.c debug_tools.c init.c signal_handling.c repl.c new_token.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 format_string.c \
builtins_part_one.c builtins_part_two.c env_tools.c error.c \
- read_heredoc.c create_files.c builtins_part_three.c handle_redir.c
+ read_heredoc.c create_files.c builtins_part_three.c handle_redir.c \
+ praise_the_norme.c
OBJ_DIR := _obj
OBJ := $(addprefix $(OBJ_DIR)/, $(SRC:%.c=%.o))
diff --git a/include/debug_tools.h b/include/debug_tools.h
index 6c4bc29..a014ff1 100644
--- a/include/debug_tools.h
+++ b/include/debug_tools.h
@@ -6,16 +6,14 @@
/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/24 18:34:37 by dkaiser #+# #+# */
-/* Updated: 2025/01/19 21:08:15 by chuhlig ### ########.fr */
+/* Updated: 2025/01/20 17:56:01 by dkaiser ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef DEBUG_TOOLS_H
# define DEBUG_TOOLS_H
-# include <stdarg.h>
-# include "debug_tools.h"
-
# include "libft.h"
+# include <stdarg.h>
# ifndef DEBUG
# define DEBUG 0
@@ -25,8 +23,6 @@
void dbg(char *str);
void panic(char *msg);
-
void dbg2(const char *format, ...);
-
#endif
diff --git a/include/minishell.h b/include/minishell.h
index ea9f687..ba2a6ad 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: 2025/01/20 17:18:48 by dkaiser ### ########.fr */
+/* Updated: 2025/01/20 18:41:11 by dkaiser ### ########.fr */
/* */
/* ************************************************************************** */
@@ -26,7 +26,6 @@
# include <termios.h>
# include <unistd.h>
# include <fcntl.h>
-# include <sys/wait.h>
int init(void);
int init_signal_handling(void);
@@ -46,20 +45,24 @@ int set_return_code(int return_code, t_env **env);
int handle_redirections(t_redirection *redirs);
void *error(int err_code, char *err_text, int exit_code,
int *ret_code);
+void command_not_found_error(char *cmd);
char *read_heredoc(char *delimiter);
int handle_input_redirection(t_redirection *redir);
int handle_output_redirection(t_redirection *redir);
int handle_redirections(t_redirection *redirs);
int handle_pipe_parent(int p[2], t_node *node, t_env **env);
-int handle_pipe_child(int p[2], t_node *node,
- t_env **env, int in_fd);
+int handle_pipe_child(int p[2], t_node *node, t_env **env,
+ int in_fd);
int open_file(char *path, int flags, int mode);
int eval_rec(t_node *node, t_env **env, int in_fd);
-int create_files(t_list *files);
+int create_files(t_list *files);
+void q4fc(t_list **queue, t_redirection *redir);
+void i_love_the_norme(t_token **cur, t_token **tokens);
-typedef struct s_minidata {
- t_env *env;
- t_list **create_files;
-} t_minidata;
+typedef struct s_minidata
+{
+ t_env *env;
+ t_list **create_files;
+} t_minidata;
#endif
diff --git a/src/collect_redirs.c b/src/collect_redirs.c
index e5bc8f6..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 17:30:00 by dkaiser ### ########.fr */
+/* Updated: 2025/01/20 18:39:24 by dkaiser ### ########.fr */
/* */
/* ************************************************************************** */
@@ -52,7 +52,6 @@ t_redirection *collect_redirs(t_token **tokens, t_env *env,
static void collect_and_check_redir(t_redirection *result, t_token **cur,
t_minidata *data, t_token **tokens)
{
- t_token *next_token;
char *str;
if ((*cur)->content.redir_type != INPUT_LIMITER)
@@ -63,28 +62,15 @@ static void collect_and_check_redir(t_redirection *result, t_token **cur,
return ;
}
else if ((*cur)->content.redir_type == INPUT_FILE)
- ft_lstadd_back(data->create_files, ft_lstnew(set_redir(&result[0],
- INPUT_FILE, format_string(str, data->env, 0), data->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(data->create_files, ft_lstnew(set_redir(&result[1],
- OUTPUT_OVERRIDE, format_string(str, data->env, 0),
- data->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(data->create_files, ft_lstnew(set_redir(&result[1],
- OUTPUT_APPEND, format_string(str, data->env, 0),
- data->env)));
- next_token = (*cur)->next;
- free_token_and_connect(*cur);
- if (next_token)
- {
- if (next_token->previous == NULL)
- *tokens = next_token->next;
- // free_token_and_connect(*cur);
- *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,
diff --git a/src/create_files.c b/src/create_files.c
index 0550e57..eee2860 100644
--- a/src/create_files.c
+++ b/src/create_files.c
@@ -6,45 +6,56 @@
/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/16 16:23:51 by dkaiser #+# #+# */
-/* Updated: 2025/01/20 15:54:00 by dkaiser ### ########.fr */
+/* Updated: 2025/01/20 18:30:40 by dkaiser ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
+#include <sys/fcntl.h>
#include <unistd.h>
+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)
{
- if (files->content == NULL)
- {
- files = files->next;
- continue ;
- }
- 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 (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);
+ 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/get_cmd_path.c b/src/get_cmd_path.c
index 2980b0f..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 13:18:45 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 5502f74..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 17:30:06 by dkaiser ### ########.fr */
+/* Updated: 2025/01/20 17:57:50 by dkaiser ### ########.fr */
/* */
/* ************************************************************************** */
@@ -35,10 +35,10 @@ 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 *cur;
+ char **result;
+ int i;
+ t_token *next;
cur = *tokens;
i = 0;
@@ -51,13 +51,12 @@ 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);
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