From 3c0daa7073c3aad962234eb200fae7b6da009d60 Mon Sep 17 00:00:00 2001 From: TriantaTV Date: Thu, 27 Oct 2022 05:38:12 -0500 Subject: [PATCH] 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)++; - } -}