Exemplo 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 */
Exemplo n.º 2
0
	void RunCmdFork(commandT* cmd, bool fork)
	{
		if (cmd->argc<=0)
			return;
		if (IsBuiltIn(cmd->argv[0]))
		{
			RunBuiltInCmd(cmd);
		}
		else
		{
			RunExternalCmd(cmd, fork);
		}
	}
Exemplo n.º 3
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);
  }
}
Exemplo n.º 4
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);
  }
}
Exemplo n.º 5
0
  /** \brief Read the next token from the string. */ 
  ParserTokenReader::token_type ParserTokenReader::ReadNextToken()
  {
    assert(m_pParser);

    std::stack<int> FunArgs;
    const char_type *szFormula = m_strFormula.c_str();
    token_type tok;

    // Ignore all non printable characters when reading the expression
    while (szFormula[m_iPos]>0 && szFormula[m_iPos]<=0x20) 
      ++m_iPos;

    if ( IsEOF(tok) )        return SaveBeforeReturn(tok); // Check for end of formula
    if ( IsOprt(tok) )       return SaveBeforeReturn(tok); // Check for user defined binary operator
    if ( IsFunTok(tok) )     return SaveBeforeReturn(tok); // Check for function token
    if ( IsBuiltIn(tok) )    return SaveBeforeReturn(tok); // Check built in operators / tokens
    if ( IsArgSep(tok) )     return SaveBeforeReturn(tok); // Check for function argument separators
    if ( IsValTok(tok) )     return SaveBeforeReturn(tok); // Check for values / constant tokens
    if ( IsVarTok(tok) )     return SaveBeforeReturn(tok); // Check for variable tokens
    if ( IsStrVarTok(tok) )  return SaveBeforeReturn(tok); // Check for string variables
    if ( IsString(tok) )     return SaveBeforeReturn(tok); // Check for String tokens
    if ( IsInfixOpTok(tok) ) return SaveBeforeReturn(tok); // Check for unary operators
    if ( IsPostOpTok(tok) )  return SaveBeforeReturn(tok); // Check for unary operators

    // Check String for undefined variable token. Done only if a 
    // flag is set indicating to ignore undefined variables.
    // This is a way to conditionally avoid an error if 
    // undefined variables occur. 
    // (The GetUsedVar function must suppress the error for
    // undefined variables in order to collect all variable 
    // names including the undefined ones.)
    if ( (m_bIgnoreUndefVar || m_pFactory) && IsUndefVarTok(tok) )  
      return SaveBeforeReturn(tok);

    // Check for unknown token
    // 
    // !!! From this point on there is no exit without an exception possible...
    // 
    string_type strTok;
    int iEnd = ExtractToken(m_pParser->ValidNameChars(), strTok, m_iPos);
    if (iEnd!=m_iPos)
      Error(ecUNASSIGNABLE_TOKEN, m_iPos, strTok);

    Error(ecUNASSIGNABLE_TOKEN, m_iPos, m_strFormula.substr(m_iPos));
    return token_type(); // never reached
  }
