mirror of
https://github.com/KeyZox71/ft_minipowershell.git
synced 2025-03-15 03:16:51 +01:00
「✏️」 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:
@ -6,7 +6,7 @@
|
||||
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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
|
||||
|
||||
# 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);
|
||||
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
|
||||
@ -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
|
||||
*/
|
||||
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
|
||||
|
@ -41,5 +41,6 @@ void free_cmd(void *content);
|
||||
|
||||
t_env **get_env(t_env **env);
|
||||
t_list **get_list(t_list **list);
|
||||
t_list **get_list2(t_list **list);
|
||||
|
||||
#endif
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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
|
||||
} t_quote;
|
||||
|
||||
t_cmd *get_redir_fd(void *content, t_env *env);
|
||||
t_list *get_cmd_list(t_list *list, t_env *env);
|
||||
t_cmd *get_redir_fd(void *content);
|
||||
t_list *get_cmd_list(t_list *list);
|
||||
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_token(t_token *token);
|
||||
void print_redir(t_redirection *redir);
|
||||
void print_cmd(t_cmd *cmd);
|
||||
|
||||
#endif
|
||||
|
@ -6,29 +6,14 @@
|
||||
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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 "builtins.h"
|
||||
#include "error_msg.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);
|
||||
}
|
||||
#include "execution.h"
|
||||
|
||||
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);
|
||||
if (i == 1)
|
||||
exit(ft_atoi(argv[1]));
|
||||
if (i > 4)
|
||||
exit(0);
|
||||
}
|
||||
else
|
||||
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;
|
||||
|
||||
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)
|
||||
{
|
||||
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] != '/')
|
||||
cmd->cmd = get_path(env_get_value("PATH", env), cmd->cmd);
|
||||
if (!(cmd->cmd))
|
||||
return (-1);
|
||||
return (0);
|
||||
exec.status = dup2(cmd->infile, STDIN_FILENO);
|
||||
if (cmd->infile != STDIN_FILENO)
|
||||
close(cmd->infile);
|
||||
if (exec.status == -1)
|
||||
exit(-1);
|
||||
exec.status = dup2(cmd->outfile, STDOUT_FILENO);
|
||||
if (cmd->outfile != STDOUT_FILENO)
|
||||
close(cmd->outfile);
|
||||
if (exec.pipe_fd[0] != -1)
|
||||
close(exec.pipe_fd[0]);
|
||||
if (exec.pipe_fd[0] != -1)
|
||||
close(exec.pipe_fd[1]);
|
||||
if (exec.status != -1)
|
||||
exec_cmd(cmd->cmd, cmd->argv, env, env_t);
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
int exec_single_cmd(t_cmd *cmd, char **env, t_env *env_t, int pipe_fd[2])
|
||||
{
|
||||
t_exec exec;
|
||||
int fork_pid;
|
||||
int status;
|
||||
char *input;
|
||||
|
||||
input = ft_strdup(cmd->cmd);
|
||||
status = switch_cmd_path(cmd, env_t);
|
||||
if (status == -1 || !input || (access(cmd->cmd, X_OK) && !is_in_builtins(cmd->cmd)))
|
||||
exec.pipe_fd[0] = pipe_fd[0];
|
||||
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);
|
||||
free(input);
|
||||
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);
|
||||
return (0);
|
||||
}
|
||||
fork_pid = fork();
|
||||
if (!fork_pid)
|
||||
{
|
||||
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);
|
||||
}
|
||||
__fork_single_cmd(cmd, env, env_t, exec);
|
||||
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)
|
||||
{
|
||||
status = pipe(pipe_fd);
|
||||
if (status)
|
||||
return (-1);
|
||||
exec.status = pipe(exec.pipe_fd);
|
||||
if (exec.status)
|
||||
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)
|
||||
((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)
|
||||
((t_cmd *)(list_cmd->next->content))->infile = pipe_fd[0];
|
||||
status = exec_single_cmd(list_cmd->content, env_array, env, pipe_fd);
|
||||
if (((t_cmd *)(list_cmd->content))->outfile != STDOUT_FILENO)
|
||||
close(((t_cmd *)(list_cmd->content))->outfile);
|
||||
if (((t_cmd *)(list_cmd->content))->infile != STDIN_FILENO)
|
||||
close(((t_cmd *)(list_cmd->content))->infile);
|
||||
if (status != -1)
|
||||
i++;
|
||||
((t_cmd *)(list_cmd->next->content))->infile = exec.pipe_fd[0];
|
||||
exec.status = exec_single_cmd(list_cmd->content, exec.env_array, \
|
||||
env, exec.pipe_fd);
|
||||
__close(list_cmd->content);
|
||||
if (exec.status != -1)
|
||||
exec.i++;
|
||||
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)
|
||||
close(((t_cmd *)(list_cmd->content))->outfile);
|
||||
if (((t_cmd *)(list_cmd->content))->infile != STDIN_FILENO)
|
||||
close(((t_cmd *)(list_cmd->content))->infile);
|
||||
ft_free("a", &env_array);
|
||||
if (status == -1)
|
||||
i--;
|
||||
if (i < 1)
|
||||
return (exec);
|
||||
}
|
||||
|
||||
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);
|
||||
if (status != 0)
|
||||
waitpid(status, &return_code, 0);
|
||||
while (i - 1)
|
||||
{
|
||||
waitpid(-1, NULL, 0);
|
||||
i--;
|
||||
}
|
||||
if (exec.status != 0)
|
||||
waitpid(exec.status, &return_code, 0);
|
||||
if (!return_code)
|
||||
return_code = 0;
|
||||
__wait(exec.i);
|
||||
print_return_value(return_code);
|
||||
return (0);
|
||||
}
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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)
|
||||
{
|
||||
uint i;
|
||||
uint i_offset;
|
||||
|
||||
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;
|
||||
if (*cmd == DOUBLE || *cmd == SINGLE)
|
||||
ft_strlcpy(cmd, cmd + 1, ft_strlen(cmd) - 1);
|
||||
return (cmd);
|
||||
}
|
||||
|
||||
int format_quotes_cmd(t_cmd *cmd)
|
||||
int format_quotes_cmd(t_cmd *cmd, t_env *env)
|
||||
{
|
||||
uint i;
|
||||
|
||||
@ -59,19 +27,20 @@ int format_quotes_cmd(t_cmd *cmd)
|
||||
i = 0;
|
||||
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++;
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
int format_quotes(t_list *list_cmd)
|
||||
int format_quotes(t_list *list_cmd, t_env *env)
|
||||
{
|
||||
if (!list_cmd)
|
||||
return (0);
|
||||
while (list_cmd)
|
||||
{
|
||||
format_quotes_cmd(list_cmd->content);
|
||||
format_quotes_cmd(list_cmd->content, env);
|
||||
list_cmd = list_cmd->next;
|
||||
}
|
||||
return (0);
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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, index_itoa, ft_strlen(index_itoa));
|
||||
free(index_itoa);
|
||||
if (mode > 0)
|
||||
return (open(path, O_RDONLY));
|
||||
else
|
||||
return (open(path, O_WRONLY | O_TRUNC | O_CREAT, 0644));
|
||||
return (__open_fd_here(path, mode));
|
||||
}
|
||||
|
||||
static int get_input(char *delimiter, int fd)
|
||||
@ -52,16 +49,16 @@ static int get_input(char *delimiter, int fd)
|
||||
int status;
|
||||
|
||||
line = readline("heredoc> ");
|
||||
while (ft_strcmp(line, delimiter))
|
||||
while (line && ft_strcmp(line, delimiter))
|
||||
{
|
||||
status = write(fd, line, ft_strlen(line));
|
||||
if (status == -1)
|
||||
fd_manager(fd);
|
||||
close(fd_manager(fd));
|
||||
if (status == -1)
|
||||
break ;
|
||||
status = write(fd, "\n", 1);
|
||||
if (status == -1)
|
||||
fd_manager(fd);
|
||||
close(fd_manager(fd));
|
||||
if (status == -1)
|
||||
break ;
|
||||
free(line);
|
||||
@ -70,11 +67,21 @@ static int get_input(char *delimiter, int fd)
|
||||
free(line);
|
||||
status = write(fd, "\0", 1);
|
||||
if (status == -1)
|
||||
fd_manager(fd);
|
||||
close(fd_manager(fd));
|
||||
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 fd;
|
||||
@ -82,33 +89,31 @@ int __heredoc(char *delimiter)
|
||||
fd = fd_manager(0);
|
||||
if (fd == -1)
|
||||
{
|
||||
fd_manager(-1);
|
||||
close(fd_manager(-1));
|
||||
return (-1);
|
||||
}
|
||||
fork_pid = fork();
|
||||
if (fork_pid == -1)
|
||||
{
|
||||
fd_manager(-1);
|
||||
close(fd_manager(-1));
|
||||
close(fd);
|
||||
return (-1);
|
||||
}
|
||||
if (!fork_pid)
|
||||
{
|
||||
get_input(delimiter, fd);
|
||||
ft_envclear(get_env(NULL), free);
|
||||
ft_lstclear(get_list(NULL), &free_token);
|
||||
__forked(delimiter, fd, cmd);
|
||||
exit(0);
|
||||
}
|
||||
else
|
||||
waitpid(fork_pid, NULL, 0);
|
||||
close(fd);
|
||||
return (fd_manager(1));
|
||||
}
|
||||
|
||||
int ft_heredoc(char *delimiter)
|
||||
int ft_heredoc(char *delimiter, t_cmd *cmd)
|
||||
{
|
||||
int fd;
|
||||
|
||||
fd = __heredoc(delimiter);
|
||||
|
||||
fd = __heredoc(delimiter, cmd);
|
||||
return (fd);
|
||||
}
|
||||
|
48
src/exec/heredoc_utils.c
Normal file
48
src/exec/heredoc_utils.c
Normal 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
91
src/exec/utils_exec.c
Normal 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
38
src/exec/utils_exec_2.c
Normal 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);
|
||||
}
|
@ -6,7 +6,7 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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;
|
||||
free(redir->file_name);
|
||||
free(redir);
|
||||
}
|
||||
|
||||
void free_token(void *token_v)
|
||||
@ -36,7 +37,14 @@ void free_cmd(void *content)
|
||||
t_cmd *cmd;
|
||||
|
||||
cmd = (t_cmd *)content;
|
||||
free(cmd->cmd);
|
||||
ft_free("a", &(cmd->argv));
|
||||
free(cmd);
|
||||
if (cmd->cmd)
|
||||
free(cmd->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);
|
||||
}
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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)
|
||||
{
|
||||
static t_list **ret;
|
||||
|
||||
|
||||
if (list)
|
||||
ret = list;
|
||||
return (ret);
|
||||
}
|
||||
|
||||
t_list **get_list2(t_list **list)
|
||||
{
|
||||
static t_list **ret;
|
||||
|
||||
if (list)
|
||||
ret = list;
|
||||
return (ret);
|
||||
|
15
src/main.c
15
src/main.c
@ -6,11 +6,12 @@
|
||||
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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 "parsing.h"
|
||||
|
||||
void sig_c(int code)
|
||||
{
|
||||
@ -49,9 +50,9 @@ int main(int ac, char **av, char **env)
|
||||
get_env(&env_l);
|
||||
if (!env_l)
|
||||
return (EXIT_FAILURE);
|
||||
signal(SIGINT, &sig_c);
|
||||
while (1)
|
||||
{
|
||||
signal(SIGINT, &sig_c);
|
||||
prompt = get_prompt(env_l);
|
||||
rl = readline(prompt);
|
||||
free(prompt);
|
||||
@ -65,15 +66,19 @@ int main(int ac, char **av, char **env)
|
||||
piped = tokenizer(rl);
|
||||
get_list(&piped);
|
||||
if (check_argv(piped))
|
||||
{
|
||||
ft_lstclear(&piped, &free_token);
|
||||
continue ;
|
||||
}
|
||||
add_history(rl);
|
||||
cmd_list = get_cmd_list(piped, env_l);
|
||||
cmd_list = get_cmd_list(piped);
|
||||
free(rl);
|
||||
ft_lstclear(&piped, &free_token);
|
||||
format_quotes(cmd_list);
|
||||
format_quotes(cmd_list, env_l);
|
||||
get_list(&cmd_list);
|
||||
if (check_redir(cmd_list))
|
||||
{
|
||||
ft_lstclear(&cmd_list, &free_cmd);
|
||||
ft_lstclear(get_list(NULL), &free_cmd);
|
||||
continue ;
|
||||
}
|
||||
exec_split_cmd(cmd_list, env_l);
|
||||
|
@ -6,12 +6,13 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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 "error_msg.h"
|
||||
#include "minishell.h"
|
||||
|
||||
bool check_redir(t_list *list)
|
||||
{
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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++;
|
||||
}
|
||||
}
|
||||
free(readline);
|
||||
return (rl_dollared);
|
||||
}
|
||||
|
@ -6,23 +6,28 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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 "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 *cmd_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)
|
||||
{
|
||||
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;
|
||||
}
|
||||
return (cmd_list);
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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 "libft.h"
|
||||
|
||||
t_cmd *get_redir_fd(void *content, t_env *env)
|
||||
t_cmd *get_redir_fd(void *content)
|
||||
{
|
||||
t_list *tmp;
|
||||
t_redir_sign sign[2];
|
||||
t_cmd *cmd;
|
||||
char *env_varred;
|
||||
|
||||
tmp = ((t_token *)content)->redirection;
|
||||
cmd = NULL;
|
||||
@ -39,8 +38,6 @@ t_cmd *get_redir_fd(void *content, t_env *env)
|
||||
cmd->outfile = STDOUT_FILENO;
|
||||
if (sign[1] == OUTFILE)
|
||||
cmd->infile = STDIN_FILENO;
|
||||
env_varred = env_var_replace(((t_token *)content)->argv, env);
|
||||
cmd = split_cmd(env_varred, cmd);
|
||||
free(env_varred);
|
||||
cmd = split_cmd(((t_token *)content)->argv, cmd);
|
||||
return (cmd);
|
||||
}
|
||||
|
@ -6,14 +6,15 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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 <fcntl.h>
|
||||
#include "execution.h"
|
||||
#include "parsing.h"
|
||||
#include "execution.h"
|
||||
#include <stdio.h>
|
||||
|
||||
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)
|
||||
sign[1] = __close_unused_fd(cmd, redir->sign);
|
||||
if (redir->sign == HEREDOC)
|
||||
cmd->infile = ft_heredoc(redir->file_name);
|
||||
cmd->infile = ft_heredoc(redir->file_name, cmd);
|
||||
else if (redir->sign == INFILE)
|
||||
cmd->infile = open(redir->file_name, O_RDONLY);
|
||||
else if (redir->sign == OUTFILE)
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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 */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
@ -6,13 +6,14 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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 "libft.h"
|
||||
#include "parsing.h"
|
||||
#include <stdio.h>
|
||||
|
||||
size_t __get_sizeof_redir(char *redir_s, t_redirection *redir)
|
||||
{
|
||||
|
Reference in New Issue
Block a user