From d36d9c92f5d8aa60419daee49593b377f72ff4e7 Mon Sep 17 00:00:00 2001 From: Camille Chauvet Date: Fri, 10 Mar 2023 12:32:39 +0100 Subject: [PATCH] core: rebuild of parsing and the execution --- Makefile | 29 ++++- builtins/:wq | 8 ++ builtins/builtins.h | 14 +++ builtins/builtins_private.h | 8 ++ builtins/cd.c | 20 +-- builtins/echo.c | 2 +- builtins/env.c | 2 +- builtins/exit.c | 8 +- builtins/export.c | 14 +-- builtins/pwd.c | 2 +- builtins/unset.c | 2 +- cmd.c => cmd/cmd.c | 43 +++---- cmd/cmd.h | 19 +++ cmd/cmd_private.h | 8 ++ cmds.c | 112 ----------------- data/data.c | 25 ++++ data/data.h | 16 +++ data/data_private.h | 6 + env/env.h | 24 ++++ env.c => env/env1.c | 13 +- env2.c => env/env2.c | 3 +- env3.c => env/env3.c | 3 +- env_fill.c => env/env_fill.c | 3 +- env/env_private.h | 14 +++ execution.c | 188 ----------------------------- execution/execution.c | 115 ++++++++++++++++++ execution/execution.h | 7 ++ execution/execution_private.h | 11 ++ spacer.c => format/format.c | 6 +- format/format.h | 6 + format/format_private.h | 6 + libftx/extra/ft_ultoa.o | Bin 4688 -> 0 bytes libftx/libft/libft.h | 17 +-- libftx/libft/list.h | 20 +++ libftx/libftx.h | 87 ++++++------- main.c | 68 +++-------- minishell.h | 99 ++------------- minishell_tester | 1 - parse/cmds_parse.c | 128 ++++++++++++++++++++ parse/parse.h | 7 ++ parse/parse_private.h | 9 ++ file.c => redirection/file.c | 3 +- heredoc.c => redirection/heredoc.c | 5 +- infile.c => redirection/infile.c | 4 +- outfile.c => redirection/outfile.c | 4 +- redirection/redirection.h | 8 ++ redirection/redirection_private.h | 16 +++ syntatics.c => syntax/syntax.c | 9 +- syntax/syntax.h | 7 ++ utils/fd.c | 17 +++ utils/ft_get_executable.c | 74 ++++++++++++ utils/ft_getstr.o | Bin 4480 -> 0 bytes utils/ft_printn.o | Bin 4128 -> 0 bytes utils/utils.h | 6 + 54 files changed, 708 insertions(+), 618 deletions(-) create mode 100644 builtins/:wq create mode 100644 builtins/builtins.h create mode 100644 builtins/builtins_private.h rename cmd.c => cmd/cmd.c (62%) create mode 100644 cmd/cmd.h create mode 100644 cmd/cmd_private.h delete mode 100644 cmds.c create mode 100644 data/data.c create mode 100644 data/data.h create mode 100644 data/data_private.h create mode 100644 env/env.h rename env.c => env/env1.c (93%) rename env2.c => env/env2.c (97%) rename env3.c => env/env3.c (97%) rename env_fill.c => env/env_fill.c (98%) create mode 100644 env/env_private.h delete mode 100644 execution.c create mode 100644 execution/execution.c create mode 100644 execution/execution.h create mode 100644 execution/execution_private.h rename spacer.c => format/format.c (95%) create mode 100644 format/format.h create mode 100644 format/format_private.h delete mode 100644 libftx/extra/ft_ultoa.o create mode 100644 libftx/libft/list.h delete mode 160000 minishell_tester create mode 100644 parse/cmds_parse.c create mode 100644 parse/parse.h create mode 100644 parse/parse_private.h rename file.c => redirection/file.c (97%) rename heredoc.c => redirection/heredoc.c (94%) rename infile.c => redirection/infile.c (98%) rename outfile.c => redirection/outfile.c (98%) create mode 100644 redirection/redirection.h create mode 100644 redirection/redirection_private.h rename syntatics.c => syntax/syntax.c (93%) create mode 100644 syntax/syntax.h create mode 100644 utils/fd.c create mode 100644 utils/ft_get_executable.c delete mode 100644 utils/ft_getstr.o delete mode 100644 utils/ft_printn.o diff --git a/Makefile b/Makefile index e8aba2a..a970ad0 100644 --- a/Makefile +++ b/Makefile @@ -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 -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 +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 OBJS = ${SRCS:.c=.o} diff --git a/builtins/:wq b/builtins/:wq new file mode 100644 index 0000000..a29128c --- /dev/null +++ b/builtins/:wq @@ -0,0 +1,8 @@ +#ifndef BUILTINS_PRIVATE_H +# define BUILTINS_PRIVATE_H +# include +# include "../libftx/libftx.h" +# include "../env/env.h" +# include "../utils/utils.h" +char *get_pwd(int fd); +#endif diff --git a/builtins/builtins.h b/builtins/builtins.h new file mode 100644 index 0000000..09ba4d9 --- /dev/null +++ b/builtins/builtins.h @@ -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 diff --git a/builtins/builtins_private.h b/builtins/builtins_private.h new file mode 100644 index 0000000..a29128c --- /dev/null +++ b/builtins/builtins_private.h @@ -0,0 +1,8 @@ +#ifndef BUILTINS_PRIVATE_H +# define BUILTINS_PRIVATE_H +# include +# include "../libftx/libftx.h" +# include "../env/env.h" +# include "../utils/utils.h" +char *get_pwd(int fd); +#endif diff --git a/builtins/cd.c b/builtins/cd.c index 78791c5..92d4291 100644 --- a/builtins/cd.c +++ b/builtins/cd.c @@ -10,7 +10,7 @@ /* */ /* ************************************************************************** */ -#include "../minishell.h" +#include "./builtins_private.h" int move_folder(char **args, int fd) { @@ -44,21 +44,3 @@ int move_folder(char **args, int fd) return (1); } } - -/*int main(int argc, char *argv[]) { - char cwd[PATH_MAX]; - if (getcwd(cwd, sizeof(cwd)) != NULL) { - printf("%s\n", cwd); - } else { - perror("getcwd() error"); - return 1; - } - move_folder(argv[1], 1); - if (getcwd(cwd, sizeof(cwd)) != NULL) { - printf("%s\n", cwd); - } else { - perror("getcwd() error"); - return 1; - } - return 0; -}*/ diff --git a/builtins/echo.c b/builtins/echo.c index 641313d..ddd3d0d 100644 --- a/builtins/echo.c +++ b/builtins/echo.c @@ -10,7 +10,7 @@ /* */ /* ************************************************************************** */ -#include "../minishell.h" +#include "./builtins_private.h" int is_space(char c) { diff --git a/builtins/env.c b/builtins/env.c index 3557710..da00299 100644 --- a/builtins/env.c +++ b/builtins/env.c @@ -10,7 +10,7 @@ /* */ /* ************************************************************************** */ -#include "../minishell.h" +#include "./builtins_private.h" int print_env(t_list **env, int fd) { diff --git a/builtins/exit.c b/builtins/exit.c index c276925..a059e50 100644 --- a/builtins/exit.c +++ b/builtins/exit.c @@ -10,7 +10,7 @@ /* */ /* ************************************************************************** */ -#include "../minishell.h" +#include "./builtins_private.h" static int error(int err, char *reason, char *problem) { @@ -39,9 +39,3 @@ int ft_exit(char **args) return(error(err, "numeric argument required", args[0])); return ((ft_atoi(args[0]) % 256 + 256) % 256); } - -/*int main(int argc, char *argv[]) -{ - (void)argc; - return(ft_exit(argv + 1, 1)); -}*/ diff --git a/builtins/export.c b/builtins/export.c index c4d27ee..81db1ab 100644 --- a/builtins/export.c +++ b/builtins/export.c @@ -10,7 +10,7 @@ /* */ /* ************************************************************************** */ -#include "../minishell.h" +#include "./builtins_private.h" static int error(char *str, int fd) { @@ -65,7 +65,7 @@ int add_export(t_list **env, char *args, int fd, int *err) return (0); } -int export(t_list **env, char **args, int fd) +int ft_export(t_list **env, char **args, int fd) { int err; int i; @@ -82,13 +82,3 @@ int 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); -}*/ diff --git a/builtins/pwd.c b/builtins/pwd.c index aa7f39c..3f3fd8f 100644 --- a/builtins/pwd.c +++ b/builtins/pwd.c @@ -10,7 +10,7 @@ /* */ /* ************************************************************************** */ -#include "../minishell.h" +#include "./builtins_private.h" int pwd(int fd) { diff --git a/builtins/unset.c b/builtins/unset.c index 8504b61..d6d28aa 100644 --- a/builtins/unset.c +++ b/builtins/unset.c @@ -10,7 +10,7 @@ /* */ /* ************************************************************************** */ -#include "../minishell.h" +#include "./builtins_private.h" int error(char *str, int fd) { diff --git a/cmd.c b/cmd/cmd.c similarity index 62% rename from cmd.c rename to cmd/cmd.c index 1bdea0f..59d9639 100644 --- a/cmd.c +++ b/cmd/cmd.c @@ -10,9 +10,7 @@ /* */ /* ************************************************************************** */ -#include "libftx/libftx.h" -#include "minishell.h" -#include +#include "cmd_private.h" void ft_cmddel(void *ptr) { @@ -21,37 +19,26 @@ void ft_cmddel(void *ptr) content = (t_cmd *) ptr; if (content->args != NULL) ft_freer_tab_ultimate(1, content->args); - if (content->executable != NULL) + if (content->own_cmd == false && content->executable != NULL) free(content->executable); - if (content->fd_in > 2) - close(content->fd_in); - if (content->fd_out > 2) - close(content->fd_out); + 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]); free(content); } -int ft_cmd_filler(t_data *data, t_list *element, char **args) +void ft_cmd_waiter(void *ptr) { - t_cmd *content; - char *temp; - size_t i; + t_cmd *cmd; - if (args == NULL) - return (1); - content = (t_cmd *)element->content; - i = 0; - while (args[i] != NULL) + cmd = ptr; + if (cmd->pid != -1) { - temp = ft_env_filler(data, args[i]); - if (temp == NULL) - return (1); - free(args[i]); - args[i] = temp; - ft_quote_remover(temp); - i++; + waitpid(cmd->pid, ft_get_exit_code(), 0); } - content->args = args; - content->executable = args[0]; - content->pid = -1; - return (0); } diff --git a/cmd/cmd.h b/cmd/cmd.h new file mode 100644 index 0000000..4c34143 --- /dev/null +++ b/cmd/cmd.h @@ -0,0 +1,19 @@ +#ifndef CMD_H +# define CMD_H +# include +# 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 diff --git a/cmd/cmd_private.h b/cmd/cmd_private.h new file mode 100644 index 0000000..d27c80f --- /dev/null +++ b/cmd/cmd_private.h @@ -0,0 +1,8 @@ +#ifndef CMD_PRIVATE_H +# define CMD_PRIVATE_H +# include +# include +# include "./cmd.h" +# include "../libftx/libftx.h" +# include "../data/data.h" +#endif diff --git a/cmds.c b/cmds.c deleted file mode 100644 index 21ac594..0000000 --- a/cmds.c +++ /dev/null @@ -1,112 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* cmds.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: cchauvet 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); -} diff --git a/data/data.c b/data/data.c new file mode 100644 index 0000000..0b47650 --- /dev/null +++ b/data/data.c @@ -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); +} + diff --git a/data/data.h b/data/data.h new file mode 100644 index 0000000..8afc656 --- /dev/null +++ b/data/data.h @@ -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 diff --git a/data/data_private.h b/data/data_private.h new file mode 100644 index 0000000..e01f9c4 --- /dev/null +++ b/data/data_private.h @@ -0,0 +1,6 @@ +#ifndef DATA_PRIVATE_H +# define DATA_PRIVATE_H +# include "../libftx/libftx.h" +# include "./data.h" + +#endif diff --git a/env/env.h b/env/env.h new file mode 100644 index 0000000..c7da96e --- /dev/null +++ b/env/env.h @@ -0,0 +1,24 @@ +#ifndef ENV_H +# define ENV_H +# include "../libftx/libft/list.h" +# include "../data/data.h" + +typedef struct s_env +{ + char *key; + char *value; +} 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 diff --git a/env.c b/env/env1.c similarity index 93% rename from env.c rename to env/env1.c index 4e29c04..239df46 100644 --- a/env.c +++ b/env/env1.c @@ -10,7 +10,7 @@ /* */ /* ************************************************************************** */ -#include "minishell.h" +#include "env_private.h" void add_sort(t_list **head, t_env *var) { @@ -108,14 +108,3 @@ 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); -}*/ diff --git a/env2.c b/env/env2.c similarity index 97% rename from env2.c rename to env/env2.c index 76f0008..59ad638 100644 --- a/env2.c +++ b/env/env2.c @@ -9,8 +9,7 @@ /* */ /* ************************************************************************** */ -#include "libftx/libftx.h" -#include "minishell.h" +#include "env_private.h" int get_index(char *s, char c) { diff --git a/env3.c b/env/env3.c similarity index 97% rename from env3.c rename to env/env3.c index 9f6f56b..3635228 100644 --- a/env3.c +++ b/env/env3.c @@ -10,8 +10,7 @@ /* */ /* ************************************************************************** */ -#include "libftx/libftx.h" -#include "minishell.h" +#include "env_private.h" char *get_value(char *str) { diff --git a/env_fill.c b/env/env_fill.c similarity index 98% rename from env_fill.c rename to env/env_fill.c index 72fdda4..504573b 100644 --- a/env_fill.c +++ b/env/env_fill.c @@ -10,8 +10,7 @@ /* */ /* ************************************************************************** */ -#include "libftx/libftx.h" -#include "minishell.h" +#include "./env_private.h" int ft_gen_exit_code_var(t_data *data) { diff --git a/env/env_private.h b/env/env_private.h new file mode 100644 index 0000000..d431242 --- /dev/null +++ b/env/env_private.h @@ -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 diff --git a/execution.c b/execution.c deleted file mode 100644 index 1600952..0000000 --- a/execution.c +++ /dev/null @@ -1,188 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* execution.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: cchauvet -#include - -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); -} diff --git a/execution/execution.c b/execution/execution.c new file mode 100644 index 0000000..9cc79fd --- /dev/null +++ b/execution/execution.c @@ -0,0 +1,115 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* execution.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cchauvet 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); +} diff --git a/execution/execution.h b/execution/execution.h new file mode 100644 index 0000000..a322533 --- /dev/null +++ b/execution/execution.h @@ -0,0 +1,7 @@ +#ifndef EXECUTION_H +# define EXECUTION_H +# include "../data/data.h" + +int ft_cmds_executor(t_data *data); + +#endif diff --git a/execution/execution_private.h b/execution/execution_private.h new file mode 100644 index 0000000..148c986 --- /dev/null +++ b/execution/execution_private.h @@ -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 diff --git a/spacer.c b/format/format.c similarity index 95% rename from spacer.c rename to format/format.c index ebda46d..de82726 100644 --- a/spacer.c +++ b/format/format.c @@ -10,9 +10,7 @@ /* */ /* ************************************************************************** */ -#include "libftx/libftx.h" -#include "minishell.h" -#include "utils/utils.h" +#include "./format_private.h" 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 *temp; diff --git a/format/format.h b/format/format.h new file mode 100644 index 0000000..fe79e26 --- /dev/null +++ b/format/format.h @@ -0,0 +1,6 @@ +#ifndef FORMAT_H +# define FORMAT_H + +char *ft_formater(char *str); + +#endif diff --git a/format/format_private.h b/format/format_private.h new file mode 100644 index 0000000..3622b59 --- /dev/null +++ b/format/format_private.h @@ -0,0 +1,6 @@ +#ifndef FORMAT_PRIVATE_H +# define FORMAT_PRIVATE_H +# include "../libftx/libftx.h" +# include "../utils/utils.h" + +#endif diff --git a/libftx/extra/ft_ultoa.o b/libftx/extra/ft_ultoa.o deleted file mode 100644 index 6786878855dc45d6d962b41c0a3b72db00f17cfe..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 4688 zcmb_fTWnNC7@l*^&e`p@-CYWD5dvGUG0It5Aqv*Ih>M4n8bU-HLuvPNw%xGZ#l190 zs5ae7C`}AJ7>zN8M_!EaL5LU$1|qE*ZIlNSQP&5_){w9y8mf@g|DQ9nm!UqG_%~-~ z{_ns4GiT29_0Z1ks-h4<3h5>_PN0PB^3>)*HVqOVXznp5e>02Mg68CC#hfgZ&Eja8 zc;9FR74uSP9yRCwKxS&RTwcJ*WT*s6C0XfPTd87B6-wppkiul>%2en|@w{Tj=FeeD zWq8nh7R+a5^W!MH6q;r_#nB4LTM(KGm5;gLclM}jt|pu4;WBV{9;B6ynaMBBn}rh0 z&mUXnLjyC`bH#kPXZq+ew6A>P29u8O%$qmB2C@uIo0!{VXnO9{!oq^E_`>1BptB&D zhjHF4UJUkKNEqIA6Bu6f%g_2XHCicu1z>TWcqbZKwjNMuSw+;$`l@=14N5Mi6e`!n z^4G`mt_#}n#M)d6m9UY}KkSe2r2;g!8Gqp6!ijdA)*6E`uxMC1R&;6ZvR!mZ1n;}x zUH1&H595KY_nci>iu*c7j)tzoBH2lRf;rVw@}6_JSpA#0mH@rydaiGtwwLiz=r$%k z_mHhq%i^@1MTMtTnVSFrv_X$dg>LsA{tn zXG`-F%@3~HvGl1`gg7WsIkaObA#`H{tKVrvh|^0@)pZ}yJl)OB&4e^92gLRZk;Wi% z2j)XuSOdXrc_*EORZum7KLDD_i^wV6FQL79;(*N^wz;92jH)LF7#ECz47k0F+gh-> zU5tAgy@1SQ+&0uC&2|YiB;3h~XPRbfqZ`b0pPZh||Re3hyG2(?#|~3+a5pA4^5j zgZ>dKn@eWW{*F##gRx=#qXj1Hc&!t-jm9RD$oCgg`Ao!!kw7|;3k+q3ty~}$8;TT0 ztbAZ&XCRkJWzvIzR5F^#9}HLr^Vvw%sy`aZSp<}`2qDv-OCGWaTKEg;TyijN#r?^2 zo*>+xN3C2wYb(J#nTr<=5R|8Fn>!6zgqr87b*7LfNs_YC%rya=#NdtmFSV0p*fAjG z&~Bm}^eL-boUY^0-8F#WgM_=2Z1wn#sM}g~tz7{XChKBSLU2D-_nVfA*5aG_L z$rDxiVZ?!OSJdQFO#UOpbdX*UD2j5+(~1M80gn+#=Ogg0%(8<>PTHVGN-ccx~|@WTbT~wAnpRb-kBr`XF6BLp^$!w)9e;{#+GZ zHAB|`+8UO`by59&>aNpt%urX6*|mT-O;1pFn5ue!HbVflm#)x< zz|}Zi8>TDHQmuf3RZs7O*yGHr3PBa{PPCUHl-KgR8DhM) z$k~1dB)&zo&40ls0dK3zhrMfn;Wrcsza&lY+=lNQ(g3dqalD}jTpaF*i8)NG86sX^ zMH>o%1dbOFq~-R&aCVw<{_Z*){XEY3wS2_1D4*l-2%pRN7LJR!?7ajG`-3F#k8PH) zk76DE4aehr6#CzDJ}coraehp~uX6rP3ICn*@A6UPFZSIjNq>a%rzKqEc~QczaekJM z!oS$3KTG=WabDPodPRT!mh?p*?7K&;pB1jR3KjQePgyVaNWrl|>_~>&D{B~C_1Gb)R znoK7${K|kIdLy05TgJ1`?^&PAM`HVlfuF$sNIVW#oI&|&QM)pLYj(x4FDv!_Q^~Z& z6SIc;6WPeHg>GVzR4Nn0nYbDmkYYX(B}VSmVI2P_`WpObi})JG|5kvm*hBS~FLRa0 z`wL?AXC4ZJ_ycrOI_l-K8UHuHvs9*2c<2Icum^&A=t`W4D>;s_<(yDd;&I2ho)~1^ zE)d8V>PC4}CM2Q6lE*-IvHXAF`LE=DFn`>ia{l)MhCW4|FlBU=JG~_UK8pBa#P7ml z_P_F%0l&}nB<#ev8-!wBujiE)lnZ6u4{ZAb(lQetw|}SJDa=Lv0>*Q0vHn$BY>fe~ zi2cJeP}qz0$Madv2~AG%_`gU(i6w7??qczG@%=C2V0=tTjvoa~+qsZ|VGN!s4RL#M bCZRo&9RD(4a_>;@C=cZ41~}lBlH>maDPoK7 diff --git a/libftx/libft/libft.h b/libftx/libft/libft.h index 5cba6cf..be70f59 100644 --- a/libftx/libft/libft.h +++ b/libftx/libft/libft.h @@ -14,6 +14,7 @@ # define LIBFT_H # include # include +# include "./list.h" void *ft_cancel(void **tab, size_t len); 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_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 diff --git a/libftx/libft/list.h b/libftx/libft/list.h new file mode 100644 index 0000000..5ae8ff2 --- /dev/null +++ b/libftx/libft/list.h @@ -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 diff --git a/libftx/libftx.h b/libftx/libftx.h index 2e24db4..1585365 100644 --- a/libftx/libftx.h +++ b/libftx/libftx.h @@ -15,6 +15,7 @@ # include # include # include +# include "./libft/libft.h" char *ft_ultoa_base(unsigned long long n, char *base); 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_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); - -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 *)); +/* 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); */ #endif diff --git a/main.c b/main.c index fb6bac8..a4e8002 100644 --- a/main.c +++ b/main.c @@ -10,9 +10,7 @@ /* */ /* ************************************************************************** */ -#include "libftx/libftx.h" #include "minishell.h" -#include 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) { - t_list **cmds; char *line_clean; - int infile; - int outfile; - if (ft_syntatic_verif(data, line)) + if (ft_syntax_verif(data, line)) return (0); - line_clean = ft_normalizer(line); + line_clean = ft_formater(line); if (line_clean == NULL) return (0); - outfile = ft_outfile(data, line_clean); - if (outfile == -2) + if (ft_cmds_parser(data, line_clean)) { free(line_clean); return (0); } - infile = ft_infile(data, line_clean); - if (infile == -2) + if (ft_cmds_executor(data) == 1) { - 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); } @@ -122,12 +79,8 @@ void ft_ctrlc(int num) void ft_quit(int num) { (void) num; - if (*ft_get_heredoc()) - { - ft_printf("pb"); - } - else - ft_printf("bozoman"); + + ft_printf("%c%c %c%c", 0, 0, 0, 0); } int main(int ac, char **av, char **env) @@ -141,7 +94,11 @@ 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_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); if (data.env == NULL) return (1); @@ -155,7 +112,10 @@ 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); diff --git a/minishell.h b/minishell.h index 111b044..8cc4505 100644 --- a/minishell.h +++ b/minishell.h @@ -12,94 +12,19 @@ #ifndef MINISHELL_H # define MINISHELL_H -# include "libftx/libftx.h" -# include "utils/utils.h" -# include -# include -# include -# include -# include -# include -# include -# include +# 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 # include -# 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; -} t_env; +# include #endif diff --git a/minishell_tester b/minishell_tester deleted file mode 160000 index 1c6111b..0000000 --- a/minishell_tester +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 1c6111b2fd281937d38ebfa7e8d87b38baef0802 diff --git a/parse/cmds_parse.c b/parse/cmds_parse.c new file mode 100644 index 0000000..476ed06 --- /dev/null +++ b/parse/cmds_parse.c @@ -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); +} diff --git a/parse/parse.h b/parse/parse.h new file mode 100644 index 0000000..64d5e9b --- /dev/null +++ b/parse/parse.h @@ -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 diff --git a/parse/parse_private.h b/parse/parse_private.h new file mode 100644 index 0000000..435294a --- /dev/null +++ b/parse/parse_private.h @@ -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 diff --git a/file.c b/redirection/file.c similarity index 97% rename from file.c rename to redirection/file.c index d0bc35c..7789403 100644 --- a/file.c +++ b/redirection/file.c @@ -10,8 +10,7 @@ /* */ /* ************************************************************************** */ -#include "minishell.h" -#include +#include "./redirection_private.h" int ft_file_is_readable(const char *path) { diff --git a/heredoc.c b/redirection/heredoc.c similarity index 94% rename from heredoc.c rename to redirection/heredoc.c index 3c58229..2ea9b01 100644 --- a/heredoc.c +++ b/redirection/heredoc.c @@ -10,10 +10,7 @@ /* */ /* ************************************************************************** */ -#include "libftx/libftx.h" -#include "minishell.h" -#include -#include +#include "./redirection_private.h" int *ft_get_heredoc() { diff --git a/infile.c b/redirection/infile.c similarity index 98% rename from infile.c rename to redirection/infile.c index a0a10dc..773bd45 100644 --- a/infile.c +++ b/redirection/infile.c @@ -10,7 +10,7 @@ /* */ /* ************************************************************************** */ -#include "minishell.h" +#include "./redirection_private.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 = 0; + fd = -1; i = 0; while (tab[i + 1] != NULL) { diff --git a/outfile.c b/redirection/outfile.c similarity index 98% rename from outfile.c rename to redirection/outfile.c index 5bf8c83..adaf83f 100644 --- a/outfile.c +++ b/redirection/outfile.c @@ -10,7 +10,7 @@ /* */ /* ************************************************************************** */ -#include "minishell.h" +#include "./redirection_private.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) { diff --git a/redirection/redirection.h b/redirection/redirection.h new file mode 100644 index 0000000..baf4b34 --- /dev/null +++ b/redirection/redirection.h @@ -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 diff --git a/redirection/redirection_private.h b/redirection/redirection_private.h new file mode 100644 index 0000000..e22e87f --- /dev/null +++ b/redirection/redirection_private.h @@ -0,0 +1,16 @@ +#ifndef REDIRECTIONS_PRIVATE +# define REDIRECTIONS_PRIVATE +# include +# include +# include +# include +# 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 diff --git a/syntatics.c b/syntax/syntax.c similarity index 93% rename from syntatics.c rename to syntax/syntax.c index e12fbfd..07afe9f 100644 --- a/syntatics.c +++ b/syntax/syntax.c @@ -10,9 +10,8 @@ /* */ /* ************************************************************************** */ -#include "libftx/libftx.h" -#include "minishell.h" -#include "utils/utils.h" +#include "../libftx/libftx.h" +#include "../utils/utils.h" static int ft_quote_verif(const char *str) { @@ -91,7 +90,7 @@ static int ft_special_char_dub(const char *str) return (0); } -int ft_empty_verif(const char *str) +static int ft_empty_verif(const char *str) { size_t i; @@ -101,7 +100,7 @@ int ft_empty_verif(const char *str) 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) || ft_empty_verif(str) diff --git a/syntax/syntax.h b/syntax/syntax.h new file mode 100644 index 0000000..9a87c39 --- /dev/null +++ b/syntax/syntax.h @@ -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 diff --git a/utils/fd.c b/utils/fd.c new file mode 100644 index 0000000..55d23b6 --- /dev/null +++ b/utils/fd.c @@ -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; +} diff --git a/utils/ft_get_executable.c b/utils/ft_get_executable.c new file mode 100644 index 0000000..4c99a6d --- /dev/null +++ b/utils/ft_get_executable.c @@ -0,0 +1,74 @@ +#include "./utils.h" +#include + +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); +} diff --git a/utils/ft_getstr.o b/utils/ft_getstr.o deleted file mode 100644 index b76c43ae0975fad5605fc5e98ea8048a1745eebe..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 4480 zcmbtXTWB0r7(TO;$!@aA=GNMT*gBTlT6AZV8q%cKG+~>mrZ#GWl?v-#W_Jg7XX9LA zT4=3A#ibUEf+&dKgMy%-FGUa~BDT~Q>w~c%f-i!S3K1VHUa0?n=A6y$$@E1J$(;ZD z{@XeKoXhMp$*IF3Ng}*S1xMCGLl?zWBS6GRpR}CM#&psG;<9MZs;|G?ndglC2yG;+=D|p3dS?5 zy7ZM^y`)#aqocX<#Wc*hiB2m*XJ0p4%96`|N1q{lHhr^c6goK)rl?6VEtkkm6v+_< zN#02jibB3lYW5K|Q6gv2(C3<|><6T_p*unyZ-HKk%)`@hj^WH|H(F0%s z-pcHtr4NkONBef#>L6pW&Ae5ys04)*cjJNl0FV9v4L*T(xKh|#LR z+u(^qy@W*TBOuWRM1uvn7pH?0@heXyeDE692qv^Z6=m`sAW8Zw)(%g)uYi?2*DN_C z;W>7~w6Y~HXEcDs{K#->cqm>nEpI+vu)IXZbeVn0%p?lg?0S>YS}2ltQw76?m#M*P zA;hyx$IWR)96j)rSvQrf*ai?XA)v__d603rD-vn(rwCx95{Zl};RU62K~W-)6_jSP zP*fsQ38n2(P^rph5^9o8D3LiO>~qV?7N6?HZdqxaQl!Y0Pza`8s#pb;K5$<+#ft5! zp5>SY%gCunHGrqQifa%UDEtw}JZ+?0lCbhlqF5;#P9mEvroB1CO$_Zyluejj(I}M& zjouJuEolP{K@Hnp%LvhILLZeJEyE{ z5yDhFC+=BK!_eYxI#V(nP(tMFlkr&_3YB#$b{yU|mY6HDrL{0KY$#|CQ`9q2idK3+w-8G;M3~7TqeIC~{u8vOl$5RUCflFx#IaoooZD750`e7o? zcS`+j;mFId_HG9r|FO}Hl8kk}5SnOj4t)j|J|FZ1SIGI>%^;l|cq8Fh_-GovWIFmr z4v|b}6U5WgcAY{NHa<6yNh=o{dYP=dHmF`EaKHsLnREoOI8Lnm`X-b1btE#~b@kzg z6CAdIQSdkqioWj_{MsIXv!%A?NosQH*!ZET)Um^dA4wih9X~Wal}w@DbW*03^7k`9 zSfy6Znwl0tG{ph?Q@@|d>6=^rqBxs>p6cX!w(7k2MnJOcm_#I z;9q4pFYtVYl?DE5hFyWb$ndiQ|2D%H1m0u#6@mYi;dcc7ABH~?_;(rpRN!A?_=>;} zG5oE-f64HVtQX&74-(LL{r~VG*(&f$j6Wdo{C)GahjH*McZrruyJ@0j9EWI>%^4Z506Q&T0UedonT&1BHK@~e z+A0{0F7SzAa2Lo!v}~nZHsC(iEVx-TIO^eMft!Y5_!iON&Y3lA_Y{k56jOOST{hT+ z{%5CxKh}!ZnTH!=0zc0<&t4OUq#qkg{}&)d0lxqM diff --git a/utils/ft_printn.o b/utils/ft_printn.o deleted file mode 100644 index e48dceb01fe5070dcd79e26bd5c37ac035f5e46d..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 4128 zcmbtXU2GIp6uz^w({8)4+ZGTSh-?rM5_h)6rlpjhrOm=>8bPIox5@16-Q9`1GtK;9 zO9Em*lZKdp5B?+upL`(E2YoOm{xz6jc+&?-Onl&lghU=Rz7RFRbMD-`oxL4j^oGto z=R3dm+%spE*DCYJQ;I^wQpgdq#tD>=yMt?VnUQ6(g={8&dZqHPs{D}uoaDb*UAgrwdcTk2>ZQsofR3;gg)doh#9!88#A)Wql>oja?rk`Oy0Tm;xkI|7srkmQC%3lY$mRW*l&zZ3WfG zh?+Z?oyksTpWOt<*iEw8BcK7*;Uwg&6h|f!+2=DfGRP?O5%wPJ5Wr(t$~1a-!I_op7nxbiz{24!C~Xu9cdWwO(bq z>xwi2W6^`&adZn`h|sb9psu$t`z8(C#lWy!j{=|)0+Kpy0FAAxDP$lzBIce~v~=dE zmbs*D9n;i1TKb-rSI=;0=H zsApTinx54<3>R3OoG28Vg<`|>mmRAGM|MNM7&@=oPQ4hIwKnxZ=m)0tN^#M118N1X zSA;hRODP0a+w@yPY`YE!tVWE->ViK)PRz|6EbLvVg-#GgnH9%F#*JUtKcIFF0Hiy>~kt=0TSx#)E`0eG$z3#BWiL;pl%C=MPEvY0f_<;o>G=knkIvKO^Cs?S25n zc`tK8L(;#+`HqDD%=tjVKjZxC68<*l-;!_4!93${ujpY}|BHX|Q{XuENW4#o?k{&>(!w5i(cTi2N86?z z(4~Orbu%!DUh{pTGqg@?Vbidkh6^%Ao3)xp&-RE-&vcr!*91P-RG8o|A>DF29SWa7 z-GTA0!>EPv4&$El@Lkto@Gnv?SmwTI%V>CJhw=mc&#eIVSVK`~;jYXHF;4`}uU)VN z?-jobA+uWlkmKVIF7)|d?I*!j_K8tN{3j%# z#FBB)O*;QYe*Q9VHatJ}uYCSlz+|5Qk((U+1ew^9br(P;W2o-(xMML}BxT)GfF zio+3>u*du{cR7C>Fxe+S # 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); @@ -26,5 +28,9 @@ 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