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))
/* 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
void dbg(char *str);
void panic(char *msg);
-
void dbg2(const char *format, ...);
-
#endif
/* 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 */
/* */
/* ************************************************************************** */
# include <termios.h>
# include <unistd.h>
# include <fcntl.h>
-# include <sys/wait.h>
int init(void);
int init_signal_handling(void);
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
/* 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 */
/* */
/* ************************************************************************** */
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)
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,
/* 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));
}
/* 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 */
/* */
/* ************************************************************************** */
*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");
+}
/* 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 */
/* */
/* ************************************************************************** */
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);
}
/* 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 */
/* */
/* ************************************************************************** */
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;
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);
/* 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 */
/* */
/* ************************************************************************** */
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;
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);
/* 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);
if (left_side_tokens == NULL)
{
free_tokens(tokens);
- tokens = NULL;//1
+ tokens = NULL;
return (NULL);
}
else if (tokens != NULL)
if (result == split)
result = NULL;
free_token(split);
- split = NULL;//1
+ split = NULL;
return (result);
}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* 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;
+}
/* 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 */
/* */
/* ************************************************************************** */
while (1)
{
input = readline(prompt);
- if (input == NULL)
+ if (input == NULL)
{
if (*promptflag > 1)
(*promptflag)--;
free(input);
}
}
-
-//echo hi | >./outfiles/outfile01 echo bye >./test_files/invalid_permission
\ No newline at end of file