✏️」 norm: Normed everything and leak erased from the surface of the earth and what the fuck am i doing at this hour coding i shoud be sleeping 1:30am wtf god damn please give me a bed and fuck you yosyo 🖕

This commit is contained in:
2024-07-10 01:22:28 +02:00
parent b1f77b386a
commit 54e01e9bc3
19 changed files with 359 additions and 220 deletions

View File

@ -6,7 +6,7 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */ /* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/29 13:20:22 by mmoussou #+# #+# */ /* Created: 2024/04/29 13:20:22 by mmoussou #+# #+# */
/* Updated: 2024/07/03 11:04:53 by mmoussou ### ########.fr */ /* Updated: 2024/07/10 01:21:10 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -14,11 +14,28 @@
# define EXECUTION_H # define EXECUTION_H
# include "env.h" # include "env.h"
# include "libft.h"
# include "parsing.h"
# include "tokenizer.h"
int format_quotes(t_list *list_cmd); typedef struct s_exec
{
char **env_array;
int status;
int i;
int pipe_fd[2];
} t_exec;
int format_quotes(t_list *list_cmd, t_env *env);
char *get_path(char *path, char *cmd); char *get_path(char *path, char *cmd);
int exec_split_cmd(t_list *list_cmd, t_env *env); int exec_split_cmd(t_list *list_cmd, t_env *env);
int switch_cmd_path(t_cmd *cmd, t_env *env);
int is_in_builtins(char *cmd);
char *get_cmd_local_path(char *cmd, t_env *env);
void print_return_value(int return_code);
void __wait(int i);
void __close(void *content);
/** /**
* @brief spawn a heredoc * @brief spawn a heredoc
@ -27,6 +44,8 @@ int exec_split_cmd(t_list *list_cmd, t_env *env);
* *
* @return (int) fd of a file containing the user's input, or -1 on error * @return (int) fd of a file containing the user's input, or -1 on error
*/ */
int ft_heredoc(char *delimiter); int ft_heredoc(char *delimiter, t_cmd *cmd);
int __open_fd_here(char *path, int mode);
void ft_lstclear_till_nxt(t_list **lst, void (*del)(void *));
#endif #endif

View File

