Exemplo n.º 1
0
void RunCmd(commandT** cmd, int n)
{
  int i;
  int count;
  total_task = n;
  //printf("Command name: %s \n",cmd[0]->argv[0]);
  //printf("Command argument count: %d \n",cmd[0]->argc-1);
  if (IsAlias((cmd[0]->argv[0])))
  {
    RunAlias((cmd[0]));
  }
  else
  {

    for ( count=1;count<cmd[0]->argc;count++){
          //printf("Argument %d : %s \n",count,cmd[0]->argv[count]);
    }
    if(n == 1)
      RunCmdFork(cmd[0], TRUE);
    else{
      RunCmdPipe(cmd[0], cmd[1]);
      for(i = 0; i < n; i++)
        ReleaseCmdT(&cmd[i]);
    }
  }
}
Exemplo n.º 2
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]);
  }
}
Exemplo n.º 3
0
void RunCmd(commandT** cmd, int n)
{
  total_task = n;
  if(n == 1)
    RunCmdFork(cmd[0], TRUE);
  else{
    fprintf(stderr, "%s\n", "Pipes were not implemented");
  }
}
Exemplo n.º 4
0
void RunCmd(commandT** cmd, int n)
{
  int i;
  total_task = n;
  if(n == 1){
    RunCmdFork(cmd[0], TRUE);

  }else{
    RunCmdPipe(cmd[0], cmd[1]);  //Qest: only two task allowed ?
    for(i = 0; i < n; i++)
      ReleaseCmdT(&cmd[i]);
  }
}
void RunCmd(commandT** cmd, int n)
{
  int i;
  total_task = n;
  if (n == 1) {
    RunCmdFork(cmd[0], TRUE);
    ReleaseCmdT(&cmd[0]);
  }
  else {
    RunCmdPipe(cmd[0], &cmd[1], n - 1, -1);
    for (i = 0; i < n; i++) {
      ReleaseCmdT(&cmd[i]);
    }
  }
}
Exemplo n.º 6
0
/*
 * RunCmd
 *
 * arguments:
 *   commandT *cmd: the command to be run
 *
 * returns: none
 *
 * Runs the given command.
 */
void
RunCmd(commandT* cmd)
{
  if (cmd->argc <= 0){
   return; 
  } 
  if (IsBG(cmd)){
   //printf("this is BGGGG");
   RunCmdBg(cmd); 
  }
  else{
    RunCmdFork(cmd, TRUE);
  }
  
} /* RunCmd */
Exemplo n.º 7
0
void RunCmd(commandT** cmd, int n)
{
  // printf("in RunCmd\n");
  int i;
  total_task = n;
  // fprintf(stdout, "Name: %s\n", cmd[0]->name);
  // fprintf(stdout, "cmdLine: %s\n", cmd[0]->cmdline);
  
  if(n == 1)
    RunCmdFork(cmd[0], TRUE);
  else{
    RunCmdPipe(cmd[0], cmd[1]);
    for(i = 0; i < n; i++)
      ReleaseCmdT(&cmd[i]);
  }
}
Exemplo n.º 8
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)) {
                      RunCmdRedirInOut(cmd[0]);
                  } else {
                      RunCmdFork(cmd[0], TRUE);
                  }
                }
        	else {	
                    RunCmdPipe(cmd[0], cmd[1]);
                    for(i = 0; i < n; i++)
                        ReleaseCmdT(&cmd[i]);
      		}
	}
