/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   p_args_escape.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 <stddef.h>

#include "d_define.h"
#include "u_utils.h"

static t_quote_mode	p_escape_squote(char *ptr,
									char **p,
									char word[],
									t_quote_mode mode)
{
	if (mode == Q_NONE)
	{
		if (u_is_not_escaped(word, ptr) == TRUE)
		{
			(void)ft_memmove(word + (ptr - word), ptr + 1,
				(ft_strlen(ptr + 1) + 1) * sizeof(char));
			*(p) -= 1;
			return (Q_SINGLE);
		}
		else
		{
			return (Q_NONE);
		}
	}
	else if (mode == Q_SINGLE)
	{
		(void)ft_memmove(word + (ptr - word), ptr + 1,
			(ft_strlen(ptr + 1) + 1) * sizeof(char));
		*(p) -= 1;
		return (Q_NONE);
	}
	return (mode);
}

static t_quote_mode	p_escape_dquote(char *ptr,
									char **p,
									char word[],
									t_quote_mode mode)
{
	if (mode == Q_NONE)
	{
		if (u_is_not_escaped(word, ptr) == TRUE)
		{
			(void)ft_memmove(word + (ptr - word), ptr + 1,
				(ft_strlen(ptr + 1) + 1) * sizeof(char));
			*(p) -= 1;
			return (Q_DOUBLE);
		}
		else
		{
			return (Q_NONE);
		}
	}
	else if (mode == Q_DOUBLE && u_is_not_escaped(word, ptr) == TRUE)
	{
		(void)ft_memmove(word + (ptr - word), ptr + 1,
			(ft_strlen(ptr + 1) + 1) * sizeof(char));
		*(p) -= 1;
		return (Q_NONE);
	}
	return (mode);
}

static void			p_escape_bs(char *ptr, char word[], t_quote_mode mode)
{
	if (mode == Q_NONE)
	{
		(void)ft_memmove(word + (ptr - word), ptr + 1,
			(ft_strlen(ptr + 1) + 1) * sizeof(char));
	}
	else if (mode == Q_DOUBLE)
	{
		if (*(ptr + 1) == C_BACKS)
		{
			(void)ft_memmove(word + (ptr - word), ptr + 1,
				(ft_strlen(ptr + 1) + 1) * sizeof(char));
		}
		else if (*(ptr + 1) == C_DQUOTE)
		{
			(void)ft_memmove(word + (ptr - word), ptr + 1,
				(ft_strlen(ptr + 1) + 1) * sizeof(char));
		}
	}
}

static void			p_escape_arg(char word[])
{
	char			*ptr;
	t_quote_mode	mode;

	ptr = word;
	mode = Q_NONE;
	while (*ptr != C_NUL)
	{
		if (*ptr == C_SQUOTE)
			mode = p_escape_squote(ptr, &ptr, word, mode);
		else if (*ptr == C_DQUOTE)
			mode = p_escape_dquote(ptr, &ptr, word, mode);
		else if (*ptr == C_BACKS)
			p_escape_bs(ptr, word, mode);
		ptr++;
	}
}

void				p_args_escape_chars_and_quotes(char *words[])
{
	char	**ptr;
	char	*head;
	char	*rptr;

	ptr = words;
	while (*ptr != NULL)
	{
		p_escape_arg(*ptr);
		rptr = *ptr;
		head = rptr;
		while (*rptr != C_NUL)
		{
			if (*rptr == C_SUB && *(rptr + 1) == C_SUB)
			{
				(void)ft_memmove(head + (rptr - head), rptr + 1,
					(ft_strlen(rptr + 1) + 1) * sizeof(char));
				*rptr = C_BACKS;
			}
			rptr++;
		}
		ptr++;
	}
}