static int builtin_help(shell_context_t *ctx, int argc, char *argv[]) { int maxwidth = 0; shell_enumerate_commands(ctx, help_maxwidth_function, &maxwidth); maxwidth += 2; return shell_enumerate_commands(ctx, help_print_function, &maxwidth); }
static char *shell_completion(const char *partial, int state) { static int completion_pass = 0, completion_matches_offset = 0; if(state == 0) { if(completion_matches) { for(int i = 0; i < completion_matches_count; i++) if(completion_matches[i]) free(completion_matches[i]); free(completion_matches); completion_matches = NULL; completion_matches_count = 0; } completion_pass = 0; char *tmp = rl_line_buffer; while(isspace(*tmp)) tmp++; int not_first = 0; for(; *tmp; tmp++) { for(const char *sep = rl_basic_word_break_characters; *sep; sep++) { if(*tmp == *sep) { not_first = 1; break; } } if(not_first) break; } if(not_first) { completion_pass = 1; return rl_filename_completion_function(partial, state); } else { shell_enumerate_commands(completion_context, shell_completion_filler, (void *) partial); completion_matches_offset = 0; } } if(completion_pass) { return rl_filename_completion_function(partial, state); } else if(completion_matches_offset == completion_matches_count) { return NULL; } else { char *val = completion_matches[completion_matches_offset]; completion_matches[completion_matches_offset++] = NULL; return val; } }
int shell_run(shell_context_t *ctx, int argc, char *argv[]) { shell_run_data_t data = { argc, argv }; int ret = shell_enumerate_commands(ctx, shell_run_function, &data); if(ret == 0) { debug(LEVEL_ERROR, "Bad command '%s'\n", argv[0]); errno = EINVAL; return -1; } else if(ret == 1) { return 0; } else return ret; }