-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrompt.c
More file actions
367 lines (332 loc) · 9.95 KB
/
Copy pathPrompt.c
File metadata and controls
367 lines (332 loc) · 9.95 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
//
// Created by yaelao on 5/30/23.
//
#include "Prompt.h"
/**
* Esta funcion obtiene el valor de un comando de entrada.
* @param key El comando de entrada.
* @return El valor del comando de entrada.
* */
enum Option value_of(char *key) {
int i;
for (i = 0; i < sizeof(lookup_table) / sizeof(Pair); i++) {
if (strcmp(key, lookup_table[i].key) == 0) {
return lookup_table[i].value;
}
}
return -1;
}
void init_shell(void) {
process_queue = create_queue();
init_memory();
}
/**
* Esta funcion se encarga de hacer un split de los argumentos
* que introduce el usuario.
* @param args: String con los argumentos pasados.
* @return un arreglo de strings con los argumentos separados.
* */
char **split_args(char *args) {
char **args_array = malloc(100 * sizeof(char *));
char *arg = strtok(args, " ");
int i = 0;
while (arg != NULL) {
arg = strtok(NULL, " ");
args_array[i++] = arg;
}
args_array[i] = NULL;
return args_array;
}
/**
* Esta función se encarga de verificar que la cantidad de argumentos pasados
* sea la correcta.
*
* @param args: los argumentos pasados por el usuario.
* @param num_of_args: la cantidad de argumentos que se espera recibir.
* @return 0 si la cantidad de argumentos es correcta, -1 en caso contrario.
* */
bool verify_num_of_args(char **args, int num_of_args) {
int i = 0;
while (args[i] != NULL) {
i++;
}
if (i < num_of_args) {
printf("Error: missing arguments.\n");
return false;
}
if (args[num_of_args] != NULL) {
printf("Error: too many arguments.\n");
return false;
}
return true;
}
/**
* Esta funcion se encarga de hacer un split de los argumentos
* que introduce el usuario.
* @param args: String con los argumentos pasados.
* @return un arreglo de strings con los argumentos separados.
* */
char **split_args_execvp(char *args) {
char **args_array = malloc(100 * sizeof(char *));
char *arg = strtok(args, " ");
int i = 0;
args_array[i++] = arg;
while (arg != NULL) {
arg = strtok(NULL, " ");
if (arg != NULL && strcmp(arg, "|") == 0)
break;
/**
* Si el usuario introduce un argumento con espacios, se
* debe de tomar en cuenta que el argumento se encuentra
* entre comillas simples.
* */
if (arg != NULL && strcmp(arg, "\'") == 0) {
args_array[i++] = " ";
arg = strtok(NULL, "\' ");
}
args_array[i++] = arg;
}
args_array[i] = NULL;
return args_array;
}
/**
* Esta funcion se encarga de ejecutar un comando simple.
* @param args: Arreglo de strings con
* los argumentos del comando.
* */
void exec_single_command(char **args) {
pid_t pid = fork();
if (pid == 0) {
if (execvp(args[0], args) == -1) {
printf("Error executing command\n");
}
} else {
wait(NULL);
}
}
/**
* En caso de que el usuario introduzca un pipe,
* se ejecuta esta funcion.
* @param first_section: Arreglo de strings con los argumentos
* del primer comando.
* @param second_section: Arreglo de strings con los argumentos
* del segundo comando.
* */
void exec_pipe(char **first_section, char **second_section) {
pid_t first_section_pid, second_section_pid;
int pipe_descriptor[2];
pipe(pipe_descriptor);
/**
* Se crea un nodo hijo para poder ejecutar
* la primera seccion del pipe.
* */
first_section_pid = fork();
if (first_section_pid == 0) {
close(pipe_descriptor[READ_END]);
/*Se redirecciona la parte de escritura
* del pipe a la salida estandar*/
dup2(pipe_descriptor[WRITE_END], STDOUT_FILENO);
close(pipe_descriptor[WRITE_END]);
execvp(first_section[0], first_section);
} else {
wait(NULL);
/**
* Se crea un segundo nodo hijo para poder ejecutar
* la segunda seccion del pipe.
* */
second_section_pid = fork();
if (second_section_pid == 0) {
close(pipe_descriptor[WRITE_END]);
/*
* Se redirecciona la parte de lectura del pipe
* a la entrada estandar .
* */
dup2(pipe_descriptor[READ_END], STDIN_FILENO);
close(pipe_descriptor[READ_END]);
execvp(second_section[0], second_section);
} else {
close(pipe_descriptor[READ_END]);
close(pipe_descriptor[WRITE_END]);
wait(NULL);
}
}
}
/**
* Esta funcion se encarga de leer la entrada del usuario.
* @param input: String donde se guardara la entrada del usuario.
* @return un booleano que indica si el usuario introdujo un pipe.
* */
bool read_user_input(char *input) {
bool has_pipe = false;
char char_input;
printf("narco_barbie_69:~$ ");
while ((char_input = (char) getchar()) != '\n') {
if (char_input == '|') has_pipe = true;
strncat(input, &char_input, 1);
}
strncat(input, "\0", 1);
return has_pipe;
}
/**
* Esta funcion se encarga de hacer un split
* de los canales de la pipe.
* @param input: String con los canales de la pipe.
* @return un arreglo de strings con los canales separados.
* */
char **split_channels(char *input) {
char **channels = malloc(100 * sizeof(char *));
int i = 0;
char *channel;
while ((channel = strsep(&input, "|")) != NULL) {
channels[i++] = channel;
}
return channels;
}
void bash_commands(bool has_pipe, char *input) {
if (has_pipe) {
char **channels = split_channels(input);
char **first_section = split_args_execvp(channels[0]);
char **second_section = split_args_execvp(channels[1]);
exec_pipe(first_section, second_section);
free(channels);
free(first_section);
free(second_section);
} else {
char **args = split_args_execvp(input);
exec_single_command(args);
free(args);
}
}
/**
* Esta funcion se encarga de mostrar el prompt al usuario
* y de ejecutar los comandos introducidos por el usuario.
* */
void show_prompt() {
char *input = malloc(100 * sizeof(char));
bool has_pipe = read_user_input(input);
bool is_exit = strcmp(input, "exit") == 0;
if (is_exit) {
printf("bye\n");
free(input);
exit(0);
}
if (input == NULL) {
free(input);
return;
}
/* Copia de la entrada estandar, y con esta copia
* se obtiene el nombre del comando. */
char *cpy_one = malloc(100 * sizeof(char));
char *cpy_two = malloc(100 * sizeof(char));
strcpy(cpy_one, input);
strcpy(cpy_two, input);
char *command_name = strtok(cpy_one, " ");
char **args = split_args(cpy_two);
switch (value_of(command_name)) {
case ALLOC:
if (!verify_num_of_args(args, 2))
break;
else {
int pid = atoi(args[0]);
Process *process = get_process(process_queue, pid);
if (process == NULL) {
printf("Process not found\n");
break;
}
assign_memory(process, args[1]);
}
break;
case FREE:
if (!verify_num_of_args(args, 1))
break;
else {
int pid = atoi(args[0]);
Process *process = get_process(process_queue, pid);
if (process == NULL) {
printf("Process not found\n");
break;
}
free_memory(pid);
}
break;
case COMPACT:
if (!verify_num_of_args(args, 0))
break;
else {
compact_memory();
}
break;
case STATE:
if (!verify_num_of_args(args, 0))
break;
else {
report_memory();
}
break;
case MKPS:
if (!verify_num_of_args(args, 3))
break;
else {
int pid = atoi(args[0]);
int burst = atoi(args[1]);
int size = atoi(args[2]);
Process *process = make_process(pid, burst, size);
if (contains(process_queue, process, compare_process)) {
printf("Process already exist\n");
break;
}
enqueue(process_queue, process, sizeof(Process));
}
break;
case LSP:
if (!verify_num_of_args(args, 0))
break;
else {
print_queue(process_queue, print_process);
}
break;
case KILL:
if (!verify_num_of_args(args, 1))
break;
else {
int pid = atoi(args[0]);
Process *process = get_process(process_queue, pid);
if (process == NULL) {
printf("Process not found\n");
break;
}
free_memory(pid);
kill_process(process_queue, pid);
}
break;
case RR:
if (!verify_num_of_args(args, 1))
break;
else {
int quantum = atoi(args[0]);
if (quantum <= 0) {
printf("Invalid quantum\n");
break;
}
round_robin(process_queue, quantum);
}
break;
case FCFS:
if (!verify_num_of_args(args, 0))
break;
else {
first_come_first_served(process_queue);
}
break;
case SJF:
if (!verify_num_of_args(args, 0))
break;
else {
shortest_job_first(process_queue);
}
break;
default:
bash_commands(has_pipe, input);
break;
}
}