#include "minishell.h"

static void handle_line(t_shell *sh, char *line)
{
	int error = 0;
	t_token *toks = NULL;
	t_pipeline *p = NULL;

	toks = lex_line(line, &error);
	if (error)
	{
		free_tokens(toks);
		return;
	}
	p = parse_tokens(toks, &error);
	free_tokens(toks);
	if (error || !p)
	{
		free_pipeline(p);
		return;
	}
	if (expand_pipeline(p, sh) != 0)
	{
		free_pipeline(p);
		return;
	}
	sh->exit_status = execute_pipeline(p, sh);
	sh->last_status = sh->exit_status;
	free_pipeline(p);
}

void ms_loop(t_shell *sh)
{
	char *line;

	while (!sh->exit_requested)
	{
		line = readline(MS_PROMPT);
		if (!line)
			break;
		if (line[0] != '\0')
			add_history(line);
		handle_line(sh, line);
		free(line);
	}
}
