示例#1
0
文件: tsh.c 项目: pwwpche/ICS-SE12
/*
 * sigtstp_handler - The kernel sends a SIGTSTP to the shell whenever
 *     the user types ctrl-z at the keyboard. Catch it and suspend the
 *     foreground job by sending it a SIGTSTP.
 */
void sigtstp_handler(int sig)
{
    pid_t fg_pid = fgpid(jobs);
    if(fg_pid == 0)
        return ;
#ifdef DEBUG_SIGTSTP
    printf("--------------SIGTSTP-----------------\n");
    printf("fg_pid = %d\n",fg_pid);
    listjobs(jobs);

#endif

	struct job_t* fg_job = getjobpid(jobs, fg_pid);
	printf("Job [%d] (%d) stopped by signal %d\n", fg_job->jid, fg_job->pid, sig);
	fg_job->state = ST;
	kill(-fg_pid, SIGTSTP);

#ifdef DEBUG_SIGTSTP
    printf("after kill");
    listjobs(jobs);
    printf("--------------------------------------\n");
#endif
    return;

}
示例#2
0
文件: tsh.c 项目: pwwpche/ICS-SE12
/*
 * sigchld_handler - The kernel sends a SIGCHLD to the shell whenever
 *     a child job terminates (becomes a zombie), or stops because it
 *     received a SIGSTOP or SIGTSTP signal. The handler reaps all
 *     available zombie children, but doesn't wait for any other
 *     currently running children to terminate.
 */
void sigchld_handler(int sig)
{
#ifdef DEBUG_SIGTSTP
    printf("--------------SIGCHLD-----------------\n");
    listjobs(jobs);
fflush(stdout);
#endif
    pid_t pid;
    int status;
    while((pid = waitpid(-1, &status, WNOHANG|WUNTRACED)) > 0){
#ifdef DEBUG_SIGTSTP
    printf("pid = %d\n", pid);
    printf("WIFEXITED(status) = %d\n",WIFEXITED(status));
#endif
        if(WIFSTOPPED(status)){
            getjobpid(jobs, pid)->state = ST;
            continue;
        }
        deletejob(jobs, pid);
    }
#ifdef DEBUG_SIGTSTP
    printf("pid=%d\n",pid);
    listjobs(jobs);
    printf("---------------------------------------\n");
    fflush(stdout);
#endif
    return;
}
/*
 * builtin_cmd - If the user has typed a built-in command then execute
 *    it immediately.
 */
int builtin_cmd(struct cmdline_tokens cmd_tokens) {
    int i;
    char **argv;
    /**
     * NOTE:
     * Based on tutorial the cmd_tokens were to be passed to builtin_cmd
     * But earlier argv was being passed.
     * Still using argv but getting that from cmd_tokens instead of passing to the function directly
     */
    int fd; // File discriptor


    switch (cmd_tokens.builtins) {
        case BUILTIN_QUIT:
        // Quit Command
            exit(0);
            return 1;
            break;
        case BUILTIN_BG: // Command background
        case BUILTIN_FG: // Command foreground
            argv = cmd_tokens.argv;     // Getting argv here
            if (argv[1] == NULL) {
                printf("There are no arguments!");
            } else {
                i = 0;
                if (argv[1][0] == '%') {
                    i = 1;
                }
                while (argv[1][i] != '\0') {
                    if (!isdigit(argv[1][i])) {
                        break;
                    }
                    ++i;
                }
                if (argv[1][i] == '\0') {
                    do_bgfg(argv);
                } else {
                    printf("%s: argument must be a PID or %%jobid\n", argv[0]);
                }
            }
            return 1;
            break;
        case BUILTIN_JOBS:
            if (cmd_tokens.outfile) {
                fd = open(cmd_tokens.outfile, O_WRONLY, 0);
                listjobs(jobs, fd);
                close(fd);
            } else {
                listjobs(jobs, STDOUT_FILENO);
            }
            return 1; /* Builtin command */
            break;
        default: return 0; /* Not a builtin command */
    }
}
示例#4
0
/* builtin_jobs_handler - List the running and stopped background jobs */
void builtin_jobs_handler(struct cmdline_tokens *tok) {
    int fd;

    if (tok -> outfile != NULL) {
        if ((fd = Open(tok -> outfile, O_CREAT | O_WRONLY, DEF_MODE)) > 0) {
            listjobs(job_list, fd);
            Close(fd);
        }
    } else {
        listjobs(job_list, STDOUT_FILENO);
    }
}
/*
 * eval helper function
 * This function can handle the built-in commands (quit, jobs, bg, fg, kill)
 * If it is not a built-in command, it will return 0. Otherwise, it will 
 * return 1. There are some smaller helper functions used to handle each
 * built-in command. For example, dobg(), dofg() and so on.
 */
