/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   u_vars.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 "d_define.h"
#include "f_fail.h"
#include "s_destroy.h"
#include "s_line.h"
#include "s_struct.h"
#include "u_utils.h"

static void
	u_get_frm_env(char str[], const char varname[], size_t dstsize, t_msh *msh)
{
	char	**env_dup;
	char	*pp_env;
	size_t	i;

	env_dup = u_get_env_var_names(msh);
	i = 0;
	while (env_dup[i] != NULL)
	{
		if (ft_strncmp(varname + 1, env_dup[i], ft_strlen(env_dup[i]) + 1) == 0)
		{
			pp_env = msh->envp[i];
			while (*pp_env != '\0' && *pp_env != '=')
				pp_env += 1;
			if (*pp_env == '=')
				pp_env += 1;
			ft_strlcpy(str, pp_env, dstsize);
			ft_delwords(env_dup);
			return ;
		}
		i++;
	}
	ft_delwords(env_dup);
}

static void
	u_get_special_var(char str[],
					const char varname[],
					size_t dstsize,
					t_msh *msh)
{
	char		tmp[255];
	int32_t		n;

	if (ft_strncmp(varname, FT_RET_VAR, 3) == 0)
	{
		ft_uitoa_s(tmp, msh->ret);
		ft_strlcpy(str, tmp, 4);
	}
	else if (ft_strncmp(varname, FT_ARGC_VAR, 3) == 0)
	{
		ft_uitoa_s(tmp, msh->argc);
		ft_strlcpy(str, tmp, 255);
	}
	else if (ft_strlen(varname) == 2 && ft_isdigit(*(varname + 1)) == TRUE)
	{
		if ((n = ft_atoi(varname + 1)) <= msh->argc)
			ft_strlcpy(str, msh->argv[n], dstsize);
		else
			(void)ft_memcpy(str, "", 1 * sizeof(char));
	}
}

void
	u_get_custom_var(char str[],
					const char varname[],
					size_t dstsize,
					t_msh *msh)
{
	t_lvars	*ptr;

	ptr = msh->vars;
	while (ptr != NULL &&
		ft_strncmp(varname + 1, ptr->name, ft_strlen(varname + 1) + 1) != 0)
	{
		ptr = ptr->next;
	}
	if (ptr != NULL)
	{
		ft_strlcpy(str, ptr->val, dstsize);
	}
}

/*
** void
** u_get_var_value(char str[], const char varname[], size_t dstsize, t_msh *msh)
**
** DESCRIPTION
** The u_get_var_value() function fills
** str[] with a null-terminated string
** containing the value of the variable varname[]
** including the '$' prefix, in a similar way as
** sprintf does. No more than dstsize bytes will be
** copied into str[]. str[0] is set to \000 if
** varname[] wasn't found.
*/

uint8_t		u_get_var_value(char str[],
							const char varname[],
							size_t dstsize,
							t_msh *msh)
{
	str[0] = C_NUL;
	u_get_special_var(str, varname, dstsize, msh);
	if (str[0] != C_NUL)
	{
		return (0);
	}
	u_get_custom_var(str, varname, dstsize, msh);
	if (str[0] != C_NUL)
	{
		return (0);
	}
	u_get_frm_env(str, varname, dstsize, msh);
	if (str[0] != C_NUL)
	{
		return (0);
	}
	return (1);
}