command work and all free

This commit is contained in:
kinou-p 2022-03-09 14:01:24 +01:00
parent 14e7abeedb
commit e6bba78db6
6 changed files with 86 additions and 25 deletions

View File

@ -6,13 +6,14 @@
# By: apommier <apommier@student.42.fr> +#+ +:+ +#+ # # By: apommier <apommier@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2022/03/06 12:50:24 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 NAME = minishell
SRCS = main.c\ SRCS = main.c\
pipe.c\ pipe.c\
free_cmd.c\
pipex_utils.c\ pipex_utils.c\
set_cmd.c set_cmd.c
OBJS = ${SRCS:.c=.o} OBJS = ${SRCS:.c=.o}

49
free_cmd.c Normal file
View File

@ -0,0 +1,49 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* free_cmd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

27
main.c
View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/06 13:27:11 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; char *input;
t_cmd *cmd; t_cmd *cmd;
cmd = 0;
while (1) while (1)
{ {
red(); red();
printf("~$"); printf("~$");
normal(); normal();
input = readline(" "); input = readline(" ");
if (!ft_strcmp("exit", input))
{
rl_clear_history();
exit_shell(cmd);
}
if (ft_strlen(input)) if (ft_strlen(input))
{ {
add_history(input); add_history(input);
cmd = set_cmd(input, path); cmd = set_cmd(input, path);
if (cmd) free(input);
execute(cmd, path); 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) int main(int ac, char **av, char **path)
{ {
printf("---MINISHELL START---\n"); printf("---MINISHELL START---\n");
signal(SIGINT, test);
print_prompt(path); print_prompt(path);
return (0); return (0);
} }

View File

@ -25,6 +25,7 @@
# include <fcntl.h> # include <fcntl.h>
# include <stdio.h> # include <stdio.h>
# include <errno.h> # include <errno.h>
# include <signal.h>
// Command Data Structure // Command Data Structure
@ -62,5 +63,9 @@ t_cmd *set_cmd(char *input, char **path);
char **get_path(char **env); char **get_path(char **env);
char *get_command(char **exec, char **env); char *get_command(char **exec, char **env);
void print_double(char **tab); void print_double(char **tab);
void free_double(char **tab);
//free_cmd
void free_cmd(t_cmd *cmd);
#endif #endif

View File

@ -6,25 +6,12 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/08 16:58:40 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" #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) void print_double(char **tab)
{ {
int i; int i;
@ -104,13 +91,13 @@ char *get_command(char **exec, char **path)
swap = 0; swap = 0;
if ((exec[0][0] == '/' || exec[0][0] == '.') && !access(exec[0], F_OK)) if ((exec[0][0] == '/' || exec[0][0] == '.') && !access(exec[0], F_OK))
{ {
free_double(path); //free_double(path);
return (exec[0]); return (exec[0]);
} }
else if (exec[0][0] == '/') else if (exec[0][0] == '/')
{ {
printf("BAD PATH FOR CMD\n"); printf("BAD PATH FOR CMD\n");
free_double(path); //free_double(path);
return(0); return(0);
} }
swap = does_access(path, exec); swap = does_access(path, exec);

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/08 15:19:42 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)); cmd = malloc(sizeof(t_cmd));
if (!cmd) if (!cmd)
return (0); 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) if (!cmd->s_cmds)
return (0); return (0);
cmd->s_cmds[double_size(cmds)] = 0;
cmd->path = get_path(env); cmd->path = get_path(env);
cmd->outfile = 0; cmd->outfile = 0;
cmd->infile = 0; cmd->infile = 0;
cmd->nb_s_cmd = double_size(cmds); cmd->nb_s_cmd = double_size(cmds);
cmd = split_cmd(cmd, cmds); //split each cmd into args in s_cmd cmd = split_cmd(cmd, cmds); //split each cmd into args in s_cmd
free_double(cmds);
if (cmd) if (cmd)
{ {
cmd->current_s_cmd = cmd->s_cmds[0];//set first s_cmd cmd->current_s_cmd = cmd->s_cmds[0];//set first s_cmd