ast_comparator/test/student_data/handout/shlab-handout/cases/58_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)
{
  sigset_t sigset;
  sigemptyset(&sigset);
  sigaddset(&sigset, SIGCHLD);
  char *argv[MAXARGS];
  pid_t pid;
  struct job_t *job;
  int bg=parseline(cmdline, argv);
  if (argv[0]==NULL) 	
    return;
  if (!builtin_cmd(argv)) {
    if (sigprocmask(SIG_BLOCK, &sigset,NULL)<0)		
      unix_error("sigprocmask");
    if ((pid=fork())<0)					
      unix_error("fork");
    if (pid&&bg) {
      if (addjob(jobs, pid, BG, cmdline)==0)
        app_error("addjob");
      if (sigprocmask(SIG_UNBLOCK, &sigset,NULL)<0) 	
        unix_error("sigprocmask");
      if ((job=getjobpid(jobs, pid))==0) {
        printf("No such job\n");
	return;
	}
      printf("[%i] (%i) %s",job->jid, job->pid, job->cmdline);
    } else if (pid){
      if (addjob(jobs, pid, FG, cmdline)==0)
        app_error("addjob\n");
      if (sigprocmask(SIG_UNBLOCK, &sigset,NULL)<0)	
        unix_error("sigprocmask");
      waitfg(pid);
    } else {
      setpgid(0, 0);	
      if (sigprocmask(SIG_UNBLOCK, &sigset,NULL)<0)	
        unix_error("sigprocmask: ");
      if (execve(argv[0], argv, environ)<0)		
        printf("%s: Command not found\n",argv[0]);
	exit(0);
    }
  }
  return;
}
void eval ( char * cmdline ) { sigset_t sigset ; sigemptyset ( & sigset ) ; sigaddset ( & sigset , SIGCHLD ) ; char * argv [ MAXARGS ] ; pid_t pid ; struct job_t * job ; int bg = parseline ( cmdline , argv ) ; if ( argv [ 0 ] == NULL ) return ; if ( ! builtin_cmd ( argv ) ) { if ( sigprocmask ( SIG_BLOCK , & sigset , NULL ) < 0 ) unix_error ( " " ) ; if ( ( pid = fork ( ) ) < 0 ) unix_error ( " " ) ; if ( pid && bg ) { if ( addjob ( jobs , pid , BG , cmdline ) == 0 ) app_error ( " " ) ; if ( sigprocmask ( SIG_UNBLOCK , & sigset , NULL ) < 0 ) unix_error ( " " ) ; if ( ( job = getjobpid ( jobs , pid ) ) == 0 ) { printf ( " \n " ) ; return ; } printf ( " " , job -> jid , job -> pid , job -> cmdline ) ; } else if ( pid ) { if ( addjob ( jobs , pid , FG , cmdline ) == 0 ) app_error ( " \n " ) ; if ( sigprocmask ( SIG_UNBLOCK , & sigset , NULL ) < 0 ) unix_error ( " " ) ; waitfg ( pid ) ; } else { setpgid ( 0 , 0 ) ; if ( sigprocmask ( SIG_UNBLOCK , & sigset , NULL ) < 0 ) unix_error ( " " ) ; if ( execve ( argv [ 0 ] , argv , environ ) < 0 ) printf ( " \n " , argv [ 0 ] ) ; exit ( 0 ) ; } } 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 .
/* creating signal set to block SIGCHLD later on*/ //int status; /* ignoring empty command line */ /* blocking SIGCHLD */ /* creating child*/ /* parent process in case of background execution */ /* unblocking SIGCHLD */ /* parent process in case of forground execution */ /* unblocking SIGCHLD */ /* child process*/ /* creating new process group for SIGINT proper utilization */ /* unblocking SIGCHLD */ /* running called programm */