2022-10-27 04:37:16 -05:00
|
|
|
#include <assert.h>
|
2022-10-29 05:14:23 -05:00
|
|
|
#include <fcntl.h>
|
2022-10-29 21:21:13 -05:00
|
|
|
#include <signal.h>
|
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-29 05:14:23 -05:00
|
|
|
#include "Common.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-29 05:14:23 -05:00
|
|
|
// Example command: ps aux | grep 'Z'
|
|
|
|
// Pipe left: [ps aux]
|
|
|
|
// argv: [ps, aux]
|
|
|
|
// Pipe right: [grep 'Z']
|
|
|
|
// argv: [grep, 'Z']
|
|
|
|
|
|
|
|
// check for first pipe, split into before and after pipe,
|
|
|
|
// connect left and right execs
|
|
|
|
|
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-29 05:14:23 -05:00
|
|
|
CommandStruct commandParentObject = {"", 0};
|
|
|
|
CommandStruct commandChildObject = {"", 0};
|
|
|
|
CommandStruct* commandParent = &commandParentObject;
|
|
|
|
CommandStruct* commandChild = &commandChildObject;
|
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-29 21:21:13 -05:00
|
|
|
signal(SIGINT, SIG_IGN);
|
|
|
|
signal(SIGQUIT, SIG_IGN);
|
|
|
|
signal(SIGTERM, SIG_IGN);
|
2022-10-29 05:14:23 -05:00
|
|
|
ReadPishrc(commandParent, commandChild, inputString);
|
2022-10-10 18:55:22 -05:00
|
|
|
while (1)
|
|
|
|
{
|
2022-10-29 05:14:23 -05:00
|
|
|
int pipefd[2];
|
|
|
|
ResetCommandStruct(commandParent); // Clean command
|
2022-10-27 04:37:16 -05:00
|
|
|
strcpy(inputString, ""); // Clean inputString
|
2022-10-25 02:12:07 -05:00
|
|
|
GetInput(inputString);
|
2022-10-29 20:29:28 -05:00
|
|
|
if (strcmp(inputString, "") == 0)
|
|
|
|
continue;
|
2022-10-29 05:14:23 -05:00
|
|
|
SplitInput(inputString, commandParent);
|
|
|
|
if (IntegratedCheck(commandParent))
|
2022-10-28 16:14:53 -05:00
|
|
|
continue;
|
2022-10-29 05:14:23 -05:00
|
|
|
forkPID = ExecPipe(commandParent, commandChild, pipefd);
|
2022-10-29 01:10:12 -05:00
|
|
|
// CheckRedirection(inputString, command);
|
2022-10-29 05:14:23 -05:00
|
|
|
// CheckRedirection(command);
|
2022-10-29 01:10:12 -05:00
|
|
|
// ioRedirection(command);
|
2022-10-19 21:14:59 -05:00
|
|
|
// Child
|
2022-10-25 01:09:00 -05:00
|
|
|
if (forkPID == 0)
|
2022-10-29 05:14:23 -05:00
|
|
|
RunChild(commandChild, pipefd);
|
2022-10-19 21:14:59 -05:00
|
|
|
// Parent
|
2022-10-27 00:25:40 -05:00
|
|
|
// This is the parent process; Wait for the child to finish
|
2022-10-10 18:55:22 -05:00
|
|
|
}
|
|
|
|
}
|
2022-10-29 05:14:23 -05:00
|
|
|
|
|
|
|
// Reads ~/.pishrc and runs each command in the file
|
|
|
|
void ReadPishrc(CommandStruct *commandParent, CommandStruct *commandChild, char *inputString)
|
|
|
|
{
|
|
|
|
char buffer[1];
|
|
|
|
int pipefd[2];
|
|
|
|
int forkPID;
|
|
|
|
int *argc = &(commandParent->argc);
|
|
|
|
char* pishLocation = GetHomeDir();
|
|
|
|
strcat(pishLocation, "/.pishrc");
|
2022-10-29 21:21:13 -05:00
|
|
|
int fd = open(pishLocation, O_RDONLY | O_CREAT, 0644);
|
2022-10-29 05:14:23 -05:00
|
|
|
assert(fd > -1);
|
|
|
|
while (read(fd, buffer, 1) > 0)
|
|
|
|
{
|
|
|
|
if (buffer[0] == '\n')
|
|
|
|
{
|
|
|
|
SplitInput(inputString, commandParent);
|
|
|
|
if (IntegratedCheck(commandParent))
|
|
|
|
continue;
|
|
|
|
forkPID = ExecPipe(commandParent, commandChild, pipefd);
|
|
|
|
if (forkPID == 0)
|
|
|
|
RunChild(commandChild, pipefd);
|
|
|
|
strcpy(inputString, "");
|
|
|
|
ResetCommandStruct(commandParent);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
strcat(inputString, buffer);
|
|
|
|
}
|
|
|
|
assert(close(fd) >= 0);
|
2022-10-29 05:37:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Run a child process
|
|
|
|
void RunChild(CommandStruct* commandChild, int* pipefd)
|
|
|
|
{
|
|
|
|
close(pipefd[0]);
|
|
|
|
dup2(pipefd[1], 1);
|
|
|
|
close(pipefd[1]);
|
|
|
|
// execve() is recommended, execvpe() may be better
|
2022-10-29 22:55:36 -05:00
|
|
|
CheckRedirection(commandChild);
|
|
|
|
ioRedirection(commandChild);
|
2022-10-29 05:37:29 -05:00
|
|
|
execvp(commandChild->argv[0], commandChild->argv);
|
|
|
|
// exec does not return if it succeeds
|
|
|
|
printf("ERROR: Could not execute %s\n", commandChild->argv[0]);
|
|
|
|
exit(1);
|
|
|
|
}
|