2 * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 FILE_LICENCE ( GPL2_OR_LATER );
31 #include <ipxe/list.h>
32 #include <ipxe/menu.h>
34 /** List of all menus */
35 static LIST_HEAD ( menus );
40 * @v name Menu name, or NULL
41 * @v title Menu title, or NULL
42 * @ret menu Menu, or NULL on failure
44 struct menu * create_menu ( const char *name, const char *title ) {
52 /* Destroy any existing menu of this name */
53 menu = find_menu ( name );
55 destroy_menu ( menu );
57 /* Use empty title if none given */
62 name_len = ( name ? ( strlen ( name ) + 1 /* NUL */ ) : 0 );
63 title_len = ( strlen ( title ) + 1 /* NUL */ );
64 len = ( sizeof ( *menu ) + name_len + title_len );
65 menu = zalloc ( len );
68 name_copy = ( ( void * ) ( menu + 1 ) );
69 title_copy = ( name_copy + name_len );
73 strcpy ( name_copy, name );
74 menu->name = name_copy;
76 strcpy ( title_copy, title );
77 menu->title = title_copy;
78 INIT_LIST_HEAD ( &menu->items );
80 /* Add to list of menus */
81 list_add_tail ( &menu->list, &menus );
83 DBGC ( menu, "MENU %s created with title \"%s\"\n",
84 menu->name, menu->title );
93 * @v label Label, or NULL
94 * @v text Text, or NULL
95 * @v shortcut Shortcut key
96 * @v is_default Item is the default item
97 * @ret item Menu item, or NULL on failure
99 struct menu_item * add_menu_item ( struct menu *menu, const char *label,
100 const char *text, int shortcut,
105 struct menu_item *item;
109 /* Use empty text if none given */
114 label_len = ( label ? ( strlen ( label ) + 1 /* NUL */ ) : 0 );
115 text_len = ( strlen ( text ) + 1 /* NUL */ );
116 len = ( sizeof ( *item ) + label_len + text_len );
117 item = zalloc ( len );
120 label_copy = ( ( void * ) ( item + 1 ) );
121 text_copy = ( label_copy + label_len );
123 /* Initialise item */
125 strcpy ( label_copy, label );
126 item->label = label_copy;
128 strcpy ( text_copy, text );
129 item->text = text_copy;
130 item->shortcut = shortcut;
131 item->is_default = is_default;
133 /* Add to list of items */
134 list_add_tail ( &item->list, &menu->items );
144 void destroy_menu ( struct menu *menu ) {
145 struct menu_item *item;
146 struct menu_item *tmp;
148 /* Remove from list of menus */
149 list_del ( &menu->list );
152 list_for_each_entry_safe ( item, tmp, &menu->items, list ) {
153 list_del ( &item->list );
164 * @v name Menu name, or NULL
165 * @ret menu Menu, or NULL if not found
167 struct menu * find_menu ( const char *name ) {
170 list_for_each_entry ( menu, &menus, list ) {
171 if ( ( menu->name == name ) ||
172 ( strcmp ( menu->name, name ) == 0 ) ) {