Skip to contentSkip to conversation

shared/shell: Enable generator for submenus

PATCH Applied Luiz Augusto von Dentz 1 participant 2 messages Mar 15, 2018 – Mar 16, 2018 Bluetooth drivers

lore
Share & tools
lore

Luiz Augusto von Dentz

author maintainer
#0 lore raw

PATCH 1/1 shared/shell: Enable generator for submenus

From: Luiz Augusto von Dentz <[email protected]>

This enables command generator to work with submenu commands like:

[bluetooth]# advertise.

advertise.appearance    advertise.manufacturer  advertise.timeout
advertise.clear         advertise.name          advertise.tx-power
advertise.duration      advertise.service       advertise.uuids

Note that by default submenus commands will not be shown until . is present in the input, that way only one command list is active at time.

---
 src/shared/shell.c | 39 +++++++++++++++++++++++++++++++++------
 1 file changed, 33 insertions(+), 6 deletions(-)
src/shared/shell.c +33 -6
index 3b680d7b0..0a05b5215 100644
@@ -102,7 +102,7 @@ static void cmd_help(int argc, char *argv[])
 	return bt_shell_noninteractive_quit(EXIT_SUCCESS);
 }
 
-static const struct bt_shell_menu *find_menu(const char *name)
+static const struct bt_shell_menu *find_menu(const char *name, size_t len)
 {
 	const struct queue_entry *entry;
 
@@ -110,8 +110,10 @@ static const struct bt_shell_menu *find_menu(const char *name)
 						entry = entry->next) {
 		struct bt_shell_menu *menu = entry->data;
 
-		if (!strcmp(menu->name, name))
+		if (!strncmp(menu->name, name, len))
 			return menu;
+
+
 	}
 
 	return NULL;
@@ -151,7 +153,7 @@ static void cmd_menu(int argc, char *argv[])
 		return bt_shell_noninteractive_quit(EXIT_FAILURE);
 	}
 
-	menu = find_menu(argv[1]);
+	menu = find_menu(argv[1], strlen(argv[1]));
 	if (!menu) {
 		bt_shell_printf("Unable find menu with name: %s\n", argv[1]);
 		return bt_shell_noninteractive_quit(EXIT_FAILURE);
@@ -403,7 +405,7 @@ static int submenu_exec(int argc, char *argv[])
 	len = name - argv[0];
 	name[0] = '\0';
 
-	submenu = find_menu(argv[0]);
+	submenu = find_menu(argv[0], strlen(argv[0]));
 	if (!submenu)
 		return -ENOENT;
 
@@ -601,12 +603,15 @@ static char *find_cmd(const char *text,
 static char *cmd_generator(const char *text, int state)
 {
 	static int index;
-	static bool default_menu_enabled;
+	static bool default_menu_enabled, submenu_enabled;
+	static const struct bt_shell_menu *menu;
 	char *cmd;
 
 	if (!state) {
 		index = 0;
+		menu = NULL;
 		default_menu_enabled = true;
+		submenu_enabled = false;
 	}
 
 	if (default_menu_enabled) {
@@ -615,11 +620,33 @@ static char *cmd_generator(const char *text, int state)
 			return cmd;
 		} else {
 			index = 0;
+			menu = data.menu;
 			default_menu_enabled = false;
 		}
 	}
 
-	return find_cmd(text, data.menu->entries, &index);
+	if (!submenu_enabled) {
+		cmd = find_cmd(text, menu->entries, &index);
+		if (cmd || menu != data.main)
+			return cmd;
+
+		cmd = strrchr(text, '.');
+		if (!cmd)
+			return NULL;
+
+		menu = find_menu(text, cmd - text);
+		if (!menu)
+			return NULL;
+
+		index = 0;
+		submenu_enabled = true;
+	}
+
+	cmd = find_cmd(text + strlen(menu->name) + 1, menu->entries, &index);
+	if (cmd)
+		asprintf(&cmd, "%s.%s", menu->name, cmd);
+
+	return cmd;
 }
 
 static wordexp_t args;
Signature · 1 line
2.14.3
author maintainer Applied
#1 lore raw

Hi,

Quoted 111 lines · Luiz Augusto von Dentz

From: Luiz Augusto von Dentz <[email protected]>

This enables command generator to work with submenu commands like:

[bluetooth]# advertise.

advertise.appearance    advertise.manufacturer  advertise.timeout
advertise.clear         advertise.name          advertise.tx-power
advertise.duration      advertise.service       advertise.uuids

Note that by default submenus commands will not be shown until . is present in the input, that way only one command list is active at time.

---
 src/shared/shell.c | 39 +++++++++++++++++++++++++++++++++------
 1 file changed, 33 insertions(+), 6 deletions(-)
src/shared/shell.c +33 -7
index 3b680d7b0..0a05b5215 100644
@@ -102,7 +102,7 @@ static void cmd_help(int argc, char *argv[])
        return bt_shell_noninteractive_quit(EXIT_SUCCESS);
 }
 
