ast_comparator/test/student_data/handout/shlab-handout/cases/prev_14_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.
 *
 * The eval function first parses the buffer, then checks if it is a builtin_cmd.
 * if it is, then it simply carries out the function. Else, it forks and execs the
 * specified program in the background or the foreground. The action differs for them.
 * We'll have to wait for the foregound job to finish, and we'll have to print out
 * the information of the background job.
 */
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.

The eval function first parses the buffer, then checks if it is a builtin_cmd.
if it is, then it simply carries out the function. Else, it forks and execs the
specified program in the background or the foreground. The action differs for them.
We'll have to wait for the foregound job to finish, and we'll have to print out
the information of the background job.
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 . The eval function first parses the buffer then checks if it is a builtin_cmd . if it is then it simply carries out the function . Else it forks and execs the specified program in the background or the foreground . The action differs for them . We ' ll have to wait for the foregound job to finish and we ' ll have to print out the information of the background job .
void eval(char *cmdline)
{ 
  char *argv[MAXARGS]; 
  int isbg; 
  pid_t pid; 
  struct job_t *job; 
  sigset_t mask; 
  isbg = parseline(cmdline, argv); 
  if(argv[0] == NULL) return; 
  if(!builtin_cmd(argv)) { 
#ifdef DEBUG
printf("--fork & exec process! (not built-in)\n");
#endif
    sigemptyset(&mask);
    sigaddset(&mask, SIGCHLD);
    sigprocmask(SIG_BLOCK, &mask, NULL);
    if((pid = fork()) == 0) { 
      sigprocmask(SIG_UNBLOCK, &mask, NULL);
      setpgid(0, 0);
      if (execve(argv[0], argv, environ) < 0){ 
        printf("%s: Command not found\n", argv[0]);
        exit(0);
      }
    } 
    if(!addjob(jobs, pid, isbg ? BG : FG, cmdline)) { 
      return; 
    }
    sigprocmask(SIG_UNBLOCK, &mask, NULL);
    if(!isbg) { 
#ifdef DEBUG
printf("foreground job");
#endif
      waitfg(pid); 
    } else { 
      job = getjobpid(jobs, pid); 
      printf("[%d] (%d) %s", job->jid, job->pid, cmdline);
    }
  }
  return;
}
void eval ( char * cmdline ) { char * argv [ MAXARGS ] ; int isbg ; pid_t pid ; struct job_t * job ; sigset_t mask ; isbg = parseline ( cmdline , argv ) ; if ( argv [ 0 ] == NULL ) return ; if ( ! builtin_cmd ( argv ) ) { #ifdef DEBUG printf ( " \n " ) ; #endif sigemptyset ( & mask ) ; sigaddset ( & mask , SIGCHLD ) ; sigprocmask ( SIG_BLOCK , & mask , NULL ) ; if ( ( pid = fork ( ) ) == 0 ) { sigprocmask ( SIG_UNBLOCK , & mask , NULL ) ; setpgid ( 0 , 0 ) ; if ( execve ( argv [ 0 ] , argv , environ ) < 0 ) { printf ( " \n " , argv [ 0 ] ) ; exit ( 0 ) ; } } if ( ! addjob ( jobs , pid , isbg ? BG : FG , cmdline ) ) { return ; } sigprocmask ( SIG_UNBLOCK , & mask , NULL ) ; if ( ! isbg ) { #ifdef DEBUG printf ( " " ) ; #endif waitfg ( pid ) ; } else { job = getjobpid ( jobs , pid ) ; printf ( " " , job -> jid , job -> pid , cmdline ) ; } } 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 .
//TODO1 // to save the arguments from the cmdline // to see if the parsed job is a background job //pid of the forked job (!builtin_cmd) //the forked job (!builtin_cmd) //for receiving signals outside terminal // parse the cmdline // if empty lines, return // if not built in, fork and exec in a child process // work for receiving signals outside terminal // in the child process // < 0 means it couldn't find the given command // add the job on the list // if couldn't add the job, return //it's a foreground job // wait for it to finish //if it is a background job, print a status messsage /* [1] (10113) ./myspin 1 & */ // get the job for printing