int builtinCommand(struct cmdline_tokens tok)
{
    if (!strcmp(tok.argv[0], "quit")) {
        exit(0);
    }
    if (!strcmp(tok.argv[0], "jobs")) {
        listjobs(job_list, getfd(OUT, tok.outfile)); 
        return 1;
    }
    if (!strcmp(tok.argv[0], "bg")) {
        dobg(tok.argv);
        return 1;
    }
    if (!strcmp(tok.argv[0], "fg")) {
        dofg(tok.argv);
        return 1;
    }
    if (!strcmp(tok.argv[0], "kill")) {
        dokill(tok.argv);
        return 1;
    }
    if (!strcmp(tok.argv[0], "&")) {
        return 1;
    }
    return 0;
}
示例#6
0
文件: tsh.c 项目: sigmaxz/hw-projects
/*
 * builtin_cmd - If the user has typed a built-in command then execute
 * it immediately.
 */
int builtin_cmd(char **argv)
{
	//	printf("fK:%s : %s \n",argv[0],argv[2]);
                if(strcmp(argv[0], "quit")==0)
                {
                        exit(0); // exits the shell
                        return 1;
                }
                else if(strcmp(argv[0], "bg")==0)
                {
                        do_bgfg(argv);
                        return 1;
                }
                else if(strcmp(argv[0], "fg")==0)
                {
                        do_bgfg(argv);
                        return 1;
                }
                else if(strcmp(argv[0], "jobs")==0)
                {
                        listjobs(jobs);
                        return 1;
                }
                else{
                        return 0;
                }
                
                return 0; // never makes it here
}
示例#7
0
文件: tsh.c 项目: hexinatgithub/CSAPP
/* 
 * builtin_cmd - If the user has typed a built-in command then execute
 *    it immediately.  
 */
int builtin_cmd(char **argv) 
{
    if (!strcmp(argv[0], "quit"))
    {
        exit(0);
    }
    if (!strcmp(argv[0], "jobs"))
    {
        listjobs(jobs);
        return 1;
    }
    if ((!strcmp(argv[0], "bg") || !strcmp(argv[0], "fg")) && (argv[1] != NULL))
    {
        do_bgfg(argv);
        return 1;
    }
    if (!strcmp(argv[0], "kill"))
    {
        pid_t pid = atoi(argv[1]);
        kill(-pid, SIGKILL);
        if (getjobpid(jobs, pid) != NULL) 
        {
            if (waitpid(pid, NULL, 0) < 0)
                unix_error("kill child process error");
            deletejob(jobs, pid);
        }
        return 1;
    }
    return 0;     /* not a builtin command */
}
示例#8
0
/* 
 * builtin_cmd - If the user has typed a built-in command then execute
 *    it immediately.  
 */
int builtin_cmd(char **argv) 
{
    //list jobs for "jobs" command
    if(!strcmp(argv[0], "jobs"))
    {
        listjobs(jobs);
        return 1;
    }

    //calling do_bgfg if built-in commands are fg or bg
    else if(!strcmp(argv[0], "fg") || !strcmp(argv[0], "bg"))
    {
        do_bgfg(argv);
        return 1;
    }

    //exit shell if command is quit
    else if(!strcmp(argv[0], "quit"))
    {
        exit(0);
    }

    //run process in background if & is parsed as command
    else if(!strcmp(argv[0], "&"))
    {
        return 1;
    }

    return 0;     /* not a builtin command */
}
示例#9
0
文件: tsh.c 项目: protos37/splab
/* 
 * builtin_cmd - If the user has typed a built-in command then execute
 *	it immediately.  
 */
