Big commit: Setup outline for piping, piping has errors still, cleaned code

This commit is contained in:
TriantaTV 2022-10-29 05:14:23 -05:00
parent 4b3f4c1433
commit b33abaf517
6 changed files with 143 additions and 65 deletions

View File

@ -1,11 +1,14 @@
#ifndef COMMON_H
#define COMMON_H
const char* GetHomeDir(void);
typedef struct CommandStruct {
char* argv[100]; // Max 100 commands
int argc;
} CommandStruct;
void CopyCommandStruct(CommandStruct* oldCommand, CommandStruct* newCommand, int start, int end);
void ResetCommandStruct(CommandStruct* command);
#endif

View File

@ -3,16 +3,16 @@
#include "Common.h"
const char* GetHomeDir(void);
void CheckRedirection(CommandStruct* command);
int CheckSymbol(CommandStruct* command, char symbol, int start);
int IntegratedCheck(CommandStruct* command);
void GetInput(char* inputString);
void append(char *input, char *file);
void input(char *command);
void ExecPipe();
int ExecPipe(CommandStruct* commandParent, CommandStruct* commandChild, int* pipefd);
void output(char *command);
void SplitInput(char* inputString, CommandStruct* command);
void ParseInput(char* inputString, CommandStruct* command, char *symbol);
void ReadPishrc(CommandStruct* command, char* inputString);
void ParseInput(char* inputString, CommandStruct* command, char* symbol);
void RunChild(CommandStruct* commandChild, int* pipefd);
void ioRedirection(CommandStruct* command);
#endif

View File

@ -4,5 +4,6 @@
#include "Common.h"
void Pish(void);
void ReadPishrc(CommandStruct* commandParent, CommandStruct *commandChild, char* inputString);
#endif

View File

