merge
This commit is contained in:
8
Makefile
8
Makefile
@ -6,7 +6,7 @@
|
|||||||
# By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ #
|
# By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ #
|
||||||
# +#+#+#+#+#+ +#+ #
|
# +#+#+#+#+#+ +#+ #
|
||||||
# Created: 2023/07/28 00:35:01 by tomoron #+# #+# #
|
# 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\
|
is_fd_open.c\
|
||||||
echo.c\
|
echo.c\
|
||||||
pwd.c\
|
pwd.c\
|
||||||
|
env.c\
|
||||||
parsing.c\
|
parsing.c\
|
||||||
debug.c\
|
debug.c\
|
||||||
env_utils.c\
|
env_utils.c\
|
||||||
@ -39,8 +40,9 @@ SRCS_RAW = main.c\
|
|||||||
utils_bonus.c\
|
utils_bonus.c\
|
||||||
signal_handler.c\
|
signal_handler.c\
|
||||||
parsing_bonus.c\
|
parsing_bonus.c\
|
||||||
exec_bonus.c
|
exec_bonus.c\
|
||||||
|
exec_utils.c\
|
||||||
|
free.c
|
||||||
|
|
||||||
OBJS_DIR = objs/
|
OBJS_DIR = objs/
|
||||||
SRCS_DIR = srcs/
|
SRCS_DIR = srcs/
|
||||||
|
BIN
srcs/.main.c.swp
Normal file
BIN
srcs/.main.c.swp
Normal file
Binary file not shown.
20
srcs/cd.c
20
srcs/cd.c
@ -6,7 +6,7 @@
|
|||||||
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/02/16 21:02:54 by marde-vr #+# #+# */
|
/* 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);
|
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)
|
if (arg)
|
||||||
{
|
{
|
||||||
nw_wd = arg->value;
|
nw_wd = arg->value;
|
||||||
if(!ft_strcmp("-", nw_wd))
|
if (!ft_strcmp("-", nw_wd))
|
||||||
{
|
{
|
||||||
nw_wd = ft_get_env(msh->env, "OLDPWD");
|
nw_wd = ft_get_env(msh->env, "OLDPWD");
|
||||||
if(!nw_wd)
|
if (!nw_wd)
|
||||||
ft_putstr_fd("minishell: cd: OLDPWD not set\n", 2);
|
ft_putstr_fd("minishell: cd: OLDPWD not set\n", 2);
|
||||||
if(!nw_wd)
|
if (!nw_wd)
|
||||||
return(0);
|
return (0);
|
||||||
ft_printf_fd((1 * (msh->out_fd == 0)) + msh->out_fd, "%s\n", nw_wd);
|
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)
|
if (!nw_wd)
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
return(nw_wd);
|
return (nw_wd);
|
||||||
}
|
}
|
||||||
|
|
||||||
int cd(t_token *args, t_msh *msh)
|
int cd(t_token *args, t_msh *msh)
|
||||||
@ -71,8 +71,8 @@ int cd(t_token *args, t_msh *msh)
|
|||||||
return (1);
|
return (1);
|
||||||
}
|
}
|
||||||
new_wd = get_new_wd(args->next, msh);
|
new_wd = get_new_wd(args->next, msh);
|
||||||
if(!new_wd)
|
if (!new_wd)
|
||||||
return(1);
|
return (1);
|
||||||
if (chdir(new_wd) == -1)
|
if (chdir(new_wd) == -1)
|
||||||
{
|
{
|
||||||
perror("minishell: cd");
|
perror("minishell: cd");
|
||||||
|
101
srcs/env.c
Normal file
101
srcs/env.c
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
@ -6,44 +6,12 @@
|
|||||||
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/03/28 13:50:14 by tomoron #+# #+# */
|
/* 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"
|
#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)
|
void exec_command_bonus(t_msh *msh, char *cmd_str)
|
||||||
{
|
{
|
||||||
t_cmd *cmds;
|
t_cmd *cmds;
|
||||||
@ -95,9 +63,9 @@ int exec(t_msh *msh, char **cmd_args, int i, int cmd_count)
|
|||||||
}
|
}
|
||||||
if (pid == 0)
|
if (pid == 0)
|
||||||
child(msh, cmd_args, i);
|
child(msh, cmd_args, i);
|
||||||
if(pid != 0)
|
if (pid != 0)
|
||||||
msh->pids[i] = pid;
|
msh->pids[i] = pid;
|
||||||
if(pid != 0)
|
if (pid != 0)
|
||||||
parent(msh, i, cmd_count, cmd_args);
|
parent(msh, i, cmd_count, cmd_args);
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
@ -115,63 +83,6 @@ void exec_command(t_msh *msh, int i, int cmd_count)
|
|||||||
remove_command_from_msh(msh);
|
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)
|
void end_execution(t_msh *msh, int cmd_count)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
|
102
srcs/exec_utils.c
Normal file
102
srcs/exec_utils.c
Normal 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;
|
||||||
|
}
|
@ -6,74 +6,12 @@
|
|||||||
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/02/18 18:29:20 by marde-vr #+# #+# */
|
/* 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"
|
#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)
|
int export_invalid_identifier(char *arg, char *name)
|
||||||
{
|
{
|
||||||
ft_putstr_fd("minishell: export: `", 2);
|
ft_putstr_fd("minishell: export: `", 2);
|
||||||
@ -146,31 +84,6 @@ int ft_export(t_msh *msh, t_token *cmd, t_env *env)
|
|||||||
return (0);
|
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)
|
int ft_unset(t_msh *msh)
|
||||||
{
|
{
|
||||||
t_token *cmd;
|
t_token *cmd;
|
||||||
|
45
srcs/free.c
Normal file
45
srcs/free.c
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
@ -6,7 +6,7 @@
|
|||||||
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/03/24 17:44:32 by marde-vr #+# #+# */
|
/* 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;
|
char *line;
|
||||||
|
|
||||||
line = NULL;
|
line = NULL;
|
||||||
//TODO: parse eof sans parse les variables
|
|
||||||
while (1)
|
while (1)
|
||||||
{
|
{
|
||||||
free(line);
|
free(line);
|
||||||
@ -68,6 +67,12 @@ void handle_here_doc(t_msh *msh, char *eof)
|
|||||||
int pid;
|
int pid;
|
||||||
|
|
||||||
here_doc_file = get_tmp_file_name(msh);
|
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->here_doc_filename = here_doc_file;
|
||||||
msh->in_fd = open(here_doc_file, O_CREAT | O_RDWR, 0644);
|
msh->in_fd = open(here_doc_file, O_CREAT | O_RDWR, 0644);
|
||||||
if (msh->in_fd == -1)
|
if (msh->in_fd == -1)
|
||||||
|
38
srcs/main.c
38
srcs/main.c
@ -6,7 +6,7 @@
|
|||||||
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/02/02 21:59:20 by tomoron #+# #+# */
|
/* 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);
|
(*msh)->env = add_shlvl((*msh)->env);
|
||||||
tcgetattr(1, &t_p);
|
tcgetattr(1, &t_p);
|
||||||
(*msh)->echoctl = t_p.c_lflag & ECHOCTL;
|
(*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);
|
signal(SIGQUIT, signal_handler_interactive);
|
||||||
if (set_echoctl(0))
|
if (set_echoctl(0))
|
||||||
ft_exit(*msh, 1);
|
ft_exit(*msh, 1);
|
||||||
return (0);
|
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)
|
int main(int argc, char **argv, char **envp)
|
||||||
{
|
{
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/02/04 17:31:38 by tomoron #+# #+# */
|
/* 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 out_fd;
|
||||||
int locked_return_code;
|
int locked_return_code;
|
||||||
int echoctl;
|
int echoctl;
|
||||||
char *here_doc_filename;
|
char *here_doc_filename;
|
||||||
} t_msh;
|
} t_msh;
|
||||||
|
|
||||||
extern int g_return_code;
|
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);
|
t_token *token_add_back(t_token *res, char *token);
|
||||||
void child(t_msh *msh, char **cmd_args, int i);
|
void child(t_msh *msh, char **cmd_args, int i);
|
||||||
t_token *parse_tokens(char *command, t_env *env);
|
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);
|
char *ft_get_env(t_env *env, char *var_name);
|
||||||
int is_fd_open(int fd);
|
int is_fd_open(int fd);
|
||||||
int get_out_type(t_msh *msh, t_cmd *cmds);
|
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);
|
t_token *parsing_syntax_error(t_token *res);
|
||||||
int file_access(t_msh *msh, int *found);
|
int file_access(t_msh *msh, int *found);
|
||||||
void remove_command_from_msh(t_msh *msh);
|
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 ft_exit(t_msh *msh, int error_code);
|
||||||
void sort_wildcards_token(t_token *list);
|
void sort_wildcards_token(t_token *list);
|
||||||
void redirect_input(t_msh *msh, int i, char **cmd_args);
|
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);
|
int check_var_name(char *name);
|
||||||
char **get_cmd_args(t_msh *msh);
|
char **get_cmd_args(t_msh *msh);
|
||||||
char *remove_path(char *token);
|
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_cmd *parsing_bonus(char *cmd);
|
||||||
t_token *free_token(t_token *cmd);
|
t_token *free_token(t_token *cmd);
|
||||||
void exec_commands(t_msh *msh);
|
void exec_commands(t_msh *msh);
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
/* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/04/19 14:09:44 by tomoron #+# #+# */
|
/* 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)
|
if (msh->out_type == RED_O)
|
||||||
msh->out_fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0644);
|
msh->out_fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0644);
|
||||||
if (msh->out_type == RED_O_APP)
|
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)
|
if (msh->out_fd == -1)
|
||||||
{
|
{
|
||||||
ft_putstr_fd("minishell: ", 2);
|
ft_putstr_fd("minishell: ", 2);
|
||||||
|
10
srcs/pipe.c
10
srcs/pipe.c
@ -6,7 +6,7 @@
|
|||||||
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/03/05 18:17:25 by marde-vr #+# #+# */
|
/* 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)
|
void handle_parenthesis(t_msh *msh)
|
||||||
{
|
{
|
||||||
char *command;
|
char *command;
|
||||||
|
|
||||||
command = 0;
|
command = 0;
|
||||||
if(msh->cmds->cmd_type == PAREN)
|
if (msh->cmds->cmd_type == PAREN)
|
||||||
command = ft_strdup(msh->cmds->value);
|
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);
|
command = ft_strdup(msh->cmds->next->value);
|
||||||
if(!command)
|
if (!command)
|
||||||
{
|
{
|
||||||
ft_printf_fd(2, "an error occured");
|
ft_printf_fd(2, "an error occured");
|
||||||
ft_exit(msh, 1);
|
ft_exit(msh, 1);
|
||||||
|
34
srcs/utils.c
34
srcs/utils.c
@ -6,44 +6,12 @@
|
|||||||
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/03/05 18:19:26 by marde-vr #+# #+# */
|
/* 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"
|
#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)
|
void ft_exit(t_msh *msh, int exit_code)
|
||||||
{
|
{
|
||||||
set_echoctl(msh->echoctl);
|
set_echoctl(msh->echoctl);
|
||||||
|
31
todo_list
31
todo_list
@ -1,6 +1,31 @@
|
|||||||
unset HOME
|
unset HOME
|
||||||
cd | lolcat
|
cd | lolcat
|
||||||
double here_doc and manual here_doc tests
|
$SHLVL
|
||||||
$OLDPWD and $PWD (manual tests)
|
|
||||||
|
|
||||||
|
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
|
potential ft_exit that doesnt unlink tmp here_doc files
|
||||||
$EMPTY (manual tests)
|
parse eof sans parse les variables
|
||||||
|
Reference in New Issue
Block a user