minishell/src/token/token_parse.c
2025-02-26 16:17:07 +01:00

75 lines
2.0 KiB
C

/* ************************************************************************** */
/* */
/* :::::::: */
/* token_parse.c :+: :+: */
/* +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/02/05 19:10:17 by qmennen #+# #+# */
/* Updated: 2025/02/26 16:15:19 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
#include "minishell.h"
static t_token_type token_from_char(char c, int is_double)
{
if (c == '<')
{
if (is_double)
return (T_HEREDOC);
return (T_REDIRECT_IN);
}
else if (c == '>')
{
if (is_double)
return (T_APPEND_OUT);
return (T_REDIRECT_OUT);
}
else if (c == '&' && is_double)
return (T_AND);
else if (c == '|')
{
if (is_double)
return (T_OR);
return (T_PIPE);
}
return (T_ERROR);
}
static char *char_from_type(t_token_type type)
{
if (type == T_HEREDOC)
return ("<<");
else if (type == T_REDIRECT_IN)
return ("<");
else if (type == T_APPEND_OUT)
return (">>");
else if (type == T_REDIRECT_OUT)
return (">");
else if (type == T_AND)
return ("&&");
else if (type == T_OR)
return ("||");
else if (type == T_PIPE)
return ("|");
return (NULL);
}
t_token *token_parse(t_minishell *msh, t_lexer *lexer)
{
int is_double;
char c;
t_token *token;
t_token_type type;
c = lexer->current_char;
is_double = lexer->input[lexer->pos + 1] == c;
type = token_from_char(c, is_double);
token = token_new(msh, type, char_from_type(type), lexer->pos);
if (is_double)
lexer_readchar(lexer);
lexer_readchar(lexer);
return (token);
}