From b6d7f3ff3e86063d7f463019faba797e84c327c3 Mon Sep 17 00:00:00 2001 From: Samantha Boyer Date: Fri, 28 Oct 2022 23:00:52 -0500 Subject: [PATCH] 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