Compare commits
No commits in common. "1a4ed128e7b12b9bdbe3d5af6aa389eb4296ece7" and "6c8e161a32fed5bbee9e011fb90ccdde1a09cba6" have entirely different histories.
1a4ed128e7
...
6c8e161a32
29
Makefile
29
Makefile
@ -1,30 +1,5 @@
|
||||
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
|
||||
|
||||
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
|
||||
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
|
||||
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
|
||||
|
||||
OBJS = ${SRCS:.c=.o}
|
||||
|
||||
|
@ -1,8 +0,0 @@
|
||||
#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
|
@ -1,14 +0,0 @@
|
||||
#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
|
@ -1,8 +0,0 @@
|
||||
#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
|
@ -10,7 +10,7 @@
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "./builtins_private.h"
|
||||
#include "../minishell.h"
|
||||
|
||||
int make_move(char *path, int fd)
|
||||
{
|
||||
|
@ -10,7 +10,7 @@
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "./builtins_private.h"
|
||||
#include "../minishell.h"
|
||||
|
||||
int is_space(char c)
|
||||
{
|
||||
|
@ -10,7 +10,7 @@
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "./builtins_private.h"
|
||||
#include "../minishell.h"
|
||||
|
||||
int print_env(t_list **env, int fd)
|
||||
{
|
||||
|
@ -10,7 +10,7 @@
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "./builtins_private.h"
|
||||
#include "../minishell.h"
|
||||
|
||||
static int error(int err, char *reason, char *problem)
|
||||
{
|
||||
|
@ -10,7 +10,7 @@
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "./builtins_private.h"
|
||||
#include "../minishell.h"
|
||||
|
||||
static int error(char *str, int fd)
|
||||
{
|
||||
@ -88,7 +88,7 @@ int add_export(t_list **env, char *args, int fd)
|
||||
return (0);
|
||||
}
|
||||
|
||||
int ft_export(t_list **env, char **args, int fd)
|
||||
int export(t_list **env, char **args, int fd)
|
||||
{
|
||||
int err;
|
||||
int i;
|
||||
@ -105,3 +105,13 @@ int ft_export(t_list **env, char **args, int fd)
|
||||
}
|
||||
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);
|
||||
}*/
|
||||
|
@ -10,7 +10,7 @@
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "./builtins_private.h"
|
||||
#include "../minishell.h"
|
||||
|
||||
int pwd(int fd)
|
||||
{
|
||||
|
@ -10,7 +10,7 @@
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "./builtins_private.h"
|
||||
#include "../minishell.h"
|
||||
|
||||
int error(char *str, int fd)
|
||||
{
|
||||
|
@ -10,7 +10,9 @@
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "cmd_private.h"
|
||||
#include "libftx/libftx.h"
|
||||
#include "minishell.h"
|
||||
#include <sys/wait.h>
|
||||
|
||||
void ft_cmddel(void *ptr)
|
||||
{
|
||||
@ -19,26 +21,37 @@ void ft_cmddel(void *ptr)
|
||||
content = (t_cmd *) ptr;
|
||||
if (content->args != NULL)
|
||||
ft_freer_tab_ultimate(1, content->args);
|
||||
if (content->own_cmd == false && content->executable != NULL)
|
||||
if (content->executable != NULL)
|
||||
free(content->executable);
|
||||
if (content->fd_in[0] > 2)
|
||||
close(content->fd_in[0]);
|
||||
if (content->fd_out[0] > 2)
|
||||
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]);
|
||||
if (content->fd_in > 2)
|
||||
close(content->fd_in);
|
||||
if (content->fd_out > 2)
|
||||
close(content->fd_out);
|
||||
free(content);
|
||||
}
|
||||
|
||||
void ft_cmd_waiter(void *ptr)
|
||||
int ft_cmd_filler(t_data *data, t_list *element, char **args)
|
||||
{
|
||||
t_cmd *cmd;
|
||||
t_cmd *content;
|
||||
char *temp;
|
||||
size_t i;
|
||||
|
||||
cmd = ptr;
|
||||
if (cmd->pid != -1)
|
||||
if (args == NULL)
|
||||
return (1);
|
||||
content = (t_cmd *)element->content;
|
||||
i = 0;
|
||||
while (args[i] != NULL)
|
||||
{
|
||||
waitpid(cmd->pid, ft_get_exit_code(), 0);
|
||||
temp = ft_env_filler(data, args[i]);
|
||||
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
19
cmd/cmd.h
@ -1,19 +0,0 @@
|
||||
#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
|
@ -1,8 +0,0 @@
|
||||
#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
Normal file
112
cmds.c
Normal file
@ -0,0 +1,112 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* 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
25
data/data.c
@ -1,25 +0,0 @@
|
||||
#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
16
data/data.h
@ -1,16 +0,0 @@
|
||||
#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
|
@ -1,6 +0,0 @@
|
||||
#ifndef DATA_PRIVATE_H
|
||||
# define DATA_PRIVATE_H
|
||||
# include "../libftx/libftx.h"
|
||||
# include "./data.h"
|
||||
|
||||
#endif
|
@ -10,7 +10,7 @@
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "env_private.h"
|
||||
#include "minishell.h"
|
||||
|
||||
void add_sort(t_list **head, t_env *var)
|
||||
{
|
||||
@ -110,3 +110,14 @@ t_list **init_env(char **env)
|
||||
}
|
||||
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);
|
||||
}*/
|
26
env/env.h
vendored
26
env/env.h
vendored
@ -1,26 +0,0 @@
|
||||
#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
|
14
env/env_private.h
vendored
14
env/env_private.h
vendored
@ -1,14 +0,0 @@
|
||||
#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
|
@ -10,7 +10,8 @@
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "env_private.h"
|
||||
#include "libftx/libftx.h"
|
||||
#include "minishell.h"
|
||||
|
||||
int get_index(char *s, char c)
|
||||
{
|
@ -10,7 +10,8 @@
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "env_private.h"
|
||||
#include "libftx/libftx.h"
|
||||
#include "minishell.h"
|
||||
|
||||
char *get_value(char *str)
|
||||
{
|
@ -10,7 +10,8 @@
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "./env_private.h"
|
||||
#include "libftx/libftx.h"
|
||||
#include "minishell.h"
|
||||
|
||||
int ft_gen_exit_code_var(t_data *data)
|
||||
{
|
188
execution.c
Normal file
188
execution.c
Normal file
@ -0,0 +1,188 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* 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);
|
||||
}
|
@ -1,115 +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 "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);
|
||||
}
|
@ -1,7 +0,0 @@
|
||||
#ifndef EXECUTION_H
|
||||
# define EXECUTION_H
|
||||
# include "../data/data.h"
|
||||
|
||||
int ft_cmds_executor(t_data *data);
|
||||
|
||||
#endif
|
@ -1,11 +0,0 @@
|
||||
#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
|
@ -10,7 +10,8 @@
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "./redirection_private.h"
|
||||
#include "minishell.h"
|
||||
#include <unistd.h>
|
||||
|
||||
int ft_file_is_readable(const char *path)
|
||||
{
|
@ -1,6 +0,0 @@
|
||||
#ifndef FORMAT_H
|
||||
# define FORMAT_H
|
||||
|
||||
char *ft_formater(char *str);
|
||||
|
||||
#endif
|
@ -1,6 +0,0 @@
|
||||
#ifndef FORMAT_PRIVATE_H
|
||||
# define FORMAT_PRIVATE_H
|
||||
# include "../libftx/libftx.h"
|
||||
# include "../utils/utils.h"
|
||||
|
||||
#endif
|
@ -10,7 +10,10 @@
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "./redirection_private.h"
|
||||
#include "libftx/libftx.h"
|
||||
#include "minishell.h"
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int *ft_get_heredoc()
|
||||
{
|
@ -10,7 +10,7 @@
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "./redirection_private.h"
|
||||
#include "minishell.h"
|
||||
|
||||
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");
|
||||
return (-2);
|
||||
}
|
||||
fd = -1;
|
||||
fd = 0;
|
||||
i = 0;
|
||||
while (tab[i + 1] != NULL)
|
||||
{
|
BIN
libftx/extra/ft_ultoa.o
Normal file
BIN
libftx/extra/ft_ultoa.o
Normal file
Binary file not shown.
@ -14,7 +14,6 @@
|
||||
# define LIBFT_H
|
||||
# include <stdlib.h>
|
||||
# include <unistd.h>
|
||||
# include "./list.h"
|
||||
|
||||
void *ft_cancel(void **tab, size_t len);
|
||||
int ft_atoi(const char *nptr);
|
||||
@ -53,4 +52,20 @@ void ft_putstr_fd(char *s, int fd);
|
||||
void ft_putendl_fd(char *s, 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
|
||||
|
@ -1,20 +0,0 @@
|
||||
#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
|
@ -15,7 +15,6 @@
|
||||
# include <stdlib.h>
|
||||
# include <unistd.h>
|
||||
# include <stdarg.h>
|
||||
# include "./libft/libft.h"
|
||||
|
||||
char *ft_ultoa_base(unsigned long long n, char *base);
|
||||
int ft_printf(const char *format, ...);
|
||||
@ -40,41 +39,57 @@ void ft_swap(void *a, void *b);
|
||||
void ft_swap_int(int *a, int *b);
|
||||
void ft_swap_char(char *a, char *b);
|
||||
|
||||
/* void *ft_cancel(void **tab, size_t len); */
|
||||
/* int ft_atoi(const char *nptr); */
|
||||
/* void ft_bzero(void *s, size_t n); */
|
||||
/* void *ft_calloc(size_t nmemb, size_t size); */
|
||||
/* int ft_isalnum(int c); */
|
||||
/* int ft_isalpha(int c); */
|
||||
/* int ft_isascii(int c); */
|
||||
/* int ft_isdigit(int c); */
|
||||
/* int ft_isprint(int c); */
|
||||
/* void *ft_memchr(const void *s, int c, 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_memmove(void *dest, const void *src, size_t n); */
|
||||
/* void *ft_memset(void *s, int c, size_t n); */
|
||||
/* char *ft_strchr(const char *s, int c); */
|
||||
/* char *ft_strdup(const char *s); */
|
||||
/* 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_strlen(const char *s); */
|
||||
/* 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_strrchr(const char *s, int c); */
|
||||
/* int ft_tolower(int c); */
|
||||
/* int ft_toupper(int c); */
|
||||
void *ft_cancel(void **tab, size_t len);
|
||||
int ft_atoi(const char *nptr);
|
||||
void ft_bzero(void *s, size_t n);
|
||||
void *ft_calloc(size_t nmemb, size_t size);
|
||||
int ft_isalnum(int c);
|
||||
int ft_isalpha(int c);
|
||||
int ft_isascii(int c);
|
||||
int ft_isdigit(int c);
|
||||
int ft_isprint(int c);
|
||||
void *ft_memchr(const void *s, int c, 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_memmove(void *dest, const void *src, size_t n);
|
||||
void *ft_memset(void *s, int c, size_t n);
|
||||
char *ft_strchr(const char *s, int c);
|
||||
char *ft_strdup(const char *s);
|
||||
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_strlen(const char *s);
|
||||
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_strrchr(const char *s, int c);
|
||||
int ft_tolower(int c);
|
||||
int ft_toupper(int c);
|
||||
|
||||
/* char *ft_substr(char const *s, unsigned int start, size_t len); */
|
||||
/* char *ft_strjoin(char const *s1, char const *s2); */
|
||||
/* char *ft_strtrim(char const *s1, char const *set); */
|
||||
/* char **ft_split(char const *s, char c); */
|
||||
/* char *ft_itoa(int n); */
|
||||
/* char *ft_strmapi(char const *s, char (*f)(unsigned int, char)); */
|
||||
/* void ft_striteri(char *s, void (*f)(unsigned int, char*)); */
|
||||
/* void ft_putchar_fd(char c, int fd); */
|
||||
/* void ft_putstr_fd(char *s, int fd); */
|
||||
/* void ft_putendl_fd(char *s, int fd); */
|
||||
/* void ft_putnbr_fd(int n, int fd); */
|
||||
char *ft_substr(char const *s, unsigned int start, size_t len);
|
||||
char *ft_strjoin(char const *s1, char const *s2);
|
||||
char *ft_strtrim(char const *s1, char const *set);
|
||||
char **ft_split(char const *s, char c);
|
||||
char *ft_itoa(int n);
|
||||
char *ft_strmapi(char const *s, char (*f)(unsigned int, char));
|
||||
void ft_striteri(char *s, void (*f)(unsigned int, char*));
|
||||
void ft_putchar_fd(char c, int fd);
|
||||
void ft_putstr_fd(char *s, int fd);
|
||||
void ft_putendl_fd(char *s, 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
|
||||
|
68
main.c
68
main.c
@ -10,7 +10,9 @@
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libftx/libftx.h"
|
||||
#include "minishell.h"
|
||||
#include <readline/readline.h>
|
||||
|
||||
static char *ft_get_user_input()
|
||||
{
|
||||
@ -37,23 +39,64 @@ static char *ft_get_user_input()
|
||||
|
||||
static int ft_minishell(t_data *data, char *line)
|
||||
{
|
||||
t_list **cmds;
|
||||
char *line_clean;
|
||||
int infile;
|
||||
int outfile;
|
||||
|
||||
if (ft_syntax_verif(data, line))
|
||||
if (ft_syntatic_verif(data, line))
|
||||
return (0);
|
||||
line_clean = ft_formater(line);
|
||||
line_clean = ft_normalizer(line);
|
||||
if (line_clean == NULL)
|
||||
return (0);
|
||||
if (ft_cmds_parser(data, line_clean))
|
||||
outfile = ft_outfile(data, line_clean);
|
||||
if (outfile == -2)
|
||||
{
|
||||
free(line_clean);
|
||||
return (0);
|
||||
}
|
||||
if (ft_cmds_executor(data) == 1)
|
||||
infile = ft_infile(data, line_clean);
|
||||
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);
|
||||
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);
|
||||
return (0);
|
||||
}
|
||||
@ -79,8 +122,12 @@ void ft_ctrlc(int num)
|
||||
void ft_quit(int num)
|
||||
{
|
||||
(void) num;
|
||||
|
||||
ft_printf("%c%c %c%c", 0, 0, 0, 0);
|
||||
if (*ft_get_heredoc())
|
||||
{
|
||||
ft_printf("pb");
|
||||
}
|
||||
else
|
||||
ft_printf("bozoman");
|
||||
}
|
||||
|
||||
int main(int ac, char **av, char **env)
|
||||
@ -94,11 +141,7 @@ int main(int ac, char **av, char **env)
|
||||
signal(SIGQUIT, ft_quit);
|
||||
data.exit_code = 0;
|
||||
data.exit_code_str = NULL;
|
||||
ft_gen_exit_code(&data);
|
||||
data.cmds = malloc(sizeof(t_cmd *));
|
||||
if (data.cmds == NULL)
|
||||
return (1);
|
||||
*data.cmds = NULL;
|
||||
ft_gen_exit_code_var(&data);
|
||||
data.env = init_env(env);
|
||||
if (data.env == NULL)
|
||||
return (1);
|
||||
@ -112,10 +155,7 @@ int main(int ac, char **av, char **env)
|
||||
if (line == NULL)
|
||||
break ;
|
||||
}
|
||||
|
||||
free(data.exit_code_str);
|
||||
ft_lstclear(data.cmds, ft_cmddel);
|
||||
free(data.cmds);
|
||||
ft_lstclear(data.env, env_del);
|
||||
free(data.env);
|
||||
return (data.exit_code);
|
||||
|
100
minishell.h
100
minishell.h
@ -12,19 +12,95 @@
|
||||
|
||||
#ifndef MINISHELL_H
|
||||
# define MINISHELL_H
|
||||
# include "./env/env.h"
|
||||
# include "./cmd/cmd.h"
|
||||
# include "./parse/parse.h"
|
||||
# include "./syntax/syntax.h"
|
||||
# include "./execution/execution.h"
|
||||
# include "./builtins/builtins.h"
|
||||
# include "./format/format.h"
|
||||
# include "./redirection/redirection.h"
|
||||
# include "./libftx/libftx.h"
|
||||
# include "./utils/utils.h"
|
||||
# include "./data/data.h"
|
||||
# include "libftx/libftx.h"
|
||||
# include "utils/utils.h"
|
||||
# include <sys/types.h>
|
||||
# include <sys/stat.h>
|
||||
# include <fcntl.h>
|
||||
# include <sys/wait.h>
|
||||
# include <stdio.h>
|
||||
# include <unistd.h>
|
||||
# include <limits.h>
|
||||
# include <string.h>
|
||||
# include <readline/readline.h>
|
||||
# include <readline/history.h>
|
||||
# include <signal.h>
|
||||
# define DEBUG 0
|
||||
|
||||
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
|
||||
|
1
minishell_tester
Submodule
1
minishell_tester
Submodule
@ -0,0 +1 @@
|
||||
Subproject commit 1c6111b2fd281937d38ebfa7e8d87b38baef0802
|
@ -10,7 +10,7 @@
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "./redirection_private.h"
|
||||
#include "minishell.h"
|
||||
|
||||
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");
|
||||
return (-2);
|
||||
}
|
||||
fd = -1;
|
||||
fd = 1;
|
||||
i = 0;
|
||||
while (tab[i + 1] != NULL)
|
||||
{
|
@ -1,128 +0,0 @@
|
||||
#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);
|
||||
}
|
@ -1,7 +0,0 @@
|
||||
#ifndef PARSE_H
|
||||
# define PARSE_H
|
||||
# include "../data/data.h"
|
||||
|
||||
int ft_cmds_parser(t_data *data, const char *line);
|
||||
|
||||
#endif
|
@ -1,9 +0,0 @@
|
||||
#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
|
@ -1,8 +0,0 @@
|
||||
#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
|
@ -1,16 +0,0 @@
|
||||
#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
|
@ -10,7 +10,9 @@
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "./format_private.h"
|
||||
#include "libftx/libftx.h"
|
||||
#include "minishell.h"
|
||||
#include "utils/utils.h"
|
||||
|
||||
static char *ft_spacer_after(const char *str)
|
||||
{
|
||||
@ -95,7 +97,7 @@ static void ft_space_simplifier(char *str)
|
||||
}
|
||||
}
|
||||
|
||||
char *ft_formater(char *str)
|
||||
char *ft_normalizer(char *str)
|
||||
{
|
||||
char *out;
|
||||
char *temp;
|
@ -10,8 +10,9 @@
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../libftx/libftx.h"
|
||||
#include "../utils/utils.h"
|
||||
#include "libftx/libftx.h"
|
||||
#include "minishell.h"
|
||||
#include "utils/utils.h"
|
||||
|
||||
static int ft_quote_verif(const char *str)
|
||||
{
|
||||
@ -90,7 +91,7 @@ static int ft_special_char_dub(const char *str)
|
||||
return (0);
|
||||
}
|
||||
|
||||
static int ft_empty_verif(const char *str)
|
||||
int ft_empty_verif(const char *str)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
@ -100,7 +101,7 @@ static int ft_empty_verif(const char *str)
|
||||
return (str[i] == '\0');
|
||||
}
|
||||
|
||||
int ft_syntax_verif(t_data *data, const char *str)
|
||||
int ft_syntatic_verif(t_data *data, const char *str)
|
||||
{
|
||||
if (ft_quote_verif(str)
|
||||
|| ft_empty_verif(str)
|
@ -1,7 +0,0 @@
|
||||
#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
17
utils/fd.c
@ -1,17 +0,0 @@
|
||||
#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;
|
||||
}
|
@ -1,74 +0,0 @@
|
||||
#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);
|
||||
}
|
BIN
utils/ft_getstr.o
Normal file
BIN
utils/ft_getstr.o
Normal file
Binary file not shown.
BIN
utils/ft_printn.o
Normal file
BIN
utils/ft_printn.o
Normal file
Binary file not shown.
@ -14,8 +14,6 @@
|
||||
# define UTILS_H
|
||||
# include <stdlib.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);
|
||||
int ft_is_in_quote(const char *str, size_t n);
|
||||
@ -28,9 +26,5 @@ char **ft_split_quoted(const char *s, char c);
|
||||
void ft_strshift(char *str, int shift);
|
||||
char *ft_quote_remover(char *str);
|
||||
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
|
||||
|
Loading…
Reference in New Issue
Block a user