ast_comparator/test/student_data/handout/shlab-handout/cases/2_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];
  int bg;
  pid_t pid;
  sigset_t *mask = NULL;
  bg = parseline(cmdline, argv);
  if (!builtin_cmd(argv)) {
    sigemptyset(mask);
    sigaddset(mask, SIGCHLD);
    sigprocmask(SIG_BLOCK, mask, NULL);
    if (!(pid = fork())) {
      setpgid(0, 0);
      sigprocmask(SIG_UNBLOCK, mask, NULL);
      if (execvp(argv[0], argv) < 0) {
        printf("%s: Command not found.\n", argv[0]);
        exit(1);
      }
    }
    else {
      if (bg) {
        addjob(jobs, pid, BG, cmdline);
        sigprocmask(SIG_UNBLOCK, mask, NULL);
        printf("[%d] (%d) %s", pid2jid(pid), pid, cmdline);
      }
      else {
        addjob(jobs, pid, FG, cmdline);
        sigprocmask(SIG_UNBLOCK, mask, NULL);
        waitfg(pid);
      }
    }
  }
}
void eval ( char * cmdline ) { char * argv [ MAXARGS ] ; int bg ; pid_t pid ; sigset_t * mask = NULL ; bg = parseline ( cmdline , argv ) ; if ( ! builtin_cmd ( argv ) ) { sigemptyset ( mask ) ; sigaddset ( mask , SIGCHLD ) ; sigprocmask ( SIG_BLOCK , mask , NULL ) ; if ( ! ( pid = fork ( ) ) ) { setpgid ( 0 , 0 ) ; sigprocmask ( SIG_UNBLOCK , mask , NULL ) ; if ( execvp ( argv [ 0 ] , argv ) < 0 ) { printf ( " \n " , argv [ 0 ] ) ; exit ( 1 ) ; } } else { if ( bg ) { addjob ( jobs , pid , BG , cmdline ) ; sigprocmask ( SIG_UNBLOCK , mask , NULL ) ; printf ( " " , pid2jid ( pid ) , pid , cmdline ) ; } else { addjob ( jobs , pid , FG , cmdline ) ; sigprocmask ( SIG_UNBLOCK , mask , NULL ) ; waitfg ( pid ) ; } } } }
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 .
/* parse command line */ /* run command */ /* block SIGCHLD signal */ /* child unblocks SIGCHILD signal and runs requested command */ /* parent adds job and unblocks SIGCHILD signal */