]> git.dkaiser.de - 42/minishell.git/commitdiff
I love the norme sooooooooooo much!!!!
authorDominik Kaiser <dkaiser@3-H-1.42heilbronn.de>
Mon, 20 Jan 2025 17:41:55 +0000 (18:41 +0100)
committerDominik Kaiser <dkaiser@3-H-1.42heilbronn.de>
Mon, 20 Jan 2025 17:41:55 +0000 (18:41 +0100)
12 files changed:
Makefile
include/debug_tools.h
include/minishell.h
src/collect_redirs.c
src/create_files.c
src/error.c
src/get_cmd_path.c
src/new_node.c
src/parse_cmd.c
src/parser.c
src/praise_the_norme.c [new file with mode: 0644]
src/repl.c

index 9cfa67099f1bc0000ce88a87423b72800c4a2e40..b3a18f384e02e9cb8a8ad19fa0a42c2a38e088db 100644 (file)
--- 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))
index 6c4bc294c0805a26e5a84dfc2c51cabf98edd063..a014ff163f9b063727b913b5290c3b31c869a9d1 100644 (file)
@@ -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
index ea9f68764d5a3635b031ce511655328ee736b782..ba2a6ad6a56f8eed201d625079221110e5a081e9 100644 (file)
@@ -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
index e5bc8f65a65c42d803e036c83ad2d9188f6143b4..f274053f0c2bff14e54ba9e984d868474ea76851 100644 (file)
@@ -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,
index 0550e572f4d6bcce69eab49cf2a35c49c4d9b53b..eee2860a335b2e2f3266f8e3dee9e91ab84b6213 100644 (file)
@@ -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));
 }
index 628200d5eed125e98b36df2be33dc2837a4306f9..2ca60b24b1aac61e6c407879bfec6028c6ff7277 100644 (file)
@@ -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");
+}
index 2980b0fb755cb8e8c18828764cbd4433e032acfd..70043a73172e5d7b5506318896c30294b08bd18f 100644 (file)
@@ -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);
 }
 
index bbac154b9d70e58dce611e17c4030a51e50b8163..b2ab7ea7965732a8550f1192b6958fba05c9364c 100644 (file)
@@ -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);
index 5502f74852442e992254126418f6c628aa8ae4cb..b30d1267883fb1fd1789625f0894e7a1e5eabd86 100644 (file)
@@ -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);
index aef8d70c19910307b70ef1fb92907e1666587de5..75f1c64d27b4c0a1d25646f0ea34a430f1434fb8 100644 (file)
@@ -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 (file)
index 0000000..a22843b
--- /dev/null
@@ -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;
+}
index 15b0a8008d6dd1e816d7330c9a9f7dd4ac95593a..16c8e95c583f2e171746bcb3b1817d045ac61438 100644 (file)
@@ -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