-
Notifications
You must be signed in to change notification settings - Fork 0
/
split.c
executable file
·212 lines (183 loc) · 3.6 KB
/
split.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
211
212
#include "main.h"
/**
* 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);
}
/**
* 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(sep_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_sep_node_end(head_s, input[i]);
if (input[i] == '|' || input[i] == '&')
{
add_sep_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);
}
/**
* go_next - go to the next command line stored
*
* @list_s: separator list
* @list_l: command line list
* @datash: data structure
* Return: no return
*/
void go_next(sep_list **list_s, line_list **list_l, data_shell *datash)
{
int loop_sep;
sep_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 (datash->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;
}
/**
* split_commands - splits command lines according to
* the separators ;, | and &, and executes them
*
* @datash: data structure
* @input: input string
* Return: 0 to exit, 1 to continue
*/
int split_commands(data_shell *datash, char *input)
{
sep_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 != NULL)
{
datash->input = list_l->line;
datash->args = split_line(datash->input);
loop = exec_line(datash);
free(datash->args);
if (loop == 0)
break;
go_next(&list_s, &list_l, datash);
if (list_l != NULL)
list_l = list_l->next;
}
free_sep_list(&head_s);
free_line_list(&head_l);
if (loop == 0)
return (0);
return (1);
}
/**
* split_line - tokenizes the input string
*
* @input: input string.
* Return: string splitted.
*/
char **split_line(char *input)
{
size_t bsize;
size_t i;
char **tokens;
char *token;
bsize = TOK_BUFSIZE;
tokens = malloc(sizeof(char *) * (bsize));
if (tokens == NULL)
{
write(STDERR_FILENO, ": allocation error\n", 18);
exit(EXIT_FAILURE);
}
token = _strtok(input, TOK_DELIM);
tokens[0] = token;
for (i = 1; token != NULL; i++)
{
if (i == bsize)
{
bsize += TOK_BUFSIZE;
tokens = _reallocdp(tokens, i, sizeof(char *) * bsize);
if (tokens == NULL)
{
write(STDERR_FILENO, ": allocation error\n", 18);
exit(EXIT_FAILURE);
}
}
token = _strtok(NULL, TOK_DELIM);
tokens[i] = token;
}
return (tokens);
}