Final cleanup before submitting
This commit is contained in:
parent
8e61e04e05
commit
01625790bd
@ -60,15 +60,12 @@ void ParseInput(char *inputString, CommandStruct *command, char *symbol)
|
|||||||
// Splits a string separated by spaces into an array of strings
|
// Splits a string separated by spaces into an array of strings
|
||||||
void SplitInput(char *inputString, CommandStruct *command)
|
void SplitInput(char *inputString, CommandStruct *command)
|
||||||
{
|
{
|
||||||
// Parse command
|
|
||||||
int *argc = &(command->argc);
|
int *argc = &(command->argc);
|
||||||
char *token = strtok(inputString, " ");
|
char *token = strtok(inputString, " ");
|
||||||
command->argv[*argc] = token;
|
command->argv[*argc] = token;
|
||||||
(*argc)++;
|
(*argc)++;
|
||||||
while (token != NULL)
|
while (token != NULL)
|
||||||
{
|
{
|
||||||
// This only holds 1 command at a time then it overrides
|
|
||||||
// Will need modified
|
|
||||||
token = strtok(NULL, " ");
|
token = strtok(NULL, " ");
|
||||||
command->argv[*argc] = token;
|
command->argv[*argc] = token;
|
||||||
(*argc)++;
|
(*argc)++;
|
||||||
@ -88,17 +85,14 @@ int CheckSymbol(CommandStruct* command, char symbol, int start)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Checking redirection
|
// Checking redirection
|
||||||
// TODO: Work on one symbol at a time
|
|
||||||
// inputString may not be useful here, only *command
|
|
||||||
// Try to add a layer of abstraction where possible
|
|
||||||
// I.E. think about using function instead for checking
|
|
||||||
void CheckRedirection(CommandStruct* command)
|
void CheckRedirection(CommandStruct* command)
|
||||||
{
|
{
|
||||||
// check command standard output
|
// check command standard output
|
||||||
// TODO: Check command->argv[] for symbol instead
|
// TODO: Check command->argv[] for symbol instead
|
||||||
for (int i = 0; i < command->argc; i++)
|
for (int i = 0; i < command->argc; i++)
|
||||||
{
|
{
|
||||||
if ((command->argv[i][0] == '>') && (command->argv[i][1] == '>')) printf(">> was found.\n");
|
if ((command->argv[i][0] == '>') && (command->argv[i][1] == '>'))
|
||||||
|
printf(">> was found.\n");
|
||||||
// ParseInput(inputString, command, ">>");
|
// ParseInput(inputString, command, ">>");
|
||||||
if (command->argv[i][0] == '<')
|
if (command->argv[i][0] == '<')
|
||||||
printf("< was found.\n");
|
printf("< was found.\n");
|
||||||
@ -115,6 +109,7 @@ void CheckRedirection(CommandStruct* command)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Count number of appearances of a symbol in a CommandStruct
|
||||||
int CountSymbol(CommandStruct* command, char symbol)
|
int CountSymbol(CommandStruct* command, char symbol)
|
||||||
{
|
{
|
||||||
int count = 0;
|
int count = 0;
|
||||||
@ -182,19 +177,12 @@ int ExecPipe(CommandStruct* commandParent, CommandStruct* commandChild, int* pip
|
|||||||
pipe(pipefd);
|
pipe(pipefd);
|
||||||
for (int i = 0; i < pipeAmount; i++)
|
for (int i = 0; i < pipeAmount; i++)
|
||||||
{
|
{
|
||||||
|
|
||||||
// if (pipeLocation > 0)
|
|
||||||
// startArgc = pipeLocation + 1;
|
|
||||||
pipeLocation = CheckSymbol(commandParent, '|', startArgc);
|
pipeLocation = CheckSymbol(commandParent, '|', startArgc);
|
||||||
// if (pipeLocation >= 0)
|
|
||||||
endArgc = pipeLocation;
|
endArgc = pipeLocation;
|
||||||
// if (pipeLocation < 0)
|
|
||||||
// endArgc = commandParent->argc;
|
|
||||||
CopyCommandStruct(commandParent, commandChild, startArgc, endArgc);
|
CopyCommandStruct(commandParent, commandChild, startArgc, endArgc);
|
||||||
forkPID = fork();
|
forkPID = fork();
|
||||||
if (forkPID == 0)
|
if (forkPID == 0)
|
||||||
return forkPID;
|
return forkPID;
|
||||||
// if (forkPID != 0)
|
|
||||||
close(pipefd[0]);
|
close(pipefd[0]);
|
||||||
close(pipefd[1]);
|
close(pipefd[1]);
|
||||||
wait(&forkPID);
|
wait(&forkPID);
|
||||||
|
11
src/Pish.c
11
src/Pish.c
@ -10,15 +10,6 @@
|
|||||||
#include "Integrated.h"
|
#include "Integrated.h"
|
||||||
#include "Pish.h"
|
#include "Pish.h"
|
||||||
|
|
||||||
// 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
|
|
||||||
|
|
||||||
// Main function for Pish program
|
// Main function for Pish program
|
||||||
void Pish(void)
|
void Pish(void)
|
||||||
{
|
{
|
||||||
@ -93,7 +84,7 @@ void RunChild(CommandStruct* commandChild, int* pipefd)
|
|||||||
close(pipefd[1]);
|
close(pipefd[1]);
|
||||||
// execve() is recommended, execvpe() may be better
|
// execve() is recommended, execvpe() may be better
|
||||||
CheckRedirection(commandChild);
|
CheckRedirection(commandChild);
|
||||||
ioRedirection(commandChild);
|
// ioRedirection(commandChild);
|
||||||
execvp(commandChild->argv[0], commandChild->argv);
|
execvp(commandChild->argv[0], commandChild->argv);
|
||||||
// exec does not return if it succeeds
|
// exec does not return if it succeeds
|
||||||
printf("ERROR: Could not execute %s\n", commandChild->argv[0]);
|
printf("ERROR: Could not execute %s\n", commandChild->argv[0]);
|
||||||
|
Loading…
Reference in New Issue
Block a user