Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / src / core / menu.c
1 /*
2  * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
3  *
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.
8  *
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.
13  *
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
17  * 02110-1301, USA.
18  */
19
20 FILE_LICENCE ( GPL2_OR_LATER );
21
22 /** @file
23  *
24  * Menu selection
25  *
26  */
27
28 #include <stdlib.h>
29 #include <string.h>
30 #include <assert.h>
31 #include <ipxe/list.h>
32 #include <ipxe/menu.h>
33
34 /** List of all menus */
35 static LIST_HEAD ( menus );
36
37 /**
38  * Create menu
39  *
40  * @v name              Menu name, or NULL
41  * @v title             Menu title, or NULL
42  * @ret menu            Menu, or NULL on failure
43  */
44 struct menu * create_menu ( const char *name, const char *title ) {
45         size_t name_len;
46         size_t title_len;
47         size_t len;
48         struct menu *menu;
49         char *name_copy;
50         char *title_copy;
51
52         /* Destroy any existing menu of this name */
53         menu = find_menu ( name );
54         if ( menu )
55                 destroy_menu ( menu );
56
57         /* Use empty title if none given */
58         if ( ! title )
59                 title = "";
60
61         /* Allocate menu */
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 );
66         if ( ! menu )
67                 return NULL;
68         name_copy = ( ( void * ) ( menu + 1 ) );
69         title_copy = ( name_copy + name_len );
70
71         /* Initialise menu */
72         if ( name ) {
73                 strcpy ( name_copy, name );
74                 menu->name = name_copy;
75         }
76         strcpy ( title_copy, title );
77         menu->title = title_copy;
78         INIT_LIST_HEAD ( &menu->items );
79
80         /* Add to list of menus */
81         list_add_tail ( &menu->list, &menus );
82
83         DBGC ( menu, "MENU %s created with title \"%s\"\n",
84                menu->name, menu->title );
85
86         return menu;
87 }
88
89 /**
90  * Add menu item
91  *
92  * @v menu              Menu
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
98  */
99 struct menu_item * add_menu_item ( struct menu *menu, const char *label,
100                                    const char *text, int shortcut,
101                                    int is_default ) {
102         size_t label_len;
103         size_t text_len;
104         size_t len;
105         struct menu_item *item;
106         char *label_copy;
107         char *text_copy;
108
109         /* Use empty text if none given */
110         if ( ! text )
111                 text = "";
112
113         /* Allocate item */
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 );
118         if ( ! item )
119                 return NULL;
120         label_copy = ( ( void * ) ( item + 1 ) );
121         text_copy = ( label_copy + label_len );
122
123         /* Initialise item */
124         if ( label ) {
125                 strcpy ( label_copy, label );
126                 item->label = label_copy;
127         }
128         strcpy ( text_copy, text );
129         item->text = text_copy;
130         item->shortcut = shortcut;
131         item->is_default = is_default;
132
133         /* Add to list of items */
134         list_add_tail ( &item->list, &menu->items );
135
136         return item;
137 }
138
139 /**
140  * Destroy menu
141  *
142  * @v menu              Menu
143  */
144 void destroy_menu ( struct menu *menu ) {
145         struct menu_item *item;
146         struct menu_item *tmp;
147
148         /* Remove from list of menus */
149         list_del ( &menu->list );
150
151         /* Free items */
152         list_for_each_entry_safe ( item, tmp, &menu->items, list ) {
153                 list_del ( &item->list );
154                 free ( item );
155         }
156
157         /* Free menu */
158         free ( menu );
159 }
160
161 /**
162  * Find menu
163  *
164  * @v name              Menu name, or NULL
165  * @ret menu            Menu, or NULL if not found
166  */
167 struct menu * find_menu ( const char *name ) {
168         struct menu *menu;
169
170         list_for_each_entry ( menu, &menus, list ) {
171                 if ( ( menu->name == name ) ||
172                      ( strcmp ( menu->name, name ) == 0 ) ) {
173                         return menu;
174                 }
175         }
176
177         return NULL;
178 }