/* ************************************************************************** */
/*                                                                            */
/*                                                        ::::::::            */
/*   lexer_read_word.c                                  :+:    :+:            */
/*                                                     +:+                    */
/*   By: qmennen <qmennen@student.codam.nl>           +#+                     */
/*                                                   +#+                      */
/*   Created: 2025/02/05 19:03:47 by qmennen       #+#    #+#                 */
/*   Updated: 2025/02/28 14:06:32 by whaffman      ########   odam.nl         */
/*                                                                            */
/* ************************************************************************** */

#include "minishell.h"

static int	is_word_char(char c)
{
	if (c == '<' || c == '>' || c == '|' || c == '\0')
		return (0);
	else if (ft_isspace(c))
		return (0);
	return (ft_isprint(c));
}

static int	calculate_word_len(t_minishell *msh, t_lexer *lexer)
{
	int		i;
	int		len;

	len = 1;
	i = lexer->pos;
	while (is_word_char(lexer->input[i]))
	{

		len++;
		i++;
	}
	return (len);
}

char	*read_word(t_minishell *msh, t_lexer *lexer, int len)
{
	char	c;
	char	*dest;
	int		i;

	dest = malloc_safe(msh, sizeof(char) * len);
	c = lexer->current_char;
	i = 0;
	while (is_word_char(c))
	{
		dest[i++] = lexer->current_char;
		lexer_readchar(lexer);
		c = lexer->current_char;
	}
	dest[i] = 0;
	return (dest);
}

char	*lexer_readword(t_minishell *msh, t_lexer *lexer)
{
	int		len;
	int		qts;
	char	*word;
	char	c;

	c = lexer->current_char;
	qts = (c == '"' || c == '\'');
	if (qts)
		return (lexer_parse_quotes(msh, lexer));
	// if (!match_quotes(msh, lexer, qts))
	// 	return (NULL);
	len = calculate_word_len(msh, lexer);
	word = read_word(msh, lexer, len);
	return (word);
}
