Skip to content

Commit

Permalink
Combine (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
LithiumOx authored Sep 29, 2023
2 parents 9bed040 + bc0eeca commit 95acf4b
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 40 deletions.
5 changes: 3 additions & 2 deletions includes/minishell.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: mdekker/jde-baai <team@codam.nl> +#+ */
/* +#+ */
/* Created: 2023/07/09 21:25:59 by mdekker #+# #+# */
/* Updated: 2023/09/26 16:33:10 by mdekker ######## odam.nl */
/* Updated: 2023/09/29 16:14:15 by mdekker/jde ######## odam.nl */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -58,7 +58,8 @@ bool is_or(char *str);
bool is_and(char *str);
void verify_token_vec(t_shell *data);
bool check_tokens(t_shell *data);
bool combine_heredoc(t_vector *vec, size_t i);
bool combine_tokens(t_vector *vec, size_t i, t_types type);
void free_found(t_vector *found);
/* group */
bool group_token_vec(t_shell *data);
bool hdoc_found(t_group *group, int i, t_shell *data);
Expand Down
8 changes: 7 additions & 1 deletion src/checker/helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: mdekker/jde-baai <team@codam.nl> +#+ */
/* +#+ */
/* Created: 2023/09/11 21:12:42 by mdekker/jde #+# #+# */
/* Updated: 2023/09/13 12:33:31 by mdekker/jde ######## odam.nl */
/* Updated: 2023/09/29 16:14:09 by mdekker/jde ######## odam.nl */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -66,3 +66,9 @@ bool type_compare(size_t num_args, t_types type, ...)
va_end(args);
return (found);
}

void free_found(t_vector *found)
{
vec_free(found);
free(found);
}
6 changes: 3 additions & 3 deletions src/checker/heredoc.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: mdekker/jde-baai <team@codam.nl> +#+ */
/* +#+ */
/* Created: 2023/09/13 22:15:51 by mdekker/jde #+# #+# */
/* Updated: 2023/09/28 12:35:58 by mdekker/jde ######## odam.nl */
/* Updated: 2023/09/29 15:06:13 by mdekker ######## odam.nl */
/* */
/* ************************************************************************** */

Expand All @@ -20,14 +20,14 @@
* @return true If the tokens were combined
* @return false If the tokens could not be combined
*/
bool combine_heredoc(t_vector *vec, size_t i)
bool combine_tokens(t_vector *vec, size_t i, t_types type)
{
t_token *token;

token = vec_get(vec, i + 1);
token->type = HEREDOC;
if (!token)
return (false);
token->type = type;
if (!vec_remove(vec, i))
return (false);
return (true);
Expand Down
67 changes: 33 additions & 34 deletions src/checker/index.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: mdekker/jde-baai <team@codam.nl> +#+ */
/* +#+ */
/* Created: 2023/08/02 16:57:32 by mdekker/jde #+# #+# */
/* Updated: 2023/09/28 12:25:18 by mdekker/jde ######## odam.nl */
/* Updated: 2023/09/29 16:13:47 by mdekker/jde ######## odam.nl */
/* */
/* ************************************************************************** */

Expand All @@ -23,22 +23,22 @@ bool filter_operators(void *item)

static bool check_double_ops(t_vector *found, t_shell *data)
{
t_found *current_found;
t_found *c_found;
t_found *next_found;
t_token *current_token;
t_token *next_token;
t_token *c_token;
t_token *n_token;
size_t i;

i = 0;
while (i < found->length - 1)
{
current_found = (t_found *)vec_get(found, i);
c_found = (t_found *)vec_get(found, i);
next_found = (t_found *)vec_get(found, i + 1);
current_token = (t_token *)current_found->item;
next_token = (t_token *)next_found->item;
if (next_found->index - current_found->index == 1)
if (current_token->type == next_token->type)
return (set_err(SYNTAX, type_symbol(current_token->type), data),
c_token = (t_token *)c_found->item;
n_token = (t_token *)next_found->item;
if (next_found->index - c_found->index == 1)
if (c_token->type == n_token->type)
return (set_err(SYNTAX, type_symbol(c_token->type), data),
false);
i++;
}
Expand Down Expand Up @@ -74,15 +74,16 @@ void print_t_found(void *item, size_t index)
*/
static void decrement_index(void *found)
{
t_found *current_found;
t_found *c_found;

current_found = (t_found *)found;
if (current_found->index != 0)
c_found = (t_found *)found;
if (c_found->index != 0)
{
current_found->item -= sizeof(t_token);
current_found->index--;
c_found->item -= sizeof(t_token);
c_found->index--;
}
}

/**
* @brief A function that checks if every heredoc has a string after it
* if so it combines the two tokens into one for the execution
Expand All @@ -92,26 +93,26 @@ static void decrement_index(void *found)
* @return true When every heredoc has a string after it
* @return false When a heredoc does not have a string after it
*/
static bool check_heredoc(t_vector *found, t_shell *data)
static bool check_ops(t_vector *found, t_shell *data)
{
t_found *current_found;
t_token *current_token;
t_token *next_token;
t_found *c_found;
t_token *c_token;
t_token *n_token;
size_t i;

i = 0;
while (i < found->length)
{
current_found = (t_found *)vec_get(found, i);
current_token = (t_token *)(current_found->item);
if (current_token && current_token->type == HEREDOC)
c_found = (t_found *)vec_get(found, i);
c_token = (t_token *)(c_found->item);
if (type_compare(4, c_token->type, 14, 12, 11, 13))
{
next_token = (t_token *)vec_get(&data->token_vec,
current_found->index + 1);
if (!next_token || next_token->type != STRING)
return (set_err(SYNTAX, type_symbol(current_token->type), data),
n_token = (t_token *)vec_get(&data->token_vec, c_found->index + 1);
if (!n_token || n_token->type != STRING)
return (set_err(SYNTAX, type_symbol(c_token->type), data),
false);
else if (!combine_heredoc(&data->token_vec, current_found->index))
else if (!combine_tokens(&data->token_vec, c_found->index,
c_token->type))
return (false);
else
vec_apply(found, decrement_index);
Expand All @@ -137,14 +138,12 @@ bool check_tokens(t_shell *data)
if (found_item->index == 0 && type_compare(5, token->type, PIPE, OR, AND,
I_REDIRECT, O_REDIRECT))
return (set_err(SYNTAX, type_symbol(token->type), data),
vec_free(found),
free(found),
free_found(found),
false);
if (!check_double_ops(found, data))
return (vec_free(found), free(found), false);
if (!check_heredoc(found, data))
return (vec_free(found), free(found), false);
vec_free(found);
free(found);
return (free_found(found), false);
if (!check_ops(found, data))
return (free_found(found), false);
free_found(found);
return (true);
}

0 comments on commit 95acf4b

Please sign in to comment.