Exemple #1
0
void RunCmd(commandT** cmd, int n)
{
  int i;
  total_task = n;

  // checkAlias(cmd,n);
  if ((*cmd)->is_redirect_out)
  {
    (*cmd)->is_redirect_out = 0;
    RunCmdRedirOut(*cmd, (*cmd)->redirect_out);
  }
  else if ((*cmd)->is_redirect_in)
  {
    (*cmd)->is_redirect_in = 0;
    RunCmdRedirIn(*cmd, (*cmd)->redirect_in);
  }

  else if(n == 1)
    RunCmdFork(cmd[0], TRUE);
  else{
    RunCmdMultiPipe(cmd,n);
    for(i = 0; i < n; i++)
      ReleaseCmdT(&cmd[i]);
  }
}
Exemple #2
0
void
RedirectIO(commandT* cmd, int* stdinFd, int* stdoutFd){
  int i = 0;
  bool hasRedirection = FALSE;
  while (cmd->argv[i] != 0){
	if (cmd->argv[i][0] == '<'){
		//printf("Found '<'.\n");
		RunCmdRedirIn(cmd, cmd->argv[i+1], stdinFd);
		hasRedirection = TRUE;
	}
	else if (cmd->argv[i][0] == '>'){
		//printf("Found '>'. Next: %s\n", cmd->argv[i+1]);
		RunCmdRedirOut(cmd, cmd->argv[i+1], stdoutFd);
		hasRedirection = TRUE;
	}
	i++;
  }
  if (hasRedirection){
	/*Updating argv*/
	i=0;
	while ((cmd->argv[i][0] != '>') && (cmd->argv[i][0] != '<')){
		i++;
	}
	/*Free arguments from index i and higher*/
	while (cmd->argv[i] != 0){
		free(cmd->argv[i]);
		cmd->argv[i] = 0;
		cmd->argc--;
		i++;
	}
  }
  return;
}
Exemple #3
0
static void Exec(commandT* cmd, bool forceFork)
{
        pid_t pid;
        //printf("%s \n",cmd->name);
        
        sigset_t mask;
        sigemptyset(&mask);
        sigaddset(&mask,SIGCHLD);
        sigaddset(&mask,SIGINT);
        sigaddset(&mask,SIGTSTP);
        //blocks the signals specified above, before calling the children
        sigprocmask(SIG_BLOCK, &mask, NULL);


        pid = fork();
        //check for fork error
        if (pid < 0){
                perror("Fork Error\n");
        }
        // //successful fork
        // else {
        if (pid==0){
        //child
              setpgid(0, 0);
              //unblocks the signals specified above
              sigprocmask(SIG_UNBLOCK, &mask, NULL);
              
              //check for redirections
              if (cmd->is_redirect_in==1){
                RunCmdRedirIn(cmd, cmd->redirect_in);
              }
              if (cmd->is_redirect_out==1){
                RunCmdRedirOut(cmd,cmd->redirect_out);
              }
              
                execv(cmd->name,cmd->argv);
          }
          else{
            //parent
            sigprocmask(SIG_UNBLOCK, &mask, NULL);
            //we add the job to the background if it was specified as background
            if (cmd->bg ==1){
                AddJob(pid,BACKGROUND, cmd->cmdline); //add job to background jobs
            }
            //otherwise the job goes in the foreground
            else {
                //printf("Adding job %d \n",pid);
                fgpid = pid;
                AddJob(pid, FOREGROUND, cmd->cmdline);
                //in the foreground, so go wait for it to finish
                WaitFg(pid);
            }
          }
    //}
}
Exemple #4
0
static void RunBuiltInCmd(commandT* cmd)
{
  
  int status;
  sigset_t mask;
  sigemptyset(&mask);
  sigaddset(&mask,SIGCHLD);
  int pid=fork();
  if(pid == 0)
  {
    if ((cmd->is_redirect_in==1) && (cmd->is_redirect_out==1)){
      RunCmdRedirInOut(cmd,cmd->redirect_in,cmd->redirect_out);
    }else if (cmd->is_redirect_in==1){
      RunCmdRedirIn(cmd,cmd->redirect_in);
    }else if(cmd->is_redirect_out==1){
      RunCmdRedirOut(cmd,cmd->redirect_out);
    }
    setpgid(0, 0);
    execv(cmd->name , cmd->argv);
  }else if(pid < 0)
    perror("Fork Error, in RunBuiltInCmd");
  else {
        {
          if(cmd[0].bg==0){
            fgpid = pid;
            waitpid(pid,&status,WUNTRACED);
          if(WTERMSIG(status) == 127){
                  // stopped by SIGTSTP
                  fgpid=-1;
                  //add it to backbground
                  addToBg(pid,cmd,2);            
          }
            return;
          }else if (cmd[0].bg==1){
            sigprocmask(SIG_UNBLOCK,&mask,NULL);
            //run command background
            //add the bg process to the bgjoblist
            addToBg(pid,cmd,0);
          }
        }
    }
}
Exemple #5
0
void RunCmd(commandT** cmd, int n)
{
  int i;
  total_task = n;
  if(n == 1)
    if (cmd[0]->is_redirect_in && cmd[0]->is_redirect_out){
      RunCmdRedir(cmd);
    }
    else if (cmd[0]->is_redirect_in){
      RunCmdRedirIn(cmd[0], cmd[0]->redirect_in);
    }
    else if (cmd[0]->is_redirect_out){
      RunCmdRedirOut(cmd[0], cmd[0]->redirect_out);
    }
    else {
      RunCmdFork(cmd[0], TRUE);
    }
  else{
    RunCmdPipe(cmd[0], cmd[1]);
    for(i = 0; i < n; i++)
      ReleaseCmdT(&cmd[i]);
  }
}
Exemple #6
0
/*
 * RunCmd
 *
 * arguments:
 *   commandT *cmd: the command to be run
 *
 * returns: none
 *
 * Runs the given command. Initializes globals.
 */
