ast_comparator/test/student_data/handout/shlab-handout/cases/prev_13_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 sigs; 
	strcpy(buf, cmdline);
	bg = parseline(buf, argv); 
	if(argv[0] == NULL) return; 
	if(sigemptyset(&sigs) < 0){ 
		unix_error("Cannot make empty signalset");
	}
	if(sigaddset(&sigs, SIGCHLD) < 0){ 
		unix_error("Cannot add SIGCHLD on signalset");
	}
	if(!builtin_cmd(argv)){ 
		if(sigprocmask(SIG_BLOCK, &sigs, NULL) < 0){ 
			unix_error("Cannot block signals in signalset");
		}
		if((pid = fork()) < 0){
			unix_error("Error on fork when eval");
		}
		else if(pid == 0){
			if(sigprocmask(SIG_UNBLOCK, &sigs, NULL) < 0){ 
				unix_error("Cannot unblock signals in signalset");
			}
			if(setpgid(0, 0) < 0){
				unix_error("cannot split any other processes by setpgid");
			}; 
			if(execve(argv[0], argv, environ) < 0){
				printf("%s: Command not found\n", argv[0]);
				exit(0);
			}
		}
		else{
			if(!bg){ 
				addjob(jobs, pid, FG, cmdline);
				if(sigprocmask(SIG_UNBLOCK, &sigs, NULL) < 0){
					unix_error("Cannot unblock signals in signalset");
				}
				waitfg(pid);
			}
			else{ 
				addjob(jobs, pid, BG, cmdline);
				if(sigprocmask(SIG_UNBLOCK, &sigs, NULL) < 0){
					unix_error("Cannot unblock signals in signalset");
				}
				printf("[%d] (%d) %s", pid2jid(pid), pid, cmdline);
			}
		}
	}
}
void eval ( char * cmdline ) { char * argv [ MAXARGS ] ; char buf [ MAXLINE ] ; int bg ; pid_t pid ; sigset_t sigs ; strcpy ( buf , cmdline ) ; bg = parseline ( buf , argv ) ; if ( argv [ 0 ] == NULL ) return ; if ( sigemptyset ( & sigs ) < 0 ) { unix_error ( " " ) ; } if ( sigaddset ( & sigs , SIGCHLD ) < 0 ) { unix_error ( " " ) ; } if ( ! builtin_cmd ( argv ) ) { if ( sigprocmask ( SIG_BLOCK , & sigs , NULL ) < 0 ) { unix_error ( " " ) ; } if ( ( pid = fork ( ) ) < 0 ) { unix_error ( " " ) ; } else if ( pid == 0 ) { if ( sigprocmask ( SIG_UNBLOCK , & sigs , NULL ) < 0 ) { unix_error ( " " ) ; } if ( setpgid ( 0 , 0 ) < 0 ) { unix_error ( " " ) ; } ; if ( execve ( argv [ 0 ] , argv , environ ) < 0 ) { printf ( " \n " , argv [ 0 ] ) ; exit ( 0 ) ; } } else { if ( ! bg ) { addjob ( jobs , pid , FG , cmdline ) ; if ( sigprocmask ( SIG_UNBLOCK , & sigs , NULL ) < 0 ) { unix_error ( " " ) ; } waitfg ( pid ) ; } else { addjob ( jobs , pid , BG , cmdline ) ; if ( sigprocmask ( SIG_UNBLOCK , & sigs , NULL ) < 0 ) { unix_error ( " " ) ; } printf ( " " , pid2jid ( pid ) , pid , cmdline ) ; } } } }
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 .
/* Note. Since fork to make child process is the most important work, 
										any signals have to be blocked until fork ends. */ /* parse. */ /* It means just enter. Ignore. */ /* make empty signalset. */ /* add SIGCHLD on empty signalset. */ /* if(sigaddset(&sigs, SIGINT) < 0){ // Reference says SIGCHLD has to be blocked, but I also temporarily added SIGINT, SIGTSTP. 
		unix_error("Cannot add SIGINT on signalset");
	}
	if(sigaddset(&sigs, SIGTSTP) < 0){
		unix_error("Cannot add SIGTSTP on signalset");
	} */ // Since builtin_cmd returns 1 iff it is a builtin command, ! is required. /* First, block SIGCHLD signals when execute builtin_cmd */ /* block signals in signalset, in this case, SIGCHLD */ /* block SIGCHLD success */ /* unblock sigs */ /* Fowllows reference. */ /* all processes have to be add on joblist with pid, 
									and by reference, cmdline also have to be added. */ /* Since SIGCHLD works when CPU time is given to parent process, 
					 * SIGCHLD have to be unblocked even here is parent side. */ /* parent process doesn't wait any background process. 
							 Just print it's information. */ /* Note that cmdline leads print \n on command line, so don't put \n. */