modified cmds to tokens

This commit is contained in:
mdev9
2024-04-01 13:59:34 +02:00
parent e05465348d
commit 7d1fba69a9
10 changed files with 53 additions and 53 deletions

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:20:21 by marde-vr #+# #+# */ /* Created: 2024/03/05 18:20:21 by marde-vr #+# #+# */
/* Updated: 2024/03/30 18:40:49 by marde-vr ### ########.fr */ /* Updated: 2024/04/01 13:22:14 by marde-vr ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -27,7 +27,7 @@ int cmd_is_builtin(t_msh *msh, char *cmd_token)
return (0); return (0);
else if (!ft_strcmp(cmd_token, "cd")) else if (!ft_strcmp(cmd_token, "cd"))
{ {
cd(msh->tokens); cd(msh->cmds);
return (1); return (1);
} }
else if (!ft_strcmp(cmd_token, "exit")) else if (!ft_strcmp(cmd_token, "exit"))
@ -54,7 +54,7 @@ int exec_builtin(t_msh *msh)
if (!msh->cmds->value) if (!msh->cmds->value)
return (0); return (0);
if (!ft_strcmp(msh->cmds->value, "echo")) if (!ft_strcmp(msh->cmds->value, "echo"))
g_return_code = echo(msh->tokens->next); g_return_code = echo(msh->cmds->next);
else if (!ft_strcmp(msh->cmds->value, "ret")) else if (!ft_strcmp(msh->cmds->value, "ret"))
g_return_code = ft_atoi(msh->cmds->next->value); g_return_code = ft_atoi(msh->cmds->next->value);
else if (!ft_strcmp(msh->cmds->value, "env")) else if (!ft_strcmp(msh->cmds->value, "env"))
@ -64,7 +64,7 @@ int exec_builtin(t_msh *msh)
else if (!ft_strcmp(msh->cmds->value, "pwd")) else if (!ft_strcmp(msh->cmds->value, "pwd"))
g_return_code = pwd(); g_return_code = pwd();
else if (!ft_strcmp(msh->cmds->value, "cd")) else if (!ft_strcmp(msh->cmds->value, "cd"))
g_return_code = cd(msh->tokens); g_return_code = cd(msh->cmds);
else else
return (0); return (0);
return (1); return (1);

View File

@ -6,45 +6,45 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */ /* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/05 18:22:15 by marde-vr #+# #+# */ /* Created: 2024/03/05 18:22:15 by marde-vr #+# #+# */
/* Updated: 2024/03/30 17:25:20 by marde-vr ### ########.fr */ /* Updated: 2024/04/01 13:27:40 by marde-vr ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
int get_cmd_count(t_cmd *cmds) int get_cmd_count(t_token *cmds)
{ {
int count; int count;
t_cmd *cur_cmd; t_token *cur_cmd;
(void)cmds;
count = 0; count = 0;
cur_cmd = cmds; cur_cmd = cmds;
while (cur_cmd->next != 0) while (cur_cmd->next != 0)
{ {
if (cur_cmd->cmd_type != PIPE) if (cur_cmd->type != PIPE)
count++; count++;
cur_cmd = cur_cmd->next; cur_cmd = cur_cmd->next;
} }
if (cur_cmd->cmd_type != PIPE) if (cur_cmd->type != PIPE)
count++; count++;
return (count); return (count);
} }
int get_args_count(t_cmd *cmds) int get_args_count(t_token *cmds)
{ {
int count; int count;
t_cmd *cur_cmd; t_token *cur_cmd;
count = 0; count = 0;
cur_cmd = cmds; cur_cmd = cmds;
if (cur_cmd->cmd_type == ARG) if (cur_cmd->type == ARG)
count++; count++;
while (cur_cmd->next) while (cur_cmd->next)
{ {
if (/*cur_cmd->type == PIPE*/ 0) if (/*cur_cmd->type == PIPE*/ 0)
break ; break ;
cur_cmd = cur_cmd->next; cur_cmd = cur_cmd->next;
if (cur_cmd->cmd_type == ARG) if (cur_cmd->type == ARG)
count++; count++;
else if (/*cur_cmd->type != PIPE*/ 1) else if (/*cur_cmd->type != PIPE*/ 1)
cur_cmd = cur_cmd->next; cur_cmd = cur_cmd->next;
@ -55,7 +55,7 @@ int get_args_count(t_cmd *cmds)
char **get_cmd_args(t_msh *msh) char **get_cmd_args(t_msh *msh)
{ {
char **cmd_args; char **cmd_args;
t_cmd *cur_cmd; t_token *cur_cmd;
int args_count; int args_count;
int i; int i;
@ -67,7 +67,7 @@ char **get_cmd_args(t_msh *msh)
i = 0; i = 0;
while (i < args_count) while (i < args_count)
{ {
if (cur_cmd->cmd_type == ARG) if (cur_cmd->type == ARG)
{ {
if (!i) if (!i)
cmd_args[i++] = remove_path(cur_cmd->value); cmd_args[i++] = remove_path(cur_cmd->value);
@ -83,8 +83,8 @@ char **get_cmd_args(t_msh *msh)
void remove_command_from_msh(t_msh *msh) void remove_command_from_msh(t_msh *msh)
{ {
t_cmd *cur_cmd; t_token *cur_cmd;
t_cmd *cmd_tmp; t_token *cmd_tmp;
cur_cmd = msh->cmds; cur_cmd = msh->cmds;
while (cur_cmd && cur_cmd->next) while (cur_cmd && cur_cmd->next)
@ -93,7 +93,7 @@ void remove_command_from_msh(t_msh *msh)
{ {
cmd_tmp = cur_cmd; cmd_tmp = cur_cmd;
cur_cmd = cur_cmd->next; cur_cmd = cur_cmd->next;
//msh->in_type = cmd_tmp->cmd_type; //msh->in_type = cmd_tmp->type;
free(cmd_tmp->value); free(cmd_tmp->value);
free(cmd_tmp); free(cmd_tmp);
msh->cmds = cur_cmd; msh->cmds = cur_cmd;

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/01 11:16:05 by marde-vr ### ########.fr */ /* Updated: 2024/04/01 13:58:58 by marde-vr ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -16,7 +16,6 @@ void exec_command_bonus(t_msh *msh, char *cmd_str)
{ {
t_cmd *cmds; t_cmd *cmds;
(void)msh;
//printf("cmd : %s\n",cmd_str); //printf("cmd : %s\n",cmd_str);
cmds = parsing_bonus(cmd_str); cmds = parsing_bonus(cmd_str);
//printf("%p\n", cmds); //printf("%p\n", cmds);
@ -24,25 +23,27 @@ void exec_command_bonus(t_msh *msh, char *cmd_str)
{ {
if (cmds->cmd_type == CMD) if (cmds->cmd_type == CMD)
{ {
msh->tokens = parse_command(cmds->value, msh->env); if (cmds->next->cmd_type == PIPE)
{
}
msh->cmds = parse_command(cmds->value, msh->env);
print_parsed_cmd(cmds); print_parsed_cmd(cmds);
print_parsed_token(msh->tokens); print_parsed_token(msh->cmds);
msh->cmds = cmds;
exec_commands(msh); exec_commands(msh);
} }
cmds = cmds->next; cmds = cmds->next;
} }
if (cmds && cmds->cmd_type == CMD) if (cmds && cmds->cmd_type == CMD)
{ {
msh->tokens = parse_command(cmds->value, msh->env); msh->cmds = parse_command(cmds->value, msh->env);
print_parsed_cmd(cmds); print_parsed_cmd(cmds);
print_parsed_token(msh->tokens); print_parsed_token(msh->cmds);
msh->cmds = cmds;
exec_commands(msh); exec_commands(msh);
} }
} }
int exec(t_msh *msh, char **cmd_args, int i, int cmd_count, int is_pipe) int exec(t_msh *msh, char **cmd_args, int i, int cmd_count)
{ {
pid_t pid; pid_t pid;
@ -61,7 +62,7 @@ int exec(t_msh *msh, char **cmd_args, int i, int cmd_count, int is_pipe)
ft_exit(msh, 1); ft_exit(msh, 1);
} }
if (pid == 0) if (pid == 0)
child(msh, cmd_args, i, is_pipe); child(msh, cmd_args, i);
else else
{ {
parent(msh, i, cmd_count); parent(msh, i, cmd_count);
@ -79,19 +80,19 @@ void exec_command(t_msh *msh, int i, int cmd_count)
ft_exit(msh, 1); ft_exit(msh, 1);
if (first_is_in_type(msh)) if (first_is_in_type(msh))
{ {
get_in_type(msh, msh->tokens); get_in_type(msh, msh->cmds);
if (!g_return_code) if (!g_return_code)
get_out_type(msh, msh->tokens); get_out_type(msh, msh->cmds);
} }
else else
{ {
get_out_type(msh, msh->tokens); get_out_type(msh, msh->cmds);
if (!g_return_code) if (!g_return_code)
get_in_type(msh, msh->tokens); get_in_type(msh, msh->cmds);
} }
if (!cmd_is_builtin(msh, msh->cmds->value)) if (!cmd_is_builtin(msh, msh->cmds->value))
get_cmd_path(msh); get_cmd_path(msh);
exec(msh, get_cmd_args(msh), i, cmd_count, 1); exec(msh, get_cmd_args(msh), i, cmd_count);
remove_command_from_msh(msh); remove_command_from_msh(msh);
} }

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */ /* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/07 16:04:11 by tomoron #+# #+# */ /* Created: 2024/02/07 16:04:11 by tomoron #+# #+# */
/* Updated: 2024/03/30 18:41:32 by marde-vr ### ########.fr */ /* Updated: 2024/04/01 13:19:41 by marde-vr ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -24,7 +24,7 @@ void get_exit_bt_return_code(t_msh *msh, int *exit_code)
{ {
t_token *cur_cmd; t_token *cur_cmd;
cur_cmd = msh->tokens->next; cur_cmd = msh->cmds->next;
if (cur_cmd && cur_cmd->type == ARG && !ft_strisnbr(cur_cmd->value)) if (cur_cmd && cur_cmd->type == ARG && !ft_strisnbr(cur_cmd->value))
numeric_arg_err(cur_cmd->value, exit_code); numeric_arg_err(cur_cmd->value, exit_code);
else if (cur_cmd && cur_cmd->type == ARG) else if (cur_cmd && cur_cmd->type == ARG)
@ -39,7 +39,7 @@ int exit_bt(t_msh *msh)
int exit_code; int exit_code;
int cmd_count; int cmd_count;
cur_cmd = msh->tokens->next; cur_cmd = msh->cmds->next;
ft_printf("exit\n"); ft_printf("exit\n");
if (cur_cmd && cur_cmd->next && cur_cmd->next->type == ARG if (cur_cmd && cur_cmd->next && cur_cmd->next->type == ARG
&& ft_strisnbr(cur_cmd->value)) && ft_strisnbr(cur_cmd->value))

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/03/30 18:45:02 by marde-vr ### ########.fr */ /* Updated: 2024/04/01 13:20:34 by marde-vr ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -44,7 +44,7 @@ int ft_export(t_msh *msh)
char *value; char *value;
int len; int len;
cmd = msh->tokens; cmd = msh->cmds;
if (cmd && (!cmd->next || (cmd->next && cmd->next->type != ARG))) if (cmd && (!cmd->next || (cmd->next && cmd->next->type != ARG)))
print_env_declare(msh->env); print_env_declare(msh->env);
if (cmd && cmd->next && cmd->next->type == ARG && (!cmd->next->next if (cmd && cmd->next && cmd->next->type == ARG && (!cmd->next->next
@ -94,7 +94,7 @@ int ft_unset(t_msh *msh)
{ {
t_token *cmd; t_token *cmd;
cmd = msh->tokens; cmd = msh->cmds;
if (cmd) if (cmd)
cmd = cmd->next; cmd = cmd->next;
while (cmd && cmd->type == ARG) while (cmd && cmd->type == ARG)

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:15:27 by marde-vr #+# #+# */ /* Created: 2024/03/05 18:15:27 by marde-vr #+# #+# */
/* Updated: 2024/03/30 17:09:09 by marde-vr ### ########.fr */ /* Updated: 2024/04/01 13:20:54 by marde-vr ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -69,7 +69,7 @@ int first_is_in_type(t_msh *msh)
{ {
t_token *cur_token; t_token *cur_token;
cur_token = msh->tokens; cur_token = msh->cmds;
while (cur_token && cur_token->type == ARG && cur_token->next) while (cur_token && cur_token->type == ARG && cur_token->next)
cur_token = cur_token->next; cur_token = cur_token->next;
if (cur_token->type == RED_I || cur_token->type == HERE_DOC) if (cur_token->type == RED_I || cur_token->type == HERE_DOC)

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/03/30 17:15:14 by marde-vr ### ########.fr */ /* Updated: 2024/04/01 13:24:39 by marde-vr ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -68,8 +68,7 @@ typedef struct s_env
typedef struct s_msh typedef struct s_msh
{ {
t_env *env; t_env *env;
t_cmd *cmds; t_token *cmds;
t_token *tokens;
int **fds; int **fds;
int *pids; int *pids;
t_token_type in_type; t_token_type in_type;
@ -116,10 +115,10 @@ void print_parsed_token(t_token *cmd);//debug
int get_var_name_len(char *command); int get_var_name_len(char *command);
void handle_minishellrc(t_msh *msh); void handle_minishellrc(t_msh *msh);
char *get_tmp_file_name(t_msh *msh); char *get_tmp_file_name(t_msh *msh);
int get_args_count(t_cmd *cmds); int get_args_count(t_token *cmds);
char **env_to_char_tab(t_env *env); char **env_to_char_tab(t_env *env);
void print_parsed_cmd(t_cmd *cmd);//debug void print_parsed_cmd(t_cmd *cmd);//debug
int get_cmd_count(t_cmd *cmds); int get_cmd_count(t_token *cmds);
int first_is_in_type(t_msh *msh); int first_is_in_type(t_msh *msh);
int contains_newline(char *str); int contains_newline(char *str);
int check_var_name(char *name); int check_var_name(char *name);

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */ /* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/05 19:10:52 by marde-vr #+# #+# */ /* Created: 2024/03/05 19:10:52 by marde-vr #+# #+# */
/* Updated: 2024/03/30 18:44:30 by marde-vr ### ########.fr */ /* Updated: 2024/04/01 13:21:28 by marde-vr ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -58,9 +58,9 @@ void get_out_type(t_msh *msh, t_token *cmds)
msh->out_type = ARG; msh->out_type = ARG;
msh->out_fd = 0; msh->out_fd = 0;
cur_cmd = cmds; cur_cmd = cmds;
if (cmds->type && msh->tokens == cmds) if (cmds->type && msh->cmds == cmds)
{ {
while (msh->cmds->cmd_type != ARG && msh->cmds->next->next) while (msh->cmds->type != ARG && msh->cmds->next->next)
msh->cmds = msh->cmds->next->next; msh->cmds = msh->cmds->next->next;
} }
while (cur_cmd && cur_cmd->next && (cur_cmd->type == ARG while (cur_cmd && cur_cmd->next && (cur_cmd->type == ARG

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/03/28 13:18:35 by tomoron ### ########.fr */ /* Updated: 2024/04/01 13:59:08 by marde-vr ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

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:19:26 by marde-vr #+# #+# */ /* Created: 2024/03/05 18:19:26 by marde-vr #+# #+# */
/* Updated: 2024/03/30 18:45:27 by marde-vr ### ########.fr */ /* Updated: 2024/04/01 13:28:00 by marde-vr ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -19,7 +19,7 @@ void free_msh(t_msh *msh)
free_env(msh->env); free_env(msh->env);
free(msh->pids); free(msh->pids);
free(msh->fds); free(msh->fds);
free_token(msh->tokens); free_token(msh->cmds);
set_echoctl(msh->echoctl); set_echoctl(msh->echoctl);
free(msh); free(msh);
} }