Compare commits
7 Commits
882a39fc94
...
master
Author | SHA1 | Date | |
---|---|---|---|
bd5e1671fc | |||
62e887cfbd | |||
3e7dcd1ba9 | |||
9ab9c06593 | |||
2f6daa4ce5 | |||
b502364a67 | |||
0b56e95868 |
@ -1,17 +1,17 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* minishell.h :+: :+: :+: */
|
||||
/* bozoshell.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/02/14 13:45:30 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/04/05 14:46:56 by alouis-j ### ########.fr */
|
||||
/* Updated: 2023/04/14 16:16:31 by erey-bet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef MINISHELL_H
|
||||
# define MINISHELL_H
|
||||
#ifndef BOZOSHELL_H
|
||||
# define BOZOSHELL_H
|
||||
# include "./env/env.h"
|
||||
# include "./cmd/cmd.h"
|
||||
# include "./parse/parse.h"
|
BIN
builtins/.nfs000000000ab804b60000004a
Normal file
BIN
builtins/.nfs000000000ab804b60000004a
Normal file
Binary file not shown.
@ -6,7 +6,7 @@
|
||||
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/03/27 13:41:30 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/04/05 12:16:50 by erey-bet ### ########.fr */
|
||||
/* Updated: 2023/04/18 12:57:36 by erey-bet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -16,10 +16,10 @@
|
||||
|
||||
int echo(int fd, char **strs);
|
||||
int pwd(int fd);
|
||||
char *get_pwd(int fd);
|
||||
char *get_pwd(void);
|
||||
int print_env(t_list **env, int fd);
|
||||
int ft_export(t_list **env, char **args, int fd);
|
||||
int move_folder(char **args, int fd);
|
||||
int move_folder(char **args, t_list **env, int fd);
|
||||
int unset(t_list **env, char **args, int fd);
|
||||
int ft_exit(char **args, int err);
|
||||
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/03/27 13:41:42 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/03/31 20:07:36 by erey-bet ### ########.fr */
|
||||
/* Updated: 2023/04/18 12:57:25 by erey-bet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -17,6 +17,6 @@
|
||||
# include "../env/env.h"
|
||||
# include "../utils/utils.h"
|
||||
|
||||
char *get_pwd(int fd);
|
||||
char *get_pwd(void);
|
||||
|
||||
#endif
|
||||
|
@ -6,31 +6,37 @@
|
||||
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/02/20 14:27:36 by erey-bet #+# #+# */
|
||||
/* Updated: 2023/03/31 15:02:43 by erey-bet ### ########.fr */
|
||||
/* Updated: 2023/04/18 13:22:22 by erey-bet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "./builtins_private.h"
|
||||
|
||||
int make_move(char *path, int fd)
|
||||
int make_move(char *path, t_list **env)
|
||||
{
|
||||
char *join;
|
||||
char *old;
|
||||
|
||||
join = ft_strjoin("/", path);
|
||||
join = ft_strfjoin(get_pwd(fd), join);
|
||||
join = ft_strfjoin(get_pwd(), join);
|
||||
old = get_pwd();
|
||||
if (chdir(join) == 0)
|
||||
{
|
||||
set_value_by_key("OLDPWD", old, env);
|
||||
set_value_by_key("PWD", get_pwd(), env);
|
||||
free(join);
|
||||
return (0);
|
||||
}
|
||||
free(old);
|
||||
free(join);
|
||||
write(2, "cd: No such file or directory\n", 30);
|
||||
return (1);
|
||||
}
|
||||
|
||||
int move_folder(char **args, int fd)
|
||||
int move_folder(char **args, t_list **env)
|
||||
{
|
||||
char *path;
|
||||
char *old;
|
||||
|
||||
if (args[0] == NULL || args[1] != NULL)
|
||||
{
|
||||
@ -40,11 +46,17 @@ int move_folder(char **args, int fd)
|
||||
path = args[0];
|
||||
if (path[0] == '/' || ft_strncmp(path, "..", ft_strlen(path)) == 0)
|
||||
{
|
||||
old = get_pwd();
|
||||
if (chdir(path) == 0)
|
||||
{
|
||||
set_value_by_key("OLDPWD", old, env);
|
||||
set_value_by_key("PWD", get_pwd(), env);
|
||||
return (0);
|
||||
}
|
||||
free(old);
|
||||
write(2, "cd: No such file or directory\n", 30);
|
||||
return (1);
|
||||
}
|
||||
else
|
||||
return (make_move(path, fd));
|
||||
return (make_move(path, env));
|
||||
}
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/02/14 14:27:08 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/04/04 13:38:27 by erey-bet ### ########.fr */
|
||||
/* Updated: 2023/04/14 19:12:10 by erey-bet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -30,6 +30,8 @@ void print_export(t_list **env, int fd)
|
||||
|
||||
current = *env;
|
||||
while (current->next != NULL)
|
||||
{
|
||||
if (ft_strcmp(((t_env *)(current->content))->key, "_"))
|
||||
{
|
||||
write(fd, "declare -x ", 11);
|
||||
ft_putstr_fd(((t_env *)(current->content))->key, fd);
|
||||
@ -42,6 +44,7 @@ void print_export(t_list **env, int fd)
|
||||
}
|
||||
else
|
||||
write(fd, "\n", 2);
|
||||
}
|
||||
current = current->next;
|
||||
}
|
||||
}
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/02/17 16:09:11 by erey-bet #+# #+# */
|
||||
/* Updated: 2023/03/09 20:00:19 by erey-bet ### ########.fr */
|
||||
/* Updated: 2023/04/18 12:59:25 by erey-bet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -20,13 +20,13 @@ int pwd(int fd)
|
||||
ft_putendl_fd(path, fd);
|
||||
else
|
||||
{
|
||||
ft_putendl_fd("Error getcwd", fd);
|
||||
ft_putendl_fd("Error getcwd", 2);
|
||||
return (1);
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
char *get_pwd(int fd)
|
||||
char *get_pwd(void)
|
||||
{
|
||||
char *str;
|
||||
|
||||
@ -35,7 +35,7 @@ char *get_pwd(int fd)
|
||||
return (str);
|
||||
else
|
||||
{
|
||||
ft_putendl_fd("Error getcwd", fd);
|
||||
ft_putendl_fd("Error getcwd", 2);
|
||||
return (NULL);
|
||||
}
|
||||
}
|
||||
|
@ -6,12 +6,14 @@
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/02/15 14:18:21 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/04/07 15:17:21 by alouis-j ### ########.fr */
|
||||
/* Updated: 2023/04/17 11:57:07 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "cmd.h"
|
||||
#include "cmd_private.h"
|
||||
#include "../signal/signal.h"
|
||||
#include <signal.h>
|
||||
|
||||
void ft_cmddel(void *ptr)
|
||||
{
|
||||
@ -56,13 +58,16 @@ void ft_cmdwaiter(void *ptr)
|
||||
{
|
||||
if (exit_status == 131)
|
||||
{
|
||||
ft_printf("Quit (core dumped)\n");
|
||||
ft_printf("Quit (core dumped)");
|
||||
*ft_get_exit_code() = 131;
|
||||
}
|
||||
else
|
||||
*ft_get_exit_code() = 130;
|
||||
ft_printf("\n");
|
||||
}
|
||||
else
|
||||
*ft_get_exit_code() = WEXITSTATUS(exit_status);
|
||||
}
|
||||
signal(SIGINT, ft_ctrlc);
|
||||
signal(SIGQUIT, SIG_IGN);
|
||||
}
|
||||
|
14
env/env1.c
vendored
14
env/env1.c
vendored
@ -6,7 +6,7 @@
|
||||
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/02/02 14:39:56 by erey-bet #+# #+# */
|
||||
/* Updated: 2023/03/30 13:07:15 by erey-bet ### ########.fr */
|
||||
/* Updated: 2023/04/18 13:57:38 by erey-bet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -34,7 +34,11 @@ void add_sort(t_list **head, t_env *var)
|
||||
}
|
||||
}
|
||||
if (current->next == NULL)
|
||||
{
|
||||
current->next = ft_calloc(1, sizeof(t_list));
|
||||
if (!current->next)
|
||||
return ;
|
||||
}
|
||||
}
|
||||
|
||||
char *get_value_by_key(char *key, t_list **head)
|
||||
@ -61,9 +65,7 @@ int set_value_by_key(char *key, char *value, t_list **head)
|
||||
if (ft_strcmp(((t_env *)current->content)->key, key) == 0)
|
||||
{
|
||||
free(((t_env *)current->content)->value);
|
||||
free(((t_env *)current->content)->key);
|
||||
((t_env *)current->content)->value = value;
|
||||
((t_env *)current->content)->key = key;
|
||||
return (0);
|
||||
}
|
||||
current = current->next;
|
||||
@ -76,7 +78,10 @@ int create_value_by_key(char *key, char *value, t_list **head)
|
||||
t_env *content;
|
||||
|
||||
if (set_value_by_key(key, value, head) == 0)
|
||||
{
|
||||
free(key);
|
||||
return (0);
|
||||
}
|
||||
content = ft_calloc(1, sizeof(t_env));
|
||||
if (content == NULL)
|
||||
return (1);
|
||||
@ -97,7 +102,10 @@ t_list **init_env(char **env)
|
||||
return (NULL);
|
||||
*head = ft_calloc(1, sizeof(t_list));
|
||||
if (*head == NULL)
|
||||
{
|
||||
free(head);
|
||||
return (NULL);
|
||||
}
|
||||
i = -1;
|
||||
while (env[++i])
|
||||
{
|
||||
|
2
env/env2.c
vendored
2
env/env2.c
vendored
@ -6,7 +6,7 @@
|
||||
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/03/09 19:59:03 by erey-bet #+# #+# */
|
||||
/* Updated: 2023/03/09 19:59:10 by erey-bet ### ########.fr */
|
||||
/* Updated: 2023/04/18 13:26:31 by erey-bet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
4
env/env_fill.c
vendored
4
env/env_fill.c
vendored
@ -31,7 +31,7 @@ static char *ft_getkey(const char *str)
|
||||
key = ft_strndup(str + 1, i - 1);
|
||||
}
|
||||
if (key == NULL)
|
||||
ft_eprintf("minishell: malloc failed\n");
|
||||
ft_eprintf("bozoshell: malloc failed\n");
|
||||
return (key);
|
||||
}
|
||||
|
||||
@ -54,7 +54,7 @@ static char *ft_getvalue(t_data *data, char *key)
|
||||
value = ft_strdup(value);
|
||||
}
|
||||
if (value == NULL)
|
||||
ft_eprintf("minishell: malloc failed\n");
|
||||
ft_eprintf("bozoshell: malloc failed\n");
|
||||
return (value);
|
||||
}
|
||||
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/02/21 12:45:16 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/04/14 13:04:01 by cchauvet ### ########.fr */
|
||||
/* Updated: 2023/04/18 13:00:08 by erey-bet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -23,7 +23,7 @@ static int ft_execute_own_cmd(t_data *data, t_cmd *cmd)
|
||||
else if (ft_strcmp(cmd->executable, "export") == 0)
|
||||
return_code = ft_export(data->env, cmd->args + 1, cmd->fd_out[0]);
|
||||
else if (ft_strcmp(cmd->executable, "cd") == 0)
|
||||
return_code = (move_folder(cmd->args + 1, cmd->fd_out[0]));
|
||||
return_code = (move_folder(cmd->args + 1, data->env, cmd->fd_out[0]));
|
||||
else if (ft_strcmp(cmd->executable, "unset") == 0)
|
||||
return_code = (unset(data->env, cmd->args, cmd->fd_out[0]));
|
||||
else if (ft_strcmp(cmd->executable, "echo") == 0)
|
||||
@ -58,6 +58,8 @@ static bool ft_executor(t_data *data, t_cmd *cmd, char **env)
|
||||
execve(cmd->executable, cmd->args, env);
|
||||
return (1);
|
||||
}
|
||||
signal(SIGQUIT, SIG_IGN);
|
||||
signal(SIGINT, SIG_IGN);
|
||||
return (0);
|
||||
}
|
||||
|
||||
|
@ -20,7 +20,7 @@ static int ft_replace(char **str, size_t i)
|
||||
free(*str);
|
||||
if (temp == NULL)
|
||||
{
|
||||
ft_eprintf("minishell: malloc failed\n");
|
||||
ft_eprintf("bozoshell: malloc failed\n");
|
||||
return (1);
|
||||
}
|
||||
*str = temp;
|
||||
|
12
main.c
12
main.c
@ -6,7 +6,7 @@
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/02/16 15:16:14 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/04/07 15:04:12 by alouis-j ### ########.fr */
|
||||
/* Updated: 2023/04/18 12:59:43 by erey-bet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -15,7 +15,7 @@
|
||||
#include "libftx/libft/libft.h"
|
||||
#include "libftx/libft/list.h"
|
||||
#include "libftx/libftx.h"
|
||||
#include "minishell.h"
|
||||
#include "bozoshell.h"
|
||||
#include "signal/signal.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
@ -25,14 +25,14 @@ static char *ft_get_user_input(void)
|
||||
char *prompt;
|
||||
char *pwd;
|
||||
|
||||
pwd = get_pwd(2);
|
||||
pwd = get_pwd();
|
||||
if (pwd == NULL)
|
||||
return (NULL);
|
||||
prompt = ft_strmerger(2, pwd, "$ ");
|
||||
free(pwd);
|
||||
if (prompt == NULL)
|
||||
{
|
||||
ft_eprintf("minishell: malloc failed\n");
|
||||
ft_eprintf("bozoshell: malloc failed\n");
|
||||
return (NULL);
|
||||
}
|
||||
line = readline(prompt);
|
||||
@ -72,14 +72,14 @@ int ft_init_data(t_data *data, char **env)
|
||||
data->cmds = malloc(sizeof(t_cmd *));
|
||||
if (data->cmds == NULL)
|
||||
{
|
||||
ft_eprintf("minishell: malloc failed\n");
|
||||
ft_eprintf("bozoshell: malloc failed\n");
|
||||
return (1);
|
||||
}
|
||||
*data->cmds = NULL;
|
||||
data->env = init_env(env);
|
||||
if (data->env == NULL)
|
||||
{
|
||||
ft_eprintf("minishell: malloc failed\n");
|
||||
ft_eprintf("bozoshell: malloc failed\n");
|
||||
free(data->cmds);
|
||||
return (1);
|
||||
}
|
||||
|
@ -80,7 +80,7 @@ int ft_cmd_parser(t_data *data, char *cmd_str)
|
||||
cmd = ft_calloc(sizeof(t_cmd), 1);
|
||||
if (cmd == NULL)
|
||||
{
|
||||
ft_eprintf("minishell: malloc failed\n");
|
||||
ft_eprintf("bozoshell: malloc failed\n");
|
||||
return (1);
|
||||
}
|
||||
if (ft_redirection(data, cmd, cmd_str))
|
||||
@ -105,7 +105,7 @@ int ft_cmds_parser(t_data *data, const char *line)
|
||||
tab = ft_split_charset_quoted(line, "|");
|
||||
if (tab == NULL)
|
||||
{
|
||||
ft_eprintf("minishell: malloc failed\n");
|
||||
ft_eprintf("bozoshell: malloc failed\n");
|
||||
return (1);
|
||||
}
|
||||
i = -1;
|
||||
|
@ -6,12 +6,14 @@
|
||||
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/03/29 17:32:06 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/04/07 15:06:39 by alouis-j ### ########.fr */
|
||||
/* Updated: 2023/04/17 11:15:38 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "./redirection_private.h"
|
||||
#include <stdbool.h>
|
||||
#include "../signal/signal.h"
|
||||
#include <signal.h>
|
||||
|
||||
static bool ft_check_heredoc(t_data *data, t_cmd *cmd,
|
||||
char *redirection_identifier, char *redirection)
|
||||
@ -22,7 +24,9 @@ static bool ft_check_heredoc(t_data *data, t_cmd *cmd,
|
||||
{
|
||||
if (cmd->fd_in[0] == -2)
|
||||
return (0);
|
||||
signal(SIGINT, ft_ctrlc_heredoc);
|
||||
fd = ft_heredoc(data, redirection);
|
||||
signal(SIGINT, ft_ctrlc);
|
||||
if (fd == -2)
|
||||
return (1);
|
||||
else
|
||||
@ -126,13 +130,13 @@ bool ft_check_redirection(t_data *data, t_cmd *cmd,
|
||||
if (ft_is_in("<>", redirection_identifier[0])
|
||||
&& ft_is_in("<>", redirection[0]))
|
||||
{
|
||||
ft_eprintf("minishell: %s: invalid redirection file\n", redirection);
|
||||
ft_eprintf("bozoshell: %s: invalid redirection file\n", redirection);
|
||||
return (1);
|
||||
}
|
||||
str = ft_strdup(redirection);
|
||||
if (str == NULL)
|
||||
{
|
||||
ft_eprintf("minishell: malloc failed\n");
|
||||
ft_eprintf("bozoshell: malloc failed\n");
|
||||
return (1);
|
||||
}
|
||||
ft_quote_remover(str);
|
||||
|
@ -21,14 +21,14 @@ int ft_file_is_readable(t_data *data, const char *path)
|
||||
if (fd == -1)
|
||||
{
|
||||
*data->exit_code = 1;
|
||||
ft_eprintf("minishell: %s: No such file or directory\n", path);
|
||||
ft_eprintf("bozoshell: %s: No such file or directory\n", path);
|
||||
return (0);
|
||||
}
|
||||
readable = read(fd, "", 0);
|
||||
if (readable == -1)
|
||||
{
|
||||
*data->exit_code = 1;
|
||||
ft_eprintf("minishell: %s: Permission denied\n", path);
|
||||
ft_eprintf("bozoshell: %s: Permission denied\n", path);
|
||||
return (0);
|
||||
}
|
||||
close(fd);
|
||||
@ -44,14 +44,14 @@ int ft_file_is_writable(t_data *data, const char *path)
|
||||
if (fd == -1)
|
||||
{
|
||||
*data->exit_code = 1;
|
||||
ft_eprintf("minishell: %s: Permission denied\n", path);
|
||||
ft_eprintf("bozoshell: %s: Permission denied\n", path);
|
||||
return (0);
|
||||
}
|
||||
writeable = write(fd, "", 0);
|
||||
if (writeable == -1)
|
||||
{
|
||||
*data->exit_code = 1;
|
||||
ft_eprintf("minishell: %s: Permission denied\n", path);
|
||||
ft_eprintf("bozoshell: %s: Permission denied\n", path);
|
||||
return (0);
|
||||
}
|
||||
close(fd);
|
||||
@ -67,14 +67,14 @@ int ft_file_is_appendable(t_data *data, const char *path)
|
||||
if (fd == -1)
|
||||
{
|
||||
*data->exit_code = 1;
|
||||
ft_eprintf("minishell: %s: Permission denied\n", path);
|
||||
ft_eprintf("bozoshell: %s: Permission denied\n", path);
|
||||
return (0);
|
||||
}
|
||||
writeable = write(fd, "", 0);
|
||||
if (writeable == -1)
|
||||
{
|
||||
*data->exit_code = 1;
|
||||
ft_eprintf("minishell: %s: Permission denied\n", path);
|
||||
ft_eprintf("bozoshell: %s: Permission denied\n", path);
|
||||
return (0);
|
||||
}
|
||||
close(fd);
|
||||
|
@ -6,17 +6,23 @@
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/02/17 15:36:26 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/04/07 15:11:17 by alouis-j ### ########.fr */
|
||||
/* Updated: 2023/04/17 12:14:11 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "./redirection_private.h"
|
||||
#include <readline/readline.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int *ft_get_heredoc(void)
|
||||
static bool ft_fd_is_closed(int fd)
|
||||
{
|
||||
static int heredoc = -1;
|
||||
int fd2;
|
||||
|
||||
return (&heredoc);
|
||||
fd2 = dup(fd);
|
||||
if (fd2 == -1)
|
||||
return (1);
|
||||
close(fd2);
|
||||
return (0);
|
||||
}
|
||||
|
||||
static int ft_format_and_write(t_data *data, const char *str, int fd)
|
||||
@ -31,75 +37,57 @@ static int ft_format_and_write(t_data *data, const char *str, int fd)
|
||||
return (0);
|
||||
}
|
||||
|
||||
static int ft_heredoc3(char *stop, int fds[2])
|
||||
int ft_heredoc2(t_data *data, char *line, char *stop, int fds[2])
|
||||
{
|
||||
if (*ft_get_heredoc() == -1)
|
||||
if (line == NULL)
|
||||
{
|
||||
ft_putchar_fd('\n', 1);
|
||||
ft_closer(fds);
|
||||
if (ft_fd_is_closed(0))
|
||||
{
|
||||
close(fds[0]);
|
||||
fds[0] = -2;
|
||||
return (1);
|
||||
}
|
||||
else
|
||||
{
|
||||
ft_eprintf("minishell: warning: here-document at line");
|
||||
ft_eprintf("1 delimited by end-of-file (wanted `%s')\n", stop);
|
||||
return (2);
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
static int ft_heredoc2(t_data *data, char *stop, int fds[2])
|
||||
{
|
||||
char *line;
|
||||
int out;
|
||||
|
||||
ft_printf("> ");
|
||||
line = get_next_line(*ft_get_heredoc());
|
||||
if (line == NULL)
|
||||
{
|
||||
out = ft_heredoc3(stop, fds);
|
||||
if (out)
|
||||
return (out);
|
||||
}
|
||||
line[ft_strlen(line) - 1] = '\0';
|
||||
if (ft_strcmp(line, stop) == 0)
|
||||
{
|
||||
free(line);
|
||||
return (2);
|
||||
}
|
||||
if (ft_format_and_write(data, line, fds[1]))
|
||||
{
|
||||
ft_closer(fds);
|
||||
free(line);
|
||||
ft_eprintf("\nbozoshell: warning: here-document at line 1%s",
|
||||
"delimited by end-of-file (wanted `adfsd')\n");
|
||||
return (1);
|
||||
}
|
||||
}
|
||||
if (ft_strcmp(line, stop) == 0)
|
||||
return (1);
|
||||
if (ft_format_and_write(data, line, fds[1]))
|
||||
{
|
||||
close(fds[0]);
|
||||
fds[0] = -2;
|
||||
return (1);
|
||||
}
|
||||
free(line);
|
||||
return (0);
|
||||
}
|
||||
|
||||
int ft_heredoc(t_data *data, char *stop)
|
||||
{
|
||||
int fds[2];
|
||||
int return_code;
|
||||
int stdin_bak;
|
||||
char *line;
|
||||
|
||||
if (pipe(fds) == -1)
|
||||
return (-1);
|
||||
*ft_get_heredoc() = dup(0);
|
||||
while (true)
|
||||
stdin_bak = dup(0);
|
||||
if (stdin_bak == -1)
|
||||
return (1);
|
||||
if (pipe(fds))
|
||||
{
|
||||
return_code = ft_heredoc2(data, stop, fds);
|
||||
if (return_code == 2)
|
||||
break ;
|
||||
else if (return_code == 1)
|
||||
close(stdin_bak);
|
||||
return (1);
|
||||
}
|
||||
line = readline("> ");
|
||||
while (ft_heredoc2(data, line, stop, fds) == 0)
|
||||
{
|
||||
if (*ft_get_heredoc() > 2)
|
||||
close(*ft_get_heredoc());
|
||||
*ft_get_heredoc() = -1;
|
||||
return (-2);
|
||||
free(line);
|
||||
line = readline("> ");
|
||||
}
|
||||
}
|
||||
close(*ft_get_heredoc());
|
||||
*ft_get_heredoc() = -1;
|
||||
free(line);
|
||||
close(fds[1]);
|
||||
dup2(stdin_bak, 0);
|
||||
close(stdin_bak);
|
||||
return (fds[0]);
|
||||
}
|
||||
|
@ -84,7 +84,7 @@ int ft_set_redirection(t_data *data, t_cmd *cmd, char **tab)
|
||||
}
|
||||
if (ft_is_in("<>", tab[i][0]))
|
||||
{
|
||||
ft_eprintf("minishell: %s: must be followed by a file\n", tab[i]);
|
||||
ft_eprintf("bozoshell: %s: must be followed by a file\n", tab[i]);
|
||||
return (1);
|
||||
}
|
||||
return (0);
|
||||
@ -101,7 +101,7 @@ int ft_redirection(t_data *data, t_cmd *cmd, char *cmd_str)
|
||||
tab = ft_split_charset_quoted(cmd_str, " \t");
|
||||
if (tab == NULL)
|
||||
{
|
||||
ft_eprintf("minishell: malloc failed\n");
|
||||
ft_eprintf("bozoshell: malloc failed\n");
|
||||
return (1);
|
||||
}
|
||||
ft_remove_redirection(cmd_str);
|
||||
|
@ -6,26 +6,25 @@
|
||||
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/03/28 15:59:01 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/04/11 13:02:34 by cchauvet ### ########.fr */
|
||||
/* Updated: 2023/04/17 11:23:21 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "signal_private.h"
|
||||
|
||||
void ft_ctrlc_heredoc(int num)
|
||||
{
|
||||
close(0);
|
||||
ft_putchar_fd('\n', 1);
|
||||
(void) num;
|
||||
}
|
||||
|
||||
void ft_ctrlc(int num)
|
||||
{
|
||||
if (*ft_get_heredoc() != -1)
|
||||
{
|
||||
close(*ft_get_heredoc());
|
||||
*ft_get_heredoc() = -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
rl_replace_line("", 0);
|
||||
rl_on_new_line();
|
||||
ft_putchar_fd('\n', 1);
|
||||
rl_redisplay();
|
||||
}
|
||||
*ft_get_exit_code() = 130;
|
||||
(void) num;
|
||||
}
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/03/28 16:02:31 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/03/28 16:03:11 by cchauvet ### ########.fr */
|
||||
/* Updated: 2023/04/17 11:14:28 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -15,5 +15,6 @@
|
||||
|
||||
void ft_quit(int num);
|
||||
void ft_ctrlc(int num);
|
||||
void ft_ctrlc_heredoc(int num);
|
||||
|
||||
#endif
|
||||
|
@ -17,7 +17,7 @@ static int ft_quote_verif(const char *str)
|
||||
{
|
||||
if (ft_is_in_quote(str, ft_strlen(str)))
|
||||
{
|
||||
ft_eprintf("minishell: Quote is not closed\n");
|
||||
ft_eprintf("bozoshell: Quote is not closed\n");
|
||||
return (1);
|
||||
}
|
||||
else
|
||||
@ -47,7 +47,7 @@ static int ft_pipe_is_alone(const char *str)
|
||||
}
|
||||
}
|
||||
if (check == 0)
|
||||
ft_eprintf("minishell: Pipe must be followed and %s",
|
||||
ft_eprintf("bozoshell: Pipe must be followed and %s",
|
||||
"preced by a command or redirection\n");
|
||||
return (check == 0);
|
||||
}
|
||||
@ -70,7 +70,7 @@ static int ft_special_char_dub(const char *str)
|
||||
if ((y > 2 && (str[i] == '>' || str[i] == '<'))
|
||||
|| (y > 1 && str[i] == '|'))
|
||||
{
|
||||
ft_eprintf("minishell: too many %s in a row\n", str);
|
||||
ft_eprintf("bozoshell: too many %s in a row\n", str);
|
||||
return (1);
|
||||
}
|
||||
i = i + y;
|
||||
|
@ -20,7 +20,7 @@ int ft_change_exit_code(t_data *data, int new_value)
|
||||
exit_code_str = ft_itoa(new_value);
|
||||
if (exit_code_str == NULL)
|
||||
{
|
||||
ft_eprintf("minishell: malloc failed\n");
|
||||
ft_eprintf("bozoshell: malloc failed\n");
|
||||
return (1);
|
||||
}
|
||||
return (0);
|
||||
|
@ -20,19 +20,19 @@ char *ft_get_executable_with_path(t_data *data, const char *name)
|
||||
if (access(name, F_OK) != 0)
|
||||
{
|
||||
*data->exit_code = 127;
|
||||
ft_eprintf("minishell: %s: No such file or directery\n", name);
|
||||
ft_eprintf("bozoshell: %s: No such file or directery\n", name);
|
||||
return (NULL);
|
||||
}
|
||||
if (access(name, X_OK) != 0)
|
||||
{
|
||||
*data->exit_code = 126;
|
||||
ft_eprintf("minishell: %s: permission denied\n", name);
|
||||
ft_eprintf("bozoshell: %s: permission denied\n", name);
|
||||
return (NULL);
|
||||
}
|
||||
path = ft_strdup(name);
|
||||
if (path == NULL)
|
||||
{
|
||||
ft_eprintf("minishell: malloc failed\n");
|
||||
ft_eprintf("bozoshell: malloc failed\n");
|
||||
return (NULL);
|
||||
}
|
||||
return (path);
|
||||
@ -47,13 +47,13 @@ static char **ft_get_paths(t_data *data, const char *name)
|
||||
if (paths == NULL)
|
||||
{
|
||||
*data->exit_code = 127;
|
||||
ft_eprintf("minishell: %s: command not found\n", name);
|
||||
ft_eprintf("bozoshell: %s: command not found\n", name);
|
||||
return (NULL);
|
||||
}
|
||||
tab = ft_split(paths, ':');
|
||||
if (tab == NULL)
|
||||
{
|
||||
ft_eprintf("minishell: malloc failed\n");
|
||||
ft_eprintf("bozoshell: malloc failed\n");
|
||||
return (NULL);
|
||||
}
|
||||
return (tab);
|
||||
@ -66,7 +66,7 @@ static char *ft_file_is_executable(const char *path, const char *name)
|
||||
out = ft_strmerger(3, path, "/", name);
|
||||
if (out == NULL)
|
||||
{
|
||||
ft_eprintf("minishell: malloc failed\n");
|
||||
ft_eprintf("bozoshell: malloc failed\n");
|
||||
free(out);
|
||||
return (NULL);
|
||||
}
|
||||
@ -98,7 +98,7 @@ static char *ft_get_executable_without_path(t_data *data, const char *name)
|
||||
if (path == NULL)
|
||||
{
|
||||
*data->exit_code = 127;
|
||||
ft_eprintf("minishell: %s: command not found\n", name);
|
||||
ft_eprintf("bozoshell: %s: command not found\n", name);
|
||||
}
|
||||
return (path);
|
||||
}
|
||||
|
Reference in New Issue
Block a user