-
Notifications
You must be signed in to change notification settings - Fork 0
/
Shell.c
489 lines (418 loc) · 12.3 KB
/
Shell.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
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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
/*This is an implementation of the basic features of a Unix shell.
It was created as a recreuitment task for the Systems SIG of the Web Enthusiasts' Club, NITK.
Created By: Suhas K S (2nd Yr CSE).
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<sys/wait.h>
#include<fcntl.h>
#include<readline/readline.h>
/*These character pointers store the filenames of the files to read from and write to
when there is need for input or output redirection in the entered command.
*/
char *inputfile;
char *outputfile;
void shell_loop();
//char* shell_readline();
void outputredirect(char*);
void inputredirect(char*);
char **shell_splitcommand(char*);
char **shell_splitpipes(char*);
void shell_execute(char **args);
/*These variables store and track the number of pipes in the command. ctr contains
(no. of pipes + 1) and rec is used to control the recursive forking during execution.
*/
int ctr=0;
int rec;
/*The main function doesn't do much. It calls shell_loop() which executes everything else.
*/
int main()
{
shell_loop();
return 0;
}
/*shell_loop() - It is the function responsible for keeping the basic parent loop of the shell running.
*/
void shell_loop()
{
char *line;
do
{
ctr = 0;
outputfile = NULL;
inputfile = NULL;
char pth[100], host[100], user[100];
gethostname(host, 100);
getlogin_r(user, 100);
getcwd(pth, 100);
char prompt[500];
sprintf(prompt, "%s@%s:%s>> ", user, host, pth);
/*readline() enables tab completion and command history.
So it made more sense to use it than the default getline implemented in
the shell_readline() function in this program.
*/
line = readline(prompt);
/*Here, after reading the line, we check it for any < or > characters since they
signify input and output redirection. If present, we call the functions inputredirect(line)
and outputredirect(line) to extract the file names to read from or write to and store it in
the global variables inputfile and outputfile
*/
int k=0;
int ird = -1;
int ord = -1;
int ictr = 0;
int octr = 0;
while(line[k]!='\0')
{
if(line[k]=='<')
{
ird = k;
ictr++;
}
else if(line[k]=='>')
{
ord = k;
octr++;
}
k++;
}
if(ictr>1||octr>1)
{
printf("Invalid I/O redirection\n");
continue;
}
if(ird<ord)
{
outputredirect(line);
if(ird!=-1)
{
inputredirect(line);
}
}
else if(ird>ord)
{
inputredirect(line);
if(ord!=-1)
{
outputredirect(line);
}
}
/*After removing I/O redirection instructions from the line, we check for pipes.
If pipes (|) exist, we extract commands between the pipes and store them in a string array
cmds. We use the function shell_splitpipes(line) to perform this.
*/
char **cmds;
cmds = shell_splitpipes(line);
/* Here, we check if the given command is a builtin. It can be a builtin only when
there are no pipes. Since ctr keeps track of the number of commands in a line and indirectly,
the pipe count, we use it to check whether there are no pipes. And then we execute the builtins
with system calls chdir() for cd, getcwd() for pwd and exit() for exit. The builtin help is used to
display all the builtins.
*/
if(ctr==1)
{
char temp[100];
strcpy(temp, *cmds);
char **args;
args = shell_splitcommand(temp);
if(strcmp(*args,"cd")==0)
{
if(*(args+2)!=NULL)
{
printf("cd: Invalid number of arguments\n");
printf("Syntax: cd <path>\n");
free(line);
free(cmds);
continue;
}
if(chdir(*(args+1))<0)
{
perror(*(args+1));
}
free(line);
free(cmds);
continue;
}
if(strcmp(*args,"pwd")==0)
{
if(*(args+1)!=NULL)
{
printf("pwd: Command does not need arguments\n");
free(line);
free(cmds);
continue;
}
printf("%s\n", pth);
free(line);
free(cmds);
continue;
}
if(strcmp(*args,"exit")==0)
{
if(*(args+1)!=NULL)
{
printf("exit: Command does not need arguments\n");
free(line);
free(cmds);
continue;
}
free(line);
free(cmds);
exit(0);
}
if(strcmp(*args,"help")==0)
{
if(*(args+1)!=NULL)
{
printf("help: Command does not need arguments\n");
free(line);
free(cmds);
continue;
}
printf("\nAn Implementation of a Unix-like Shell.\n");
printf("These are the built-in commands (Type help to view them):\n");
printf("cd <pathname> : Changes current directory to the specified pathname.\n");
printf("pwd : Displays the path of the current working directory.\n");
printf("exit : Exits out of the current shell.\n\n");
printf("Use man <command> for detailed help on individual commands.\n\n");
free(line);
free(cmds);
continue;
}
}
/*After checking for builtins, we finally execute the line by passing cmds into
shell_execute()
*/
rec = ctr;
shell_execute(cmds);
/*After the execution is done, we free the dynamically allocated line and cmds
*/
free(line);
free(cmds);
}while(1);
}
/*shell_execute() is the function that executes the given command. It can perform execution of
simple commands, I/O redirection and command piping.
*/
/*The function uses recursion to implement command piping. It starts execution in the
last command in the pipeline. It then calls itself till it reaches the first command.
A fork() system call is encountered during every function call. So each command
uses fork() to create a child process which does the same till the first command is reached.
Since that command has an input, it is executed and its output if piped to the input of its parent
process. This is done till the shell parent process is reached.
*/
void shell_execute(char **cmds)
{
pid_t child_pid;
pid_t w_pid;
int status;
int outputfd, inputfd; //File descripters for the input and output files for I/O redirection
int pfd[2];
pipe(pfd);
child_pid = fork(); //forking the parent process
if(child_pid==0) //This block is executed if the current process is a child of the running process
{
if(rec!=0)
{
rec--;
shell_execute(cmds);
}
if(cmds[rec+1]!=NULL)
{
close(pfd[0]);
dup2(pfd[1], 1);
}
if(rec==ctr-1)
{
if(outputfile!=NULL)
{
outputfd = open(outputfile, O_RDWR | O_CREAT | O_EXCL, 0666);
if(outputfd<0)
{
outputfd = open(outputfile, O_RDWR | O_CREAT | O_TRUNC, 0666);
}
dup2(outputfd, 1);
}
}
if(rec==0)
{
if(inputfile!=NULL)
{
inputfd = open(inputfile, O_RDWR);
if(inputfd<0)
{
perror("file");
exit(1);
}
dup2(inputfd, 0);
}
close(pfd[0]);
}
char **args;
args = shell_splitcommand(cmds[rec]);
int j;
for(j=0;j<ctr;j++)
printf("%s", args[j]);
char pth[100];
getcwd(pth, 100);
if(strcmp(*args,"pwd")==0)
{
printf("%s\n", pth);
close(pfd[1]);
exit(0);
}
if(execvp(args[0], args)==-1)
{
close(pfd[1]);
close(pfd[0]);
perror("Shell");
}
close(pfd[1]);
close(pfd[0]);
if(close(outputfd)<0)
{
perror("file");
exit(EXIT_FAILURE);
}
exit(0);
}
else if(child_pid<0) //Errors during forking
perror("Shell");
else //This block is executed if the running process is the parent after forking
{
if(rec!=ctr)
{
close(pfd[1]);
dup2(pfd[0], 0);
}
do
{
w_pid = waitpid(child_pid, &status, WUNTRACED);
}while(!WIFEXITED(status) && !WIFSIGNALED(status));
rec++;
}
}
/*shell_readline() takes input in the shell
EDIT: It has been replaced by readline() in the shell_loop() function
*/
/*char *shell_readline()
{
char *line = NULL;
ssize_t bufsize = 0;
getline(&line, &bufsize, stdin);
return line;
}*/
/* The following two functions extract the filenames from the input line and stores them
for input and output redirection
*/
void outputredirect(char* line)
{
char* token = strtok(line, ">");
int i = 0;
while(token != NULL)
{
if(i==1)
{
char* tokenws = strtok(token, " \n");
outputfile = tokenws;
return;
}
token = strtok(NULL, ">");
i++;
}
}
void inputredirect(char* line)
{
char* token = strtok(line, "<");
int i = 0;
while(token != NULL)
{
if(i==1)
{
char* tokenws = strtok(token, " \n");
inputfile = tokenws;
return;
}
token = strtok(NULL, "<");
i++;
}
}
/*The following functions split input lines according to the pipes present and commands according
to the spaces present. Since each seperate command is divided according to spaces, quotes and newline,
we can observe that it doesn't support arguments with spaces
*/
char **shell_splitpipes(char* line)
{
int size = 64;
char **args = malloc(size * sizeof(char*));
char* token = strtok(line, "|\n");
int i=0;
ctr=0;
while(token != NULL)
{
args[i] = token;
token = strtok(NULL, "|\n");
i++;
ctr++;
}
args[i]=NULL;
return args;
}
/*This updated version of shell_splitcommand() introduces quoting, so that filenames
with spaces can be used without any problems. In the previous version of the function,
the command was split only with respect to spaces which eliminated any possibility of using
filenames with spaces*/
char **shell_splitcommand(char* line)
{
int size = 64;
char temp[100];
char **argsquo = malloc(size * sizeof(char*));
char **args = malloc(size * sizeof(char*));
char* token = strtok(line, "\"\n");
int i=0;
while(token != NULL)
{
argsquo[i] = token;
token = strtok(NULL, "\"\n");
i++;
}
argsquo[i] = NULL;
int k = 0;
int j;
for(j=0;j<i;j++)
{
if(j%2!=0)
{
args[k] = argsquo[j];
k++;
continue;
}
token = strtok(argsquo[j], " ");
while(token != NULL)
{
args[k] = token;
token = strtok(NULL, " ");
k++;
}
}
args[k] = NULL;
return args;
}
/*This is the previous version of the function*/
/*char **shell_splitcommand(char* line)
{
int size = 64;
char **args = malloc(size * sizeof(char*));
char* token = strtok(line, " \n\"");
int i=0;
while(token != NULL)
{
args[i] = token;
token = strtok(NULL, " \n\"");
i++;
}
args[i]=NULL;
return args;
}*/