export et autre

This commit is contained in:
2024-04-21 23:54:15 +02:00
parent a42ddc1955
commit 51934faa06
8 changed files with 80 additions and 60 deletions

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/05 18:20:21 by marde-vr #+# #+# */
/* Updated: 2024/04/19 19:36:51 by marde-vr ### ########.fr */
/* Updated: 2024/04/21 23:25:05 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
@ -39,8 +39,7 @@ int cmd_is_builtin(t_msh *msh, char *cmd_token)
}
else if (!ft_strcmp(cmd_token, "export"))
{
if (!(/*msh->in_type == PIPE || msh->out_type == PIPE*/ 0))
g_return_code = ft_export(msh);
g_return_code = ft_export(msh->tokens, msh->env);
return (1);
}
else if (!ft_strcmp(cmd_token, "unset"))

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/28 13:50:14 by tomoron #+# #+# */
/* Updated: 2024/04/19 20:50:11 by tomoron ### ########.fr */
/* Updated: 2024/04/21 21:52:10 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
@ -142,32 +142,29 @@ int get_cmd_count(t_cmd *cmds)
void print_signaled(int status)
{
int signal;
int print;
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",\
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"};
signal = WTERMSIG(status);
print = 0;
if(signal < 31 && sigmsg[signal])
{
ft_putstr_fd((char *)sigmsg[signal], 2);
print = 1;
}
if(signal >= 34 && signal <= 64)
{
ft_putstr_fd("Real-time signal ", 2);
ft_putnbr_fd(signal - 34, 2);
print = 1;
}
if(print)
ft_putstr_fd("\n", 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)
@ -178,20 +175,26 @@ void end_execution(t_msh *msh, int cmd_count)
i = 0;
while (i < cmd_count)
waitpid(msh->pids[i++], &status, 0);
if (!g_return_code && WIFEXITED(status))
if (WIFEXITED(status))
g_return_code = WEXITSTATUS(status);
if (WIFSIGNALED(status))
print_signaled(status);
// TODO: (core dumped) WCOREDUMP
free(msh->pids);
free_fds(msh);
msh->pids = 0;
free(msh->fds);
// signal(SIGINT, signal_handler_interactive); //enables ctrl-C
signal(SIGINT, signal_handler_interactive); //enables ctrl-C
signal(SIGQUIT, signal_handler_interactive);
set_echoctl(0);
}
int handle_parenthesis(t_msh *msh)
{
if(!(msh->cmds->cmd_type == PAREN || (msh->cmds->cmd_type == PIPE && msh->cmds->next->cmd_type == PAREN)))
return(0);
return(1);
}
void exec_commands(t_msh *msh)
{
@ -201,7 +204,6 @@ void exec_commands(t_msh *msh)
if (!msh->tokens)
return ;
cmd_count = get_cmd_count(msh->cmds);
//printf("cmd_count : %d\n", cmd_count);
msh->fds = ft_calloc(cmd_count + 1, sizeof(int **));
msh->pids = ft_calloc(cmd_count, sizeof(int *));
if (!msh->pids || !msh->fds)
@ -210,11 +212,8 @@ void exec_commands(t_msh *msh)
while (i < cmd_count)
{
get_redirections(msh, msh->cmds);
//fprintf(stderr, "command: %s, in_type: %d, out_type: %d\n", msh->cmds->value, msh->in_type, msh->out_type);
exec_command(msh, i, cmd_count);
//free(msh->fds[i]);
i++;
}
end_execution(msh, cmd_count);
}

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/18 18:29:20 by marde-vr #+# #+# */
/* Updated: 2024/04/19 19:55:18 by tomoron ### ########.fr */
/* Updated: 2024/04/21 23:46:43 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
@ -18,7 +18,7 @@ void print_env_declare(t_env *env)
{
if (strcmp(env->name, "_"))
{
if (*(env->value))
if (env->value && *(env->value))
printf("declare -x %s=\"%s\"\n", env->name, env->value);
else
printf("declare -x %s\n", env->name);
@ -36,33 +36,65 @@ int export_invalid_identifier(char *arg, char *name)
return (1);
}
int ft_export(t_msh *msh)
t_env *export_set_env(t_env *env, char *name, char *value, int append)
{
t_env *tmp;
tmp = env;
if(!value)
{
free(name);
ft_printf_fd(2, "minishell: malloc failed");
return(env);
}
while(tmp)
{
if(!ft_strcmp(name, tmp->name))
{
free(name);
if(!*value)
{
free(value);
return(env);
}
if(append)
value = ft_strjoin_free(tmp->value, value, 2);
if(!value)
return(env);
free(tmp->value);
tmp->value = value;
return(env);
}
tmp = tmp->next;
}
return(env_add_back(env, name, value));
}
int ft_export(t_token *cmd, t_env *env)
{
t_token *cmd;
char *arg;
char *name;
char *value;
int len;
int append;
cmd = msh->tokens;
if (cmd && !cmd->next)
print_env_declare(msh->env);
print_env_declare(env);
if (cmd && cmd->next && !cmd->next->next)
{
arg = cmd->next->value;
len = 0;
while (arg[len] && arg[len] != '=')
while (arg[len] && arg[len] != '=' && arg[len] != '+')
len++;
name = ft_substr(arg, 0, len);
if (!name || !check_var_name(name))
append = arg[len] == '+';
if (arg[len] == '=' || (arg[len] == '+' && arg[len + 1] == '='))
len += 1 + (arg[len] == '+');
if (!name || !check_var_name(name) || arg[len] == '+')
return (export_invalid_identifier(arg, name));
if (arg[len])
len++;
value = ft_strdup(arg + len);
msh->env = env_add_back(msh->env, name, value);
env = export_set_env(env, name, value, append);
}
//export +=
//replacer si ça existe deja sauf si il y a pas de =
return (0);
}

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/19 18:49:27 by marde-vr ### ########.fr */
/* Updated: 2024/04/21 23:24:35 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
@ -140,7 +140,7 @@ int set_echoctl(int value);
int is_output_type(t_cmd *cmd);
int print_env(t_env *env);
t_cmd *free_cmd(t_cmd *cmd);
int ft_export(t_msh *msh);
int ft_export(t_token *cmd, t_env *env);
int is_input_type(t_cmd *cmd);
void free_env(t_env *env);
int ft_unset(t_msh *msh);

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/19 20:00:09 by tomoron ### ########.fr */
/* Updated: 2024/04/21 23:49:47 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
@ -42,7 +42,10 @@ void execute_command(t_msh *msh, char **cmd_args)
set_echoctl(msh->echoctl);
env = env_to_char_tab(msh->env);
if (env)
execve(msh->tokens->value, cmd_args, env);
{
if(execve(msh->tokens->value, cmd_args, env))
perror("execve");
}
ft_free_str_arr(env);
}
}

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/22 14:31:13 by tomoron #+# #+# */
/* Updated: 2024/04/18 20:57:35 by marde-vr ### ########.fr */
/* Updated: 2024/04/21 20:22:09 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
@ -58,12 +58,10 @@ int set_echoctl(int value)
struct termios t_p;
//ft_printf("echoctl value : %d\n",value);
if(!isatty(1))
return(0);
if (tcgetattr(1, &t_p))
{
//fprintf(stderr, "minishell: an error occured while getting the local fl\
//ags\n");
return (1);
}
if (((t_p.c_lflag & ECHOCTL) != 0) == value)
return (0);
//ft_printf("change\n");
@ -72,11 +70,7 @@ int set_echoctl(int value)
else
t_p.c_lflag = t_p.c_lflag & (~ECHOCTL);
if (tcsetattr(1, TCSANOW, &t_p))
{
//fprintf(stderr, "minishell: an error occured while setting the local fl\
//ags\n");
return (1);
}
return (0);
}

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/05 18:19:26 by marde-vr #+# #+# */
/* Updated: 2024/04/19 18:52:13 by marde-vr ### ########.fr */
/* Updated: 2024/04/21 21:56:47 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
@ -39,7 +39,6 @@ void free_msh(t_msh *msh)
free_cmd(msh->cmds_head);
free_fds(msh);
free_token(msh->tokens);
set_echoctl(msh->echoctl);
free(msh);
}
}
@ -48,6 +47,7 @@ void ft_exit(t_msh *msh, int exit_code)
{
//ft_printf("exiting");
free_msh(msh);
set_echoctl(msh->echoctl);
exit(exit_code);
}

View File

@ -1,10 +1,3 @@
"(core dumped)" si WCOREDUMP
export +=
export sort
here doc in fork
pipe with parenthesis
LEAKS
add cmds_head in msh_struct to free in runtime