int builtin_cmd(char **argv) 
{
	if(strcmp(argv[0], "quit") == 0)
	{
		// quit command
		exit(0);
	}
	else if(strcmp(argv[0], "jobs") == 0)
	{
		// jobs command
		listjobs(jobs);
	}
	else if(strcmp(argv[0], "bg") == 0 || strcmp(argv[0], "fg") == 0)
	{
		// bg or fg command
		do_bgfg(argv);
	}
	else
	{
		// not a builtin command
		return 0;
	}
	// it was a builtin command
	return 1;
}
示例#10
0
文件: tsh.c 项目: shilparoys/CS160
/* 
 * builtin_cmd - If the user has typed a built-in command then execute
 *    it immediately.  
 * the builtin_cmd include quit, jobs, bg <job>, and fg <job>
 */
int builtin_cmd(char **argv) 
{
	//quit
	if( !strcmp(argv[0], "quit")){
		exit(0);
	}
	//jobs
	else if (!strcmp(argv[0], "jobs")){
		listjobs(jobs);
		return 1;
	}
	//bg
	else if(!strcmp(argv[0], "bg")){
		do_bgfg(argv);
		return 1;
	}
	//fg
	else if(!strcmp(argv[0], "fg")){
		do_bgfg(argv);
		return 1;
	}
	else{
    		return 0;     /* not a builtin command */
	}
}
示例#11
0
文件: tsh.c 项目: vaibhav-495/Shell
/* 
 * builtin_cmd - If the user has typed a built-in command then execute
 *    it immediately.  
 */
int builtin_cmd(char **argv) 
{
	int r=0;
	char* p;
        int jobid;
   	if(strcmp(argv[0],"quit")==0)
	{
		r++;
		exit(0);
	}
	else if(strcmp(argv[0],"jobs")==0)
        {
	 	r++;
	        listjobs(jobs);
	}
	else if((strcmp(argv[0],"bg")==0)||(strcmp(argv[0],"fg")==0))
        {
		r++;
		do_bgfg(argv);
	}
	else if(strcmp(argv[0],"kill")==0)
	{
        	r++;
                p = strstr(argv[1],"%");
                p++;
                jobid=atoi(p);     //Since jobid is specified as %5 for job 5 we get 5 from argv[1].
                jobid=getjobjid(jobs,jobid)->pid;
                kill(jobid,SIGKILL);
                deletejob(jobs,jobid);
	}
return r;     /* not a builtin command */
}
示例#12
0
/* 
 * builtin_cmd - If the user has typed a built-in command then execute
 *    it immediately.  
 */
int builtin_cmd(char **argv) 
{
    
    if(!strcmp(argv[0], "quit")){
        int i = 0;
        for (i = 0; i < MAXJOBS; i++)       /*If any job is in stopped state then shell will not quit.*/
            if (jobs[i].state == ST)
            {
                printf("There is stopped job.\n");
                return 1;
            }
        exit(0);
    }
    if(!strcmp(argv[0], "jobs")){
        listjobs(jobs);
        return 1;}
    if((!strcmp(argv[0], "bg"))||(!strcmp(argv[0], "fg"))){
        do_bgfg(argv);
        return 1;}
    if(!strcmp(argv[0], "&"))    
        return 1;
    
    return 0;           /* not a builtin command */

    
    
}
示例#13
0
文件: tsh.c 项目: anamika2910/IT215
		/*
		 * builtin_cmd - If the user has typed a built-in command then execute
		 *    it immediately.
		 */
		int builtin_cmd(char **argv)
		{
			//if(verbose)
			//printf("%s\n",argv[0]);
			/*builtin cmd quit*/
			if(!(strcmp(argv[0],"quit"))){
				exit(0);
			}
			/*builtin cmd jobs prints the currently running and stopped jobs*/
			else if(!(strcmp(argv[0],"jobs"))){
				listjobs(jobs);
				return 1;
			}
			/*builtin cmd fg for converting a process to foreground*/
			else if(!(strcmp(argv[0],"fg"))){
				do_bgfg(argv);
				return 1;
			}
			/*builtin cmd bg for converting a process to background*/
			else if(!(strcmp(argv[0],"bg"))){
				do_bgfg(argv);	
				return 1;
			}
			 /* not a builtin command */
			else {
		    return 0;
		    }   
		}
