From 8a1ac21a5ccf5b55a568f2c9f1723b0243043f52 Mon Sep 17 00:00:00 2001 From: TriantaTV Date: Thu, 27 Oct 2022 00:25:40 -0500 Subject: [PATCH 1/4] 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 2/4] 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 3/4] 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 4/4] 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)++; - } -}