summaryrefslogtreecommitdiffstats
path: root/src/p_args_escape.c
blob: 6e4422b127c0a0285c1d92f6ecb77972e3bb2905 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   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++;
	}
}