From 52aeeb02e5cc60fa4687fb345df521468eb957c6 Mon Sep 17 00:00:00 2001 From: TriantaTV Date: Tue, 25 Oct 2022 01:09:00 -0500 Subject: [PATCH 1/3] 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 2/3] 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 3/3] 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)++; } }