示例#14
0
文件: util.c 项目: heapsters/shelldon
/*
 * builtin_cmd - If the user has typed a built-int
 *    command then execute it immediately.
 *    quit, fg, bg, jobs
 */
int builtin_cmd(char **argv)
{
    char *cmd = argv[0];
    sigset_t mask, prev;

    /* Blocks all signals while determining
     * whether command is built in
     */
     Sigfillset(&mask);
     Sigprocmask(SIG_BLOCK, &mask, &prev);

    if (!strcmp(cmd, "quit")) {
        Sigprocmask(SIG_SETMASK, &prev, NULL);
        exit(0);
    }
    if (!strcmp(cmd, "jobs")) {
        listjobs(jobs);
        Sigprocmask(SIG_SETMASK, &prev, NULL);
        return 1;
    }
    if (!strcmp(cmd, "bg") || !strcmp(cmd, "fg")) {
        Sigprocmask(SIG_SETMASK, &prev, NULL);
        do_bgfg(argv);
        return 1;
    }

    Sigprocmask(SIG_SETMASK, &prev, NULL);
    return 0;
}
示例#15
0
文件: tsh.c 项目: MyMiracle/Shell-Lab
void builtinjobs(struct cmdline_tokens *tok){
  int fd = STDOUT_FILENO;
  if (tok->outfile!=NULL){
    if ((fd = open(tok->outfile, O_WRONLY)) < 0)
      return;
  }
  listjobs(job_list,fd);
  return;
}
示例#16
0
文件: tsh.c 项目: YurieCo/csapp-2
/*
 * execute the jobs builtin command
 */
void
exec_jobs_cmd(struct cmdline_tokens* toks)
{
    if(toks -> outfile != NULL){
        int output_fd = open(toks -> outfile, O_CREAT | O_WRONLY, 0666);
        if(output_fd != -1){
            listjobs(job_list, output_fd);
            close(output_fd);
        } else {
            int size = 12 + strlen(toks -> outfile);
            char* msg = malloc(sizeof(char) * size);
            snprintf(msg, size, "cannot open %s", toks -> outfile);
            unix_error(msg);
        }
    } else {
        listjobs(job_list, STDOUT_FILENO);
    }
}
示例#17
0
文件: tsh.c 项目: kuoliu/15213
/* wrapper for function listjobs */
void Listjobs(struct cmdline_tokens *tokens_ptr){
    if(tokens_ptr->outfile != NULL){
        /* if there is a redirect outfile */
        int rd_out = open(tokens_ptr->outfile, O_WRONLY);
        FILE *fout = NULL;
        /* if an error happens, open a file first */
        if(rd_out == -1){
            fout = fopen(tokens_ptr->outfile, "w");
            rd_out = open(tokens_ptr->outfile, O_WRONLY);
        }
        listjobs(job_list, rd_out);
        if(fout != NULL)
            fclose(fout);
        close(rd_out);  
    } else {
        /* just list jobs through STDOUT */
        listjobs(job_list, STDOUT_FILENO);
    }
}
示例#18
0
/*
 * mylistjob - list all jobs in job_list to the assigned file
 *
 */
void mylistjob(struct cmdline_tokens* tok)
{
	/*when the cmd line has a IO redirection to an assigned output file,
	 jobs will be listed there*/
	if(tok->outfile!=NULL)
	{
		int out_f=open(tok->outfile,O_WRONLY);
		if(out_f!=-1)
		{
			listjobs(job_list,out_f);
			close(out_f);
		}
		else printf("File open error!");

	}
	else
		listjobs(job_list,STDOUT_FILENO);

}
示例#19
0
文件: tsh.c 项目: cneale/Sample-Code
/*
 * builtin_hander - Handles builtin commands of the tsh
 * 
 * Executes a builtin_command per the user's request
 * Returns 1 if the command is builtin
 * Returns 0 otherwise
 */
