This repository has been archived by the owner on Nov 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
136 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
|
@@ -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 ---------------------------------------*/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |