Compare commits

..

2 Commits

Author SHA1 Message Date
Camille Chauvet
1a4ed128e7 core: merge 2023-03-10 12:37:09 +01:00
Camille Chauvet
d36d9c92f5 core: rebuild of parsing and the execution 2023-03-10 12:32:39 +01:00
54 changed files with 710 additions and 595 deletions

View File

@ -1,5 +1,30 @@
UTILS_SRC = utils/ft_is_in_quote.c utils/ft_strncpy.c utils/ft_strreplace.c utils/ft_strnchr.c utils/ft_split_quoted.c utils/ft_strshift.c utils/ft_quote_remover.c utils/ft_str_is_empty.c utils/ft_atoi_check.c UTILS_SRC = utils/ft_is_in_quote.c utils/ft_strncpy.c utils/ft_strreplace.c utils/ft_strnchr.c utils/ft_split_quoted.c utils/ft_strshift.c utils/ft_quote_remover.c utils/ft_str_is_empty.c utils/ft_atoi_check.c ./utils/ft_get_executable.c ./utils/fd.c
SRCS = ${UTILS_SRC} main.c file.c infile.c outfile.c heredoc.c syntatics.c cmd.c cmds.c env.c env2.c env3.c execution.c spacer.c env_fill.c builtins/echo.c builtins/pwd.c builtins/export.c builtins/env.c builtins/cd.c builtins/exit.c builtins/unset.c
BUILTINS_SRC = builtins/pwd.c \
builtins/export.c \
builtins/env.c \
builtins/cd.c \
builtins/exit.c \
builtins/unset.c \
builtins/echo.c
SRCS = ${BUILTINS_SRC} \
${UTILS_SRC} \
main.c \
./cmd/cmd.c \
./env/env1.c \
./env/env2.c \
./env/env3.c \
./env/env_fill.c \
./data/data.c \
./execution/execution.c \
./syntax/syntax.c \
./format/format.c \
./redirection/heredoc.c \
./redirection/infile.c \
./redirection/outfile.c \
./redirection/file.c \
./parse/cmds_parse.c
OBJS = ${SRCS:.c=.o} OBJS = ${SRCS:.c=.o}

8
builtins/:wq Normal file
View File

@ -0,0 +1,8 @@
#ifndef BUILTINS_PRIVATE_H
# define BUILTINS_PRIVATE_H
# include <limits.h>
# include "../libftx/libftx.h"
# include "../env/env.h"
# include "../utils/utils.h"
char *get_pwd(int fd);
#endif

14
builtins/builtins.h Normal file
View File

@ -0,0 +1,14 @@
#ifndef BUILTINS_H
# define BUILTINS_H
# include "../libftx/libft/list.h"
int echo(int fd, char **strs);
int pwd(int fd);
char *get_pwd(int fd);
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 unset(t_list **env, char **args, int fd);
int ft_exit(char **args);
#endif

View File

@ -0,0 +1,8 @@
#ifndef BUILTINS_PRIVATE_H
# define BUILTINS_PRIVATE_H
# include <limits.h>
# include "../libftx/libftx.h"
# include "../env/env.h"
# include "../utils/utils.h"
char *get_pwd(int fd);
#endif

View File

