-
Notifications
You must be signed in to change notification settings - Fork 11
/
rep_var.c
183 lines (160 loc) · 3.22 KB
/
rep_var.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
178
179
180
181
182
183
#include "main.h"
/**
* check_env - checks if the typed variable is an env variable
*
* @h: head of linked list
* @in: input string
* @data: data structure
* Return: no return
*/
void check_env(r_var **h, char *in, data_shell *data)
{
int row, chr, j, lval;
char **_envr;
_envr = data->_environ;
for (row = 0; _envr[row]; row++)
{
for (j = 1, chr = 0; _envr[row][chr]; chr++)
{
if (_envr[row][chr] == '=')
{
lval = _strlen(_envr[row] + chr + 1);
add_rvar_node(h, j, _envr[row] + chr + 1, lval);
return;
}
if (in[j] == _envr[row][chr])
j++;
else
break;
}
}
for (j = 0; in[j]; j++)
{
if (in[j] == ' ' || in[j] == '\t' || in[j] == ';' || in[j] == '\n')
break;
}
add_rvar_node(h, j, NULL, 0);
}
/**
* check_vars - check if the typed variable is $$ or $?
*
* @h: head of the linked list
* @in: input string
* @st: last status of the Shell
* @data: data structure
* Return: no return
*/
int check_vars(r_var **h, char *in, char *st, data_shell *data)
{
int i, lst, lpd;
lst = _strlen(st);
lpd = _strlen(data->pid);
for (i = 0; in[i]; i++)
{
if (in[i] == '$')
{
if (in[i + 1] == '?')
add_rvar_node(h, 2, st, lst), i++;
else if (in[i + 1] == '$')
add_rvar_node(h, 2, data->pid, lpd), i++;
else if (in[i + 1] == '\n')
add_rvar_node(h, 0, NULL, 0);
else if (in[i + 1] == '\0')
add_rvar_node(h, 0, NULL, 0);
else if (in[i + 1] == ' ')
add_rvar_node(h, 0, NULL, 0);
else if (in[i + 1] == '\t')
add_rvar_node(h, 0, NULL, 0);
else if (in[i + 1] == ';')
add_rvar_node(h, 0, NULL, 0);
else
check_env(h, in + i, data);
}
}
return (i);
}
/**
* replaced_input - replaces string into variables
*
* @head: head of the linked list
* @input: input string
* @new_input: new input string (replaced)
* @nlen: new length
* Return: replaced string
*/
char *replaced_input(r_var **head, char *input, char *new_input, int nlen)
{
r_var *indx;
int i, j, k;
indx = *head;
for (j = i = 0; i < nlen; i++)
{
if (input[j] == '$')
{
if (!(indx->len_var) && !(indx->len_val))
{
new_input[i] = input[j];
j++;
}
else if (indx->len_var && !(indx->len_val))
{
for (k = 0; k < indx->len_var; k++)
j++;
i--;
}
else
{
for (k = 0; k < indx->len_val; k++)
{
new_input[i] = indx->val[k];
i++;
}
j += (indx->len_var);
i--;
}
indx = indx->next;
}
else
{
new_input[i] = input[j];
j++;
}
}
return (new_input);
}
/**
* rep_var - calls functions to replace string into vars
*
* @input: input string
* @datash: data structure
* Return: replaced string
*/
char *rep_var(char *input, data_shell *datash)
{
r_var *head, *indx;
char *status, *new_input;
int olen, nlen;
status = aux_itoa(datash->status);
head = NULL;
olen = check_vars(&head, input, status, datash);
if (head == NULL)
{
free(status);
return (input);
}
indx = head;
nlen = 0;
while (indx != NULL)
{
nlen += (indx->len_val - indx->len_var);
indx = indx->next;
}
nlen += olen;
new_input = malloc(sizeof(char) * (nlen + 1));
new_input[nlen] = '\0';
new_input = replaced_input(&head, input, new_input, nlen);
free(input);
free(status);
free_rvar_list(&head);
return (new_input);
}