Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions inc/tokenizer/tokenizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
/* By: ttsubo <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/15 12:36:01 by ttsubo #+# #+# */
/* Updated: 2025/04/17 15:13:49 by ttsubo ### ########.fr */
/* Updated: 2025/05/17 16:18:06 by ttsubo ### ########.fr */
/* */
/* ************************************************************************** */

#ifndef TOKENIZER_H
# define TOKENIZER_H

# include "libft.h"
# include "common.h"
# include <stdio.h>

# define ERR_UNEXPECTED_STR "unexpected error occureed.\n"
Expand Down Expand Up @@ -59,7 +60,7 @@ char *read_token(t_tokenizer *tkn);
void show_tokenizer_error(t_tokenizer_errors err_code);
size_t get_token_capa(char *str);
int is_quote_closed(char *str);
int is_redirect_validate(char *str);
char **tokenizer(char *str);
int is_redirect_validate(char *str, t_minish *minish);
char **tokenizer(char *str, t_minish *minish);

#endif
2 changes: 1 addition & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ static bool prompt(t_minish *minish)
if (res == READ_EMPTY)
return (free_str(&line), true);
_set_g_sig_sts(minish);
tokens = tokenizer(line);
tokens = tokenizer(line, minish);
cmds = parser(tokens, minish);
if (!cmds)
return (free_prompt(&tokens, &cmds, &line), true);
Expand Down
28 changes: 12 additions & 16 deletions src/tokenizer/is_redirect_validate.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,32 @@
/* By: ttsubo <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/15 13:59:15 by ttsubo #+# #+# */
/* Updated: 2025/04/27 15:03:26 by ttsubo ### ########.fr */
/* Updated: 2025/05/17 16:19:18 by ttsubo ### ########.fr */
/* */
/* ************************************************************************** */

#include "tokenizer.h"

static void _show_err_and_set_sts(t_tokenizer_errors err_code, t_minish *minish)
{
show_tokenizer_error(err_code);
minish->last_status = 2;
}

/**
* @brief Checks if the redirect symbol is a valid value.
* @param str
* @return int
* @note For now, this function only checks for consecutive redirect symbols.
*/
int is_redirect_validate(char *str)
int is_redirect_validate(char *str, t_minish *minish)
{
while (*str)
{
if (!ft_strncmp(str, ">>>", 3))
return (show_tokenizer_error(ERR_UNEXPECTED_TOKEN_R1), 0);
else if (!ft_strncmp(str, "<<<", 3))
return (show_tokenizer_error(ERR_UNEXPECTED_TOKEN_L1), 0);
if (*str == '>')
{
if (*(str + 1) == '<')
return (show_tokenizer_error(ERR_UNEXPECTED_TOKEN_L1), 0);
}
if (*str == '<')
{
if (*(str + 1) == '>')
return (show_tokenizer_error(ERR_UNEXPECTED_TOKEN_R1), 0);
}
if (!ft_strncmp(str, ">>>", 3) || (*str == '<' && *(str + 1) == '>'))
return (_show_err_and_set_sts(ERR_UNEXPECTED_TOKEN_R1, minish), 0);
if (!ft_strncmp(str, "<<<", 3) || (*str == '>' && *(str + 1) == '<'))
return (_show_err_and_set_sts(ERR_UNEXPECTED_TOKEN_L1, minish), 0);
str++;
}
return (1);
Expand Down
6 changes: 3 additions & 3 deletions src/tokenizer/tokenizer.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: ttsubo <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/15 12:35:38 by ttsubo #+# #+# */
/* Updated: 2025/04/29 14:58:23 by ttsubo ### ########.fr */
/* Updated: 2025/05/17 16:10:10 by ttsubo ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -24,14 +24,14 @@ void skip_spaces(t_tokenizer *tkn)
* @param str
* @return char**
*/
char **tokenizer(char *str)
char **tokenizer(char *str, t_minish *minish)
{
int token_i;
char **tokens;
t_tokenizer tkn;

tkn = (t_tokenizer){.input = str, .pos = 0, .in_squote = 0, .in_dquote = 0};
if (!is_redirect_validate(str))
if (!is_redirect_validate(str, minish))
return (NULL);
tokens = ft_calloc(get_token_capa(str) + 1, sizeof(char *));
if (!tokens)
Expand Down