2022-10-10 18:55:22 -05:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/wait.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
|
|
|
// Prints a prompt and then reads a command from the terminal
|
2022-10-19 21:14:59 -05:00
|
|
|
char* getInput(void)
|
2022-10-10 18:55:22 -05:00
|
|
|
{
|
2022-10-19 21:46:44 -05:00
|
|
|
return "echo hello world";
|
2022-10-10 18:55:22 -05:00
|
|
|
}
|
|
|
|
|
2022-10-19 21:46:44 -05:00
|
|
|
// Executes a command to the system
|
2022-10-10 18:55:22 -05:00
|
|
|
void exec(char* command)
|
|
|
|
{
|
2022-10-19 21:46:44 -05:00
|
|
|
system(command);
|
2022-10-10 18:55:22 -05:00
|
|
|
}
|
|
|
|
|
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-19 21:46:44 -05:00
|
|
|
char* command;
|
|
|
|
int retval;
|
2022-10-10 18:55:22 -05:00
|
|
|
while (1)
|
|
|
|
{
|
2022-10-19 21:46:44 -05:00
|
|
|
command = getInput();
|
|
|
|
IntegratedCheck(command);
|
|
|
|
retval = fork();
|
2022-10-19 21:14:59 -05:00
|
|
|
// Child
|
2022-10-10 18:55:22 -05:00
|
|
|
if (retval == 0)
|
|
|
|
{
|
|
|
|
// 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-19 21:14:59 -05:00
|
|
|
exec(command);
|
2022-10-10 18:55:22 -05:00
|
|
|
// exec does not return if it succeeds
|
2022-10-19 21:14:59 -05:00
|
|
|
printf("ERROR: Could not execute %s\n", command);
|
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
|
|
|
|
int pid = retval;
|
|
|
|
wait(pid);
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|