-
Notifications
You must be signed in to change notification settings - Fork 0
/
shs_tokenize.c
210 lines (183 loc) · 3.58 KB
/
shs_tokenize.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include "shs.h"
/**
* add_nodes - add separators and command lines in the lists
*
* @head_s: head of separator list
* @head_l: head of command lines list
* @input: input string
* Return: no return
*/
void add_nodes(div_list **head_s, line_list **head_l, char *input)
{
int i;
char *line;
input = swap_char(input, 0);
for (i = 0; input[i]; i++)
{
if (input[i] == ';')
add_div_node_end(head_s, input[i]);
if (input[i] == '|' || input[i] == '&')
{
add_div_node_end(head_s, input[i]);
i++;
}
}
line = _strtok(input, ";|&");
do {
line = swap_char(line, 1);
add_line_node_end(head_l, line);
line = _strtok(NULL, ";|&");
} while (line != NULL);
}
/**
* next_cmd - go to the next command line stored
*
* @list_s: separator list
* @list_l: command line list
* @shdata: data structure
* Return: no return
*/
void next_cmd(div_list **list_s, line_list **list_l, shell_data *shdata)
{
int loop_sep;
div_list *ls_s;
line_list *ls_l;
loop_sep = 1;
ls_s = *list_s;
ls_l = *list_l;
while (ls_s != NULL && loop_sep)
{
if (shdata->status == 0)
{
if (ls_s->separator == '&' || ls_s->separator == ';')
loop_sep = 0;
if (ls_s->separator == '|')
ls_l = ls_l->next, ls_s = ls_s->next;
}
else
{
if (ls_s->separator == '|' || ls_s->separator == ';')
loop_sep = 0;
if (ls_s->separator == '&')
ls_l = ls_l->next, ls_s = ls_s->next;
}
if (ls_s != NULL && !loop_sep)
ls_s = ls_s->next;
}
*list_s = ls_s;
*list_l = ls_l;
}
/**
* div_commands - splits command lines according to
* the separators ;, | and &, and executes them
*
* @shdata: data structure
* @input: input string
* Return: 0 to exit, 1 to continue
*/
int div_commands(shell_data *shdata, char *input)
{
div_list *head_s, *list_s;
line_list *head_l, *list_l;
int loop;
head_s = NULL;
head_l = NULL;
add_nodes(&head_s, &head_l, input);
list_s = head_s;
list_l = head_l;
while (list_l)
{
shdata->input = list_l->line;
shdata->args = tokenize_line(shdata->input);
loop = exec_line(shdata);
free(shdata->args);
if (loop == 0)
break;
next_cmd(&list_s, &list_l, shdata);
if (list_l)
list_l = list_l->next;
}
free_div_list(&head_s);
free_line_list(&head_l);
if (loop == 0)
return (0);
return (1);
}
/**
* tokenize_line - tokenizes the input string
*
* @input: input string.
* Return: string splitted.
*/
char **tokenize_line(char *input)
{
size_t bsize;
size_t i;
char **tokens;
char *token;
bsize = TOKEN_SIZE;
tokens = malloc(sizeof(char *) * (bsize));
if (!tokens)
{
write(STDERR_FILENO, ": allocation error\n", 18);
exit(EXIT_FAILURE);
}
token = _strtok(input, TOKEN_DELIM);
tokens[0] = token;
for (i = 1; token != NULL; i++)
{
if (i == bsize)
{
bsize += TOKEN_SIZE;
tokens = _realloc_ptr(tokens, i, sizeof(char *) * bsize);
if (!tokens)
{
write(STDERR_FILENO, ": allocation error\n", 18);
exit(EXIT_FAILURE);
}
}
token = _strtok(NULL, TOKEN_DELIM);
tokens[i] = token;
}
return (tokens);
}
/**
* swap_char - swaps | and & for non-printed chars
*
* @input: input string
* @bool: type of swap
* Return: swapped string
*/
char *swap_char(char *input, int bool)
{
int i;
if (bool == 0)
{
for (i = 0; input[i]; i++)
{
if (input[i] == '|')
{
if (input[i + 1] != '|')
input[i] = 16;
else
i++;
}
if (input[i] == '&')
{
if (input[i + 1] != '&')
input[i] = 12;
else
i++;
}
}
}
else
{
for (i = 0; input[i]; i++)
{
input[i] = (input[i] == 16 ? '|' : input[i]);
input[i] = (input[i] == 12 ? '&' : input[i]);
}
}
return (input);
}