Fixed pipe operator breaking output. Nothing actually pipes yet

This commit is contained in:
TriantaTV 2022-10-29 05:37:29 -05:00
parent b33abaf517
commit a0d601f1e3
4 changed files with 46 additions and 29 deletions

View File

@ -5,6 +5,7 @@
void CheckRedirection(CommandStruct* command); void CheckRedirection(CommandStruct* command);
int CheckSymbol(CommandStruct* command, char symbol, int start); int CheckSymbol(CommandStruct* command, char symbol, int start);
int CountSymbol(CommandStruct* command, char symbol);
int IntegratedCheck(CommandStruct* command); int IntegratedCheck(CommandStruct* command);
void GetInput(char* inputString); void GetInput(char* inputString);
void append(char *input, char *file); void append(char *input, char *file);
@ -13,6 +14,5 @@ int ExecPipe(CommandStruct* commandParent, CommandStruct* commandChild, int* pip
void output(char *command); void output(char *command);
void SplitInput(char* inputString, CommandStruct* command); void SplitInput(char* inputString, CommandStruct* command);
void ParseInput(char* inputString, CommandStruct* command, char* symbol); void ParseInput(char* inputString, CommandStruct* command, char* symbol);
void RunChild(CommandStruct* commandChild, int* pipefd);
void ioRedirection(CommandStruct* command); void ioRedirection(CommandStruct* command);
#endif #endif

View File

@ -5,5 +5,6 @@
void Pish(void); void Pish(void);
void ReadPishrc(CommandStruct* commandParent, CommandStruct *commandChild, char* inputString); void ReadPishrc(CommandStruct* commandParent, CommandStruct *commandChild, char* inputString);
void RunChild(CommandStruct* commandChild, int* pipefd);
#endif #endif

View File

@ -57,23 +57,6 @@ void ParseInput(char *inputString, CommandStruct *command, char *symbol)
} }
} }
// Run child process
void RunChild(CommandStruct* commandChild, int* pipefd)
{
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 // Splits a string separated by spaces into an array of strings
void SplitInput(char *inputString, CommandStruct *command) void SplitInput(char *inputString, CommandStruct *command)
{ {
@ -104,7 +87,6 @@ int CheckSymbol(CommandStruct* command, char symbol, int start)
return -1; return -1;
} }
// Checking redirection // Checking redirection
// TODO: Work on one symbol at a time // TODO: Work on one symbol at a time
// inputString may not be useful here, only *command // inputString may not be useful here, only *command
@ -133,6 +115,16 @@ void CheckRedirection(CommandStruct* command)
} }
return; return;
} }
int CountSymbol(CommandStruct* command, char symbol)
{
int count = 0;
for (int i = 0; i < command->argc; i++)
if (command->argv[i][0] == symbol)
count++;
return count;
}
void ioRedirection(CommandStruct* command) void ioRedirection(CommandStruct* command)
{ {
for (int i = 0; i <sizeof(command); i++) for (int i = 0; i <sizeof(command); i++)
@ -179,34 +171,45 @@ void input(char *command)
dup2(newfd,0); dup2(newfd,0);
} }
// Splits a parent process into separate children processes
int ExecPipe(CommandStruct* commandParent, CommandStruct* commandChild, int* pipefd) int ExecPipe(CommandStruct* commandParent, CommandStruct* commandChild, int* pipefd)
{ {
char buffer[1024]; char buffer[1024];
int forkPID; int forkPID;
int pipeAmount = CountSymbol(commandParent, '|');
int pipeLocation = 0; int pipeLocation = 0;
int startArgc = 0; int startArgc = 0;
int endArgc = commandParent->argc; int endArgc = commandParent->argc;
while (pipeLocation >= 0) for (int i = 0; i < pipeAmount; i++)
{ {
if (pipeLocation > 0)
startArgc = pipeLocation + 1; // if (pipeLocation > 0)
// startArgc = pipeLocation + 1;
pipeLocation = CheckSymbol(commandParent, '|', startArgc); pipeLocation = CheckSymbol(commandParent, '|', startArgc);
if (pipeLocation >= 0) // if (pipeLocation >= 0)
endArgc = pipeLocation - 1; endArgc = pipeLocation;
if (pipeLocation < 0) // if (pipeLocation < 0)
endArgc = commandParent->argc; // endArgc = commandParent->argc;
CopyCommandStruct(commandParent, commandChild, startArgc, endArgc); CopyCommandStruct(commandParent, commandChild, startArgc, endArgc);
pipe(pipefd); pipe(pipefd);
forkPID = fork(); forkPID = fork();
if (forkPID == 0) if (forkPID == 0)
return forkPID; return forkPID;
if (forkPID != 0) // if (forkPID != 0)
wait(&forkPID); wait(&forkPID);
close(pipefd[1]); close(pipefd[1]);
while (read(pipefd[0], buffer, sizeof(buffer)) != 0) while (read(pipefd[0], buffer, sizeof(buffer)) != 0)
printf("%s", buffer); printf("%s", buffer);
startArgc = pipeLocation + 1;
ResetCommandStruct(commandChild); // Clean command ResetCommandStruct(commandChild); // Clean command
} }
endArgc = commandParent->argc;
forkPID = fork();
CopyCommandStruct(commandParent, commandChild, startArgc, endArgc);
if (forkPID == 0)
return forkPID;
wait(&forkPID);
ResetCommandStruct(commandChild); // Clean command
return 1; return 1;
} }

View File

@ -78,3 +78,16 @@ void ReadPishrc(CommandStruct *commandParent, CommandStruct *commandChild, char
} }
assert(close(fd) >= 0); assert(close(fd) >= 0);
} }
// 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
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);
}