ast_comparator/test/student_data/handout/shlab-handout/cases/reference_12_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(argv[0] == NULL)
        return ;        
    if(!builtin_cmd(argv)) 
    {
        if((pid = fork()) < 0)
        {
            unix_error("forking error");    
        }
        else if(pid == 0)   
        {
            if(setpgid(0, 0) < 0)   
            {
                unix_error("setpgid error");
            }
            if(execve(argv[0], argv, environ) < 0)      
            {
                printf("%s: Command not found\n", argv[0]); 
                exit(1);
            }
        } 
        else 
        {
            if(!bg)
            {
                addjob(jobs, pid, FG, cmdline);     
            }
            else 
            {
                addjob(jobs, pid, BG, cmdline);     
            }
            if (!bg)
            {
                waitfg(pid);    
            }
            else 
            {
                printf("[%d] (%d) %s", pid2jid(pid), pid, cmdline); 
            }
        }
       }
}
void eval ( char * cmdline ) { char * argv [ MAXARGS ] ; int bg ; pid_t pid ; bg = parseline ( cmdline , argv ) ; if ( argv [ 0 ] == NULL ) return ; if ( ! builtin_cmd ( argv ) ) { if ( ( pid = fork ( ) ) < 0 ) { unix_error ( " " ) ; } else if ( pid == 0 ) { if ( setpgid ( 0 , 0 ) < 0 ) { unix_error ( " " ) ; } if ( execve ( argv [ 0 ] , argv , environ ) < 0 ) { printf ( " \n " , argv [ 0 ] ) ; exit ( 1 ) ; } } else { if ( ! bg ) { addjob ( jobs , pid , FG , cmdline ) ; } else { addjob ( jobs , pid , BG , cmdline ) ; } if ( ! bg ) { waitfg ( pid ) ; } else { printf ( " " , pid2jid ( pid ) , pid , cmdline ) ; } } } }
eval - Evaluate the command line that the user has just typed in
eval - Evaluate the command line that the user has just typed in
// the argument that will contain the cmdline takes // Argument List // bg/fg decider // process id // parse the line, then determine if it is a built-in or not and also indicates whether the command line is to run in the background. // ignore blank lines // Forking // forking child error // after forking we set new process group, run command // for child process // we put the child in a new process group whose group ID is identical to the child's PID // execve is similar to execv, but it permit you to specify the environment for the new program explicitly as the environ argument  // for built-in command not found // parent // Parent- add job to list,then do job // add job for foreground // add job for background // Testing for a fg job // wait until a process is no longer the foreground process             