@ -10,7 +10,7 @@
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "../minishell.h" #include "./builtins_private.h"
int make_move(char *path, int fd) int make_move(char *path, int fd)
{ {

View File

@ -10,7 +10,7 @@
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "../minishell.h" #include "./builtins_private.h"
int is_space(char c) int is_space(char c)
{ {

View File

@ -10,7 +10,7 @@
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "../minishell.h" #include "./builtins_private.h"
int print_env(t_list **env, int fd) int print_env(t_list **env, int fd)
{ {

View File

@ -10,7 +10,7 @@
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "../minishell.h" #include "./builtins_private.h"
static int error(int err, char *reason, char *problem) static int error(int err, char *reason, char *problem)
{ {

View File

@ -10,7 +10,7 @@
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "../minishell.h" #include "./builtins_private.h"
static int error(char *str, int fd) static int error(char *str, int fd)
{ {
@ -88,7 +88,7 @@ int add_export(t_list **env, char *args, int fd)
return (0); return (0);
} }
int export(t_list **env, char **args, int fd) int ft_export(t_list **env, char **args, int fd)
{ {
int err; int err;
int i; int i;
@ -105,13 +105,3 @@ int export(t_list **env, char **args, int fd)
} }
return (err); return (err);
} }
/*
int main(int argc, char *argv[], char **env)
{
t_list **n_env;
(void)argc;
n_env = init_env(env);
export(n_env, argv + 1, 1);
return (0);
}*/

View File

@ -10,7 +10,7 @@
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "../minishell.h" #include "./builtins_private.h"
int pwd(int fd) int pwd(int fd)
{ {

View File

@ -10,7 +10,7 @@
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "../minishell.h" #include "./builtins_private.h"
int error(char *str, int fd) int error(char *str, int fd)
{ {

View File

@ -10,9 +10,7 @@
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "libftx/libftx.h" #include "cmd_private.h"
#include "minishell.h"
#include <sys/wait.h>
void ft_cmddel(void *ptr) void ft_cmddel(void *ptr)
{ {
@ -21,37 +19,26 @@ void ft_cmddel(void *ptr)
content = (t_cmd *) ptr; content = (t_cmd *) ptr;
if (content->args != NULL) if (content->args != NULL)
ft_freer_tab_ultimate(1, content->args); ft_freer_tab_ultimate(1, content->args);
if (content->executable != NULL) if (content->own_cmd == false && content->executable != NULL)
free(content->executable); free(content->executable);
if (content->fd_in > 2) if (content->fd_in[0] > 2)
close(content->fd_in); close(content->fd_in[0]);
if (content->fd_out > 2) if (content->fd_out[0] > 2)
close(content->fd_out); close(content->fd_out[0]);
if (content->fd_in[1] > 2)
close(content->fd_in[1]);
if (content->fd_out[1] > 2)
close(content->fd_out[1]);
free(content); free(content);
} }
int ft_cmd_filler(t_data *data, t_list *element, char **args) void ft_cmd_waiter(void *ptr)
{ {
t_cmd *content; t_cmd *cmd;
char *temp;
size_t i;
if (args == NULL) cmd = ptr;
return (1); if (cmd->pid != -1)
content = (t_cmd *)element->content;
i = 0;
while (args[i] != NULL)
{ {
temp = ft_env_filler(data, args[i]); waitpid(cmd->pid, ft_get_exit_code(), 0);
if (temp == NULL)
return (1);
free(args[i]);
args[i] = temp;
ft_quote_remover(temp);
i++;
} }
content->args = args;
content->executable = args[0];
content->pid = -1;
return (0);
} }

19
cmd/cmd.h Normal file
View File

@ -0,0 +1,19 @@
#ifndef CMD_H
# define CMD_H
# include <stdbool.h>
# include "../data/data.h"
typedef struct s_cmd
{
int fd_in[2];
int fd_out[2];
int pid;
char *executable;
char **args;
bool own_cmd;
} t_cmd;
void ft_cmddel(void *content);
void ft_cmd_waiter(void *content);
#endif

8
cmd/cmd_private.h Normal file
View File

@ -0,0 +1,8 @@
#ifndef CMD_PRIVATE_H
# define CMD_PRIVATE_H
# include <sys/types.h>
# include <sys/wait.h>
# include "./cmd.h"
# include "../libftx/libftx.h"
# include "../data/data.h"
#endif

112
cmds.c
View File

@ -1,112 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cmds.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/15 14:17:26 by cchauvet #+# #+# */
/* Updated: 2023/02/17 15:20:29 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "libftx/libftx.h"
#include "minishell.h"
static int ft_cmds_init(t_list **cmds, size_t len)
{
t_cmd *content;
t_list *current;
size_t i;
*cmds = NULL;
if (len > 0)
*cmds = malloc(sizeof(t_list));
current = *cmds;
i = 0;
while (i < len)
{
content = malloc(sizeof(t_cmd));
if (content == NULL)
{
ft_lstclear(cmds, ft_cmddel);
return (1);
}
content->args = NULL;
content->executable = NULL;
content->fd_in = -1;
content->fd_out = -1;
current->content = content;
if (!((i + 1) < len))
{
current->next = NULL;
return (0);
}
current->next = malloc(sizeof(t_list));
if (current->next == NULL)
ft_lstclear(cmds, ft_cmddel);
current = current->next;
i++;
}
return (0);
}
static int ft_cmds_prep(t_list **cmds, const char *line, int infile, int outfile)
{
t_cmd *cmd;
t_list *current;
if (ft_cmds_init(cmds, ft_seglen_quoted(line, '|')))
{
free(cmds);
return (1);
}
if (*cmds == NULL)
return (0);
current = *cmds;
cmd = current->content;
cmd->fd_in = infile;
cmd = ft_lstlast(*cmds)->content;
cmd->fd_out = outfile;
return (0);
}
static int ft_cmds_fill(t_data *data, t_list **cmds, const char *line)
{
char **tab;
char **args;
t_list *current;
size_t i;
tab = ft_split_quoted(line, '|');
if (tab == NULL)
return (1);
i = 0;
current = *cmds;
while (tab[i] != NULL)
{
args = ft_split_quoted(tab[i], ' ');
if (ft_cmd_filler(data, current, args) == 1)
{
ft_lstclear(cmds, ft_cmddel);
ft_freer_tab_ultimate(2, args, tab);
return (1);
}
current = current->next;
i++;
}
ft_freer_tab_ultimate(1, tab);
return (0);
}
t_list **ft_parse_cmds(t_data *data, char *line, int infile, int outfile)
{
t_list **cmds;
cmds = malloc(sizeof(t_list *));
if (ft_cmds_prep(cmds, line, infile, outfile) == 1)
return (NULL);
if (ft_cmds_fill(data, cmds, line) == 1)
return (NULL);
return (cmds);
}

25
data/data.c Normal file
View File

@ -0,0 +1,25 @@
#include "./data_private.h"
int *ft_get_exit_code(void)
{
static int exit_code = 0;
return (&exit_code);
}
int ft_gen_exit_code(t_data *data)
{
char *str;
if (data->exit_code_str != NULL)
free(data->exit_code_str);
str = ft_itoa(data->exit_code);
if (str == NULL)
{
ft_printf("minishell: malloc failed");
return (1);
}
data->exit_code_str = str;
return (0);
}

16
data/data.h Normal file
View File

@ -0,0 +1,16 @@
#ifndef DATA_H
# define DATA_H
# include "../libftx/libft/list.h"
typedef struct s_data
{
t_list **env;
t_list **cmds;
int exit_code;
char *exit_code_str;
} t_data;
int *ft_get_exit_code();
int ft_gen_exit_code(t_data *data);
#endif

6
data/data_private.h Normal file
View File

@ -0,0 +1,6 @@
#ifndef DATA_PRIVATE_H
# define DATA_PRIVATE_H
# include "../libftx/libftx.h"
# include "./data.h"
#endif

26
env/env.h vendored Normal file
View File

@ -0,0 +1,26 @@
#ifndef ENV_H
# define ENV_H
# include <stdbool.h>
# include "../libftx/libft/list.h"
# include "../data/data.h"
typedef struct s_env
{
char *key;
char *value;
bool original;
} t_env;
char *ft_env_filler(t_data *data, const char *str);
void env_del(void *content);
t_list **init_env(char **env);
char **env_to_strs(t_list **head);
int create_value_by_key(char *key, char *value, t_list **head);
int create_value_by_key_dup(char *key, char *value, t_list **head);
int set_value_by_key(char *key, char *value, t_list **head);
char *get_value_by_key(char *key, t_list **head);
void env_del(void *ptr);
int delete_by_key(char *key, t_list **head);
int possible_key(char *key);
#endif

13
env.c → env/env1.c vendored
View File

@ -10,7 +10,7 @@
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "env_private.h"
void add_sort(t_list **head, t_env *var) void add_sort(t_list **head, t_env *var)
{ {
@ -110,14 +110,3 @@ t_list **init_env(char **env)
} }
return (head); return (head);
} }
/*int main(int argc, char *argv[], char **env)
{
t_list **n_env;
n_env = init_env(env);
delete_by_key("SSH_AUTH_SOCK", n_env);
print_env(n_env, 1);
return (0);
}*/

View File

@ -10,8 +10,7 @@
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "libftx/libftx.h" #include "env_private.h"
#include "minishell.h"
int get_index(char *s, char c) int get_index(char *s, char c)
{ {

View File

@ -10,8 +10,7 @@
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "libftx/libftx.h" #include "env_private.h"
#include "minishell.h"
char *get_value(char *str) char *get_value(char *str)
{ {

View File

@ -10,8 +10,7 @@
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "libftx/libftx.h" #include "./env_private.h"
#include "minishell.h"
int ft_gen_exit_code_var(t_data *data) int ft_gen_exit_code_var(t_data *data)
{ {

14
env/env_private.h vendored Normal file
View File

@ -0,0 +1,14 @@
#ifndef ENV_PRIVATE_H
# define ENV_PRIVATE_H
# include "./env.h"
# include "../libftx/libftx.h"
# include "../utils/utils.h"
void swap_env_3(void **a, void **b, void **c);
void swap_env(void **a, void **b);
char *get_value(char *str);
char *get_key(char *str);
int get_index(char *str, char c);
char *ft_env_filler(t_data *data, const char *str);
#endif

View File

@ -1,188 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* execution.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/21 12:45:16 by cchauvet #+# #+# */
/* Updated: 2023/02/28 14:56:05 by erey-bet ### ########.fr */
/* */
/* ************************************************************************** */
#include "libftx/libftx.h"
#include "minishell.h"
#include <sys/wait.h>
#include <unistd.h>
static char *ft_get_executable_path(t_data *data, char *executable_name)
{
char *path;
char *temp;
char **tab;
size_t i;
if (executable_name == NULL)
return (NULL);
path = NULL;
if (executable_name[0] == '.' || executable_name[0] == '/')
{
path = ft_strdup(executable_name);
if (path == NULL)
{
ft_eprintf("minishell: malloc failed\n");
return (NULL);
}
if (access(path, X_OK) == 0)
{
ft_eprintf("minishell: %s: permission denied\n", path);
return (NULL);
}
}
else
{
tab = ft_split(get_value_by_key("PATH", data->env), ':');
if (tab == NULL)
return (NULL);
i = 0;
while (tab[i] != NULL)
{
temp = ft_strmerger(3, tab[i], "/", executable_name);
if (temp == NULL)
{
ft_freer_tab_ultimate(1, tab);
free(executable_name);
ft_eprintf("minishell: malloc failed\n");
}
if (access(temp, X_OK) == 0)
{
path = temp;
break ;
}
free(temp);
i++;
}
if (path == NULL)
{
ft_eprintf("%s: command not found\n", executable_name);
data->exit_code = 127;
}
ft_freer_tab_ultimate(1, tab);
}
return (path);
}
static void ft_executor(t_data *data, t_cmd *cmd)
{
int pid;
char **tab;
if (cmd->fd_in == -1 || cmd->fd_out == -1)
return ;
pid = fork();
if (pid == -1)
return ;
if (pid == 0)
{
tab = env_to_strs(data->env);
if (tab == NULL)
return ;
dup2(cmd->fd_out, 1);
dup2(cmd->fd_in, 0);
if (cmd->fd_out > 2)
close(cmd->fd_out);
if (cmd->fd_in > 2)
close(cmd->fd_in);
execve(cmd->executable, cmd->args, tab);
}
cmd->pid = pid;
}
static int ft_own_cmd(t_data *data, t_cmd *cmd)
{
int return_code;
int exit_code;
return_code = -1;
if (ft_strcmp(cmd->executable, "pwd") == 0)
return_code = pwd(cmd->fd_out);
else if (ft_strcmp(cmd->executable, "env") == 0)
return_code = print_env(data->env, cmd->fd_out);
else if (ft_strcmp(cmd->executable, "export") == 0)
return_code = (export(data->env,cmd->args + 1, cmd->fd_out));
else if (ft_strcmp(cmd->executable, "cd") == 0)
return_code = (move_folder(cmd->args + 1, cmd->fd_out));
if (ft_strcmp(cmd->executable, "unset") == 0)
return_code = (unset(data->env, cmd->args, cmd->fd_out));
else if (ft_strcmp(cmd->executable, "echo") == 0)
return_code = (echo(cmd->fd_out, cmd->args + 1));
else if (ft_strcmp(cmd->executable, "exit") == 0)
{
exit_code = ft_exit(cmd->args + 1);
if (exit_code > -1)
{
data->exit_code = exit_code;
return_code = -2;
}
else
{
data->exit_code = 1;
return_code = -3;
}
}
if (return_code >= 0)
data->exit_code = return_code;
if (return_code != -1)
cmd->executable = NULL;
return (return_code);
}
int ft_cmds_executor(t_data *data, t_list **cmds)
{
t_cmd *content;
t_list *current;
int exit_code;
int fds[2];
current = *cmds;
while (current != NULL)
{
content = current->content;
if (current->next != NULL)
{
if (pipe(fds) == -1)
{
ft_eprintf("minishell: pipe failed\n");
return (1);
}
content->fd_out = fds[1];
((t_cmd *) current->next->content)->fd_in = fds[0];
}
exit_code = ft_own_cmd(data, content);
if (exit_code == -1)
{
content->executable = ft_get_executable_path(data,
content->executable);
if (content->executable != NULL)
ft_executor(data, content);
}
else if (exit_code == -2)
return (1);
if (ft_gen_exit_code_var(data))
return (1);
if (content->fd_in > 2)
close(content->fd_in);
if (content->fd_out > 2)
close(content->fd_out);
current = current->next;
}
current = *cmds;
while (current != NULL)
{
content = current->content;
if (content->pid != -1)
waitpid(content->pid, &(data->exit_code), 0);
current = current->next;
}
return (0);
}

115
execution/execution.c Normal file
View File

@ -0,0 +1,115 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* execution.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/21 12:45:16 by cchauvet #+# #+# */
/* Updated: 2023/02/28 14:56:05 by erey-bet ### ########.fr */
/* */
/* ************************************************************************** */
#include "execution_private.h"
static int ft_own_cmd(t_data *data, t_cmd *cmd)
{
int return_code;
return_code = -1;
if (ft_strcmp(cmd->executable, "pwd") == 0)
return_code = pwd(cmd->fd_out[0]);
else if (ft_strcmp(cmd->executable, "env") == 0)
return_code = print_env(data->env, cmd->fd_out[0]);
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]));
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)
return_code = (echo(cmd->fd_out[0], cmd->args + 1));
else if (ft_strcmp(cmd->executable, "exit") == 0)
{
return_code = ft_exit(cmd->args + 1);
if (return_code > 0)
{
data->exit_code = return_code;
return (-1);
}
}
return (return_code);
}
static int ft_executor(t_cmd *cmd, char **env)
{
if (cmd->fd_in[0] == -1 || cmd->fd_out[0] == -1 || cmd->executable == NULL)
return (0);
cmd->pid = fork();
if (cmd->pid == -1)
return (1);
if (cmd->pid == 0)
{
dup2(cmd->fd_in[0], 0);
dup2(cmd->fd_out[0], 1);
if (cmd->fd_in[0] > 2)
close(cmd->fd_in[0]);
if (cmd->fd_out[0] > 2)
close(cmd->fd_out[0]);
execve(cmd->executable, cmd->args, env);
}
return (0);
}
static int ft_cmd_executor(t_data *data, t_cmd *cmd)
{
int exit_code;
char **env;
if (cmd->own_cmd == 1)
{
exit_code = ft_own_cmd(data, cmd);
if (exit_code == -1)
return (1);
}
else
{
env = env_to_strs(data->env);
if (env == NULL)
return (1);
exit_code = ft_executor(cmd, env);
if (exit_code == -1)
return (1);
ft_freer_tab_ultimate(1, env);
}
if (ft_gen_exit_code(data))
return (1);
return (0);
}
int ft_cmds_executor(t_data *data)
{
int fds[2];
t_list *current;
t_cmd *content;
current = *data->cmds;
while (current != NULL)
{
content = current->content;
if (current->next != NULL)
{
if (pipe(fds) == -1)
return (1);
ft_add_fd(content->fd_out, fds[1]);
ft_add_fd(((t_cmd *) (current->next->content))->fd_in, fds[0]);
}
if (ft_cmd_executor(data, content))
return (1);
ft_closer(content->fd_in);
ft_closer(content->fd_out);
current = current->next;
}
ft_lstiter(*data->cmds, ft_cmd_waiter);
return (0);
}

7
execution/execution.h Normal file
View File

@ -0,0 +1,7 @@
#ifndef EXECUTION_H
# define EXECUTION_H
# include "../data/data.h"
int ft_cmds_executor(t_data *data);
#endif

View File

@ -0,0 +1,11 @@
#ifndef EXECUTION_PRIVATE_H
# define EXECUTION_PRIVATE_H
# include "../data/data.h"
# include "../libftx/libftx.h"
# include "../cmd/cmd.h"
# include "../env/env.h"
# include "../builtins/builtins.h"
void ft_closer(int fds[2]);
void ft_add_fd(int fds[2], int fd);
#endif

View File

@ -10,9 +10,7 @@
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "libftx/libftx.h" #include "./format_private.h"
#include "minishell.h"
#include "utils/utils.h"
static char *ft_spacer_after(const char *str) static char *ft_spacer_after(const char *str)
{ {
@ -97,7 +95,7 @@ static void ft_space_simplifier(char *str)
} }
} }
char *ft_normalizer(char *str) char *ft_formater(char *str)
{ {
char *out; char *out;
char *temp; char *temp;

6
format/format.h Normal file
View File

@ -0,0 +1,6 @@
#ifndef FORMAT_H
# define FORMAT_H
char *ft_formater(char *str);
#endif

6
format/format_private.h Normal file
View File

@ -0,0 +1,6 @@
#ifndef FORMAT_PRIVATE_H
# define FORMAT_PRIVATE_H
# include "../libftx/libftx.h"
# include "../utils/utils.h"
#endif

Binary file not shown.

View File

@ -14,6 +14,7 @@
# define LIBFT_H # define LIBFT_H
# include <stdlib.h> # include <stdlib.h>
# include <unistd.h> # include <unistd.h>
# include "./list.h"
void *ft_cancel(void **tab, size_t len); void *ft_cancel(void **tab, size_t len);
int ft_atoi(const char *nptr); int ft_atoi(const char *nptr);
@ -52,20 +53,4 @@ void ft_putstr_fd(char *s, int fd);
void ft_putendl_fd(char *s, int fd); void ft_putendl_fd(char *s, int fd);
void ft_putnbr_fd(int n, int fd); void ft_putnbr_fd(int n, int fd);
typedef struct s_list
{
void *content;
struct s_list *next;
} t_list;
t_list *ft_lstnew(void *content);
void ft_lstadd_front(t_list **lst, t_list *nouveau);
int ft_lstsize(t_list *lst);
t_list *ft_lstlast(t_list *lst);
void ft_lstadd_back(t_list **lst, t_list *nouveau);
void ft_lstdelone(t_list *lst, void (*del)(void *));
void ft_lstclear(t_list **lst, void (*del)(void *));
void ft_lstiter(t_list *lst, void (*f)(void *));
t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *));
#endif #endif

20
libftx/libft/list.h Normal file
View File

@ -0,0 +1,20 @@
#ifndef LIST_H
# define LIST_H
typedef struct s_list
{
void *content;
struct s_list *next;
} t_list;
t_list *ft_lstnew(void *content);
void ft_lstadd_front(t_list **lst, t_list *nouveau);
int ft_lstsize(t_list *lst);
t_list *ft_lstlast(t_list *lst);
void ft_lstadd_back(t_list **lst, t_list *nouveau);
void ft_lstdelone(t_list *lst, void (*del)(void *));
void ft_lstclear(t_list **lst, void (*del)(void *));
void ft_lstiter(t_list *lst, void (*f)(void *));
t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *));
#endif

View File

@ -15,6 +15,7 @@
# include <stdlib.h> # include <stdlib.h>
# include <unistd.h> # include <unistd.h>
# include <stdarg.h> # include <stdarg.h>
# include "./libft/libft.h"
char *ft_ultoa_base(unsigned long long n, char *base); char *ft_ultoa_base(unsigned long long n, char *base);
int ft_printf(const char *format, ...); int ft_printf(const char *format, ...);
@ -39,57 +40,41 @@ void ft_swap(void *a, void *b);
void ft_swap_int(int *a, int *b); void ft_swap_int(int *a, int *b);
void ft_swap_char(char *a, char *b); void ft_swap_char(char *a, char *b);
void *ft_cancel(void **tab, size_t len); /* void *ft_cancel(void **tab, size_t len); */
int ft_atoi(const char *nptr); /* int ft_atoi(const char *nptr); */
void ft_bzero(void *s, size_t n); /* void ft_bzero(void *s, size_t n); */
void *ft_calloc(size_t nmemb, size_t size); /* void *ft_calloc(size_t nmemb, size_t size); */
int ft_isalnum(int c); /* int ft_isalnum(int c); */
int ft_isalpha(int c); /* int ft_isalpha(int c); */
int ft_isascii(int c); /* int ft_isascii(int c); */
int ft_isdigit(int c); /* int ft_isdigit(int c); */
int ft_isprint(int c); /* int ft_isprint(int c); */
void *ft_memchr(const void *s, int c, size_t n); /* void *ft_memchr(const void *s, int c, size_t n); */
int ft_memcmp(const void *s1, const void *s2, size_t n); /* int ft_memcmp(const void *s1, const void *s2, size_t n); */
void *ft_memcpy(void *dest, const void *src, size_t n); /* void *ft_memcpy(void *dest, const void *src, size_t n); */
void *ft_memmove(void *dest, const void *src, size_t n); /* void *ft_memmove(void *dest, const void *src, size_t n); */
void *ft_memset(void *s, int c, size_t n); /* void *ft_memset(void *s, int c, size_t n); */
char *ft_strchr(const char *s, int c); /* char *ft_strchr(const char *s, int c); */
char *ft_strdup(const char *s); /* char *ft_strdup(const char *s); */
size_t ft_strlcat(char *dst, const char *src, size_t size); /* size_t ft_strlcat(char *dst, const char *src, size_t size); */
size_t ft_strlcpy(char *dst, const char *src, size_t size); /* size_t ft_strlcpy(char *dst, const char *src, size_t size); */
size_t ft_strlen(const char *s); /* size_t ft_strlen(const char *s); */
int ft_strncmp(const char *s1, const char *s2, size_t n); /* int ft_strncmp(const char *s1, const char *s2, size_t n); */
char *ft_strnstr(const char *big, const char *little, size_t len); /* char *ft_strnstr(const char *big, const char *little, size_t len); */
char *ft_strrchr(const char *s, int c); /* char *ft_strrchr(const char *s, int c); */
int ft_tolower(int c); /* int ft_tolower(int c); */
int ft_toupper(int c); /* int ft_toupper(int c); */
char *ft_substr(char const *s, unsigned int start, size_t len); /* char *ft_substr(char const *s, unsigned int start, size_t len); */
char *ft_strjoin(char const *s1, char const *s2); /* char *ft_strjoin(char const *s1, char const *s2); */
char *ft_strtrim(char const *s1, char const *set); /* char *ft_strtrim(char const *s1, char const *set); */
char **ft_split(char const *s, char c); /* char **ft_split(char const *s, char c); */
char *ft_itoa(int n); /* char *ft_itoa(int n); */
char *ft_strmapi(char const *s, char (*f)(unsigned int, char)); /* char *ft_strmapi(char const *s, char (*f)(unsigned int, char)); */
void ft_striteri(char *s, void (*f)(unsigned int, char*)); /* void ft_striteri(char *s, void (*f)(unsigned int, char*)); */
void ft_putchar_fd(char c, int fd); /* void ft_putchar_fd(char c, int fd); */
void ft_putstr_fd(char *s, int fd); /* void ft_putstr_fd(char *s, int fd); */
void ft_putendl_fd(char *s, int fd); /* void ft_putendl_fd(char *s, int fd); */
void ft_putnbr_fd(int n, int fd); /* void ft_putnbr_fd(int n, int fd); */
typedef struct s_list
{
void *content;
struct s_list *next;
} t_list;
t_list *ft_lstnew(void *content);
void ft_lstadd_front(t_list **lst, t_list *nouveau);
int ft_lstsize(t_list *lst);
t_list *ft_lstlast(t_list *lst);
void ft_lstadd_back(t_list **lst, t_list *nouveau);
void ft_lstdelone(t_list *lst, void (*del)(void *));
void ft_lstclear(t_list **lst, void (*del)(void *));
void ft_lstiter(t_list *lst, void (*f)(void *));
t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *));
#endif #endif

68
main.c
View File

@ -10,9 +10,7 @@
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "libftx/libftx.h"
#include "minishell.h" #include "minishell.h"
#include <readline/readline.h>
static char *ft_get_user_input() static char *ft_get_user_input()
{ {
@ -39,64 +37,23 @@ static char *ft_get_user_input()
static int ft_minishell(t_data *data, char *line) static int ft_minishell(t_data *data, char *line)
{ {
t_list **cmds;
char *line_clean; char *line_clean;
int infile;
int outfile;
if (ft_syntatic_verif(data, line)) if (ft_syntax_verif(data, line))
return (0); return (0);
line_clean = ft_normalizer(line); line_clean = ft_formater(line);
if (line_clean == NULL) if (line_clean == NULL)
return (0); return (0);
outfile = ft_outfile(data, line_clean); if (ft_cmds_parser(data, line_clean))
if (outfile == -2)
{ {
free(line_clean); free(line_clean);
return (0); return (0);
} }
infile = ft_infile(data, line_clean); if (ft_cmds_executor(data) == 1)
if (infile == -2)
{ {
if (outfile > 2)
close(outfile);
free(line_clean);
return (0);
}
if (ft_gen_exit_code_var(data))
{
if (outfile > 2)
close(outfile);
if (infile > 2)
close(infile);
free(line_clean); free(line_clean);
return (1); return (1);
} }
cmds = ft_parse_cmds(data, line_clean, infile, outfile);
if (cmds == NULL)
{
if (outfile > 2)
close(outfile);
if (infile > 2)
close(infile);
ft_lstclear(cmds, ft_cmddel);
free(cmds);
free(line_clean);
return (1);
}
if (ft_cmds_executor(data, cmds))
{
if (outfile > 2)
close(outfile);
if (infile > 2)
close(infile);
ft_lstclear(cmds, ft_cmddel);
free(cmds);
free(line_clean);
return (1);
}
ft_lstclear(cmds, ft_cmddel);
free(cmds);
free(line_clean); free(line_clean);
return (0); return (0);
} }
@ -122,12 +79,8 @@ void ft_ctrlc(int num)
void ft_quit(int num) void ft_quit(int num)
{ {
(void) num; (void) num;
if (*ft_get_heredoc())
{ ft_printf("%c%c %c%c", 0, 0, 0, 0);
ft_printf("pb");
}
else
ft_printf("bozoman");
} }
int main(int ac, char **av, char **env) int main(int ac, char **av, char **env)
@ -141,7 +94,11 @@ int main(int ac, char **av, char **env)
signal(SIGQUIT, ft_quit); signal(SIGQUIT, ft_quit);
data.exit_code = 0; data.exit_code = 0;
data.exit_code_str = NULL; data.exit_code_str = NULL;
ft_gen_exit_code_var(&data); ft_gen_exit_code(&data);
data.cmds = malloc(sizeof(t_cmd *));
if (data.cmds == NULL)
return (1);
*data.cmds = NULL;
data.env = init_env(env); data.env = init_env(env);
if (data.env == NULL) if (data.env == NULL)
return (1); return (1);
@ -155,7 +112,10 @@ int main(int ac, char **av, char **env)
if (line == NULL) if (line == NULL)
break ; break ;
} }
free(data.exit_code_str); free(data.exit_code_str);
ft_lstclear(data.cmds, ft_cmddel);
free(data.cmds);
ft_lstclear(data.env, env_del); ft_lstclear(data.env, env_del);
free(data.env); free(data.env);
return (data.exit_code); return (data.exit_code);

View File

@ -12,95 +12,19 @@
#ifndef MINISHELL_H #ifndef MINISHELL_H
# define MINISHELL_H # define MINISHELL_H
# include "libftx/libftx.h" # include "./env/env.h"
# include "utils/utils.h" # include "./cmd/cmd.h"
# include <sys/types.h> # include "./parse/parse.h"
# include <sys/stat.h> # include "./syntax/syntax.h"
# include <fcntl.h> # include "./execution/execution.h"
# include <sys/wait.h> # include "./builtins/builtins.h"
# include <stdio.h> # include "./format/format.h"
# include <unistd.h> # include "./redirection/redirection.h"
# include <limits.h> # include "./libftx/libftx.h"
# include <string.h> # include "./utils/utils.h"
# include "./data/data.h"
# include <readline/readline.h> # include <readline/readline.h>
# include <readline/history.h> # include <readline/history.h>
# define DEBUG 0 # include <signal.h>
typedef struct s_data
{
t_list **env;
int exit_code;
char *exit_code_str;
} t_data;
int ft_syntatic_verif(t_data *data, const char *str);
int ft_file_is_readable(const char *path);
int ft_file_is_writable(const char *path);
int ft_file_is_appendable(const char *path);
char *ft_get_file_path(const char *infile);
int ft_infile(t_data *data, char *line);
int ft_outfile(t_data *data, char *line);
int ft_heredoc(t_data *data, char *stop);
int *ft_get_heredoc();
size_t ft_seglen_quoted(const char *str, char c);
int ft_cmds_executor(t_data *data, t_list **cmds);
char **ft_split_quoted(const char *s, char c);
void ft_cmddel(void *content);
void env_del(void *content);
t_list **ft_parse_cmds(t_data *data, char *line, int infile, int outfile);
int ft_gen_exit_code_var(t_data *data);
int ft_cmd_filler(t_data *data, t_list *current, char **args);
char *ft_normalizer(char *str);
char *ft_env_filler(t_data *data, const char *str);
char **env_to_strs(t_list **head);
/* Environnement */
t_list **init_env(char **env);
char **env_to_strs(t_list **head);
int create_value_by_key(char *key, char *value, t_list **head);
int create_value_by_key_dup(char *key, char *value, t_list **head);
int set_value_by_key(char *key, char *value, t_list **head);
char *get_value_by_key(char *key, t_list **head);
int get_index(char *s, char c);
void swap_env_3(void **a, void **b, void **c);
void swap_env(void **a, void **b);
void env_del(void *ptr);
char **env_to_strs(t_list **head);
char *get_value(char *str);
char *get_key(char *str);
int delete_by_key(char *key, t_list **head);
int possible_key(char *key);
/* Echo */
int echo(int fd, char **strs);
/* PWD */
int pwd(int fd);
char *get_pwd(int fd);
/* Env */
int print_env(t_list **env, int fd);
/* Export */
int export(t_list **env, char **args, int fd);
/* CD */
int move_folder(char **args, int fd);
/* Unset */
int unset(t_list **env, char **args, int fd);
/* Exit */
int ft_exit(char **args);
typedef struct s_cmd
{
int fd_in;
int fd_out;
int pid;
char *executable;
char **args;
} t_cmd;
typedef struct s_env
{
char *key;
char *value;
int original;
} t_env;
#endif #endif

@ -1 +0,0 @@
Subproject commit 1c6111b2fd281937d38ebfa7e8d87b38baef0802

128
parse/cmds_parse.c Normal file
View File

@ -0,0 +1,128 @@
#include "./parse_private.h"
static int ft_redirection_parse(t_data *data, char *cmd_str, t_cmd *cmd)
{
int fd_in;
int fd_out;
fd_in = ft_infile(data, cmd_str);
if (fd_in == -2)
return (1);
fd_out = ft_outfile(data, cmd_str);
if (fd_out == -2)
{
close(fd_in);
return (1);
}
cmd->fd_in[0] = fd_in;
cmd->fd_in[1] = -1;
cmd->fd_out[0] = fd_out;
cmd->fd_out[1] = -1;
return (0);
}
int ft_args_parse(t_data *data, char *cmd_str, t_cmd *cmd)
{
char **tab;
char *str;
size_t i;
tab = ft_split_quoted(cmd_str, ' ');
if (tab == NULL)
return (1);
i = 0;
while (tab[i] == NULL)
{
str = ft_env_filler(data, tab[i]);
if (str == NULL)
{
ft_freer_tab_ultimate(1, tab);
return (1);
}
free(tab[i]);
tab[i] = str;
i++;
}
cmd->args = tab;
return (0);
}
int ft_executable_parse(t_data *data, t_cmd *cmd)
{
bool own;
char *path;
path = cmd->args[0];
own = 0;
if (ft_strcmp(cmd->args[0], "env") == 0)
own = 1;
else if (ft_strcmp(cmd->args[0], "export") == 0)
own = 1;
else if (ft_strcmp(cmd->args[0], "echo") == 0)
own = 1;
else if (ft_strcmp(cmd->args[0], "unset") == 0)
own = 1;
else if (ft_strcmp(cmd->args[0], "exit") == 0)
own = 1;
else if (ft_strcmp(cmd->args[0], "pwd") == 0)
own = 1;
else if (ft_strcmp(cmd->args[0], "cmd") == 0)
own = 1;
if (own == 0)
path = ft_get_executable(data->env, cmd->args[0]);
cmd->own_cmd = own;
cmd->executable = path;
return (own);
}
int ft_cmd_parser(t_data *data, char *cmd_str)
{
t_cmd *cmd;
t_list *element;
cmd = ft_calloc(sizeof(t_cmd), 1);
if (cmd == NULL)
{
ft_eprintf("minishell: malloc failed\n");
return (1);
}
if (ft_redirection_parse(data, cmd_str, cmd))
return (1);
if (ft_args_parse(data, cmd_str, cmd))
{
ft_cmddel(cmd);
return (1);
}
ft_executable_parse(data, cmd);
element = ft_lstnew(cmd);
if (element == NULL)
{
ft_cmddel(cmd);
return (1);
}
ft_lstadd_back(data->cmds, element);
return (0);
}
int ft_cmds_parser(t_data *data, const char *line)
{
char **tab;
size_t i;
tab = ft_split_quoted(line, '|');
if (tab == NULL)
{
ft_eprintf("minishell: malloc failed\n");
return (1);
}
i = 0;
while (tab[i] != NULL)
{
ft_cmd_parser(data, tab[i]);
i++;
}
ft_add_fd(((t_cmd *) (*data->cmds)->content)->fd_in, 0);
ft_add_fd(((t_cmd *) ((ft_lstlast(*data->cmds))->content))->fd_out, 1);
ft_freer_tab_ultimate(1, tab);
return (0);
}

7
parse/parse.h Normal file
View File

@ -0,0 +1,7 @@
#ifndef PARSE_H
# define PARSE_H
# include "../data/data.h"
int ft_cmds_parser(t_data *data, const char *line);
#endif

9
parse/parse_private.h Normal file
View File

@ -0,0 +1,9 @@
#ifndef PARSE_PRIVATE_H
# define PARSE_PRIVATE_H
# include "../redirection/redirection.h"
# include "../libftx/libftx.h"
# include "../utils/utils.h"
# include "../env/env.h"
# include "../data/data.h"
# include "../cmd/cmd.h"
#endif

View File

@ -10,8 +10,7 @@
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "./redirection_private.h"
#include <unistd.h>
int ft_file_is_readable(const char *path) int ft_file_is_readable(const char *path)
{ {

View File

@ -10,10 +10,7 @@
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "libftx/libftx.h" #include "./redirection_private.h"
#include "minishell.h"
#include <stdlib.h>
#include <unistd.h>
int *ft_get_heredoc() int *ft_get_heredoc()
{ {

View File

@ -10,7 +10,7 @@
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "./redirection_private.h"
static int ft_infile_is_valid(t_data *data, const char *line) static int ft_infile_is_valid(t_data *data, const char *line)
{ {
@ -70,7 +70,7 @@ static int ft_get_infile(t_data *data, const char *line)
ft_eprintf("minishell: malloc failed\n"); ft_eprintf("minishell: malloc failed\n");
return (-2); return (-2);
} }
fd = 0; fd = -1;
i = 0; i = 0;
while (tab[i + 1] != NULL) while (tab[i + 1] != NULL)
{ {

View File

@ -10,7 +10,7 @@
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "./redirection_private.h"
static int ft_outfile_is_valid(t_data *data, const char *line) static int ft_outfile_is_valid(t_data *data, const char *line)
{ {
@ -69,7 +69,7 @@ static int ft_get_outfile(t_data *data, const char *line)
ft_eprintf("minishell: malloc failed\n"); ft_eprintf("minishell: malloc failed\n");
return (-2); return (-2);
} }
fd = 1; fd = -1;
i = 0; i = 0;
while (tab[i + 1] != NULL) while (tab[i + 1] != NULL)
{ {

View File

@ -0,0 +1,8 @@
#ifndef REDIRECTION_H
# define REDIRECTION_H
# include "../data/data.h"
int ft_infile(t_data *data, char *line);
int ft_outfile(t_data *data, char *line);
int *ft_get_heredoc();
#endif

View File

@ -0,0 +1,16 @@
#ifndef REDIRECTIONS_PRIVATE
# define REDIRECTIONS_PRIVATE
# include <sys/stat.h>
# include <fcntl.h>
# include <stddef.h>
# include <unistd.h>
# include "../libftx/libftx.h"
# include "../data/data.h"
# include "../env/env.h"
# include "../utils/utils.h"
int ft_file_is_readable(const char *path);
int ft_file_is_writable(const char *path);
int ft_heredoc(t_data *data, char *stop);
#endif

View File

@ -10,9 +10,8 @@
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "libftx/libftx.h" #include "../libftx/libftx.h"
#include "minishell.h" #include "../utils/utils.h"
#include "utils/utils.h"
static int ft_quote_verif(const char *str) static int ft_quote_verif(const char *str)
{ {
@ -91,7 +90,7 @@ static int ft_special_char_dub(const char *str)
return (0); return (0);
} }
int ft_empty_verif(const char *str) static int ft_empty_verif(const char *str)
{ {
size_t i; size_t i;
@ -101,7 +100,7 @@ int ft_empty_verif(const char *str)
return (str[i] == '\0'); return (str[i] == '\0');
} }
int ft_syntatic_verif(t_data *data, const char *str) int ft_syntax_verif(t_data *data, const char *str)
{ {
if (ft_quote_verif(str) if (ft_quote_verif(str)
|| ft_empty_verif(str) || ft_empty_verif(str)

7
syntax/syntax.h Normal file
View File

@ -0,0 +1,7 @@
#ifndef SYNTAX_H
# define SYNTAX_H
# include "../data/data.h"
int ft_syntax_verif(t_data *data, const char *str);
#endif

17
utils/fd.c Normal file
View File

@ -0,0 +1,17 @@
#include "./utils.h"
void ft_closer(int fds[2])
{
if (fds[0] > 2)
close(fds[0]);
if (fds[1] > 2)
close(fds[1]);
}
void ft_add_fd(int fds[2], int fd)
{
if (fds[0] == -1)
fds[0] = fd;
else
fds[1] = fd;
}

74
utils/ft_get_executable.c Normal file
View File

@ -0,0 +1,74 @@
#include "./utils.h"
#include <unistd.h>
char *ft_get_executable_with_path(const char *name)
{
char *path;
if (access(name, F_OK) == 0)
{
ft_eprintf("minishell: %s bash: No such file or directery\n");
return (NULL);
}
if (access(name, X_OK) != 0)
{
ft_eprintf("minishell: %s: permission denied\n", name);
return (NULL);
}
path = ft_strdup(name);
if (path == NULL)
{
ft_eprintf("minishell: malloc failed\n");
return (NULL);
}
return (path);
}
char *ft_get_executable_without_path(t_list **env, const char *name)
{
char **tab;
char *paths;
char *path;
size_t i;
path = NULL;
paths = get_value_by_key("PATH", env);
if (paths == NULL)
path = "";
tab = ft_split(paths, ':');
if (tab == NULL)
{
ft_eprintf("minishell: malloc failed\n");
return (NULL);
}
i = 0;
while (tab[i] != NULL)
{
path = ft_strmerger(3, tab[i], "/", name);
if (path == NULL)
{
ft_eprintf("minishell: malloc failed\n");
break;
}
if (access(path, X_OK))
break;
free(path);
path = NULL;
i++;
}
ft_freer_tab_ultimate(1, tab);
if (path == NULL)
ft_eprintf("minishell: %s: command not found\n", name);
return (path);
}
char *ft_get_executable(t_list **env, const char *name)
{
char *path;
if (name[0] == '.' || name[0] == '/')
path = ft_get_executable_with_path(name);
else
path = ft_get_executable_without_path(env, name);
return (path);
}

Binary file not shown.

Binary file not shown.

View File

@ -14,6 +14,8 @@
# define UTILS_H # define UTILS_H
# include <stdlib.h> # include <stdlib.h>
# include "../libftx/libftx.h" # include "../libftx/libftx.h"
# include "../data/data.h"
# include "../env/env.h"
size_t ft_strncpy(char *dst, const char *src, size_t n); size_t ft_strncpy(char *dst, const char *src, size_t n);
int ft_is_in_quote(const char *str, size_t n); int ft_is_in_quote(const char *str, size_t n);
@ -26,5 +28,9 @@ char **ft_split_quoted(const char *s, char c);
void ft_strshift(char *str, int shift); void ft_strshift(char *str, int shift);
char *ft_quote_remover(char *str); char *ft_quote_remover(char *str);
int ft_atoi_check(const char *nptr); int ft_atoi_check(const char *nptr);
char *ft_get_executable(t_list **env, const char *name);
void ft_closer(int fds[2]);
void ft_add_fd(int fds[2], int fd);
#endif #endif