ast_comparator/test/student_data/handout/shlab-handout/cases/reference_24_tsh.c
/* 
 * eval - Evaluate the command line that the user has just typed in
 * 
 * If the user has requested a built-in command (quit, jobs, bg or fg)
 * then execute it immediately. Otherwise, fork a child process and
 * run the job in the context of the child. If the job is running in
 * the foreground, wait for it to terminate and then return.  Note:
 * each child process must have a unique process group ID so that our
 * background children don't receive SIGINT (SIGTSTP) from the kernel
 * when we type ctrl-c (ctrl-z) at the keyboard.  
*/
Evaluate the command line that the user has just typed in

If the user has requested a built-in command (quit, jobs, bg or fg)
then execute it immediately. Otherwise, fork a child process and
run the job in the context of the child. If the job is running in
the foreground, wait for it to terminate and then return.  Note:
each child process must have a unique process group ID so that our
background children don't receive SIGINT (SIGTSTP) from the kernel
when we type ctrl-c (ctrl-z) at the keyboard.
Evaluate the command line that the user has just typed in If the user has requested a built - in command ( quit jobs bg or fg ) then execute it immediately . Otherwise fork a child process and run the job in the context of the child . If the job is running in the foreground wait for it to terminate and then return . Note : each child process must have a unique process group ID so that our background children don ' t receive SIGINT ( SIGTSTP ) from the kernel when we type ctrl - c ( ctrl - z ) at the keyboard .
void eval(char *cmdline) 
{
    char *argv[MAXARGS];
    int bg;
    pid_t pid;
    bg = parseline(cmdline, argv);
    if( (bg!=-1) && (!builtin_cmd(argv)) )
    {
	    int j = 0;
	    char * pipe_str = "|";
	    int pipeindex = -1;
	    while(argv[j]!=NULL)
	    {
	      if(strncmp(pipe_str,argv[j],MAXLINE)==0)
	      {
		pipeindex = j;
		break;
	      }
	      j++;
	    }
	    if(pipeindex > 0)
	    {
	      int childleftpid;
	      char *argvleft[MAXARGS];
	      char *argvright[MAXARGS];
	      int childrightpid;
	      for(j=0;j< pipeindex; j++)
	      {
		argvleft[j] = argv[j];
	      }
	      argvleft[j] = NULL;
	      int newindex;
	      for(j=pipeindex+1;j<MAXARGS; j++)
	      {
		newindex = j - (pipeindex+1);
		argvright[newindex] = argv[j];
	      }
	      int pipefd[2];
	      if (pipe(pipefd) < 0) {
		unix_error("Pipe error.");
	      }
	      if((pid = fork()) == 0)
	      {
		dup2(pipefd[1], 1);
		if(execve(argvleft[0], argvleft, environ) < 0)
		{
		    printf("%s: Command not found.\n", argvleft[0]);
		    exit(0);
		}
	      }
	      else
	      {
		childleftpid = pid;
		if((pid = fork()) == 0)
		{
		  dup2(pipefd[0], 0);
		  if(execve(argvright[0], argvright, environ) < 0)
		  {	 
		      printf("%s: Command not found.\n", argvright[0]);
		      exit(0);
		  }
		}
		else
		{
		  childrightpid = pid;
		  if(!bg)
		  {
		    addjob(jobs, childleftpid, FG, cmdline, childrightpid);
		    waitfg(childrightpid);
		  }
		  else
		  {
		    addjob(jobs, childleftpid, BG, cmdline, childrightpid);
		  }
		}  
	      }  
	    }
	    else
	    {      
	      if((pid = fork()) == 0)
	      {
		if(execve(argv[0], argv, environ) < 0)
		{
		    printf("%s: Command not found.\n", argv[0]);
		    exit(0);
		}
	      }
	      else
	      {
		if(!bg)
		{
		  addjob(jobs, pid, FG, cmdline,0);
		  waitfg(pid);
		}
		else
		{
		  addjob(jobs, pid, BG, cmdline,0);
		}
	      }  
	    }
    }	
    return;
}
void eval ( char * cmdline ) { char * argv [ MAXARGS ] ; int bg ; pid_t pid ; bg = parseline ( cmdline , argv ) ; if ( ( bg != -1 ) && ( ! builtin_cmd ( argv ) ) ) { int j = 0 ; char * pipe_str = " " ; int pipeindex = -1 ; while ( argv [ j ] != NULL ) { if ( strncmp ( pipe_str , argv [ j ] , MAXLINE ) == 0 ) { pipeindex = j ; break ; } j ++ ; } if ( pipeindex > 0 ) { int childleftpid ; char * argvleft [ MAXARGS ] ; char * argvright [ MAXARGS ] ; int childrightpid ; for ( j = 0 ; j < pipeindex ; j ++ ) { argvleft [ j ] = argv [ j ] ; } argvleft [ j ] = NULL ; int newindex ; for ( j = pipeindex + 1 ; j < MAXARGS ; j ++ ) { newindex = j - ( pipeindex + 1 ) ; argvright [ newindex ] = argv [ j ] ; } int pipefd [ 2 ] ; if ( pipe ( pipefd ) < 0 ) { unix_error ( " " ) ; } if ( ( pid = fork ( ) ) == 0 ) { dup2 ( pipefd [ 1 ] , 1 ) ; if ( execve ( argvleft [ 0 ] , argvleft , environ ) < 0 ) { printf ( " \n " , argvleft [ 0 ] ) ; exit ( 0 ) ; } } else { childleftpid = pid ; if ( ( pid = fork ( ) ) == 0 ) { dup2 ( pipefd [ 0 ] , 0 ) ; if ( execve ( argvright [ 0 ] , argvright , environ ) < 0 ) { printf ( " \n " , argvright [ 0 ] ) ; exit ( 0 ) ; } } else { childrightpid = pid ; if ( ! bg ) { addjob ( jobs , childleftpid , FG , cmdline , childrightpid ) ; waitfg ( childrightpid ) ; } else { addjob ( jobs , childleftpid , BG , cmdline , childrightpid ) ; } } } } else { if ( ( pid = fork ( ) ) == 0 ) { if ( execve ( argv [ 0 ] , argv , environ ) < 0 ) { printf ( " \n " , argv [ 0 ] ) ; exit ( 0 ) ; } } else { if ( ! bg ) { addjob ( jobs , pid , FG , cmdline , 0 ) ; waitfg ( pid ) ; } else { addjob ( jobs , pid , BG , cmdline , 0 ) ; } } } } return ; }
eval - Evaluate the command line that the user has just typed in
eval - Evaluate the command line that the user has just typed in
//Modified so bg is -1 for empty command //Look through argv for pipe //If pipe exists //Pipe cant be first argument //Need to fork two children, one for each side of the pipe //Copy from 0 to pipeindex-1 into leftargv //Copy from pipeindex + 1 to MAXARGS //Create a pipe //Read is 0, write is 1 //Have two new argvs //Fork two children //Fork left child //Left child //Route stdout to write side of pipe //Parent Still //Fork right child //Is right child //Route stdin to read side of pipe //Is parent //Only right process in in fg //Left is always in bg //Add job only once //Whole thing is in bg //No Pipes present - just one command //Is child //Set the group id to the parent id - convention //Is parent //pid is child process //Child is in fg //Child is in bg