ast_comparator/test/student_data/handout/shlab-handout/cases/reference_6_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. Otherwise,
 * all children of the shell including background children would be
 * in the same process group and would receive a SIGINT (SIGTSTP) signal
 * when we type ctrl-c (ctrl-z) at the keyboard. Only the foreground process
 * group should receive those signals.
*/
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. Otherwise,
all children of the shell including background children would be
in the same process group and would receive a SIGINT (SIGTSTP) signal
when we type ctrl-c (ctrl-z) at the keyboard. Only the foreground process
group should receive those signals.
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 . Otherwise all children of the shell including background children would be in the same process group and would receive a SIGINT ( SIGTSTP ) signal when we type ctrl - c ( ctrl - z ) at the keyboard . Only the foreground process group should receive those signals .
void eval(char *cmdline) 
{
    char *argv[MAXARGS];
    char buf[MAXLINE];
    int bg;
    pid_t pid;
    sigset_t mask;
    sigemptyset(&mask);
    sigaddset(&mask, SIGCHLD);
    strcpy(buf, cmdline);
    bg = parseline(buf, argv);
    if (argv[0] == NULL)
    return;   
    if (!builtin_cmd(argv)) {        
    sigprocmask(SIG_BLOCK, &mask, 0);
    if ((pid = fork()) < 0) {   
        printf("fork(): forking error\n");
        return;
    }
    if (pid == 0) {
        setpgid(0,0);               
        if (execvp(argv[0], argv) < 0) {
        printf("%s: Command not found. \n", argv[0]);
        exit(0);
        }
    } else {
        if (bg == 1) {
        addjob(jobs, pid, BG, cmdline);     
        printf("[%d] (%d) %s", pid2jid(pid), pid, cmdline);
        } else {
        addjob(jobs, pid, FG, cmdline);     
        }
        sigprocmask(SIG_UNBLOCK, &mask, 0);     
        waitfg(pid);
    }
    }
    return;
}
void eval ( char * cmdline ) { char * argv [ MAXARGS ] ; char buf [ MAXLINE ] ; int bg ; pid_t pid ; sigset_t mask ; sigemptyset ( & mask ) ; sigaddset ( & mask , SIGCHLD ) ; strcpy ( buf , cmdline ) ; bg = parseline ( buf , argv ) ; if ( argv [ 0 ] == NULL ) return ; if ( ! builtin_cmd ( argv ) ) { sigprocmask ( SIG_BLOCK , & mask , 0 ) ; if ( ( pid = fork ( ) ) < 0 ) { printf ( " \n " ) ; return ; } if ( pid == 0 ) { setpgid ( 0 , 0 ) ; if ( execvp ( argv [ 0 ] , argv ) < 0 ) { printf ( " \n " , argv [ 0 ] ) ; exit ( 0 ) ; } } else { if ( bg == 1 ) { addjob ( jobs , pid , BG , cmdline ) ; printf ( " " , pid2jid ( pid ) , pid , cmdline ) ; } else { addjob ( jobs , pid , FG , cmdline ) ; } sigprocmask ( SIG_UNBLOCK , & mask , 0 ) ; waitfg ( pid ) ; } } 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
/* Declare and initialize a signal set */ /* Ignore empty lines */ /* If user input is not a built in command, fork() */ /* Parent blocks SIGCHLD signal temporarily */ /* Child runs user job */ /* Change child process group id */ /* Parent waits for foreground job to terminate */ /* If bg, add job to job list as bg */ /* If !bg, add job to job list as fg */ /* Parent unblocks SIGCHLD */