-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathp4.c
101 lines (82 loc) · 2.35 KB
/
p4.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
// TITLE: Sistemas Operativos Práctica 4
// AUTHOR 1: Miguel López López LOGIN 1: m.llopez
// AUTHOR 2: Xoel Díaz Préstamo LOGIN 2: xoel.diaz
// GROUP: 2.1.4
// DATE: 10/12/2021
#include "headers.h"
#include <stdio.h>
char* deleteEnter(char *str){
int length = strlen(str)-1;
if(str[length] == '\n')
str[length] = '\0';
return str;
}
int trocearCadena(char * str, char * tokens[]){ int i=1;
if ((tokens[0]=strtok(str," \n\t"))==NULL)
return 0;
while ((tokens[i]=strtok(NULL," \n\t"))!=NULL)
i++;
return i;
}
int process(char *tokens[], int ntokens, context *ctx) {
for(int i=0; cmds[i].cmd_name != NULL; i++) {
if(strcmp(tokens[0], cmds[i].cmd_name) ==0) {
return cmds[i].cmd_fun(tokens+1, ntokens-1, ctx);
}
}
if(strcmp(tokens[ntokens-1], "&")==0){
char *aux[ntokens+1];
for(int i=0;i<ntokens-1;i++){
aux[i] = tokens[i];
}
backlistAll(aux,ntokens-1,0,0,ctx);
}else
executeAll(tokens,ntokens,0,0,1);
//printf(RED"Comando no encontrado\n"RESET);
return 0;
}
int imprimirPrompt(char *line){
//uncomment to see working directory in the prompt
/*
char path[MAX_LINE];
getcwd(path, sizeof(path));
printf(GREEN"%s ",path);
*/
printf(BLUE"✦"GREEN"»> "RESET);
fgets(line, MAX_LINE, stdin);
return 0;
}
int leerEntrada(int end, char *line, context *ctx){
char *tokens[MAX_TOKENS];
int ntokens;
if(empiezaPor("comando", line) != 0){
struct histData *info = malloc(sizeof(struct histData));
strcpy(info->cmd, deleteEnter(line));
insert(&ctx->historial, info);
}
ntokens = trocearCadena(line, tokens);
end = process(tokens, ntokens, ctx);
return end;
}
int main(int argc, char* argv[], char* envp[]) {
char line [MAX_LINE];
int end=0;
context ctx;
ctx.historial = init_list();
ctx.malloc = init_list();
ctx.mmap = init_list();
ctx.shared = init_list();
ctx.envp = envp;
strcpy(ctx.error, "");
ctx.jobs= init_list();
while(!end) {
imprimirPrompt(line);
if (line[0]!='\n'){
end = leerEntrada(end, line, &ctx);
}
}
freeList(&ctx.historial,free);
freeList(&ctx.malloc,freeMem);
freeList(&ctx.mmap,freeMmap);
freeList(&ctx.shared,free);
}