-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell.c
More file actions
402 lines (271 loc) · 8.28 KB
/
shell.c
File metadata and controls
402 lines (271 loc) · 8.28 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
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <string.h>
#include <signal.h>
#include <sys/resource.h>
/*
*** pesudo code:
function parent_main()
register_child_signal(on_child_exit()) #done
setup_environment() #done
shell()
function on_child_exit() #done
reap_child_zombie() #done
write_to_log_file("Child terminated") #done
function setup_environment() #done
cd(Current_Working_Directory)
function shell()
do
parse_input(read_input()) #done
evaluate_expression():
switch(input_type): #done
case shell_builtin:
execute_shell_bultin();
case executable_or_error:
execute_command():
while command_is_not_exit
function execute_shell_bultin()
switch(command_type):
case cd:
case echo:
case export:
function execute_command() #done
child_pid = fork()
if child:
execvp(command argument_list)
print("Error)
exit()
else if parent and foreground:
waitpid(child)
*** future improvements:
** use perror() to print meaningful error messages #done
** use heder files for protoypes
*/
/************* Macros **************/
#define CWD_BUF_SIZE 2000
#define CLI_BUF_SIZE 100
/******* functions' prototype ********/
void parent_main();
void on_child_exit(); //
void setup_environment();
void shell();
char* parse_input();
int execute_shell_builtin();
int execute_command();
void write_to_file(int sig);
/************************************/
int main(void){
char* argument_list[] = {"ls", "-l", "-a", NULL};
// char* command = "ls";
//parent_main();
char input_line[CLI_BUF_SIZE];
printf("starting main()\n");
char* list[50];
parse_input(input_line, list);
// int exe_status = execute_shell_builtin(list[0], list, "FOREGROUND");
printf("end of main()\n");
//printf("argument list: %s", list);
//int status_code = execute_command(argument_list[0], argument_list, "BACKGROUND");
// // driver for write_to_file
// // write_to_file(0);
// char* attr[] = {"l", "file", NULL};
// char* command = "nano";
// execvp(command, attr);
}
/****** functions' declarations ******/
void parent_main(){
// debug
printf("inside parent_main\n");
signal (SIGCHLD, on_child_exit);
setup_environment();
//shell();
}
void on_child_exit(int sig){
// write child termination message to the log file
write_to_file(0);
//debug
printf("\ninside on_child_exit\n");
wait(NULL);
// pid_t pid;
// while (1) {
// pid = wait3 (NULL, WNOHANG, (struct rusage *)NULL );
// if (pid == 0)
// return;
// else if (pid == -1)
// return;
// else
// printf ("Return code: \n");
// }
// pid_t pid;
// while(1){
// // pid = wait(NULL);
// // if(pid == 0){
// // return;
// // }
// // else if(pid == -1){
// // return;
// // }
// // else{
// // perror("Child exit error");
// // }
// printf("\nWaiting for child to terminate..");
// end_pid = waitpid(child_pid, NULL, WUNTRACED);
// if (end_pid == -1) {
// status_code = -1;
// perror("Waitpid Error");
// exit(EXIT_FAILURE);
// }
// else if(end_pid == child_pid){
// // child ended
// printf("\nChild terminated\n");
// break;
// }
// }
// waitpid(-1,NULL,WNOHANG | WUNTRACED);
}
void setup_environment(){
// debug
printf("inside setup_environment\n");
char* CWD[CWD_BUF_SIZE];
getcwd(&CWD, CWD_BUF_SIZE);
chdir(CWD);
}
void shell(){
char argument_list[CLI_BUF_SIZE];
char command;
char* layer;
int arg_len;
while(strcmp(command, "exit")){
// read input from the user
strcpy(argument_list, parse_input());
command = argument_list[0];
arg_len = strlen(argument_list);
// background layer
if(!strcmp(argument_list[arg_len-1],"&")){
argument_list[arg_len-1] == NULL;
layer = "BACKGROUND";
}
// foreground layer
else{
argument_list[arg_len] == NULL;
layer = "FOREGROUND";
}
// built-in commands
if(command=="cd" || command=="echo" || command=="export" ){
execute_shell_builtin(command, argument_list, layer);
}
// executable commands
else{
execute_command(command, argument_list, layer);
}
}
}
char* parse_input(char input_line[], char argument_list[]){
int i = 0;
// char* argument_list[CLI_BUF_SIZE];
//debug
printf("Inside parse_input\n");
char *ptr;
fgets(input_line, CLI_BUF_SIZE, stdin);
if ((strlen(input_line) > 0) && (input_line[strlen (input_line) - 1] == '\n'))
input_line[strlen (input_line) - 1] = '\0';
ptr = strtok(input_line, " ");
while(ptr != NULL){
//printf("%s\n", ptr);
argument_list[i] = ptr;
i++;
ptr = strtok(NULL, " ");
}
// fgets(input_line, CLI_BUF_SIZE, stdin);
// // scanf ("%[^\n]%*c", input_line);
// printf("got input from fgets()\n");
// if ((strlen(input_line) > 0) && (input_line[strlen (input_line) - 1] == '\n'))
// input_line[strlen (input_line) - 1] = '\0';
// printf("len of input_line >> %ld\n", strlen(input_line));
// // argument_list[strlen(input_line) - 1] = NULL;
// for (i = 0; i < CLI_BUF_SIZE; i++) {
// argument_list[i] = (char*) strsep(&input_line, " ");
// printf("token >> %s\n", argument_list[i]);
// if (!strcmp(argument_list[i], NULL))
// break;
// if (strlen(argument_list[i]) == 0)
// i--;
// }
printf("returning from parse_input..\n");
return(argument_list);
}
int execute_shell_builtin(char* command, char* argument_list[], char layer[]){
printf("argumennt[0]: %s\n", command);
printf("argumennt[1]: %s\n", argument_list[1]);
if(!strcmp(command, "cd")){
chdir(argument_list[1]);
printf("built-in attr for cd command is: %s\n", argument_list[1]);
}
else if(!strcmp(command, "echo")){ // not so completely functional
int i = 1;
// print all arguments to the right of the "echo" command
while(strcmp(argument_list[i],NULL)){
printf("%s", argument_list[i]);
i++;
}
}
else if(!strcmp(command, "export")){
// implement
}
else{
perror("command not found");
}
}
int execute_command(char*command, char* argument_list[], char layer[]){
printf("\nexecute_command in action..");
fflush(stdout);
int status_code = 0;
pid_t end_pid, child_pid;
child_pid = fork();
if(child_pid == -1){
status_code = -1;
perror("Fork Error");
exit(EXIT_FAILURE);
}
else if(child_pid == 0){
printf("\nChild PID is >> %d\n\nExecuting child..\n", getpid());
execvp(command, argument_list);
status_code = -1;
printf("Error");
exit(EXIT_SUCCESS);
}
//debug
printf("\nchecking if foreground..\n");
printf("\nParent PID is >> %d\n", getpid());
if(!strcmp(layer, "FOREGROUND")){
while (1){
printf("\nWaiting for child to terminate..");
end_pid = waitpid(child_pid, NULL, WUNTRACED);
if (end_pid == -1) {
status_code = -1;
perror("Waitpid Error");
exit(EXIT_FAILURE);
}
else if(end_pid == child_pid){
// child ended
printf("\nChild terminated\n");
break;
}
}
}
if(!strcmp(layer, "BACKGROUND")){
printf("\nBackground detected!\n");
sleep(3); // to prevent the main process from terminating before the child
}
return(0);
}
void write_to_file(int sig){
FILE *pFile;
pFile = fopen("log.txt", "a");
if(pFile==NULL) perror("[Error] opening file");
else fprintf(pFile, "Child terminated\n");
fclose(pFile);
}