From a0d601f1e35f4a9a92b1eac1cb31f77f96f38977 Mon Sep 17 00:00:00 2001 From: TriantaTV Date: Sat, 29 Oct 2022 05:37:29 -0500 Subject: [PATCH] Fixed pipe operator breaking output. Nothing actually pipes yet --- include/Integrated.h | 2 +- include/Pish.h | 1 + src/Integrated.c | 57 +++++++++++++++++++++++--------------------- src/Pish.c | 15 +++++++++++- 4 files changed, 46 insertions(+), 29 deletions(-) diff --git a/include/Integrated.h b/include/Integrated.h index b8433e9..e3c66a9 100755 --- a/include/Integrated.h +++ b/include/Integrated.h @@ -5,6 +5,7 @@ void CheckRedirection(CommandStruct* command); int CheckSymbol(CommandStruct* command, char symbol, int start); +int CountSymbol(CommandStruct* command, char symbol); int IntegratedCheck(CommandStruct* command); void GetInput(char* inputString); void append(char *input, char *file); @@ -13,6 +14,5 @@ int ExecPipe(CommandStruct* commandParent, CommandStruct* commandChild, int* pip void output(char *command); void SplitInput(char* inputString, CommandStruct* command); void ParseInput(char* inputString, CommandStruct* command, char* symbol); -void RunChild(CommandStruct* commandChild, int* pipefd); void ioRedirection(CommandStruct* command); #endif diff --git a/include/Pish.h b/include/Pish.h index e093cd1..d42d669 100644 --- a/include/Pish.h +++ b/include/Pish.h @@ -5,5 +5,6 @@ void Pish(void); void ReadPishrc(CommandStruct* commandParent, CommandStruct *commandChild, char* inputString); +void RunChild(CommandStruct* commandChild, int* pipefd); #endif diff --git a/src/Integrated.c b/src/Integrated.c index c39bcfc..0fa1ff0 100755 --- a/src/Integrated.c +++ b/src/Integrated.c @@ -57,23 +57,6 @@ void ParseInput(char *inputString, CommandStruct *command, char *symbol) } } -// Run child process -void RunChild(CommandStruct* commandChild, int* pipefd) -{ - close(pipefd[0]); - dup2(pipefd[1], 1); - close(pipefd[1]); - // This is the child process - // Setup the child's process environment here - // E.g., where is standard I/O, how to handle signals? - // execve() is recommended, execvpe() may be better - execvp(commandChild->argv[0], commandChild->argv); - // exec does not return if it succeeds - printf("ERROR: Could not execute %s\n", commandChild->argv[0]); - exit(1); -} - - // Splits a string separated by spaces into an array of strings void SplitInput(char *inputString, CommandStruct *command) { @@ -104,7 +87,6 @@ int CheckSymbol(CommandStruct* command, char symbol, int start) return -1; } - // Checking redirection // TODO: Work on one symbol at a time // inputString may not be useful here, only *command @@ -133,6 +115,16 @@ void CheckRedirection(CommandStruct* command) } return; } + +int CountSymbol(CommandStruct* command, char symbol) +{ + int count = 0; + for (int i = 0; i < command->argc; i++) + if (command->argv[i][0] == symbol) + count++; + return count; +} + void ioRedirection(CommandStruct* command) { for (int i = 0; i argc; - while (pipeLocation >= 0) + for (int i = 0; i < pipeAmount; i++) { - if (pipeLocation > 0) - startArgc = pipeLocation + 1; + + // if (pipeLocation > 0) + // startArgc = pipeLocation + 1; pipeLocation = CheckSymbol(commandParent, '|', startArgc); - if (pipeLocation >= 0) - endArgc = pipeLocation - 1; - if (pipeLocation < 0) - endArgc = commandParent->argc; + // if (pipeLocation >= 0) + endArgc = pipeLocation; + // if (pipeLocation < 0) + // endArgc = commandParent->argc; CopyCommandStruct(commandParent, commandChild, startArgc, endArgc); pipe(pipefd); forkPID = fork(); if (forkPID == 0) return forkPID; - if (forkPID != 0) - wait(&forkPID); + // if (forkPID != 0) + wait(&forkPID); close(pipefd[1]); while (read(pipefd[0], buffer, sizeof(buffer)) != 0) printf("%s", buffer); + startArgc = pipeLocation + 1; ResetCommandStruct(commandChild); // Clean command } + endArgc = commandParent->argc; + forkPID = fork(); + CopyCommandStruct(commandParent, commandChild, startArgc, endArgc); + if (forkPID == 0) + return forkPID; + wait(&forkPID); + ResetCommandStruct(commandChild); // Clean command return 1; } diff --git a/src/Pish.c b/src/Pish.c index fa4f055..fad10b0 100644 --- a/src/Pish.c +++ b/src/Pish.c @@ -77,4 +77,17 @@ void ReadPishrc(CommandStruct *commandParent, CommandStruct *commandChild, char strcat(inputString, buffer); } assert(close(fd) >= 0); -} \ No newline at end of file +} + +// Run a child process +void RunChild(CommandStruct* commandChild, int* pipefd) +{ + close(pipefd[0]); + dup2(pipefd[1], 1); + close(pipefd[1]); + // execve() is recommended, execvpe() may be better + execvp(commandChild->argv[0], commandChild->argv); + // exec does not return if it succeeds + printf("ERROR: Could not execute %s\n", commandChild->argv[0]); + exit(1); +}