fixed parcing and ioredirection

This commit is contained in:
Samantha Boyer 2022-10-28 23:00:52 -05:00
parent e0905dbaf2
commit b6d7f3ff3e
2 changed files with 59 additions and 32 deletions

View File

@ -49,18 +49,18 @@ void GetInput(char *inputString)
} }
// Splits a string separated by spaces into an array of strings // 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 // Parse command
int *argc = &(command->argc); int *argc = &(command->argc);
char *token = strtok(inputString, " "); char *token = strtok(inputString, symbol);//change
command->argv[*argc] = token; command->argv[*argc] = token;
(*argc)++; (*argc)++;
while (token != NULL) while (token != NULL)
{ {
// This only holds 1 command at a time then it overrides // This only holds 1 command at a time then it overrides
// Will need modified // Will need modified
token = strtok(NULL, " "); token = strtok(NULL, symbol);//change
command->argv[*argc] = token; command->argv[*argc] = token;
(*argc)++; (*argc)++;
} }
@ -80,7 +80,7 @@ void ReadPishrc(CommandStruct *command, char *inputString)
{ {
if (buffer[0] == '\n') if (buffer[0] == '\n')
{ {
ParseInput(inputString, command); ParseInput(inputString, command, " ");
forkID = fork(); forkID = fork();
if (forkID == 0) if (forkID == 0)
execvp(command->argv[0], command->argv); execvp(command->argv[0], command->argv);
@ -95,36 +95,61 @@ void ReadPishrc(CommandStruct *command, char *inputString)
assert(close(fd) >= 0); assert(close(fd) >= 0);
} }
// i/o redirection // Checking redirection
// TODO: Work on one symbol at a time // TODO: Work on one symbol at a time
// inputString may not be useful here, only *command // inputString may not be useful here, only *command
// Try to add a layer of abstraction where possible // Try to add a layer of abstraction where possible
// I.E. think about using function instead for checking // I.E. think about using function instead for checking
void ioRedirection(CommandStruct *command) void CheckRedirection(char *inputString, CommandStruct *command)
{ {
int newfd;
// check command standard output // check command standard output
// TODO: Check command->argv[] for symbol instead // TODO: Check command->argv[] for symbol instead
for(int i = 0; i < sizeof(command); i++) // for(int i = 0; i < sizeof(inputString); i++)
// {
if (strpbrk(inputString, ">>") != NULL)
{ {
if (strchr(command->argv[i], '>') != NULL) ParseInput(inputString, command, ">>");
{
output(command->argv[i+1]);
// Check if targeting a specific string in command->argv works
} }
if (strchr(command->argv[i], '<') != NULL) else if (strchr(inputString, '<') != NULL)
{ {
input(command->argv[i+1]); ParseInput(inputString, command, "<");
} }
if (strchr(command->argv[i], '|')!= NULL) else if (strchr(inputString, '|')!= NULL)
{ {
// not sure if i + 1 correct ParseInput(inputString, command, "|");
// pipe(command->argv[i+1]);
} }
if (strpbrk(command->argv[i], ">>")!= NULL) else if (strchr(inputString, '>')!= NULL)
{
ParseInput(inputString, command, ">");
}
else
{
ParseInput(inputString, command, " ");
}
// }
}
void ioRedirection(CommandStruct *command)
{
for (int i = 0; i <sizeof(command); i++)
{
if (strpbrk(command->argv[i], ">>")!= NULL)//append
{ {
append(command->argv[i-1], command->argv[i+1]); 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
} }
} }
@ -151,7 +176,7 @@ void input(char *command)
void ExecPipe() void ExecPipe()
{ {
; // ;
} }
void append(char *input, char *file) void append(char *input, char *file)

View File

@ -20,7 +20,9 @@ void Pish(void)
ResetCommandStruct(command); // Clean command ResetCommandStruct(command); // Clean command
strcpy(inputString, ""); // Clean inputString strcpy(inputString, ""); // Clean inputString
GetInput(inputString); GetInput(inputString);
ParseInput(inputString, command); // ParseInput(inputString, command);
CheckRedirection(inputString, command);
ioRedirection(command);
if (IntegratedCheck(command)) if (IntegratedCheck(command))
continue; continue;
forkPID = fork(); forkPID = fork();