/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   p_lblock_next.c                                    :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: rbousset <marvin@42.fr>                    +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2020/02/14 17:19:27 by rbousset          #+#    #+#             */
/*   Updated: 2020/02/14 17:19:29 by rbousset         ###   ########lyon.fr   */
/*                                                                            */
/* ************************************************************************** */

#include <libft.h>
#include <stdlib.h>
#include <stdint.h>
#include <limits.h>

#include "d_define.h"
#include "s_destroy.h"
#include "f_fail.h"
#include "p_lblock_more.h"
#include "s_struct.h"
#include "u_alias.h"
#include "u_parse.h"
#include "u_utils.h"
#include "u_vars.h"
#include "u_vars_next.h"

static char		**p_alloc_rewords(char *words[], int64_t j, t_msh *msh)
{
	char	**rewords;

	if ((rewords = (char**)malloc((j + 1) * sizeof(char*))) == NULL)
	{
		ft_delwords(words);
		f_alloc_and_destroy_msh(msh);
	}
	return (rewords);
}

static char		**p_add_to_variables_and_delete(char *words[],
												t_bool reg,
												int64_t i,
												t_msh *msh)
{
	char	**rewords;
	int64_t	j;
	int64_t	k;

	j = -1;
	while (reg == TRUE && words[++j] != NULL && j < i)
		p_register_word(words[j], msh);
	j = 0;
	while (words[i + j] != NULL)
		j++;
	rewords = p_alloc_rewords(words, j, msh);
	k = i;
	while (i - k < j)
	{
		if ((rewords[i - k] = ft_strdup(words[i])) == NULL)
		{
			ft_delwords(words);
			f_alloc_and_destroy_msh(msh);
		}
		i++;
	}
	rewords[i - k] = 0;
	ft_delwords(words);
	i++;
	return (rewords);
}

static void		p_add_to_env_fork(int64_t i, char *words[], t_msh *msh)
{
	int64_t	j;

	j = 0;
	while (j < i)
	{
		ft_strlcpy(msh->env_fork_tmp[j], words[j], ft_strlen(words[j]) + 1);
		j++;
	}
	msh->env_fork_tmp[j][0] = '\0';
}

static char		*p_set_ptr(char *words[], int64_t i, t_bool *reg, t_bool *isvar)
{
	char	*ptr;

	ptr = words[i];
	while (*ptr != '\0' && *ptr != '=')
		ptr++;
	*reg = (*ptr == '=') ? (TRUE) : (*reg);
	*isvar = (*ptr == '=') ? (TRUE) : (*isvar);
	return (ptr);
}

char			**p_check_args_equals(char *words[], t_msh *msh)
{
	char	*ptr;
	int64_t	i;
	t_bool	reg;
	t_bool	isvar;

	reg = FALSE;
	isvar = FALSE;
	i = -1;
	while (words[++i] != NULL)
	{
		ptr = p_set_ptr(words, i, &reg, &isvar);
		if (*ptr == C_NUL || words[i][0] == '=' || ft_isdigit(words[i][0]))
		{
			reg = FALSE;
			isvar = (i == 0) ? (FALSE) : (isvar);
			if (isvar == TRUE)
				p_add_to_env_fork(i, words, msh);
			else
				msh->env_fork_tmp[0][0] = C_NUL;
			break ;
		}
	}
	if (isvar == TRUE)
		return (p_add_to_variables_and_delete(words, reg, i, msh));
	return (words);
}