diff --git a/Makefile b/Makefile index ddfeda6..7e77e4c 100644 --- a/Makefile +++ b/Makefile @@ -6,13 +6,14 @@ # By: apommier +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2022/03/06 12:50:24 by apommier #+# #+# # -# Updated: 2022/03/08 17:30:48 by apommier ### ########.fr # +# Updated: 2022/03/09 12:44:11 by apommier ### ########.fr # # # # **************************************************************************** # NAME = minishell SRCS = main.c\ pipe.c\ + free_cmd.c\ pipex_utils.c\ set_cmd.c OBJS = ${SRCS:.c=.o} diff --git a/free_cmd.c b/free_cmd.c new file mode 100644 index 0000000..1175cfd --- /dev/null +++ b/free_cmd.c @@ -0,0 +1,49 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* free_cmd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: apommier +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/03/09 12:33:30 by apommier #+# #+# */ +/* Updated: 2022/03/09 13:58:03 by apommier ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +void free_double(char **tab) +{ + int i; + + i = 0; + if (tab) + { + while (tab[i]) + free(tab[i++]); + free(tab); + } +} + +void exit_shell(t_cmd *cmd) +{ + exit(1); +} + +void free_cmd(t_cmd *cmd) +{ + int i; + + i = 0; + printf("free_cmd\n"); + while (cmd->s_cmds[i]) + { + free_double(cmd->s_cmds[i]->args); + free(cmd->s_cmds[i]->cmd); + free(cmd->s_cmds[i]); + i++; + } + free_double(cmd->path); + free(cmd->s_cmds); + free(cmd); +} \ No newline at end of file diff --git a/main.c b/main.c index 553f5f2..8ecbe5c 100644 --- a/main.c +++ b/main.c @@ -6,7 +6,7 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/03/06 13:27:11 by apommier #+# #+# */ -/* Updated: 2022/03/09 11:37:23 by apommier ### ########.fr */ +/* Updated: 2022/03/09 14:00:05 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ @@ -27,25 +27,42 @@ void print_prompt(char **path) char *input; t_cmd *cmd; + cmd = 0; while (1) { red(); printf("~$"); normal(); input = readline(" "); + if (!ft_strcmp("exit", input)) + { + rl_clear_history(); + exit_shell(cmd); + } if (ft_strlen(input)) { - add_history(input); - cmd = set_cmd(input, path); - if (cmd) - execute(cmd, path); + add_history(input); + cmd = set_cmd(input, path); + free(input); + if (cmd) + { + execute(cmd, path); + free_cmd(cmd); + cmd = 0; + } } } } +void test(int num) +{ + printf("CRTL-C ? try exit\n"); +} + int main(int ac, char **av, char **path) { - printf("---MINISHELL START---\n"); + printf("---MINISHELL START---\n"); + signal(SIGINT, test); print_prompt(path); return (0); } diff --git a/minishell.h b/minishell.h index 6b6f984..e6a7968 100644 --- a/minishell.h +++ b/minishell.h @@ -25,6 +25,7 @@ # include # include # include +# include // Command Data Structure @@ -62,5 +63,9 @@ t_cmd *set_cmd(char *input, char **path); char **get_path(char **env); char *get_command(char **exec, char **env); void print_double(char **tab); +void free_double(char **tab); + +//free_cmd +void free_cmd(t_cmd *cmd); #endif \ No newline at end of file diff --git a/pipex_utils.c b/pipex_utils.c index e6bcbc2..e50588b 100644 --- a/pipex_utils.c +++ b/pipex_utils.c @@ -6,25 +6,12 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/03/08 16:58:40 by apommier #+# #+# */ -/* Updated: 2022/03/09 11:04:45 by apommier ### ########.fr */ +/* Updated: 2022/03/09 13:18:04 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -void free_double(char **tab) -{ - int i; - - i = 0; - if (tab) - { - while (tab[i]) - free(tab[i++]); - free(tab); - } -} - void print_double(char **tab) { int i; @@ -104,13 +91,13 @@ char *get_command(char **exec, char **path) swap = 0; if ((exec[0][0] == '/' || exec[0][0] == '.') && !access(exec[0], F_OK)) { - free_double(path); + //free_double(path); return (exec[0]); } else if (exec[0][0] == '/') { printf("BAD PATH FOR CMD\n"); - free_double(path); + //free_double(path); return(0); } swap = does_access(path, exec); diff --git a/set_cmd.c b/set_cmd.c index 969b0c4..a6f2693 100644 --- a/set_cmd.c +++ b/set_cmd.c @@ -6,7 +6,7 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/03/08 15:19:42 by apommier #+# #+# */ -/* Updated: 2022/03/08 21:05:36 by apommier ### ########.fr */ +/* Updated: 2022/03/09 13:49:49 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ @@ -70,14 +70,16 @@ t_cmd *set_cmd(char *input, char **env) cmd = malloc(sizeof(t_cmd)); if (!cmd) return (0); - cmd->s_cmds = malloc(sizeof(t_s_cmd) * double_size(cmds)); + cmd->s_cmds = malloc(sizeof(t_s_cmd) * double_size(cmds) + 1); if (!cmd->s_cmds) return (0); + cmd->s_cmds[double_size(cmds)] = 0; cmd->path = get_path(env); cmd->outfile = 0; cmd->infile = 0; cmd->nb_s_cmd = double_size(cmds); cmd = split_cmd(cmd, cmds); //split each cmd into args in s_cmd + free_double(cmds); if (cmd) { cmd->current_s_cmd = cmd->s_cmds[0];//set first s_cmd