@ -41,5 +41,6 @@ void free_cmd(void *content);
t_env **get_env(t_env **env); t_env **get_env(t_env **env);
t_list **get_list(t_list **list); t_list **get_list(t_list **list);
t_list **get_list2(t_list **list);
#endif #endif

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/25 12:20:26 by adjoly #+# #+# */ /* Created: 2024/04/25 12:20:26 by adjoly #+# #+# */
/* Updated: 2024/07/07 14:37:59 by adjoly ### ########.fr */ /* Updated: 2024/07/09 23:52:39 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -33,8 +33,8 @@ typedef enum s_quote
DOUBLE = 34 DOUBLE = 34
} t_quote; } t_quote;
t_cmd *get_redir_fd(void *content, t_env *env); t_cmd *get_redir_fd(void *content);
t_list *get_cmd_list(t_list *list, t_env *env); t_list *get_cmd_list(t_list *list);
void open_redir(t_redirection *redir, t_cmd *cmd, t_redir_sign sign[2]); void open_redir(t_redirection *redir, t_cmd *cmd, t_redir_sign sign[2]);
/** /**
@ -135,5 +135,6 @@ void print_quote_type(t_quote type);
void print_redir_sign(t_redir_sign redir_sign); void print_redir_sign(t_redir_sign redir_sign);
void print_token(t_token *token); void print_token(t_token *token);
void print_redir(t_redirection *redir); void print_redir(t_redirection *redir);
void print_cmd(t_cmd *cmd);
#endif #endif

View File

@ -6,29 +6,14 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */ /* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/01 14:55:06 by mmoussou #+# #+# */ /* Created: 2024/06/01 14:55:06 by mmoussou #+# #+# */
/* Updated: 2024/07/06 18:12:25 by mmoussou ### ########.fr */ /* Updated: 2024/07/10 01:18:17 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
#include "builtins.h" #include "builtins.h"
#include "error_msg.h" #include "error_msg.h"
#include "execution.h"
int is_in_builtins(char *cmd)
{
int i;
static const char *builtins[] = {"exit", "cd", "unset", "export", "echo",
"pwd", "env", NULL};
i = 0;
while (builtins[i])
{
if (!ft_strcmp(cmd, builtins[i]))
return (i + 1);
i++;
}
return (0);
}
void exec_cmd(char *cmd, char **argv, char **env, t_env *env_t) void exec_cmd(char *cmd, char **argv, char **env, t_env *env_t)
{ {
@ -51,157 +36,111 @@ void exec_cmd(char *cmd, char **argv, char **env, t_env *env_t)
ft_env(env_t); ft_env(env_t);
if (i == 1) if (i == 1)
exit(ft_atoi(argv[1])); exit(ft_atoi(argv[1]));
if (i > 4)
exit(0);
} }
else else
execve(cmd, argv, env); execve(cmd, argv, env);
} }
char *get_cmd_local_path(char *cmd, t_env *env) void __fork_single_cmd(t_cmd *cmd, char **env, t_env *env_t, t_exec exec)
{ {
char *path; exec.status = dup2(cmd->infile, STDIN_FILENO);
if (cmd->infile != STDIN_FILENO)
path = env_get_value("PWD", env); close(cmd->infile);
if (!path) if (exec.status == -1)
return (NULL); exit(-1);
path = ft_strjoin_free_s1(path, "/"); exec.status = dup2(cmd->outfile, STDOUT_FILENO);
if (!path) if (cmd->outfile != STDOUT_FILENO)
return (NULL); close(cmd->outfile);
path = ft_strjoin_free_s1(path, cmd); if (exec.pipe_fd[0] != -1)
return (path); close(exec.pipe_fd[0]);
} if (exec.pipe_fd[0] != -1)
close(exec.pipe_fd[1]);
int switch_cmd_path(t_cmd *cmd, t_env *env) if (exec.status != -1)
{ exec_cmd(cmd->cmd, cmd->argv, env, env_t);
if (is_in_builtins(cmd->cmd)) exit(-1);
return (0);
if (cmd->cmd[0] == '.' && cmd->cmd[1] == '/')
cmd->cmd = get_cmd_local_path(cmd->cmd, env);
else if (cmd->cmd[0] != '/')
cmd->cmd = get_path(env_get_value("PATH", env), cmd->cmd);
if (!(cmd->cmd))
return (-1);
return (0);
} }
int exec_single_cmd(t_cmd *cmd, char **env, t_env *env_t, int pipe_fd[2]) int exec_single_cmd(t_cmd *cmd, char **env, t_env *env_t, int pipe_fd[2])
{ {
t_exec exec;
int fork_pid; int fork_pid;
int status;
char *input; char *input;
input = ft_strdup(cmd->cmd); input = ft_strdup(cmd->cmd);
status = switch_cmd_path(cmd, env_t); exec.pipe_fd[0] = pipe_fd[0];
if (status == -1 || !input || (access(cmd->cmd, X_OK) && !is_in_builtins(cmd->cmd))) exec.pipe_fd[1] = pipe_fd[1];
exec.status = switch_cmd_path(cmd, env_t);
if (exec.status == -1 || !input || (access(cmd->cmd, X_OK) \
&& !is_in_builtins(cmd->cmd)))
{ {
printf("minishell : command not found: %s\n", input); printf("minishell : command not found: %s\n", input);
free(input);
return (-1); return (-1);
} }
if (is_in_builtins(cmd->cmd) && is_in_builtins(cmd->cmd) < 5) free(input);
if (is_in_builtins(cmd->cmd) > 0)
{ {
exec_cmd(cmd->cmd, cmd->argv, env, env_t); exec_cmd(cmd->cmd, cmd->argv, env, env_t);
return (0); return (0);
} }
fork_pid = fork(); fork_pid = fork();
if (!fork_pid) if (!fork_pid)
{ __fork_single_cmd(cmd, env, env_t, exec);
status = dup2(cmd->infile, STDIN_FILENO);
if (cmd->infile != STDIN_FILENO)
close(cmd->infile);
if (status == -1)
exit(-1);
status = dup2(cmd->outfile, STDOUT_FILENO);
if (cmd->outfile != STDOUT_FILENO)
close(cmd->outfile);
if (pipe_fd[0] != -1)
close(pipe_fd[0]);
if (pipe_fd[0] != -1)
close(pipe_fd[1]);
if (status != -1)
exec_cmd(cmd->cmd, cmd->argv, env, env_t);
exit(-1);
}
return (fork_pid); return (fork_pid);
} }
void print_return_value(int return_code) t_exec exec_pipe(t_exec exec, t_list *list_cmd, t_env *env)
{ {
int code;
static const char *sigmsg[] = {0, "Hangup", 0, "Quit", "Illegal \
instruction", "Trace/breakpoint trap", "Aborted", "Bus error",
"Floating point exception", "Killed", "User defined signal 1",
"Segmentation fault", "User defined signal 2", 0,
"Alarm clock", "Terminated", "Stack fault", 0, 0, "Stopped", "Stopped",
"Stopped", "Stopped", 0, "CPU time limit exceeded",
"File size limit exceeded", "Virtual time expired",
"Profiling timer expired", "I/O possible", "Power failure",
"Bad system call"};
if (!WIFEXITED(return_code))
{
if (WIFSIGNALED(return_code))
{
code = WTERMSIG(return_code);
if (!sigmsg[code])
return ;
if (WCOREDUMP(return_code))
printf("minishell : %s %s\n", sigmsg[code], ERROR_COREDUMP);
else
printf("minishell : %s\n", sigmsg[code]);
}
}
}
int exec_split_cmd(t_list *list_cmd, t_env *env)
{
char **env_array;
int status;
int return_code;
int i;
int pipe_fd[2];
env_array = env_get(env);
pipe_fd[0] = -1;
pipe_fd[1] = -1;
if (!env_array)
return (-1);
i = 1;
while (list_cmd->next) while (list_cmd->next)
{ {
status = pipe(pipe_fd); exec.status = pipe(exec.pipe_fd);
if (status) if (exec.status)
return (-1); return ((t_exec){exec.env_array, -1, exec.i, \
{exec.pipe_fd[0], exec.pipe_fd[1]}});
if (((t_cmd *)(list_cmd->content))->outfile == STDOUT_FILENO) if (((t_cmd *)(list_cmd->content))->outfile == STDOUT_FILENO)
((t_cmd *)(list_cmd->content))->outfile = pipe_fd[1]; ((t_cmd *)(list_cmd->content))->outfile = exec.pipe_fd[1];
if (((t_cmd *)(list_cmd->next->content))->infile == STDIN_FILENO) if (((t_cmd *)(list_cmd->next->content))->infile == STDIN_FILENO)
((t_cmd *)(list_cmd->next->content))->infile = pipe_fd[0]; ((t_cmd *)(list_cmd->next->content))->infile = exec.pipe_fd[0];
status = exec_single_cmd(list_cmd->content, env_array, env, pipe_fd); exec.status = exec_single_cmd(list_cmd->content, exec.env_array, \
if (((t_cmd *)(list_cmd->content))->outfile != STDOUT_FILENO) env, exec.pipe_fd);
close(((t_cmd *)(list_cmd->content))->outfile); __close(list_cmd->content);
if (((t_cmd *)(list_cmd->content))->infile != STDIN_FILENO) if (exec.status != -1)
close(((t_cmd *)(list_cmd->content))->infile); exec.i++;
if (status != -1)
i++;
list_cmd = list_cmd->next; list_cmd = list_cmd->next;
} }
status = exec_single_cmd(list_cmd->content, env_array, env, pipe_fd); exec.status = exec_single_cmd(list_cmd->content, exec.env_array, \
env, exec.pipe_fd);
if (((t_cmd *)(list_cmd->content))->outfile != STDOUT_FILENO) if (((t_cmd *)(list_cmd->content))->outfile != STDOUT_FILENO)
close(((t_cmd *)(list_cmd->content))->outfile); close(((t_cmd *)(list_cmd->content))->outfile);
if (((t_cmd *)(list_cmd->content))->infile != STDIN_FILENO) if (((t_cmd *)(list_cmd->content))->infile != STDIN_FILENO)
close(((t_cmd *)(list_cmd->content))->infile); close(((t_cmd *)(list_cmd->content))->infile);
ft_free("a", &env_array); return (exec);
if (status == -1) }
i--;
if (i < 1) int exec_split_cmd(t_list *list_cmd, t_env *env)
{
t_exec exec;
int return_code;
exec.env_array = env_get(env);
if (!exec.env_array)
return (-1);
return_code = 0;
exec.pipe_fd[0] = -1;
exec.pipe_fd[1] = -1;
exec.i = 1;
exec = exec_pipe(exec, list_cmd, env);
if (exec.env_array)
ft_free("a", &exec.env_array);
if (exec.status == -1)
exec.i--;
if (exec.i < 1)
return (0); return (0);
if (status != 0) if (exec.status != 0)
waitpid(status, &return_code, 0); waitpid(exec.status, &return_code, 0);
while (i - 1) if (!return_code)
{ return_code = 0;
waitpid(-1, NULL, 0); __wait(exec.i);
i--;
}
print_return_value(return_code); print_return_value(return_code);
return (0); return (0);
} }

View File

@ -6,7 +6,7 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */ /* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/03 10:50:52 by mmoussou #+# #+# */ /* Created: 2024/07/03 10:50:52 by mmoussou #+# #+# */
/* Updated: 2024/07/06 17:47:53 by mmoussou ### ########.fr */ /* Updated: 2024/07/09 21:21:16 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -14,44 +14,12 @@
char *format_quotes_string(char *cmd) char *format_quotes_string(char *cmd)
{ {
uint i; if (*cmd == DOUBLE || *cmd == SINGLE)
uint i_offset; ft_strlcpy(cmd, cmd + 1, ft_strlen(cmd) - 1);
i = 0;
i_offset = 0;
while (cmd[i])
{
if (cmd[i] == SINGLE)
{
i_offset++;
i++;
while (cmd[i] && cmd[i] != SINGLE)
{
cmd[i - i_offset] = cmd[i];
i++;
}
i_offset++;
}
else if (cmd[i] == DOUBLE)
{
i_offset++;
i++;
while (cmd[i] && cmd[i] != DOUBLE)
{
cmd[i - i_offset] = cmd[i];
i++;
}
i_offset++;
}
else
cmd[i - i_offset] = cmd[i];
i++;
}
cmd[i - i_offset] = 0;
return (cmd); return (cmd);
} }
int format_quotes_cmd(t_cmd *cmd) int format_quotes_cmd(t_cmd *cmd, t_env *env)
{ {
uint i; uint i;
@ -59,19 +27,20 @@ int format_quotes_cmd(t_cmd *cmd)
i = 0; i = 0;
while (cmd->argv[i]) while (cmd->argv[i])
{ {
format_quotes_string(cmd->argv[i]); cmd->argv[i] = format_quotes_string(cmd->argv[i]);
cmd->argv[i] = env_var_replace(cmd->argv[i], env);
i++; i++;
} }
return (0); return (0);
} }
int format_quotes(t_list *list_cmd) int format_quotes(t_list *list_cmd, t_env *env)
{ {
if (!list_cmd) if (!list_cmd)
return (0); return (0);
while (list_cmd) while (list_cmd)
{ {
format_quotes_cmd(list_cmd->content); format_quotes_cmd(list_cmd->content, env);
list_cmd = list_cmd->next; list_cmd = list_cmd->next;
} }
return (0); return (0);

View File

@ -6,7 +6,7 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */ /* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/20 09:19:39 by mmoussou #+# #+# */ /* Created: 2024/05/20 09:19:39 by mmoussou #+# #+# */
/* Updated: 2024/07/09 18:02:28 by adjoly ### ########.fr */ /* Updated: 2024/07/10 01:14:41 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -40,10 +40,7 @@ int fd_manager(int mode)
ft_strlcat(path, "/tmp/.minishell-heredoc-", 24); ft_strlcat(path, "/tmp/.minishell-heredoc-", 24);
ft_strlcat(path, index_itoa, ft_strlen(index_itoa)); ft_strlcat(path, index_itoa, ft_strlen(index_itoa));
free(index_itoa); free(index_itoa);
if (mode > 0) return (__open_fd_here(path, mode));
return (open(path, O_RDONLY));
else
return (open(path, O_WRONLY | O_TRUNC | O_CREAT, 0644));
} }
static int get_input(char *delimiter, int fd) static int get_input(char *delimiter, int fd)
@ -52,16 +49,16 @@ static int get_input(char *delimiter, int fd)
int status; int status;
line = readline("heredoc> "); line = readline("heredoc> ");
while (ft_strcmp(line, delimiter)) while (line && ft_strcmp(line, delimiter))
{ {
status = write(fd, line, ft_strlen(line)); status = write(fd, line, ft_strlen(line));
if (status == -1) if (status == -1)
fd_manager(fd); close(fd_manager(fd));
if (status == -1) if (status == -1)
break ; break ;
status = write(fd, "\n", 1); status = write(fd, "\n", 1);
if (status == -1) if (status == -1)
fd_manager(fd); close(fd_manager(fd));
if (status == -1) if (status == -1)
break ; break ;
free(line); free(line);
@ -70,11 +67,21 @@ static int get_input(char *delimiter, int fd)
free(line); free(line);
status = write(fd, "\0", 1); status = write(fd, "\0", 1);
if (status == -1) if (status == -1)
fd_manager(fd); close(fd_manager(fd));
return (-(status == -1)); return (-(status == -1));
} }
int __heredoc(char *delimiter) void __forked(char *delimiter, int fd, t_cmd *cmd)
{
get_input(delimiter, fd);
free(cmd);
ft_envclear(get_env(NULL), free);
ft_lstclear_till_nxt(get_list2(NULL), &free_cmd);
ft_lstclear(get_list(NULL), &free_token);
close(fd);
}
int __heredoc(char *delimiter, t_cmd *cmd)
{ {
int fork_pid; int fork_pid;
int fd; int fd;
@ -82,33 +89,31 @@ int __heredoc(char *delimiter)
fd = fd_manager(0); fd = fd_manager(0);
if (fd == -1) if (fd == -1)
{ {
fd_manager(-1); close(fd_manager(-1));
return (-1); return (-1);
} }
fork_pid = fork(); fork_pid = fork();
if (fork_pid == -1) if (fork_pid == -1)
{ {
fd_manager(-1); close(fd_manager(-1));
close(fd); close(fd);
return (-1); return (-1);
} }
if (!fork_pid) if (!fork_pid)
{ {
get_input(delimiter, fd); __forked(delimiter, fd, cmd);
ft_envclear(get_env(NULL), free);
ft_lstclear(get_list(NULL), &free_token);
exit(0); exit(0);
} }
else else
waitpid(fork_pid, NULL, 0); waitpid(fork_pid, NULL, 0);
close(fd);
return (fd_manager(1)); return (fd_manager(1));
} }
int ft_heredoc(char *delimiter) int ft_heredoc(char *delimiter, t_cmd *cmd)
{ {
int fd; int fd;
fd = __heredoc(delimiter); fd = __heredoc(delimiter, cmd);
return (fd); return (fd);
} }

48
src/exec/heredoc_utils.c Normal file
View File

@ -0,0 +1,48 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* heredoc_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/10 01:14:10 by adjoly #+# #+# */
/* Updated: 2024/07/10 01:16:48 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include <stdlib.h>
#include <fcntl.h>
int __open_fd_here(char *path, int mode)
{
int fd;
if (mode > 0)
fd = open(path, O_RDONLY);
else
fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, 0644);
free(path);
return (fd);
}
void ft_lstclear_till_nxt(t_list **lst, void (*del)(void *))
{
t_list *tmp;
if (!(*lst)->next)
{
free(*lst);
return ;
}
if (!lst || !del)
return ;
while (lst && *lst && (*lst)->next && del)
{
tmp = (*lst)->next;
if ((*lst)->content)
ft_lstdelone(*lst, del);
*lst = tmp;
}
free(*lst);
}

91
src/exec/utils_exec.c Normal file
View File

@ -0,0 +1,91 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* utils_exec.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/09 19:25:18 by adjoly #+# #+# */
/* Updated: 2024/07/10 01:17:11 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include "minishell.h"
int is_in_builtins(char *cmd)
{
int i;
static const char *builtins[] = {"exit", "cd", "unset", "export", "echo",
"pwd", "env", NULL};
i = 0;
while (builtins[i])
{
if (!ft_strcmp(cmd, builtins[i]))
return (i + 1);
i++;
}
return (0);
}
void print_return_value(int return_code)
{
int code;
static const char *sigmsg[] = {0, "Hangup", 0, "Quit", "Illegal \
instruction", "Trace/breakpoint trap", "Aborted", "Bus error",
"Floating point exception", "Killed", "User defined signal 1",
"Segmentation fault", "User defined signal 2", 0,
"Alarm clock", "Terminated", "Stack fault", 0, 0, "Stopped", "Stopped",
"Stopped", "Stopped", 0, "CPU time limit exceeded",
"File size limit exceeded", "Virtual time expired",
"Profiling timer expired", "I/O possible", "Power failure",
"Bad system call"};
if (!WIFEXITED(return_code))
{
if (WIFSIGNALED(return_code))
{
code = WTERMSIG(return_code);
if (!sigmsg[code])
return ;
if (WCOREDUMP(return_code))
printf("minishell : %s %s\n", sigmsg[code], ERROR_COREDUMP);
else
printf("minishell : %s\n", sigmsg[code]);
}
}
}
char *get_cmd_local_path(char *cmd, t_env *env)
{
char *path;
path = env_get_value("PWD", env);
if (!path)
return (NULL);
path = ft_strjoin_free_s1(path, "/");
if (!path)
return (NULL);
path = ft_strjoin_free_s1(path, cmd);
return (path);
}
int switch_cmd_path(t_cmd *cmd, t_env *env)
{
char *path;
if (is_in_builtins(cmd->cmd))
return (0);
if (cmd->cmd[0] == '.' && cmd->cmd[1] == '/')
cmd->cmd = get_cmd_local_path(cmd->cmd, env);
else if (cmd->cmd[0] != '/')
{
path = env_get_value("PATH", env);
cmd->cmd = get_path(path, cmd->cmd);
free(path);
}
if (!(cmd->cmd))
return (-1);
return (0);
}

38
src/exec/utils_exec_2.c Normal file
View File

@ -0,0 +1,38 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* utils_exec_2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/09 22:53:01 by adjoly #+# #+# */
/* Updated: 2024/07/09 22:57:38 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include "minishell.h"
void __wait(int i)
{
while (i - 1)
{
waitpid(-1, NULL, 0);
i--;
}
}
void __close(void *content)
{
if (((t_cmd *)(content))->outfile != STDOUT_FILENO)
close(((t_cmd *)(content))->outfile);
if (((t_cmd *)(content))->infile != STDIN_FILENO)
close(((t_cmd *)(content))->infile);
}
int send_error_exec(char *input)
{
printf("minishell : command not found: %s\n", input);
free(input);
return (-1);
}

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/04 20:10:35 by adjoly #+# #+# */ /* Created: 2024/07/04 20:10:35 by adjoly #+# #+# */
/* Updated: 2024/07/04 20:12:06 by adjoly ### ########.fr */ /* Updated: 2024/07/10 01:19:32 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -19,6 +19,7 @@ void free_redir(void *redir_v)
redir = redir_v; redir = redir_v;
free(redir->file_name); free(redir->file_name);
free(redir);
} }
void free_token(void *token_v) void free_token(void *token_v)
@ -36,7 +37,14 @@ void free_cmd(void *content)
t_cmd *cmd; t_cmd *cmd;
cmd = (t_cmd *)content; cmd = (t_cmd *)content;
free(cmd->cmd); if (cmd->cmd)
ft_free("a", &(cmd->argv)); free(cmd->cmd);
free(cmd); if (cmd->infile != STDIN_FILENO && cmd->infile != -1)
close(cmd->infile);
if (cmd->outfile != STDOUT_FILENO && cmd->outfile != -1)
close(cmd->outfile);
if (cmd->argv)
ft_free("a", &(cmd->argv));
if (cmd)
free(cmd);
} }

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/09 17:24:15 by adjoly #+# #+# */ /* Created: 2024/07/09 17:24:15 by adjoly #+# #+# */
/* Updated: 2024/07/09 18:05:07 by adjoly ### ########.fr */ /* Updated: 2024/07/10 00:46:24 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -25,7 +25,16 @@ t_env **get_env(t_env **env)
t_list **get_list(t_list **list) t_list **get_list(t_list **list)
{ {
static t_list **ret; static t_list **ret;
if (list)
ret = list;
return (ret);
}
t_list **get_list2(t_list **list)
{
static t_list **ret;
if (list) if (list)
ret = list; ret = list;
return (ret); return (ret);

View File

@ -6,11 +6,12 @@
/* 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/09 18:04:47 by adjoly ### ########.fr */ /* Updated: 2024/07/10 00:31:20 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
#include "parsing.h"
void sig_c(int code) void sig_c(int code)
{ {
@ -49,9 +50,9 @@ int main(int ac, char **av, char **env)
get_env(&env_l); get_env(&env_l);
if (!env_l) if (!env_l)
return (EXIT_FAILURE); return (EXIT_FAILURE);
signal(SIGINT, &sig_c);
while (1) while (1)
{ {
signal(SIGINT, &sig_c);
prompt = get_prompt(env_l); prompt = get_prompt(env_l);
rl = readline(prompt); rl = readline(prompt);
free(prompt); free(prompt);
@ -65,15 +66,19 @@ int main(int ac, char **av, char **env)
piped = tokenizer(rl); piped = tokenizer(rl);
get_list(&piped); get_list(&piped);
if (check_argv(piped)) if (check_argv(piped))
{
ft_lstclear(&piped, &free_token);
continue ; continue ;
}
add_history(rl); add_history(rl);
cmd_list = get_cmd_list(piped, env_l); cmd_list = get_cmd_list(piped);
free(rl); free(rl);
ft_lstclear(&piped, &free_token); ft_lstclear(&piped, &free_token);
format_quotes(cmd_list); format_quotes(cmd_list, env_l);
get_list(&cmd_list);
if (check_redir(cmd_list)) if (check_redir(cmd_list))
{ {
ft_lstclear(&cmd_list, &free_cmd); ft_lstclear(get_list(NULL), &free_cmd);
continue ; continue ;
} }
exec_split_cmd(cmd_list, env_l); exec_split_cmd(cmd_list, env_l);

View File

@ -6,12 +6,13 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/09 16:32:21 by adjoly #+# #+# */ /* Created: 2024/07/09 16:32:21 by adjoly #+# #+# */
/* Updated: 2024/07/09 16:39:44 by adjoly ### ########.fr */ /* Updated: 2024/07/09 23:09:41 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "parsing.h" #include "parsing.h"
#include "error_msg.h" #include "error_msg.h"
#include "minishell.h"
bool check_redir(t_list *list) bool check_redir(t_list *list)
{ {

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/05 21:14:04 by adjoly #+# #+# */ /* Created: 2024/06/05 21:14:04 by adjoly #+# #+# */
/* Updated: 2024/07/09 16:12:34 by adjoly ### ########.fr */ /* Updated: 2024/07/09 20:23:16 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -64,5 +64,6 @@ char *env_var_replace(char *readline, t_env *env)
tmp++; tmp++;
} }
} }
free(readline);
return (rl_dollared); return (rl_dollared);
} }

View File

@ -6,23 +6,28 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/31 12:47:13 by adjoly #+# #+# */ /* Created: 2024/05/31 12:47:13 by adjoly #+# #+# */
/* Updated: 2024/07/06 18:05:27 by adjoly ### ########.fr */ /* Updated: 2024/07/10 01:12:52 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "libft.h" #include "libft.h"
#include "parsing.h" #include "parsing.h"
#include "minishell.h"
t_list *get_cmd_list(t_list *list, t_env *env) t_list *get_cmd_list(t_list *list)
{ {
t_list *tmp; t_list *tmp;
t_list *cmd_list; t_list *cmd_list;
tmp = list; tmp = list;
cmd_list = NULL; cmd_list = malloc(sizeof(t_list));
get_list2(&cmd_list);
cmd_list->next = NULL;
cmd_list->content = get_redir_fd(tmp->content);
tmp = tmp->next;
while (tmp) while (tmp)
{ {
ft_lstadd_back(&cmd_list, ft_lstnew(get_redir_fd(tmp->content, env))); ft_lstadd_back(&cmd_list, ft_lstnew(get_redir_fd(tmp->content)));
tmp = tmp->next; tmp = tmp->next;
} }
return (cmd_list); return (cmd_list);

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/30 10:48:41 by adjoly #+# #+# */ /* Created: 2024/05/30 10:48:41 by adjoly #+# #+# */
/* Updated: 2024/07/09 16:16:27 by adjoly ### ########.fr */ /* Updated: 2024/07/10 01:07:50 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -18,12 +18,11 @@
#include <stdio.h> #include <stdio.h>
#include "libft.h" #include "libft.h"
t_cmd *get_redir_fd(void *content, t_env *env) t_cmd *get_redir_fd(void *content)
{ {
t_list *tmp; t_list *tmp;
t_redir_sign sign[2]; t_redir_sign sign[2];
t_cmd *cmd; t_cmd *cmd;
char *env_varred;
tmp = ((t_token *)content)->redirection; tmp = ((t_token *)content)->redirection;
cmd = NULL; cmd = NULL;
@ -39,8 +38,6 @@ t_cmd *get_redir_fd(void *content, t_env *env)
cmd->outfile = STDOUT_FILENO; cmd->outfile = STDOUT_FILENO;
if (sign[1] == OUTFILE) if (sign[1] == OUTFILE)
cmd->infile = STDIN_FILENO; cmd->infile = STDIN_FILENO;
env_varred = env_var_replace(((t_token *)content)->argv, env); cmd = split_cmd(((t_token *)content)->argv, cmd);
cmd = split_cmd(env_varred, cmd);
free(env_varred);
return (cmd); return (cmd);
} }

View File

@ -6,14 +6,15 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/30 15:56:53 by adjoly #+# #+# */ /* Created: 2024/06/30 15:56:53 by adjoly #+# #+# */
/* Updated: 2024/06/30 18:03:14 by adjoly ### ########.fr */ /* Updated: 2024/07/10 01:07:09 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "tokenizer.h" #include "tokenizer.h"
#include <fcntl.h> #include <fcntl.h>
#include "execution.h"
#include "parsing.h" #include "parsing.h"
#include "execution.h"
#include <stdio.h>
t_redir_sign __close_unused_fd(t_cmd *cmd, t_redir_sign sign) t_redir_sign __close_unused_fd(t_cmd *cmd, t_redir_sign sign)
{ {
@ -31,7 +32,7 @@ void open_redir(t_redirection *redir, t_cmd *cmd, t_redir_sign sign[2])
if (redir->sign == INFILE || redir->sign == HEREDOC) if (redir->sign == INFILE || redir->sign == HEREDOC)
sign[1] = __close_unused_fd(cmd, redir->sign); sign[1] = __close_unused_fd(cmd, redir->sign);
if (redir->sign == HEREDOC) if (redir->sign == HEREDOC)
cmd->infile = ft_heredoc(redir->file_name); cmd->infile = ft_heredoc(redir->file_name, cmd);
else if (redir->sign == INFILE) else if (redir->sign == INFILE)
cmd->infile = open(redir->file_name, O_RDONLY); cmd->infile = open(redir->file_name, O_RDONLY);
else if (redir->sign == OUTFILE) else if (redir->sign == OUTFILE)

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/25 15:06:15 by adjoly #+# #+# */ /* Created: 2024/05/25 15:06:15 by adjoly #+# #+# */
/* Updated: 2024/05/30 15:47:19 by adjoly ### ########.fr */ /* Updated: 2024/07/10 00:21:08 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View File

@ -6,13 +6,14 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/20 21:05:04 by adjoly #+# #+# */ /* Created: 2024/05/20 21:05:04 by adjoly #+# #+# */
/* Updated: 2024/06/26 12:56:14 by adjoly ### ########.fr */ /* Updated: 2024/07/09 23:43:39 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "tokenizer.h" #include "tokenizer.h"
#include "libft.h" #include "libft.h"
#include "parsing.h" #include "parsing.h"
#include <stdio.h>
size_t __get_sizeof_redir(char *redir_s, t_redirection *redir) size_t __get_sizeof_redir(char *redir_s, t_redirection *redir)
{ {