Skip to content
This repository has been archived by the owner on Nov 30, 2024. It is now read-only.

Commit

Permalink
norminette
Browse files Browse the repository at this point in the history
  • Loading branch information
mcombeau committed Nov 7, 2022
1 parent 2d6f678 commit bd56321
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 128 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ SRC = main.c \
parser/create_commands.c \
parser/parse_word.c \
parser/fill_args_echo.c \
parser/fill_args_echo_utils.c \
parser/fill_args_default.c \
parser/parse_input.c \
parser/parse_trunc.c \
Expand Down
33 changes: 20 additions & 13 deletions includes/minishell.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: mcombeau <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/17 17:14:16 by mcombeau #+# #+# */
/* Updated: 2022/11/07 17:17:58 by mcombeau ### ########.fr */
/* Updated: 2022/11/07 17:39:16 by mcombeau ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -208,48 +208,55 @@ int remove_quotes(t_token **token_node);

/* ------------------------ PARSER ---------------------------------------*/

//create_command.c
// create_command.c
void create_commands(t_data *data, t_token *token);

//parse_command.c
// parse_command.c
void parse_word(t_command **cmd, t_token **token_lst);

//fill_arguments.c
// fill_args_default.c
int fill_args(t_token **token_node, t_command *last_cmd);
int count_args(t_token *temp);
char *join_vars(t_token **token_node);
int add_args_default_mode(t_token **token_node, t_command *last_cmd);
int create_args_default_mode(t_token **token_node, t_command *last_cmd);

// fill_args_echo_mode.c
int add_args_echo_mode(t_token **token_node, t_command *last_cmd);
int create_args_echo_mode(t_token **token_node, t_command *last_cmd);

//cmd_lst_utils.c
// fill_args_echo_utils.c
char *join_vars(t_token **token_node);
int count_args(t_token *temp);
char **copy_in_new_tab(int len, char **new_tab,
t_command *last_cmd, t_token *tmp);
void remove_empty_var_args(t_token **tokens);

// cmd_lst_utils.c
t_command *lst_new_cmd(bool value);
void lst_add_back_cmd(t_command **alst, t_command *new_node);
void lst_delone_cmd(t_command *lst, void (*del)(void *));
void lst_clear_cmd(t_command **lst, void (*del)(void *));
t_command *lst_last_cmd(t_command *cmd);
t_command *lst_first_cmd(t_command *cmd);

//parse_trunc.c
// parse_trunc.c
void parse_trunc(t_command **last_cmd, t_token **token_lst);
char *get_relative_path(char *file_to_open);

//parse_input.c
// parse_input.c
bool remove_old_file_ref(t_io_fds *io, bool infile);
void parse_input(t_command **last_cmd, t_token **token_lst);

//parse_append.c
// parse_append.c
void parse_append(t_command **last_cmd, t_token **token_lst);

//parse_heredoc.c
// parse_heredoc.c
void parse_heredoc(t_data *data, t_command **last_cmd,
t_token **token_lst);

//parse_heredoc_utils.c
// parse_heredoc_utils.c
bool fill_heredoc(t_data *data, t_io_fds *io, int fd);

//parse_pipec
// parse_pipec
void parse_pipe(t_command **cmd, t_token **token_lst);

/* ------------------------ EXECUTION ---------------------------------------*/
Expand Down
115 changes: 0 additions & 115 deletions sources/parser/fill_args_echo.c
Original file line number Diff line number Diff line change
@@ -1,96 +1,6 @@

#include "minishell.h"

/*
** This function joins all the tokens of a quoted sentence
** (ex " Hello $user") that was previously split.
** To join them, the tokens have to be of type VAR and the join
** setting must be set to true (the quotes are implied)
** The function is only used when the command is "echo".
**
** ex: The strings -> "Hello" " " "world"
** become "Hello world"
*/

char *join_vars(t_token **token_node)
{
t_token *temp;
char *str;

temp = *token_node;
str = ft_strdup(temp->str);
while (temp->type == VAR && temp->next->type == VAR
&& temp->next->join == true)
{
str = ft_strjoin(str, temp->next->str);
temp = temp->next;
}
*token_node = temp;
return (str);
}

/*
** This function counts the number of arguments in the list of tokens
** To consider a token or multiple tokens as an argument they must be either
** a WORD or a VAR and if they temp = *token_node;
are a VAR that has to be joined, we have
** to loop through all the tokens that check these conditions
** (type == VAR and join == true) before counting them as one argument
*/

int count_args(t_token *temp)
{
int i;

i = 0;
while (temp && (temp->type == WORD || temp->type == VAR))
{
if (temp->type == VAR && temp->join == true)
{
while (temp->type == VAR && temp->join == true)
temp = temp->next;
i++;
}
else
{
i++;
temp = temp->next;
}
}
return (i);
}

