-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
450 lines (376 loc) · 11.2 KB
/
main.c
File metadata and controls
450 lines (376 loc) · 11.2 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
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
#include "main.h"
int main (int argc, char *argv[]) {
FILE *inputFile; //stdin in interactive mode or batchFile in batch mode
int isBatch; //specify whether in batch mode or interactive mode
char *shellenv;
//interactive mode
if(argc == 1) {
printf("Welcome to the Shell\n");
isBatch = 0;
inputFile = stdin;
}
//batch mode
else if(argc == 2) {
isBatch = 1;
inputFile = fopen(argv[1], "r");
if (inputFile == NULL) {
fputs("Invalid batch file.\n", stderr);
exit(1);
}
} else {
fputs("Wrong number of command line arguments.\n", stderr);
exit(1);
}
//write a user manual file for the help command
createUserManual();
//set shell environment variable
shellenv = malloc(strlen(getenv("PWD")) + strlen("/myshell"));
strcpy(shellenv, getenv("PWD"));
strcat(shellenv, "/myshell");
if (setenv("shell", shellenv, 1) == -1) {
fprintf(stderr, "Unable to set shell environment variable.\n");
exit(1);
}
shellLoop(inputFile, isBatch);
if (isBatch) {
fclose(inputFile);
}
return 0;
}
int shellLoop(FILE *inputFile, int isBatch) {
int num_cmds = 0; // number of commands in a line
struct Cmd cmd[MAXCMDS]; // array of commands (for one line)
pid_t pid;
int num_pids = 0; // number of pids in background execution mode
pthread_t threads[MAXCMDS]; // array of threads
int rtns[MAXCMDS]; // array of return values
int t_succs[MAXCMDS]; // array of thread creation return values
int i;
while(1) {
//clear previous values
for (i=0; i < MAXCMDS; i++) {
cmd[i] = ResetCmd;
}
//if interactive mode, prompt input if finished processing line
if (!isBatch) {
printf("8[ ");
}
//parse command, will return 1 if EOF in interactive mode
num_cmds = parseLine(cmd, inputFile, isBatch);
if (num_cmds == -1) {
for (i = 0; i < num_pids; i++) {
waitpid(-1, NULL, 0);
}
return 0;
}
for (i = 0; i < num_cmds; i++) {
//quit loop
if (strcmp(cmd[i].argv[0], "quit") == 0) {
for (i = 0; i < num_pids; i++) {
waitpid(-1, NULL, 0);
}
return 0;
}
t_succs[i] = pthread_create(&threads[i], NULL, execCmd, (void *)&cmd[i]);
}
for (i = 0; i < num_cmds; i++) {
if (t_succs[i] == 0) {
pthread_join(threads[i], (void *)&rtns[i]);
num_pids += rtns[i];
} else {
fprintf(stderr, "Could not create thread %d!\n", i+1);
}
}
}
return 0;
}
int parseLine(struct Cmd cmd[], FILE *input_filestream, int isBatch) {
char *line = NULL;
size_t len = 0;
ssize_t line_len = 0;
char *cmd_str;
int cmd_num = 0;
int i;
line_len = getline(&line, &len, input_filestream);
//getline returns -1 if EOF
if (line_len == -1) {
return -1;
}
// if batch mode, echo input
if (isBatch) {
printf("%s\n", line);
}
//parse commands in a line separated by ';'
cmd_str = strtok(line, ";");
while (cmd_str != NULL) {
if (cmd_num == MAXCMDS) {
fprintf(stderr, "Reached maximum number of commands per line.\n");
fprintf(stderr, "Only processing the first %d commands.\n", MAXCMDS);
return cmd_num;
}
for (i = 0; i < strlen(cmd_str); i++) {
if (!isspace(cmd_str[i])) {
parseCmd(cmd_str, &cmd[cmd_num++]);
break;
}
}
cmd_str = strtok(NULL, ";");
}
return cmd_num;
}
int parseCmd(char *cmd_str, struct Cmd *cmd) {
int cmd_len = strlen(cmd_str);
char tmp_cmd[cmd_len];
char reset_tmp[cmd_len];
int prev_space = 1;
int char_index = 0;
char c;
int i,j;
for (i = 0; i < cmd_len+1; i++) {
if (i < cmd_len) {
c = cmd_str[i];
} else {
c = ' ';
}
if (isspace(c)) {
if (!prev_space) {
tmp_cmd[char_index] = '\0';
cmd->argv[cmd->argc] = malloc(strlen(tmp_cmd));
strcpy(cmd->argv[cmd->argc], tmp_cmd);
strcpy(tmp_cmd, reset_tmp);
cmd->argc++;
char_index=0;
}
prev_space=1;
} else {
tmp_cmd[char_index++] = c;
prev_space=0;
}
}
return 0;
}
void *execCmd(void *v_cmd) {
struct Cmd *cmd = (struct Cmd *)v_cmd;
int pid;
int isBackground = 0;
FILE *fp;
int fd;
int j;
// Background Execution mode
isBackground = getBackgroundExecution(cmd);
//don't need to fork for cd
if (strcmp(cmd->argv[0], "cd") == 0) {
cdCmd(cmd);
} else {
pid = fork();
// Child process
if (pid == 0) {
// I/O Redirection
if (getOutputFile(cmd) == 1) {
fprintf(stderr, "Invalid redirect file\n");
exit(1);
}
//execute command
cmdCases(cmd);
exit(0);
}
// Parent process
//wait for child to finish if not in background execution mode
else if (!isBackground){
waitpid(pid, NULL, 0);
}
}
if (isBackground) {
return (void *)1;
}
return (void *)0;
}
int getBackgroundExecution(struct Cmd *cmd) {
int len_last = strlen(cmd->argv[cmd->argc-1]);
if (cmd->argv[cmd->argc-1][len_last-1] == '&') {
if (len_last == 1) {
cmd->argc--;
} else {
cmd->argv[cmd->argc-1][len_last-1] = 0;
}
return 1;
}
return 0;
}
int getOutputFile(struct Cmd *cmd) {
FILE *fp = stdout;
int fd;
int i;
for (i = 0; i < cmd->argc; i++) {
if (strcmp(cmd->argv[i], ">") == 0) {
fp = fopen(cmd->argv[i+1], "w");
break;
} else if (strcmp(cmd->argv[i], ">>") == 0) {
fp = fopen(cmd->argv[i+1], "a");
break;
}
}
//if redirect and redirect file are not last two words on command
// or the file was unable to be opened
if (i < cmd->argc-2 || fp == NULL) {
return 1;
}
if (fileno(fp) != fileno(stdout)) {
if (dup2(fileno(fp), fileno(stdout)) < 0) {
fprintf(stderr, "%s\n",strerror(errno));
return 1;
}
close(fd);
fclose(fp);
//truncate command so we don't have to deal with redirect in future
cmd->argc = i;
}
return 0;
}
int cmdCases(struct Cmd *cmd) {
int i;
char *out;
int cmd_size = 0;
//length of entire command
for (i = 0; i < cmd->argc; i++) {
cmd_size += strlen(cmd->argv[i]) + 1;
}
if (strcmp(cmd->argv[0], "clr") == 0) {
system("clear");
} else if (strcmp(cmd->argv[0], "dir") == 0) {
dirCmd(cmd, cmd_size);
} else if (strcmp(cmd->argv[0], "environ") == 0) {
environCmd(cmd);
} else if (strcmp(cmd->argv[0], "echo") == 0) {
echoCmd(cmd);
} else if (strcmp(cmd->argv[0], "help") == 0) {
helpCmd(cmd, cmd_size);
} else if (strcmp(cmd->argv[0], "pause") == 0) {
pauseCmd(cmd);
} else {
// runs external programs,
// or deals with invalid commands
extCmd(cmd);
}
return 0;
}
void cdCmd(struct Cmd *cmd) {
char *oldpwd;
char cwd[1024];
if(cmd->argc == 1) {
//report current directory if not directory argument given
printf("%s\n",getenv("PWD"));
}
// directory is specified
else if (cmd->argc == 2) {
oldpwd = getenv("OLDPWD");
//change cwd
if (chdir(cmd->argv[1]) == -1) {
fprintf(stderr, "%s\n",strerror(errno));
} else {
//change pwd and oldpwd
setenv("OLDPWD", getenv("PWD"), 1);
if(setenv("PWD", getcwd(cwd, sizeof(cwd)), 1) == -1) {
chdir(getenv("OLDPWD"));
setenv("OLDPWD",oldpwd,1);
fprintf(stderr,"can't cd into %s\n",cmd->argv[1]);
}
}
} else {
fprintf(stderr, "Invalid number of arguments\n");
}
}
void dirCmd(struct Cmd *cmd, int cmd_size) {
int i;
char *out;
if (cmd->argc > 2) {
fprintf(stderr, "Invalid number of arguments\n");
} else {
out = malloc(cmd_size + strlen(getenv("PWD")) + 1);
strcpy(out, "ls ");
//if directory is not specified
if (cmd->argc == 1) {
strcat(out, getenv("PWD"));
}
//if directory is specified
else {
strcat(out, cmd->argv[1]);
}
sem_wait(&io_sem);
if (system(out) == -1) {
fprintf(stderr, "%s\n",strerror(errno));
}
sem_post(&io_sem);
free(out);
}
}
void environCmd(struct Cmd *cmd) {
int i = 0;
if (cmd->argc > 1) {
fprintf(stderr, "Invalid number of arguments\n");
} else {
sem_wait(&io_sem);
while(environ[i]) {
printf("%s\n", environ[i++]);
}
sem_post(&io_sem);
}
}
void echoCmd(struct Cmd *cmd) {
int i;
sem_wait(&io_sem);
for (i = 1; i < cmd->argc; i++) {
printf("%s ", cmd->argv[i]);
}
printf("\n");
sem_post(&io_sem);
}
void helpCmd(struct Cmd *cmd, int cmd_size) {
int i;
char *out;
if (cmd->argc > 1) {
fprintf(stderr, "Invalid number of arguments\n");
} else {
out = malloc(strlen("more userManual"));
strcpy(out, "more userManual");
sem_wait(&io_sem);
if (system(out) == -1) {
fprintf(stderr, "%s\n",strerror(errno));
}
sem_post(&io_sem);
free(out);
}
}
void pauseCmd(struct Cmd *cmd) {
if (cmd->argc > 1) {
fprintf(stderr, "Invalid number of arguments\n");
} else {
sem_wait(&io_sem);
printf("Press Enter to continue ---------------\n");
sem_post(&io_sem);
getchar();
}
}
void extCmd(struct Cmd *cmd) {
if (setenv("parent", getenv("shell"), 1) == -1) {
fprintf(stderr, "%s\n",strerror(errno));
}
else if(execve(cmd->argv[0],cmd->argv,environ)==-1) {
fprintf(stderr, "%s\n",strerror(errno));
}
}
int createUserManual() {
FILE *outputFile;
outputFile = fopen("userManual", "w");
fprintf(outputFile, "Shell environment developed by Lisa Fan and Tyler Frasca\n");
fprintf(outputFile, "cd <directory> --Change current directory to <directory>\n");
fprintf(outputFile, "clr --Clear the Screen\n");
fprintf(outputFile, "dir <directory> --List contents of <directory>\n");
fprintf(outputFile, "echo --Display <comment> '>' overwrites file '>>' appends to file\n");
fprintf(outputFile, "environ --Display environment strings\n");
fprintf(outputFile, "help --View User Manual\n");
fprintf(outputFile, "pause --Pauses operation until Enter\n");
fprintf(outputFile, "quit --Exit shell\n");
fclose(outputFile);
return 0;
}