ast_comparator/test/student_data/handout/shlab-handout/cases/reference_2_tsh.c
void eval(char *cmdline) 
{
    char *argv[MAXARGS];    // Argument List
    int bg;         // bg/fg decider
    pid_t pid;      // process id
    sigset_t mask;      // for blocking signals
    
    // parse the line, then determine if it is a builtin
    bg = parseline(cmdline, argv);
    if(!builtin_cmd(argv)) { 
        
        // Blocking SIGCHILD signals to avoid a race
        if(sigemptyset(&mask) != 0){
            unix_error("sigemptyset error");
        }
        if(sigaddset(&mask, SIGCHLD) != 0){
            unix_error("sigaddset error");
        }
        if(sigprocmask(SIG_BLOCK, &mask, NULL) != 0){
            unix_error("sigprocmask error");
        }
        
        // Forking
        if((pid = fork()) < 0){
            unix_error("forking error");
        }
        // Child- unblock mask, set new process group, run command 
        else if(pid == 0) {
            if (sigprocmask(SIG_UNBLOCK, &mask, NULL) != 0){
                unix_error("sigprocmask error");
            }
            if(setpgid(0, 0) < 0) {
                unix_error("setpgid error");
            }
            
            if(execvp(argv[0], argv) < 0) {
                printf("%s: Command not found\n", argv[0]);
                exit(1);
            }
        } 
        // Parent- add job to list, unblock signal, then do job
        else {
            if(!bg){
                addjob(jobs, pid, FG, cmdline);
            }
            else {
                addjob(jobs, pid, BG, cmdline);
            }
            if (sigprocmask(SIG_UNBLOCK, &mask, NULL) != 0){
                unix_error("sigprocmask error");
            }
            
            // Testing for a fg job
            if (!bg){
                waitfg(pid);
            } 
            else {
                printf("[%d] (%d) %s", pid2jid(pid), pid, cmdline);
            }
        }
    }
    
}
/* 
 * 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;      
    sigset_t mask;      
    bg = parseline(cmdline, argv);
    if(!builtin_cmd(argv)) { 
        if(sigemptyset(&mask) != 0){
            unix_error("sigemptyset error");
        }
        if(sigaddset(&mask, SIGCHLD) != 0){
            unix_error("sigaddset error");
        }
        if(sigprocmask(SIG_BLOCK, &mask, NULL) != 0){
            unix_error("sigprocmask error");
        }
        if((pid = fork()) < 0){
            unix_error("forking error");
        }
        else if(pid == 0) {
            if (sigprocmask(SIG_UNBLOCK, &mask, NULL) != 0){
                unix_error("sigprocmask error");
            }
            if(setpgid(0, 0) < 0) {
                unix_error("setpgid error");
            }
            if(execvp(argv[0], argv) < 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 (sigprocmask(SIG_UNBLOCK, &mask, NULL) != 0){
                unix_error("sigprocmask error");
            }
            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 ; sigset_t mask ; bg = parseline ( cmdline , argv ) ; if ( ! builtin_cmd ( argv ) ) { if ( sigemptyset ( & mask ) != 0 ) { unix_error ( " " ) ; } if ( sigaddset ( & mask , SIGCHLD ) != 0 ) { unix_error ( " " ) ; } if ( sigprocmask ( SIG_BLOCK , & mask , NULL ) != 0 ) { unix_error ( " " ) ; } if ( ( pid = fork ( ) ) < 0 ) { unix_error ( " " ) ; } else if ( pid == 0 ) { if ( sigprocmask ( SIG_UNBLOCK , & mask , NULL ) != 0 ) { unix_error ( " " ) ; } if ( setpgid ( 0 , 0 ) < 0 ) { unix_error ( " " ) ; } if ( execvp ( argv [ 0 ] , argv ) < 0 ) { printf ( " \n " , argv [ 0 ] ) ; exit ( 1 ) ; } } else { if ( ! bg ) { addjob ( jobs , pid , FG , cmdline ) ; } else { addjob ( jobs , pid , BG , cmdline ) ; } if ( sigprocmask ( SIG_UNBLOCK , & mask , NULL ) != 0 ) { unix_error ( " " ) ; } 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
// Argument List // bg/fg decider // process id // for blocking signals // parse the line, then determine if it is a builtin // Blocking SIGCHILD signals to avoid a race // Forking // Child- unblock mask, set new process group, run command  // Parent- add job to list, unblock signal, then do job // Testing for a fg job