-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflunalopeztristanbailey_ShellScript.c
595 lines (531 loc) · 15.4 KB
/
flunalopeztristanbailey_ShellScript.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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
/**
* flunalopez_tristanbailey_ShellScript:
* Program simulates a shell with basic functionaility in the console.
* Such as cd, exit, help, execvp(ls, touch, mkdir, etc...)
*
* @author: Froilan Luna-Lopez
* @author: Tristan Bailey
* University of Nevada, Reno
* 27 February 2022
*
*
*/
// Libraries
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <sys/wait.h>
#include <sys/types.h>
// Macros
#define ARRAY_MAXSIZE 10000
/*
* printError():
* Informs user that an error was encountered.
* args:
* None
* return
* None
*/
static inline void printError();
/*
* parseInput():
* Parses a given pointer of characters to tokenize delimited by a space character.
* args:
* input: Pointer to characters to parse.
* splitWords: Pointer to array of characters to save tokenized words into.
* return:
* Integer with the number of words tokenized into array.
*/
int parseInput(char *input, char *splitWords[]);
/*
* redirectCommand():
* Directs output received from command to a destination.
* args:
* @special: Special character within command (e.g., >)
* @line: Character pointer to the user inputted command.
* @isDirect: Determines whether the output will go to a destination that isn't the console.
* @tokens: Tokenized input.
* @outputTokens: 2D array of all words to be outputted.
*/
char* redirectCommand(char* special, char* line, bool* isDirect, char* tokens[],
char* outputTokens[]);
/*
* exitProgram():
* Tests if tokens from user input is a valid exit call.
* args:
* @tokens: 2D array containing tokens as strings.
* @numTokens: Integer with the number of tokens.
* return:
* True if tokens have a valid exit call.
* False otherwise.
*/
bool exitProgram(char* tokens[], int numTokens);
/*
* launchProcess():
*
* args:
* tokens: 2-d array of tokens
* numTokens: the number of tokens in the prev list/array
* isRedirect: bool determining if command is a redirect
* return:
* none
*/
void launchProcesses(char *tokens[], int numTokens, bool isRedirect);
/*
* changeDirectories():
* Changes the current working directory if the first argument in tokens is "cd"
* and iff there is one other argument that is the directory to be changed to
* o.w. it errors
* args:
* tokens: 2-d array of tokens
* numTokens: the number of tokens in the prev list/array
* return:
* none
*/
void changeDirectories(char *tokens[], int numTokens);
/*
* promptUser():
* Display shell prefix and prompt user for input.
* args:
* isBatch: Boolean variable indicating whether program is in batch mode or not.
* return:
* Void
*/
void promptUser(bool isBatch);
/*
* executeCommand():
* executes the cd, help, exict, redirect and execvp type arguments that were input by the user
* through appropriate function calls or launchProcesses
* args:
* cmd: the line input by the user before parsing
* isRedirect: a bool that tracks if a redirectCommand() was given and is changed by redirectCommand
* tokens: a 2-d matrix that holds out tokenized command + arguments fromt he user
* outputTokens: a 2-d matrix of output tokens updated by redirectCommand()
* isExits:a bool representign if the user want to quit the program, is updated by exitProgram()
* return:
* char* which represents the outputfile from a redirect
*/
char *executeCommand(char *cmd, bool *isRedirect, char* tokens[], char* outputTokens[],
bool *isExits);
/*
* printHelp();
* Displays the help information/menu telling the user acceptable commands
* args:
* tokens: a matrix where each row is a token that was parsed from the user input
* numTokens: the number of tokens parsed
* return:
* Void
*/
void printHelp(char* tokens[], int numTokens);
int main(int argc, char* argv[]) {
//make a pid_t for the main process
pid_t mainProc;
// Variables
char command[ARRAY_MAXSIZE+1]; // String with command to run from batch file.
char* outputFileName = ""; // Name of file to send command output to.
char* tokens[ARRAY_MAXSIZE + 1]; // 2D array of strings from tokenizing input command.
char* outputTokens[ARRAY_MAXSIZE + 1]; // Tokens to output.
bool isExits; // Determines whether to exit from program (negligible for batch mode).
bool isRedirect; // Determines if command output should be piped to seperate file.
FILE* inputFile = stdin; // File with commands to feed in.
// Test if running in match mode
if (argc > 2) {
printError();
return 0;
}
else{
bool is_batch;
// Test if file can be accessed and change input stream
if (argc == 2){
if((inputFile = fopen(argv[1], "r")) == NULL) {
printError();
return 0;
}
//batch mode active
is_batch = true;
}
else{
//batch mode inactive
is_batch = false;
}
// Run commands in batch mode
//gets each line from the file till the EOF
//if it is not batch then wile short circuits to always being true
//it will then prompt and read user input
//if it is batch then input will be taken from the file till 'exit' or EOF
while(!is_batch || fgets(command, sizeof(command), inputFile) != NULL) {
promptUser(is_batch); // Prompt user with shell prefix
//get user input if not batch mode
if(!is_batch){
fgets(command, sizeof(command), inputFile);
}
//remove the newline char
if(command[strlen(command)-1] == '\n')
command[strlen(command)-1] = '\0';
//remove the caruage return (it appears at end of fgets str)
if(command[strlen(command)-1] == '\r')
command[strlen(command)-1] = '\0';
//print the command from file if in batch mode
if(is_batch){
printf("\n%s\n", command);
}
//execute command
outputFileName = executeCommand(command, &isRedirect, tokens, outputTokens, &isExits);
if(isExits)
{
fclose(inputFile);
free(outputFileName);
kill(0, SIGTERM);
}
// Test if output file proved
if (strcmp(outputFileName, "") != 0) {
FILE* outputFile; // File pointer to output file
// Test if we can access output file
if ((outputFile = fopen(outputFileName, "w")) == NULL) {
printError();
return 1;
}
// Write output tokens to output file
for(int i = 0; outputTokens[i]; i++) {
fprintf(outputFile, "%s", outputTokens[i]);
}
fclose(outputFile);
free(outputFileName);
}
}
fclose(inputFile);
return 0;
}
/*
// Run in interactive mode
while(1) {
// Variables
char command[ARRAY_MAXSIZE + 1];
// Initialize shell
promptUser(false); // Prompt user with shell prefix
fgets(command, sizeof(command), stdin); // Get command
unsigned int command_len = strlen(command);
// Parse input
if (command_len > 0) {
// Variables
char* tokens[ARRAY_MAXSIZE + 1];
unsigned int tokens_count;
char* commandDup = strdup(command);
tokens_count = parseInput(command, tokens); // Parse input
// TEMP: Print out tokens to shell
for (int i = 0; i < tokens_count; i++) {
printf("%s ", tokens[i]);
}
printf("\n");
// Test for exit call
if (exitProgram(tokens, tokens_count)) return 0;
// TEMP: Test redirectCommand()
bool isRedirect;
char* outputTokens[ARRAY_MAXSIZE + 1];
char* outputFilename = redirectCommand(">", commandDup, &isRedirect, tokens, outputTokens);
if (isRedirect) {
FILE* outputFile;
if((outputFile = fopen(outputFilename, "w")) == NULL) {
printError();
}
else {
for(int i = 0; outputTokens[i] != NULL; i++) {
fprintf(outputFile, "%s", outputTokens[i]);
}
}
}
free(commandDup);
free(outputFilename);
}
}*/
return 0;
}
static inline void printError() {
printf("Shell Program Error Encountered\n");
}
int parseInput(char *input, char *splitWords[]) {
int wordInd = 0;
splitWords[0] = strtok(input, " ");
while (splitWords[wordInd] != NULL) {
splitWords[++wordInd] = strtok(NULL, " ");
}
return wordInd;
}
void promptUser(bool isBatch) {
// Don't prompt user if in batch mode
if (isBatch) {
return;
}
// Variables
char* username; // System username
char* hostname; // System hostname
char cwd_path[ARRAY_MAXSIZE + 1]; // Current working directory path
username = getenv("LOGNAME"); // Get system username
hostname = getenv("HOSTNAME"); // Get system hostname
getcwd(cwd_path, sizeof(cwd_path)); // Get current working directory path
printf("\n%s@%s:%s $ ", username, hostname, cwd_path); // Prompt user
}
char* redirectCommand(char* special, char* line, bool* isRedirect, char* tokens[], char* outputTokens[]) {
// Variables
bool foundSpecial = false;
char* outputFileName = (char*) malloc(sizeof(char) * (ARRAY_MAXSIZE + 1));
char inputFileName[ARRAY_MAXSIZE + 1];
int outputFileNameIndex = 0;
int inputFileNameIndex = 0;
outputFileName[0] = '\0';
inputFileName[0] = '\0';
// Split command
for(int i = 0; line[i]; i++) {
if (line[i] == '\0' || line[i] == '\n') continue;
if (line[i] == '>') {
if (foundSpecial == false) {
foundSpecial = true;
*isRedirect = true;
}
else {
*isRedirect = false;
//printError(); this is not needed
strcpy(outputFileName, "");
return outputFileName;
}
}
else if (foundSpecial == false) {
inputFileName[inputFileNameIndex] = line[i];
inputFileNameIndex++;
}
else {
outputFileName[outputFileNameIndex] = line[i];
outputFileNameIndex++;
}
}
inputFileName[inputFileNameIndex] = '\0';
outputFileName[outputFileNameIndex] = '\0';
// Remove leading whitespace
char* trimmedInputs = inputFileName;
char* trimmedOutputs = outputFileName;
while(*trimmedInputs == ' ') trimmedInputs++;
while(*trimmedOutputs == ' ') trimmedOutputs++;
// Remove trailing whitespace
char* trimmedInBack = trimmedInputs + strlen(trimmedInputs) - 1;
char* trimmedOutBack = trimmedOutputs + strlen(trimmedOutputs) - 1;
while(*trimmedInBack == ' ') {
*trimmedInBack = '\0';
trimmedInBack--;
}
while(*trimmedOutBack == ' ') {
*trimmedOutBack = '\0';
trimmedOutBack--;
}
// Test input/output counts
char* inputFileTokens[ARRAY_MAXSIZE + 1];
char* outputFileTokens[ARRAY_MAXSIZE + 1];
int inputNum = parseInput(trimmedInputs, inputFileTokens);
int outputNum = parseInput(trimmedOutputs, outputFileTokens);
if (outputNum != 1) { // Too many output files found
*isRedirect = false;
printError();
strcpy(outputFileName, "");
return outputFileName;
}
if (inputNum != 1) { // Too many input files found
if (strcmp(inputFileTokens[0], "cat\n") && inputNum == 2) { // Test if command in inputs
strcpy(inputFileName, inputFileTokens[1]);
}
else { // Too many input files found
*isRedirect = false;
printError();
strcpy(outputFileName, "");
return outputFileName;
}
}
// Get output tokens
FILE* inputFile;
char* readLine;
ssize_t contentRead;
size_t len = 0;
unsigned int outputRow = 0;
// Test if access to file is allowed
inputFile = fopen(inputFileName, "r");
if (inputFile == NULL) {
*isRedirect = false;
printError();
strcpy(outputFileName, "");
return outputFileName;
}
// Get contents within file
while((contentRead = getline(&readLine, &len, inputFile)) != -1) {
char* lineCopy = strdup(readLine);
outputTokens[outputRow] = lineCopy;
outputRow++;
}
fclose(inputFile);
strcpy(outputFileName, trimmedOutputs);
return outputFileName;
}
bool exitProgram(char* tokens[], int numTokens) {
// Variables
char* backOfToken = tokens[0] + strlen(tokens[0]);
// Remove trailing newline (this is unecessary and doesn't work as intended)
// but it does not cause any issues either
while((*--backOfToken) == '\n');
*(backOfToken + 1) = '\0';
// Test for valid exit call
if (strcmp(tokens[0], "exit") == 0) {
if (numTokens != 1) { // Test for extra arguments
printError();
return false;
}
else { // Valid exit call
return true;
}
}
return false;
}
void launchProcesses(char *tokens[], int numTokens, bool isRedirect)
{
//do not execvp with these four commands
if(isRedirect || (strcmp(tokens[0], "cd")*strcmp(tokens[0], "exit")*strcmp(tokens[0], "help")
== 0))
{
return;
}
//single command, no args
if(numTokens == 1){
//store child process id
pid_t child;
//create tokens list to feed to execvp, must end with 0
char* temp[2];
temp[0] = tokens[0];
temp[1] = 0;
int result;
//child process faild to launch
if ((child = fork()) < 0){
printError();
//halt process
exit(1);
}
//child process
else if (child == 0){
result = execvp(temp[0], temp);
if(result < 0)
{
printError();
exit(1);
}
}
//parent process
else{
//have parent wait unless execvp failes or child fails
while(!(result < 0) && wait(NULL) != child){;}
}
}
//single command, many args
else{
//loop through each argument after the command
for(int i = 1; i < numTokens; ++i) {
pid_t child;
char* temp[3];
temp[0] = tokens[0];
temp[1] = tokens[i];
temp[2] = 0;
int result;
//fork failed
if ((child = fork()) < 0){
printError();
//halt process
exit(1);
}
//child process
else if (child == 0){
result = execvp(temp[0], temp);
if(result < 0)
{
printError();
exit(1);
}
}
//parent process
else{
//have parent wait unless execvp failes or child fails
while(!(result < 0) && wait(NULL) != child){;}
}
}
}
}
void changeDirectories(char *tokens[], int numTokens)
{
//strcmp ignores the tail null char in strings when comparing so "aa\0" == "aa"
if(strcmp(tokens[0],"cd") == 0){
//special case where we cd to home directory
if(numTokens == 1){
chdir(getenv("HOME"));
}
//if num args is equalt to 2, which is required by cd then cd
else if(numTokens == 2){
chdir(tokens[1]);
}
//o.w. tell user error
else{
printError();
}
}
}
char *executeCommand(char *cmd, bool *isRedirect, char* tokens[], char* outputTokens[],
bool *isExits)
{
//var creation
char* out_fname = (char* ) malloc(sizeof(char) * (ARRAY_MAXSIZE + 1)); // String to save the output file name to.
strcpy(out_fname, "");
char* clone = strdup(cmd);
char* is_redirect = (strchr(cmd, '>'));
//printf("%c\n", *is_redirect);
int num_tokens = 0;
//add '\n' for batch executions
strcat(cmd, "\n");
//check for redirect
if(is_redirect != NULL){
//was a redirect so save output file name
char* temp;
temp = redirectCommand(">", clone, isRedirect, tokens, outputTokens);
strcpy(out_fname, temp);
free(temp);
}
else{
isRedirect = false;
//o.w. parse string and get # of tokens (command + num of args)
num_tokens = parseInput(clone, tokens);
//exit if no tokens
if(num_tokens == 0){
return out_fname;
}
*isExits = exitProgram(tokens, num_tokens);
changeDirectories(tokens, num_tokens);
printHelp(tokens, num_tokens);
launchProcesses(tokens, num_tokens, isRedirect);
}
return out_fname;
}
void printHelp(char* tokens[], int numTokens)
{
if(strcmp(tokens[0],"help") == 0){
if (numTokens != 1)
{
printError();
}
else{
printf("\nFroilin/Tristan's example linux shell.\n");
printf("These shell commands are defined internally.\n");
printf("help -prints this screen so you can see available shell commands.\n");
printf("cd -changes directories to specified path; if not given, defaults to home.\n");
printf("exit -closes the example shell.\n");
printf("[input] > [output] -pipes input file into output file\n\n");
printf("And more! If it's not explicitly defined here (or in the documentation for the assignment)");
printf("then the command should try to be executed by launchProcesses.\n");
printf("That's how we get ls -la to work here!\n\n");
//we straigth up lied here, as cd defaults home is not req to be implemented
//and therefore was not.
}
}
}