Hey guys,



So I'm making a UNIX minishell, and am trying to add pipelines, so I can do things like this:


Code:
ps aux | grep dh | grep -v grep | cut -c1-5
However I'm having trouble wrapping my head around the piping part. I replace all the "|" characters with 0, and then run each line as a normal line. However, I am trying to divert the output and input. The input of a command needs to be the output of the previous command, and the output of a command needs to be the input of the next command.
I'm doing this using pipes, however I can't figure out where to call pipe() and where to close them. From the main processing function, processline(), I have this code:

Code:
if((pix = findUnquotChar(line_itr, '|')))
{
	line_itr[pix++] = 0;
	if(pipe (fd_a) < 0) perror("pipe");
	processline(line_itr, inFD, fd_a[1], pl_flags);
	line_itr = &(line_itr[pix]);

	while((pix = findUnquotChar(line_itr, '|')) && pix < line_len)
	{
		close(fd_a[1]);
		line_itr[pix++] = 0;
		if(pipe (fd_b) < 0) perror("pipe");
		processline(line_itr, fd_a[0], fd_b[1] pl_flags);
		close(fd_a[0]);
		close(fd_b[1]);
		line_itr = &(line_itr[pix]);
	}
	return;
}
So, I'm recursively(the code above is in processline) sending the commands in between the "|" to be processed by processline.


The 2nd and 3rd parameter of processline are the inputFD and outputFD respectively, so I need to process a command, write the output to a pipe, and then call processline again on the next command, however this time the output of the previous command is the input.


This just doesn't seem like it can work though with one pipe, so in the above example I'm trying to make it work with two, by possibly flip flopping back and forth between using fd_a[0] as input for commands that occur during an odd iteration in the while loop, and use fd_b[0] for input on commands that occur during an even iteration.



I'm just having trouble seeing how this is possible with a single pipe, if you guys need any additional info just ask. Here's the entire processline function in case you want to take a look:
[C] processline - Pastebin.com