/* remove_empty_var_args:
** If a variable does not exist in the environment, the token string
** will contain "\0". In this case, echo should not print the variable
** or any spaces before/after it. Therefore the token must be
** removed before creating/adding echo args.
** i.e., if variable X does not exist in environment,
** 'echo $X $X $X $USER' should print:
** 'username' (not ' username')
** However, if the variable exists but its value is empty, the token
** should not be removed.
*/
static void remove_empty_var_args(t_token **tokens)
{
t_token *temp;

temp = *tokens;
while (temp->type == WORD || temp->type == VAR)
{
if (temp->type == VAR && temp->str[0] == '\0'
&& temp->var_exists == false)
{
temp = temp->next;
if (temp == (*tokens)->next)
(*tokens) = (*tokens)->next;
lstdelone_token(temp->prev, free_ptr);
}
else
temp = temp->next;
}
}

/*
** This function deals with the specific case when the command is "echo"
** - It allocates the array of arguments thanks to the count_args function
Expand All @@ -101,7 +11,6 @@ static void remove_empty_var_args(t_token **tokens)
** * if "join = false" we just fill the last_cmd_>args[i]
** with the current token.
*/

int create_args_echo_mode(t_token **token_node, t_command *last_cmd)
{
int nb_args;
Expand Down Expand Up @@ -131,30 +40,6 @@ int create_args_echo_mode(t_token **token_node, t_command *last_cmd)
return (SUCCESS);
}

static char **copy_in_new_tab(
int len, char **new_tab, t_command *last_cmd, t_token *tmp)
{
int i;

i = 0;
while (i < len)
{
new_tab[i] = last_cmd->args[i];
i++;
}
while (tmp->type == WORD || tmp->type == VAR)
{
if (tmp->join == true)
new_tab[i] = join_vars(&tmp);
else
new_tab[i] = ft_strdup(tmp->str);
i++;
tmp = tmp->next;
}
new_tab[i] = NULL;
return (new_tab);
}

int add_args_echo_mode(t_token **token_node, t_command *last_cmd)
{
int len;
Expand Down
115 changes: 115 additions & 0 deletions sources/parser/fill_args_echo_utils.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
#include "minishell.h"

/*
** This function joins all the tokens of a quoted sentence
** (ex " Hello $user") that was previously split.
** To join them, the tokens have to be of type VAR and the join
** setting must be set to true (the quotes are implied)
** The function is only used when the command is "echo".
**
** ex: The strings -> "Hello" " " "world"
** become "Hello world"
*/

char *join_vars(t_token **token_node)
{
t_token *temp;
char *str;

temp = *token_node;
str = ft_strdup(temp->str);
while (temp->type == VAR && temp->next->type == VAR
&& temp->next->join == true)
{
str = ft_strjoin(str, temp->next->str);
temp = temp->next;
}
*token_node = temp;
return (str);
}

/*
** This function counts the number of arguments in the list of tokens
** To consider a token or multiple tokens as an argument they must be either
** a WORD or a VAR and if they temp = *token_node;
are a VAR that has to be joined, we have
** to loop through all the tokens that check these conditions
** (type == VAR and join == true) before counting them as one argument
*/

int count_args(t_token *temp)
{
int i;

i = 0;
while (temp && (temp->type == WORD || temp->type == VAR))
{
if (temp->type == VAR && temp->join == true)
{
while (temp->type == VAR && temp->join == true)
temp = temp->next;
i++;
}
else
{
i++;
temp = temp->next;
}
}
return (i);
}

char **copy_in_new_tab(int len, char **new_tab,
t_command *last_cmd, t_token *tmp)
{
int i;

i = 0;
while (i < len)
{
new_tab[i] = last_cmd->args[i];
i++;
}
while (tmp->type == WORD || tmp->type == VAR)
{
if (tmp->join == true)
new_tab[i] = join_vars(&tmp);
else
new_tab[i] = ft_strdup(tmp->str);
i++;
tmp = tmp->next;
}
new_tab[i] = NULL;
return (new_tab);
}

/* remove_empty_var_args:
** If a variable does not exist in the environment, the token string
** will contain "\0". In this case, echo should not print the variable
** or any spaces before/after it. Therefore the token must be
** removed before creating/adding echo args.
** i.e., if variable X does not exist in environment,
** 'echo $X $X $X $USER' should print:
** 'username' (not ' username')
** However, if the variable exists but its value is empty, the token
** should not be removed.
*/
void remove_empty_var_args(t_token **tokens)
{
t_token *temp;

temp = *tokens;
while (temp->type == WORD || temp->type == VAR)
{
if (temp->type == VAR && temp->str[0] == '\0'
&& temp->var_exists == false)
{
temp = temp->next;
if (temp == (*tokens)->next)
(*tokens) = (*tokens)->next;
lstdelone_token(temp->prev, free_ptr);
}
else
temp = temp->next;
}
}

0 comments on commit bd56321

Please sign in to comment.