fixed norme

This commit is contained in:
mdev9
2024-04-24 10:45:19 +02:00
parent 8becb3f468
commit ab5190ee8f
7 changed files with 40 additions and 74 deletions

View File

@ -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");

View File

@ -6,7 +6,7 @@
/* 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:44:54 by marde-vr ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -95,9 +95,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);
} }

View File

@ -6,7 +6,7 @@
/* 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/23 12:49:46 by tomoron ### ########.fr */ /* Updated: 2024/04/24 10:42:54 by marde-vr ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -14,31 +14,31 @@
t_env *dup_env(t_env *env) t_env *dup_env(t_env *env)
{ {
t_env *res; t_env *res;
res = 0; res = 0;
while(env) while (env)
{ {
res = env_add_back(res, env->name, env->value); res = env_add_back(res, env->name, env->value);
env = env->next; env = env->next;
} }
return(res); return (res);
} }
void sort_env(t_env *env) void sort_env(t_env *env)
{ {
t_env *tmp; t_env *tmp;
t_env *start; t_env *start;
char *tmp_str; char *tmp_str;
tmp = env; tmp = env;
start = env; start = env;
while(tmp) while (tmp)
{ {
env = start; env = start;
while(env) while (env)
{ {
if(ft_strcmp(tmp->name, env->name) < 0) if (ft_strcmp(tmp->name, env->name) < 0)
{ {
tmp_str = tmp->name; tmp_str = tmp->name;
tmp->name = env->name; tmp->name = env->name;
@ -55,18 +55,19 @@ void sort_env(t_env *env)
void print_env_declare(t_msh *msh, t_env *env_orig) void print_env_declare(t_msh *msh, t_env *env_orig)
{ {
t_env *env; t_env *env;
env = dup_env(env_orig); env = dup_env(env_orig);
sort_env(env); sort_env(env);
if(!msh->out_fd) if (!msh->out_fd)
msh->out_fd = 1; msh->out_fd = 1;
while (env) while (env)
{ {
if (strcmp(env->name, "_")) if (strcmp(env->name, "_"))
{ {
if (env->value && *(env->value)) if (env->value && *(env->value))
ft_printf_fd(msh->out_fd, "declare -x %s=\"%s\"\n", env->name, env->value); ft_printf_fd(msh->out_fd, "declare -x %s=\"%s\"\n", env->name,
env->value);
else else
ft_printf_fd(msh->out_fd, "declare -x %s\n", env->name); ft_printf_fd(msh->out_fd, "declare -x %s\n", env->name);
} }

View File

@ -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/24 09:57:20 by marde-vr ### ########.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);

View File

@ -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/23 14:33:16 by tomoron ### ########.fr */ /* Updated: 2024/04/24 10:43:27 by marde-vr ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -83,46 +83,12 @@ int init_minishell(t_msh **msh, int argc, char **argv, char **envp)
(*msh)->env = get_env(envp); (*msh)->env = get_env(envp);
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)
{ {

View File

@ -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:41:34 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);

View File

@ -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);
@ -57,7 +57,7 @@ void handle_parenthesis(t_msh *msh)
void execute_command(t_msh *msh, char **cmd_args) void execute_command(t_msh *msh, char **cmd_args)
{ {
char **env; char **env;
if (is_parenthesis(msh->cmds)) if (is_parenthesis(msh->cmds))
{ {
free(cmd_args); free(cmd_args);