ast_comparator/test/student_data/handout/shlab-handout/cases/36_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)
{
  if(strlen(cmdline) <= 1)
    return;
  char *argv[MAXLINE];
  int isbg = parseline(cmdline, argv);
  if(!builtin_cmd(argv))
  {
    int pid = Fork();
    if(pid == 0){
      Setpgid(getpid(), getpid());
      if(execv(argv[0], argv) == -1){
        cmdline[strlen(cmdline) - 1] = '\0';
        printf("%s: Command not found\n", cmdline);
        exit(0);
      }
    }
    else{
      if(isbg){
        if(addjob(jobs, pid, BG, cmdline) == 0)
          return;
        int jid = getjobpid(jobs, pid)->jid;
        printf("[%d] (%d) %s", jid, pid, cmdline);
        fflush(stdout);
      }
      else{
        if(addjob(jobs, pid, FG, cmdline) == 0)
          return;
        waitfg(pid);
      }
    }
  }
  return;
}
void eval ( char * cmdline ) { if ( strlen ( cmdline ) <= 1 ) return ; char * argv [ MAXLINE ] ; int isbg = parseline ( cmdline , argv ) ; if ( ! builtin_cmd ( argv ) ) { int pid = Fork ( ) ; if ( pid == 0 ) { Setpgid ( getpid ( ) , getpid ( ) ) ; if ( execv ( argv [ 0 ] , argv ) == -1 ) { cmdline [ strlen ( cmdline ) - 1 ] = ' \0 ' ; printf ( " \n " , cmdline ) ; exit ( 0 ) ; } } else { if ( isbg ) { if ( addjob ( jobs , pid , BG , cmdline ) == 0 ) return ; int jid = getjobpid ( jobs , pid ) -> jid ; printf ( " " , jid , pid , cmdline ) ; fflush ( stdout ) ; } else { if ( addjob ( jobs , pid , FG , cmdline ) == 0 ) return ; 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 .
/* return if no argument. compared with 1 because of the fact that cmdline includes newline character */ /* Child process */ /* set process group different from shell process */ /* execute command line */ /* eliminating newline character */ /* Parent process */