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