void
RunCmd(commandT* cmd)
{
	int redirstatus=0;
	int pipestatus=0;
	int bgstatus=0;
	int i;
	char redirfile[MAXLINE];
	if ((cmd->argc - 1 >= 0) && cmd->argv[cmd->argc - 1][0] == '&') { //we need to background
		free(cmd->argv[cmd->argc-1]);
		cmd->argv[cmd->argc-1] = 0;
		cmd->argc--;

		bgstatus = 1;
	}
	if( ( cmd->argc-2>=0 ) && (cmd->argv[cmd->argc-2][0] == '>') ){//we get the redir out.
		strcpy(redirfile,cmd->argv[cmd->argc-1]);
		
		free(cmd->argv[cmd->argc-2]);//wo don't need it anymore
		free(cmd->argv[cmd->argc-1]);
		cmd->argv[cmd->argc-2] = 0;
		cmd->argv[cmd->argc-1] = 0;
		cmd->argc=cmd->argc-2;
		
		RunCmdRedirOut(cmd, redirfile);//redirect output
		redirstatus=1;
	}
	else if( ( cmd->argc-2>=0 ) && ( cmd->argv[cmd->argc-2][0] == '<') ){//we get the redir in
		strcpy(redirfile,cmd->argv[cmd->argc-1]);
		
		free(cmd->argv[cmd->argc-2]);//wo don't need it anymore
		free(cmd->argv[cmd->argc-1]);
		cmd->argv[cmd->argc-2] = 0;
		cmd->argv[cmd->argc-1] = 0;
		cmd->argc=cmd->argc-2;
		
		RunCmdRedirIn(cmd, redirfile);//redirect input
		redirstatus=1;
	}
	else
	for(i = 0; i < cmd->argc; ++i)
	{
		if(cmd->argv[i][0] == '|')
		{
			pipestatus=i;
			break;
		}
		else if(cmd->argv[i][0] == '~')
		{
			char* tempcmd=malloc(  sizeof(char) * ( strlen(cmd->argv[i]) + strlen(getenv("HOME")) )  );
			strcpy(tempcmd,getenv("HOME"));
			strcat(tempcmd,cmd->argv[i]+1);
			free(cmd->argv[i]);
			cmd->argv[i]=tempcmd;
			
		}
	}
	
	if(redirstatus==1)
	{
		;
	}
	else if(pipestatus > 0)
	{
		commandT* cmd1 = malloc(sizeof(commandT) + sizeof(char*) * cmd->argc);
		commandT* cmd2 = malloc(sizeof(commandT) + sizeof(char*) * cmd->argc);
		char* tmp = cmd->argv[pipestatus];
		cmd->argv[pipestatus]=0;
		
		/*	copy the cmd before '|' to cmd1	*/
		cmd1->name = cmd->argv[0];
		cmd1->argc = pipestatus;
		for(i = 0; cmd->argv[i] != 0; ++i)
		{
			cmd1->argv[i] = cmd->argv[i];
		}
		cmd1->argv[i]=0;
		
		/*	copy the cmd after '|' to cmd2	*/
		cmd2->name = cmd->argv[pipestatus+1];
		cmd2->argc = cmd->argc-pipestatus-1;
		for(i = pipestatus +1; cmd->argv[i] != 0; ++i)
		{
			cmd2->argv[i-pipestatus-1] = cmd->argv[i];
		}
		cmd2->argv[i-pipestatus-1] = 0;
		
		cmd->argv[pipestatus]=tmp; //restore the cmd->argv[pipestatus]
		
		RunCmdPipe(cmd1,cmd2);
	}
	else {
		if (bgstatus)
			RunCmdFork(cmd, FALSE);
		else
			RunCmdFork(cmd, TRUE);
	}
} /* RunCmd */