Esempio n. 1
0
/*
 * RunCmdFork
 *
 * arguments:
 *   commandT *cmd: the command to be run
 *   bool fork: whether to fork
 *
 * returns: none
 *
 * Runs a command, switching between built-in and external mode
 * depending on cmd->argv[0].
 */
void
RunCmdFork(commandT* cmd, bool fork)
{
  if (cmd->argc <= 0)
    return;
  int i;
  for (i = 0; i < cmd->argc; i++) {
    if (cmd->argv[i][0] == '|') {
      commandT** tcmd1 = malloc(sizeof(commandT*));
      commandT** tcmd2 = malloc(sizeof(commandT*));
      getCmds(cmd, tcmd1, tcmd2, i);
      commandT* cmd1 = *tcmd1;
      commandT* cmd2 = *tcmd2;
      RunCmdPipe(cmd1, cmd2);
      free(tcmd1);
      free(tcmd2);
      return;
    }
  }

  if (IsBuiltIn(cmd->argv[0]))
    {
      RunBuiltInCmd(cmd);
    }
  else
    {
      RunExternalCmd(cmd, fork);
    }
} /* RunCmdFork */
Esempio n. 2
0
/*
 * RunCmdBg
 *
 * arguments:
 *   commandT *cmd: the command to be run
 *
 * returns: none
 *
 * Runs a command in the background.
 */
void
RunCmdBg(commandT* cmd)
{
  // TODO
  
  RunExternalCmd(cmd, TRUE, TRUE);
  //free(newCmd);
  return;
} /* RunCmdBg */
Esempio n. 3
0
	void RunCmdFork(commandT* cmd, bool fork)
	{
		if (cmd->argc<=0)
			return;
		if (IsBuiltIn(cmd->argv[0]))
		{
			RunBuiltInCmd(cmd);
		}
		else
		{
			RunExternalCmd(cmd, fork);
		}
	}
Esempio n. 4
0
void RunCmdFork(commandT* cmd, bool fork)
{
  if (cmd->argc<=0)
    //printf("You typed enter! \n");
    return;
  if (IsBuiltIn(cmd->argv[0]))
  {
    RunBuiltInCmd(cmd);
  }
  else
  {
    RunExternalCmd(cmd, fork);
  }
}
Esempio n. 5
0
void RunCmdFork(commandT* cmd, bool fork)
{
  // printf("in runcmdfork\n");
  if (cmd->argc<=0)
    return;
  if (IsBuiltIn(cmd->argv[0]))
  {
    RunBuiltInCmd(cmd);
  }
  else
  {
    RunExternalCmd(cmd, fork);
  }
}
Esempio n. 6
0
void RunCmdFork(commandT* cmd, bool fork)
{

  if (cmd->argc<=0)
    return;
  if (ResolveExternalCmd(cmd))//IsBuiltIn(*(cmd[0].argv))) //is builtin?
  {
    RunBuiltInCmd(cmd);
  }
  else
  {
    RunExternalCmd(cmd, fork);

  }
}
Esempio n. 7
0
        void RunCmdRedirInOut(commandT* cmd)
        {
           int save_stdout = dup(1); 
		int save_stdin = dup(0);
		if (cmd->is_redirect_out) {
			int fid_out = open(cmd->redirect_out, O_RDWR | O_CREAT,S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
        		close(1);
        		dup2(fid_out,1);
        		close(fid_out);
		}
		if (cmd->is_redirect_in) {
			 int fid_in = open(cmd->redirect_in, O_RDONLY, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
                	 close(0);
                	 dup2(fid_in, 0);
                	 close(fid_in);
		}
		RunExternalCmd(cmd, TRUE);
		dup2(save_stdout, 1);
		dup2(save_stdin, 0);
        }
Esempio n. 8
0
/**************Implementation***********************************************/
void RunCmd(commandT** cmd, int n, int fd_in, int fd_out) {
  int i;
  int fd[2];
  fd[0] = fd_in;
  for (i = 0; i < n; i++) {
    cmd[i]->fd_in = fd[0];
    if (i != n-1) {
      if (pipe(fd) == -1) {
        perror("pipe error in RunCmd");
      }
      cmd[i]->fd_out = fd[1];
    } else {
      cmd[i]->fd_out = fd_out;
    }
    if (cmd[i]->argc > 0) {
      if (InterpretAlias(alist, cmd[i]->argv[0])) {
        int j;
        char* rcmd = NULL;
        char* realcmd = (char*)malloc(sizeof(char) * 1024);
        for (j = 0; j < cmd[i]->argc; j++) {
          rcmd = InterpretAlias(alist, cmd[i]->argv[j]);
          strcat(realcmd, " ");
          if (rcmd) {
            strcat(realcmd, rcmd);
          } else {
            strcat(realcmd, cmd[i]->argv[j]);
          }
        }
        int taskNum = 0;
        commandT** aliasCmd = Interpret(realcmd, &taskNum);
        RunCmd(aliasCmd, taskNum, cmd[i]->fd_in, cmd[i]->fd_out);
      } else if (IsBuiltIn(cmd[i]->argv[0])) {
        RunBuiltInCmd(cmd[i]);
      } else {
        RunExternalCmd(cmd[i], TRUE);
      }
    }
    ReleaseCmdT(&cmd[i]);
  }
  free(cmd);
}
Esempio n. 9
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 */
Esempio n. 10
0
/*
 * RunCmdFork
 *
 * arguments:
 *   commandT *cmd: the command to be run
 *   bool fg: whether to fg
 *
 * returns: none
 *
 * Runs a command, switching between built-in and external mode
 * depending on cmd->argv[0]
 */
void
RunCmdFork(commandT* cmd, bool fg)
{
  if (cmd->argc <= 0)
    return;
  if (IsBuiltIn(cmd->argv[0]))
	{
		RunBuiltInCmd(cmd);
	}
  else if (ResolveExternalCmd(cmd))
	{
		RunExternalCmd(cmd, fg);
	}
	else
	{
		char* temp = (char*)malloc(500*sizeof(char));
		char* unfoundCommand = cmd->argv[0];
		char* unresolvedCommand = "line 1: "; // does line 1 refer to error in first argument?
		strcpy(temp, unresolvedCommand);
		strcat(temp, unfoundCommand);
		PrintPError(temp);
		free(temp);
	}
} /* RunCmdFork */