Fixed pipe operator breaking output. Nothing actually pipes yet
This commit is contained in:
parent
b33abaf517
commit
a0d601f1e3
@ -5,6 +5,7 @@
|
||||
|
||||
void CheckRedirection(CommandStruct* command);
|
||||
int CheckSymbol(CommandStruct* command, char symbol, int start);
|
||||
int CountSymbol(CommandStruct* command, char symbol);
|
||||
int IntegratedCheck(CommandStruct* command);
|
||||
void GetInput(char* inputString);
|
||||
void append(char *input, char *file);
|
||||
@ -13,6 +14,5 @@ int ExecPipe(CommandStruct* commandParent, CommandStruct* commandChild, int* pip
|
||||
void output(char *command);
|
||||
void SplitInput(char* inputString, CommandStruct* command);
|
||||
void ParseInput(char* inputString, CommandStruct* command, char* symbol);
|
||||
void RunChild(CommandStruct* commandChild, int* pipefd);
|
||||
void ioRedirection(CommandStruct* command);
|
||||
#endif
|
||||
|
@ -5,5 +5,6 @@
|
||||
|
||||
void Pish(void);
|
||||
void ReadPishrc(CommandStruct* commandParent, CommandStruct *commandChild, char* inputString);
|
||||
void RunChild(CommandStruct* commandChild, int* pipefd);
|
||||
|
||||
#endif
|
||||
|
@ -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
|
||||
void SplitInput(char *inputString, CommandStruct *command)
|
||||
{
|
||||
@ -104,7 +87,6 @@ int CheckSymbol(CommandStruct* command, char symbol, int start)
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
// Checking redirection
|
||||
// TODO: Work on one symbol at a time
|
||||
// inputString may not be useful here, only *command
|
||||
@ -133,6 +115,16 @@ void CheckRedirection(CommandStruct* command)
|
||||
}
|
||||
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)
|
||||
{
|
||||
for (int i = 0; i <sizeof(command); i++)
|
||||
@ -179,34 +171,45 @@ void input(char *command)
|
||||
dup2(newfd,0);
|
||||
}
|
||||
|
||||
// Splits a parent process into separate children processes
|
||||
int ExecPipe(CommandStruct* commandParent, CommandStruct* commandChild, int* pipefd)
|
||||
{
|
||||
char buffer[1024];
|
||||
int forkPID;
|
||||
int pipeAmount = CountSymbol(commandParent, '|');
|
||||
int pipeLocation = 0;
|
||||
int startArgc = 0;
|
||||
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);
|
||||
if (pipeLocation >= 0)
|
||||
endArgc = pipeLocation - 1;
|
||||
if (pipeLocation < 0)
|
||||
endArgc = commandParent->argc;
|
||||
// if (pipeLocation >= 0)
|
||||
endArgc = pipeLocation;
|
||||
// if (pipeLocation < 0)
|
||||
// endArgc = commandParent->argc;
|
||||
CopyCommandStruct(commandParent, commandChild, startArgc, endArgc);
|
||||
pipe(pipefd);
|
||||
forkPID = fork();
|
||||
if (forkPID == 0)
|
||||
return forkPID;
|
||||
if (forkPID != 0)
|
||||
// if (forkPID != 0)
|
||||
wait(&forkPID);
|
||||
close(pipefd[1]);
|
||||
while (read(pipefd[0], buffer, sizeof(buffer)) != 0)
|
||||
printf("%s", buffer);
|
||||
startArgc = pipeLocation + 1;
|
||||
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;
|
||||
}
|
||||
|
||||
|
13
src/Pish.c
13
src/Pish.c
@ -78,3 +78,16 @@ void ReadPishrc(CommandStruct *commandParent, CommandStruct *commandChild, char
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user