Final fix for .pishrc, any additional changes are for handling operators

This commit is contained in:
TriantaTV 2022-10-27 05:38:12 -05:00
parent bd66bb0189
commit 3c0daa7073
4 changed files with 40 additions and 43 deletions

View File

@ -5,6 +5,8 @@
const char* GetHomeDir(void);
void IntegratedCheck(CommandStruct* command);
void GetInput(char* inputString);
void ParseInput(char* inputString, CommandStruct* command);
void ReadPishrc(CommandStruct* command, char* inputString);
#endif

View File

@ -4,7 +4,5 @@
#include "Common.h"
void Pish(void);
void GetInput(char* inputString);
void ParseInput(char* inputString, CommandStruct* command);
#endif

View File

@ -31,6 +31,38 @@ void IntegratedCheck(CommandStruct* command)
return;
}
// Prints a prompt and then reads a command from the terminal
void GetInput(char* inputString)
{
char c;
char* prompt = "pish%>";
printf("%s ", prompt);
// assert(scanf("%[^\n]s", inputString) == 1);
scanf("%1000[^\n]s", inputString);
while((c = getchar()) != '\n' && c != EOF)
/* discard */ ;
return;
}
// Splits a string separated by spaces into an array of strings
void ParseInput(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)++;
}
}
// Reads ~/.pishrc and runs each command in the file
void ReadPishrc(CommandStruct* command, char* inputString)
{
@ -41,21 +73,17 @@ void ReadPishrc(CommandStruct* command, char* inputString)
assert(fd > -1);
while (read(fd, buffer, 1) > 0)
{
if (buffer[0] == ' ')
{
command->argv[*argc] = inputString;
strcpy(inputString, "");
*argc++;
}
if (buffer[0] == '\n')
{
command->argv[*argc] = inputString;
strcpy(inputString, "");
ParseInput(inputString, command);
forkID = fork();
if (forkID == 0)
execvp(command->argv[0], command->argv);
else
wait(&forkID);
strcpy(inputString, "");
ResetCommandStruct(command);
continue;
}
strcat(inputString, buffer);
}

View File

@ -40,34 +40,3 @@ void Pish(void)
wait(&forkPID);
}
}
// Prints a prompt and then reads a command from the terminal
void GetInput(char* inputString)
{
char c;
char* prompt = "pish%>";
printf("%s ", prompt);
// assert(scanf("%[^\n]s", inputString) == 1);
scanf("%1000[^\n]s", inputString);
while((c = getchar()) != '\n' && c != EOF)
/* discard */ ;
return;
}
// Splits a string separated by spaces into an array of strings
void ParseInput(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)++;
}
}