-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsimple-shell.c
More file actions
400 lines (366 loc) · 8.35 KB
/
simple-shell.c
File metadata and controls
400 lines (366 loc) · 8.35 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
/***************************************************************
* Simple UNIX Shell
* @authors: Nguyen Hoang Gia (1751064) & Pham Bao Duy (1751065)
*
**/
#include <stdio.h>
#include <unistd.h> // fork
#include <string.h>
#include <stdlib.h> // exit
#include <sys/types.h> // pid_t
#include <sys/wait.h> // wait
#include <fcntl.h> // open
// For clear screen
#ifdef _WIN32
#include <conio.h>
#else
#include <stdio.h>
#define clrscr() printf("\e[1;1H\e[2J")
#endif
#define MAX_LENGTH 80 // The maximum length of the commands
void init_shell() //Open when launch shell
{
clrscr();
printf("\n============================================");
printf("\n| SIMPLE SHELL |");
printf("\n____________________________________________");
printf("\n| Project 2 - CS333 Spring 2020 |");
printf("\n| Authors: Nguyen Hoang Gia (1751064) |");
printf("\n| Pham Bao Duy (1751063) |");
printf("\n============================================");
printf("\n");
char *username = getenv("USER");
printf("\nHello @%s, type help for menu.", username);
printf("\n\n");
}
void help_panel()
{
puts("\n____________________________________________"
"\n|________________HELP PANEL________________|"
"\n|__________________________________________|"
"\n| Some commands supported: |"
"\n| >!! |"
"\n| >ls |"
"\n| >pwd |"
"\n| >ping |"
"\n| >exit |"
"\n|__________________________________________|\n");
return;
}
void parse(char *command, char **args)
{
while (*command != '\0') //if not the end of line, continue
{
while (*command == ' ' || *command == '\t' || *command == '\n')
{
*command++ = '\0'; //replace white spaces with 0
}
*args++ = command;
while (*command != '\0' && *command != ' ' && *command != '\t' && *command != '\n')
{
command++;
}
*args = '\0'; //mark the end of argument list
}
}
int findAmpersand(char **args)
{
//Find "&"
int count = 0;
while (args[count] != NULL && strcmp(args[count], "&") != 0)
count++;
if (args[count] != NULL)
{
//Found
args[count] = NULL; // Remove "&" to ensure that fork() will run properly.
return 1;
};
return 0;
}
int findPipe(char **args)
{
//Find "|""
int count = 0;
while (args[count] != NULL && strcmp(args[count], "|") != 0)
count++;
if (args[count] != NULL)
//Found
return 1;
return 0;
}
int findIORedirect(char **args)
{
// Find ">" or "<" symbol
// Define: Return 0 - Not found
// Return 1 - Found ">" OUTPUT
// Return 2 - Found "<" INPUT
int count = 0;
while (args[count] != NULL && strcmp(args[count], ">") != 0 && strcmp(args[count], "<") != 0)
count++;
if (args[count] != NULL) {
if (strcmp(args[count], ">") == 0)
//Found ">"
return 1;
else if (strcmp(args[count], "<") == 0)
//Found "<"
return 2;
else
// Error handle
return 0;
};
return 0;
}
void exec_w_Pipe(char **args)
{
if (findPipe(args) != 1)
{
printf("\n*** PIPE: error, doing nothing.");
return;
}
//Pre-process: Take argument before "|" and after.
char *parsed[MAX_LENGTH];
char *parsedpipe[MAX_LENGTH];
int countTemp = 0, foundPipe = 0, argLen = 0, pipeLen = 0;
while (args[countTemp] != NULL)
{
if (strcmp(args[countTemp], "|") == 0)
foundPipe = 1;
if (foundPipe == 0)
{
parsed[argLen] = args[countTemp];
argLen++;
}
else if (strcmp(args[countTemp], "|") != 0)
{
parsedpipe[pipeLen] = args[countTemp];
pipeLen++;
};
countTemp++;
};
// Mark the end of char array by "end" character
parsed[argLen] = '\0';
parsedpipe[pipeLen] = '\0';
// Define:
// fd[0] is read end, fd[1] is write end
int fd[2];
pid_t pipe1, pipe2;
if (pipe(fd) < 0)
{
printf("\n*** PIPE: Init error.");
return;
}
pipe1 = fork();
if (pipe1 < 0)
{
printf("\n*** PIPE: Fork error.");
return;
}
if (pipe1 == 0)
{
// pipe1
// write at fd[1]
close(fd[0]);
dup2(fd[1], STDOUT_FILENO); //duplicate
close(fd[1]);
if (execvp(parsed[0], parsed) < 0)
{
printf("\n*** PIPE: Error when executing command befor pipe.");
exit(0);
}
}
else
{
// Parent execute
pipe2 = fork();
if (pipe2 < 0)
{
printf("\n*** PIPE: Fork parent error");
return;
}
// Child 2 execute.
// read at fd[0]
if (pipe2 == 0)
{
close(fd[1]);
dup2(fd[0], STDIN_FILENO); //duplicate
close(fd[0]);
if (execvp(parsedpipe[0], parsedpipe) < 0)
{
printf("\n*** PIPE: Error when executing command after pipe.");
exit(0);
}
}
else
{
// parent executing, waiting for two children
wait(NULL);
}
}
}
void execute_w_io_redirect(int rType, char **args)
{
// rType = 1: Output redirect
// rType = 2: Input redirect
// Double check if rType is valid
if (rType <= 0)
return;
//Pre-process: Find argument after ">" or "<" symbol (file name)
char* fileName;
int countTemp = 0;
while (args[countTemp] != NULL && strcmp(args[countTemp], ">") != 0 && strcmp(args[countTemp], "<") != 0)
countTemp++;
if (args[countTemp] != NULL && args[countTemp + 1] != NULL)
if (strcmp(args[countTemp], ">") == 0 || strcmp(args[countTemp], "<") == 0)
fileName = args[countTemp + 1];
else
return;
else
// Error
return;
if (fileName == NULL) // Cannot find filename
return;
// Remove ">", "<" and file name to fork child process
args[countTemp] = NULL;
args[countTemp + 1] = NULL;
pid_t pid;
int status;
if ((pid = fork()) < 0) // fork child process
{
printf("*** IOREDIRECT: forking child process failed.\n");
exit(1);
}
else if (pid == 0)
{
// Child process
switch (rType)
{
case 1:
{
// Output redirect
int destination = open(fileName, O_WRONLY | O_CREAT | O_TRUNC, 0777);
dup2(destination, 1);
break;
}
case 2:
{
// Input redirect
int destination = open(fileName, O_RDONLY);
if (destination < 0)
{
printf("*** IOREDIRECT: File not found.\n");
exit(1);
}
dup2(destination, 0);
break;
}
case 3:
{
// Error handle
printf("*** IOREDIRECT: Unexpected error.\n");
exit(1);
break;
}
}
if (execvp(*args, args) < 0)
{
printf("*** IOREDIRECT: Exec failed.\n");
exit(1);
}
}
else
{
while (pid != wait(&status))
{
printf("...\n");
};
}
}
void execute(char **args)
{
int isPipe = findPipe(args);
int isConcurrence = findAmpersand(args);
int isIORedirect = findIORedirect(args);
if (isPipe == 1)
{
//Execute if find pipe
exec_w_Pipe(args);
}
else if (isIORedirect > 0) {
execute_w_io_redirect(isIORedirect, args);
}
else
{
pid_t pid;
int status;
//Single command
if ((pid = fork()) < 0) // fork child process
{
printf("*** ERROR: forking child process failed.\n");
exit(1);
}
else if (pid == 0)
{
if (execvp(*args, args) < 0)
{
printf("*** ERROR: Exec failed.\n");
exit(1);
}
}
else
{
// Execute at parent process
if (isConcurrence == 1)
{
// Found "&"
//Execute concurrence, immediately go back to parent
return;
};
// Not found "&", waiting for child
while (pid != wait(&status))
{
printf("...\n");
};
}
}
}
int main(void)
{
char command[MAX_LENGTH];
char prevCommand[MAX_LENGTH];
char *args[MAX_LENGTH / 2 + 1]; // MAximum 40 argments
prevCommand[0] = '\0';
int should_run = 1;
init_shell();
while (should_run)
{
printf("ssh>> ");
fflush(stdout);
fgets(command, MAX_LENGTH, stdin);
command[strlen(command) - 1] = '\0';
if (strcmp(command, "") == 0)
continue;
else if (strcmp(command, "!!") == 0)
{
if (strcmp(prevCommand, "") == 0)
{
printf("No commands in history.\n");
continue;
}
strncpy(command, prevCommand, strlen(prevCommand) + 1);
}
//Parse command and arguments.
parse(command, args);
if (strcmp(args[0], "exit") == 0)
exit(0);
else if (strcmp(args[0], "help") == 0)
help_panel();
else if (strcmp(args[0], "clear") == 0)
clrscr();
else
{
strncpy(prevCommand, command, strlen(command) + 1);
execute(args);
}
}
return 0;
}