@ -1,7 +1,28 @@
#include "Common.h"
#include <pwd.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
// Gets home directory location of user
const char* GetHomeDir(void)
{
char *homeDir = (getpwuid(getuid()))->pw_dir;
return homeDir;
}
// Copies a range of an old command object to new command object
void CopyCommandStruct(CommandStruct* oldCommand, CommandStruct* newCommand, int start, int end)
{
newCommand->argc = end - start;
for (int i = 0; i < newCommand->argc; i++)
newCommand->argv[i] = oldCommand->argv[i + start];
return;
}
// Resets entire command object
void ResetCommandStruct(CommandStruct* command)
{
for (int i = 0; i < command->argc; i++)

View File

@ -1,22 +1,13 @@
#include <assert.h>
#include <fcntl.h>
#include <pwd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include "Integrated.h"
// Gets home directory location of user
const char *GetHomeDir(void)
{
char *homeDir = (getpwuid(getuid()))->pw_dir;
return homeDir;
}
// Checks for commands that are built into Pish
int IntegratedCheck(CommandStruct *command)
{
@ -66,35 +57,23 @@ void ParseInput(char *inputString, CommandStruct *command, char *symbol)
}
}
// Reads ~/.pishrc and runs each command in the file
void ReadPishrc(CommandStruct *command, char *inputString)
// Run child process
void RunChild(CommandStruct* commandChild, int* pipefd)
{
char buffer[1];
int forkID;
int *argc = &(command->argc);
char* pishLocation = GetHomeDir();
strcat(pishLocation, "/.pishrc");
int fd = open(pishLocation, O_RDONLY | O_CREAT);
assert(fd > -1);
while (read(fd, buffer, 1) > 0)
{
if (buffer[0] == '\n')
{
ParseInput(inputString, command, " ");
forkID = fork();
if (forkID == 0)
execvp(command->argv[0], command->argv);
else
wait(&forkID);
strcpy(inputString, "");
ResetCommandStruct(command);
continue;
}
strcat(inputString, buffer);
}
assert(close(fd) >= 0);
close(pipefd[0]);
dup2(pipefd[1], 1);
close(pipefd[1]);
// This is the child process
// Setup the child's process environment here
// E.g., where is standard I/O, how to handle signals?
// execve() is recommended, execvpe() may be better
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);
}
// Splits a string separated by spaces into an array of strings
void SplitInput(char *inputString, CommandStruct *command)
{
@ -114,6 +93,18 @@ void SplitInput(char *inputString, CommandStruct *command)
(*argc)--;
}
// Splits a command array into left and right
// void SplitPipe(CommandStruct* commandLeft, CommandStruct* commandRight);
int CheckSymbol(CommandStruct* command, char symbol, int start)
{
for (int i = start; i < command->argc; i++)
if (command->argv[i][0] == symbol)
return i;
return -1;
}
// Checking redirection
// TODO: Work on one symbol at a time
// inputString may not be useful here, only *command
@ -188,17 +179,45 @@ void input(char *command)
dup2(newfd,0);
}
void ExecPipe()
int ExecPipe(CommandStruct* commandParent, CommandStruct* commandChild, int* pipefd)
{
// ;
char buffer[1024];
int forkPID;
int pipeLocation = 0;
int startArgc = 0;
int endArgc = commandParent->argc;
while (pipeLocation >= 0)
{
if (pipeLocation > 0)
startArgc = pipeLocation + 1;
pipeLocation = CheckSymbol(commandParent, '|', startArgc);
if (pipeLocation >= 0)
endArgc = pipeLocation - 1;
if (pipeLocation < 0)
endArgc = commandParent->argc;
CopyCommandStruct(commandParent, commandChild, startArgc, endArgc);
pipe(pipefd);
forkPID = fork();
if (forkPID == 0)
return forkPID;
if (forkPID != 0)
wait(&forkPID);
close(pipefd[1]);
while (read(pipefd[0], buffer, sizeof(buffer)) != 0)
printf("%s", buffer);
ResetCommandStruct(commandChild); // Clean command
}
return 1;
}
void append(char *input, char *file)
void append(char* input, char* fileName)
{
FILE *fp;
fp = fopen(file, "a+");
fputs(input, fp);
fclose(fp);
char buffer[4];
int fd = open(fileName, O_APPEND | O_CREAT);
// fp = fopen(file, "a+");
// fputs(input, fp);
// fclose(fp);
}
// check command standard input

View File

@ -1,46 +1,80 @@
#include <assert.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <string.h>
#include "Common.h"
#include "Integrated.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
void Pish(void)
{
CommandStruct commandObject = {"", 0};
CommandStruct* command = &commandObject;
CommandStruct commandParentObject = {"", 0};
CommandStruct commandChildObject = {"", 0};
CommandStruct* commandParent = &commandParentObject;
CommandStruct* commandChild = &commandChildObject;
char inputString[1000]; // Max 1000 characters
int forkPID;
ReadPishrc(command, inputString);
ReadPishrc(commandParent, commandChild, inputString);
while (1)
{
ResetCommandStruct(command); // Clean command
int pipefd[2];
ResetCommandStruct(commandParent); // Clean command
strcpy(inputString, ""); // Clean inputString
GetInput(inputString);
SplitInput(inputString, command);
if (IntegratedCheck(command))
SplitInput(inputString, commandParent);
if (IntegratedCheck(commandParent))
continue;
forkPID = ExecPipe(commandParent, commandChild, pipefd);
// CheckRedirection(inputString, command);
CheckRedirection(command);
// CheckRedirection(command);
// ioRedirection(command);
forkPID = fork();
// 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?
// execve() is recommended, execvpe() may be better
execvp(command->argv[0], command->argv);
// exec does not return if it succeeds
printf("ERROR: Could not execute %s\n", inputString);
exit(1);
}
RunChild(commandChild, pipefd);
// Parent
// This is the parent process; Wait for the child to finish
wait(&forkPID);
}
}
// 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");
int fd = open(pishLocation, O_RDONLY | O_CREAT);
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);
}