Skip to content

Commit 7284b4f

Browse files
committed
kconfig: add menu_next() function and menu_for_each(_sub)_entry macros
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>
1 parent 377d909 commit 7284b4f

2 files changed

Lines changed: 26 additions & 0 deletions

File tree

scripts/kconfig/lkc.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ void str_printf(struct gstr *gs, const char *fmt, ...);
7979
char *str_get(struct gstr *gs);
8080

8181
/* menu.c */
82+
struct menu *menu_next(struct menu *menu, struct menu *root);
83+
#define menu_for_each_sub_entry(menu, root) \
84+
for (menu = menu_next(root, root); menu; menu = menu_next(menu, root))
85+
#define menu_for_each_entry(menu) \
86+
menu_for_each_sub_entry(menu, &rootmenu)
8287
void _menu_init(void);
8388
void menu_warn(struct menu *menu, const char *fmt, ...);
8489
struct menu *menu_add_menu(void);

scripts/kconfig/menu.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,27 @@ static const char nohelp_text[] = "There is no help available for this option.";
1717
struct menu rootmenu;
1818
static struct menu **last_entry_ptr;
1919

20+
/**
21+
* menu_next - return the next menu entry with depth-first traversal
22+
* @menu: pointer to the current menu
23+
* @root: root of the sub-tree to traverse. If NULL is given, the traveral
24+
* continues until it reaches the end of the entire menu tree.
25+
* return: the menu to visit next, or NULL when it reaches the end.
26+
*/
27+
struct menu *menu_next(struct menu *menu, struct menu *root)
28+
{
29+
if (menu->list)
30+
return menu->list;
31+
32+
while (menu != root && !menu->next)
33+
menu = menu->parent;
34+
35+
if (menu == root)
36+
return NULL;
37+
38+
return menu->next;
39+
}
40+
2041
void menu_warn(struct menu *menu, const char *fmt, ...)
2142
{
2243
va_list ap;

0 commit comments

Comments
 (0)