ast_comparator/test/student_data/handout/shlab-handout/cases/reference_10_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];    
    char buf[MAXLINE];  
    int bg;         
    pid_t pid;      
    int jid;
    sigset_t mask;
    strcpy(buf, cmdline);
    bg = parseline(buf, argv);
    if (NULL == argv[0]) {
        return;     
    }
    if (!builtin_cmd(argv)) {
        sigemptyset(&mask);
        sigaddset(&mask, SIGCHLD);
        sigprocmask(SIG_BLOCK, &mask, NULL); 
        if ((pid = Fork()) == 0) {  
            Setpgid(0, 0);      
            sigprocmask(SIG_UNBLOCK, &mask, NULL); 
            if (execve(argv[0], argv, environ) < 0) {
                printf("%s: Command not found.\n", argv[0]);
                exit(0);
            }
        }
        if (!bg) {
            int status;
            addjob(jobs, pid, FG, cmdline);
            sigprocmask(SIG_UNBLOCK, &mask, NULL); 
            waitfg(pid);
        } else {    
            addjob(jobs, pid, BG, cmdline);
            jid = pid2jid(pid);
            printf("[%d] (%d) %s", jid, pid ,cmdline);
            sigprocmask(SIG_UNBLOCK, &mask, NULL); 
        }
    }
    return;
}
void eval ( char * cmdline ) { char * argv [ MAXARGS ] ; char buf [ MAXLINE ] ; int bg ; pid_t pid ; int jid ; sigset_t mask ; strcpy ( buf , cmdline ) ; bg = parseline ( buf , argv ) ; if ( NULL == argv [ 0 ] ) { return ; } if ( ! builtin_cmd ( argv ) ) { sigemptyset ( & mask ) ; sigaddset ( & mask , SIGCHLD ) ; sigprocmask ( SIG_BLOCK , & mask , NULL ) ; if ( ( pid = Fork ( ) ) == 0 ) { Setpgid ( 0 , 0 ) ; sigprocmask ( SIG_UNBLOCK , & mask , NULL ) ; if ( execve ( argv [ 0 ] , argv , environ ) < 0 ) { printf ( " \n " , argv [ 0 ] ) ; exit ( 0 ) ; } } if ( ! bg ) { int status ; addjob ( jobs , pid , FG , cmdline ) ; sigprocmask ( SIG_UNBLOCK , & mask , NULL ) ; waitfg ( pid ) ; } else { addjob ( jobs , pid , BG , cmdline ) ; jid = pid2jid ( pid ) ; printf ( " " , jid , pid , cmdline ) ; sigprocmask ( SIG_UNBLOCK , & mask , NULL ) ; } } 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
/* Argument list execve() */ /* Holds modified command line */ /* Should the job run in bg or fg? */ /* Process id */ /* Ignore empty lines */ /* Block SIGCHLD */ /* child runs user job */ /* set child group pid */ /* Unblock SIGCHLD */ /* Parent waits for foreground job to terminate */ /* Unblock SIGCHLD */ /* child runs in backround */ /* Unblock SIGCHLD */