Skip to content

Commit

Permalink
norminette updates! (only lexer and errors to go)
Browse files Browse the repository at this point in the history
  • Loading branch information
LithiumOx committed Nov 21, 2023
1 parent ecccafe commit 8c01a5d
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 30 deletions.
2 changes: 1 addition & 1 deletion src/group/hdoc_expand.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,4 @@ bool hdoc_expand(char **str, t_shell *data)
}
}
return ((str_vec_to_string(&vec, str, data)));
}
}
4 changes: 1 addition & 3 deletions src/group/heredoc.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ static bool push_hdoc(char *filename, t_group *group, t_shell *data)
return (true);
}



/**
* @note if signal ctrl D break while loop and return true
*/
Expand Down Expand Up @@ -68,7 +66,7 @@ static bool hdoc_read(size_t heredoc_fd, t_token *token, t_shell *data)
*/
bool heredoc(char *filename, t_token *token, t_shell *data)
{
int heredoc_fd;
int heredoc_fd;

heredoc_fd = open(filename, O_CREAT | O_TRUNC | O_RDWR, 0644);
if (heredoc_fd == -1)
Expand Down
2 changes: 1 addition & 1 deletion src/lexer/index.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ bool lexer(char *input, t_shell *data)
{
if (checkchar(input[i], "();\\&"))
{
return (set_err(OUT_OF_SCOPE, (char[2]){input[i], '\0'}, data));
return (set_err(OUT_OF_SCOPE, ((char[2]){input[i], '\0'}), data));
}
if (checkchar(input[i], "<>"))
if (!build_redir_token(input, &i, data))
Expand Down
4 changes: 2 additions & 2 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ static void soft_exit(char *input, t_shell *data)

static bool function_map(char *input, t_shell *data)
{
bool (*function[5])(t_shell *);
int i;
int i;
static bool (*function[5])(t_shell *);

function[0] = parser;
function[1] = check_tokens;
Expand Down
61 changes: 40 additions & 21 deletions src/structs/shell.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,44 @@ void free_shell(t_shell *data, bool close_shell)
}
}

/**
* @brief A helper function for init_shell for the first init
* @param env The enviroment char array
* @return t_shell* The created data struct
*/
static t_shell *init_first_shell(char **env)
{
t_shell *data;

data = malloc(sizeof(t_shell));
if (!data)
exit_mini("failed to init data", 1);
if (!vec_init(&data->token_vec, 3, sizeof(t_token), clear_token))
exit_mini("failed to vec_init token_vec", 1);
if (!vec_init(&data->env, 50, sizeof(t_env), clear_env))
exit_mini("failed to vec_init env", 1);
init_env(env, &data->env);
data->exec = NULL;
data->exit_type = GOOD;
data->error_type = NO_ERROR;
data->exit_msg = NULL;
return (data);
}

/**
* @brief A helper function for init_shell for the subsequent inits
* @return t_shell* The created data struct
*/
static t_shell *init_subsequent_shell(t_shell *data)
{
if (!vec_init(&data->token_vec, 3, sizeof(t_token), clear_token))
exit_mini("failed to vec_init env", 1);
data->exec = NULL;
data->exit_type = GOOD;
data->exit_msg = NULL;
return (data);
}

/**
* @brief Initializes the global variables
*
Expand All @@ -47,27 +85,8 @@ t_shell *init_shell(char **env, bool first_init)

data = NULL;
if (first_init)
{
data = malloc(sizeof(t_shell));
if (!data)
exit_mini("failed to init data", 1);
if (!vec_init(&data->token_vec, 3, sizeof(t_token), clear_token))
exit_mini("failed to vec_init token_vec", 1);
if (!vec_init(&data->env, 50, sizeof(t_env), clear_env))
exit_mini("failed to vec_init env", 1);
init_env(env, &data->env);
data->exec = NULL;
data->exit_type = GOOD;
data->error_type = NO_ERROR;
data->exit_msg = NULL;
}
data = init_first_shell(env);
else
{
if (!vec_init(&data->token_vec, 3, sizeof(t_token), clear_token))
exit_mini("failed to vec_init env", 1);
data->exec = NULL;
data->exit_type = GOOD;
data->exit_msg = NULL;
}
init_subsequent_shell(data);
return (data);
}
4 changes: 2 additions & 2 deletions src/utils/miscellaneous.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
bool is_redirect(t_token *token)
{
if (token->type == O_REDIRECT || token->type == I_REDIRECT
|| token->type == A_REDIRECT || token->type == HEREDOC ||
token->type == HDOC_LITERAL)
|| token->type == A_REDIRECT || token->type == HEREDOC
|| token->type == HDOC_LITERAL)
{
return (true);
}
Expand Down

0 comments on commit 8c01a5d

Please sign in to comment.