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); const char* GetHomeDir(void);
void IntegratedCheck(CommandStruct* command); void IntegratedCheck(CommandStruct* command);
void GetInput(char* inputString);
void ParseInput(char* inputString, CommandStruct* command);
void ReadPishrc(CommandStruct* command, char* inputString); void ReadPishrc(CommandStruct* command, char* inputString);
#endif #endif

View File

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

View File

@ -31,6 +31,38 @@ void IntegratedCheck(CommandStruct* command)
return; 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 // Reads ~/.pishrc and runs each command in the file
void ReadPishrc(CommandStruct* command, char* inputString) void ReadPishrc(CommandStruct* command, char* inputString)
{ {
@ -41,21 +73,17 @@ void ReadPishrc(CommandStruct* command, char* inputString)
assert(fd > -1); assert(fd > -1);
while (read(fd, buffer, 1) > 0) while (read(fd, buffer, 1) > 0)
{ {
if (buffer[0] == ' ')
{
command->argv[*argc] = inputString;
strcpy(inputString, "");
*argc++;
}
if (buffer[0] == '\n') if (buffer[0] == '\n')
{ {
command->argv[*argc] = inputString; ParseInput(inputString, command);
strcpy(inputString, "");
forkID = fork(); forkID = fork();
if (forkID == 0) if (forkID == 0)
execvp(command->argv[0], command->argv); execvp(command->argv[0], command->argv);
else
wait(&forkID); wait(&forkID);
strcpy(inputString, "");
ResetCommandStruct(command); ResetCommandStruct(command);
continue;
} }
strcat(inputString, buffer); strcat(inputString, buffer);
} }

View File

@ -40,34 +40,3 @@ void Pish(void)
wait(&forkPID); 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)++;
}
}