summaryrefslogtreecommitdiffstats
path: root/src/p_lblock_next.c
blob: 9def2307250b2ac64b04275e80df5f62fba23d07 (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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   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_double_them_bs(char varval[])
{
	char	*ptr;

	ptr = varval;
	while (*ptr != C_NUL)
	{
		if (*ptr == C_BACKS)
		{
			ptr = ft_memmove(ptr + 1, ptr, ft_strlen(ptr) + 1);
			varval[ptr - varval] = C_BACKS;
		}
		ptr++;
	}
	return (varval);
}

static void		p_subst_this_var(char **p, int64_t i, char word[], t_msh *msh)
{
	char	tmp[ARG_MAX];
	char	varval[ARG_MAX];
	char	*ptr;
	size_t	varlen;

	ptr = word;
	varlen = i + 1;
	while (ptr[varlen] != C_NUL &&
		ft_ischarset("$=\\/@%^*+{}[]<>(),.-", ptr[varlen]) == FALSE &&
		ft_iswhitespace(ptr[varlen]) == FALSE)
		varlen += 1;
	ft_strlcpy(tmp, ptr + i, varlen + 1 - i);
	u_get_var_value(varval, tmp, ARG_MAX, msh);
	p_double_them_bs(varval);
	(void)ft_memmove(ptr + (i + ft_strlen(varval)),
		ptr + varlen,
		(ft_strlen(ptr + varlen) + 1) * sizeof(char));
	(void)ft_memmove(word + i, varval, ft_strlen(varval) * sizeof(char));
	*(p) = word + (i + ft_strlen(varval) - 1);
}

void			p_subst_vars(char word[], t_msh *msh)
{
	char			*ptr;
	t_quote_mode	mode;

	mode = Q_NONE;
	ptr = word;
	while (*ptr != C_NUL)
	{
		if (*ptr == C_DQUOTE)
			mode = u_meet_dquote(word, ptr, mode);
		else if (*ptr == C_SQUOTE)
			mode = u_meet_squote(word, ptr, mode);
		if ((mode == Q_NONE || mode == Q_DOUBLE) && *ptr == C_DOLLAR &&
			u_is_not_escaped(word, ptr) == TRUE)
		{
			p_subst_this_var(&ptr, (ptr - word), word, msh);
		}
		ptr++;
	}
}








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);
}