int builtin_handler(struct cmdline_tokens tok)
{
  struct job_t *job;
  int fd;
  int id;
  job=NULL;
  id=-1;
  
  switch(tok.builtins)
    {
    case BUILTIN_QUIT:
      exit(0);
    case BUILTIN_NONE:
      return 0;
    case BUILTIN_JOBS:
      {
	/*output to terminal*/
	if(tok.outfile==NULL) listjobs(job_list,1);
	/*output to a file*/
	else
	  {
	    fd = open(tok.outfile,O_WRONLY,0);
	    listjobs(job_list,fd);
	    close(fd);
	  }
	return 1;
      }
    case BUILTIN_BG:
      {
	job = extractjob(tok);
	bg_handler(job);
	return 1;
      }
    case BUILTIN_FG:
      {
	job = extractjob(tok);
	fg_handler(job);
	return 1;
      }
    }
  return 0;
}
示例#20
0
文件: tsh.c 项目: sanha/SP2015
/*
 * builtin_cmd - If the user has typed a built-in command then execute
 *    it immediately.
 */
int builtin_cmd(char **argv)
{
  	if (strcmp (argv[0], "quit") == 0) 		// case 1: cmd is quit
		exit(0);				// exit
	else if (strcmp (argv[0], "jobs") == 0) 	// case 2: cmd is jobs
	    	listjobs(jobs);				// listing all jobs
	else if (strcmp (argv[0], "bg") == 0 || 	// case 3: cmd is bg or fg
		strcmp (argv[0], "fg") == 0) 		
	    	do_bgfg (argv);				// 
	else return 0;     /* not a builtin command */

	return 1;
}
示例#21
0
文件: tsh.c 项目: MintPaw/College
/* 
	* builtin_cmd - If the user has typed a built-in command then execute
	*    it immediately.  
	*/
int builtin_cmd(char **argv) 
{
	if (strcmp(argv[0], "quit") == 0) exit(0);
	if (strcmp(argv[0], "jobs") == 0) {
		listjobs(jobs);
		return 1;
	}
	if (strcmp(argv[0], "bg") == 0 || strcmp(argv[0], "fg") == 0) {
		do_bgfg(argv);
		return 1;
	}
	return 0;     /* not a builtin command */
}
示例#22
0
/* 
 * builtin_cmd - If the user has typed a built-in command then execute
 *    it immediately.  
 */
int builtin_cmd(char **argv) 
{
    if (strcmp(argv[0], "quit")==0)
        exit(0);
    else if (strcmp(argv[0], "fg")==0)
        do_bgfg(argv);
    else if (strcmp(argv[0], "bg")==0)
        do_bgfg(argv);
    else if (strcmp(argv[0], "jobs")==0)
        listjobs(jobs);
    else
        return 0;     /* not a builtin command */
    return 1;
}
示例#23
0
文件: tsh.c 项目: whyzidane/CSAPP
/* 
 * builtin_cmd - If the user has typed a built-in command then execute
 *    it immediately.  
 */
int builtin_cmd(char **argv) 
{
    if(!strcmp("quit", argv[0]))
      exit(0);
    if (!strcmp("jobs", argv[0])) {
      listjobs(jobs);
      return 1;
    }
    if (!strcmp("bg", argv[0]) || !strcmp("fg", argv[0])) {
      do_bgfg(argv);
      return 1;
    }
    return 0;     /* not a builtin command */
}
示例#24
0
文件: tsh.c 项目: Azureyjt/ICS_LAB
int builtin_cmd(char **argv, int n) {
	char *command = argv[0];
	if (!strcmp(argv[0], "quit")) { /* quit command */
		exit(0);
	}
    else if (!strcmp("bg", command) || !(strcmp("fg", command))) {      
        do_bgfg(argv,n);
        return 1;
        }
    else if (!strcmp("jobs", command)) {
        listjobs(jobs);
        return 1;
    }
    return 0;     /* not a builtin command */
}
示例#25
0
文件: tsh.c 项目: FDUJiChao/wdan-ics
/* 
 * builtin_cmd - If the user has typed a built-in command then execute
 *    it immediately.  
 */
