您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

menu.c 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. FILE_LICENCE ( GPL2_OR_LATER );
  19. /** @file
  20. *
  21. * Menu selection
  22. *
  23. */
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <assert.h>
  27. #include <ipxe/list.h>
  28. #include <ipxe/menu.h>
  29. /** List of all menus */
  30. static LIST_HEAD ( menus );
  31. /**
  32. * Create menu
  33. *
  34. * @v name Menu name, or NULL
  35. * @v title Menu title, or NULL
  36. * @ret menu Menu, or NULL on failure
  37. */
  38. struct menu * create_menu ( const char *name, const char *title ) {
  39. size_t name_len;
  40. size_t title_len;
  41. size_t len;
  42. struct menu *menu;
  43. char *name_copy;
  44. char *title_copy;
  45. /* Destroy any existing menu of this name */
  46. menu = find_menu ( name );
  47. if ( menu )
  48. destroy_menu ( menu );
  49. /* Use empty title if none given */
  50. if ( ! title )
  51. title = "";
  52. /* Allocate menu */
  53. name_len = ( name ? ( strlen ( name ) + 1 /* NUL */ ) : 0 );
  54. title_len = ( strlen ( title ) + 1 /* NUL */ );
  55. len = ( sizeof ( *menu ) + name_len + title_len );
  56. menu = zalloc ( len );
  57. if ( ! menu )
  58. return NULL;
  59. name_copy = ( ( void * ) ( menu + 1 ) );
  60. title_copy = ( name_copy + name_len );
  61. /* Initialise menu */
  62. if ( name ) {
  63. strcpy ( name_copy, name );
  64. menu->name = name_copy;
  65. }
  66. strcpy ( title_copy, title );
  67. menu->title = title_copy;
  68. INIT_LIST_HEAD ( &menu->items );
  69. /* Add to list of menus */
  70. list_add_tail ( &menu->list, &menus );
  71. DBGC ( menu, "MENU %s created with title \"%s\"\n",
  72. menu->name, menu->title );
  73. return menu;
  74. }
  75. /**
  76. * Add menu item
  77. *
  78. * @v menu Menu
  79. * @v label Label, or NULL
  80. * @v text Text, or NULL
  81. * @v shortcut Shortcut key
  82. * @v is_default Item is the default item
  83. * @ret item Menu item, or NULL on failure
  84. */
  85. struct menu_item * add_menu_item ( struct menu *menu, const char *label,
  86. const char *text, int shortcut,
  87. int is_default ) {
  88. size_t label_len;
  89. size_t text_len;
  90. size_t len;
  91. struct menu_item *item;
  92. char *label_copy;
  93. char *text_copy;
  94. /* Use empty text if none given */
  95. if ( ! text )
  96. text = "";
  97. /* Allocate item */
  98. label_len = ( label ? ( strlen ( label ) + 1 /* NUL */ ) : 0 );
  99. text_len = ( strlen ( text ) + 1 /* NUL */ );
  100. len = ( sizeof ( *item ) + label_len + text_len );
  101. item = zalloc ( len );
  102. if ( ! item )
  103. return NULL;
  104. label_copy = ( ( void * ) ( item + 1 ) );
  105. text_copy = ( label_copy + label_len );
  106. /* Initialise item */
  107. if ( label ) {
  108. strcpy ( label_copy, label );
  109. item->label = label_copy;
  110. }
  111. strcpy ( text_copy, text );
  112. item->text = text_copy;
  113. item->shortcut = shortcut;
  114. item->is_default = is_default;
  115. /* Add to list of items */
  116. list_add_tail ( &item->list, &menu->items );
  117. return item;
  118. }
  119. /**
  120. * Destroy menu
  121. *
  122. * @v menu Menu
  123. */
  124. void destroy_menu ( struct menu *menu ) {
  125. struct menu_item *item;
  126. struct menu_item *tmp;
  127. /* Remove from list of menus */
  128. list_del ( &menu->list );
  129. /* Free items */
  130. list_for_each_entry_safe ( item, tmp, &menu->items, list ) {
  131. list_del ( &item->list );
  132. free ( item );
  133. }
  134. /* Free menu */
  135. free ( menu );
  136. }
  137. /**
  138. * Find menu
  139. *
  140. * @v name Menu name, or NULL
  141. * @ret menu Menu, or NULL if not found
  142. */
  143. struct menu * find_menu ( const char *name ) {
  144. struct menu *menu;
  145. list_for_each_entry ( menu, &menus, list ) {
  146. if ( ( menu->name == name ) ||
  147. ( strcmp ( menu->name, name ) == 0 ) ) {
  148. return menu;
  149. }
  150. }
  151. return NULL;
  152. }