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"
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

View File

@ -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)
for (int i = 0; i < command->argc; i++)
{
ParseInput(inputString, command, ">>");
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, " ");
}
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, " ");
}
// }
return;
}
void ioRedirection(CommandStruct *command)
void ioRedirection(CommandStruct* command)
{
for (int i = 0; i <sizeof(command); i++)
{

View File

@ -20,11 +20,12 @@ void Pish(void)
ResetCommandStruct(command); // Clean command
strcpy(inputString, ""); // Clean inputString
GetInput(inputString);
// ParseInput(inputString, command);
CheckRedirection(inputString, command);
ioRedirection(command);
SplitInput(inputString, command);
if (IntegratedCheck(command))
continue;
// CheckRedirection(inputString, command);
CheckRedirection(command);
// ioRedirection(command);
forkPID = fork();
// Child
if (forkPID == 0)