-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpansion.c
177 lines (164 loc) · 4.02 KB
/
expansion.c
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* expansion.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rng <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/13 15:51:35 by ethanlim #+# #+# */
/* Updated: 2024/09/18 09:36:50 by rng ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void expand_string_helper_cuz_norminette_3(char **res, int *i, int exit_code)
{
int k;
char *temp;
k = -1;
temp = ft_itoa(exit_code);
while (temp[++k])
(*res)[(*i)++] = temp[k];
free(temp);
}
int expand_string_helper_cuz_norminette(char **res, char *line,
int *i, t_data *data)
{
int j;
char *temp;
j = 0;
if (line[j + 1] == '\0' || line[j + 1] == '\"'
|| line [j + 1] == '\'' || line[j + 1] == '\"' || line[j + 1] == ' ')
(*res)[(*i)++] = '$';
else if (line[j + 1] == '?')
{
expand_string_helper_cuz_norminette_3(res, i, data->exit_code);
j++;
}
else
{
temp = get_expand_string(get_substr(line, j), data->envp);
(*i) = copy_substr(&(*res), temp, (*i));
j++;
while (line[j] && line[j] != ' '
&& line[j] != '$' && line[j] != '\'' && line[j] != '\"')
j++;
j--;
}
j++;
return (j);
}
void expand_string_helper_cuz_norminette_2(char **res, char *line,
int *i, int *j)
{
(*res)[(*i)++] = line[(*j)++];
while (line[(*j)] && line[(*j)] != '\'')
(*res)[(*i)++] = line[(*j)++];
(*res)[(*i)++] = line[(*j)++];
}
char *expand_string(char *line, t_data *data)
{
int i;
char *res;
int double_quotes_flag;
int j;
j = 0;
i = 0;
res = malloc(sizeof(char) * 1000000);
double_quotes_flag = 0;
while (line[j])
{
if (line[j] == '\"')
{
res[i++] = line[j++];
double_quotes_flag = !(double_quotes_flag);
}
else if (line[j] == '$')
j += expand_string_helper_cuz_norminette(&res, line + j, &i, data);
else if (line[j] == '\'' && double_quotes_flag == 0)
expand_string_helper_cuz_norminette_2(&res, line, &i, &j);
else
res[i++] = line[j++];
}
res[i] = '\0';
return (res);
}
void expansion(t_tokens *list, t_data *data)
{
t_tokens *templist;
char *tempptr;
templist = list;
while (templist)
{
if (templist->type == STRING)
{
tempptr = templist->token;
templist->token = expand_string(templist->token, data);
free(tempptr);
}
templist = templist->next;
}
}
// char *expansion(char *line, t_lexing *lexer)
// {
// int i;
// int start;
// char *res;
// char *temp;
// int j;
// int k = 0;
// char *tmp;
// i = 0;
// j = 0;
// res = malloc(sizeof(char) * 1000000);
// while (line[j])
// {
// if (line[j] == '$')
// {
// start = j + 1;
// if(line[start] == '\0')
// res[i] = '$';
// else if(line[start] == '?')
// {
// while(line[j] == '$' && line[j + 1] == '?')
// {
// res[i] = lexer->exit_code + 48;
// i++;
// j += 2;
// }
// }
// else
// {
// while(line[j] && line[j] != ' ')
// j++;
// tmp = ft_substr(line, start, j - start);
// temp = get_expand_string(tmp, lexer->envp);
// free(tmp);
// if (temp != NULL)
// {
// k = 0;
// while(temp[k])
// {
// res[i] = temp[k];
// i++;
// k++;
// }
// free(temp);
// }
// }
// }
// if (line[j])
// res[i++] = line[j++];
// }
// res[i] = '\0';
// return res;
// }
// int main(int argc, char **argv, char **envp)
// {
// char temp[] = "$invalid";
// t_data data;
// data.envp = envp;
// data.exit_code = 0;
// char *str = expand_string(temp, &data);
// printf("%s\n", str);
// return 0;
// }