diff mbox series

[2/3] kconfig: add menu_next() function and menu_for_each(_sub)_entry macros

Message ID 20240310141619.606415-2-masahiroy@kernel.org (mailing list archive)
State New, archived
Headers show
Series [1/3] kconfig: remove unneeded menu_is_visible() call in conf_write_defconfig() | expand

Commit Message

Masahiro Yamada March 10, 2024, 2:16 p.m. UTC
Several functions require traversing menu entries sequentially. This
commit introduces some helpers to simplify such operations.

The menu_next() function facilitates depth-first traversal:

 1. Descend to the child level if the current menu has one
 2. Move to the next sibling at the same level if available
 3. Ascend to the parent level if there is no more child or sibling

The menu_for_each_sub_entry() macro iterates over all submenu entries
using depth-first traverse.

The menu_for_each_entry() macro is the same, but over all menu entries.

Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
---

 scripts/kconfig/lkc.h  |  5 +++++
 scripts/kconfig/menu.c | 15 +++++++++++++++
 2 files changed, 20 insertions(+)
diff mbox series

Patch

diff --git a/scripts/kconfig/lkc.h b/scripts/kconfig/lkc.h
index e69d7c59d930..5241dccd559e 100644
--- a/scripts/kconfig/lkc.h
+++ b/scripts/kconfig/lkc.h
@@ -79,6 +79,11 @@  void str_printf(struct gstr *gs, const char *fmt, ...);
 char *str_get(struct gstr *gs);
 
 /* menu.c */
+struct menu *menu_next(struct menu *menu);
+#define menu_for_each_sub_entry(menu, root) \
+	for (struct menu *menu = (root)->list; menu != (root)->next && menu != (root)->parent; menu = menu_next(menu))
+#define menu_for_each_entry(menu) \
+	menu_for_each_sub_entry(menu, &rootmenu)
 void _menu_init(void);
 void menu_warn(struct menu *menu, const char *fmt, ...);
 struct menu *menu_add_menu(void);
diff --git a/scripts/kconfig/menu.c b/scripts/kconfig/menu.c
index 8498481e6afe..417dc01ac412 100644
--- a/scripts/kconfig/menu.c
+++ b/scripts/kconfig/menu.c
@@ -17,6 +17,21 @@  static const char nohelp_text[] = "There is no help available for this option.";
 struct menu rootmenu;
 static struct menu **last_entry_ptr;
 
+/**
+ * menu_next - return the next menu entry with depth-first traversal
+ * @menu: the pointer to the current menu
+ */
+struct menu *menu_next(struct menu *menu)
+{
+	if (menu->list)
+		return menu->list;
+
+	while (!menu->next && menu->parent)
+		menu = menu->parent;
+
+	return menu->next;
+}
+
 void menu_warn(struct menu *menu, const char *fmt, ...)
 {
 	va_list ap;