ast_comparator/test/student_data/handout/shlab-handout/cases/reference_8_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[MAXLINE];    
    char buf[MAXLINE];      
    int bg;                 
    pid_t pid;
    sigset_t mask;          
    stpcpy(buf,cmdline);
    bg = parseline(buf,argv);
    sigemptyset(&mask);
    sigaddset(&mask,SIGCHLD);
    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 [ MAXLINE ] ; char buf [ MAXLINE ] ; int bg ; pid_t pid ; sigset_t mask ; stpcpy ( buf , cmdline ) ; bg = parseline ( buf , argv ) ; sigemptyset ( & mask ) ; sigaddset ( & mask , SIGCHLD ) ; 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
 If the user has requested a built-in command (quit, jobs, bg or fg)
 then execute it immediately.
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 .
/*argument list of execve()*/ /*hold modified commend line*/ /*should the job run in bg or fg?*/ // The 'bg' variable is TRUE if the job should run // in background mode or FALSE if it should run in FG /*mask for signal*/ /*ignore empty line*/ /* If user input is not a built in command, fork(), not a build in cmd */ /* Parent blocks SIGCHLD signal temporarily */ /*block the SIGCHLD signal*/ /* Child runs user job */ /* Change child process group id, puts the child in a new process group */ /* Parent waits for fg job to terminate */ /* adds to joblist as bg if it is bg */ /* adds to joblist as fg, if !bg */ /* unblock the SIGCHLD */ //do in background or foreground