patate
This commit is contained in:
@ -6,7 +6,7 @@
|
||||
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/03/05 18:20:21 by marde-vr #+# #+# */
|
||||
/* Updated: 2024/04/23 12:50:19 by tomoron ### ########.fr */
|
||||
/* Updated: 2024/04/23 18:08:20 by tomoron ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -30,7 +30,7 @@ int cmd_is_builtin(t_msh *msh, char *cmd_token)
|
||||
&& cmd_is_forkable_builtin(cmd_token) && ft_strcmp(cmd_token, "export"))
|
||||
return (1);
|
||||
else if (!ft_strcmp(cmd_token, "cd"))
|
||||
g_return_code = cd(msh->tokens);
|
||||
g_return_code = cd(msh->tokens, msh->env, msh);
|
||||
else if (!ft_strcmp(cmd_token, "exit"))
|
||||
g_return_code = exit_bt(msh);
|
||||
else if (!ft_strcmp(cmd_token, "export") && msh->out_type != PIPE)
|
||||
|
34
srcs/cd.c
34
srcs/cd.c
@ -6,13 +6,34 @@
|
||||
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/16 21:02:54 by marde-vr #+# #+# */
|
||||
/* Updated: 2024/04/23 16:41:42 by tomoron ### ########.fr */
|
||||
/* Updated: 2024/04/23 18:34:12 by tomoron ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
|
||||
int cd(t_token *args)
|
||||
void cd_update_pwd(char *new_path, t_msh *msh)
|
||||
{
|
||||
char *pwd;
|
||||
char *new;
|
||||
|
||||
new = ft_strdup(new_path);
|
||||
if (!new)
|
||||
return ;
|
||||
pwd = ft_get_env(msh->env, "PWD");
|
||||
pwd = ft_strdup(pwd);
|
||||
if (!pwd)
|
||||
ft_strdup("");
|
||||
if (!pwd)
|
||||
free(new);
|
||||
if (!pwd)
|
||||
return ;
|
||||
msh->env = export_set_env(msh->env, ft_strdup("OLDPWD"), pwd, 0);
|
||||
if (ft_get_env(msh->env, "PWD"))
|
||||
msh->env = export_set_env(msh->env, ft_strdup("PWD"), new, 0);
|
||||
}
|
||||
|
||||
int cd(t_token *args, t_env *env, t_msh *msh)
|
||||
{
|
||||
char *new_wd;
|
||||
|
||||
@ -22,9 +43,16 @@ int cd(t_token *args)
|
||||
return (1);
|
||||
}
|
||||
if (!args->next)
|
||||
new_wd = getenv("HOME");
|
||||
{
|
||||
new_wd = ft_get_env(env, "HOME");
|
||||
if (!new_wd)
|
||||
ft_putstr_fd("minishell: cd: HOME not set\n", 2);
|
||||
if (!new_wd)
|
||||
return (1);
|
||||
}
|
||||
else
|
||||
new_wd = args->next->value;
|
||||
cd_update_pwd(new_wd, msh);
|
||||
if (chdir(new_wd) == -1)
|
||||
{
|
||||
perror("minishell: cd");
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/03/28 13:50:14 by tomoron #+# #+# */
|
||||
/* Updated: 2024/04/23 17:07:41 by tomoron ### ########.fr */
|
||||
/* Updated: 2024/04/23 18:43:24 by tomoron ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -95,12 +95,10 @@ int exec(t_msh *msh, char **cmd_args, int i, int cmd_count)
|
||||
}
|
||||
if (pid == 0)
|
||||
child(msh, cmd_args, i);
|
||||
else
|
||||
{
|
||||
parent(msh, i, cmd_count);
|
||||
if(pid != 0)
|
||||
msh->pids[i] = pid;
|
||||
free(cmd_args);
|
||||
}
|
||||
if(pid != 0)
|
||||
parent(msh, i, cmd_count, cmd_args);
|
||||
return (0);
|
||||
}
|
||||
|
||||
@ -139,9 +137,10 @@ int get_cmd_count(t_cmd *cmds)
|
||||
|
||||
int is_parenthesis(t_cmd *cmd)
|
||||
{
|
||||
if(!cmd)
|
||||
return(0);
|
||||
return(cmd->cmd_type == PAREN || (cmd->cmd_type == PIPE && cmd->next->cmd_type == PAREN));
|
||||
if (!cmd)
|
||||
return (0);
|
||||
return (cmd->cmd_type == PAREN || (cmd->cmd_type == PIPE
|
||||
&& cmd->next->cmd_type == PAREN));
|
||||
}
|
||||
|
||||
void print_signaled(int status)
|
||||
@ -186,7 +185,7 @@ void end_execution(t_msh *msh, int cmd_count)
|
||||
g_return_code = WEXITSTATUS(status);
|
||||
if (WIFSIGNALED(status))
|
||||
print_signaled(status);
|
||||
if(msh->here_doc_filename)
|
||||
if (msh->here_doc_filename)
|
||||
{
|
||||
unlink(msh->here_doc_filename);
|
||||
free(msh->here_doc_filename);
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/04 17:31:38 by tomoron #+# #+# */
|
||||
/* Updated: 2024/04/23 17:05:54 by tomoron ### ########.fr */
|
||||
/* Updated: 2024/04/23 18:42:10 by tomoron ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -36,7 +36,8 @@ typedef enum e_cmd_type
|
||||
RED_O_APP,
|
||||
HERE_DOC,
|
||||
RED_O,
|
||||
RED_I
|
||||
RED_I,
|
||||
ERR
|
||||
} t_cmd_type;
|
||||
|
||||
typedef struct s_cmd
|
||||
@ -79,6 +80,7 @@ typedef struct s_msh
|
||||
extern int g_return_code;
|
||||
|
||||
t_cmd *cmd_add_back(t_cmd *res, char *cmd, t_cmd_type type);
|
||||
t_env *export_set_env(t_env *env, char *name, char *value, int append);
|
||||
void *here_doc_variables(int write, void *data);
|
||||
int add_var_to_str(char *res, char **command, t_env *env);
|
||||
void find_cmd_path(t_msh *msh, char **paths, int *found);
|
||||
@ -91,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);
|
||||
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);
|
||||
@ -154,7 +156,7 @@ int echo(t_token *args);
|
||||
int exit_bt(t_msh *msh);
|
||||
int is_cmd_type(t_cmd *cmd);
|
||||
int is_cmd_char(char c);
|
||||
int cd(t_token *args);
|
||||
int cd(t_token *args, t_env *env, t_msh *msh);
|
||||
int pwd(void);
|
||||
|
||||
#endif
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/03/27 14:40:44 by tomoron #+# #+# */
|
||||
/* Updated: 2024/04/23 16:41:42 by tomoron ### ########.fr */
|
||||
/* Updated: 2024/04/23 18:41:13 by tomoron ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -36,28 +36,34 @@ int check_str_syntax(char *cmd)
|
||||
return (!(in_quote || in_dquote || parenthesis));
|
||||
}
|
||||
|
||||
t_cmd_type str_to_cmd_type(char *cmd)
|
||||
{
|
||||
if (*cmd == '|' && cmd[1] != '|')
|
||||
return (PIPE);
|
||||
else if (*cmd == '|' && cmd[1] == '|')
|
||||
return (OR);
|
||||
else if (*cmd == '&' && cmd[1] == '&')
|
||||
return (AND);
|
||||
else if (*cmd == '>' && cmd[1] == '>')
|
||||
return (RED_O_APP);
|
||||
else if (*cmd == '<' && cmd[1] == '<')
|
||||
return (HERE_DOC);
|
||||
else if (*cmd == '>')
|
||||
return (RED_O);
|
||||
else if (*cmd == '<')
|
||||
return (RED_I);
|
||||
else if (*cmd == '(')
|
||||
return (PAREN);
|
||||
else if (*cmd == '&')
|
||||
return (ERR);
|
||||
return (CMD);
|
||||
}
|
||||
|
||||
t_cmd_type get_cmd_type_bonus(char **cmd)
|
||||
{
|
||||
t_cmd_type res;
|
||||
|
||||
if (**cmd == '|' && (*cmd)[1] != '|')
|
||||
res = PIPE;
|
||||
else if (**cmd == '|' && (*cmd)[1] == '|')
|
||||
res = OR;
|
||||
else if (**cmd == '&' && (*cmd)[1] == '&')
|
||||
res = AND;
|
||||
else if (**cmd == '>' && (*cmd)[1] == '>')
|
||||
res = RED_O_APP;
|
||||
else if (**cmd == '<' && (*cmd)[1] == '<')
|
||||
res = HERE_DOC;
|
||||
else if (**cmd == '>')
|
||||
res = RED_O;
|
||||
else if (**cmd == '<')
|
||||
res = RED_I;
|
||||
else if (**cmd == '(')
|
||||
res = PAREN;
|
||||
else
|
||||
res = CMD;
|
||||
res = str_to_cmd_type(*cmd);
|
||||
if (res != CMD)
|
||||
(*cmd)++;
|
||||
if (res == OR || res == AND || res == RED_O_APP || res == HERE_DOC)
|
||||
@ -138,31 +144,32 @@ void print_syntax_error_bonus(t_cmd *cmd)
|
||||
ft_printf_fd(2, "||");
|
||||
if (cmd->cmd_type == PIPE)
|
||||
ft_printf_fd(2, "|");
|
||||
if (cmd->cmd_type == ERR)
|
||||
ft_printf_fd(2, "&");
|
||||
ft_printf_fd(2, "'\n");
|
||||
}
|
||||
|
||||
|
||||
int check_parens_syntax(t_cmd *cmd, t_cmd *last, t_env *env)
|
||||
{
|
||||
t_cmd *parsed_cmd;
|
||||
t_cmd *tmp;
|
||||
|
||||
if(last && is_cmd_type(last))
|
||||
if (last && is_cmd_type(last))
|
||||
{
|
||||
ft_putstr_fd("minishell: syntax error\n", 2);
|
||||
return(0);
|
||||
return (0);
|
||||
}
|
||||
parsed_cmd = parsing_bonus(cmd->value);
|
||||
if(!parsed_cmd)
|
||||
if (!parsed_cmd)
|
||||
{
|
||||
ft_putstr_fd("minishell: syntax error\n", 2);
|
||||
return(0);
|
||||
return (0);
|
||||
}
|
||||
tmp = check_cmds_syntax(parsed_cmd, env);
|
||||
if(tmp)
|
||||
if (tmp)
|
||||
print_syntax_error_bonus(tmp);
|
||||
free_cmd(parsed_cmd);
|
||||
return(tmp == 0);
|
||||
return (tmp == 0);
|
||||
}
|
||||
|
||||
int check_tokens_syntax(t_cmd *cmd, t_cmd *last, t_env *env)
|
||||
@ -188,12 +195,12 @@ int check_cmd_type_syntax(t_cmd *cmds, t_cmd *last, t_env *env)
|
||||
if (!check_tokens_syntax(cmds, last, env))
|
||||
return (0);
|
||||
}
|
||||
else if(cmds->cmd_type == PAREN)
|
||||
else if (cmds->cmd_type == PAREN)
|
||||
{
|
||||
if(!check_parens_syntax(cmds, last, env))
|
||||
return(0);
|
||||
if (!check_parens_syntax(cmds, last, env))
|
||||
return (0);
|
||||
}
|
||||
return(1);
|
||||
return (1);
|
||||
}
|
||||
|
||||
t_cmd *check_cmds_syntax(t_cmd *cmds, t_env *env)
|
||||
@ -205,19 +212,24 @@ t_cmd *check_cmds_syntax(t_cmd *cmds, t_env *env)
|
||||
if (!is_operand_type(cmds) && cmds->cmd_type != PIPE && cmds->value == 0)
|
||||
return (cmds);
|
||||
last = cmds;
|
||||
if(is_cmd_type(cmds) && !check_cmd_type_syntax(cmds, 0, env))
|
||||
return(cmds);
|
||||
if (is_cmd_type(cmds) && !check_cmd_type_syntax(cmds, 0, env))
|
||||
return (cmds);
|
||||
cmds = cmds->next;
|
||||
while (cmds)
|
||||
{
|
||||
if (cmds->cmd_type == ERR)
|
||||
return (cmds);
|
||||
if (!is_operand_type(cmds) && cmds->cmd_type != PIPE
|
||||
&& cmds->value == 0)
|
||||
return (cmds);
|
||||
if (is_operand_type(cmds) || cmds->cmd_type == PIPE)
|
||||
if ((!is_cmd_type(last) && !is_output_type(last) && !is_input_type(last)) || !cmds->next || (!is_cmd_type(cmds->next) && !is_output_type(cmds->next) && !is_input_type(cmds->next)))
|
||||
if ((!is_cmd_type(last) && !is_output_type(last)
|
||||
&& !is_input_type(last)) || !cmds->next
|
||||
|| (!is_cmd_type(cmds->next) && !is_output_type(cmds->next)
|
||||
&& !is_input_type(cmds->next)))
|
||||
return (cmds);
|
||||
if (is_cmd_type(cmds) && !check_cmd_type_syntax(cmds, last, env))
|
||||
return (cmds);
|
||||
if(is_cmd_type(cmds) && !check_cmd_type_syntax(cmds, last, env))
|
||||
return(cmds);
|
||||
last = cmds;
|
||||
cmds = cmds->next;
|
||||
}
|
||||
|
@ -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 14:47:24 by tomoron ### ########.fr */
|
||||
/* Updated: 2024/04/23 18:51:29 by tomoron ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -99,8 +99,9 @@ void child(t_msh *msh, char **cmd_args, int i)
|
||||
ft_exit(msh, g_return_code);
|
||||
}
|
||||
|
||||
void parent(t_msh *msh, int i, int cmd_count)
|
||||
void parent(t_msh *msh, int i, int cmd_count, char **cmd_args)
|
||||
{
|
||||
free(cmd_args);
|
||||
signal(SIGINT, signal_handler_command);
|
||||
signal(SIGQUIT, signal_handler_command);
|
||||
if (i != 0)
|
||||
|
Reference in New Issue
Block a user