-
Notifications
You must be signed in to change notification settings - Fork 0
/
interpretCmd.c
125 lines (123 loc) · 2.65 KB
/
interpretCmd.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
#include <stdio.h>
#include "shell.h"
#include <sys/types.h>
#include <sys/wait.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
int is_bg, size_running_procs = 0;
void interpretCmd (char* cmd)
{
char str_delim[] = ";";
char cmd_delim[] = " &\n\t\v\f\r\a";
char** cmds = parseStr(cmd, str_delim);
for (int i=0, pid, status; cmds[i] != NULL; ++i)
{
char* s = (char*)malloc(sizeof(char)*(strlen(cmds[i])+2));
strcpy(s, cmds[i]);
trim(s, cmds[i]);
strcpy(s, cmds[i]);
is_bg = 0;
if (pipe_handler(cmds[i]) != -1) continue;
for (int j=0; cmds[i][j]; ++j)
if (cmds[i][j] == '&')
{
cmds[i][j] = '\0';
is_bg = 1;
break;
}
char** args;
if (strncmp(cmds[i], "quit", 4) == 0)
execute_quit(cmds[i], args);
else if (strncmp(cmds[i], "cd", 2) == 0)
{
execute_cd(cmds[i], parseStr(cmds[i], cmd_delim));
continue;
}
else if (strncmp(cmds[i], "setenv", 6) == 0)
{
execute_setenv(cmds[i], parseStr(cmds[i], cmd_delim));
continue;
}
else if (strncmp(cmds[i], "unsetenv", 8) == 0)
{
execute_unsetenv(cmds[i], parseStr(cmds[i], cmd_delim));
continue;
}
else if (strncmp(cmds[i], "jobs", 4) == 0)
{
execute_jobs(1);
continue;
}
else if (strncmp(cmds[i], "kjob", 4) == 0)
{
execute_jobs(0);
execute_kjob(cmds[i], parseStr(cmds[i], cmd_delim));
continue;
}
else if (strncmp(cmds[i], "fg", 2) == 0)
{
execute_jobs(0);
execute_fg(cmds[i], parseStr(cmds[i], cmd_delim));
continue;
}
else if (strncmp(cmds[i], "bg", 2) == 0)
{
execute_jobs(0);
execute_bg(cmds[i], parseStr(cmds[i], cmd_delim));
continue;
}
else if (strncmp(cmds[i], "overkill", 8) == 0)
{
for (int j=0; j<size_running_procs; ++j)
{
kill(running[j].pid, 9);
running[j].pid = -1;
}
continue;
}
pid = fork();
if (pid == 0)
{
if (is_bg) setpgid(0, 0);
args = redirect_handler(cmds[i]);
if (!args) exit(0);
if (strncmp("echo", s, 4) == 0)
{
int no_of_quotes = 0;
for (int j=4; s[j]; ++j)
if (s[j] == '"') no_of_quotes++;
if (no_of_quotes % 2)
fprintf(stderr, "Error: invalid syntax\n");
else execute_echo(s+4);
}
else
{
int x = isAllowed(args[0]);
if (x >= 0)
{
is_bg = 0;
executeCmd(cmds[i], args, allowed_execs[x]);
}
else
{
execvp(args[0], args);
fprintf(stderr, "Error: command not found\n");
}
}
exit(0);
}
else
{
if (!is_bg) waitpid(pid, &status, WUNTRACED);
else
{
args = parseStr(cmds[i], cmd_delim);
strcpy(running[size_running_procs].pname, args[0]);
running[size_running_procs++].pid = pid;
}
}
}
}