iPXE
Functions
menu.c File Reference

Menu selection. More...

#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <ipxe/list.h>
#include <ipxe/menu.h>

Go to the source code of this file.

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 
static LIST_HEAD (menus)
 List of all menus. More...
 
struct menucreate_menu (const char *name, const char *title)
 Create menu. More...
 
struct menu_itemadd_menu_item (struct menu *menu, const char *label, const char *text, int shortcut, int is_default)
 Add menu item. More...
 
void destroy_menu (struct menu *menu)
 Destroy menu. More...
 
struct menufind_menu (const char *name)
 Find menu. More...
 

Detailed Description

Menu selection.

Definition in file menu.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ LIST_HEAD()

static LIST_HEAD ( menus  )
static

List of all menus.

◆ create_menu()

struct menu* create_menu ( const char *  name,
const char *  title 
)

Create menu.

Parameters
nameMenu name, or NULL
titleMenu title, or NULL
Return values
menuMenu, or NULL on failure

Definition at line 48 of file menu.c.

48  {
49  size_t name_len;
50  size_t title_len;
51  size_t len;
52  struct menu *menu;
53  char *name_copy;
54  char *title_copy;
55 
56  /* Destroy any existing menu of this name */
57  menu = find_menu ( name );
58  if ( menu )
59  destroy_menu ( menu );
60 
61  /* Use empty title if none given */
62  if ( ! title )
63  title = "";
64 
65  /* Allocate menu */
66  name_len = ( name ? ( strlen ( name ) + 1 /* NUL */ ) : 0 );
67  title_len = ( strlen ( title ) + 1 /* NUL */ );
68  len = ( sizeof ( *menu ) + name_len + title_len );
69  menu = zalloc ( len );
70  if ( ! menu )
71  return NULL;
72  name_copy = ( ( void * ) ( menu + 1 ) );
73  title_copy = ( name_copy + name_len );
74 
75  /* Initialise menu */
76  if ( name ) {
77  strcpy ( name_copy, name );
78  menu->name = name_copy;
79  }
80  strcpy ( title_copy, title );
81  menu->title = title_copy;
83 
84  /* Add to list of menus */
85  list_add_tail ( &menu->list, &menus );
86 
87  DBGC ( menu, "MENU %s created with title \"%s\"\n",
88  menu->name, menu->title );
89 
90  return menu;
91 }
const char * name
Definition: ath9k_hw.c:1984
struct list_head list
List of menus.
Definition: menu.h:17
#define DBGC(...)
Definition: compiler.h:505
struct list_head items
Menu items.
Definition: menu.h:23
#define list_add_tail(new, head)
Add a new entry to the tail of a list.
Definition: list.h:93
char * strcpy(char *dest, const char *src)
Copy string.
Definition: string.c:296
const char * name
Name.
Definition: menu.h:19
void * zalloc(size_t size)
Allocate cleared memory.
Definition: malloc.c:624
const char * title
Title.
Definition: menu.h:21
size_t strlen(const char *src)
Get length of string.
Definition: string.c:213
#define INIT_LIST_HEAD(list)
Initialise a list head.
Definition: list.h:45
uint32_t len
Length.
Definition: ena.h:14
A menu.
Definition: menu.h:15
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362

References DBGC, destroy_menu(), find_menu(), INIT_LIST_HEAD, menu::items, len, menu::list, list_add_tail, menu::name, name, NULL, strcpy(), strlen(), menu::title, and zalloc().

Referenced by menu_exec().

◆ add_menu_item()

struct menu_item* add_menu_item ( struct menu menu,
const char *  label,
const char *  text,
int  shortcut,
int  is_default 
)

Add menu item.

Parameters
menuMenu
labelLabel, or NULL
textText, or NULL
shortcutShortcut key
is_defaultItem is the default item
Return values
itemMenu item, or NULL on failure

Definition at line 103 of file menu.c.

