-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple_shell.c
457 lines (426 loc) · 9.46 KB
/
simple_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
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <ctype.h>
#include "utils.h"
#include <sys/wait.h>
char ** parseArgs(char* command);
int runCommand(char** args);
int childCommands(char** args);
void exitFunc(char** args);
void chdirFunc(char** args);
void getenvFunc(char** args);
void setenvFunc(char** args);
void echoFunc(char** args);
void writeHistory(char** args);
void getPath(char **args);
int parseScript(char *script);
//present prompt for commands
int main(int argc, char *argv[])
{
if (argc == 2)
{
parseScript(argv[1]);
return 1;
}
FILE *sh_profile = fopen(".421sh_profile", "r");
if (sh_profile != NULL)
{
fclose(sh_profile);
parseScript(".421sh_profile");
}
unsigned int len_max = 9;
//char *command = malloc(len_max);
//unsigned int command_size = len_max;
//char** args = malloc(len_max);
//accept command input of arbitrary length until q is input
while(1){
// Read command from standard input
printf ("Enter your command:");
char *command = malloc(len_max);
unsigned int command_size = len_max;
unsigned int comi = 0;
int c = 0;
int search = 0;
//accept user input until hit enter or end of file or a comment
while (( c = getchar() ))
{
if (c == '\n')
{
command[comi++] = '\0';
break;
}
else if (c == '#')
{
//do nothing
search = 1;
}
else if (search == 0)
{
command[comi++]=(char)c;
//if i reached maximize size then realloc size
if(comi == (command_size-1))
{
command_size = comi+len_max;
char *comptr = realloc(command, command_size);
if (comptr == NULL)
{
printf("Realloc failed!!\n");
free(command);
exit(0);
}
else
{
command = comptr;
}
}
}
}
// split command into array of parameters
char ** args = parseArgs(command);
//printf("%s", args[3]);
// writeHistory(args);
// run the command breaks the while for the child
if(runCommand(args) == 0)
{
break;
}
free(command);
free(args);
}
return 0;
}
// splits the given command into an array of arguments
// command name, and rest of input
char **parseArgs(char* command)
{
char **args = malloc(20*sizeof(char));
int current_size = 0;
int actual_size = 0;
int x = 0;
char * tmpcom = strtok(command, " ");
while (1){
if (tmpcom == NULL)
{
args[x++] = NULL;
break;
}
actual_size += strlen(tmpcom);
// increase the size of args
while (actual_size >= (current_size-2))
{
current_size += 20;
}
char **args2 = realloc(args, current_size);
if (args2 == NULL)
{
printf("Realloc failed!!\n");
free(args);
exit(0);
}
else
{
args = args2;
}
args[x++] = tmpcom;
tmpcom = strtok(NULL, "\0");
}
return args;
}
int runCommand(char** args)
{
//run parent commands
if (args[0] == NULL || args[0] == '\0')
{
return 1;
}
else if (strcmp(args[0], "exit") == 0)
{
exitFunc(args);
return 1;
}
else if (strcmp(args[0], "chdir") == 0 || strcmp(args[0], "cd") == 0)
{
chdirFunc(args);
return 1;
}
else if (strcmp(args[0], "setenv") == 0)
{
setenvFunc(args);
return 1;
}
//fork the shell
pid_t pid = fork();
// Error
if (pid == -1) {
char* error = strerror(errno);
printf("fork: %s\n", error);
return 1;
}
// Child process
else if (pid == 0){
if (childCommands(args) == 0);
else if (args[0][0] == '/')
{
getPath(args);
execv(args[0], args);
//check for error
char* error = strerror(errno);
printf("shell: %s: %s\n", args[0], error);
}
else
{
getPath(args);
execvp(args[0], args);
//check for error
char* error = strerror(errno);
printf("shell: %s: %s\n", args[0], error);
}
return 0;
}
// Parent process
else {
// Wait for child to finish
int childStatus;
waitpid(pid, &childStatus, 0);
return 1;
}
}
// gets the program path for each program
void getPath(char **args)
{
printf("this is \n");
int index = 1;
if (args[index] != NULL)
{
int space = first_unquoted_space(args[index]);
FILE *enverr;
char *tmpstr;
enverr = fopen("enverr.txt", "a+");
while (space != -1)
{
tmpstr = &args[index][space+1];
args[index][space] = '\0';
args[index] = unescape(args[index], enverr);
index++;
args[index] = tmpstr;
printf("this is %s\n", args[index]);
space = first_unquoted_space(args[index]);
}
args[index] = unescape(args[index], enverr);
printf("this is %s\n", args[index]);
fclose(enverr);
}
}
// runs my commands for child works
int childCommands(char** args)
{
if (strcmp(args[0], "getenv") == 0)
{
getenvFunc(args);
}
else if (strcmp(args[0], "echo") == 0)
{
echoFunc(args);
}
else
{
return 1;
}
return 0;
}
// exit function
void exitFunc(char** args)
{
getPath(args);
if (args[1] == NULL)
{
exit(0);
}
int exitcode = atoi(args[1]);
if ((exitcode == 0 && args[1][0] != 0) || args[2] != NULL)
{
printf("error: incorrect exit arguments\n");
}
else
{
exit(exitcode);
}
}
// change the dir, gets passed an array of
void chdirFunc(char** args)
{
getPath(args);
if (args[1] == NULL)
{
if (getenv("HOME") == NULL) printf("error: Home environment variable is not set\n");
// go to home directory
else
{
chdir(getenv("HOME"));
// update the PWD
setenv("PWD", getenv("HOME"), 1);
}
}
else
{
int index = 1;
while (args[index] != NULL)
{
if (chdir(args[index++]) == -1)
{
printf("error: %s\n", strerror(errno));
}
// update the PWD
setenv("PWD", getenv("HOME"), 1);
}
}
}
// look up the specified environment variable
void getenvFunc(char** args)
{
if (args[1] == NULL) printf("error: no argument given to setenv\n");
else if (args[2] != NULL) printf("error: more than one argument given to setenv\n");
else
{
if (getenv(args[1]) == NULL) printf("\n");
else printf("%s\n", getenv(args[1]));
}
}
// look up the specified environment variable
void setenvFunc(char** args)
{
// printf("error: unquoted space found\n");
char *var = strtok(args[1], "=");
char *message = strtok(NULL, "\0");
args[1] = var;
args[2] = message;
if (first_unquoted_space(args[2]) != -1)
{
printf("error: unquoted space found\n");
exit(0);
}
if (args[1] == NULL || args[2] == NULL) printf("error: no arguments given to setenv\n");
FILE *enverr;
enverr = fopen("enverr.txt", "a+");
char *unenv = unescape(args[2], enverr);
if (unenv != NULL)
{
setenv(args[1], unenv, 1);
}
fclose(enverr);
}
// unescape a string and print it out with a new line
void echoFunc(char** args)
{
if (args[1] == NULL) printf("error: no argument given to echo\n");
else if (first_unquoted_space(args[1]) != -1)
{
printf("error: unquoted space found\n");
}
else if (args[2] != NULL) printf("error: more than one argument given to echo\n");
else
{
// open a file for echo errors
FILE *echoerr;
echoerr = fopen("echoerr.txt", "a+");
char *unecho = unescape(args[1], echoerr);
if (unecho != NULL)
{
printf("%s\n", unecho);
}
fclose(echoerr);
}
}
// write to the 421sh_history file
void writeHistory(char** args)
{
char **temp_args = args;
FILE *history = fopen("./.421sh_history", "a+");
fprintf(history, "test\n");
int index = 0;
while (temp_args[index] != NULL)
{
char *writeString = temp_args[index++];
fprintf(history, "%s ", writeString);
}
fprintf(history, "\n");
fclose(history);
}
// takes a script, executes it, returns 0 on success -1 on failure
int parseScript(char *script)
{
//try and open script, exit if can't
FILE *scriptF = fopen(script, "r");
if (scriptF == NULL)
{
printf("error opening file: %s, %s\n", script, strerror(errno));
return -1;
}
unsigned int len_max = 9;
char *command = malloc(len_max);
unsigned int command_size = len_max;
unsigned int comi = 0;
int c = 0;
int search = 0;
while (1)
{
c = fgetc(scriptF);
// determine if end of file
if (feof(scriptF))
{
break;
}
// run the command
else if (c == '\n')
{
command[comi++] = '\0';
// split command into array of parameters
char ** args = parseArgs(command);
//printf("%s", args[3]);
// writeHistory(args);
// run the command breaks the while for the child
if(runCommand(args) == 0)
{
break;
}
free(args);
free(command);
command = malloc(len_max);
command_size = len_max;
comi = 0;
c = 0;
search = 0;
}
else if (c == '#')
{
//do nothing
search = 1;
}
else if (search == 0)
{
command[comi++]=(char)c;
//if i reached maximize size then realloc size
if(comi == (command_size-1))
{
command_size = comi+len_max;
char *comptr = realloc(command, command_size);
if (comptr == NULL)
{
printf("Realloc failed!!\n");
free(command);
exit(0);
}
else
{
command = comptr;
}
}
}
}
fclose(scriptF);
return 0;
//read line by line, parse args and run commands
}