Pish now runs, Redirection only prints, doesn't affect output

This commit is contained in:
TriantaTV 2022-10-29 01:10:12 -05:00
parent bef7952abf
commit 4b3f4c1433
4 changed files with 48 additions and 32 deletions

0
bin/.keep Normal file
View File

View File

@ -4,14 +4,15 @@
#include "Common.h" #include "Common.h"
const char* GetHomeDir(void); const char* GetHomeDir(void);
void CheckRedirection(char *inputString, CommandStruct *command); void CheckRedirection(CommandStruct* command);
int IntegratedCheck(CommandStruct* command); int IntegratedCheck(CommandStruct* command);
void GetInput(char* inputString); void GetInput(char* inputString);
void append(char *input, char *file); void append(char *input, char *file);
void input(char *command); void input(char *command);
void ExecPipe(); void ExecPipe();
void output(char *command); void output(char *command);
void SplitInput(char* inputString, CommandStruct* command);
void ParseInput(char* inputString, CommandStruct* command, char *symbol); void ParseInput(char* inputString, CommandStruct* command, char *symbol);
void ReadPishrc(CommandStruct* command, char* inputString); void ReadPishrc(CommandStruct* command, char* inputString);
void ioRedirection(CommandStruct *command); void ioRedirection(CommandStruct* command);
#endif #endif

View File

@ -43,7 +43,7 @@ void GetInput(char *inputString)
printf("%s ", prompt); printf("%s ", prompt);
// assert(scanf("%[^\n]s", inputString) == 1); // assert(scanf("%[^\n]s", inputString) == 1);
scanf("%1000[^\n]s", inputString); scanf("%1000[^\n]s", inputString);
while ((c = getchar()) != '\n' && c != EOF) while ((c = getchar()) != '\n' && c != EOF) // Cleans input
/* discard */; /* discard */;
return; return;
} }
@ -95,40 +95,54 @@ void ReadPishrc(CommandStruct *command, char *inputString)
assert(close(fd) >= 0); 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 // 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) void CheckRedirection(CommandStruct* command)
{ {
// 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(inputString); i++) for (int i = 0; i < command->argc; i++)
// { {
if (strpbrk(inputString, ">>") != NULL) if ((command->argv[i][0] == '>') && (command->argv[i][1] == '>'))
{ printf(">> was found.\n");
ParseInput(inputString, command, ">>"); // ParseInput(inputString, command, ">>");
} if (command->argv[i][0] == '<')
else if (strchr(inputString, '<') != NULL) printf("< was found.\n");
{ // ParseInput(inputString, command, "<");
ParseInput(inputString, command, "<"); if (command->argv[i][0] == '>')
} printf("> was found.\n");
else if (strchr(inputString, '|')!= NULL) // ParseInput(inputString, command, ">");
{ if (command->argv[i][0] == '|')
ParseInput(inputString, command, "|"); printf("| was found.\n");
} // ParseInput(inputString, command, "|");
else if (strchr(inputString, '>')!= NULL) // else
{ // ParseInput(inputString, command, " ");
ParseInput(inputString, command, ">"); }
} return;
else
{
ParseInput(inputString, command, " ");
}
// }
} }
void ioRedirection(CommandStruct *command) void ioRedirection(CommandStruct* command)
{ {
for (int i = 0; i <sizeof(command); i++) for (int i = 0; i <sizeof(command); i++)
{ {

View File

@ -20,11 +20,12 @@ 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); SplitInput(inputString, command);
CheckRedirection(inputString, command); if (IntegratedCheck(command))
ioRedirection(command);
if (IntegratedCheck(command))
continue; continue;
// CheckRedirection(inputString, command);
CheckRedirection(command);
// ioRedirection(command);
forkPID = fork(); forkPID = fork();
// Child // Child
if (forkPID == 0) if (forkPID == 0)