-static const struct bt_shell_menu *find_menu(const char *name)
+static const struct bt_shell_menu *find_menu(const char *name, size_t len)
 {
        const struct queue_entry *entry;
 
@@ -110,8 +110,10 @@ static const struct bt_shell_menu *find_menu(const char *name)
                                                entry = entry->next) {
                struct bt_shell_menu *menu = entry->data;
 
-               if (!strcmp(menu->name, name))
+               if (!strncmp(menu->name, name, len))
                        return menu;
+
+
        }
 
        return NULL;
@@ -151,7 +153,7 @@ static void cmd_menu(int argc, char *argv[])
                return bt_shell_noninteractive_quit(EXIT_FAILURE);
        }
 
-       menu = find_menu(argv[1]);
+       menu = find_menu(argv[1], strlen(argv[1]));
        if (!menu) {
                bt_shell_printf("Unable find menu with name: %s\n", argv[1]);
                return bt_shell_noninteractive_quit(EXIT_FAILURE);
@@ -403,7 +405,7 @@ static int submenu_exec(int argc, char *argv[])
        len = name - argv[0];
        name[0] = '\0';
 
-       submenu = find_menu(argv[0]);
+       submenu = find_menu(argv[0], strlen(argv[0]));
        if (!submenu)
                return -ENOENT;
 
@@ -601,12 +603,15 @@ static char *find_cmd(const char *text,
 static char *cmd_generator(const char *text, int state)
 {
        static int index;
-       static bool default_menu_enabled;
+       static bool default_menu_enabled, submenu_enabled;
+       static const struct bt_shell_menu *menu;
        char *cmd;
 
        if (!state) {
                index = 0;
+               menu = NULL;
                default_menu_enabled = true;
+               submenu_enabled = false;
        }
 
        if (default_menu_enabled) {
@@ -615,11 +620,33 @@ static char *cmd_generator(const char *text, int state)
                        return cmd;
                } else {
                        index = 0;
+                       menu = data.menu;
                        default_menu_enabled = false;
                }
        }
 
-       return find_cmd(text, data.menu->entries, &index);
+       if (!submenu_enabled) {
+               cmd = find_cmd(text, menu->entries, &index);
+               if (cmd || menu != data.main)
+                       return cmd;
+
+               cmd = strrchr(text, '.');
+               if (!cmd)
+                       return NULL;
+
+               menu = find_menu(text, cmd - text);
+               if (!menu)
+                       return NULL;
+
+               index = 0;
+               submenu_enabled = true;
+       }
+
+       cmd = find_cmd(text + strlen(menu->name) + 1, menu->entries, &index);
+       if (cmd)
+               asprintf(&cmd, "%s.%s", menu->name, cmd);
+
+       return cmd;
 }
 
 static wordexp_t args;
--
Quoted 1 line

2.14.3

Applied.

Signature · 1 line
Luiz Augusto von Dentz