char* match_cmd(const char* text, int state) { static STR_FUNC_MAP_ITER it; static int len = 0; const char* cmd = NULL; if (!state) { it = g_cmd_map.begin(); len = strlen(text); } while(it != g_cmd_map.end()) { cmd = it->first.c_str(); it++; if (strncmp(cmd, text, len) == 0) { int32_t cmd_len = strlen(cmd) + 1; // memory will be freed by readline return strncpy(new char[cmd_len], cmd, cmd_len); } } return NULL; }
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); }
int cmd_show_help(const VSTRING&) { fprintf(stderr, "\nsupported command:"); for (STR_FUNC_MAP_ITER it = g_cmd_map.begin(); it != g_cmd_map.end(); it++) { fprintf(stderr, "\n%-40s %s", it->second.syntax_, it->second.info_); } fprintf(stderr, "\n\n"); return TFS_SUCCESS; }
int ToolUtil::show_help(const STR_FUNC_MAP& cmd_map) { fprintf(stdout, "\nsupported command:"); for (STR_FUNC_MAP_CONST_ITER it = cmd_map.begin(); it != cmd_map.end(); it++) { fprintf(stdout, "\n%-40s %s", it->second.syntax_, it->second.info_); } fprintf(stdout, "\n\n"); return TFS_SUCCESS; }
// 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; }