ast_comparator/test/student_data/handout/shlab-handout/cases/24_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];	
  char buf[MAXLINE];	
  int bg;				
  pid_t pid;			
  sigset_t sigset;		
  strcpy(buf, cmdline);
  bg = parseline(buf, argv);
  if(argv[0] == NULL) return;	
  if(!builtin_cmd(argv)){
	if(sigemptyset(&sigset) != 0)				   
		unix_error("Sigemptyset error");		   
	if(sigaddset(&sigset, SIGCHLD) != 0)	   	   
		unix_error("Sigaddset error");			   
	if(sigprocmask(SIG_BLOCK, &sigset, NULL) != 0) 
		unix_error("Sigprocmask block error");	   
	if((pid = fork()) < 0)		
		unix_error("Fork error");
	else if(pid == 0){			
		if(sigprocmask(SIG_UNBLOCK, &sigset, NULL) != 0) 
			unix_error("Sigprocmask unblock error");
		if(setpgid(0, 0) < 0) 	
			unix_error("Setpgid error");
		if(execve(argv[0], argv, environ) < 0){	
			printf("%s: Command not found\n", argv[0]);	
			exit(0);
		}
	}
	if(!bg){		
		addjob(jobs, pid, FG, cmdline);
		if(sigprocmask(SIG_UNBLOCK, &sigset, NULL) != 0) 
			unix_error("Sigprocmask unblock error");
		waitfg(pid);
	}else{			
		addjob(jobs, pid, BG, cmdline);
		if(sigprocmask(SIG_UNBLOCK, &sigset, NULL) != 0) 
			unix_error("Sigprocmask unblock error");
		printf("[%d] (%d) %s", getjobpid(jobs, pid)->jid, pid, cmdline);
	}
  }
  return;
}
void eval ( char * cmdline ) { char * argv [ MAXARGS ] ; char buf [ MAXLINE ] ; int bg ; pid_t pid ; sigset_t sigset ; strcpy ( buf , cmdline ) ; bg = parseline ( buf , argv ) ; if ( argv [ 0 ] == NULL ) return ; if ( ! builtin_cmd ( argv ) ) { if ( sigemptyset ( & sigset ) != 0 ) unix_error ( " " ) ; if ( sigaddset ( & sigset , SIGCHLD ) != 0 ) unix_error ( " " ) ; if ( sigprocmask ( SIG_BLOCK , & sigset , NULL ) != 0 ) unix_error ( " " ) ; if ( ( pid = fork ( ) ) < 0 ) unix_error ( " " ) ; else if ( pid == 0 ) { if ( sigprocmask ( SIG_UNBLOCK , & sigset , NULL ) != 0 ) unix_error ( " " ) ; if ( setpgid ( 0 , 0 ) < 0 ) unix_error ( " " ) ; if ( execve ( argv [ 0 ] , argv , environ ) < 0 ) { printf ( " \n " , argv [ 0 ] ) ; exit ( 0 ) ; } } if ( ! bg ) { addjob ( jobs , pid , FG , cmdline ) ; if ( sigprocmask ( SIG_UNBLOCK , & sigset , NULL ) != 0 ) unix_error ( " " ) ; waitfg ( pid ) ; } else { addjob ( jobs , pid , BG , cmdline ) ; if ( sigprocmask ( SIG_UNBLOCK , & sigset , NULL ) != 0 ) unix_error ( " " ) ; printf ( " " , getjobpid ( jobs , pid ) -> jid , 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 .
/* Argument list execve() */ /* Holds modified command line */ /* Should the job run in bg or fg? */ /* Process id */ /* What signal will be blocked */ /* Ignore empty lines */ /* Delete all content of signal set */ /* error check */ /* Add to signal set what will be blocked */ /* error check */ /* Block signal in sigset(SIGCHLD) */ /* error check */ /* Check fork error */ /* Child runs user job */ /* Unblock signal in sigset(SIGCHLD) */ /* For manage tsh and foreground process separately */ /* exec foreground job */ /* In argv, there is invalid path */ /* Parent waits for foreground job to terminate */ /* foreground job */ /* Unblock signal in sigset after addjob */ /* Wait until foreground job finished. foreground job can exist only one */ /* background job */ /* Unblock signal in sigset after addjob */