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