🔨」 fix: Fixed some leak and started refactor main

This commit is contained in:
2024-07-04 17:15:08 +02:00
parent 8769b5d25f
commit 4452a06e5d
4 changed files with 41 additions and 45 deletions

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/18 20:14:15 by adjoly #+# #+# */ /* Created: 2024/05/18 20:14:15 by adjoly #+# #+# */
/* Updated: 2024/06/30 17:18:32 by adjoly ### ########.fr */ /* Updated: 2024/07/04 16:50:24 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View File

@ -6,7 +6,7 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */ /* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/24 11:18:04 by adjoly #+# #+# */ /* Created: 2024/04/24 11:18:04 by adjoly #+# #+# */
/* Updated: 2024/07/04 12:07:26 by mmoussou ### ########.fr */ /* Updated: 2024/07/04 17:02:14 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -37,8 +37,10 @@ void free_token(void *token_v)
t_token *token; t_token *token;
token = token_v; token = token_v;
print_token(token);
free(token->argv); free(token->argv);
ft_lstclear(&(token->redirection), free_redir); ft_lstclear(&(token->redirection), free_redir);
free(token);
} }
/*void print_cmd(t_cmd cmd) /*void print_cmd(t_cmd cmd)
@ -63,7 +65,7 @@ void print_pipe(t_list *pipe)
} }
} }
void sigggg(int code) void sig_c(int code)
{ {
(void)code; (void)code;
ft_putchar_fd('\n', STDOUT_FILENO); ft_putchar_fd('\n', STDOUT_FILENO);
@ -72,65 +74,54 @@ void sigggg(int code)
rl_redisplay(); rl_redisplay();
} }
void siggg_backslash(int code) void free_cmd(void *content)
{ {
(void)code; t_cmd *cmd;
rl_replace_line("", 0);
rl_redisplay();
}
void siggg_d(int code) cmd = (t_cmd *)content;
{ free(cmd->cmd);
(void)code; ft_free("a", &(cmd->argv));
exit(EXIT_SUCCESS); free(cmd);
} }
int main(int ac, char **av, char **env) int main(int ac, char **av, char **env)
{ {
char *test; char *rl;
char *prompt; char *prompt;
char **lll;
t_list *piped;
t_env env_l; t_env env_l;
t_list *cmd_list; t_list *cmd_list;
t_list *piped;
(void)ac; (void)ac;
(void)av;
get_program_name(av[0]); get_program_name(av[0]);
piped = NULL;
if (env_init(env, &env_l)) if (env_init(env, &env_l))
return (EXIT_FAILURE); return (EXIT_FAILURE);
//sigemptyset(&(sigset_t){ SIGQUIT }); signal(SIGINT, &sig_c);
signal(SIGINT, &sigggg);
signal(SIGQUIT, &siggg_backslash);
//signal(SIGSEGV, &siggg_d);
while (1) while (1)
{ {
prompt = get_prompt(env_l); prompt = get_prompt(env_l);
test = readline(prompt); rl = readline(prompt);
if (!test)
exit(727);
free(prompt); free(prompt);
add_history(test); if (!rl)
if (check_syntax(test)) exit(727);
if (!*rl)
continue ; continue ;
lll = ft_split(test, ' '); if (check_syntax(rl))
if (!*lll)
continue ; continue ;
if (check_quote(test)) if (check_quote(rl))
continue ; continue ;
if (check_pipe(test)) if (check_pipe(rl))
continue ; continue ;
piped = tokenizer(test); piped = tokenizer(rl);
if (check_argv(((t_token *)(piped->content))->redirection)) if (check_argv(piped))
continue ; continue ;
add_history(rl);
cmd_list = get_cmd_list(piped); cmd_list = get_cmd_list(piped);
ft_lstclear(&piped, &free_token);
format_quotes(cmd_list); format_quotes(cmd_list);
exec_split_cmd(cmd_list, &env_l); exec_split_cmd(cmd_list, &env_l);
free(test); ft_lstclear(&cmd_list, &free_cmd);
ft_lstclear(&piped, free_token); free(rl);
ft_free("a", &lll);
} }
ft_free("a", &lll);
return (0); return (0);
} }

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/30 13:25:42 by adjoly #+# #+# */ /* Created: 2024/04/30 13:25:42 by adjoly #+# #+# */
/* Updated: 2024/06/30 15:41:16 by adjoly ### ########.fr */ /* Updated: 2024/07/04 17:14:30 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -21,7 +21,7 @@ char *get_prompt(t_env env)
prompt = env_get_value("USER", &env); prompt = env_get_value("USER", &env);
if (!prompt) if (!prompt)
prompt = ft_strdup("nixos"); prompt = ft_strdup("nixos");
prompt = ft_strjoin(prompt, "@"); prompt = ft_strjoin_free_s1(prompt, "@");
if (!prompt) if (!prompt)
return (NULL); return (NULL);
prompt = ft_strjoin_free(prompt, get_hostname()); prompt = ft_strjoin_free(prompt, get_hostname());

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/02 14:42:00 by adjoly #+# #+# */ /* Created: 2024/05/02 14:42:00 by adjoly #+# #+# */
/* Updated: 2024/06/30 15:41:46 by adjoly ### ########.fr */ /* Updated: 2024/07/04 17:14:14 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -18,20 +18,25 @@ char *get_pwd(t_env env)
{ {
char *pwd; char *pwd;
char *home; char *home;
char *tmp;
pwd = env_get_value("PWD", &env); pwd = env_get_value("PWD", &env);
if (!pwd) if (!pwd)
return (NULL); return (NULL);
home = env_get_value("HOME", &env); home = env_get_value("HOME", &env);
if (!pwd) if (!home)
return (NULL); return (NULL);
if (!ft_strncmp(pwd, home, ft_strlen(home) - 1)) if (!ft_strncmp(pwd, home, ft_strlen(home) - 1))
{ {
pwd += ft_strlen(home); tmp = pwd;
pwd = ft_strjoin("~", pwd); tmp += ft_strlen(home);
if (!pwd) free(home);
tmp = ft_strjoin("~", tmp);
free(pwd);
if (!tmp)
return (NULL); return (NULL);
return (pwd); return (tmp);
} }
return (ft_strdup(pwd)); free(home);
return (pwd);
} }