-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparse_command.c
More file actions
412 lines (373 loc) · 14.2 KB
/
parse_command.c
File metadata and controls
412 lines (373 loc) · 14.2 KB
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
#include "built_in.c"
#define MAX_NUM_ARGUMENTS 50
#define MAX_HISTORY_LENGTH 10
#define MAX_COMMAND_LENGTH 100
int pipes_func(char *(*args)[MAX_NUM_ARGUMENTS], int num_cmds, int background)
{
int pipes[MAX_COMMAND_LENGTH - 1][2];
// Crear los pipes
for (int i = 0; i < num_cmds - 1; i++)
{
if (pipe(pipes[i]) == -1)
{
perror("pipe");
exit(EXIT_FAILURE);
}
}
// Crear los procesos hijos y ejecutar los comandos
for (int i = 0; i < num_cmds; i++)
{
pid_t pid = fork();
if (pid == -1)
{
perror("fork");
exit(EXIT_FAILURE);
}
else if (pid == 0)
{
// Proceso hijo
if (i > 0)
{
// Conectar la entrada estándar con el pipe anterior
dup2(pipes[i - 1][0], STDIN_FILENO);
}
if (i < num_cmds - 1)
{
// Conectar la salida estándar con el siguiente pipe
dup2(pipes[i][1], STDOUT_FILENO);
}
// Cerrar los descriptores de archivo de los pipes
for (int j = 0; j < num_cmds - 1; j++)
{
close(pipes[j][0]);
close(pipes[j][1]);
}
if (built_in(args[i], num_cmds, background) == 0)
{
exit(EXIT_SUCCESS);
}
/*if (execvp(args[i][0], args[i]) == -1)
{
return EXIT_FAILURE;
}*/
}
}
// Cerrar los descriptores de archivo de los pipes en el proceso padre
for (int i = 0; i < num_cmds - 1; i++)
{
close(pipes[i][0]);
close(pipes[i][1]);
}
// Esperar a que todos los procesos hijos terminen
for (int i = 0; i < num_cmds; i++)
{
wait(NULL);
}
int status;
pid_t pidt = getpid();
if (!background)
{
// Establecer el proceso hijo en primer plano
set_foreground(pidt);
waitpid(pidt, &status, WUNTRACED);
set_background(pidt);
}
else
{
// Establecer el proceso hijo en segundo plano
bg_pids[num_bg_pids] = pidt;
proces[num_bg_pids] = "Pipes";
num_bg_pids++;
set_background(pidt);
}
return EXIT_SUCCESS;
}
void ended_processes()
{
while ((pid_gen = waitpid(-1, NULL, WNOHANG)) > 0 || num_bg_pids != 0) // terminar el proceso
{
int l = Process(pid_gen);
if (l == -1)
{
l = num_bg_pids - 1;
}
printf("[%d] Done -> %s\n", il++, proces[l]);
fg(l);
for (int j = num_bg_pids - 1; j > 0; j--)
{
bg_pids[j - 1] = bg_pids[j];
if (j - 2 >= 0)
{
proces[j - 2] = proces[j - 1];
}
}
}
}
/**
* @brief El metodo se encarga de procesar cada comando simple o estandarizado.
* @param arguments Matriz con la entrada tokenizada del usuario.
* @param last_null Puntero al comienzo del comando.
* @param num_arguments Numero de argumentos tokenizados.
* @param auxiliar1 Esacio donde sera almacenado el comando
* @param len Indice de auxiliar1
* @return No devuelve nada
*/
void auxiliar_process(char **arguments, int *last_null, int num_arguments, char **auxiliar1, int *len)
{
int i = *last_null + 1; // creo un indice q empieza en el token siguiente al ultimo null
*last_null = num_arguments; // actualizo el ultimo null como el operador actual
while (i < num_arguments) // tomo todo lo que hay entre operador y operador
{
auxiliar1[*len] = arguments[i]; // lo agrego a auxiliar
i++; // aumento los indices
(*len)++;
}
auxiliar1[*len] = NULL; // hago null la ultima posicion
}
/**
* @brief El metodo se encarga de procesar cada comando simple o estandarizado.
* @param arguments Matriz con la entrada tokenizada del usuario.
* @param last_null Puntero al comienzo del comando.
* @param num_arguments Numero de argumentos tokenizados.
* @return Devuelve 1 si ocurrio algun error y 0 e.o.c.
*/
int tokenized_util(char **arguments, int *last_null, int num_arguments, int background)
{
char *auxiliar1[MAX_NUM_ARGUMENTS]; // construyo el comando simple que voy a procesar
int len = 0; // creo un indice para ir construyendo auxiliar1
auxiliar_process(arguments, last_null, num_arguments, auxiliar1, &len);
int std_status = std_method(auxiliar1, len, background);
if (std_status == 1)
std_status = built_in(auxiliar1, len, background);
return std_status;
}
/**
* @brief El metodo se encarga de parsear cada comando y ejecutarlo.
* @param parsed_arguments Matriz con la entrada tokenizada del usuario.
* @return No devuelve nada.
*/
void tokenized(char *parsed_arguments, int background)
{
char *operators[] = {"then", "else", "end"
"||",
"&&", NULL};
char *token;
int piped_len = 0;
char *piped_args[MAX_NUM_ARGUMENTS][MAX_NUM_ARGUMENTS];
char *arguments[MAX_NUM_ARGUMENTS]; // Arreglo de punteros a los argumentos del comando
int num_arguments = 0; // Inicializar el número de argumentos
int last_null = -1;
int or_status = 0;
int if_status = 0;
int else_status = 0;
int else_before_end = 0;
int end_status = 0;
int not_else = 0;
int first_true = 0;
int piped = 0;
int piped_out = 0;
token = strtok(parsed_arguments, " "); // Obtener el primer token del comando
while (token != NULL && num_arguments < MAX_NUM_ARGUMENTS - 1) // Mientras haya tokens y no se haya alcanzado el número máximo de argumentos
{
if (or_status == 1)
{
return;
}
arguments[num_arguments] = token; // Agregar el token al arreglo de argumentos
if (piped != 0)
{
int end_pipe = 0;
int if_flag = 0;
for (int i = 0; i < 4; i++)
{
if (strcmp(token, "if") == 0)
{
last_null++;
num_arguments++; // Incrementar el número de argumentos
token = strtok(NULL, " "); // Obtener el siguiente token del comando
if_flag = 1;
}
else if (strcmp(token, operators[i]) == 0)
{
end_pipe = 1;
break;
}
}
if (if_flag == 1)
continue;
else if (end_pipe == 1)
{
char *args1[MAX_NUM_ARGUMENTS]; // construyo el comando simple que voy a procesar
int len1 = 0; // creo un indice para ir construyendo auxiliar1
auxiliar_process(arguments, &last_null, num_arguments, args1, &len1);
for (int i = 0; i < MAX_NUM_ARGUMENTS; i++)
{
piped_args[piped_len][i] = args1[i];
}
if (pipes_func(piped_args, piped_len, background) == EXIT_SUCCESS)
{
piped_out = 1;
piped = 0;
}
}
}
if (strcmp(token, "|") == 0)
{
piped = num_arguments;
char *args1[MAX_NUM_ARGUMENTS]; // construyo el comando simple que voy a procesar
int len1 = 0; // creo un indice para ir construyendo auxiliar1
auxiliar_process(arguments, &last_null, piped, args1, &len1);
for (int i = 0; i < MAX_NUM_ARGUMENTS; i++)
{
piped_args[piped_len][i] = args1[i];
}
piped_len++;
}
else if (strcmp(token, "if") == 0)
last_null++;
else if (strcmp(token, "&&") == 0)
{
int std_status = tokenized_util(arguments, &last_null, num_arguments, background);
if (std_status == 1)
return;
}
else if (strcmp(token, "||") == 0)
{
int std_status = tokenized_util(arguments, &last_null, num_arguments, background);
if (std_status == 0)
{
or_status = 1;
}
}
else if (strcmp(token, "then") == 0 && first_true == 0)
{
if (strcmp(arguments[last_null], "if") == 0)
{
if_status += 1; // esto me da cuantos if tengo actualmente
int std_status;
if (piped == 0 && piped_out == 1)
std_status = 0;
else
std_status = tokenized_util(arguments, &last_null, num_arguments, background);
else_status = 0; // actualizo los estados de else
else_before_end = 0; // para el nuevo bloque de if
if (std_status == 1) // si hubo un error en la condicion
{
else_status = 1; // actualizo el else para que se ejecute
}
else
{
end_status = 1; // actualizo el end para que se ejecute si no hay else
first_true = 1;
}
}
else
printf("then: \"if\" command is missing\n");
}
else if (strcmp(token, "else") == 0 && first_true != 2)
{
else_before_end = 1;
if (else_status == 1) // si hubo error en la condicion
{
last_null = num_arguments; // last_null ahora es el else
}
else // si no hubo error
{
int std_status = tokenized_util(arguments, &last_null, num_arguments, background); // ejecuto el comando entre then y else
not_else = 1; // actualizo el estado de no ejecutar lo que hay en el else
first_true = 2;
}
}
else if (strcmp(token, "end") == 0)
{
if (first_true == 2 || (not_else == 1 && if_status > 0)) // si no hay que ejecutar el contenido del else y hubo un if al menos
{
last_null = num_arguments; // last_null es el end actual
if_status -= 1; // quito un if de los que haya sin finalizar
}
else if (else_status == 1 && else_before_end == 1) // si hay que ejecutar el contenido del else
{
int std_status = tokenized_util(arguments, &last_null, num_arguments, background); // ejecuto el comando
else_status = 0; // actualizo el else como ejecutado
if_status -= 1; // quito un if de los que haya sin finalizar
}
else if (end_status == 1 && else_before_end == 0 && strcmp(arguments[last_null], "end") != 0)
{
int std_status = tokenized_util(arguments, &last_null, num_arguments, background);
if_status -= 1;
}
else
{
last_null = num_arguments;
if_status -= 1;
}
if (if_status == 0) // si no quedan if por finalizar
not_else = 0; // actualizo el estado de los else
else_before_end = 0;
}
num_arguments++; // Incrementar el número de argumentos
token = strtok(NULL, " "); // Obtener el siguiente token del comando
}
arguments[num_arguments] = NULL; // Agregar un puntero nulo al final del arreglo de argumentos
ended_processes();
if (strcmp(arguments[num_arguments - 1], "&") == 0)
background = 1;
else
background = 0;
if (background)
{
arguments[num_arguments - 1] = '\0';
num_arguments--;
}
if (piped != 0)
{
char *args2[MAX_NUM_ARGUMENTS]; // construyo el comando simple que voy a procesar
int len2 = 0; // creo un indice para ir construyendo auxiliar1
int aux1 = piped;
auxiliar_process(arguments, &aux1, num_arguments, args2, &len2);
for (int i = 0; i < MAX_NUM_ARGUMENTS; i++)
{
piped_args[piped_len][i] = args2[i];
}
piped_len++;
if (pipes_func(piped_args, piped_len, background) == EXIT_SUCCESS)
{
// fg(bg_pids[num_bg_pids - 1], 1);
return;
}
}
if (if_status > 0)
{
printf("Fatal error: \"end\" command is missing\n");
exit(1);
}
if (num_arguments > 0 && num_arguments - 1 != last_null) // Ejecuta el ultimo comando si aun no se ha hecho
{
tokenized_util(arguments, &last_null, num_arguments, background);
}
}
/**
* @brief El metodo se encarga de parsear cada comando y ejecutarlo.
* @param command Entrada del usuario sin procesar
* @return No devuelve nada.
*/
void parse_command(char *command)
{
char *parsed_arguments[MAX_NUM_ARGUMENTS];
int num_tok = 0;
int background = 0;
char *token = strtok(command, ";"); // Obtener el primer token del comando
while (token != NULL && num_tok < MAX_NUM_ARGUMENTS - 1) // Mientras haya tokens y no se haya alcanzado el número máximo de argumentos
{
parsed_arguments[num_tok] = token; // Agregar el token al arreglo de argumentos
num_tok++; // Incrementar el número de argumentos
token = strtok(NULL, ";"); // Obtener el siguiente token del comando
}
parsed_arguments[num_tok] = NULL; // Agregar un puntero nulo al final del arreglo de argumentos
int index = 0;
while (parsed_arguments[index] != NULL && index < MAX_NUM_ARGUMENTS - 1)
{
tokenized(parsed_arguments[index], background);
index++;
}
}