wip (ça compile pas)

This commit is contained in:
2024-04-02 12:32:18 +02:00
parent 68469542ff
commit 5580852791
14 changed files with 155 additions and 184 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/03/27 16:21:48 by tomoron ### ########.fr */ /* Updated: 2024/04/02 00:43:31 by tomoron ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -16,13 +16,13 @@ int cd(t_token *args)
{ {
char *new_wd; char *new_wd;
if (args->next && args->next->next && args->next->next->type == ARG) if (args->next && args->next->next)
{ {
ft_printf_fd(2, "minishell: cd: too many arguments\n"); ft_printf_fd(2, "minishell: cd: too many arguments\n");
g_return_code = 1; g_return_code = 1;
return (1); return (1);
} }
if (!args->next || args->next->type != ARG) if (!args->next)
new_wd = getenv("HOME"); new_wd = getenv("HOME");
else else
new_wd = args->next->value; new_wd = args->next->value;

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:22:15 by marde-vr #+# #+# */ /* Created: 2024/03/05 18:22:15 by marde-vr #+# #+# */
/* Updated: 2024/04/01 20:01:21 by marde-vr ### ########.fr */ /* Updated: 2024/04/02 02:02:02 by tomoron ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -19,13 +19,13 @@ int get_cmd_count(t_token *cmds)
count = 0; count = 0;
cur_cmd = cmds; cur_cmd = cmds;
while (cur_cmd->next != 0) while (cur_cmd->next)
{ {
if (cur_cmd->type != PIPE) if (/*cur_cmd->type != PIPE*/ 1)
count++; count++;
cur_cmd = cur_cmd->next; cur_cmd = cur_cmd->next;
} }
if (cur_cmd->type != PIPE) if (/*cur_cmd->type != PIPE */ 1)
count++; count++;
return (count); return (count);
} }
@ -37,17 +37,12 @@ int get_args_count(t_token *cmds)
count = 0; count = 0;
cur_cmd = cmds; cur_cmd = cmds;
if (cur_cmd->type == ARG) if (cur_cmd)
count++; count++;
while (cur_cmd->next) while (cur_cmd->next)
{ {
if (/*cur_cmd->type == PIPE*/ 0)
break ;
cur_cmd = cur_cmd->next; cur_cmd = cur_cmd->next;
if (cur_cmd->type == ARG)
count++; count++;
else if (/*cur_cmd->type != PIPE*/ 1)
cur_cmd = cur_cmd->next;
} }
return (count); return (count);
} }
@ -67,7 +62,7 @@ char **get_cmd_args(t_msh *msh)
i = 0; i = 0;
while (i < args_count) while (i < args_count)
{ {
if (cur_cmd->type == ARG) if (cur_cmd)
{ {
if (!i) if (!i)
cmd_args[i++] = remove_path(cur_cmd->value); cmd_args[i++] = remove_path(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 15:46:50 by tomoron #+# #+# */ /* Created: 2024/02/18 15:46:50 by tomoron #+# #+# */
/* Updated: 2024/04/01 20:07:08 by tomoron ### ########.fr */ /* Updated: 2024/04/02 00:45:25 by tomoron ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -17,17 +17,7 @@ void print_parsed_token(t_token *token)
while (token) while (token)
{ {
printf("["); printf("[");
if (token->type == ARG)
printf("ARG : \"%s\"", token->value); printf("ARG : \"%s\"", token->value);
else if (token->type == RED_O)
printf("RED_O");
else if (token->type == RED_O_APP)
printf("RED_O_APP");
else if (token->type == RED_I)
printf("RED_I");
else if (token->type == HERE_DOC)
printf("HERE_DOC");
printf("] ");
token = token->next; token = token->next;
} }
printf("\n"); printf("\n");

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */ /* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/07 15:30:37 by tomoron #+# #+# */ /* Created: 2024/02/07 15:30:37 by tomoron #+# #+# */
/* Updated: 2024/03/28 13:21:05 by tomoron ### ########.fr */ /* Updated: 2024/04/02 00:44:56 by tomoron ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -18,17 +18,12 @@ void put_args(t_token *args)
int first; int first;
first = 1; first = 1;
while (args && args->type == ARG) while (args)
{
if (args->type != ARG)
args = args->next;
else
{ {
if (!first) if (!first)
ft_putchar_fd(' ', STDOUT_FILENO); ft_putchar_fd(' ', STDOUT_FILENO);
ft_putstr_fd(args->value, STDOUT_FILENO); ft_putstr_fd(args->value, STDOUT_FILENO);
first = 0; first = 0;
}
args = args->next; args = args->next;
} }
} }

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 21:56:12 by tomoron ### ########.fr */ /* Updated: 2024/04/02 00:09:50 by tomoron ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -118,6 +118,7 @@ void end_execution(t_msh *msh, int cmd_count)
g_return_code = WEXITSTATUS(status); g_return_code = WEXITSTATUS(status);
if (WIFSIGNALED(status) && WTERMSIG(status) == SIGQUIT) if (WIFSIGNALED(status) && WTERMSIG(status) == SIGQUIT)
printf("Quit (core dumped)\n"); printf("Quit (core dumped)\n");
//TODO: (core dumped) WCOREDUMP
free(msh->pids); free(msh->pids);
msh->pids = 0; msh->pids = 0;
//signal(SIGINT, signal_handler_interactive); //enables ctrl-C //signal(SIGINT, signal_handler_interactive); //enables ctrl-C

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/04/01 20:07:47 by marde-vr ### ########.fr */ /* Updated: 2024/04/02 00:42:19 by tomoron ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -25,9 +25,9 @@ void get_exit_bt_return_code(t_msh *msh, int *exit_code)
t_token *cur_cmd; t_token *cur_cmd;
cur_cmd = msh->cmds->next; cur_cmd = msh->cmds->next;
if (cur_cmd && cur_cmd->type == ARG && !ft_strisnbr(cur_cmd->value)) if (cur_cmd && !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)
*exit_code = (unsigned char)ft_atoi(cur_cmd->value); *exit_code = (unsigned char)ft_atoi(cur_cmd->value);
else else
*exit_code = g_return_code; *exit_code = g_return_code;
@ -40,8 +40,7 @@ int exit_bt(t_msh *msh)
cur_cmd = msh->cmds->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 && ft_strisnbr(cur_cmd->value))
&& ft_strisnbr(cur_cmd->value))
ft_putstr_fd("minishell: exit: too many arguments\n", 2); ft_putstr_fd("minishell: exit: too many arguments\n", 2);
else else
{ {

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/01 13:20:34 by marde-vr ### ########.fr */ /* Updated: 2024/04/02 02:16:18 by tomoron ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -45,10 +45,9 @@ int ft_export(t_msh *msh)
int len; int len;
cmd = msh->cmds; cmd = msh->cmds;
if (cmd && (!cmd->next || (cmd->next && cmd->next->type != ARG))) if (cmd && cmd->next)
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->next)
|| (cmd->next->next && cmd->next->next->type != ARG)))
{ {
arg = cmd->next->value; arg = cmd->next->value;
len = 0; len = 0;
@ -62,6 +61,8 @@ int ft_export(t_msh *msh)
value = ft_strdup(arg + len); value = ft_strdup(arg + len);
msh->env = env_add_back(msh->env, name, value); msh->env = env_add_back(msh->env, name, value);
} }
//export +=
//replacer si ça existe deja sauf si il y a pas de =
return (0); return (0);
} }
@ -97,7 +98,7 @@ int ft_unset(t_msh *msh)
cmd = msh->cmds; cmd = msh->cmds;
if (cmd) if (cmd)
cmd = cmd->next; cmd = cmd->next;
while (cmd && cmd->type == ARG) while (cmd)
{ {
delete_from_env(msh, cmd->value); delete_from_env(msh, cmd->value);
cmd = cmd->next; cmd = cmd->next;

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/04/01 20:08:48 by marde-vr ### ########.fr */ /* Updated: 2024/04/02 02:13:50 by tomoron ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -27,11 +27,11 @@ void redirect_input(t_msh *msh)
} }
} }
void open_input_file(t_msh *msh, t_token **cur_token) void open_input_file(t_msh *msh, t_cmd **cur_token)
{ {
if ((*cur_token)->type == HERE_DOC) if ((*cur_token)->cmd_type == HERE_DOC)
handle_here_doc(msh, (*cur_token)->next->value); handle_here_doc(msh, (*cur_token)->next->value);
if ((*cur_token)->type == RED_I) if ((*cur_token)->cmd_type == RED_I)
{ {
if (msh->in_fd != 0) if (msh->in_fd != 0)
close(msh->in_fd); close(msh->in_fd);
@ -45,34 +45,39 @@ void open_input_file(t_msh *msh, t_token **cur_token)
} }
} }
void get_in_type(t_msh *msh, t_token *tokens) void get_in_type(t_msh *msh, t_cmd *tokens)
{ {
t_token *cur_token; t_cmd *cur_token;
cur_token = tokens; cur_token = tokens;
while (cur_token && cur_token->next && cur_token->type == ARG) while (cur_token && cur_token->next && cur_token->cmd_type == CMD)
cur_token = cur_token->next; cur_token = cur_token->next;
if (cur_token->type) // if (cur_token->type)
//{
// msh->in_type = cur_token->type;
// if (cur_token->type == HERE_DOC || cur_token->type == RED_I)
// open_input_file(msh, &cur_token);
//} pas sur de ce que c'est censé faire , j'ai fais un truc différent en dessous
if (cur_token->cmd_type == HERE_DOC || cur_token->cmd_type == RED_I)
{ {
msh->in_type = cur_token->type; msh->in_type = cur_token->cmd_type;
if (cur_token->type == HERE_DOC || cur_token->type == RED_I)
open_input_file(msh, &cur_token); open_input_file(msh, &cur_token);
} }
while (cur_token && cur_token->next && cur_token->next->type == ARG) while (cur_token && cur_token->next && cur_token->next->cmd_type == CMD)
cur_token = cur_token->next; cur_token = cur_token->next;
if (cur_token->next && (cur_token->next->type == HERE_DOC if (cur_token->next && (cur_token->next->cmd_type == HERE_DOC
|| cur_token->next->type == RED_I)) || cur_token->next->cmd_type == RED_I))
get_in_type(msh, cur_token); get_in_type(msh, cur_token);
} }
int first_is_in_type(t_msh *msh) int first_is_in_type(t_cmd *cmd)
{ {
t_token *cur_token; t_cmd *cur_token;
cur_token = msh->cmds; cur_token = cmd;
while (cur_token && cur_token->type == ARG && cur_token->next) while (cur_token && cur_token->cmd_type == CMD && 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->cmd_type == RED_I || cur_token->cmd_type == HERE_DOC)
return (1); return (1);
return (0); return (0);
} }

View File

@ -6,13 +6,13 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */ /* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/06 20:46:19 by tomoron #+# #+# */ /* Created: 2024/02/06 20:46:19 by tomoron #+# #+# */
/* Updated: 2024/03/28 12:34:31 by tomoron ### ########.fr */ /* Updated: 2024/04/02 00:44:16 by tomoron ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
t_token *token_add_back(t_token *token, char *value, t_token_type type) t_token *token_add_back(t_token *token, char *value)
{ {
t_token *res; t_token *res;
t_token *current; t_token *current;
@ -26,7 +26,6 @@ t_token *token_add_back(t_token *token, char *value, t_token_type type)
if (!res) if (!res)
return (token); return (token);
res->value = value; res->value = value;
res->type = type;
if (!token) if (!token)
return (res); return (res);
current = token; current = token;

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/01 21:56:27 by tomoron ### ########.fr */ /* Updated: 2024/04/02 02:07:36 by tomoron ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -31,7 +31,11 @@ typedef enum e_cmd_type
PAREN, PAREN,
AND, AND,
OR, OR,
PIPE PIPE,
RED_O_APP,
HERE_DOC,
RED_O,
RED_I
} t_cmd_type; } t_cmd_type;
typedef struct s_cmd typedef struct s_cmd
@ -60,8 +64,8 @@ typedef struct s_msh
t_env *env; t_env *env;
t_token *cmds; t_token *cmds;
int *pids; int *pids;
t_token_type in_type; t_cmd_type in_type;
t_token_type out_type; t_cmd_type out_type;
int in_fd; int in_fd;
int out_fd; int out_fd;
int locked_return_code; int locked_return_code;
@ -70,7 +74,7 @@ typedef struct s_msh
extern int g_return_code; extern int g_return_code;
t_token *token_add_back(t_token *res, char *token, t_token_type type); t_token *token_add_back(t_token *res, char *token);
t_cmd *cmd_add_back(t_cmd *res, char *cmd, t_cmd_type type); t_cmd *cmd_add_back(t_cmd *res, char *cmd, t_cmd_type type);
void *here_doc_variables(int write, int index, void *data); void *here_doc_variables(int write, int index, void *data);
int add_var_to_str(char *res, char **command, t_env *env); int add_var_to_str(char *res, char **command, t_env *env);
@ -82,9 +86,9 @@ void child(t_msh *msh, char **cmd_args, int i);
t_token *parse_command(char *command, t_env *env); t_token *parse_command(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 *ft_get_env(t_env *env, char *var_name); char *ft_get_env(t_env *env, char *var_name);
void get_out_type(t_msh *msh, t_token *cmds); void get_out_type(t_msh *msh, t_cmd *cmds);
void handle_here_doc(t_msh *msh, char *eof); void handle_here_doc(t_msh *msh, char *eof);
void get_in_type(t_msh *msh, t_token *tokens); void get_in_type(t_msh *msh, t_cmd *tokens);
void signal_handler_interactive(int signum); void signal_handler_interactive(int signum);
int get_token_len(char *cmd, t_env *env); int get_token_len(char *cmd, t_env *env);
void signal_handler_here_doc(int signum); void signal_handler_here_doc(int signum);
@ -95,7 +99,7 @@ void remove_command_from_msh(t_msh *msh);
void ft_exit(t_msh *msh, int error_code); void ft_exit(t_msh *msh, int error_code);
void signal_handler_command(int signum); void signal_handler_command(int signum);
void ft_exit(t_msh *msh, int exit_code); void ft_exit(t_msh *msh, int exit_code);
void redirect_output(t_msh *msh); void redirect_output(t_msh *msh, int i);
char **split_paths_from_env(t_env *env); char **split_paths_from_env(t_env *env);
int add_return_code_to_str(char *res); int add_return_code_to_str(char *res);
void redirect_input(t_msh *msh); void redirect_input(t_msh *msh);
@ -109,7 +113,7 @@ 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_token *cmds); int get_cmd_count(t_token *cmds);
int first_is_in_type(t_msh *msh); int first_is_in_type(t_cmd *cmd);
int contains_newline(char *str); int contains_newline(char *str);
int check_var_name(char *name); int check_var_name(char *name);
char **get_cmd_args(t_msh *msh); char **get_cmd_args(t_msh *msh);

View File

@ -6,15 +6,16 @@
/* 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/04/01 20:09:27 by marde-vr ### ########.fr */ /* Updated: 2024/04/02 02:08:43 by tomoron ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
void redirect_output(t_msh *msh) void redirect_output(t_msh *msh, int i)
{ {
if (/*msh->out_type != PIPE*/ 1) (void)i;
if (msh->out_type != PIPE)
{ {
if (dup2(msh->out_fd, 1) < 0) if (dup2(msh->out_fd, 1) < 0)
ft_exit(msh, 1); ft_exit(msh, 1);
@ -26,48 +27,43 @@ void redirect_output(t_msh *msh)
} }
} }
void open_out_file(t_msh *msh, t_token **cur_cmd) void open_out_file(t_msh *msh, t_cmd **cur_cmd)
{ {
msh->out_type = (*cur_cmd)->type; msh->out_type = (*cur_cmd)->cmd_type;
if (msh->out_type == RED_O) if (msh->out_type == RED_O)
msh->out_fd = open((*cur_cmd)->next->value, msh->out_fd = open((*cur_cmd)->next->value,
O_CREAT | O_WRONLY | O_TRUNC, 0644); O_CREAT | O_WRONLY | O_TRUNC, 0644);
if (msh->out_type == RED_O_APP) if (msh->out_type == RED_O_APP)
msh->out_fd = open((*cur_cmd)->next->value, msh->out_fd = open((*cur_cmd)->next->value, O_CREAT | O_RDWR | O_APPEND,
O_CREAT | O_RDWR | O_APPEND, 0644); 0644);
if (msh->out_fd == -1) if (msh->out_fd == -1)
{ {
g_return_code = 1; g_return_code = 1;
perror("open"); perror("open");
return ; return ;
} }
if (/*(*cur_cmd)->type != PIPE*/ 1) if ((*cur_cmd)->cmd_type != PIPE)
{ {
while ((*cur_cmd)->next && (*cur_cmd)->next->type == ARG) while ((*cur_cmd)->next && (*cur_cmd)->next->cmd_type == CMD)
*cur_cmd = (*cur_cmd)->next; *cur_cmd = (*cur_cmd)->next;
if ((*cur_cmd)->next && ((*cur_cmd)->next->type == RED_O if ((*cur_cmd)->next && ((*cur_cmd)->next->cmd_type == RED_O
|| (*cur_cmd)->next->type == RED_O_APP)) || (*cur_cmd)->next->cmd_type == RED_O_APP))
get_out_type(msh, *cur_cmd); get_out_type(msh, *cur_cmd);
} }
} }
void get_out_type(t_msh *msh, t_token *cmds) void get_out_type(t_msh *msh, t_cmd *cmds)
{ {
t_token *cur_cmd; t_cmd *cur_cmd;
msh->out_type = ARG; msh->out_type = CMD;
msh->out_fd = 0; msh->out_fd = 0;
cur_cmd = cmds; cur_cmd = cmds;
if (cmds->type && msh->cmds == cmds) while (cur_cmd && cur_cmd->next && cur_cmd->cmd_type != AND
{ && cur_cmd->cmd_type != OR)
while (msh->cmds->type != ARG && msh->cmds->next->next)
msh->cmds = msh->cmds->next->next;
}
while (cur_cmd && cur_cmd->next && (cur_cmd->type == ARG
|| cur_cmd->type > 3))
cur_cmd = cur_cmd->next; cur_cmd = cur_cmd->next;
if (!cur_cmd->type) if (cur_cmd->cmd_type == CMD || cur_cmd->cmd_type == PAREN)
msh->out_type = ARG; msh->out_type = 0;
else else
open_out_file(msh, &cur_cmd); open_out_file(msh, &cur_cmd);
} }

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */ /* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/09 15:26:01 by tomoron #+# #+# */ /* Created: 2024/02/09 15:26:01 by tomoron #+# #+# */
/* Updated: 2024/03/29 14:31:04 by tomoron ### ########.fr */ /* Updated: 2024/04/02 00:42:36 by tomoron ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
@ -58,50 +58,22 @@ char *get_token(char **cmd, int *in_quote, int *in_dquote, t_env *env)
return (res); return (res);
} }
t_token_type get_token_type(char **command)
{
t_token_type res;
while (ft_isspace(**command))
(*command)++;
if ((*command)[0] == '>' && (*command)[1] == '>')
res = RED_O_APP;
else if ((*command)[0] == '<' && (*command)[1] == '<')
res = HERE_DOC;
else if ((*command)[0] == '>')
res = RED_O;
else if ((*command)[0] == '<')
res = RED_I;
else
res = ARG;
if (res == RED_O_APP || res == HERE_DOC)
(*command) += 2;
if (res == RED_O || res == RED_I)
(*command)++;
return (res);
}
t_token *parse_command(char *command, t_env *env) t_token *parse_command(char *command, t_env *env)
{ {
int in_quote; int in_quote;
int in_dquote; int in_dquote;
t_token *res; t_token *res;
char *value; char *value;
t_token_type type;
in_quote = 0; in_quote = 0;
in_dquote = 0; in_dquote = 0;
res = 0; res = 0;
while (command && *command) while (command && *command)
{ {
type = get_token_type(&command);
if (type == ARG)
value = get_token(&command, &in_quote, &in_dquote, env); value = get_token(&command, &in_quote, &in_dquote, env);
else if (!value)
value = 0;
if (type == ARG && value == 0)
return (free_token(res)); return (free_token(res));
res = token_add_back(res, value, type); res = token_add_back(res, value);
while (ft_isspace(*command)) while (ft_isspace(*command))
command++; command++;
} }

View File

@ -6,7 +6,7 @@
/* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */ /* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/27 14:40:44 by tomoron #+# #+# */ /* Created: 2024/03/27 14:40:44 by tomoron #+# #+# */
/* Updated: 2024/04/01 20:04:55 by tomoron ### ########.fr */ /* Updated: 2024/04/02 02:08:14 by tomoron ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -40,21 +40,27 @@ t_cmd_type get_cmd_type_bonus(char **cmd)
{ {
t_cmd_type res; t_cmd_type res;
while (ft_isspace(**cmd))
(*cmd)++;
if (**cmd == '|' && (*cmd)[1] != '|') if (**cmd == '|' && (*cmd)[1] != '|')
res = PIPE; res = PIPE;
else if (**cmd == '|' && (*cmd)[1] == '|') else if (**cmd == '|' && (*cmd)[1] == '|')
res = OR; res = OR;
else if (**cmd == '&' && (*cmd)[1] == '&') else if (**cmd == '&' && (*cmd)[1] == '&')
res = AND; 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 == '(') else if (**cmd == '(')
res = PAREN; res = PAREN;
else else
res = CMD; res = CMD;
if(res != CMD) if (res != CMD)
(*cmd)++; (*cmd)++;
if (res == OR || res == AND) if (res == OR || res == AND || res == RED_O_APP || res == HERE_DOC)
(*cmd)++; (*cmd)++;
return (res); return (res);
} }
@ -70,7 +76,7 @@ int get_parenthesis_cmd_len(char *cmd)
parenthesis = 1; parenthesis = 1;
in_quote = 0; in_quote = 0;
in_dquote = 0; in_dquote = 0;
while(cmd[len] && parenthesis) while (cmd[len] && parenthesis)
{ {
if (cmd[len] == '\'' && !in_dquote) if (cmd[len] == '\'' && !in_dquote)
in_quote = !in_quote; in_quote = !in_quote;
@ -80,7 +86,7 @@ int get_parenthesis_cmd_len(char *cmd)
parenthesis += 1 * (-(cmd[len] == ')')); parenthesis += 1 * (-(cmd[len] == ')'));
len++; len++;
} }
return(len - 1); return (len - 1);
} }
int get_normal_cmd_len(char *cmd) int get_normal_cmd_len(char *cmd)
@ -92,8 +98,9 @@ int get_normal_cmd_len(char *cmd)
len = 0; len = 0;
in_quote = 0; in_quote = 0;
in_dquote = 0; in_dquote = 0;
while(cmd[len] && (in_quote || in_dquote || (cmd[len] != '|' && while (cmd[len] && (in_quote || in_dquote || (cmd[len] != '|'
cmd[len] != '&' && cmd[len] != '(' && cmd[len] != ')'))) && cmd[len] != '&' && cmd[len] != '(' && cmd[len] != ')'
&& cmd[len] != '<' && cmd[len] != '>')))
{ {
if (cmd[len] == '\'' && !in_dquote) if (cmd[len] == '\'' && !in_dquote)
in_quote = !in_quote; in_quote = !in_quote;
@ -101,10 +108,10 @@ int get_normal_cmd_len(char *cmd)
in_dquote = !in_dquote; in_dquote = !in_dquote;
len++; len++;
} }
return(len); return (len);
} }
char *get_cmd_value(char **cmd , t_cmd_type type) char *get_cmd_value(char **cmd, t_cmd_type type)
{ {
int len; int len;
char *res; char *res;
@ -115,22 +122,21 @@ char *get_cmd_value(char **cmd , t_cmd_type type)
len = get_normal_cmd_len(*cmd); len = get_normal_cmd_len(*cmd);
res = ft_substr(*cmd, 0, len); res = ft_substr(*cmd, 0, len);
(*cmd) += len; (*cmd) += len;
if(type == PAREN) if (type == PAREN)
(*cmd)++; (*cmd)++;
return(res); return (res);
} }
void print_syntax_error_bonus(t_cmd *cmd) void print_syntax_error_bonus(t_cmd *cmd)
{ {
if (cmd->cmd_type == CMD || cmd->cmd_type == PAREN) if (cmd->cmd_type == CMD || cmd->cmd_type == PAREN)
return; return ;
ft_printf_fd(2, "minishell : syntax error near unexpected token `"); ft_printf_fd(2, "minishell : syntax error near unexpected token `");
if(cmd->cmd_type == AND) if (cmd->cmd_type == AND)
ft_printf_fd(2, "AND"); ft_printf_fd(2, "&&");
if(cmd->cmd_type == OR) if (cmd->cmd_type == OR)
ft_printf_fd(2, "OR"); ft_printf_fd(2, "||");
if(cmd->cmd_type == PIPE) if (cmd->cmd_type == PIPE)
ft_printf_fd(2, "PIPE"); ft_printf_fd(2, "|");
ft_printf_fd(2, "'\n"); ft_printf_fd(2, "'\n");
} }
@ -139,24 +145,27 @@ t_cmd *check_cmds_syntax(t_cmd *cmds)
t_cmd_type last; t_cmd_type last;
t_token *token; t_token *token;
if(cmds->cmd_type == OR || cmds->cmd_type == AND || cmds->cmd_type == PIPE) if (cmds->cmd_type == OR || cmds->cmd_type == AND || cmds->cmd_type == PIPE)
return(cmds); return (cmds);
last = cmds->cmd_type; last = cmds->cmd_type;
cmds = cmds->next; cmds = cmds->next;
while(cmds) while (cmds)
{ {
if(cmds->cmd_type == OR || cmds->cmd_type == AND || cmds->cmd_type == PIPE) if (cmds->cmd_type == OR || cmds->cmd_type == AND
if((last != CMD && last != PAREN) || (!cmds->next && cmds->next->cmd_type != CMD && cmds->next->cmd_type != PAREN)) || cmds->cmd_type == PIPE)
return(cmds); if ((last != CMD && last != PAREN) || (!cmds->next
if(cmds->cmd_type == CMD || cmds->cmd_type == PAREN) && cmds->next->cmd_type != CMD
&& cmds->next->cmd_type != PAREN))
return (cmds);
if (cmds->cmd_type == CMD || cmds->cmd_type == PAREN)
{ {
token = parse_command(cmds->value, 0); token = parse_command(cmds->value, 0);
if(!token) if (!token)
return(cmds); return (cmds);
free_token(token); free_token(token);
} }
} }
return(0); return (0);
} }
t_cmd *parsing_bonus(char *cmd) t_cmd *parsing_bonus(char *cmd)
@ -170,9 +179,13 @@ t_cmd *parsing_bonus(char *cmd)
return (0); return (0);
while (*cmd) while (*cmd)
{ {
while (ft_isspace(*cmd))
cmd++;
type = get_cmd_type_bonus(&cmd); type = get_cmd_type_bonus(&cmd);
if (type == CMD || type == PAREN) if (type == CMD || type == PAREN)
value = get_cmd_value(&cmd, type); value = get_cmd_value(&cmd, type);
else if (type == RED_O || type == RED_O_APP)
value = 0; // TODO: set value to the next argument
else else
value = 0; value = 0;
res = cmd_add_back(res, value, type); res = cmd_add_back(res, value, type);

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/01 20:09:14 by marde-vr ### ########.fr */ /* Updated: 2024/04/02 02:07:25 by tomoron ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -48,9 +48,10 @@ void execute_command(t_msh *msh, char **cmd_args)
void child(t_msh *msh, char **cmd_args, int i) void child(t_msh *msh, char **cmd_args, int i)
{ {
if ((msh->in_type != ARG/* && msh->in_type != PIPE*/) // flemme
|| (/*msh->in_type == PIPE &&*/ i > 0)) //if ((msh->in_type != ARG /*&& msh->in_type != PIPE*/)
redirect_input(msh); // || (/*msh->in_type == PIPE &&*/ i > 0))
// redirect_input(msh);
if (/*msh->out_type == PIPE ||*/ msh->out_type == RED_O if (/*msh->out_type == PIPE ||*/ msh->out_type == RED_O
|| msh->out_type == RED_O_APP) || msh->out_type == RED_O_APP)
redirect_output(msh, i); redirect_output(msh, i);