
processline
By: a guest on
Nov 29th, 2012 | syntax:
C | size: 1.75 KB | hits: 39 | expires: Never
void processline (char *line, int inFD, int outFD, plFlags FLAGS) //, int mainargc, char **mainargv, char **shiftedMArgV)
{
int status;
char expanded_line[EXPANDEDLEN];
char **args;
char *line_itr = line;
int command_count;
int pix;
int fd[2];
int line_len = strlen(line);
plFlags pl_flags = {0U, 0U, 0U};
if(FLAGS.EXPAND)
{
if(expand(line, expanded_line, EXPANDEDLEN)) return; //, mainargc, mainargv, shiftedMArgV)) return;
line_itr = expanded_line;
line_len = strlen(expanded_line);
}
if((pix = findUnquotChar(line_itr, '|')))
{
line_itr[pix++] = 0;
if(pipe (fd) < 0) perror("pipe");
processline(line_itr, inFD, fd[1], pl_flags);
line_itr = &(line_itr[pix]);
while((pix = findUnquotChar(line_itr, '|')) && pix < line_len)
{
line_itr[pix++] = 0;
//if(pipe (fd) < 0) perror("pipe");
processline(line_itr, fd[0], fd[1] pl_flags);
line_itr = &(line_itr[pix]);
}
return;
}
command_count = arg_parse(expanded_line, &args);
/* print_args(args); */
if(!command_count) return;
if(!exec_if_built_in(args, outFD))
{
/* Start a new process to do the job. */
cpid = fork();
if (cpid < 0) {
perror ("fork");
free(args);
return;
}
/* Check for who we are! */
if (cpid == 0) {
/* We are the child! */
/* Turning stdout into pipe output if needed */
if((dup2(outFD, 1)) < 0)
{
perror("dup");
return;
}
execvp(args[0], args);
perror ("exec");
exit (127);
last_exit_status = 127;
}
/* Have the parent wait for child to complete */
if(FLAGS.WAIT)
{
if(wait(&status) < 0) perror("wait");
last_exit_status = ((WEXITSTATUS(status) || !WIFEXITED(status)) ? 127 : 0);
}
}
free(args);
}