-
Notifications
You must be signed in to change notification settings - Fork 0
/
command.c
313 lines (267 loc) · 7.14 KB
/
command.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#include <stdbool.h>
#include <glib.h>
#include "command.h"
#include <assert.h>
#include "strextra.h"
#include <string.h>
struct scommand_s
{
GList *command;
char *args_in;
char *args_out;
};
scommand scommand_new(void)
{
scommand init;
init = calloc(1, sizeof(struct scommand_s));
init->args_in = NULL;
init->args_out = NULL;
init->command = NULL;
assert(init != NULL && scommand_is_empty(init) &&
scommand_get_redir_in(init) == NULL &&
scommand_get_redir_out(init) == NULL);
return init;
}
scommand scommand_destroy(scommand self)
{
assert(self != NULL);
if (self->command != NULL)
{
g_list_free_full(self->command, free); /* liberar la memoria de la lista */
self->command = NULL;
}
free(self->args_in);
self->args_in = NULL;
free(self->args_out);
self->args_out = NULL;
free(self);
self = NULL;
assert(self == NULL);
return self;
}
void scommand_push_back(scommand self, char *argument)
{
assert(self != NULL && argument != NULL);
self->command = g_list_append(self->command, argument);
assert(!scommand_is_empty(self));
}
void scommand_pop_front(scommand self)
{
assert(self != NULL && !scommand_is_empty(self));
free(g_list_nth_data(self->command, 0));
self->command = g_list_delete_link(self->command, self->command);
}
void scommand_set_redir_in(scommand self, char *filename)
{
assert(self != NULL);
free(self->args_in);
self->args_in = filename;
}
void scommand_set_redir_out(scommand self, char *filename)
{
assert(self != NULL);
free(self->args_out);
self->args_out = filename;
}
bool scommand_is_empty(const scommand self)
{
assert(self != NULL);
return self->command == NULL;
}
unsigned int scommand_length(const scommand self)
{
assert(self != NULL);
unsigned int length = g_list_length(self->command);
assert((length == 0)== scommand_is_empty(self));
return length;
}
char *scommand_front(const scommand self)
{
assert(self != NULL && !scommand_is_empty(self));
char *front;
front = g_list_nth_data(self->command, 0);
assert(front != NULL);
return front;
}
char *scommand_get_argument(const scommand self, int i)
{
assert(self != NULL && !scommand_is_empty(self));
assert(i>=0);
char *argument = g_list_nth_data(self->command, i);
return argument;
}
void scommand_to_array(const scommand self, char ** args)
{
assert(self != NULL);
unsigned int length = scommand_length(self);
for (unsigned int i = 0; i < length; i++)
{
args[i] = scommand_get_argument(self,i);
}
}
char *scommand_get_redir_in(const scommand self)
{
assert(self != NULL);
return self->args_in;
}
char *scommand_get_redir_out(const scommand self)
{
assert(self != NULL);
return self->args_out;
}
char *scommand_to_string(const scommand self)
{
assert(self != NULL);
GList *list = self->command;
char *args_in = scommand_get_redir_in(self);
char *args_out = scommand_get_redir_out(self);
size_t length = 0;
char *str=strdup("");
if (list != NULL)
{
char *list_str;
for (unsigned int i = 0; i < scommand_length(self); i++)
{
length = strlen(str);
list_str = g_list_nth_data(list, i);
length = (length + strlen(list_str));
str = realloc(str, (length + (size_t) 1) * sizeof(char));
str = strcat(str, list_str);
str = strcat(str, " ");
};
if (args_in != NULL)
{
length = strlen(str) + strlen(args_in);
str = realloc(str, (length + 3) * sizeof(char));
str = strcat(str, " < ");
str = strcat(str, args_in);
};
if (args_out != NULL)
{
length = strlen(str) + strlen(args_out);
str = realloc(str, (length + 3) * sizeof(char));
str = strcat(str, " > ");
str = strcat(str, args_out);
};
}
assert(
scommand_is_empty(self) ||
scommand_get_redir_in(self) == NULL ||
scommand_get_redir_out(self) == NULL ||
strlen(str) > 0);
return str;
}
struct pipeline_s
{
GList *commands_queue;
bool wait;
bool secuencial;
};
pipeline pipeline_new(void)
{
pipeline init;
init = calloc(1, sizeof(struct pipeline_s));
init->wait = true;
init->commands_queue = NULL;
assert(init != NULL && pipeline_is_empty(init) && pipeline_get_wait(init));
return init;
}
pipeline pipeline_destroy(pipeline self)
{
assert(self != NULL);
if (self->commands_queue != NULL)
{
while(self->commands_queue!=NULL)
{
pipeline_pop_front(self);
}
self->commands_queue = NULL;
}
free(self);
self = NULL;
assert(self == NULL);
return self;
}
void pipeline_push_back(pipeline self, scommand sc)
{
assert(self != NULL && sc != NULL);
self->commands_queue = g_list_append(self->commands_queue, sc);
assert(!pipeline_is_empty(self));
}
void pipeline_pop_front(pipeline self)
{
assert(self != NULL && !pipeline_is_empty(self));
scommand_destroy(pipeline_front(self));
self->commands_queue = g_list_delete_link(self->commands_queue, self->commands_queue);
}
void pipeline_set_wait(pipeline self, const bool w)
{
assert(self != NULL);
self->wait = w;
}
void pipeline_set_secuencial(pipeline self, const bool s)
{
assert(self != NULL);
self->secuencial = s;
}
bool pipeline_is_empty(const pipeline self)
{
assert(self != NULL);
return self->commands_queue == NULL;
}
unsigned int pipeline_length(const pipeline self)
{
assert(self != NULL);
unsigned int length = g_list_length(self->commands_queue);
assert((length==0) == pipeline_is_empty(self));
return length;
}
scommand pipeline_front(const pipeline self)
{
assert(self != NULL && !pipeline_is_empty(self));
return g_list_nth_data(self->commands_queue, 0);
}
bool pipeline_get_wait(const pipeline self)
{
assert(self != NULL);
return self->wait;
}
bool pipeline_get_secuencial(const pipeline self)
{
assert(self != NULL);
return self->secuencial;
}
char *pipeline_to_string(const pipeline self)
{
assert(self != NULL);
GList *list = self->commands_queue;
size_t length;
char *str;
if (list != NULL)
{
char *list_str;
str = scommand_to_string(g_list_nth_data(list, 0));
for (unsigned int i = 1; i < pipeline_length(self); i++)
{
length=strlen(str);
list_str=scommand_to_string(g_list_nth_data(list, i));
length = (length + strlen(list_str));
str = realloc(str, (length + (size_t) 3) * sizeof(char));
str = strcat(str, " | ");
str = strcat(str, (list_str));
};
free(list_str);
if (!self->wait)
{
length=strlen(str);
str = realloc(str, (length + (size_t) 2) * sizeof(char));
str = strcat(str, " &");
}
}
else
{
str=strdup("");
}
assert(pipeline_is_empty(self) || pipeline_get_wait(self) || strlen(str) > 0);
return str;
}