Fixed pish function so it can endlessly take input. Still getting random core dumps on .pishrc reading

This commit is contained in:
TriantaTV 2022-10-27 04:37:16 -05:00
parent a8420ec3f8
commit bd66bb0189
3 changed files with 17 additions and 18 deletions

View File

@ -4,7 +4,7 @@
#include "Common.h"
const char* GetHomeDir(void);
void IntegratedCheck(char* command);
void IntegratedCheck(CommandStruct* command);
void ReadPishrc(CommandStruct* command, char* inputString);
#endif

View File

@ -20,13 +20,13 @@ const char* GetHomeDir(void)
// Checks for commands that are built into Pish
void IntegratedCheck(char* command)
void IntegratedCheck(CommandStruct* command)
{
if (command == "exit")
if (strcmp(command->argv[0], "exit") == 0)
exit(0);
// If there is an argument, change to argument location
// Else, change to home directory
if (command[0] == 'c' && command[1] == 'd')
if (command->argv[0][0] == 'c' && command->argv[0][1] == 'd')
;
return;
}
@ -34,21 +34,20 @@ void IntegratedCheck(char* command)
// Reads ~/.pishrc and runs each command in the file
void ReadPishrc(CommandStruct* command, char* inputString)
{
char buffer;
char buffer[1];
int forkID;
int* argc = &(command->argc);
int fd = open(GetHomeDir(), O_RDONLY | O_CREAT);
assert(fd > -1);
while (read(fd, &buffer, 1) > 0)
while (read(fd, buffer, 1) > 0)
{
printf("%c", buffer);
if (buffer == ' ')
if (buffer[0] == ' ')
{
command->argv[*argc] = inputString;
strcpy(inputString, "");
*argc++;
}
if (buffer == '\n')
if (buffer[0] == '\n')
{
command->argv[*argc] = inputString;
strcpy(inputString, "");
@ -58,7 +57,7 @@ void ReadPishrc(CommandStruct* command, char* inputString)
wait(&forkID);
ResetCommandStruct(command);
}
strcat(inputString, &buffer);
strcat(inputString, buffer);
}
assert(close(fd) >= 0);
}

View File

@ -1,3 +1,4 @@
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
@ -17,10 +18,10 @@ void Pish(void)
while (1)
{
ResetCommandStruct(command); // Clean command
memset(inputString, 0, sizeof(inputString)); // Clean inputString
strcpy(inputString, ""); // Clean inputString
GetInput(inputString);
ParseInput(inputString, command);
IntegratedCheck(command->argv[0]);
IntegratedCheck(command);
forkPID = fork();
// Child
if (forkPID == 0)
@ -36,21 +37,20 @@ void Pish(void)
}
// Parent
// This is the parent process; Wait for the child to finish
// Removed due to potentially useless
// int pid = forkPID;
// wait(&pid);
wait(&forkPID);
// TODO: Remove break when while loop doesn't break program
// break;
}
}
// Prints a prompt and then reads a command from the terminal
void GetInput(char* inputString)
{
char c;
char* prompt = "pish%>";
printf("%s ", prompt);
scanf("%[^\n]", inputString);
// assert(scanf("%[^\n]s", inputString) == 1);
scanf("%1000[^\n]s", inputString);
while((c = getchar()) != '\n' && c != EOF)
/* discard */ ;
return;
}