This commit is contained in:
2024-04-24 13:07:36 +02:00
15 changed files with 326 additions and 279 deletions

View File

@ -6,7 +6,7 @@
# By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2023/07/28 00:35:01 by tomoron #+# #+# #
# Updated: 2024/04/21 23:58:44 by tomoron ### ########.fr #
# Updated: 2024/04/24 11:00:09 by marde-vr ### ########.fr #
# #
# **************************************************************************** #
@ -20,6 +20,7 @@ SRCS_RAW = main.c\
is_fd_open.c\
echo.c\
pwd.c\
env.c\
parsing.c\
debug.c\
env_utils.c\
@ -39,8 +40,9 @@ SRCS_RAW = main.c\
utils_bonus.c\
signal_handler.c\
parsing_bonus.c\
exec_bonus.c
exec_bonus.c\
exec_utils.c\
free.c
OBJS_DIR = objs/
SRCS_DIR = srcs/

BIN
srcs/.main.c.swp Normal file

Binary file not shown.

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/16 21:02:54 by marde-vr #+# #+# */
/* Updated: 2024/04/23 20:06:06 by tomoron ### ########.fr */
/* Updated: 2024/04/24 10:43:52 by marde-vr ### ########.fr */
/* */
/* ************************************************************************** */
@ -33,20 +33,20 @@ void cd_update_pwd(t_msh *msh)
msh->env = export_set_env(msh->env, ft_strdup("PWD"), new, 0);
}
char *get_new_wd(t_token *arg, t_msh *msh)
char *get_new_wd(t_token *arg, t_msh *msh)
{
char *nw_wd;
char *nw_wd;
if (arg)
{
nw_wd = arg->value;
if(!ft_strcmp("-", nw_wd))
if (!ft_strcmp("-", nw_wd))
{
nw_wd = ft_get_env(msh->env, "OLDPWD");
if(!nw_wd)
if (!nw_wd)
ft_putstr_fd("minishell: cd: OLDPWD not set\n", 2);
if(!nw_wd)
return(0);
if (!nw_wd)
return (0);
ft_printf_fd((1 * (msh->out_fd == 0)) + msh->out_fd, "%s\n", nw_wd);
}
}
@ -58,7 +58,7 @@ char *get_new_wd(t_token *arg, t_msh *msh)
if (!nw_wd)
return (0);
}
return(nw_wd);
return (nw_wd);
}
int cd(t_token *args, t_msh *msh)
@ -71,8 +71,8 @@ int cd(t_token *args, t_msh *msh)
return (1);
}
new_wd = get_new_wd(args->next, msh);
if(!new_wd)
return(1);
if (!new_wd)
return (1);
if (chdir(new_wd) == -1)
{
perror("minishell: cd");

101
srcs/env.c Normal file
View File

@ -0,0 +1,101 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* env.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/24 10:58:36 by marde-vr #+# #+# */
/* Updated: 2024/04/24 10:59:12 by marde-vr ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
t_env *dup_env(t_env *env)
{
t_env *res;
res = 0;
while (env)
{
res = env_add_back(res, env->name, env->value);
env = env->next;
}
return (res);
}
void sort_env(t_env *env)
{
t_env *tmp;
t_env *start;
char *tmp_str;
tmp = env;
start = env;
while (tmp)
{
env = start;
while (env)
{
if (ft_strcmp(tmp->name, env->name) < 0)
{
tmp_str = tmp->name;
tmp->name = env->name;
env->name = tmp_str;
tmp_str = tmp->value;
tmp->value = env->value;
env->value = tmp_str;
}
env = env->next;
}
tmp = tmp->next;
}
}
void print_env_declare(t_msh *msh, t_env *env_orig)
{
t_env *env;
env = dup_env(env_orig);
sort_env(env);
if (!msh->out_fd)
msh->out_fd = 1;
while (env)
{
if (strcmp(env->name, "_"))
{
if (env->value && *(env->value))
ft_printf_fd(msh->out_fd, "declare -x %s=\"%s\"\n", env->name,
env->value);
else
ft_printf_fd(msh->out_fd, "declare -x %s\n", env->name);
}
env = env->next;
}
}
void delete_from_env(t_msh *msh, char *name)
{
t_env *tmp_env;
t_env *prev;
tmp_env = msh->env;
prev = 0;
while (tmp_env)
{
if (!strcmp(name, tmp_env->name))
{
free(tmp_env->name);
free(tmp_env->value);
if (!prev)
msh->env = tmp_env->next;
else
prev->next = tmp_env->next;
free(tmp_env);
return ;
}
prev = tmp_env;
tmp_env = tmp_env->next;
}
}

View File

@ -6,44 +6,12 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/28 13:50:14 by tomoron #+# #+# */
/* Updated: 2024/04/23 18:43:24 by tomoron ### ########.fr */
/* Updated: 2024/04/24 10:49:07 by marde-vr ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void get_redirections(t_msh *msh, t_cmd *cmds)
{
msh->in_type = 0;
msh->out_type = 0;
if (first_is_in_type(cmds))
{
if (!get_in_type(msh, cmds))
get_out_type(msh, cmds);
}
else
{
if (!get_out_type(msh, cmds))
get_in_type(msh, cmds);
}
}
t_cmd *get_next_command(t_cmd *cmd)
{
while (cmd)
{
while (cmd && !is_operand_type(cmd))
cmd = cmd->next;
if (cmd && cmd->cmd_type == AND && !g_return_code)
return (cmd->next);
if (cmd && cmd->cmd_type == OR && g_return_code)
return (cmd->next);
if (cmd)
cmd = cmd->next;
}
return (0);
}
void exec_command_bonus(t_msh *msh, char *cmd_str)
{
t_cmd *cmds;
@ -95,9 +63,9 @@ int exec(t_msh *msh, char **cmd_args, int i, int cmd_count)
}
if (pid == 0)
child(msh, cmd_args, i);
if(pid != 0)
if (pid != 0)
msh->pids[i] = pid;
if(pid != 0)
if (pid != 0)
parent(msh, i, cmd_count, cmd_args);
return (0);
}
@ -115,63 +83,6 @@ void exec_command(t_msh *msh, int i, int cmd_count)
remove_command_from_msh(msh);
}
int get_cmd_count(t_cmd *cmds)
{
int nb;
nb = 0;
while (cmds && !is_operand_type(cmds))
{
while (cmds && (is_output_type(cmds) || is_input_type(cmds)))
cmds = cmds->next;
if (is_cmd_type(cmds))
nb++;
while (cmds && (is_output_type(cmds) || is_input_type(cmds)
|| is_cmd_type(cmds)))
cmds = cmds->next;
if (cmds && cmds->cmd_type == PIPE)
cmds = cmds->next;
}
return (nb);
}
int is_parenthesis(t_cmd *cmd)
{
if (!cmd)
return (0);
return (cmd->cmd_type == PAREN || (cmd->cmd_type == PIPE
&& cmd->next->cmd_type == PAREN));
}
void print_signaled(int status)
{
int signal;
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 (skill issue)", "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"};
signal = WTERMSIG(status);
if (signal < 31 && sigmsg[signal])
{
ft_putstr_fd((char *)sigmsg[signal], 2);
}
if (signal >= 34 && signal <= 64)
{
ft_putstr_fd("Real-time signal ", 2);
ft_putnbr_fd(signal - 34, 2);
}
if (WCOREDUMP(status))
ft_putstr_fd(" (core dumped)", 2);
ft_putstr_fd("\n", 2);
g_return_code = signal + 128;
}
void end_execution(t_msh *msh, int cmd_count)
{
int i;

102
srcs/exec_utils.c Normal file
View File

@ -0,0 +1,102 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* exec_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/24 10:46:28 by marde-vr #+# #+# */
/* Updated: 2024/04/24 10:49:02 by marde-vr ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void get_redirections(t_msh *msh, t_cmd *cmds)
{
msh->in_type = 0;
msh->out_type = 0;
if (first_is_in_type(cmds))
{
if (!get_in_type(msh, cmds))
get_out_type(msh, cmds);
}
else
{
if (!get_out_type(msh, cmds))
get_in_type(msh, cmds);
}
}
t_cmd *get_next_command(t_cmd *cmd)
{
while (cmd)
{
while (cmd && !is_operand_type(cmd))
cmd = cmd->next;
if (cmd && cmd->cmd_type == AND && !g_return_code)
return (cmd->next);
if (cmd && cmd->cmd_type == OR && g_return_code)
return (cmd->next);
if (cmd)
cmd = cmd->next;
}
return (0);
}
int get_cmd_count(t_cmd *cmds)
{
int nb;
nb = 0;
while (cmds && !is_operand_type(cmds))
{
while (cmds && (is_output_type(cmds) || is_input_type(cmds)))
cmds = cmds->next;
if (is_cmd_type(cmds))
nb++;
while (cmds && (is_output_type(cmds) || is_input_type(cmds)
|| is_cmd_type(cmds)))
cmds = cmds->next;
if (cmds && cmds->cmd_type == PIPE)
cmds = cmds->next;
}
return (nb);
}
int is_parenthesis(t_cmd *cmd)
{
if (!cmd)
return (0);
return (cmd->cmd_type == PAREN || (cmd->cmd_type == PIPE
&& cmd->next->cmd_type == PAREN));
}
void print_signaled(int status)
{
int signal;
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 (skill issue)", "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"};
signal = WTERMSIG(status);
if (signal < 31 && sigmsg[signal])
{
ft_putstr_fd((char *)sigmsg[signal], 2);
}
if (signal >= 34 && signal <= 64)
{
ft_putstr_fd("Real-time signal ", 2);
ft_putnbr_fd(signal - 34, 2);
}
if (WCOREDUMP(status))
ft_putstr_fd(" (core dumped)", 2);
ft_putstr_fd("\n", 2);
g_return_code = signal + 128;
}

View File

@ -6,74 +6,12 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/18 18:29:20 by marde-vr #+# #+# */
/* Updated: 2024/04/24 13:03:32 by tomoron ### ########.fr */
/* Updated: 2024/04/24 13:06:58 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
t_env *dup_env(t_env *env)
{
t_env *res;
res = 0;
while(env)
{
res = env_add_back(res, env->name, env->value);
env = env->next;
}
return(res);
}
void sort_env(t_env *env)
{
t_env *tmp;
t_env *start;
char *tmp_str;
tmp = env;
start = env;
while(tmp)
{
env = start;
while(env)
{
if(ft_strcmp(tmp->name, env->name) < 0)
{
tmp_str = tmp->name;
tmp->name = env->name;
env->name = tmp_str;
tmp_str = tmp->value;
tmp->value = env->value;
env->value = tmp_str;
}
env = env->next;
}
tmp = tmp->next;
}
}
void print_env_declare(t_msh *msh, t_env *env_orig)
{
t_env *env;
env = dup_env(env_orig);
sort_env(env);
if(!msh->out_fd)
msh->out_fd = 1;
while (env)
{
if (strcmp(env->name, "_"))
{
if (env->value && *(env->value))
ft_printf_fd(msh->out_fd, "declare -x %s=\"%s\"\n", env->name, env->value);
else
ft_printf_fd(msh->out_fd, "declare -x %s\n", env->name);
}
env = env->next;
}
}
int export_invalid_identifier(char *arg, char *name)
{
ft_putstr_fd("minishell: export: `", 2);
@ -146,31 +84,6 @@ int ft_export(t_msh *msh, t_token *cmd, t_env *env)
return (0);
}
void delete_from_env(t_msh *msh, char *name)
{
t_env *tmp_env;
t_env *prev;
tmp_env = msh->env;
prev = 0;
while (tmp_env)
{
if (!strcmp(name, tmp_env->name))
{
free(tmp_env->name);
free(tmp_env->value);
if (!prev)
msh->env = tmp_env->next;
else
prev->next = tmp_env->next;
free(tmp_env);
return ;
}
prev = tmp_env;
tmp_env = tmp_env->next;
}
}
int ft_unset(t_msh *msh)
{
t_token *cmd;

45
srcs/free.c Normal file
View File

@ -0,0 +1,45 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* free.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/24 10:51:13 by marde-vr #+# #+# */
/* Updated: 2024/04/24 10:51:25 by marde-vr ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void free_fds(t_msh *msh)
{
int i;
if (msh->fds)
{
i = 0;
while (msh->fds[i])
{
free(msh->fds[i]);
msh->fds[i] = 0;
i++;
}
free(msh->fds);
msh->fds = 0;
}
}
void free_msh(t_msh *msh)
{
if (msh)
{
free_env(msh->env);
free(msh->pids);
free_cmd(msh->cmds_head);
free_fds(msh);
free(msh->here_doc_filename);
free_token(msh->tokens);
free(msh);
}
}

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/24 17:44:32 by marde-vr #+# #+# */
/* Updated: 2024/04/23 17:07:00 by tomoron ### ########.fr */
/* Updated: 2024/04/24 10:40:41 by marde-vr ### ########.fr */
/* */
/* ************************************************************************** */
@ -17,7 +17,6 @@ void get_here_doc_input(t_msh *msh, char *eof)
char *line;
line = NULL;
//TODO: parse eof sans parse les variables
while (1)
{
free(line);
@ -68,6 +67,12 @@ void handle_here_doc(t_msh *msh, char *eof)
int pid;
here_doc_file = get_tmp_file_name(msh);
if (msh->here_doc_filename)
{
close(msh->in_fd);
unlink(msh->here_doc_filename);
free(msh->here_doc_filename);
}
msh->here_doc_filename = here_doc_file;
msh->in_fd = open(here_doc_file, O_CREAT | O_RDWR, 0644);
if (msh->in_fd == -1)

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/02 21:59:20 by tomoron #+# #+# */
/* Updated: 2024/04/24 13:05:21 by tomoron ### ########.fr */
/* Updated: 2024/04/24 13:07:14 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
@ -99,46 +99,12 @@ int init_minishell(t_msh **msh, int argc, char **argv, char **envp)
(*msh)->env = add_shlvl((*msh)->env);
tcgetattr(1, &t_p);
(*msh)->echoctl = t_p.c_lflag & ECHOCTL;
signal(SIGINT, signal_handler_interactive); //enables ctrl-C
signal(SIGINT, signal_handler_interactive);
signal(SIGQUIT, signal_handler_interactive);
if (set_echoctl(0))
ft_exit(*msh, 1);
return (0);
}
/*
mandatory
int main(int argc, char **argv, char **envp)
{
char *commands;
char *prompt;
t_msh *msh;
char *commands;
char *prompt;
t_msh *msh;
commands = (char *)1;
init_minishell(&msh, argc, argv, envp);
while (commands)
{
prompt = get_prompt(msh->env);
if (!prompt)
exit(1);
commands = readline(prompt);
free(prompt);
add_history(commands);
msh->tokens = parse_command(commands, msh->env);
print_parsed_cmd(msh->tokens);
free(commands);
exec_commands(msh);
free_token(msh->tokens);
}
rl_clear_history();
set_echoctl(msh->echoctl);
free_msh(msh);
ft_printf("exit\n");
return (g_return_code);
}*/
int main(int argc, char **argv, char **envp)
{

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/04 17:31:38 by tomoron #+# #+# */
/* Updated: 2024/04/23 19:52:16 by tomoron ### ########.fr */
/* Updated: 2024/04/24 10:59:46 by marde-vr ### ########.fr */
/* */
/* ************************************************************************** */
@ -74,7 +74,7 @@ typedef struct s_msh
int out_fd;
int locked_return_code;
int echoctl;
char *here_doc_filename;
char *here_doc_filename;
} t_msh;
extern int g_return_code;
@ -93,7 +93,7 @@ int cmd_is_builtin(t_msh *msh, char *cmd_token);
t_token *token_add_back(t_token *res, char *token);
void child(t_msh *msh, char **cmd_args, int i);
t_token *parse_tokens(char *command, t_env *env);
void parent(t_msh *msh, int i, int cmd_count,char **cmd_args);
void parent(t_msh *msh, int i, int cmd_count, char **cmd_args);
char *ft_get_env(t_env *env, char *var_name);
int is_fd_open(int fd);
int get_out_type(t_msh *msh, t_cmd *cmds);
@ -107,6 +107,11 @@ void signal_handler_here_doc(int signum);
t_token *parsing_syntax_error(t_token *res);
int file_access(t_msh *msh, int *found);
void remove_command_from_msh(t_msh *msh);
void get_redirections(t_msh *msh, t_cmd *cmds);
t_cmd *get_next_command(t_cmd *cmd);
int get_cmd_count(t_cmd *cmds);
int is_parenthesis(t_cmd *cmd);
void print_signaled(int status);
void ft_exit(t_msh *msh, int error_code);
void sort_wildcards_token(t_token *list);
void redirect_input(t_msh *msh, int i, char **cmd_args);
@ -133,6 +138,10 @@ int get_cmd_count(t_cmd *cmds);
int check_var_name(char *name);
char **get_cmd_args(t_msh *msh);
char *remove_path(char *token);
t_env *dup_env(t_env *env);
void sort_env(t_env *env);
void print_env_declare(t_msh *msh, t_env *env_orig);
void delete_from_env(t_msh *msh, char *name);
t_cmd *parsing_bonus(char *cmd);
t_token *free_token(t_token *cmd);
void exec_commands(t_msh *msh);

View File

@ -6,7 +6,7 @@
/* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/19 14:09:44 by tomoron #+# #+# */
/* Updated: 2024/04/22 19:42:05 by marde-vr ### ########.fr */
/* Updated: 2024/04/24 11:04:42 by marde-vr ### ########.fr */
/* */
/* ************************************************************************** */
@ -38,7 +38,7 @@ int open_out_file(t_msh *msh, t_cmd **cur_cmd, char *filename)
if (msh->out_type == RED_O)
msh->out_fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0644);
if (msh->out_type == RED_O_APP)
msh->out_fd = open(filename, O_CREAT | O_RDWR | O_APPEND, 0644);
msh->out_fd = open(filename, O_CREAT | O_WRONLY | O_APPEND, 0644);
if (msh->out_fd == -1)
{
ft_putstr_fd("minishell: ", 2);

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/05 18:17:25 by marde-vr #+# #+# */
/* Updated: 2024/04/23 18:51:29 by tomoron ### ########.fr */
/* Updated: 2024/04/24 10:42:16 by marde-vr ### ########.fr */
/* */
/* ************************************************************************** */
@ -29,14 +29,14 @@ void close_pipe_fds(t_msh *msh, int i)
void handle_parenthesis(t_msh *msh)
{
char *command;
char *command;
command = 0;
if(msh->cmds->cmd_type == PAREN)
if (msh->cmds->cmd_type == PAREN)
command = ft_strdup(msh->cmds->value);
else if(msh->cmds->cmd_type == PIPE)
else if (msh->cmds->cmd_type == PIPE)
command = ft_strdup(msh->cmds->next->value);
if(!command)
if (!command)
{
ft_printf_fd(2, "an error occured");
ft_exit(msh, 1);

View File

@ -6,44 +6,12 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/05 18:19:26 by marde-vr #+# #+# */
/* Updated: 2024/04/23 16:58:07 by tomoron ### ########.fr */
/* Updated: 2024/04/24 10:51:28 by marde-vr ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void free_fds(t_msh *msh)
{
int i;
if (msh->fds)
{
i = 0;
while (msh->fds[i])
{
free(msh->fds[i]);
msh->fds[i] = 0;
i++;
}
free(msh->fds);
msh->fds = 0;
}
}
void free_msh(t_msh *msh)
{
if (msh)
{
free_env(msh->env);
free(msh->pids);
free_cmd(msh->cmds_head);
free_fds(msh);
free(msh->here_doc_filename);
free_token(msh->tokens);
free(msh);
}
}
void ft_exit(t_msh *msh, int exit_code)
{
set_echoctl(msh->echoctl);

View File

@ -1,6 +1,31 @@
unset HOME
cd | lolcat
double here_doc and manual here_doc tests
$OLDPWD and $PWD (manual tests)
$SHLVL
cat <minishell.h <<HERE <missing <<DOC | echo oi
#invalid command, followed by empty variable, should clear the exit code
doesntexist
$EMPTY
echo $?
# Should skip the empty argument, and print hello after spaces
echo - "" " " hello
NORME :c
test signals
test and verify all malocs
verify forbidden functions
Not mandatory ig? # Empty export isn't set on `env` but is set on `export`
export hello
env | grep hello
export | grep hello
potential ft_exit that doesnt unlink tmp here_doc files
$EMPTY (manual tests)
parse eof sans parse les variables