Exemplo n.º 9
0
void RunCmdRedirIn(commandT* cmd, char* file) {
  // Save the state of the stdin into the input
  int input = dup(0);

  // Get the file descriptor for the file to be inputted
  int fd = open(file, O_RDONLY);
  if (fd < 0)
    return;

  // Tell the system to read from the file for stdin
  dup2(fd, 0);
  RunCmdFork(cmd, TRUE);

  // Restore the stdin to the keyboard
  dup2(input, 0);
  close(fd);
}
Exemplo n.º 10
0
void RunCmdRedirOut(commandT* cmd, char* file) {
  // Save the state of the stdout into the output
  int output = dup(1);

  // Get the file descriptor for the file to be outputted
  int fd = open(file, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
  if (fd < 0)
    return;

  // Tell the system to write to the file for stdout
  dup2(fd,1);
  RunCmdFork(cmd, TRUE);

  // Restore the stdout to the screen
  dup2(output,1);
  close(fd);
}
Exemplo n.º 11
0
/* handles multiple pipes */
void RunCmdMultiPipe(commandT** cmd, int n){

  int pipefd[2];

  pid_t cpid;

  int temp = 0;

  if (pipe(pipefd) == -1){    //get the initialized pipe fd

    perror("Error pipe");
    exit(EXIT_FAILURE);
  }
  int com =  0;   
  while (com < n) {
    if (pipe(pipefd) == -1)
      perror("Error in pipe"); 
    cpid = fork();
    //child process
    if (cpid == 0){   //if it is not the last command, redirect the pipe to output
      dup2(temp,0);
      if (com+1 != n){
        dup2(pipefd[1],1);
      }
      close(pipefd[0]);

      RunCmdFork(cmd[com],TRUE); //run the command
      exit(EXIT_FAILURE);
    }
    else {    //arent process
      wait(NULL);
      close(pipefd[1]);
      temp = pipefd[0];
    }

    com++;
  }


  close(pipefd[0]); //close all the pipe
  close(pipefd[1]);

}
Exemplo n.º 12
0
void RunCmdRedir(commandT** cmd) {
  // Save the state of the stdin and the stdout before we do redirection
  int input = dup(0);
  int output = dup(1);

  // Get the file file descriptors for the input and output files
  int fd_in = open(cmd[0]->redirect_in, O_RDONLY);
  int fd_out =  open(cmd[0]->redirect_out, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
  if (fd_in < 0 || fd_out < 0)
    return;

  // Tell the system to use the files for the stdin and the stdout
  dup2(fd_in, 0);
  dup2(fd_out, 1);
  RunCmdFork(cmd[0], TRUE);

  // Restore the original states of the stdin and the stdout
  dup2(input, 0);
  dup2(output, 1);
  close(fd_in);
  close(fd_out);
}
Exemplo n.º 13
0
/*
 * RunCmdPipe
 *
 * arguments:
 *   commandT *cmd1: the commandT struct for the left hand side of the pipe
 *   commandT *cmd2: the commandT struct for the right hand side of the pipe
 *
 * returns: none
 *
 * Runs two commands, redirecting standard output from the first to
 * standard input on the second.
 */
void
RunCmdPipe(commandT* cmd1, commandT* cmd2)
{
  int pid, ppid, fd[2];
  if (pipe(fd) < 0) {
    perror("pipe error");
  }

  if ((pid = fork()) < 0) {
    perror("fork error");
  } else if (pid == 0) {
    dup2(fd[1], 1);
    close(fd[0]);
    setpgid(0, fgpid(jobs));
    RunExternalCmd(cmd1, 0);
  } else {
    if (fgpid(jobs) == 0) 
      addjob(jobs, pid, FG, "nil");
    if ((ppid = fork()) < 0) {
      perror("fork error");
    } else if (ppid == 0) {
      dup2(fd[0], 0);
      close(fd[1]);
      setpgid(0, fgpid(jobs));
      RunCmdFork(cmd2, 0);
    } else {
      close(fd[0]);
      close(fd[1]);
      waitpid(pid, NULL, 0);
      waitpid(ppid, NULL, 0);
      if (fgpid(jobs) == pid)
        deletejob(jobs, pid);
    }
  }
  freeCommand(cmd1);
  freeCommand(cmd2);
} /* RunCmdPipe */
Exemplo n.º 14
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 */
Exemplo n.º 15
0
/*
 * RunCmd
 *
 * arguments:
 *   commandT *cmd: the command to be run
 *
 * returns: none
 *
 * Runs the given command.
 */
void
RunCmd(commandT* cmd)
{
  RunCmdFork(cmd, TRUE);
} /* RunCmd */
Exemplo n.º 16
0
void RunCmdBg(commandT* cmd)
{
  // printf("in RunCmdBg\n");
  RunCmdFork(cmd, FALSE);// TODO
}