void ReferenceCleanupPass::CleanupCall(CallStatement* c)
{
  assert(procDef != NULL) ;
  assert(c != NULL) ;

  // We only need to clean up module calls.  If they are built in
  //  functions, like boolsel, we don't want to do this.
  if (IsBuiltIn(c))
  {
    return ;
  }
  
  // Go through the arguments and see if any of them are load variable
  //  expressions to a reference typed variable, and replace those with
  //  symbol address expressions
  for (unsigned int i = 0 ; i < c->get_argument_count() ; ++i)
  {
    Expression* currentArg = c->get_argument(i) ;
    LoadVariableExpression* currentLoadVar = 
      dynamic_cast<LoadVariableExpression*>(currentArg) ;
    if (currentLoadVar != NULL)
    {
      VariableSymbol* currentVar = currentLoadVar->get_source() ;
      DataType* varType = currentVar->get_type()->get_base_type() ;
      ReferenceType* refType = dynamic_cast<ReferenceType*>(varType) ;
      if (refType != NULL)
      {
	QualifiedType* internalType = 
	  dynamic_cast<QualifiedType*>(refType->get_reference_type()) ;
	assert(internalType != NULL) ;
	//	currentVar->set_type(internalType) ;
	SymbolAddressExpression* symAddrExp = 
	  create_symbol_address_expression(theEnv, 
					   internalType->get_base_type(),
					   currentVar) ;
	if (currentLoadVar->lookup_annote_by_name("UndefinedPath") != NULL)
	{
	  symAddrExp->append_annote(create_brick_annote(theEnv, "UndefinedPath")) ;
	}
	currentLoadVar->get_parent()->replace(currentLoadVar, symAddrExp) ;
      }
    }
  }
}
Exemplo n.º 7
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);
}
Exemplo n.º 8
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 */
void RunCmdPipe(commandT* cmd, commandT** rest, int n, int incoming)
{
  // cmd = current command to be run
  // rest = remaining commands to be run
  // n = length of rest
  // incoming = incoming "STDIN" fd

  int builtin = FALSE;

  if (IsBuiltIn(cmd->argv[0])) {
    builtin = TRUE;
  } else {
    if (!ResolveExternalCmd(cmd)) {
      printf("%s: command not found\n", cmd->argv[0]);
      fflush(stdout);

      if (incoming != -1) {
        close(incoming);
      }

      return;
    }
  }

  // Set up pipe if there are commands left to run
  int fd[2];
  if (n) {
    if (pipe(fd) < 0) {
      printf("failed to create pipe\n");
      fflush(stdout);
    }
  }

  sigset_t mask;
  sigset_t old;

  sigemptyset(&mask);
  sigaddset(&mask, SIGCHLD);
  sigaddset(&mask, SIGTSTP);
  sigaddset(&mask, SIGINT);

  if (sigprocmask(SIG_BLOCK, &mask, &old) < 0) {
    printf("tsh: failed to change tsh signal mask");
    fflush(stdout);
    return;
  }

  int child_pid = fork();

  /* The processes split here */

  if(child_pid < 0) {
    printf("failed to fork\n");
    fflush(stdout);
    return;
  }

  if (child_pid) {
    setpgid(child_pid, 0); // Move child into its own process group.

    bgjobL* job = AddJob(child_pid, cmd->cmdline, FOREGROUND | RUNNING);

    if (sigprocmask(SIG_SETMASK, &old, NULL) < 0) {
      printf("tsh: failed to change tsh signal mask");
      fflush(stdout);
      return;
    }

    // close incoming (if available) now that we're done reading it
    if (incoming != -1) {
      close(incoming);
    }

    // close the write end of fd (if available) now that we're done writing to it
    if (n) {
      close(fd[1]);  
    }

    while (IS_RUNNING(job)) {
      sleep(1);
    }

    CleanupJob(&job, TRUE);
  } else {
    setpgid(0, 0); // Move child into its own process group.

    if (sigprocmask(SIG_SETMASK, &old, NULL) < 0) {
      printf("tsh: failed to change child signal mask");
      fflush(stdout);
      exit(2);
    }

    // Map incoming pipe fd to STDIN (if available)
    if (incoming != -1) {
      if (dup2(incoming, STDIN) < 0) {
        printf("failed to map pipe to STDIN\n");
        fflush(stdout);
        exit(2);
      }
      close(incoming);
    }

    // Map STDOUT to outgoing pipe fd (if available)
    if (n) {
      if (dup2(fd[1], STDOUT) < 0) {
        printf("failed to map STDOUT to pipe\n");
        fflush(stdout);
        exit(2);
      }
      close(fd[1]);
    }

    if (builtin) {
      RunBuiltInCmd(cmd);
      exit(0);
    } else {
      execv(cmd->name, cmd->argv);
      exit(2);
    }
  }

  if (n) {
    // pipe into the next process
    RunCmdPipe(rest[0], &rest[1], n - 1, fd[0]); 
  }
}
/** \brief Read the next token from the string. */
ptr_tok_type TokenReader::ReadNextToken()
{
    assert(m_pParser);

    SkipCommentsAndWhitespaces();

    int token_pos = m_nPos;
    ptr_tok_type pTok;

    // Check for end of expression
    if (IsEOF(pTok))
        return Store(pTok, token_pos);

    if (IsNewline(pTok))
        return Store(pTok, token_pos);

    if (!(m_nSynFlags & noOPT) && IsOprt(pTok))
        return Store(pTok, token_pos); // Check for user defined binary operator

    if (!(m_nSynFlags & noIFX) && IsInfixOpTok(pTok))
        return Store(pTok, token_pos); // Check for unary operators

    if (IsValTok(pTok))
        return Store(pTok, token_pos); // Check for values / constant tokens

    if (IsBuiltIn(pTok))
        return Store(pTok, token_pos); // Check built in operators / tokens

    if (IsVarOrConstTok(pTok))
        return Store(pTok, token_pos); // Check for variable tokens

    if (IsFunTok(pTok))
        return Store(pTok, token_pos);

    if (!(m_nSynFlags & noPFX) && IsPostOpTok(pTok))
        return Store(pTok, token_pos); // Check for unary operators

    // 2.) We have found no token, maybe there is a token that we don't expect here.
    //     Again call the Identifier functions but this time only those we don't expect 
    //     to find.
    if ((m_nSynFlags & noOPT) && IsOprt(pTok))
        return Store(pTok, token_pos); // Check for user defined binary operator

    if ((m_nSynFlags & noIFX) && IsInfixOpTok(pTok))
        return Store(pTok, token_pos); // Check for unary operators

    if ((m_nSynFlags & noPFX) && IsPostOpTok(pTok))
        return Store(pTok, token_pos); // Check for unary operators
    // </ibg>

    // Now we are in trouble because there is something completely unknown....

    // Check the string for an undefined variable token. This is done 
    // only if a flag is set indicating to ignore undefined variables.
    // This is a way to conditionally avoid an error if undefined variables 
    // occur. The GetExprVar function must supress the error for undefined 
    // variables in order to collect all variable names including the 
    // undefined ones.
    if ((m_pParser->m_bIsQueryingExprVar || m_pParser->m_bAutoCreateVar) && IsUndefVarTok(pTok))
        return Store(pTok, token_pos);

    // Check for unknown token
    // 
    // !!! From this point on there is no exit without an exception possible...
    // 
    string_type sTok;
    int iEnd = ExtractToken(m_pParser->ValidNameChars(), sTok, m_nPos);

    ErrorContext err;
    err.Errc = ecUNASSIGNABLE_TOKEN;
    err.Expr = m_sExpr;
    err.Pos = m_nPos;

    if (iEnd != m_nPos)
        err.Ident = sTok;
    else
        err.Ident = m_sExpr.substr(m_nPos);

    throw ParserError(err);
}