iPXE
menu.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017  * 02110-1301, USA.
00018  *
00019  * You can also choose to distribute this program under the terms of
00020  * the Unmodified Binary Distribution Licence (as given in the file
00021  * COPYING.UBDL), provided that you have satisfied its requirements.
00022  */
00023 
00024 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00025 
00026 /** @file
00027  *
00028  * Menu selection
00029  *
00030  */
00031 
00032 #include <stdlib.h>
00033 #include <string.h>
00034 #include <assert.h>
00035 #include <ipxe/list.h>
00036 #include <ipxe/menu.h>
00037 
00038 /** List of all menus */
00039 static LIST_HEAD ( menus );
00040 
00041 /**
00042  * Create menu
00043  *
00044  * @v name              Menu name, or NULL
00045  * @v title             Menu title, or NULL
00046  * @ret menu            Menu, or NULL on failure
00047  */
00048 struct menu * create_menu ( const char *name, const char *title ) {
00049         size_t name_len;
00050         size_t title_len;
00051         size_t len;
00052         struct menu *menu;
00053         char *name_copy;
00054         char *title_copy;
00055 
00056         /* Destroy any existing menu of this name */
00057         menu = find_menu ( name );
00058         if ( menu )
00059                 destroy_menu ( menu );
00060 
00061         /* Use empty title if none given */
00062         if ( ! title )
00063                 title = "";
00064 
00065         /* Allocate menu */
00066         name_len = ( name ? ( strlen ( name ) + 1 /* NUL */ ) : 0 );
00067         title_len = ( strlen ( title ) + 1 /* NUL */ );
00068         len = ( sizeof ( *menu ) + name_len + title_len );
00069         menu = zalloc ( len );
00070         if ( ! menu )
00071                 return NULL;
00072         name_copy = ( ( void * ) ( menu + 1 ) );
00073         title_copy = ( name_copy + name_len );
00074 
00075         /* Initialise menu */
00076         if ( name ) {
00077                 strcpy ( name_copy, name );
00078                 menu->name = name_copy;
00079         }
00080         strcpy ( title_copy, title );
00081         menu->title = title_copy;
00082         INIT_LIST_HEAD ( &menu->items );
00083 
00084         /* Add to list of menus */
00085         list_add_tail ( &menu->list, &menus );
00086 
00087         DBGC ( menu, "MENU %s created with title \"%s\"\n",
00088                menu->name, menu->title );
00089 
00090         return menu;
00091 }
00092 
00093 /**
00094  * Add menu item
00095  *
00096  * @v menu              Menu
00097  * @v label             Label, or NULL
00098  * @v text              Text, or NULL
00099  * @v shortcut          Shortcut key
00100  * @v is_default        Item is the default item
00101  * @ret item            Menu item, or NULL on failure
00102  */
00103 struct menu_item * add_menu_item ( struct menu *menu, const char *label,
00104                                    const char *text, int shortcut,
00105                                    int is_default ) {
00106         size_t label_len;
00107         size_t text_len;
00108         size_t len;
00109         struct menu_item *item;
00110         char *label_copy;
00111         char *text_copy;
00112 
00113         /* Use empty text if none given */
00114         if ( ! text )
00115                 text = "";
00116 
00117         /* Allocate item */
00118         label_len = ( label ? ( strlen ( label ) + 1 /* NUL */ ) : 0 );
00119         text_len = ( strlen ( text ) + 1 /* NUL */ );
00120         len = ( sizeof ( *item ) + label_len + text_len );
00121         item = zalloc ( len );
00122         if ( ! item )
00123                 return NULL;
00124         label_copy = ( ( void * ) ( item + 1 ) );
00125         text_copy = ( label_copy + label_len );
00126 
00127         /* Initialise item */
00128         if ( label ) {
00129                 strcpy ( label_copy, label );
00130                 item->label = label_copy;
00131         }
00132         strcpy ( text_copy, text );
00133         item->text = text_copy;
00134         item->shortcut = shortcut;
00135         item->is_default = is_default;
00136 
00137         /* Add to list of items */
00138         list_add_tail ( &item->list, &menu->items );
00139 
00140         return item;
00141 }
00142 
00143 /**
00144  * Destroy menu
00145  *
00146  * @v menu              Menu
00147  */
00148 void destroy_menu ( struct menu *menu ) {
00149         struct menu_item *item;
00150         struct menu_item *tmp;
00151 
00152         /* Remove from list of menus */
00153         list_del ( &menu->list );
00154 
00155         /* Free items */
00156         list_for_each_entry_safe ( item, tmp, &menu->items, list ) {
00157                 list_del ( &item->list );
00158                 free ( item );
00159         }
00160 
00161         /* Free menu */
00162         free ( menu );
00163 }
00164 
00165 /**
00166  * Find menu
00167  *
00168  * @v name              Menu name, or NULL
00169  * @ret menu            Menu, or NULL if not found
00170  */
00171 struct menu * find_menu ( const char *name ) {
00172         struct menu *menu;
00173 
00174         list_for_each_entry ( menu, &menus, list ) {
00175                 if ( ( menu->name == name ) ||
00176                      ( strcmp ( menu->name, name ) == 0 ) ) {
00177                         return menu;
00178                 }
00179         }
00180 
00181         return NULL;
00182 }