pish/src/Pish.c

81 lines
2.0 KiB
C
Raw Normal View History

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
2022-10-22 20:47:03 -05:00
#include <string.h>
#include "Integrated.h"
#include "Pish.h"
// Main function for Pish program
void Pish(void)
{
CommandStruct commandObject = {"", 0};
CommandStruct* command = &commandObject;
char inputString[1000]; // Max 1000 characters
int forkPID;
while (1)
{
GetInput(inputString);
ParseInput(inputString, command);
IntegratedCheck(command->argv[0]);
forkPID = fork();
2022-10-19 21:14:59 -05:00
// Child
if (forkPID == 0)
{
// This is the child process
// Setup the child's process environment here
// E.g., where is standard I/O, how to handle signals?
// TODO: Adjust Execute() to handle CommandStruct
Execute(inputString);
// exec does not return if it succeeds
printf("ERROR: Could not execute %s\n", inputString);
exit(1);
2022-10-19 21:14:59 -05:00
}
// Parent
else
{
// 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;
}
}
// Executes a command to the system
void Execute(char* command)
{
printf("%s\n", command);
system(command);
exit(0);
}
// Prints a prompt and then reads a command from the terminal
void GetInput(char* inputString)
{
char prompt = '$';
printf("%c ", prompt);
scanf("%[^\n]", inputString);
return;
}
void ParseInput(char* inputString, CommandStruct* command)
{
//Parse command
int* count = &(command->argc);
char* token = strtok(inputString, " ");
command->argv[*count] = token;
(*count)++;
while (token != NULL)
{
//This only holds 1 command at a time then it overrides
//Will need modified
token = strtok(NULL, " ");
command->argv[*count] = token;
(*count)++;
}
}