void cmd_print_usage (int cmd_index, int cmd_index_sub) { if(GCMD_NAME(cmd_index)) dprintf("usage: %s", GCMD_NAME(cmd_index)); if (GCMD_SUB_NAME(cmd_index, cmd_index_sub)) dprintf(" %s", GCMD_SUB_NAME(cmd_index, cmd_index_sub)); if(GCMD_SUB_ACT(cmd_index, cmd_index_sub) && GCMD_SUB_USAGE(cmd_index, cmd_index_sub)) dprintf(" %s %s\n\n", GCMD_SUB_ACT(cmd_index, cmd_index_sub), GCMD_SUB_USAGE(cmd_index, cmd_index_sub)); }
/*command string lookup*/ a_uint32_t cmd_lookup(char **cmd_str, int *cmd_index, int *cmd_index_sub) { a_uint32_t no, sub_no; a_uint32_t cmd_deepth = 0; *cmd_index = GCMD_DESC_NO_MATCH; *cmd_index_sub = GCMD_DESC_NO_MATCH; if (cmd_str[0] == NULL) return cmd_deepth; for (no = 0; GCMD_DESC_VALID(no); no++) { if (strcasecmp(cmd_str[0], GCMD_NAME(no))) continue; for (sub_no = 0; GCMD_SUB_DESC_VALID(no, sub_no); sub_no++) { if (cmd_str[1] != NULL && cmd_str[2] != NULL) { if (GCMD_SUB_NAME(no, sub_no) && GCMD_SUB_ACT(no, sub_no) && !strcasecmp(cmd_str[1], GCMD_SUB_NAME(no, sub_no)) && !strcasecmp(cmd_str[2], GCMD_SUB_ACT(no, sub_no))) { *cmd_index = no; *cmd_index_sub = sub_no; cmd_deepth = 3; return cmd_deepth; } else if (!GCMD_SUB_NAME(no, sub_no) && GCMD_SUB_ACT(no, sub_no) && !strcasecmp(cmd_str[1], GCMD_SUB_ACT(no, sub_no))) { *cmd_index = no; *cmd_index_sub = sub_no; cmd_deepth = 2; return cmd_deepth; } } else if (cmd_str[1] != NULL && cmd_str[2] == NULL) { if (!GCMD_SUB_NAME(no, sub_no) && GCMD_SUB_ACT(no, sub_no) && !strcasecmp(cmd_str[1], GCMD_SUB_ACT(no, sub_no))) { *cmd_index = no; *cmd_index_sub = sub_no; cmd_deepth = 2; return cmd_deepth; } } } } return cmd_deepth; }
static int search_cmd_sub(int cmd_id, int *pmatch_sub_id, char *sub_name, int is_print) { int cmd_sub_no, pmatch_sub_nr = 0, fmatch_sub_save = GCMD_DESC_NO_MATCH; *pmatch_sub_id = GCMD_DESC_NO_MATCH; /*search for full matched */ for (cmd_sub_no = 0; GCMD_SUB_DESC_VALID(cmd_id, cmd_sub_no); cmd_sub_no++) { if (!GCMD_SUB_NAME(cmd_id, cmd_sub_no)) continue; if (!strcasecmp(sub_name, GCMD_SUB_NAME(cmd_id, cmd_sub_no))) { /*full matched */ printc(is_print, "%-10s\t%s\n", GCMD_SUB_ACT(cmd_id, cmd_sub_no), GCMD_SUB_MEMO(cmd_id, cmd_sub_no)); //*pmatch_sub_id = cmd_sub_no; if(fmatch_sub_save == GCMD_DESC_NO_MATCH) { *pmatch_sub_id = fmatch_sub_save = cmd_sub_no; } } else if (!strncasecmp(sub_name, GCMD_SUB_NAME(cmd_id, cmd_sub_no), strlen(sub_name))) { if(fmatch_sub_save != GCMD_DESC_NO_MATCH) continue; /*partly matched */ if (*pmatch_sub_id == GCMD_DESC_NO_MATCH || (GCMD_SUB_NAME(cmd_id, cmd_sub_no-1) && strcasecmp(GCMD_SUB_NAME(cmd_id, cmd_sub_no), GCMD_SUB_NAME(cmd_id, cmd_sub_no-1)))) { printc(is_print, "%-10s\t", GCMD_SUB_NAME(cmd_id, cmd_sub_no)); pmatch_sub_nr++; *pmatch_sub_id = cmd_sub_no; } } } if (pmatch_sub_nr > 1) printc(is_print, "\n"); return pmatch_sub_nr; }
static int search_cmd_action(int cmd_id, int *pmatch_act_id, char *sub_name, char *action, int is_print) { int cmd_act_no = 0, pmatch_act_nr = 0; *pmatch_act_id = GCMD_DESC_NO_MATCH; /*search for full matched */ for (cmd_act_no = 0; GCMD_SUB_DESC_VALID(cmd_id, cmd_act_no); cmd_act_no++) { if (strcasecmp(sub_name, GCMD_SUB_NAME(cmd_id, cmd_act_no))) continue; if (!GCMD_SUB_ACT(cmd_id, cmd_act_no)) continue; if (!strcasecmp(action, GCMD_SUB_ACT(cmd_id, cmd_act_no))) { /*full matched */ if (*pmatch_act_id == GCMD_DESC_NO_MATCH) { printc(is_print, "%-10s\n", GCMD_SUB_USAGE(cmd_id, cmd_act_no)); } *pmatch_act_id = cmd_act_no; break; } else if (!strncasecmp(action, GCMD_SUB_ACT(cmd_id, cmd_act_no), strlen(action))) { /*partly matched */ if (*pmatch_act_id == GCMD_DESC_NO_MATCH ||( GCMD_SUB_ACT(cmd_id, cmd_act_no-1) && strcasecmp(GCMD_SUB_ACT(cmd_id, cmd_act_no), GCMD_SUB_ACT(cmd_id, cmd_act_no-1)))) { printc(is_print, "%-10s\t%s\n", GCMD_SUB_ACT(cmd_id, cmd_act_no), GCMD_SUB_MEMO(cmd_id, cmd_act_no)); pmatch_act_nr++; *pmatch_act_id = cmd_act_no; } } } return pmatch_act_nr; }
static void handle_tab(void) { int cmd_nr = 0; char matchBuf[MATCH_BUF_MAX]; char *tmp_str[3]; memset(matchBuf, 0, MATCH_BUF_MAX); strncpy(matchBuf, cmd_strp, cmd_cursor); printf("\n"); /* split command string into temp array */ tmp_str[cmd_nr] = (void *) strtok(matchBuf, " "); if(!tmp_str[cmd_nr]) { print_cmd_all(); if (cmd_promptp) printf("\n%s%s", cmd_promptp, cmd_strp); _cursor_recover(); return; } while (tmp_str[cmd_nr]) { if (++cmd_nr == 3) break; tmp_str[cmd_nr] = (void *) strtok(NULL, " "); } int is_print = 1, is_completed = 0; int pmatch_nr = 0, pmatch_id = GCMD_DESC_NO_MATCH; pmatch_nr = search_cmd_type(tmp_str[0], &pmatch_id, is_print); if (cmd_nr == 1) { if (ONE_PART_MATCHED(pmatch_nr, pmatch_id)) { _cmd_complete(tmp_str[0], GCMD_NAME(pmatch_id)); is_completed = 1; } if(NONE_MATCHED(pmatch_nr, pmatch_id)) { print_cmd_all(); } else if(FULL_MATCHED(pmatch_nr, pmatch_id)) { print_sub_all(pmatch_id); } } else if (cmd_nr > 1) { if (FULL_MATCHED(pmatch_nr, pmatch_id)) { int pmatch_sub_nr = 0, pmatch_sub_id = GCMD_DESC_NO_MATCH; if(cmd_nr == 3) is_print = 0; pmatch_sub_nr = search_cmd_sub(pmatch_id, &pmatch_sub_id, tmp_str[1], is_print); if(cmd_nr == 2) { if (ONE_PART_MATCHED(pmatch_sub_nr, pmatch_sub_id)) { _cmd_complete(tmp_str[1], GCMD_SUB_NAME(pmatch_id, pmatch_sub_id)); is_completed = 1; } } else if (cmd_nr == 3){ int pmatch_act_nr = 0, pmatch_act_id = GCMD_DESC_NO_MATCH; pmatch_act_nr = search_cmd_action(pmatch_id, &pmatch_act_id, tmp_str[1], tmp_str[2], is_print); if (ONE_PART_MATCHED(pmatch_act_nr, pmatch_act_id)) { _cmd_complete(tmp_str[2], GCMD_SUB_ACT(pmatch_id, pmatch_act_id)); is_completed = 1; } else if (FULL_MATCHED(pmatch_act_nr, pmatch_act_id)) { is_print = 1; printc(is_print, "%-10s\n", GCMD_SUB_USAGE(pmatch_id, pmatch_act_id)); } } } } if (is_completed == 0) { /*re-echo */ if (cmd_promptp) printf("\n%s%s", cmd_promptp, cmd_strp); _cursor_recover(); } //_cursor_recover(); }