예제 #1
0
void mysh_loop(void)
{
    char *line;
    TokenInfo *token_infos;
    int is_running = 1;

    do {
        printf("$ ");
        line = mysh_read_line();

        token_infos = malloc(sizeof(TokenInfo*) * 100);
        int token_count = mysh_get_tokens(line, token_infos);
        int in = STDIN_FILENO;
        int out = -1;
        char **args;
        for (int i=0; i<token_count; i++) {
            TokenInfo token_info = token_infos[i];
            int fd[2];
            pipe (fd);

            if (i == token_count - 1) out = STDOUT_FILENO;

            if (token_info.token_id == TKN_NORMAL) {
                args = malloc(sizeof(char*) * 100);
                args = parse_cmd(token_info.token);
                if (is_next(TKN_PIPE, i, token_count, token_infos)) {
                    out = fd[PIPE_WRITE];
                    mysh_execute(in, out, args);
                    close (fd[PIPE_WRITE]);
                    in = fd[PIPE_READ];
                } else if (is_next(TKN_REDIR_IN, i, token_count, token_infos)) {
                    mysh_execute(open_for_rdir_in(token_infos[i+2].token), out, args);
                    i += 2;
                } else if (is_next(TKN_REDIR_OUT, i, token_count, token_infos)) {
                    printf("%s\n", token_infos[i].token);
                    mysh_execute(in, open_for_rdir_out(token_infos[i+2].token), args);
                    i += 2;
                } else {
                    mysh_execute(in, out, args);
                }
                free(args);
            }
        }

        free(line);
        free(token_infos);
    } while (is_running);
}
예제 #2
0
파일: sqlite.hpp 프로젝트: liveralmask/ease
 void statement( sqlite3_stmt* value ){
   if ( null != m_statement ){
     if ( is_next() ) while ( next() ){}
     sqlite3_finalize( m_statement );
   }
   m_statement = value;
 }
예제 #3
0
std::string Tokenizer::slurp_until(const char c) {
  std::string result;

  while (scanner->current_char()) {
    result += *scanner->current_char();

    if (scanner->next_char() && !is_next(c))
    {
      scanner->pop();
    }
    else {
      break;
    }
  }

  if (!scanner->next_char()) {
    throw ReaderException("Unexpected stream end");
  }

  return result;
}
예제 #4
0
std::string Tokenizer::slurp_until(const std::initializer_list<std::set<char>>& stop_lists) {

  std::string result;

  while (scanner->current_char()) {
    result += *scanner->current_char();

    if (scanner->next_char())
    {
      for (auto it = stop_lists.begin(); it != stop_lists.end(); ++it) {
        if (is_next(*it)) {
          goto ret;
        }
      }

      scanner->pop();
    }
    else {
      break;
    }
  }
  ret:
  return result;
}
예제 #5
0
Token Tokenizer::next() {

  ready = false;

  if (!scanner->current_char()) {
    return m_end;
  }

  while (scanner->current_char()) {
    char c = *scanner->current_char();

    if (c == DOUBLE_QUOTE) {
      position pos = scanner->position();
      scanner->pop();
      ret(Token::String(slurp_until(DOUBLE_QUOTE), pos));
      scanner->pop();
    }
    else if (c == SEMICOLON || (c == DISPATCH && is_next(BANG))) {
      flush_line();
      continue;
    }
    else if (whitespace.find(c) != whitespace.end()) {
      //nothing
    }
    else if (c == BACKSLASH) {
      position pos = scanner->position();
      scanner->pop();
      ret(Token::Char(slurp_until({whitespace, delimiters}), pos));
    }
    else if (c == DISPATCH && is_next(CURLY_OPEN)) {
      ret(Token::SetOpen(scanner->position()));
      scanner->pop();
    }
    else if (c == DISPATCH && is_next(ROUND_OPEN)) {
      ret(Token::FunctionOpen(scanner->position()));
      scanner->pop();
    }
    else if (c == DISPATCH && is_next(DOUBLE_QUOTE)) {
      position pos = scanner->position();
      scanner->pop();
      scanner->pop();
      ret(Token::Regex(slurp_until(DOUBLE_QUOTE), pos));
      scanner->pop();
    }
    else if (c == DISPATCH) {
      position pos = scanner->position();
      scanner->pop();
      ret(Token::Dispatch(slurp_until({whitespace, delimiters}), pos));
    }
    else if (c == ROUND_OPEN) {
      ret(Token::RoundOpen(scanner->position()));
    }
    else if (c == ROUND_CLOSE) {
      ret(Token::RoundClose(scanner->position()));
    }
    else if (c == CURLY_OPEN) {
      ret(Token::CurlyOpen(scanner->position()));
    }
    else if (c == CURLY_CLOSE) {
      ret(Token::CurlyClose(scanner->position()));
    }
    else if (c == SQUARE_OPEN) {
      ret(Token::SquareOpen(scanner->position()));
    }
    else if (c == SQUARE_CLOSE) {
      ret(Token::SquareClose(scanner->position()));
    }
    else {
      position pos = scanner->position();
      ret(Token::Literal(slurp_until({whitespace, delimiters}), pos));
    }

    scanner->pop();

    if (ready) {
      break;
    }

  }

  if (ready) {
    return current;
  }
  else {
    return m_end;
  }
}
예제 #6
0
PUBLIC uint32_t insert_seg(seglst_t **lst, seglst_t **lst_aux, segment_t *seg, size_t size, uint32_t base)
{
    seglst_t *aux, *aux2;
    uint32_t count = base, ret = 0;
    if((*lst) == NULL)
    {
        if(seg->seq_num == base)
        {
            (*lst) = (seglst_t *)malloc(sizeof(seglst_t));
            (*lst)->segm = seg;
            (*lst)->size = size;
            (*lst)->next = NULL;

            ret = size;
            aux = (*lst);
            count = (count + size) % MAX_SEQ_NUM;
            while(1==1)
            {
                aux2 = is_next(lst_aux, count);
                if(aux2 == NULL)
                {
                    break;
                }
                count = (count + aux2->size) % MAX_SEQ_NUM;
                aux2->next = NULL;
                aux->next = (struct seglst_t *)aux2;
                aux = aux2;
                ret += aux2->size;
            }
            return ret;
        }
        else
        {
            insert_seg_lst(lst_aux, seg, size);
            return 0;
        }
    }
    else
    {
        aux = (*lst);
        while(aux->next != NULL)
        {
            aux = (seglst_t *)aux->next;
        }
        if(seg->seq_num == base)
        {
            aux->next = (struct seglst_t *)malloc(sizeof(seglst_t));
            aux = (seglst_t *)aux->next;
            aux->segm = seg;
            aux->size = size;
            aux->next = NULL;

            ret = size;
            count = (count + size) % MAX_SEQ_NUM;
            while(1==1)
            {
                aux2 = is_next(lst_aux, count);
                if(aux2 == NULL)
                {
                    break;
                }
                count = (count + aux2->size) % MAX_SEQ_NUM;
                aux2->next = NULL;
                aux->next = (struct seglst_t *)aux2;
                aux = aux2;
                ret += aux2->size;
            }
            return ret;
        }
        else
        {
            insert_seg_lst(lst_aux, seg, size);
            return 0;
        }
    }
}