int cmd_opt(char **arg) { const char *opt_text = get_arg(arg); struct opdb_key key; union opdb_value value; if (opt_text) { if (opdb_get(opt_text, &key, &value) < 0) { printc_err("opt: no such option: %s\n", opt_text); return -1; } } if (**arg) { if (parse_option(key.type, &value, *arg) < 0) { printc_err("opt: can't parse option: %s\n", *arg); return -1; } opdb_set(key.name, &value); } else if (opt_text) { display_option(NULL, &key, &value); } else { opdb_enum(display_option, NULL); } return 0; }
int cmd_help(char **arg) { const char *topic = get_arg(arg); if (topic) { struct cmddb_record cmd; struct opdb_key key; if (!cmddb_get(topic, &cmd)) { printc("\x1b[1mCOMMAND: %s\x1b[0m\n\n%s\n", cmd.name, cmd.help); return 0; } if (!opdb_get(topic, &key, NULL)) { printc("\x1b[1mOPTION: %s (%s)\x1b[0m\n\n%s\n", key.name, type_text(key.type), key.help); return 0; } printc_err("help: unknown command: %s\n", topic); return -1; } else { struct vector v; vector_init(&v, sizeof(const char *)); if (!cmddb_enum(push_command_name, &v)) { printc("Available commands:\n"); namelist_print(&v); printc("\n"); } else { pr_error("help: can't allocate memory for command list"); } vector_realloc(&v, 0); if (!opdb_enum(push_option_name, &v)) { printc("Available options:\n"); namelist_print(&v); printc("\n"); } else { pr_error("help: can't allocate memory for option list"); } vector_destroy(&v); printc("Type \"help <topic>\" for more information.\n"); printc("Use the \"opt\" command (\"help opt\") to set " "options.\n"); #if defined(__Windows__) && !defined(USE_READLINE) printc("Press Ctrl+Z, Enter to quit.\n"); #else printc("Press Ctrl+D to quit.\n"); #endif } return 0; }
static char *option_generator(const char *text, int state) { static struct find_context find_data; if (state == 0) { // start a new search find_data.search_text = text; find_data.skip_until = NULL; } else { // continue searching after the last match find_data.skip_until = find_data.result; } find_data.result = NULL; opdb_enum(opdb_find_option, &find_data); if (find_data.result) return strdup(find_data.result); return NULL; }