From b6d7f3ff3e86063d7f463019faba797e84c327c3 Mon Sep 17 00:00:00 2001 From: Samantha Boyer Date: Fri, 28 Oct 2022 23:00:52 -0500 Subject: [PATCH 1/9] fixed parcing and ioredirection --- src/Integrated.c | 85 +++++++++++++++++++++++++++++++----------------- src/Pish.c | 6 ++-- 2 files changed, 59 insertions(+), 32 deletions(-) diff --git a/src/Integrated.c b/src/Integrated.c index 3722651..3fe09fa 100755 --- a/src/Integrated.c +++ b/src/Integrated.c @@ -49,18 +49,18 @@ void GetInput(char *inputString) } // Splits a string separated by spaces into an array of strings -void ParseInput(char *inputString, CommandStruct *command) +void ParseInput(char *inputString, CommandStruct *command, char *symbol) { // Parse command int *argc = &(command->argc); - char *token = strtok(inputString, " "); + char *token = strtok(inputString, symbol);//change 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, " "); + token = strtok(NULL, symbol);//change command->argv[*argc] = token; (*argc)++; } @@ -80,7 +80,7 @@ void ReadPishrc(CommandStruct *command, char *inputString) { if (buffer[0] == '\n') { - ParseInput(inputString, command); + ParseInput(inputString, command, " "); forkID = fork(); if (forkID == 0) execvp(command->argv[0], command->argv); @@ -95,37 +95,62 @@ void ReadPishrc(CommandStruct *command, char *inputString) assert(close(fd) >= 0); } -// i/o redirection +// Checking redirection // TODO: Work on one symbol at a time // inputString may not be useful here, only *command // Try to add a layer of abstraction where possible // I.E. think about using function instead for checking +void CheckRedirection(char *inputString, CommandStruct *command) +{ + // check command standard output + // TODO: Check command->argv[] for symbol instead + // for(int i = 0; i < sizeof(inputString); i++) + // { + if (strpbrk(inputString, ">>") != NULL) + { + ParseInput(inputString, command, ">>"); + } + else if (strchr(inputString, '<') != NULL) + { + ParseInput(inputString, command, "<"); + } + else if (strchr(inputString, '|')!= NULL) + { + ParseInput(inputString, command, "|"); + } + else if (strchr(inputString, '>')!= NULL) + { + ParseInput(inputString, command, ">"); + } + else + { + ParseInput(inputString, command, " "); + } + // } +} void ioRedirection(CommandStruct *command) { - int newfd; - // check command standard output - // TODO: Check command->argv[] for symbol instead - for(int i = 0; i < sizeof(command); i++) - { - if (strchr(command->argv[i], '>') != NULL) - { - output(command->argv[i+1]); - // Check if targeting a specific string in command->argv works - } - if (strchr(command->argv[i], '<') != NULL) - { - input(command->argv[i+1]); - } - if (strchr(command->argv[i], '|')!= NULL) - { - // not sure if i + 1 correct - // pipe(command->argv[i+1]); - } - if (strpbrk(command->argv[i], ">>")!= NULL) - { - append(command->argv[i-1], command->argv[i+1]); - } - } + for (int i = 0; i argv[i], ">>")!= NULL)//append + { + append(command->argv[i-1], command->argv[i+1]); + } + else if (strchr(command->argv[i], '<')!= NULL)//input + { + input(command->argv[i+1]); + } + else if (strchr(command->argv[i],'|')!= NULL)//pipe + { + //ExecPipe(); + } + else if (strchr(command->argv[i], '>')!= NULL)//output + { + output(command->argv[i+1]); + } + else{} + //exit + } } void output(char *command){ @@ -151,7 +176,7 @@ void input(char *command) void ExecPipe() { - ; + // ; } void append(char *input, char *file) diff --git a/src/Pish.c b/src/Pish.c index a4d370d..40902de 100644 --- a/src/Pish.c +++ b/src/Pish.c @@ -20,8 +20,10 @@ void Pish(void) ResetCommandStruct(command); // Clean command strcpy(inputString, ""); // Clean inputString GetInput(inputString); - ParseInput(inputString, command); - if (IntegratedCheck(command)) + // ParseInput(inputString, command); + CheckRedirection(inputString, command); + ioRedirection(command); + if (IntegratedCheck(command)) continue; forkPID = fork(); // Child From bef7952abf567a25be776618d17f4e0844e6bc83 Mon Sep 17 00:00:00 2001 From: Samantha Boyer Date: Fri, 28 Oct 2022 23:01:31 -0500 Subject: [PATCH 2/9] add h files --- include/Integrated.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/include/Integrated.h b/include/Integrated.h index ecfd85f..136d418 100755 --- a/include/Integrated.h +++ b/include/Integrated.h @@ -4,14 +4,14 @@ #include "Common.h" const char* GetHomeDir(void); -void ioRedirection(CommandStruct *command); +void CheckRedirection(char *inputString, CommandStruct *command); int IntegratedCheck(CommandStruct* command); void GetInput(char* inputString); void append(char *input, char *file); void input(char *command); void ExecPipe(); void output(char *command); -void ParseInput(char* inputString, CommandStruct* command); +void ParseInput(char* inputString, CommandStruct* command, char *symbol); void ReadPishrc(CommandStruct* command, char* inputString); - -#endif \ No newline at end of file +void ioRedirection(CommandStruct *command); +#endif From 4b3f4c14330570d2869c4b3026c47a64e552438f Mon Sep 17 00:00:00 2001 From: TriantaTV Date: Sat, 29 Oct 2022 01:10:12 -0500 Subject: [PATCH 3/9] Pish now runs, Redirection only prints, doesn't affect output --- bin/.keep | 0 include/Integrated.h | 5 ++-- src/Integrated.c | 66 +++++++++++++++++++++++++++----------------- src/Pish.c | 9 +++--- 4 files changed, 48 insertions(+), 32 deletions(-) create mode 100644 bin/.keep diff --git a/bin/.keep b/bin/.keep new file mode 100644 index 0000000..e69de29 diff --git a/include/Integrated.h b/include/Integrated.h index 136d418..ca25509 100755 --- a/include/Integrated.h +++ b/include/Integrated.h @@ -4,14 +4,15 @@ #include "Common.h" const char* GetHomeDir(void); -void CheckRedirection(char *inputString, CommandStruct *command); +void CheckRedirection(CommandStruct* command); int IntegratedCheck(CommandStruct* command); void GetInput(char* inputString); void append(char *input, char *file); void input(char *command); void ExecPipe(); void output(char *command); +void SplitInput(char* inputString, CommandStruct* command); void ParseInput(char* inputString, CommandStruct* command, char *symbol); void ReadPishrc(CommandStruct* command, char* inputString); -void ioRedirection(CommandStruct *command); +void ioRedirection(CommandStruct* command); #endif diff --git a/src/Integrated.c b/src/Integrated.c index 3fe09fa..8e5b051 100755 --- a/src/Integrated.c +++ b/src/Integrated.c @@ -43,7 +43,7 @@ void GetInput(char *inputString) printf("%s ", prompt); // assert(scanf("%[^\n]s", inputString) == 1); scanf("%1000[^\n]s", inputString); - while ((c = getchar()) != '\n' && c != EOF) + while ((c = getchar()) != '\n' && c != EOF) // Cleans input /* discard */; return; } @@ -95,40 +95,54 @@ void ReadPishrc(CommandStruct *command, char *inputString) assert(close(fd) >= 0); } +// Splits a string separated by spaces into an array of strings +void SplitInput(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)++; + } + (*argc)--; +} + // Checking redirection // TODO: Work on one symbol at a time // inputString may not be useful here, only *command // Try to add a layer of abstraction where possible // I.E. think about using function instead for checking -void CheckRedirection(char *inputString, CommandStruct *command) +void CheckRedirection(CommandStruct* command) { // check command standard output // TODO: Check command->argv[] for symbol instead - // for(int i = 0; i < sizeof(inputString); i++) - // { - if (strpbrk(inputString, ">>") != NULL) - { - ParseInput(inputString, command, ">>"); - } - else if (strchr(inputString, '<') != NULL) - { - ParseInput(inputString, command, "<"); - } - else if (strchr(inputString, '|')!= NULL) - { - ParseInput(inputString, command, "|"); - } - else if (strchr(inputString, '>')!= NULL) - { - ParseInput(inputString, command, ">"); - } - else - { - ParseInput(inputString, command, " "); - } - // } + for (int i = 0; i < command->argc; i++) + { + if ((command->argv[i][0] == '>') && (command->argv[i][1] == '>')) + printf(">> was found.\n"); + // ParseInput(inputString, command, ">>"); + if (command->argv[i][0] == '<') + printf("< was found.\n"); + // ParseInput(inputString, command, "<"); + if (command->argv[i][0] == '>') + printf("> was found.\n"); + // ParseInput(inputString, command, ">"); + if (command->argv[i][0] == '|') + printf("| was found.\n"); + // ParseInput(inputString, command, "|"); + // else + // ParseInput(inputString, command, " "); + } + return; } -void ioRedirection(CommandStruct *command) +void ioRedirection(CommandStruct* command) { for (int i = 0; i Date: Sat, 29 Oct 2022 05:14:23 -0500 Subject: [PATCH 4/9] Big commit: Setup outline for piping, piping has errors still, cleaned code --- include/Common.h | 3 ++ include/Integrated.h | 8 ++-- include/Pish.h | 1 + src/Common.c | 21 +++++++++ src/Integrated.c | 103 +++++++++++++++++++++++++------------------ src/Pish.c | 72 ++++++++++++++++++++++-------- 6 files changed, 143 insertions(+), 65 deletions(-) diff --git a/include/Common.h b/include/Common.h index 25ac07a..7120cfc 100755 --- a/include/Common.h +++ b/include/Common.h @@ -1,11 +1,14 @@ #ifndef COMMON_H #define COMMON_H +const char* GetHomeDir(void); + typedef struct CommandStruct { char* argv[100]; // Max 100 commands int argc; } CommandStruct; +void CopyCommandStruct(CommandStruct* oldCommand, CommandStruct* newCommand, int start, int end); void ResetCommandStruct(CommandStruct* command); #endif \ No newline at end of file diff --git a/include/Integrated.h b/include/Integrated.h index ca25509..b8433e9 100755 --- a/include/Integrated.h +++ b/include/Integrated.h @@ -3,16 +3,16 @@ #include "Common.h" -const char* GetHomeDir(void); void CheckRedirection(CommandStruct* command); +int CheckSymbol(CommandStruct* command, char symbol, int start); int IntegratedCheck(CommandStruct* command); void GetInput(char* inputString); void append(char *input, char *file); void input(char *command); -void ExecPipe(); +int ExecPipe(CommandStruct* commandParent, CommandStruct* commandChild, int* pipefd); void output(char *command); void SplitInput(char* inputString, CommandStruct* command); -void ParseInput(char* inputString, CommandStruct* command, char *symbol); -void ReadPishrc(CommandStruct* command, char* inputString); +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 9b54a1a..e093cd1 100644 --- a/include/Pish.h +++ b/include/Pish.h @@ -4,5 +4,6 @@ #include "Common.h" void Pish(void); +void ReadPishrc(CommandStruct* commandParent, CommandStruct *commandChild, char* inputString); #endif diff --git a/src/Common.c b/src/Common.c index 1f230a8..74b79a3 100644 --- a/src/Common.c +++ b/src/Common.c @@ -1,7 +1,28 @@ #include "Common.h" +#include #include #include +#include +#include + +// Gets home directory location of user +const char* GetHomeDir(void) +{ + char *homeDir = (getpwuid(getuid()))->pw_dir; + return homeDir; +} + +// Copies a range of an old command object to new command object +void CopyCommandStruct(CommandStruct* oldCommand, CommandStruct* newCommand, int start, int end) +{ + newCommand->argc = end - start; + for (int i = 0; i < newCommand->argc; i++) + newCommand->argv[i] = oldCommand->argv[i + start]; + return; +} + +// Resets entire command object void ResetCommandStruct(CommandStruct* command) { for (int i = 0; i < command->argc; i++) diff --git a/src/Integrated.c b/src/Integrated.c index 8e5b051..c39bcfc 100755 --- a/src/Integrated.c +++ b/src/Integrated.c @@ -1,22 +1,13 @@ #include #include -#include #include #include #include #include #include -#include #include #include "Integrated.h" -// Gets home directory location of user -const char *GetHomeDir(void) -{ - char *homeDir = (getpwuid(getuid()))->pw_dir; - return homeDir; -} - // Checks for commands that are built into Pish int IntegratedCheck(CommandStruct *command) { @@ -66,35 +57,23 @@ void ParseInput(char *inputString, CommandStruct *command, char *symbol) } } -// Reads ~/.pishrc and runs each command in the file -void ReadPishrc(CommandStruct *command, char *inputString) +// Run child process +void RunChild(CommandStruct* commandChild, int* pipefd) { - char buffer[1]; - int forkID; - int *argc = &(command->argc); - char* pishLocation = GetHomeDir(); - strcat(pishLocation, "/.pishrc"); - int fd = open(pishLocation, O_RDONLY | O_CREAT); - assert(fd > -1); - while (read(fd, buffer, 1) > 0) - { - if (buffer[0] == '\n') - { - ParseInput(inputString, command, " "); - forkID = fork(); - if (forkID == 0) - execvp(command->argv[0], command->argv); - else - wait(&forkID); - strcpy(inputString, ""); - ResetCommandStruct(command); - continue; - } - strcat(inputString, buffer); - } - assert(close(fd) >= 0); + 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) { @@ -114,6 +93,18 @@ void SplitInput(char *inputString, CommandStruct *command) (*argc)--; } +// Splits a command array into left and right +// void SplitPipe(CommandStruct* commandLeft, CommandStruct* commandRight); + +int CheckSymbol(CommandStruct* command, char symbol, int start) +{ + for (int i = start; i < command->argc; i++) + if (command->argv[i][0] == symbol) + return i; + return -1; +} + + // Checking redirection // TODO: Work on one symbol at a time // inputString may not be useful here, only *command @@ -188,17 +179,45 @@ void input(char *command) dup2(newfd,0); } -void ExecPipe() +int ExecPipe(CommandStruct* commandParent, CommandStruct* commandChild, int* pipefd) { - // ; + char buffer[1024]; + int forkPID; + int pipeLocation = 0; + int startArgc = 0; + int endArgc = commandParent->argc; + while (pipeLocation >= 0) + { + if (pipeLocation > 0) + startArgc = pipeLocation + 1; + pipeLocation = CheckSymbol(commandParent, '|', startArgc); + if (pipeLocation >= 0) + endArgc = pipeLocation - 1; + 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); + close(pipefd[1]); + while (read(pipefd[0], buffer, sizeof(buffer)) != 0) + printf("%s", buffer); + ResetCommandStruct(commandChild); // Clean command + } + return 1; } -void append(char *input, char *file) +void append(char* input, char* fileName) { - FILE *fp; - fp = fopen(file, "a+"); - fputs(input, fp); - fclose(fp); + char buffer[4]; + int fd = open(fileName, O_APPEND | O_CREAT); + + // fp = fopen(file, "a+"); + // fputs(input, fp); + // fclose(fp); } // check command standard input diff --git a/src/Pish.c b/src/Pish.c index 477fbf3..fa4f055 100644 --- a/src/Pish.c +++ b/src/Pish.c @@ -1,46 +1,80 @@ #include +#include #include #include #include #include #include +#include "Common.h" #include "Integrated.h" #include "Pish.h" +// Example command: ps aux | grep 'Z' +// Pipe left: [ps aux] +// argv: [ps, aux] +// Pipe right: [grep 'Z'] +// argv: [grep, 'Z'] + +// check for first pipe, split into before and after pipe, +// connect left and right execs + // Main function for Pish program void Pish(void) { - CommandStruct commandObject = {"", 0}; - CommandStruct* command = &commandObject; + CommandStruct commandParentObject = {"", 0}; + CommandStruct commandChildObject = {"", 0}; + CommandStruct* commandParent = &commandParentObject; + CommandStruct* commandChild = &commandChildObject; char inputString[1000]; // Max 1000 characters int forkPID; - ReadPishrc(command, inputString); + ReadPishrc(commandParent, commandChild, inputString); while (1) { - ResetCommandStruct(command); // Clean command + int pipefd[2]; + ResetCommandStruct(commandParent); // Clean command strcpy(inputString, ""); // Clean inputString GetInput(inputString); - SplitInput(inputString, command); - if (IntegratedCheck(command)) + SplitInput(inputString, commandParent); + if (IntegratedCheck(commandParent)) continue; + forkPID = ExecPipe(commandParent, commandChild, pipefd); // CheckRedirection(inputString, command); - CheckRedirection(command); + // CheckRedirection(command); // ioRedirection(command); - 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? - // 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); - } + RunChild(commandChild, pipefd); // Parent // This is the parent process; Wait for the child to finish - wait(&forkPID); } } + +// Reads ~/.pishrc and runs each command in the file +void ReadPishrc(CommandStruct *commandParent, CommandStruct *commandChild, char *inputString) +{ + char buffer[1]; + int pipefd[2]; + int forkPID; + int *argc = &(commandParent->argc); + char* pishLocation = GetHomeDir(); + strcat(pishLocation, "/.pishrc"); + int fd = open(pishLocation, O_RDONLY | O_CREAT); + assert(fd > -1); + while (read(fd, buffer, 1) > 0) + { + if (buffer[0] == '\n') + { + SplitInput(inputString, commandParent); + if (IntegratedCheck(commandParent)) + continue; + forkPID = ExecPipe(commandParent, commandChild, pipefd); + if (forkPID == 0) + RunChild(commandChild, pipefd); + strcpy(inputString, ""); + ResetCommandStruct(commandParent); + continue; + } + strcat(inputString, buffer); + } + assert(close(fd) >= 0); +} \ No newline at end of file From a0d601f1e35f4a9a92b1eac1cb31f77f96f38977 Mon Sep 17 00:00:00 2001 From: TriantaTV Date: Sat, 29 Oct 2022 05:37:29 -0500 Subject: [PATCH 5/9] 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); +} From 8bd2940f1ae6eebca22229db6fbb34be876ccef7 Mon Sep 17 00:00:00 2001 From: TriantaTV Date: Sat, 29 Oct 2022 20:29:28 -0500 Subject: [PATCH 6/9] Fixed crashing on no input --- src/Integrated.c | 12 ++++++++---- src/Pish.c | 2 ++ 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/Integrated.c b/src/Integrated.c index 0fa1ff0..38dd88c 100755 --- a/src/Integrated.c +++ b/src/Integrated.c @@ -180,6 +180,7 @@ int ExecPipe(CommandStruct* commandParent, CommandStruct* commandChild, int* pip int pipeLocation = 0; int startArgc = 0; int endArgc = commandParent->argc; + pipe(pipefd); for (int i = 0; i < pipeAmount; i++) { @@ -191,24 +192,27 @@ int ExecPipe(CommandStruct* commandParent, CommandStruct* commandChild, int* pip // 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); + close(pipefd[0]); close(pipefd[1]); - while (read(pipefd[0], buffer, sizeof(buffer)) != 0) - printf("%s", buffer); + wait(&forkPID); startArgc = pipeLocation + 1; ResetCommandStruct(commandChild); // Clean command + pipe(pipefd); } endArgc = commandParent->argc; forkPID = fork(); CopyCommandStruct(commandParent, commandChild, startArgc, endArgc); if (forkPID == 0) return forkPID; + close(pipefd[1]); wait(&forkPID); + while (read(pipefd[0], buffer, sizeof(buffer)) != 0) + printf("%s", buffer); + close(pipefd[0]); ResetCommandStruct(commandChild); // Clean command return 1; } diff --git a/src/Pish.c b/src/Pish.c index fad10b0..26bcf70 100644 --- a/src/Pish.c +++ b/src/Pish.c @@ -34,6 +34,8 @@ void Pish(void) ResetCommandStruct(commandParent); // Clean command strcpy(inputString, ""); // Clean inputString GetInput(inputString); + if (strcmp(inputString, "") == 0) + continue; SplitInput(inputString, commandParent); if (IntegratedCheck(commandParent)) continue; From d8e729fdd6cc1b20cb2ab6b4d07dcaad0a87328d Mon Sep 17 00:00:00 2001 From: TriantaTV Date: Sat, 29 Oct 2022 21:21:13 -0500 Subject: [PATCH 7/9] Added signal handling and fixed permissions with pishrc --- src/Pish.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Pish.c b/src/Pish.c index 26bcf70..34fce97 100644 --- a/src/Pish.c +++ b/src/Pish.c @@ -1,5 +1,6 @@ #include #include +#include #include #include #include @@ -27,6 +28,9 @@ void Pish(void) CommandStruct* commandChild = &commandChildObject; char inputString[1000]; // Max 1000 characters int forkPID; + signal(SIGINT, SIG_IGN); + signal(SIGQUIT, SIG_IGN); + signal(SIGTERM, SIG_IGN); ReadPishrc(commandParent, commandChild, inputString); while (1) { @@ -60,7 +64,7 @@ void ReadPishrc(CommandStruct *commandParent, CommandStruct *commandChild, char int *argc = &(commandParent->argc); char* pishLocation = GetHomeDir(); strcat(pishLocation, "/.pishrc"); - int fd = open(pishLocation, O_RDONLY | O_CREAT); + int fd = open(pishLocation, O_RDONLY | O_CREAT, 0644); assert(fd > -1); while (read(fd, buffer, 1) > 0) { From 0062cd1cc55243eefc0d021cec21c113cc800b66 Mon Sep 17 00:00:00 2001 From: Samantha Boyer Date: Sat, 29 Oct 2022 22:55:36 -0500 Subject: [PATCH 8/9] added EV , fixed input and output --- include/Common.h | 3 ++- src/Integrated.c | 13 ++++++------- src/Pish.c | 2 ++ 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/include/Common.h b/include/Common.h index 7120cfc..bda50d1 100755 --- a/include/Common.h +++ b/include/Common.h @@ -6,9 +6,10 @@ const char* GetHomeDir(void); typedef struct CommandStruct { char* argv[100]; // Max 100 commands int argc; + char* envp[20]; // Enviroment Varibles } CommandStruct; void CopyCommandStruct(CommandStruct* oldCommand, CommandStruct* newCommand, int start, int end); void ResetCommandStruct(CommandStruct* command); -#endif \ No newline at end of file +#endif diff --git a/src/Integrated.c b/src/Integrated.c index 0fa1ff0..f72b4ff 100755 --- a/src/Integrated.c +++ b/src/Integrated.c @@ -98,13 +98,12 @@ void CheckRedirection(CommandStruct* command) // TODO: Check command->argv[] for symbol instead for (int i = 0; i < command->argc; i++) { - if ((command->argv[i][0] == '>') && (command->argv[i][1] == '>')) - printf(">> was found.\n"); + if ((command->argv[i][0] == '>') && (command->argv[i][1] == '>')) printf(">> was found.\n"); // ParseInput(inputString, command, ">>"); if (command->argv[i][0] == '<') printf("< was found.\n"); // ParseInput(inputString, command, "<"); - if (command->argv[i][0] == '>') + if (command->argv[i][0] == '>' && command->argv[i][1] != '>') printf("> was found.\n"); // ParseInput(inputString, command, ">"); if (command->argv[i][0] == '|') @@ -133,7 +132,7 @@ void ioRedirection(CommandStruct* command) { append(command->argv[i-1], command->argv[i+1]); } - else if (strchr(command->argv[i], '<')!= NULL)//input + else if (command->argv[i][0] =='<')//input { input(command->argv[i+1]); } @@ -141,7 +140,7 @@ void ioRedirection(CommandStruct* command) { //ExecPipe(); } - else if (strchr(command->argv[i], '>')!= NULL)//output + else if (command->argv[i][0] == '>')//output { output(command->argv[i+1]); } @@ -152,7 +151,7 @@ void ioRedirection(CommandStruct* command) void output(char *command){ int newfd; - if (newfd = open(command, O_CREAT | O_TRUNC | O_WRONLY, 0644) < 0) + if (newfd = open(command, O_CREAT | O_CREAT, 0666) < 0) { perror(command); exit(1); @@ -163,7 +162,7 @@ void output(char *command){ void input(char *command) { int newfd; - if (newfd = open(command, O_RDONLY) < 0) + if (newfd = open(command, O_RDONLY | O_CREAT) < 0) { perror(command); exit(1); diff --git a/src/Pish.c b/src/Pish.c index fad10b0..2c9f7fd 100644 --- a/src/Pish.c +++ b/src/Pish.c @@ -86,6 +86,8 @@ void RunChild(CommandStruct* commandChild, int* pipefd) dup2(pipefd[1], 1); close(pipefd[1]); // execve() is recommended, execvpe() may be better + CheckRedirection(commandChild); + ioRedirection(commandChild); execvp(commandChild->argv[0], commandChild->argv); // exec does not return if it succeeds printf("ERROR: Could not execute %s\n", commandChild->argv[0]); From 01625790bdf476c84efef7b378cc04e362577038 Mon Sep 17 00:00:00 2001 From: TriantaTV Date: Sat, 29 Oct 2022 23:11:35 -0500 Subject: [PATCH 9/9] Final cleanup before submitting --- src/Integrated.c | 18 +++--------------- src/Pish.c | 11 +---------- 2 files changed, 4 insertions(+), 25 deletions(-) diff --git a/src/Integrated.c b/src/Integrated.c index 2e99e9d..c676849 100755 --- a/src/Integrated.c +++ b/src/Integrated.c @@ -60,15 +60,12 @@ void ParseInput(char *inputString, CommandStruct *command, char *symbol) // Splits a string separated by spaces into an array of strings void SplitInput(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)++; @@ -88,17 +85,14 @@ int CheckSymbol(CommandStruct* command, char symbol, int start) } // Checking redirection -// TODO: Work on one symbol at a time -// inputString may not be useful here, only *command -// Try to add a layer of abstraction where possible -// I.E. think about using function instead for checking void CheckRedirection(CommandStruct* command) { // check command standard output // TODO: Check command->argv[] for symbol instead for (int i = 0; i < command->argc; i++) { - if ((command->argv[i][0] == '>') && (command->argv[i][1] == '>')) printf(">> was found.\n"); + if ((command->argv[i][0] == '>') && (command->argv[i][1] == '>')) + printf(">> was found.\n"); // ParseInput(inputString, command, ">>"); if (command->argv[i][0] == '<') printf("< was found.\n"); @@ -115,6 +109,7 @@ void CheckRedirection(CommandStruct* command) return; } +// Count number of appearances of a symbol in a CommandStruct int CountSymbol(CommandStruct* command, char symbol) { int count = 0; @@ -182,19 +177,12 @@ int ExecPipe(CommandStruct* commandParent, CommandStruct* commandChild, int* pip pipe(pipefd); for (int i = 0; i < pipeAmount; i++) { - - // if (pipeLocation > 0) - // startArgc = pipeLocation + 1; pipeLocation = CheckSymbol(commandParent, '|', startArgc); - // if (pipeLocation >= 0) endArgc = pipeLocation; - // if (pipeLocation < 0) - // endArgc = commandParent->argc; CopyCommandStruct(commandParent, commandChild, startArgc, endArgc); forkPID = fork(); if (forkPID == 0) return forkPID; - // if (forkPID != 0) close(pipefd[0]); close(pipefd[1]); wait(&forkPID); diff --git a/src/Pish.c b/src/Pish.c index b864c1c..c65d5d9 100644 --- a/src/Pish.c +++ b/src/Pish.c @@ -10,15 +10,6 @@ #include "Integrated.h" #include "Pish.h" -// Example command: ps aux | grep 'Z' -// Pipe left: [ps aux] -// argv: [ps, aux] -// Pipe right: [grep 'Z'] -// argv: [grep, 'Z'] - -// check for first pipe, split into before and after pipe, -// connect left and right execs - // Main function for Pish program void Pish(void) { @@ -93,7 +84,7 @@ void RunChild(CommandStruct* commandChild, int* pipefd) close(pipefd[1]); // execve() is recommended, execvpe() may be better CheckRedirection(commandChild); - ioRedirection(commandChild); + // ioRedirection(commandChild); execvp(commandChild->argv[0], commandChild->argv); // exec does not return if it succeeds printf("ERROR: Could not execute %s\n", commandChild->argv[0]);