ast_comparator/test/student_data/handout/shlab-handout/cases/prev_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[MAXARGS];
    char buf[MAXLINE];
    strcpy(buf, cmdline); 
    sigset_t sig_set;
    sigemptyset(&sig_set);
    sigaddset(&sig_set, SIGCHLD);
    int bg;
    bg = parseline(buf, argv);
    dprintf("|  current cmdline : %s\n", cmdline);
    if(argv[0] == NULL) return;
    else if(!builtin_cmd(argv)){ 
        dprintf("|  |  command not a builtin function\n");
        sigprocmask(SIG_BLOCK, &sig_set, 0);
        pid_t pid = fork(); 
        if(pid < 0){
            printf("fork error\n");
            return;
        }
        if(pid == 0){ 
            dprintf("|  |  child!!\n");
            setpgid(0, 0); dprintf("1\n");
            if(execvp(argv[0], argv) < 0){
                printf("%s:  Command not found\n", argv[0]);
                exit(0);
            }
        }
        else{ 
            dprintf("|  |  parent!!\n");
            if(bg){ 
                addjob(jobs, pid, BG, cmdline);
                printf("[%d] (%d) %s", pid2jid(pid), pid, cmdline);
            }
            else{ 
                addjob(jobs, pid, FG, cmdline);
            }
            sigprocmask(SIG_UNBLOCK, &sig_set, 0); 
            waitfg(pid);
        }
    }
    return;
}
void eval ( char * cmdline ) { char * argv [ MAXARGS ] ; char buf [ MAXLINE ] ; strcpy ( buf , cmdline ) ; sigset_t sig_set ; sigemptyset ( & sig_set ) ; sigaddset ( & sig_set , SIGCHLD ) ; int bg ; bg = parseline ( buf , argv ) ; dprintf ( " \n " , cmdline ) ; if ( argv [ 0 ] == NULL ) return ; else if ( ! builtin_cmd ( argv ) ) { dprintf ( " \n " ) ; sigprocmask ( SIG_BLOCK , & sig_set , 0 ) ; pid_t pid = fork ( ) ; if ( pid < 0 ) { printf ( " \n " ) ; return ; } if ( pid == 0 ) { dprintf ( " \n " ) ; setpgid ( 0 , 0 ) ; dprintf ( " \n " ) ; if ( execvp ( argv [ 0 ] , argv ) < 0 ) { printf ( " \n " , argv [ 0 ] ) ; exit ( 0 ) ; } } else { dprintf ( " \n " ) ; if ( bg ) { addjob ( jobs , pid , BG , cmdline ) ; printf ( " " , pid2jid ( pid ) , pid , cmdline ) ; } else { addjob ( jobs , pid , FG , cmdline ) ; } sigprocmask ( SIG_UNBLOCK , & sig_set , 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 .
// variables // copy cmdline to buffer // have to set sigset to control SIGCHLD // parseline // send argv to buildin_cmd func, if false, handle it // argv is not a buildin func // fork to run it in child process // child process // parent process // waits child process // process is in background // for printing same as ref // process is in foreground // set SIGCHLD free