-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
212 lines (175 loc) · 4.36 KB
/
main.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
#include "headers.h"
void sigintHandler(int sig_num)
{
if(getpid() != shellPID)
return;
printf("\n To exit shell, please type exit \n");
if(curPID != -1)
{
kill(curPID, SIGINT);
for (int k = 0; k < process_count; k++)
{
if (process_list[k].pid == curPID)
{
strcpy(process_list[k].status, "killed");
break;
}
}
}
// generate_commandline();
fflush(stdout);
}
void sigintHandler3(int sig_num)
{
if(getpid() != shellPID)
return;
printf("Ctrl Z was pressed \n");
if(curPID != -1)
{
int flag =0;
kill(curPID, SIGTSTP);
for(int i=0; i<process_count; i++)
{
if(process_list[i].pid == curPID)
{
flag =1;
break;
}
}
if ( flag ==0 )
{
process_list[process_count].pid = curPID;
strcpy(process_list[process_count].pname, curPROCESS);
strcpy(process_list[process_count++].status, "stopped");
}
}
curPID=-1;
// generate_commandline();
fflush(stdout);
return;
}
char *read_command(void)
{
char *line = NULL;
ssize_t bufsize = 0; // have getline allocate a buffer for us
getline(&line, &bufsize, stdin);
for(int i =0; line[i]!='\0'; i++)
{
if(line[i]=='|')
{
is_pipe=1;
break;
}
}
return line;
}
int execute_pipes(char *args2)
{
char **args3;
char **args;
args3 = split_command(args2, "|");
int m=0;
//No of commands to be executed that are separated by pipes! save, split, redirect, exec, putback
while(args3[m]!=NULL)
{
no_of_pipes++;
m++;
}
int in = 0;
int fd[2];
int c,d;
int i;
for (i = 0; i < no_of_pipes-1; i++) //until the second last command
{
c = dup(0);
d = dup(1);
args = split_command(args3[i], " \t\r\n\a");
pipe(fd);
redirection(args);
execute_command(args, in, fd[1]);
close(fd[1]);
dup2(c,0);
dup2(d,1);
in=fd[0];
}
int e,f;
e=dup(0);
f=dup(1);
args = split_command(args3[i], " \t\r\n\a");
redirection(args);
execute_command(args, in, 1);
dup2(e,0);
dup2(f,1);
return 1;
}
void shell_loop()
{
char *line;
char **args;
char **args2;
char **args3;
int status;
process_count = 0;
pid_t pid;
int status2;
inp_redir = 0;
op_redir = 0;
app_redir = 0;
do
{
is_pipe=0;
no_of_pipes=0;
generate_commandline();
line = read_command();
args2 = split_command(line, ";");//semi colon separated
int k = 0;
while(args2[k] != NULL)
{
while((pid = waitpid(-1, &status2, WNOHANG)) > 0)
{
//bg process has ended
char pname[100];
for (int k = 0; k < process_count; k++)
if (process_list[k].pid == pid)
{
// process_list[k].pid = -1;
strcpy(process_list[k].status, "killed");
break;
}
if (WEXITSTATUS(status2) == 0)
printf("Process with pid %d exited normally\n", pid);
else
printf("Process with pid %d exited abnormally\n", pid);
}
if(is_pipe==1)
status = execute_pipes(args2[k]);
else
{
//Saving stdin and stdout
a = dup(0);
b = dup(1);
args = split_command(args2[k], " \t\r\n\a");//taking care of spaces, tabs etc.
redirection(args);
status = execute_command(args, 0, 1);
//Putting stdin and stdout back, as it may have changed during redirection
dup2(a,0);
dup2(b,1);
}
k++;
} // while(args2[k] != NULL)
free(line);
} while (status);
} // shell_loop
int main(int argc, char **argv)
{
curPID = -1;
shellPID = getpid();
ctrlZ = 0;
signal(SIGINT, sigintHandler);
// signal (SIGQUIT, sigintHandler2);
signal (SIGTSTP, sigintHandler3);
strcpy(addr, "~");
getcwd(home_addr, 1000);
shell_loop();
return 0;
}