-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdyhsh.c
367 lines (305 loc) · 7.82 KB
/
dyhsh.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
// C Program to design a shell in Linux
#include"dyhsh.h"
#include<sys/types.h>
#include<sys/wait.h>
#include<readline/readline.h>
#include<readline/history.h>
#include<errno.h>
#include<pwd.h>
#include"processLinkedList.c"
#include"parsing.c"
struct passwd *pw;
char *homedir;
static node_t * start;
static char CMD_BUFFER[MAXCOM] = {0};
// Greeting shell during startup
void init_shell() {
clear();
printf("\033[0;32m");
printf("\n\n %s \033[0;32m", headerText);
printf("%s\n", headerCard);
printf("\n\n\nUSER is: \033[0;33m@%s", getUsername());
printf("\nHome dir is %s", homedir);
printf("\033[0m\n"); //reset text color
return;
}
// Function to take input
int takeInput(char* str) {
char* buf;
buf = readline(" ");
if (strlen(buf) != 0) {
add_history(buf);
strcpy(str, buf);
return 1;
} else {
return 0;
}
}
// Function to print Current Directory.
void printDir() {
char cwd[1024];
getcwd(cwd, sizeof(cwd));
if (strcmp(cwd, homedir)) {
strcpy(cwd, "~");
}
printf("\n\033[0;31mDir: \033[0;36m%s :\033[0m", cwd);
return;
}
// Function where the system command is executed
void execArgs(char** parsed, int isBackgroundProcess) {
int argLen = getArgLen(parsed);
char pathin[MAXCOM] = {0};
char pathout[MAXCOM] = {0};
int inputFlag = 0;
int outputFlag = 0;
fflush(stdout);
for (int n = 0; n < argLen; n++) {
char *p_char_in = strchr(parsed[n], (int) '<');
char *p_char_out = strchr(parsed[n], (int) '>');
//NOTE: If there are multiple repeated in/output streams, an option is just to overwrite the source
//The **DIFFICULT** alternative would be to chain the streams.
//Output appending (>>) is not supported
if (p_char_in != NULL && parsed[n+1][0] != '\0') {
inputFlag = 1;
strcpy(pathin, parsed[n+1]);
parsed[n][0] = '\0';
parsed[n+1][0] = '\0';
for (int m = n; m < argLen -2; m++) { //Truncate 'parsed' by 2. (Removing the < symbol entry as well as the entry behind it)
memcpy(parsed[m], parsed[m+2], sizeof(char *));
}
strcpy(parsed[argLen -1], "\0");
strcpy(parsed[argLen -2],"\0");
argLen -= 2;
n--;
} else if (p_char_out != NULL && parsed[n+1][0] != '\0') {
outputFlag = 1;
strcpy(pathout, parsed[n+1]);
parsed[n][0] = '\0';
parsed[n+1][0] = '\0';
for (int m = n; m < argLen -2; m++) { //Truncate 'parsed' by 2. (Removing the > symbol entry as well as the entry behind it)
parsed[m] = parsed[m+2];
}
parsed[argLen -1][0] = '\0';
parsed[argLen -2][0] = '\0';
argLen -= 2;
n--;
}
}
char* executable [MAXLIST] = {'\0'};
for (int i = 0; i < argLen; i++) {
executable[i] = parsed[i];
}
pid_t pid = fork();
if (pid == -1) {
printf("\nFailed forking child...\n");
return;
} else if (pid == 0) {
if (inputFlag == 1) { //Return -1 if there's an error with the input stream
if (freopen(pathin, "r", stdin) == NULL) perror("Error: Couldn't redirect input stream");
}
if (outputFlag == 1) { //Return -1 if there's an error with the output stream
if (freopen(pathout, "w", stdout) == NULL) perror("Error: Couldn't redirect output stream");
}
if (execvp(executable[0], executable) < 0) {
printf("\nCould not execute command [%s]...\n\n", parsed[0]);
fflush(stdout);
}
freopen("dev/stdin", "r", stdin);
freopen("dev/stdout", "w", stdout);
return exit(0);
}
if (pid != 0){
node_t * node = createNode(pid, CMD_BUFFER);
addNode(node);
memset(CMD_BUFFER, 0, sizeof(CMD_BUFFER)); //Reset CMD_BUFFER to zero
// waiting for child to terminate
if (!isBackgroundProcess) {
int status;
waitpid(pid, &status, 0);
}
return;
}
return;
}
// Function where the piped system commands is executed
void execArgsPiped(char** unparsedPipe, int isBackgroundProcess)
{
printf("Piping unimplemented....\n");
int pipefd[MAXPIPE];
checkArgsList(unparsedPipe);
for (int n = 0; n < getArgLen(unparsedPipe) && n < MAXPIPE; n++) {
printf("Pipe part %i: [%s]\n", n, unparsedPipe[n]);
char* temp[MAXLIST];
for (int i = 0; i < MAXLIST; i++) {
temp[i] = malloc(sizeof(char) * MAXCOM);
temp[i][0] = '\0';
}
parseChar(unparsedPipe[n], temp, " ", MAXLIST);
parseIO(temp);
checkArgsList(temp);
/*
*/
}
/*
// 0 is read end, 1 is write end
int pipefd[2];
pid_t p1, p2;
if (pipe(pipefd) < 0) {
printf("\nPipe could not be initialized");
return;
}
p1 = fork();
if (p1 < 0) {
printf("\nCould not fork");
return;
}
if (p1 == 0) {
// Child 1 executing..Type Something
// It only needs to write at the write end
close(pipefd[0]);
dup2(pipefd[1], STDOUT_FILENO);
close(pipefd[1]);
if (execvp(parsed[0], parsed) < 0) {
printf("\nCould not execute command 1..");
exit(0);
}
} else {
// Parent executing
p2 = fork();
if (p2 < 0) {
printf("\nCould not fork");
return;
}
// Child 2 executing..
// It only needs to read at the read end
if (p2 == 0) {
close(pipefd[1]);
dup2(pipefd[0], STDIN_FILENO);
close(pipefd[0]);
if (execvp(parsedpipe[0], parsedpipe) < 0) {
printf("\nCould not execute command 2..");
exit(0);
}
} else {
// parent executing, waiting for two children
wait(NULL);
wait(NULL);
}
}
*/
return;
}
// Help command builtin
void openHelp() {
printf("%s\n", helpText);
return;
}
// Function to execute builtin commands
int ownCmdHandler(char** parsed) {
int NoOfOwnCmds = 6, i, switchOwnArg = 0;
char* ListOfOwnCmds[NoOfOwnCmds];
ListOfOwnCmds[0] = "exit";
ListOfOwnCmds[1] = "cd";
ListOfOwnCmds[2] = "help";
ListOfOwnCmds[3] = "hello";
ListOfOwnCmds[4] = "jobs";
for (i = 0; i < NoOfOwnCmds; i++) {
if (strcmp(parsed[0], ListOfOwnCmds[i]) == 0) {
switchOwnArg = i + 1;
break;
}
}
switch (switchOwnArg) {
case 1:
printf("\nGoodbye\n");
exit(0);
case 2:
if (parsed[1][0] == '\0') {
chdir(homedir);
return 1;
}
chdir(parsed[1]);
return 1;
case 3:
openHelp();
return 1;
case 4:
printf(helloText,
getUsername());
return 1;
case 5:
jobs(start);
return 1;
default:
break;
}
return 0;
}
int processString(char* str, char** parsed, char** unparsedPiped, int* isBackgroundTask)
{
int daemon = 0;
daemon = parseDaemon(str);
*isBackgroundTask = daemon;
int isPiped = parseChar(str, unparsedPiped, "|", MAXPIPE);
if (isPiped) {
return 2;
}
parseChar(str, parsed, " ", MAXLIST);
if (parsed[0][0] == '\0') return 0;
parseIO(parsed);
if (ownCmdHandler(parsed)) {
return 0;
}
else{
return 1;
}
}
int main() {
pw = getpwuid(getuid());
homedir = pw->pw_dir;
char *inputString = malloc(sizeof(char) * MAXCOM);
char **parsedArgs = malloc(sizeof(char*) * (MAXLIST + 1));
char **unparsedPipeParts = malloc(sizeof(char*) * (MAXPIPE + 1)); //Need an extra spot for judging end state
for(int i = 0; i < MAXLIST + 1; i++) {
parsedArgs[i] = (char*) malloc(sizeof(char) * MAXCOM);
}
for(int i = 0; i < MAXPIPE + 1; i++) {
unparsedPipeParts[i] = (char*) malloc(sizeof(char) * MAXCOM);
}
//char* parsedArgsPiped[MAXLIST];
int execFlag = 0;
init_shell();
start = createNode(getpid(), "Parent\0");
setLastNode(start);
for(;;){
memset(inputString, 0, sizeof(char) * MAXCOM);
zombie_cleanup(start);
for(int i =0; i < MAXLIST + 1; i ++) {
parsedArgs[i][0] = '\0';
}
for(int i =0; i < MAXPIPE + 1; i ++) {
unparsedPipeParts[i][0] = '\0';
}
printDir();
if (!takeInput(inputString))
continue;
strcpy(CMD_BUFFER, inputString);
zombie_cleanup(start);
int isBackgroundProcess = 0;
// process
execFlag = processString(inputString, parsedArgs, unparsedPipeParts, &isBackgroundProcess);
// execflag returns zero if there is no command
// or it is a builtin command,
// 1 if it is a simple command
// 2 if it is including a pipe.
// execute
if (execFlag == 1) {
execArgs(parsedArgs, isBackgroundProcess);
}
if (execFlag == 2) {
execArgsPiped(unparsedPipeParts, isBackgroundProcess);
}
}
freeArgs(parsedArgs);
return 0;
}