organised functions into multiple files and did some more norming

This commit is contained in:
mdev9
2024-04-24 11:06:03 +02:00
parent ab5190ee8f
commit 20037a4609
10 changed files with 276 additions and 223 deletions

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/24 10:44:54 by marde-vr ### ########.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;
@ -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,75 +6,12 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/18 18:29:20 by marde-vr #+# #+# */
/* Updated: 2024/04/24 10:42:54 by marde-vr ### ########.fr */
/* Updated: 2024/04/24 10:59:52 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;
}
}
int export_invalid_identifier(char *arg, char *name)
{
ft_putstr_fd("minishell: export: `", 2);
@ -146,31 +83,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/02/04 17:31:38 by tomoron #+# #+# */
/* Updated: 2024/04/24 10:41:34 by marde-vr ### ########.fr */
/* Updated: 2024/04/24 10:59:46 by marde-vr ### ########.fr */
/* */
/* ************************************************************************** */
@ -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,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);