Beispiel #1
0
int32_t do_cmd(char* key)
{
  key = strip_line(key);
  if (!key[0])
  {
    return TFS_SUCCESS;
  }

#ifdef _WITH_READ_LINE
  // not blank line, add to history
  add_history(key);
#endif

  char* token = strchr(key, ' ');
  if (token != NULL)
  {
    *token = '\0';
  }

  STR_FUNC_MAP_ITER it = g_cmd_map.find(Func::str_to_lower(key));

  if (it == g_cmd_map.end())
  {
    fprintf(stderr, "unknown command. \n");
    return TFS_ERROR;
  }
  // ok this is current command
  g_cur_cmd = key;

  if (token != NULL)
  {
    token++;
    key = token;
  }
  else
  {
    key = NULL;
  }

  VSTRING param;
  param.clear();
  while ((token = strsep(&key, " ")) != NULL)
  {
    if ('\0' == token[0])
    {
      continue;
    }
    param.push_back(token);
  }

  // check param count
  int32_t param_cnt = param.size();
  if (param_cnt < it->second.min_param_cnt_ || param_cnt > it->second.max_param_cnt_)
  {
    fprintf(stderr, "%s\t\t%s\n\n", it->second.syntax_, it->second.info_);
    return TFS_ERROR;
  }

  return it->second.func_(param);
}
Beispiel #2
0
// get nth word in current input to store, base on keyth word to check count
int get_nth_word(int32_t nth, char* store, int32_t keyth)
{
  int32_t count = 0, pos = 0, start = 0, key_start = 0, key_end = 0, end = rl_point;
  store[0] = '\0';

  // reach current word's start
  while (end && !is_whitespace(rl_line_buffer[end]))
  {
    end--;
  }

  while (pos < end)
  {
    // reach next word's start
    while (pos < end && is_whitespace(rl_line_buffer[pos]))
    {
      pos++;
    }

    start = pos;
    // reach next word's end
    while (pos < end && !is_whitespace(rl_line_buffer[pos]))
    {
      pos++;
    }

    if (pos != start)
    {
      count++;
    }

    if (keyth == count)         // found key
    {
      key_start = start;
      key_end = pos;
    }
    if (nth == count)           // found nth
    {
      int len = pos - start;
      strncpy(store, rl_line_buffer+start, len);
      store[len] = '\0';
    }
  }

  if (count >= keyth)                // already have key, check count
  {
    char* key = rl_copy_text(key_start, key_end);
    STR_FUNC_MAP_ITER it;
    if ((it = g_cmd_map.find(key)) != g_cmd_map.end()) // valid key
    {
      if (count > it->second.min_param_cnt_) // over count
      {
        if (count < it->second.max_param_cnt_)           // valid over count
        {
          count = INDEX_COMPLETE;
        }
        else
        {
          count = STOP_COMPLETE;
        }
      }
    }
    else                        // invalid key
    {
      count = STOP_COMPLETE;
    }
    free(key);
    key = NULL;
  }

  return count;
}