105  {
106  size_t label_len;
107  size_t text_len;
108  size_t len;
109  struct menu_item *item;
110  char *label_copy;
111  char *text_copy;
112 
113  /* Use empty text if none given */
114  if ( ! text )
115  text = "";
116 
117  /* Allocate item */
118  label_len = ( label ? ( strlen ( label ) + 1 /* NUL */ ) : 0 );
119  text_len = ( strlen ( text ) + 1 /* NUL */ );
120  len = ( sizeof ( *item ) + label_len + text_len );
121  item = zalloc ( len );
122  if ( ! item )
123  return NULL;
124  label_copy = ( ( void * ) ( item + 1 ) );
125  text_copy = ( label_copy + label_len );
126 
127  /* Initialise item */
128  if ( label ) {
129  strcpy ( label_copy, label );
130  item->label = label_copy;
131  }
132  strcpy ( text_copy, text );
133  item->text = text_copy;
134  item->shortcut = shortcut;
135  item->is_default = is_default;
136 
137  /* Add to list of items */
138  list_add_tail ( &item->list, &menu->items );
139 
140  return item;
141 }
A menu item.
Definition: menu.h:27
const char * label
Label.
Definition: menu.h:31
struct list_head items
Menu items.
Definition: menu.h:23
struct list_head list
List of menu items.
Definition: menu.h:29
#define list_add_tail(new, head)
Add a new entry to the tail of a list.
Definition: list.h:93
char * strcpy(char *dest, const char *src)
Copy string.
Definition: string.c:296
void * zalloc(size_t size)
Allocate cleared memory.
Definition: malloc.c:624
size_t strlen(const char *src)
Get length of string.
Definition: string.c:213
const char * text
Text.
Definition: menu.h:33
uint32_t len
Length.
Definition: ena.h:14
int is_default
Is default item.
Definition: menu.h:37
A menu.
Definition: menu.h:15
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362
int shortcut
Shortcut key.
Definition: menu.h:35

References menu_item::is_default, menu::items, menu_item::label, len, menu_item::list, list_add_tail, NULL, menu_item::shortcut, strcpy(), strlen(), menu_item::text, and zalloc().

Referenced by item_exec().

◆ destroy_menu()

void destroy_menu ( struct menu menu)

Destroy menu.

Parameters
menuMenu

Definition at line 148 of file menu.c.

148  {
149  struct menu_item *item;
150  struct menu_item *tmp;
151 
152  /* Remove from list of menus */
153  list_del ( &menu->list );
154 
155  /* Free items */
156  list_for_each_entry_safe ( item, tmp, &menu->items, list ) {
157  list_del ( &item->list );
158  free ( item );
159  }
160 
161  /* Free menu */
162  free ( menu );
163 }
A menu item.
Definition: menu.h:27
struct list_head list
List of menus.
Definition: menu.h:17
struct list_head items
Menu items.
Definition: menu.h:23
#define list_del(list)
Delete an entry from a list.
Definition: list.h:119
struct list_head list
List of menu items.
Definition: menu.h:29
#define list_for_each_entry_safe(pos, tmp, head, member)
Iterate over entries in a list, safe against deletion of the current entry.
Definition: list.h:447
static void(* free)(struct refcnt *refcnt))
Definition: refcnt.h:54
uint8_t * tmp
Definition: entropy.h:156
A menu.
Definition: menu.h:15

References free, menu::items, menu_item::list, menu::list, list_del, list_for_each_entry_safe, and tmp.

Referenced by choose_exec(), create_menu(), and menu_exec().

◆ find_menu()

struct menu* find_menu ( const char *  name)

Find menu.

Parameters
nameMenu name, or NULL
Return values
menuMenu, or NULL if not found

Definition at line 171 of file menu.c.

171  {
172  struct menu *menu;
173 
174  list_for_each_entry ( menu, &menus, list ) {
175  if ( ( menu->name == name ) ||
176  ( strcmp ( menu->name, name ) == 0 ) ) {
177  return menu;
178  }
179  }
180 
181  return NULL;
182 }
const char * name
Definition: ath9k_hw.c:1984
struct list_head list
List of menus.
Definition: menu.h:17
#define list_for_each_entry(pos, head, member)
Iterate over entries in a list.
Definition: list.h:420
const char * name
Name.
Definition: menu.h:19
int strcmp(const char *first, const char *second)
Compare strings.
Definition: string.c:157
A menu.
Definition: menu.h:15
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362

References menu::list, list_for_each_entry, menu::name, name, NULL, and strcmp().

Referenced by create_menu(), and parse_menu().