int builtin_cmd(char **argv, int *input_fd, int *output_fd) {
	if (!strcmp(argv[0], "quit"))
		exit(0);
	if ((!strcmp(argv[0], "bg")) || (!strcmp(argv[0], "fg"))) {
		do_bgfg(argv, 1);
		return 1;
	}
	if (!strcmp(argv[0], "jobs")) {
		listjobs(jobs, 1);
		return 1;
	};
	if (!strcmp(argv[0], "&"))
		return 1;
	return 0; /* not a builtin command */
}
示例#26
0
/*
 * builtin_cmd - If the user has typed a built-in command then execute
 *    it immediately.
 */
int builtin_cmd(char **argv)
{
    if(argv[1]==NULL && !strcmp(argv[0], "quit")){  /*If the built in command 'quit' is given*/
        exit(0);
    }
    else if(argv[1]==NULL && !strcmp(argv[0], "jobs")){
      listjobs(jobs);
      return 1;
    }
    else if(argv[2]==NULL && (!strcmp(argv[0], "bg") || !strcmp(argv[0], "fg"))){
      do_bgfg(argv);
      return 1;
    }
    return 0;     /* If not a builtin command */
}
示例#27
0
/*
 * builtin_cmd - If the user has typed a built-in command then execute
 *    it immediately.
 */
int builtin_cmd(char **argv)
{
	if(strcmp(argv[0], "quit") == 0) // quit 
		exit(0); // exit
	else if(strcmp(argv[0], "fg") == 0 || strcmp(argv[0], "bg") == 0) {  // fg & bg
		do_bgfg(argv); // do_bgfg
		return 1;
	}
	else if(strcmp(argv[0], "jobs") == 0) {  // jobs
		listjobs(jobs); // print all jobs.
		return 1;
	}
	else
  		return 0;     /* not a builtin command */
}
示例#28
0
文件: tsh.c 项目: fffxj/csapp
/* 
 * builtin_cmd - If the user has typed a built-in command then execute
 *    it immediately.  
 */
int builtin_cmd(char **argv) 
{
  if (!strcmp(argv[0], "quit"))   /* quit command */
    exit(0);
  if (!strcmp(argv[0], "jobs")) { /* jobs command */
    listjobs(jobs);
    return 1;
  }    
  if (!strcmp(argv[0], "bg") || !strcmp(argv[0], "fg")) { /* bg or fg command */
    do_bgfg(argv);
    return 1;
  }
  if (!strcmp(argv[0], "&"))      /* ignore singleton & */
    return 1;
  return 0;                       /* not a builtin command */
}
示例#29
0
/* 
 *  * builtin_cmd - If the user has typed a built-in command then execute
 *   *    it immediately.  
 *    */
int builtin_cmd(char **argv) 
{
	if (strcmp("quit", argv[0]) == 0) {
		exit(0);
	}
	else if (strcmp("jobs", argv[0]) == 0) { 
		listjobs(jobs);
		return 1;
	}
	else if(!strcmp(argv[0],"bg") || !strcmp(argv[0],"fg")) {
        	do_bgfg(argv);
		return 1;
	}
	
    return 0;     /* not a builtin command */
}
示例#30
0
文件: tsh.c 项目: FakerLulu/syspro
int builtin_cmd(char **argv)
{
	char *cmd = argv[0];
	if(strcmp(cmd,"quit")== 0){
		exit(0);
	}
	else if (strcmp(cmd, "jobs")== 0) {
		listjobs(jobs, 1);
		return 1;
	}
	else if (!strcmp("bg", argv[0]) || !(strcmp("fg", argv[0]))) {  
		do_bgfg(argv);  
		return 1;  
	}
	return 0;
}