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,37 +95,62 @@ 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 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) void ioRedirection(CommandStruct *command)
{ {
int newfd; for (int i = 0; i <sizeof(command); i++)
// check command standard output {
// TODO: Check command->argv[] for symbol instead if (strpbrk(command->argv[i], ">>")!= NULL)//append
for(int i = 0; i < sizeof(command); i++) {
{ append(command->argv[i-1], command->argv[i+1]);
if (strchr(command->argv[i], '>') != NULL) }
{ else if (strchr(command->argv[i], '<')!= NULL)//input
output(command->argv[i+1]); {
// Check if targeting a specific string in command->argv works input(command->argv[i+1]);
} }
if (strchr(command->argv[i], '<') != NULL) else if (strchr(command->argv[i],'|')!= NULL)//pipe
{ {
input(command->argv[i+1]); //ExecPipe();
} }
if (strchr(command->argv[i], '|')!= NULL) else if (strchr(command->argv[i], '>')!= NULL)//output
{ {
// not sure if i + 1 correct output(command->argv[i+1]);
// pipe(command->argv[i+1]); }
} else{}
if (strpbrk(command->argv[i], ">>")!= NULL) //exit
{ }
append(command->argv[i-1], command->argv[i+1]);
}
}
} }
void output(char *command){ void output(char *command){
@ -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,8 +20,10 @@ 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);
if (IntegratedCheck(command)) CheckRedirection(inputString, command);
ioRedirection(command);
if (IntegratedCheck(command))
continue; continue;
forkPID = fork(); forkPID = fork();
// Child // Child