ast_comparator/test/student_data/handout/shlab-handout/cases/reference_28_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 help with programming assignment  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 help with programming assignment  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 help with programming assignment 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) 
{
  int bg;
  char *argv[MAXARGS];
  sigset_t set;
  bg = parseline(cmdline, argv);
  if (argv[0] == NULL) return; 
  if (builtin_cmd(argv)) {
     return;
  }
  sigemptyset(&set);
  sigaddset(&set, SIGCHLD);
  sigprocmask(SIG_BLOCK, &set, NULL);
  Signal(SIGTTOU, SIG_IGN);
  Signal(SIGTTIN, SIG_IGN);
  pid_t child = fork();
  if (child == -1) {
    printf("Error: could not fork child process.\n");
    return;
  }
  if (child > 0) {
    addjob(jobs, child, bg ? BG : FG, cmdline);
    if (bg) {
      struct job_t *job = getjobpid(jobs, child);
      printf("[%d] (%d) %s", job->jid, job->pid, cmdline);
    }
    sigemptyset(&set);
    sigaddset(&set, SIGCHLD);
    sigprocmask(SIG_UNBLOCK, &set, NULL);
    if (!bg) {
      waitfg(child);
    }
  } else {
    setpgid(0, 0);
    sigemptyset(&set);
    sigaddset(&set, SIGCHLD);
    sigprocmask(SIG_UNBLOCK, &set, NULL);
    if (execvp(argv[0], argv) < 0) {
      printf("%s: Command not found.\n", argv[0]);
      exit(-1);
    }
  }
}
void eval ( char * cmdline ) { int bg ; char * argv [ MAXARGS ] ; sigset_t set ; bg = parseline ( cmdline , argv ) ; if ( argv [ 0 ] == NULL ) return ; if ( builtin_cmd ( argv ) ) { return ; } sigemptyset ( & set ) ; sigaddset ( & set , SIGCHLD ) ; sigprocmask ( SIG_BLOCK , & set , NULL ) ; Signal ( SIGTTOU , SIG_IGN ) ; Signal ( SIGTTIN , SIG_IGN ) ; pid_t child = fork ( ) ; if ( child == -1 ) { printf ( " \n " ) ; return ; } if ( child > 0 ) { addjob ( jobs , child , bg ? BG : FG , cmdline ) ; if ( bg ) { struct job_t * job = getjobpid ( jobs , child ) ; printf ( " " , job -> jid , job -> pid , cmdline ) ; } sigemptyset ( & set ) ; sigaddset ( & set , SIGCHLD ) ; sigprocmask ( SIG_UNBLOCK , & set , NULL ) ; if ( ! bg ) { waitfg ( child ) ; } } else { setpgid ( 0 , 0 ) ; sigemptyset ( & set ) ; sigaddset ( & set , SIGCHLD ) ; sigprocmask ( SIG_UNBLOCK , & set , NULL ) ; if ( execvp ( argv [ 0 ] , argv ) < 0 ) { printf ( " \n " , argv [ 0 ] ) ; exit ( -1 ) ; } } }
eval - Evaluate the command line that the user has just typed in
eval - Evaluate the command line that the user has just typed in
/* the following code demonstrates how to use parseline --- you'll 
   * want to replace most of help with programming homework  it (at least the print statements). */ /* ignore empty lines */ /* try to execute as a builtin-cmd */ /* mask SIGCHLD before create the process */ /* disable SIGTTOUT and make my programming assignment  SIGTTIN for job controls*/ /* fork a child process to execute it */ /* parent process. add to job list and unblock SIGCHLD */ /* display information about the job */ /* if not bg, wait until it's not the foreground
     * process */ /* child process. create a new group for this process and 
     * unmark the SIGCHLD */ /* execute the program in the child process*/