diff --git a/src/shared/shell.c b/src/shared/shell.c
index f20c749..3aa4959 100644
--- a/src/shared/shell.c
+++ b/src/shared/shell.c
shell_print_menu();
}
+static bool cmd_menu_exists(const struct bt_shell_menu *menu)
+{
+ /* Skip menu command if not on main menu or if there are no
+ * submenus.
+ */
+ if (menu != data.main || queue_isempty(data.submenus))
+ return false;
+
+ return true;
+}
+
static void cmd_back(int argc, char *argv[])
{
if (data.menu == data.main) {
shell_print_menu();
}
+static bool cmd_back_exists(const struct bt_shell_menu *menu)
+{
+ /* Skip back command if on main menu */
+ if (menu == data.main)
+ return false;
+
+ return true;
+}
+
static const struct bt_shell_menu_entry default_menu[] = {
- { "back", NULL, cmd_back, "Return to main menu" },
+ { "back", NULL, cmd_back, "Return to main menu", NULL,
+ NULL, cmd_back_exists },
{ "menu", "<name>", cmd_menu, "Select submenu",
- menu_generator },
+ menu_generator, NULL,
+ cmd_menu_exists},
{ "version", NULL, cmd_version, "Display version" },
{ "quit", NULL, cmd_quit, "Quit program" },
{ "exit", NULL, cmd_quit, "Quit program" },
{ }
};
-static bool command_isskipped(const char *cmd)
-{
- /* Skip menu command if not on main menu or if there are no
- * submenus.
- */
- if (!strcmp(cmd, "menu") &&
- (data.menu != data.main || queue_isempty(data.submenus)))
- return true;
-
- /* Skip back command if on main menu */
- if (data.menu == data.main && !strcmp(cmd, "back"))
- return true;
-
- return false;
-}
-
static void shell_print_menu(void)
{
const struct bt_shell_menu_entry *entry;
}
for (entry = default_menu; entry->cmd; entry++) {
- if (command_isskipped(entry->cmd))
+ if (entry->exists && !entry->exists(data.menu))
continue;
print_menu(entry->cmd, entry->arg ? : "", entry->desc ? : "");
static char *find_cmd(const char *text,
const struct bt_shell_menu_entry *entry, int *index)
{
- const char *cmd;
+ const struct bt_shell_menu_entry *tmp;
int len;
len = strlen(text);
- while ((cmd = entry[*index].cmd)) {
+ while ((tmp = &entry[*index])) {
(*index)++;
- if (!strncmp(cmd, text, len) && !command_isskipped(cmd))
- return strdup(cmd);
+ if (!tmp->cmd)
+ break;
+
+ if (tmp->exists && !tmp->exists(data.menu))
+ continue;
+
+ if (!strncmp(tmp->cmd, text, len))
+ return strdup(tmp->cmd);
}
return NULL;
diff --git a/src/shared/shell.h b/src/shared/shell.h
index 2b9e918..5f86ed6 100644
--- a/src/shared/shell.h
+++ b/src/shared/shell.h
#define COLOR_BOLDWHITE "\x1B[1;37m"
#define COLOR_HIGHLIGHT "\x1B[1;39m"
+struct bt_shell_menu;
+
typedef void (*bt_shell_menu_cb_t)(int argc, char *argv[]);
typedef char * (*bt_shell_menu_gen_t)(const char *text, int state);
typedef void (*bt_shell_menu_disp_t) (char **matches, int num_matches,
int max_length);
typedef void (*bt_shell_prompt_input_func) (const char *input, void *user_data);
+typedef bool (*bt_shell_menu_exists_t) (const struct bt_shell_menu *menu);
struct bt_shell_menu_entry {
const char *cmd;
const char *desc;
bt_shell_menu_gen_t gen;
bt_shell_menu_disp_t disp;
+ bt_shell_menu_exists_t exists;
};
struct bt_shell_menu {