From cd7861dbce5ed272c331e9c80cf8d348b1cea8d9 Mon Sep 17 00:00:00 2001 From: Samantha Boyer Date: Sat, 22 Oct 2022 20:47:03 -0500 Subject: [PATCH 01/18] Made changes to getInput() --- src/Pish.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/Pish.c b/src/Pish.c index cac9605..9241d98 100644 --- a/src/Pish.c +++ b/src/Pish.c @@ -2,13 +2,20 @@ #include #include #include +#include #include "Integrated.h" #include "Pish.h" // Prints a prompt and then reads a command from the terminal char* getInput(void) { - return "echo hello world"; + char prompt = '$'; + char *commandLine; + + printf("%c ", prompt); + scanf("%[^\n]", commandLine); + + return commandLine; } // Executes a command to the system From c21e2fb7f6d61936387a0286b3273d58b73363db Mon Sep 17 00:00:00 2001 From: Samantha Boyer Date: Sat, 22 Oct 2022 21:16:24 -0500 Subject: [PATCH 02/18] Added parsing to Pish() --- src/Pish.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/Pish.c b/src/Pish.c index 9241d98..e2cb4d1 100644 --- a/src/Pish.c +++ b/src/Pish.c @@ -32,7 +32,16 @@ void Pish(void) while (1) { command = getInput(); - IntegratedCheck(command); + //Parse command + char* token = strtok(command, " "); + while(token != NULL) + { + //This only holds 1 command at a time then it overrides + //Will need modified + token = strtok(NULL, " "); + } + + IntegratedCheck(command); retval = fork(); // Child if (retval == 0) From adc4064f08319ce7158f73e9c33932e3347b55c8 Mon Sep 17 00:00:00 2001 From: Samantha Boyer Date: Sun, 23 Oct 2022 20:57:30 -0500 Subject: [PATCH 03/18] Added a redirect checker and started ioredirect function --- src/Pish.c | 49 +++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 43 insertions(+), 6 deletions(-) diff --git a/src/Pish.c b/src/Pish.c index e2cb4d1..45a0104 100644 --- a/src/Pish.c +++ b/src/Pish.c @@ -6,6 +6,32 @@ #include "Integrated.h" #include "Pish.h" +//checks if there is a special symbol for redirection +int redirectCheck(char* command) +{ + int check; + if(strchr(str, '|') != NULL){ + check = 1; + } + else if (strpbrk(str, ">>") != NULL){ + check = 2; + } + else if (strchr(str, '>') != NULL){ + check = 3; + } + else if (strchr(str, '<') != NULL){ + check = 4; + } + else{ + check = 0; +} +//i/o Redirection (void my need to be changed) +void ioRedirect(int sym, char* command) +{ + // |1, >>2, >3, <4 + +} + // Prints a prompt and then reads a command from the terminal char* getInput(void) { @@ -29,18 +55,29 @@ void Pish(void) { char* command; int retval; + int redirect; + while (1) { command = getInput(); //Parse command - char* token = strtok(command, " "); - while(token != NULL) + // Need to check for |,<,>,>> + //redirect = redirectCheck(command); + if (redirect > 0) { - //This only holds 1 command at a time then it overrides - //Will need modified - token = strtok(NULL, " "); + //ioRedirect(redirect, command); } - + else + { + char* token = strtok(command, " "); + while(token != NULL) + { + //This only holds 1 command at a time then it overrides + //Will need modified + token = strtok(NULL, " "); + } + { + IntegratedCheck(command); retval = fork(); // Child From 52aeeb02e5cc60fa4687fb345df521468eb957c6 Mon Sep 17 00:00:00 2001 From: TriantaTV Date: Tue, 25 Oct 2022 01:09:00 -0500 Subject: [PATCH 04/18] Big commit: Cleaned formatting of Pish, added CommandStruct and Common.h for it --- Makefile | 9 +++-- include/Common.h | 9 +++++ include/Integrated.h | 2 + include/Pish.h | 7 +++- src/Pish.c | 89 ++++++++++++++++++++++++-------------------- 5 files changed, 69 insertions(+), 47 deletions(-) create mode 100755 include/Common.h diff --git a/Makefile b/Makefile index ed958b4..ae43041 100644 --- a/Makefile +++ b/Makefile @@ -1,14 +1,15 @@ INC := -I include -all: main exec +all: compile link -main: +compile: gcc $(INC) -c -o build/main.o src/main.c gcc $(INC) -c -o build/Integrated.o src/Integrated.c gcc $(INC) -c -o build/Pish.o src/Pish.c -exec: +link: gcc -o bin/pish.out build/*.o clean: - rm build/* + rm build/*.o + rm bin/*.out diff --git a/include/Common.h b/include/Common.h new file mode 100755 index 0000000..c88b51b --- /dev/null +++ b/include/Common.h @@ -0,0 +1,9 @@ +#ifndef COMMON_H +#define COMMON_H + +typedef struct CommandStruct { + char** argv; + int argc; +} CommandStruct; + +#endif \ No newline at end of file diff --git a/include/Integrated.h b/include/Integrated.h index fe2509b..0826d06 100755 --- a/include/Integrated.h +++ b/include/Integrated.h @@ -1,6 +1,8 @@ #ifndef INTEGRATED_H #define INTEGRATED_H +#include "Common.h" + void IntegratedCheck(char* command); #endif \ No newline at end of file diff --git a/include/Pish.h b/include/Pish.h index fd60acd..8242c08 100644 --- a/include/Pish.h +++ b/include/Pish.h @@ -1,8 +1,11 @@ #ifndef PISH_H #define PISH_H -char* getcmd(void); -void exec(char* command); +#include "Common.h" + void Pish(void); +void Execute(char* command); +char* GetInput(void); +void ParseInput(char* inputString, CommandStruct command); #endif diff --git a/src/Pish.c b/src/Pish.c index e2cb4d1..adfbba9 100644 --- a/src/Pish.c +++ b/src/Pish.c @@ -6,8 +6,51 @@ #include "Integrated.h" #include "Pish.h" +// Main function for Pish program +void Pish(void) +{ + CommandStruct command; + command.argc = 0; + int forkPID; + while (1) + { + ParseInput(GetInput(), command); + IntegratedCheck(command.argv[0]); + forkPID = fork(); + // Child + if (forkPID == 0) + { + // This is the child process + // Setup the child's process environment here + // E.g., where is standard I/O, how to handle signals? + // TODO: Adjust Execute() to handle CommandStruct + Execute(command.argv[0]); + // exec does not return if it succeeds + printf("ERROR: Could not execute %s\n", command.argv[0]); + exit(1); + } + // Parent + else + { + // This is the parent process; Wait for the child to finish + // Removed due to potentially useless + // int pid = forkPID; + // wait(&pid); + wait(&forkPID); + } + // TODO: Remove break when while loop doesn't break program + break; + } +} + +// Executes a command to the system +void Execute(char* command) +{ + system(command); +} + // Prints a prompt and then reads a command from the terminal -char* getInput(void) +char* GetInput(void) { char prompt = '$'; char *commandLine; @@ -18,50 +61,14 @@ char* getInput(void) return commandLine; } -// Executes a command to the system -void exec(char* command) +void ParseInput(char* inputString, CommandStruct command) { - system(command); -} - -// Main function for Pish program -void Pish(void) -{ - char* command; - int retval; - while (1) - { - command = getInput(); - //Parse command - char* token = strtok(command, " "); + //Parse command + char* token = strtok(inputString, " "); while(token != NULL) - { + { //This only holds 1 command at a time then it overrides //Will need modified token = strtok(NULL, " "); } - - IntegratedCheck(command); - retval = fork(); - // Child - if (retval == 0) - { - // This is the child process - // Setup the child's process environment here - // E.g., where is standard I/O, how to handle signals? - exec(command); - // exec does not return if it succeeds - printf("ERROR: Could not execute %s\n", command); - exit(1); - } - // Parent - else - { - // This is the parent process; Wait for the child to finish - int pid = retval; - wait(pid); - } - // TODO: Remove break when while loop doesn't break program - break; - } } From 66794db9025fd3b0b11babdb41a1da953033b37f Mon Sep 17 00:00:00 2001 From: TriantaTV Date: Tue, 25 Oct 2022 01:30:43 -0500 Subject: [PATCH 05/18] Fixed Pish errors, prompt now prints, core dump after prompt input still --- include/Common.h | 2 +- include/Pish.h | 2 +- src/Pish.c | 32 +++++++++++++++++++++----------- 3 files changed, 23 insertions(+), 13 deletions(-) diff --git a/include/Common.h b/include/Common.h index c88b51b..7b217a3 100755 --- a/include/Common.h +++ b/include/Common.h @@ -2,7 +2,7 @@ #define COMMON_H typedef struct CommandStruct { - char** argv; + char* argv[100]; // Max 100 commands int argc; } CommandStruct; diff --git a/include/Pish.h b/include/Pish.h index 8242c08..baca03e 100644 --- a/include/Pish.h +++ b/include/Pish.h @@ -6,6 +6,6 @@ void Pish(void); void Execute(char* command); char* GetInput(void); -void ParseInput(char* inputString, CommandStruct command); +void ParseInput(char* inputString, CommandStruct* command); #endif diff --git a/src/Pish.c b/src/Pish.c index adfbba9..7566cce 100644 --- a/src/Pish.c +++ b/src/Pish.c @@ -9,13 +9,16 @@ // Main function for Pish program void Pish(void) { - CommandStruct command; - command.argc = 0; + CommandStruct commandObject = {"", 0}; + CommandStruct* command = &commandObject; + char* inputStr; int forkPID; while (1) { - ParseInput(GetInput(), command); - IntegratedCheck(command.argv[0]); + inputStr = GetInput(); + ParseInput(inputStr, command); + IntegratedCheck(command->argv[0]); + printf("Successfully passed input phase\n"); forkPID = fork(); // Child if (forkPID == 0) @@ -24,9 +27,10 @@ void Pish(void) // Setup the child's process environment here // E.g., where is standard I/O, how to handle signals? // TODO: Adjust Execute() to handle CommandStruct - Execute(command.argv[0]); + printf("Executing in fork...\n"); + Execute(command->argv[0]); // exec does not return if it succeeds - printf("ERROR: Could not execute %s\n", command.argv[0]); + printf("ERROR: Could not execute %s\n", command->argv[0]); exit(1); } // Parent @@ -36,6 +40,7 @@ void Pish(void) // Removed due to potentially useless // int pid = forkPID; // wait(&pid); + printf("Waiting for fork...\n"); wait(&forkPID); } // TODO: Remove break when while loop doesn't break program @@ -61,14 +66,19 @@ char* GetInput(void) return commandLine; } -void ParseInput(char* inputString, CommandStruct command) +void ParseInput(char* inputString, CommandStruct* command) { //Parse command + int* count = &(command->argc); char* token = strtok(inputString, " "); - while(token != NULL) + command->argv[*count] = token; + *count++; + while (token != NULL) { - //This only holds 1 command at a time then it overrides - //Will need modified - token = strtok(NULL, " "); + //This only holds 1 command at a time then it overrides + //Will need modified + token = strtok(NULL, " "); + command->argv[*count] = token; + command->argc++; } } From 68e9985c186e111ce6eb59fe473bca49a3ea9bf3 Mon Sep 17 00:00:00 2001 From: TriantaTV Date: Tue, 25 Oct 2022 02:12:07 -0500 Subject: [PATCH 06/18] Program now runs once, input seems to not work, added debug in makefile for debugging --- Makefile | 14 ++++++++++++++ include/Pish.h | 2 +- src/Pish.c | 28 ++++++++++++---------------- 3 files changed, 27 insertions(+), 17 deletions(-) diff --git a/Makefile b/Makefile index ae43041..4993ac8 100644 --- a/Makefile +++ b/Makefile @@ -10,6 +10,20 @@ compile: link: gcc -o bin/pish.out build/*.o +exec: compile link + ./bin/pish.out + +debug: debugCompile debugLink + +debugCompile: + gcc $(INC) -g -c -o build/main.o src/main.c + gcc $(INC) -g -c -o build/Integrated.o src/Integrated.c + gcc $(INC) -g -c -o build/Pish.o src/Pish.c + +debugLink: + gcc -g -o bin/pish.out build/*.o + + clean: rm build/*.o rm bin/*.out diff --git a/include/Pish.h b/include/Pish.h index baca03e..1f230e4 100644 --- a/include/Pish.h +++ b/include/Pish.h @@ -5,7 +5,7 @@ void Pish(void); void Execute(char* command); -char* GetInput(void); +void GetInput(char* inputString); void ParseInput(char* inputString, CommandStruct* command); #endif diff --git a/src/Pish.c b/src/Pish.c index 7566cce..76f6191 100644 --- a/src/Pish.c +++ b/src/Pish.c @@ -11,14 +11,13 @@ void Pish(void) { CommandStruct commandObject = {"", 0}; CommandStruct* command = &commandObject; - char* inputStr; + char inputString[1000]; // Max 1000 characters int forkPID; while (1) { - inputStr = GetInput(); - ParseInput(inputStr, command); + GetInput(inputString); + ParseInput(inputString, command); IntegratedCheck(command->argv[0]); - printf("Successfully passed input phase\n"); forkPID = fork(); // Child if (forkPID == 0) @@ -27,10 +26,9 @@ void Pish(void) // Setup the child's process environment here // E.g., where is standard I/O, how to handle signals? // TODO: Adjust Execute() to handle CommandStruct - printf("Executing in fork...\n"); - Execute(command->argv[0]); + Execute(inputString); // exec does not return if it succeeds - printf("ERROR: Could not execute %s\n", command->argv[0]); + printf("ERROR: Could not execute %s\n", inputString); exit(1); } // Parent @@ -40,7 +38,6 @@ void Pish(void) // Removed due to potentially useless // int pid = forkPID; // wait(&pid); - printf("Waiting for fork...\n"); wait(&forkPID); } // TODO: Remove break when while loop doesn't break program @@ -51,19 +48,18 @@ void Pish(void) // Executes a command to the system void Execute(char* command) { + printf("%s\n", command); system(command); + exit(0); } // Prints a prompt and then reads a command from the terminal -char* GetInput(void) +void GetInput(char* inputString) { char prompt = '$'; - char *commandLine; - printf("%c ", prompt); - scanf("%[^\n]", commandLine); - - return commandLine; + scanf("%[^\n]", inputString); + return; } void ParseInput(char* inputString, CommandStruct* command) @@ -72,13 +68,13 @@ void ParseInput(char* inputString, CommandStruct* command) int* count = &(command->argc); char* token = strtok(inputString, " "); command->argv[*count] = token; - *count++; + (*count)++; while (token != NULL) { //This only holds 1 command at a time then it overrides //Will need modified token = strtok(NULL, " "); command->argv[*count] = token; - command->argc++; + (*count)++; } } From 8504f22597fafae06e2c766a2e9003c0cd77e7d2 Mon Sep 17 00:00:00 2001 From: Samantha Boyer Date: Tue, 25 Oct 2022 21:01:53 -0500 Subject: [PATCH 07/18] Fixed an error wrong name in checkRedirect --- src/Pish.c | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/src/Pish.c b/src/Pish.c index 45a0104..b6fc767 100644 --- a/src/Pish.c +++ b/src/Pish.c @@ -10,16 +10,16 @@ int redirectCheck(char* command) { int check; - if(strchr(str, '|') != NULL){ + if(strchr(command, '|') != NULL){ check = 1; } - else if (strpbrk(str, ">>") != NULL){ + else if (strpbrk(command, ">>") != NULL){ check = 2; } - else if (strchr(str, '>') != NULL){ + else if (strchr(command, '>') != NULL){ check = 3; } - else if (strchr(str, '<') != NULL){ + else if (strchr(command, '<') != NULL){ check = 4; } else{ @@ -29,7 +29,25 @@ int redirectCheck(char* command) void ioRedirect(int sym, char* command) { // |1, >>2, >3, <4 - + if(sym == 1) + { + // | + } + else if(sym == 2) + { + // >> Append + // does not replace file contents just adds + } + else if(sym == 3) + { + // > Redirecting output + // create a file if doesnt exist + // replaces files contents + } + else + { + // < ?? + } } // Prints a prompt and then reads a command from the terminal From c734e5973120d500c19c2782e4c7916be7e12411 Mon Sep 17 00:00:00 2001 From: Samantha Boyer Date: Tue, 25 Oct 2022 21:39:08 -0500 Subject: [PATCH 08/18] Fixed merge error --- src/Pish.c | 142 +++++++++++++++++++---------------------------------- 1 file changed, 50 insertions(+), 92 deletions(-) diff --git a/src/Pish.c b/src/Pish.c index b6fc767..76f6191 100644 --- a/src/Pish.c +++ b/src/Pish.c @@ -6,117 +6,75 @@ #include "Integrated.h" #include "Pish.h" -//checks if there is a special symbol for redirection -int redirectCheck(char* command) -{ - int check; - if(strchr(command, '|') != NULL){ - check = 1; - } - else if (strpbrk(command, ">>") != NULL){ - check = 2; - } - else if (strchr(command, '>') != NULL){ - check = 3; - } - else if (strchr(command, '<') != NULL){ - check = 4; - } - else{ - check = 0; -} -//i/o Redirection (void my need to be changed) -void ioRedirect(int sym, char* command) -{ - // |1, >>2, >3, <4 - if(sym == 1) - { - // | - } - else if(sym == 2) - { - // >> Append - // does not replace file contents just adds - } - else if(sym == 3) - { - // > Redirecting output - // create a file if doesnt exist - // replaces files contents - } - else - { - // < ?? - } -} - -// Prints a prompt and then reads a command from the terminal -char* getInput(void) -{ - char prompt = '$'; - char *commandLine; - - printf("%c ", prompt); - scanf("%[^\n]", commandLine); - - return commandLine; -} - -// Executes a command to the system -void exec(char* command) -{ - system(command); -} - // Main function for Pish program void Pish(void) { - char* command; - int retval; - int redirect; - + CommandStruct commandObject = {"", 0}; + CommandStruct* command = &commandObject; + char inputString[1000]; // Max 1000 characters + int forkPID; while (1) { - command = getInput(); - //Parse command - // Need to check for |,<,>,>> - //redirect = redirectCheck(command); - if (redirect > 0) - { - //ioRedirect(redirect, command); - } - else - { - char* token = strtok(command, " "); - while(token != NULL) - { - //This only holds 1 command at a time then it overrides - //Will need modified - token = strtok(NULL, " "); - } - { - - IntegratedCheck(command); - retval = fork(); + GetInput(inputString); + ParseInput(inputString, command); + IntegratedCheck(command->argv[0]); + forkPID = fork(); // Child - if (retval == 0) + if (forkPID == 0) { // This is the child process // Setup the child's process environment here // E.g., where is standard I/O, how to handle signals? - exec(command); + // TODO: Adjust Execute() to handle CommandStruct + Execute(inputString); // exec does not return if it succeeds - printf("ERROR: Could not execute %s\n", command); + printf("ERROR: Could not execute %s\n", inputString); exit(1); } // Parent else { // This is the parent process; Wait for the child to finish - int pid = retval; - wait(pid); + // Removed due to potentially useless + // int pid = forkPID; + // wait(&pid); + wait(&forkPID); } // TODO: Remove break when while loop doesn't break program break; } } + +// Executes a command to the system +void Execute(char* command) +{ + printf("%s\n", command); + system(command); + exit(0); +} + +// Prints a prompt and then reads a command from the terminal +void GetInput(char* inputString) +{ + char prompt = '$'; + printf("%c ", prompt); + scanf("%[^\n]", inputString); + return; +} + +void ParseInput(char* inputString, CommandStruct* command) +{ + //Parse command + int* count = &(command->argc); + char* token = strtok(inputString, " "); + command->argv[*count] = token; + (*count)++; + while (token != NULL) + { + //This only holds 1 command at a time then it overrides + //Will need modified + token = strtok(NULL, " "); + command->argv[*count] = token; + (*count)++; + } +} From 8a1ac21a5ccf5b55a568f2c9f1723b0243043f52 Mon Sep 17 00:00:00 2001 From: TriantaTV Date: Thu, 27 Oct 2022 00:25:40 -0500 Subject: [PATCH 09/18] Large commit: Added reset function for CommandStruct, added reading from ~/.pishrc, and fixed running command in fork --- Makefile | 5 +++-- include/Common.h | 2 ++ include/Integrated.h | 1 + include/Pish.h | 1 - src/Common.c | 11 +++++++++++ src/Integrated.c | 31 +++++++++++++++++++++++++++++++ src/Pish.c | 28 ++++++++++------------------ 7 files changed, 58 insertions(+), 21 deletions(-) create mode 100644 src/Common.c diff --git a/Makefile b/Makefile index 4993ac8..6d352fe 100644 --- a/Makefile +++ b/Makefile @@ -4,6 +4,7 @@ all: compile link compile: gcc $(INC) -c -o build/main.o src/main.c + gcc $(INC) -c -o build/Common.o src/Common.c gcc $(INC) -c -o build/Integrated.o src/Integrated.c gcc $(INC) -c -o build/Pish.o src/Pish.c @@ -13,17 +14,17 @@ link: exec: compile link ./bin/pish.out -debug: debugCompile debugLink +debug: clean debugCompile debugLink debugCompile: gcc $(INC) -g -c -o build/main.o src/main.c gcc $(INC) -g -c -o build/Integrated.o src/Integrated.c + gcc $(INC) -g -c -o build/Common.o src/Common.c gcc $(INC) -g -c -o build/Pish.o src/Pish.c debugLink: gcc -g -o bin/pish.out build/*.o - clean: rm build/*.o rm bin/*.out diff --git a/include/Common.h b/include/Common.h index 7b217a3..25ac07a 100755 --- a/include/Common.h +++ b/include/Common.h @@ -6,4 +6,6 @@ typedef struct CommandStruct { int argc; } CommandStruct; +void ResetCommandStruct(CommandStruct* command); + #endif \ No newline at end of file diff --git a/include/Integrated.h b/include/Integrated.h index 0826d06..ac3a921 100755 --- a/include/Integrated.h +++ b/include/Integrated.h @@ -4,5 +4,6 @@ #include "Common.h" void IntegratedCheck(char* command); +void ReadPishrc(void); #endif \ No newline at end of file diff --git a/include/Pish.h b/include/Pish.h index 1f230e4..8868804 100644 --- a/include/Pish.h +++ b/include/Pish.h @@ -4,7 +4,6 @@ #include "Common.h" void Pish(void); -void Execute(char* command); void GetInput(char* inputString); void ParseInput(char* inputString, CommandStruct* command); diff --git a/src/Common.c b/src/Common.c new file mode 100644 index 0000000..e416eee --- /dev/null +++ b/src/Common.c @@ -0,0 +1,11 @@ +#include "Common.h" +#include +#include + +void ResetCommandStruct(CommandStruct* command) +{ + for (int i = 0; i < command->argc; i++) + memset(command->argv[i], 0, sizeof(command->argv[i])); + command->argc = 0; + return; +} diff --git a/src/Integrated.c b/src/Integrated.c index 00103c8..407f900 100755 --- a/src/Integrated.c +++ b/src/Integrated.c @@ -1,4 +1,12 @@ +#include +#include +#include #include +#include +#include +#include +#include +#include #include "Integrated.h" // Checks for commands that are built into Pish @@ -11,4 +19,27 @@ void IntegratedCheck(char* command) if (command[0] == 'c' && command[1] == 'd') ; return; +} + +// Reads ~/.pishrc and runs each command in the file +void ReadPishrc(void) +{ + char* homeDir = (getpwuid(getuid()))->pw_dir; + strcat(homeDir, "/.pishrc"); + int fd = open(homeDir, O_RDONLY | O_CREAT); + char commandString[1000] = ""; + char buffer; + assert(fd > -1); + while (read(fd, &buffer, 1) > 0) + { + strcat(commandString, &buffer); + if (buffer == '\n') + { + // TODO: Make each command run + printf("%s\n", commandString); + memset(commandString, 0, sizeof(commandString)); + continue; + } + } + assert(close(fd) >= 0); } \ No newline at end of file diff --git a/src/Pish.c b/src/Pish.c index 76f6191..3558c32 100644 --- a/src/Pish.c +++ b/src/Pish.c @@ -13,8 +13,10 @@ void Pish(void) CommandStruct* command = &commandObject; char inputString[1000]; // Max 1000 characters int forkPID; + ReadPishrc(); while (1) { + ResetCommandStruct(command); GetInput(inputString); ParseInput(inputString, command); IntegratedCheck(command->argv[0]); @@ -25,34 +27,23 @@ void Pish(void) // This is the child process // Setup the child's process environment here // E.g., where is standard I/O, how to handle signals? - // TODO: Adjust Execute() to handle CommandStruct - Execute(inputString); + // execve() is recommended, execvpe() may be better + execvp(command->argv[0], command->argv); // exec does not return if it succeeds printf("ERROR: Could not execute %s\n", inputString); exit(1); } // Parent - else - { - // This is the parent process; Wait for the child to finish - // Removed due to potentially useless - // int pid = forkPID; - // wait(&pid); - wait(&forkPID); - } + // This is the parent process; Wait for the child to finish + // Removed due to potentially useless + // int pid = forkPID; + // wait(&pid); + wait(&forkPID); // TODO: Remove break when while loop doesn't break program break; } } -// Executes a command to the system -void Execute(char* command) -{ - printf("%s\n", command); - system(command); - exit(0); -} - // Prints a prompt and then reads a command from the terminal void GetInput(char* inputString) { @@ -62,6 +53,7 @@ void GetInput(char* inputString) return; } +// Splits a string separated by spaces into an array of strings void ParseInput(char* inputString, CommandStruct* command) { //Parse command From a8420ec3f850d82779ba1554dd5f97d271417a03 Mon Sep 17 00:00:00 2001 From: TriantaTV Date: Thu, 27 Oct 2022 03:47:19 -0500 Subject: [PATCH 10/18] Added .pishrc and a function for resetting an object of the struct --- include/Integrated.h | 3 ++- src/Common.c | 2 +- src/Integrated.c | 39 +++++++++++++++++++++++++++++---------- src/Pish.c | 21 +++++++++++---------- 4 files changed, 43 insertions(+), 22 deletions(-) diff --git a/include/Integrated.h b/include/Integrated.h index ac3a921..8fa09a7 100755 --- a/include/Integrated.h +++ b/include/Integrated.h @@ -3,7 +3,8 @@ #include "Common.h" +const char* GetHomeDir(void); void IntegratedCheck(char* command); -void ReadPishrc(void); +void ReadPishrc(CommandStruct* command, char* inputString); #endif \ No newline at end of file diff --git a/src/Common.c b/src/Common.c index e416eee..62393af 100644 --- a/src/Common.c +++ b/src/Common.c @@ -5,7 +5,7 @@ void ResetCommandStruct(CommandStruct* command) { for (int i = 0; i < command->argc; i++) - memset(command->argv[i], 0, sizeof(command->argv[i])); + command->argv[i] = ""; command->argc = 0; return; } diff --git a/src/Integrated.c b/src/Integrated.c index 407f900..3753902 100755 --- a/src/Integrated.c +++ b/src/Integrated.c @@ -7,8 +7,18 @@ #include #include #include +#include #include "Integrated.h" +// Gets home directory location of user +const char* GetHomeDir(void) +{ + char* homeDir = (getpwuid(getuid()))->pw_dir; + strcat(homeDir, "/.pishrc"); + return homeDir; +} + + // Checks for commands that are built into Pish void IntegratedCheck(char* command) { @@ -22,24 +32,33 @@ void IntegratedCheck(char* command) } // Reads ~/.pishrc and runs each command in the file -void ReadPishrc(void) +void ReadPishrc(CommandStruct* command, char* inputString) { - char* homeDir = (getpwuid(getuid()))->pw_dir; - strcat(homeDir, "/.pishrc"); - int fd = open(homeDir, O_RDONLY | O_CREAT); - char commandString[1000] = ""; char buffer; + int forkID; + int* argc = &(command->argc); + int fd = open(GetHomeDir(), O_RDONLY | O_CREAT); assert(fd > -1); while (read(fd, &buffer, 1) > 0) { - strcat(commandString, &buffer); + printf("%c", buffer); + if (buffer == ' ') + { + command->argv[*argc] = inputString; + strcpy(inputString, ""); + *argc++; + } if (buffer == '\n') { - // TODO: Make each command run - printf("%s\n", commandString); - memset(commandString, 0, sizeof(commandString)); - continue; + command->argv[*argc] = inputString; + strcpy(inputString, ""); + forkID = fork(); + if (forkID == 0) + execvp(command->argv[0], command->argv); + wait(&forkID); + ResetCommandStruct(command); } + strcat(inputString, &buffer); } assert(close(fd) >= 0); } \ No newline at end of file diff --git a/src/Pish.c b/src/Pish.c index 3558c32..3874824 100644 --- a/src/Pish.c +++ b/src/Pish.c @@ -13,10 +13,11 @@ void Pish(void) CommandStruct* command = &commandObject; char inputString[1000]; // Max 1000 characters int forkPID; - ReadPishrc(); + ReadPishrc(command, inputString); while (1) { - ResetCommandStruct(command); + ResetCommandStruct(command); // Clean command + memset(inputString, 0, sizeof(inputString)); // Clean inputString GetInput(inputString); ParseInput(inputString, command); IntegratedCheck(command->argv[0]); @@ -40,15 +41,15 @@ void Pish(void) // wait(&pid); wait(&forkPID); // TODO: Remove break when while loop doesn't break program - break; + // break; } } // Prints a prompt and then reads a command from the terminal void GetInput(char* inputString) { - char prompt = '$'; - printf("%c ", prompt); + char* prompt = "pish%>"; + printf("%s ", prompt); scanf("%[^\n]", inputString); return; } @@ -57,16 +58,16 @@ void GetInput(char* inputString) void ParseInput(char* inputString, CommandStruct* command) { //Parse command - int* count = &(command->argc); + int* argc = &(command->argc); char* token = strtok(inputString, " "); - command->argv[*count] = token; - (*count)++; + command->argv[*argc] = token; + (*argc)++; while (token != NULL) { //This only holds 1 command at a time then it overrides //Will need modified token = strtok(NULL, " "); - command->argv[*count] = token; - (*count)++; + command->argv[*argc] = token; + (*argc)++; } } From bd66bb01893a94c3b13ab1e1bb011f647118df79 Mon Sep 17 00:00:00 2001 From: TriantaTV Date: Thu, 27 Oct 2022 04:37:16 -0500 Subject: [PATCH 11/18] Fixed pish function so it can endlessly take input. Still getting random core dumps on .pishrc reading --- include/Integrated.h | 2 +- src/Integrated.c | 17 ++++++++--------- src/Pish.c | 16 ++++++++-------- 3 files changed, 17 insertions(+), 18 deletions(-) diff --git a/include/Integrated.h b/include/Integrated.h index 8fa09a7..abb848b 100755 --- a/include/Integrated.h +++ b/include/Integrated.h @@ -4,7 +4,7 @@ #include "Common.h" const char* GetHomeDir(void); -void IntegratedCheck(char* command); +void IntegratedCheck(CommandStruct* command); void ReadPishrc(CommandStruct* command, char* inputString); #endif \ No newline at end of file diff --git a/src/Integrated.c b/src/Integrated.c index 3753902..7d16273 100755 --- a/src/Integrated.c +++ b/src/Integrated.c @@ -20,13 +20,13 @@ const char* GetHomeDir(void) // Checks for commands that are built into Pish -void IntegratedCheck(char* command) +void IntegratedCheck(CommandStruct* command) { - if (command == "exit") + if (strcmp(command->argv[0], "exit") == 0) exit(0); // If there is an argument, change to argument location // Else, change to home directory - if (command[0] == 'c' && command[1] == 'd') + if (command->argv[0][0] == 'c' && command->argv[0][1] == 'd') ; return; } @@ -34,21 +34,20 @@ void IntegratedCheck(char* command) // Reads ~/.pishrc and runs each command in the file void ReadPishrc(CommandStruct* command, char* inputString) { - char buffer; + char buffer[1]; int forkID; int* argc = &(command->argc); int fd = open(GetHomeDir(), O_RDONLY | O_CREAT); assert(fd > -1); - while (read(fd, &buffer, 1) > 0) + while (read(fd, buffer, 1) > 0) { - printf("%c", buffer); - if (buffer == ' ') + if (buffer[0] == ' ') { command->argv[*argc] = inputString; strcpy(inputString, ""); *argc++; } - if (buffer == '\n') + if (buffer[0] == '\n') { command->argv[*argc] = inputString; strcpy(inputString, ""); @@ -58,7 +57,7 @@ void ReadPishrc(CommandStruct* command, char* inputString) wait(&forkID); ResetCommandStruct(command); } - strcat(inputString, &buffer); + strcat(inputString, buffer); } assert(close(fd) >= 0); } \ No newline at end of file diff --git a/src/Pish.c b/src/Pish.c index 3874824..8f583cc 100644 --- a/src/Pish.c +++ b/src/Pish.c @@ -1,3 +1,4 @@ +#include #include #include #include @@ -17,10 +18,10 @@ void Pish(void) while (1) { ResetCommandStruct(command); // Clean command - memset(inputString, 0, sizeof(inputString)); // Clean inputString + strcpy(inputString, ""); // Clean inputString GetInput(inputString); ParseInput(inputString, command); - IntegratedCheck(command->argv[0]); + IntegratedCheck(command); forkPID = fork(); // Child if (forkPID == 0) @@ -36,21 +37,20 @@ void Pish(void) } // Parent // This is the parent process; Wait for the child to finish - // Removed due to potentially useless - // int pid = forkPID; - // wait(&pid); wait(&forkPID); - // TODO: Remove break when while loop doesn't break program - // break; } } // Prints a prompt and then reads a command from the terminal void GetInput(char* inputString) { + char c; char* prompt = "pish%>"; printf("%s ", prompt); - scanf("%[^\n]", inputString); + // assert(scanf("%[^\n]s", inputString) == 1); + scanf("%1000[^\n]s", inputString); + while((c = getchar()) != '\n' && c != EOF) + /* discard */ ; return; } From 3c0daa7073c3aad962234eb200fae7b6da009d60 Mon Sep 17 00:00:00 2001 From: TriantaTV Date: Thu, 27 Oct 2022 05:38:12 -0500 Subject: [PATCH 12/18] Final fix for .pishrc, any additional changes are for handling operators --- include/Integrated.h | 2 ++ include/Pish.h | 2 -- src/Integrated.c | 48 +++++++++++++++++++++++++++++++++++--------- src/Pish.c | 31 ---------------------------- 4 files changed, 40 insertions(+), 43 deletions(-) diff --git a/include/Integrated.h b/include/Integrated.h index abb848b..fe2f4c3 100755 --- a/include/Integrated.h +++ b/include/Integrated.h @@ -5,6 +5,8 @@ const char* GetHomeDir(void); void IntegratedCheck(CommandStruct* command); +void GetInput(char* inputString); +void ParseInput(char* inputString, CommandStruct* command); void ReadPishrc(CommandStruct* command, char* inputString); #endif \ No newline at end of file diff --git a/include/Pish.h b/include/Pish.h index 8868804..9b54a1a 100644 --- a/include/Pish.h +++ b/include/Pish.h @@ -4,7 +4,5 @@ #include "Common.h" void Pish(void); -void GetInput(char* inputString); -void ParseInput(char* inputString, CommandStruct* command); #endif diff --git a/src/Integrated.c b/src/Integrated.c index 7d16273..d578754 100755 --- a/src/Integrated.c +++ b/src/Integrated.c @@ -31,6 +31,38 @@ void IntegratedCheck(CommandStruct* command) return; } + +// Prints a prompt and then reads a command from the terminal +void GetInput(char* inputString) +{ + char c; + char* prompt = "pish%>"; + printf("%s ", prompt); + // assert(scanf("%[^\n]s", inputString) == 1); + scanf("%1000[^\n]s", inputString); + while((c = getchar()) != '\n' && c != EOF) + /* discard */ ; + return; +} + +// Splits a string separated by spaces into an array of strings +void ParseInput(char* inputString, CommandStruct* command) +{ + //Parse command + int* argc = &(command->argc); + char* token = strtok(inputString, " "); + command->argv[*argc] = token; + (*argc)++; + while (token != NULL) + { + //This only holds 1 command at a time then it overrides + //Will need modified + token = strtok(NULL, " "); + command->argv[*argc] = token; + (*argc)++; + } +} + // Reads ~/.pishrc and runs each command in the file void ReadPishrc(CommandStruct* command, char* inputString) { @@ -41,23 +73,19 @@ void ReadPishrc(CommandStruct* command, char* inputString) assert(fd > -1); while (read(fd, buffer, 1) > 0) { - if (buffer[0] == ' ') - { - command->argv[*argc] = inputString; - strcpy(inputString, ""); - *argc++; - } if (buffer[0] == '\n') { - command->argv[*argc] = inputString; - strcpy(inputString, ""); + ParseInput(inputString, command); forkID = fork(); if (forkID == 0) execvp(command->argv[0], command->argv); - wait(&forkID); + else + wait(&forkID); + strcpy(inputString, ""); ResetCommandStruct(command); + continue; } strcat(inputString, buffer); } assert(close(fd) >= 0); -} \ No newline at end of file +} diff --git a/src/Pish.c b/src/Pish.c index 8f583cc..cc42bf7 100644 --- a/src/Pish.c +++ b/src/Pish.c @@ -40,34 +40,3 @@ void Pish(void) wait(&forkPID); } } - -// Prints a prompt and then reads a command from the terminal -void GetInput(char* inputString) -{ - char c; - char* prompt = "pish%>"; - printf("%s ", prompt); - // assert(scanf("%[^\n]s", inputString) == 1); - scanf("%1000[^\n]s", inputString); - while((c = getchar()) != '\n' && c != EOF) - /* discard */ ; - return; -} - -// Splits a string separated by spaces into an array of strings -void ParseInput(char* inputString, CommandStruct* command) -{ - //Parse command - int* argc = &(command->argc); - char* token = strtok(inputString, " "); - command->argv[*argc] = token; - (*argc)++; - while (token != NULL) - { - //This only holds 1 command at a time then it overrides - //Will need modified - token = strtok(NULL, " "); - command->argv[*argc] = token; - (*argc)++; - } -} From 9f28625f1a9b0de3282eccdc79e975b9c88704a3 Mon Sep 17 00:00:00 2001 From: Samantha Boyer Date: Thu, 27 Oct 2022 19:17:35 -0500 Subject: [PATCH 13/18] Committing again because push did not go through --- src/Integrated.c | 62 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/src/Integrated.c b/src/Integrated.c index d578754..339c314 100755 --- a/src/Integrated.c +++ b/src/Integrated.c @@ -89,3 +89,65 @@ void ReadPishrc(CommandStruct* command, char* inputString) } assert(close(fd) >= 0); } +// i/o redirection +void ioRedirection(char* inputString, CommandStruct* command) +{ + int newfd; + // check command standard output +if (strchr(inputString, '>' )!= NULL) + { + + newfd = open((command, O_CREAT |O_TRUNC | O_WRONLY, 0644)) <0) { + //failed + perror(command); + exit(1); + } + + // copies the new file descriptor + dup2(newfd,1); + }//this is supposed to close if statement?? + +// check command standard input + if(strchr(inputString, '<') != NULL) + { + newfd = open((command, O_CREAT |O_TRUNC | O_WRONLY, 0644)) <0) { + //failed + perror(command); + exit(1); + } + dup2(newfd,0); + } + +//check pipe + if(strchr(command, '|') != NULL) + { + + } +//check append + if (strpbrk(command, ">>") != NULL) + newfd = open((command, O_CREAT |O_TRUNC | O_WRONLY, 0644)) <0) { + //failed + perror(command); + exit(1); + } + dup2(newfd,0); + } + +//check pipe + if(strchr(command, '|') != NULL) + { + + } +//check append + if (strpbrk(command, ">>") != NULL) + { + + } +} +//Environment varibles +void varibles(char str, int count) +{ + char var[1000]; + var[count]= str; + +} From 72c42aa8252620d4c694ca9715a16a1c1e38f80b Mon Sep 17 00:00:00 2001 From: TriantaTV Date: Thu, 27 Oct 2022 19:57:26 -0500 Subject: [PATCH 14/18] Added comments and tips for code --- include/Integrated.h | 1 + src/Integrated.c | 244 +++++++++++++++++++++++-------------------- 2 files changed, 132 insertions(+), 113 deletions(-) diff --git a/include/Integrated.h b/include/Integrated.h index fe2f4c3..9a0f05a 100755 --- a/include/Integrated.h +++ b/include/Integrated.h @@ -4,6 +4,7 @@ #include "Common.h" const char* GetHomeDir(void); +void ioRedirection(char *inputString, CommandStruct *command); void IntegratedCheck(CommandStruct* command); void GetInput(char* inputString); void ParseInput(char* inputString, CommandStruct* command); diff --git a/src/Integrated.c b/src/Integrated.c index 339c314..fedc7be 100755 --- a/src/Integrated.c +++ b/src/Integrated.c @@ -11,143 +11,161 @@ #include "Integrated.h" // Gets home directory location of user -const char* GetHomeDir(void) +const char *GetHomeDir(void) { - char* homeDir = (getpwuid(getuid()))->pw_dir; - strcat(homeDir, "/.pishrc"); - return homeDir; + char *homeDir = (getpwuid(getuid()))->pw_dir; + strcat(homeDir, "/.pishrc"); + return homeDir; } - // Checks for commands that are built into Pish -void IntegratedCheck(CommandStruct* command) +void IntegratedCheck(CommandStruct *command) { - if (strcmp(command->argv[0], "exit") == 0) - exit(0); - // If there is an argument, change to argument location - // Else, change to home directory - if (command->argv[0][0] == 'c' && command->argv[0][1] == 'd') - ; - return; + if (strcmp(command->argv[0], "exit") == 0) + exit(0); + // If there is an argument, change to argument location + // Else, change to home directory + if (command->argv[0][0] == 'c' && command->argv[0][1] == 'd') + ; + return; } - // Prints a prompt and then reads a command from the terminal -void GetInput(char* inputString) +void GetInput(char *inputString) { - char c; - char* prompt = "pish%>"; - printf("%s ", prompt); - // assert(scanf("%[^\n]s", inputString) == 1); - scanf("%1000[^\n]s", inputString); - while((c = getchar()) != '\n' && c != EOF) - /* discard */ ; - return; + char c; + char *prompt = "pish%>"; + printf("%s ", prompt); + // assert(scanf("%[^\n]s", inputString) == 1); + scanf("%1000[^\n]s", inputString); + while ((c = getchar()) != '\n' && c != EOF) + /* discard */; + return; } // Splits a string separated by spaces into an array of strings -void ParseInput(char* inputString, CommandStruct* command) +void ParseInput(char *inputString, CommandStruct *command) { - //Parse command - int* argc = &(command->argc); - char* token = strtok(inputString, " "); - command->argv[*argc] = token; - (*argc)++; + // Parse command + int *argc = &(command->argc); + char *token = strtok(inputString, " "); + command->argv[*argc] = token; + (*argc)++; while (token != NULL) - { - //This only holds 1 command at a time then it overrides - //Will need modified - token = strtok(NULL, " "); - command->argv[*argc] = token; - (*argc)++; + { + // This only holds 1 command at a time then it overrides + // Will need modified + token = strtok(NULL, " "); + command->argv[*argc] = token; + (*argc)++; } } // Reads ~/.pishrc and runs each command in the file -void ReadPishrc(CommandStruct* command, char* inputString) +void ReadPishrc(CommandStruct *command, char *inputString) { - char buffer[1]; - int forkID; - int* argc = &(command->argc); - int fd = open(GetHomeDir(), O_RDONLY | O_CREAT); - assert(fd > -1); - while (read(fd, buffer, 1) > 0) - { - if (buffer[0] == '\n') - { - ParseInput(inputString, command); - forkID = fork(); - if (forkID == 0) - execvp(command->argv[0], command->argv); - else - wait(&forkID); - strcpy(inputString, ""); - ResetCommandStruct(command); - continue; - } - strcat(inputString, buffer); - } - assert(close(fd) >= 0); + char buffer[1]; + int forkID; + int *argc = &(command->argc); + int fd = open(GetHomeDir(), O_RDONLY | O_CREAT); + assert(fd > -1); + while (read(fd, buffer, 1) > 0) + { + if (buffer[0] == '\n') + { + ParseInput(inputString, command); + forkID = fork(); + if (forkID == 0) + execvp(command->argv[0], command->argv); + else + wait(&forkID); + strcpy(inputString, ""); + ResetCommandStruct(command); + continue; + } + strcat(inputString, buffer); + } + assert(close(fd) >= 0); } + // i/o redirection -void ioRedirection(char* inputString, CommandStruct* command) +// TODO: Work on one symbol at a time +// inputString may not be useful here, only *command +// Try to add a layer of abstraction where possible +// I.E. think about using function instead for checking +void ioRedirection(char *inputString, CommandStruct *command) { - int newfd; - // check command standard output -if (strchr(inputString, '>' )!= NULL) - { + int newfd; + // check command standard output + // TODO: Check command->argv[] for symbol instead + if (strchr(inputString, '>') != NULL) + { + // TODO: Throwing an error here and line 108 for using command + // Check if targeting a specific string in command->argv works + newfd = (open((command, O_CREAT |O_TRUNC | O_WRONLY, 0644)) < 0); + // No if statement for fail checking, brackets just exist here + { + // failed + perror(command); + exit(1); + } - newfd = open((command, O_CREAT |O_TRUNC | O_WRONLY, 0644)) <0) { - //failed - perror(command); - exit(1); - } + // copies the new file descriptor + dup2(newfd, 1); + } // this is supposed to close if statement?? - // copies the new file descriptor - dup2(newfd,1); - }//this is supposed to close if statement?? + // check command standard input + // if (strchr(inputString, '<') != NULL) + // { + // newfd = open((command, O_CREAT |O_TRUNC | O_WRONLY, 0644)) < 0); + // // No if statement for fail checking, brackets just exist here + // { + // // failed + // perror(command); + // exit(1); + // } + // dup2(newfd, 0); + // } -// check command standard input - if(strchr(inputString, '<') != NULL) - { - newfd = open((command, O_CREAT |O_TRUNC | O_WRONLY, 0644)) <0) { - //failed - perror(command); - exit(1); - } - dup2(newfd,0); - } - -//check pipe - if(strchr(command, '|') != NULL) - { - - } -//check append - if (strpbrk(command, ">>") != NULL) - newfd = open((command, O_CREAT |O_TRUNC | O_WRONLY, 0644)) <0) { - //failed - perror(command); - exit(1); - } - dup2(newfd,0); - } - -//check pipe - if(strchr(command, '|') != NULL) - { - - } -//check append - if (strpbrk(command, ">>") != NULL) - { - - } + // check pipe + // TODO: Check for pipe symbol + // Check in command->argv[] instead + // if (strchr(command, '|') != NULL) + // { + // ; + // } + // check append + // TODO: Implement append + // if (strpbrk(command, ">>") != NULL) + // { + // newfd = open((command, O_CREAT |O_TRUNC | O_WRONLY, 0644)) < 0); + // // No if statement for fail checking, brackets just exist here + // { + // // failed + // perror(command); + // exit(1); + // } + // dup2(newfd, 0); + // } + // check pipe + // TODO: Check pipe already exists above, maybe loop? + // if (strchr(command, '|') != NULL) + // { + // ; + // } + // check append + // TODO: Check append already exists above, maybe loop? + // if (strpbrk(command, ">>") != NULL) + // { + // ; + // } } -//Environment varibles -void varibles(char str, int count) -{ - char var[1000]; - var[count]= str; -} +// Environment varibles +// TODO: Add this function to CommandStruct +// Environment Variables can just be a part of the struct +// void varibles(char str, int count) +// { +// char var[1000]; +// var[count] = str; +// } From f87379c9dac963bf834922da1990ece858d42c7e Mon Sep 17 00:00:00 2001 From: Samantha Boyer Date: Thu, 27 Oct 2022 22:45:40 -0500 Subject: [PATCH 15/18] fixed the io redirection --- src/Integrated.c | 69 ++++++++++++++++++++++++++++++++++++------------ 1 file changed, 52 insertions(+), 17 deletions(-) diff --git a/src/Integrated.c b/src/Integrated.c index fedc7be..d1a2ef0 100755 --- a/src/Integrated.c +++ b/src/Integrated.c @@ -93,27 +93,62 @@ void ReadPishrc(CommandStruct *command, char *inputString) // inputString may not be useful here, only *command // Try to add a layer of abstraction where possible // I.E. think about using function instead for checking -void ioRedirection(char *inputString, CommandStruct *command) +void ioRedirection(CommandStruct *command) { int newfd; // check command standard output // TODO: Check command->argv[] for symbol instead - if (strchr(inputString, '>') != NULL) - { - // TODO: Throwing an error here and line 108 for using command - // Check if targeting a specific string in command->argv works - newfd = (open((command, O_CREAT |O_TRUNC | O_WRONLY, 0644)) < 0); - // No if statement for fail checking, brackets just exist here - { - // failed - perror(command); - exit(1); - } - - // copies the new file descriptor - dup2(newfd, 1); - } // this is supposed to close if statement?? - + for(int i = 0; i < sizeof(command); i++) + { + if (strchr(command->argv[i], '>') != NULL) + { + output(command->argv[i+1]); + // Check if targeting a specific string in command->argv works + } + if (strchr(command->argv[i], '<') != NULL) + { + input(command->[i+1]); + } + if (strchr(command->argv[i], '|')!= NULL) + { + // not sure if i + 1 correct + // pipe(command->argv[i+1]); + } + if (strpbrk(command->argv[i], ">>")!= NULL) + { + append(command->argv[i-1], command->argv[i+1]); + } + } +} +void output(char *command){ + int newfd; + if(newfd = open(command, O_CREAT | O_TRUNC | O_WRONLY, 0644)< 0) + { + perror(command); + exit(1); + } + dup2(newfd,1); +} +void input(char *command) +{ + int newfd; + if(newfd = open(command, O_RDONLY)<0) + { + perror(command); + exit(1); + } + dup2(newfd,0); +} +void pipe(){} +void append(char *input, char *file) +{ + FILE *fp; + + fp = fopen(file, "a+"); + + fputs(input); + fclose(fp); +} // check command standard input // if (strchr(inputString, '<') != NULL) // { From 702b77073fc3001238e2ad62ea2253d42f76c61e Mon Sep 17 00:00:00 2001 From: TriantaTV Date: Thu, 27 Oct 2022 23:11:40 -0500 Subject: [PATCH 16/18] Fixed compilation errors --- include/Integrated.h | 6 +++++- src/Integrated.c | 14 ++++++++++---- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/include/Integrated.h b/include/Integrated.h index 9a0f05a..1b91336 100755 --- a/include/Integrated.h +++ b/include/Integrated.h @@ -4,9 +4,13 @@ #include "Common.h" const char* GetHomeDir(void); -void ioRedirection(char *inputString, CommandStruct *command); +void ioRedirection(CommandStruct *command); void IntegratedCheck(CommandStruct* command); void GetInput(char* inputString); +void append(char *input, char *file); +void input(char *command); +void ExecPipe(); +void output(char *command); void ParseInput(char* inputString, CommandStruct* command); void ReadPishrc(CommandStruct* command, char* inputString); diff --git a/src/Integrated.c b/src/Integrated.c index d1a2ef0..3c14943 100755 --- a/src/Integrated.c +++ b/src/Integrated.c @@ -107,7 +107,7 @@ void ioRedirection(CommandStruct *command) } if (strchr(command->argv[i], '<') != NULL) { - input(command->[i+1]); + input(command->argv[i+1]); } if (strchr(command->argv[i], '|')!= NULL) { @@ -120,6 +120,7 @@ void ioRedirection(CommandStruct *command) } } } + void output(char *command){ int newfd; if(newfd = open(command, O_CREAT | O_TRUNC | O_WRONLY, 0644)< 0) @@ -129,6 +130,7 @@ void output(char *command){ } dup2(newfd,1); } + void input(char *command) { int newfd; @@ -139,14 +141,19 @@ void input(char *command) } dup2(newfd,0); } -void pipe(){} + +void ExecPipe() +{ + ; +} + void append(char *input, char *file) { FILE *fp; fp = fopen(file, "a+"); - fputs(input); + fputs(input, fp); fclose(fp); } // check command standard input @@ -194,7 +201,6 @@ void append(char *input, char *file) // { // ; // } -} // Environment varibles // TODO: Add this function to CommandStruct From 94f2c4fcb8581b13d3e22bb5ffaad343ecdb47a7 Mon Sep 17 00:00:00 2001 From: TriantaTV Date: Thu, 27 Oct 2022 23:23:02 -0500 Subject: [PATCH 17/18] Adjusted a little bit of syntax (personal) --- src/Integrated.c | 151 +++++++++++++++++++++++------------------------ 1 file changed, 75 insertions(+), 76 deletions(-) diff --git a/src/Integrated.c b/src/Integrated.c index 3c14943..b003a34 100755 --- a/src/Integrated.c +++ b/src/Integrated.c @@ -100,30 +100,30 @@ void ioRedirection(CommandStruct *command) // TODO: Check command->argv[] for symbol instead for(int i = 0; i < sizeof(command); i++) { - if (strchr(command->argv[i], '>') != NULL) - { - output(command->argv[i+1]); - // Check if targeting a specific string in command->argv works - } - if (strchr(command->argv[i], '<') != NULL) - { - input(command->argv[i+1]); - } - if (strchr(command->argv[i], '|')!= NULL) - { - // not sure if i + 1 correct - // pipe(command->argv[i+1]); - } - if (strpbrk(command->argv[i], ">>")!= NULL) - { - append(command->argv[i-1], command->argv[i+1]); - } + if (strchr(command->argv[i], '>') != NULL) + { + output(command->argv[i+1]); + // Check if targeting a specific string in command->argv works + } + if (strchr(command->argv[i], '<') != NULL) + { + input(command->argv[i+1]); + } + if (strchr(command->argv[i], '|')!= NULL) + { + // not sure if i + 1 correct + // pipe(command->argv[i+1]); + } + if (strpbrk(command->argv[i], ">>")!= NULL) + { + append(command->argv[i-1], command->argv[i+1]); + } } } void output(char *command){ int newfd; - if(newfd = open(command, O_CREAT | O_TRUNC | O_WRONLY, 0644)< 0) + if (newfd = open(command, O_CREAT | O_TRUNC | O_WRONLY, 0644) < 0) { perror(command); exit(1); @@ -133,13 +133,13 @@ void output(char *command){ void input(char *command) { - int newfd; - if(newfd = open(command, O_RDONLY)<0) - { - perror(command); - exit(1); - } - dup2(newfd,0); + int newfd; + if (newfd = open(command, O_RDONLY) < 0) + { + perror(command); + exit(1); + } + dup2(newfd,0); } void ExecPipe() @@ -149,58 +149,57 @@ void ExecPipe() void append(char *input, char *file) { - FILE *fp; - - fp = fopen(file, "a+"); - - fputs(input, fp); - fclose(fp); + FILE *fp; + fp = fopen(file, "a+"); + fputs(input, fp); + fclose(fp); } - // check command standard input - // if (strchr(inputString, '<') != NULL) - // { - // newfd = open((command, O_CREAT |O_TRUNC | O_WRONLY, 0644)) < 0); - // // No if statement for fail checking, brackets just exist here - // { - // // failed - // perror(command); - // exit(1); - // } - // dup2(newfd, 0); - // } - // check pipe - // TODO: Check for pipe symbol - // Check in command->argv[] instead - // if (strchr(command, '|') != NULL) - // { - // ; - // } - // check append - // TODO: Implement append - // if (strpbrk(command, ">>") != NULL) - // { - // newfd = open((command, O_CREAT |O_TRUNC | O_WRONLY, 0644)) < 0); - // // No if statement for fail checking, brackets just exist here - // { - // // failed - // perror(command); - // exit(1); - // } - // dup2(newfd, 0); - // } - // check pipe - // TODO: Check pipe already exists above, maybe loop? - // if (strchr(command, '|') != NULL) - // { - // ; - // } - // check append - // TODO: Check append already exists above, maybe loop? - // if (strpbrk(command, ">>") != NULL) - // { - // ; - // } +// check command standard input +// if (strchr(inputString, '<') != NULL) +// { +// newfd = open((command, O_CREAT |O_TRUNC | O_WRONLY, 0644)) < 0); +// // No if statement for fail checking, brackets just exist here +// { +// // failed +// perror(command); +// exit(1); +// } +// dup2(newfd, 0); +// } + +// check pipe +// TODO: Check for pipe symbol +// Check in command->argv[] instead +// if (strchr(command, '|') != NULL) +// { +// ; +// } +// check append +// TODO: Implement append +// if (strpbrk(command, ">>") != NULL) +// { +// newfd = open((command, O_CREAT |O_TRUNC | O_WRONLY, 0644)) < 0); +// // No if statement for fail checking, brackets just exist here +// { +// // failed +// perror(command); +// exit(1); +// } +// dup2(newfd, 0); +// } +// check pipe +// TODO: Check pipe already exists above, maybe loop? +// if (strchr(command, '|') != NULL) +// { +// ; +// } +// check append +// TODO: Check append already exists above, maybe loop? +// if (strpbrk(command, ">>") != NULL) +// { +// ; +// } // Environment varibles // TODO: Add this function to CommandStruct From e0905dbaf2b785420ab67aada282d0a252ce3170 Mon Sep 17 00:00:00 2001 From: TriantaTV Date: Fri, 28 Oct 2022 16:14:53 -0500 Subject: [PATCH 18/18] Adjusted GetHomeDir() and fixed cd command --- include/Integrated.h | 2 +- src/Common.c | 2 +- src/Integrated.c | 17 ++++++++++++----- src/Pish.c | 3 ++- 4 files changed, 16 insertions(+), 8 deletions(-) diff --git a/include/Integrated.h b/include/Integrated.h index 1b91336..ecfd85f 100755 --- a/include/Integrated.h +++ b/include/Integrated.h @@ -5,7 +5,7 @@ const char* GetHomeDir(void); void ioRedirection(CommandStruct *command); -void IntegratedCheck(CommandStruct* command); +int IntegratedCheck(CommandStruct* command); void GetInput(char* inputString); void append(char *input, char *file); void input(char *command); diff --git a/src/Common.c b/src/Common.c index 62393af..1f230a8 100644 --- a/src/Common.c +++ b/src/Common.c @@ -5,7 +5,7 @@ void ResetCommandStruct(CommandStruct* command) { for (int i = 0; i < command->argc; i++) - command->argv[i] = ""; + command->argv[i] = NULL; command->argc = 0; return; } diff --git a/src/Integrated.c b/src/Integrated.c index b003a34..3722651 100755 --- a/src/Integrated.c +++ b/src/Integrated.c @@ -14,20 +14,25 @@ const char *GetHomeDir(void) { char *homeDir = (getpwuid(getuid()))->pw_dir; - strcat(homeDir, "/.pishrc"); return homeDir; } // Checks for commands that are built into Pish -void IntegratedCheck(CommandStruct *command) +int IntegratedCheck(CommandStruct *command) { if (strcmp(command->argv[0], "exit") == 0) exit(0); // If there is an argument, change to argument location // Else, change to home directory if (command->argv[0][0] == 'c' && command->argv[0][1] == 'd') - ; - return; + { + if (command->argv[1] != NULL) + chdir(command->argv[1]); + if (command->argv[1] == NULL) + chdir(GetHomeDir()); + return 1; + } + return 0; } // Prints a prompt and then reads a command from the terminal @@ -67,7 +72,9 @@ void ReadPishrc(CommandStruct *command, char *inputString) char buffer[1]; int forkID; int *argc = &(command->argc); - int fd = open(GetHomeDir(), O_RDONLY | O_CREAT); + char* pishLocation = GetHomeDir(); + strcat(pishLocation, "/.pishrc"); + int fd = open(pishLocation, O_RDONLY | O_CREAT); assert(fd > -1); while (read(fd, buffer, 1) > 0) { diff --git a/src/Pish.c b/src/Pish.c index cc42bf7..a4d370d 100644 --- a/src/Pish.c +++ b/src/Pish.c @@ -21,7 +21,8 @@ void Pish(void) strcpy(inputString, ""); // Clean inputString GetInput(inputString); ParseInput(inputString, command); - IntegratedCheck(command); + if (IntegratedCheck(command)) + continue; forkPID = fork(); // Child if (forkPID == 0)