Diff between 251a2b3ca4d31391dbd08d966909290c090f244a and fa6db23fd1c14d9e384461a6d3f60ebc71886c30

Changed Files

File Additions Deletions Status
src/shared/shell.c +29 -15 modified

Full Patch

diff --git a/src/shared/shell.c b/src/shared/shell.c
index 378c0c0..c271a53 100644
--- a/src/shared/shell.c
+++ b/src/shared/shell.c
@@ -390,32 +390,46 @@ done:
 	free(input);
 }
 
-static char *cmd_generator(const char *text, int state)
+static char *find_cmd(const char *text,
+			const struct bt_shell_menu_entry *entry, int *index)
 {
-	static const struct bt_shell_menu_entry *entry;
-	static int index, len;
 	const char *cmd;
+	int len;
 
-	if (!state) {
-		entry = default_menu;
-		index = 0;
-		len = strlen(text);
-	}
+	len = strlen(text);
 
-	while ((cmd = entry[index].cmd)) {
-		index++;
+	while ((cmd = entry[*index].cmd)) {
+		(*index)++;
 
 		if (!strncmp(cmd, text, len))
 			return strdup(cmd);
 	}
 
-	if (state)
-		return NULL;
+	return NULL;
+}
 
-	entry = data.menu->entries;
-	index = 0;
+static char *cmd_generator(const char *text, int state)
+{
+	static int index;
+	static bool default_menu_enabled;
+	char *cmd;
+
+	if (!state) {
+		index = 0;
+		default_menu_enabled = true;
+	}
+
+	if (default_menu_enabled) {
+		cmd = find_cmd(text, default_menu, &index);
+		if (cmd) {
+			return cmd;
+		} else {
+			index = 0;
+			default_menu_enabled = false;
+		}
+	}
 
-	return cmd_generator(text, 1);
+	return find_cmd(text, data.menu->entries, &index);
 }
 
 static char **menu_completion(const struct bt_shell_menu_entry *entry,