char *cmlsh_filename_completion_function(const char *text, int state)
{
char current_dir[PATH_MAX];
char abs_path[PATH_MAX];
int is_prefix_of_existing = 0;
int should_reset = 0;
struct stat st;
int flash_len = NOS_CMLSH_FLASH_DIR_LEN; int is_flash_mode = (strncmp(text, NOS_CMLSH_FLASH_DIR, flash_len) == 0); int is_flash_prefix = (strncmp(NOS_CMLSH_FLASH_DIR, text, strlen(text)) == 0) && (strlen(text) < flash_len); const char *compare_text = text; if (is_flash_mode && strncmp(text, NOS_CMLSH_FLASH_DIR, flash_len) == 0) { compare_text = text + flash_len; } const char *slash = strrchr(compare_text, '/'); const char *base = slash ? (slash + 1) : compare_text; int base_len = strlen(base); if (comp_state.matches && comp_state.count > 0 && base_len > 0) { int all_match = 1; for (int i = 0; i < comp_state.count; i++) { if (comp_state.matches[i] == NULL) continue; if (strncmp(comp_state.matches[i], base, base_len) != 0) { all_match = 0; break; } } is_prefix_of_existing = all_match; } else { is_prefix_of_existing = 0; } getcwd(current_dir, sizeof(current_dir)); if (is_flash_mode) { char flash_path[PATH_MAX]; snprintf(flash_path, sizeof(flash_path), "%s%s", NOS_CMLSH_HOME_DIR, text + flash_len); snprintf(abs_path, sizeof(abs_path), "%s", flash_path); } else { snprintf(abs_path, sizeof(abs_path), "%s/%s", current_dir, text); } should_reset = !comp_state.initialized || comp_state.matches == NULL || (!is_prefix_of_existing && access(abs_path, F_OK) != 0) || (comp_state.count == 1 && stat(abs_path, &st) == 0 && S_ISDIR(st.st_mode)) || (comp_state.count == 0 && stat(abs_path, &st) == 0 && S_ISDIR(st.st_mode)) || (comp_state.current_text && strlen(text) < strlen(comp_state.current_text)); if (comp_state.matches && comp_state.count == 1 && stat(abs_path, &st) == 0 && !S_ISDIR(st.st_mode)) { comp_state.initialized = 0; return NULL; } if (should_reset) { char *saved_last_prefix = NULL; char *saved_base_dir = NULL; int saved_flash_mode = comp_state.flash_mode; if (comp_state.last_prefix) { saved_last_prefix = XSTRDUP(MTYPE_TMP, comp_state.last_prefix); } if (comp_state.base_dir) { saved_base_dir = XSTRDUP(MTYPE_TMP, comp_state.base_dir); } cmlsh_reset_completion_state(); char *current_base = NULL; char *current_prefix = NULL; int current_level = 0; cmlsh_parse_input_text(text, ¤t_base, ¤t_prefix, ¤t_level); if (saved_last_prefix) { comp_state.last_prefix = saved_last_prefix; } else { comp_state.last_prefix = NULL; } if (saved_base_dir) { comp_state.base_dir = saved_base_dir; } else { comp_state.base_dir = NULL; } const char *use_prefix = current_prefix; if ((current_prefix == NULL || *current_prefix == '\0') && comp_state.last_prefix && comp_state.last_prefix[0] != '\0' && comp_state.base_dir && current_base && strcmp(comp_state.base_dir, current_base) == 0) { use_prefix = comp_state.last_prefix; } if (comp_state.base_dir == NULL || strcmp(comp_state.base_dir, current_base) != 0) { if (comp_state.base_dir) { XFREE(MTYPE_TMP, comp_state.base_dir); } comp_state.base_dir = XSTRDUP(MTYPE_TMP, current_base); } comp_state.flash_mode = saved_flash_mode; comp_state.current_level = current_level; comp_state.current_text = XSTRDUP(MTYPE_TMP, text); if (is_flash_mode) { const char *rel_path = comp_state.base_dir + flash_len; int needed = NOS_CMLSH_HOME_DIR_LEN + strlen(rel_path) + 2; char *actual_path = XCALLOC(MTYPE_TMP, needed); snprintf(actual_path, needed, "%s%s", NOS_CMLSH_HOME_DIR, rel_path); if (actual_path[strlen(actual_path) - 1] != '/') { actual_path = XREALLOC(MTYPE_TMP, actual_path, strlen(actual_path) + 2); strcat(actual_path, "/"); } comp_state.matches = cmlsh_generate_current_dir_paths(actual_path, use_prefix, current_level == 0); XFREE(MTYPE_TMP, actual_path); } else if (is_flash_prefix) { comp_state.matches = cmlsh_generate_current_dir_paths(current_base, use_prefix, 1); int count = 0; if (comp_state.matches) { while (comp_state.matches[count]) count++; } else { comp_state.matches = XCALLOC(MTYPE_TMP, 2 * sizeof(char*)); } int found = 0; for (int i = 0; i < count; i++) { if (strcmp(comp_state.matches[i], NOS_CMLSH_FLASH_DIR) == 0) { found = 1; break; } } if (!found) { comp_state.matches = XREALLOC(MTYPE_TMP, comp_state.matches, (count + 2) * sizeof(char*)); comp_state.matches[count] = XSTRDUP(MTYPE_TMP, NOS_CMLSH_FLASH_DIR); comp_state.matches[count + 1] = NULL; } comp_state.count = count + (found ? 0 : 1); } else { comp_state.matches = cmlsh_generate_current_dir_paths(current_base, use_prefix, current_level == 0); } if (current_prefix && *current_prefix) { if (comp_state.last_prefix) { XFREE(MTYPE_TMP, comp_state.last_prefix); } comp_state.last_prefix = XSTRDUP(MTYPE_TMP, current_prefix); } comp_state.count = 0; if (comp_state.matches) { while (comp_state.matches[comp_state.count]) { comp_state.count++; } } comp_state.last_state = -1; comp_state.initialized = 1; } if (!comp_state.matches || comp_state.count == 0) { comp_state.initialized = 0; return NULL; } int next_index = (comp_state.last_state + 1) % comp_state.count; comp_state.last_state = next_index; if (comp_state.count == 1) { next_index = 0; } char *match = comp_state.matches[next_index]; char *full_path = NULL; if (strcmp(match, NOS_CMLSH_FLASH_DIR) == 0) { full_path = XSTRDUP(MTYPE_TMP, NOS_CMLSH_FLASH_DIR); } else if (comp_state.flash_mode) { char *virtual_base = comp_state.base_dir; char *virtual_path = NULL; if (virtual_base && strlen(virtual_base) > 0) { int base_len = strlen(virtual_base); int needs_slash = (base_len > 0 && virtual_base[base_len - 1] != '/'); virtual_path = XCALLOC(MTYPE_TMP, base_len + strlen(match) + 2); if (needs_slash) { snprintf(virtual_path, base_len + strlen(match) + 2, "%s/%s", virtual_base, match); } else { snprintf(virtual_path, base_len + strlen(match) + 1, "%s%s", virtual_base, match); } } else { virtual_path = XSTRDUP(MTYPE_TMP, match); } full_path = virtual_path; } else { if (comp_state.base_dir && strlen(comp_state.base_dir) > 0) { int base_len = strlen(comp_state.base_dir); int needs_slash = (base_len > 0 && comp_state.base_dir[base_len - 1] != '/'); full_path = XCALLOC(MTYPE_TMP, base_len + strlen(match) + 2); if (needs_slash) { snprintf(full_path, base_len + strlen(match) + 2, "%s/%s", comp_state.base_dir, match); } else { snprintf(full_path, base_len + strlen(match) + 1, "%s%s", comp_state.base_dir, match); } } else { full_path = XSTRDUP(MTYPE_TMP, match); } } return full_path;
}
char **cmlsh_filename_completion_matches(const char text, CPFunction genfunc)
{
if (text == NULL || *text == ‘\0’) {
comp_state.initialized = 0;
return NULL;
}
char *match = (*genfunc)(text, 0); if (!match) { return NULL; } char **matches = (char **)XCALLOC(MTYPE_TMP, 2 * sizeof(char *)); matches[0] = match; matches[1] = NULL; return matches;
}
char** cmlsh_completion(char* text, int start, int end)
{
char** matches;
char** filename_matches = NULL;
int is_fs_command = 0;
int fs_cmd_len = 0;
int i;
const LineInfo* li;
li = el_line(e); rl_line_buffer = (char*)li->buffer; rl_end = li->lastchar - li->buffer; rl_line_buffer[rl_end] = '\0'; matches = completion_matches(text, cmlsh_completion_matches); for (i = 0; cmlsh_fs_commands[i]; i++) { int len = strlen(cmlsh_fs_commands[i]); if (strncmp(rl_line_buffer, cmlsh_fs_commands[i], len) == 0 && (rl_line_buffer[len] == ' ' || rl_line_buffer[len] == '\0')) { is_fs_command = 1; fs_cmd_len = len; break; } } if (!matches || !matches[0]) { if (matches) { for (i = 0; matches[i]; i++) { XFREE(MTYPE_TMP,matches[i]); } XFREE(MTYPE_TMP,matches); matches = NULL; } if (is_fs_command && start > fs_cmd_len) { filename_matches = cmlsh_filename_completion_matches(text, cmlsh_filename_completion_function); } if (!filename_matches || !filename_matches[0]) { return NULL; } #if 1 int matches_num, maxlen, match_len, match_display = 1; for (i = 0, maxlen = 0; comp_state.matches[i]; i++) { match_len = strlen(comp_state.matches[i]); if (match_len > maxlen) maxlen = match_len; } matches_num = i; printf("\n"); if (match_display) rl_display_match_list_tmp(comp_state.matches, matches_num, maxlen); #endif return NULL; } return matches;
}
在这个基础上优化成linux原生的补全效果,不要另写钩子函数
最新发布