iPXE
Data Structures | Functions
menu.h File Reference

Menu selection. More...

#include <ipxe/list.h>

Go to the source code of this file.

Data Structures

struct  menu
 A menu. More...
 
struct  menu_item
 A menu item. More...
 

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 
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...
 
int show_menu (struct menu *menu, unsigned long timeout, const char *select, struct menu_item **selected)
 Show menu. More...
 

Detailed Description

Menu selection.

Definition in file menu.h.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ 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:311
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:228
#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:311
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:228
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:172
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().

◆ show_menu()

int show_menu ( struct menu menu,
unsigned long  timeout,
const char *  select,
struct menu_item **  selected 
)

Show menu.

Parameters
menuMenu
timeoutTimeout period, in ticks (0=indefinite)
Return values
selectedSelected item
rcReturn status code

Definition at line 273 of file menu_ui.c.

274  {
275  struct menu_item *item;
276  struct menu_ui ui;
277  char buf[ MENU_COLS + 1 /* NUL */ ];
278  int labelled_count = 0;
279  int rc;
280 
281  /* Initialise UI */
282  memset ( &ui, 0, sizeof ( ui ) );
283  ui.menu = menu;
284  ui.scroll.rows = MENU_ROWS;
285  ui.timeout = timeout;
286  list_for_each_entry ( item, &menu->items, list ) {
287  if ( item->label ) {
288  if ( ! labelled_count )
289  ui.scroll.current = ui.scroll.count;
290  labelled_count++;
291  if ( select ) {
292  if ( strcmp ( select, item->label ) == 0 )
293  ui.scroll.current = ui.scroll.count;
294  } else {
295  if ( item->is_default )
296  ui.scroll.current = ui.scroll.count;
297  }
298  }
299  ui.scroll.count++;
300  }
301  if ( ! labelled_count ) {
302  /* Menus with no labelled items cannot be selected
303  * from, and will seriously confuse the navigation
304  * logic. Refuse to display any such menus.
305  */
306  return -ENOENT;
307  }
308 
309  /* Initialise screen */
310  initscr();
311  start_color();
313  curs_set ( 0 );
314  erase();
315 
316  /* Draw initial content */
317  attron ( A_BOLD );
318  snprintf ( buf, sizeof ( buf ), "%s", ui.menu->title );
319  mvprintw ( TITLE_ROW, ( ( COLS - strlen ( buf ) ) / 2 ), "%s", buf );
320  attroff ( A_BOLD );
321  jump_scroll ( &ui.scroll );
322  draw_menu_items ( &ui );
323  draw_menu_item ( &ui, ui.scroll.current );
324 
325  /* Enter main loop */
326  rc = menu_loop ( &ui, selected );
327  assert ( *selected );
328 
329  /* Clear screen */
330  endwin();
331 
332  return rc;
333 }
A menu item.
Definition: menu.h:27
struct menu * menu
Menu.
Definition: menu_ui.c:53
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
int erase(void)
Completely clear the screen.
Definition: clear.c:97
A menu user interface.
Definition: menu_ui.c:51
const char * label
Label.
Definition: menu.h:31
static int attroff(int attrs)
Definition: curses.h:508
#define start_color()
Definition: curses.h:396
int endwin(void)
Finalise console environment.
Definition: wininit.c:31
#define ENOENT
No such file or directory.
Definition: errno.h:514
#define mvprintw(y, x, fmt,...)
Definition: curses.h:648
struct list_head items
Menu items.
Definition: menu.h:23
#define CPAIR_NORMAL
Normal text.
Definition: ansicol.h:40
WINDOW * initscr(void)
Initialise console environment.
Definition: wininit.c:17
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
#define list_for_each_entry(pos, head, member)
Iterate over entries in a list.
Definition: list.h:420
int jump_scroll(struct jump_scroller *scroll)
Jump scroll to new page (if applicable)
Definition: jumpscroll.c:119
size_t strlen(const char *src)
Get length of string.
Definition: string.c:228
int select(fd_set *readfds, int wait)
Check file descriptors for readiness.
Definition: posix_io.c:229
#define COLS
Definition: curses.h:111
int curs_set(int visibility)
Set cursor visibility.
Definition: mucurses.c:153
int strcmp(const char *first, const char *second)
Compare strings.
Definition: string.c:172
int is_default
Is default item.
Definition: menu.h:37
int snprintf(char *buf, size_t size, const char *fmt,...)
Write a formatted string to a buffer.
Definition: vsprintf.c:382
#define color_set(cpno, opts)
Definition: curses.h:240
void timeout(int)
#define A_BOLD
Definition: curses.h:138
A menu.
Definition: menu.h:15
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362
static int attron(int attrs)
Definition: curses.h:512
void * memset(void *dest, int character, size_t len) __nonnull

References A_BOLD, assert(), attroff(), attron(), color_set, COLS, jump_scroller::count, CPAIR_NORMAL, jump_scroller::current, curs_set(), draw_menu_item(), draw_menu_items(), endwin(), ENOENT, erase(), initscr(), menu_item::is_default, menu::items, jump_scroll(), menu_item::label, list_for_each_entry, memset(), menu_ui::menu, MENU_COLS, menu_loop(), MENU_ROWS, mvprintw, NULL, rc, jump_scroller::rows, menu_ui::scroll, select(), snprintf(), start_color, strcmp(), strlen(), menu_ui::timeout, timeout(), menu::title, and TITLE_ROW.

Referenced by choose_exec().