iPXE
Functions
dynui.c File Reference

Dynamic user interfaces. More...

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

Go to the source code of this file.

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 
 FILE_SECBOOT (PERMITTED)
 
static LIST_HEAD (dynamic_uis)
 List of all dynamic user interfaces. More...
 
struct dynamic_uicreate_dynui (const char *name, const char *title)
 Create dynamic user interface. More...
 
struct dynamic_itemadd_dynui_item (struct dynamic_ui *dynui, const char *name, const char *text, unsigned int flags, int shortcut)
 Add dynamic user interface item. More...
 
void destroy_dynui (struct dynamic_ui *dynui)
 Destroy dynamic user interface. More...
 
struct dynamic_uifind_dynui (const char *name)
 Find dynamic user interface. More...
 
struct dynamic_itemdynui_item (struct dynamic_ui *dynui, unsigned int index)
 Find dynamic user interface item by index. More...
 
struct dynamic_itemdynui_shortcut (struct dynamic_ui *dynui, int key)
 Find dynamic user interface item by shortcut key. More...
 

Detailed Description

Dynamic user interfaces.

Definition in file dynui.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ FILE_SECBOOT()

FILE_SECBOOT ( PERMITTED  )

◆ LIST_HEAD()

static LIST_HEAD ( dynamic_uis  )
static

List of all dynamic user interfaces.

◆ create_dynui()

struct dynamic_ui* create_dynui ( const char *  name,
const char *  title 
)

Create dynamic user interface.

Parameters
nameUser interface name, or NULL
titleUser interface title, or NULL
Return values
dynuiDynamic user interface, or NULL on failure

Definition at line 49 of file dynui.c.

49  {
50  struct dynamic_ui *dynui;
51  size_t name_len;
52  size_t title_len;
53  size_t len;
54  char *name_copy;
55  char *title_copy;
56 
57  /* Destroy any existing user interface of this name */
58  dynui = find_dynui ( name );
59  if ( dynui )
60  destroy_dynui ( dynui );
61 
62  /* Use empty title if none given */
63  if ( ! title )
64  title = "";
65 
66  /* Allocate user interface */
67  name_len = ( name ? ( strlen ( name ) + 1 /* NUL */ ) : 0 );
68  title_len = ( strlen ( title ) + 1 /* NUL */ );
69  len = ( sizeof ( *dynui ) + name_len + title_len );
70  dynui = zalloc ( len );
71  if ( ! dynui )
72  return NULL;
73  name_copy = ( ( void * ) ( dynui + 1 ) );
74  title_copy = ( name_copy + name_len );
75 
76  /* Initialise user interface */
77  if ( name ) {
78  strcpy ( name_copy, name );
79  dynui->name = name_copy;
80  }
81  strcpy ( title_copy, title );
82  dynui->title = title_copy;
83  INIT_LIST_HEAD ( &dynui->items );
84 
85  /* Add to list of user interfaces */
86  list_add_tail ( &dynui->list, &dynamic_uis );
87 
88  DBGC ( dynui, "DYNUI %s created with title \"%s\"\n",
89  dynui->name, dynui->title );
90 
91  return dynui;
92 }
const char * title
Title.
Definition: dynui.h:22
const char * name
Definition: ath9k_hw.c:1986
#define DBGC(...)
Definition: compiler.h:505
struct dynamic_ui * find_dynui(const char *name)
Find dynamic user interface.
Definition: dynui.c:173
struct list_head items
Dynamic user interface items.
Definition: dynui.h:24
#define list_add_tail(new, head)
Add a new entry to the tail of a list.
Definition: list.h:94
ring len
Length.
Definition: dwmac.h:231
char * strcpy(char *dest, const char *src)
Copy string.
Definition: string.c:347
void * zalloc(size_t size)
Allocate cleared memory.
Definition: malloc.c:662
size_t strlen(const char *src)
Get length of string.
Definition: string.c:244
A dynamic user interface.
Definition: dynui.h:16
#define INIT_LIST_HEAD(list)
Initialise a list head.
Definition: list.h:46
struct list_head list
List of dynamic user interfaces.
Definition: dynui.h:18
const char * name
Name.
Definition: dynui.h:20
void destroy_dynui(struct dynamic_ui *dynui)
Destroy dynamic user interface.
Definition: dynui.c:150
#define NULL
NULL pointer (VOID *)
Definition: Base.h:322

References DBGC, destroy_dynui(), find_dynui(), INIT_LIST_HEAD, dynamic_ui::items, len, dynamic_ui::list, list_add_tail, dynamic_ui::name, name, NULL, strcpy(), strlen(), dynamic_ui::title, and zalloc().

Referenced by dynui_exec().

◆ add_dynui_item()

struct dynamic_item* add_dynui_item ( struct dynamic_ui dynui,
const char *  name,
const char *  text,
unsigned int  flags,
int  shortcut 
)

Add dynamic user interface item.

Parameters
dynuiDynamic user interface
nameName, or NULL
textText, or NULL
flagsFlags
shortcutShortcut key
Return values
itemUser interface item, or NULL on failure

Definition at line 104 of file dynui.c.

106  {
107  struct dynamic_item *item;
108  size_t name_len;
109  size_t text_len;
110  size_t len;
111  char *name_copy;
112  char *text_copy;
113 
114  /* Use empty text if none given */
115  if ( ! text )
116  text = "";
117 
118  /* Allocate item */
119  name_len = ( name ? ( strlen ( name ) + 1 /* NUL */ ) : 0 );
120  text_len = ( strlen ( text ) + 1 /* NUL */ );
121  len = ( sizeof ( *item ) + name_len + text_len );
122  item = zalloc ( len );
123  if ( ! item )
124  return NULL;
125  name_copy = ( ( void * ) ( item + 1 ) );
126  text_copy = ( name_copy + name_len );
127 
128  /* Initialise item */
129  if ( name ) {
130  strcpy ( name_copy, name );
131  item->name = name_copy;
132  }
133  strcpy ( text_copy, text );
134  item->text = text_copy;
135  item->index = dynui->count++;
136  item->flags = flags;
137  item->shortcut = shortcut;
138 
139  /* Add to list of items */
140  list_add_tail ( &item->list, &dynui->items );
141 
142  return item;
143 }
const char * name
Definition: ath9k_hw.c:1986
A dynamic user interface item.
Definition: dynui.h:30
unsigned int count
Number of user interface items.
Definition: dynui.h:26
struct list_head items
Dynamic user interface items.
Definition: dynui.h:24
const char * name
Name.
Definition: dynui.h:34
#define list_add_tail(new, head)
Add a new entry to the tail of a list.
Definition: list.h:94
ring len
Length.
Definition: dwmac.h:231
char * strcpy(char *dest, const char *src)
Copy string.
Definition: string.c:347
uint8_t flags
Flags.
Definition: ena.h:18
void * zalloc(size_t size)
Allocate cleared memory.
Definition: malloc.c:662
size_t strlen(const char *src)
Get length of string.
Definition: string.c:244
int shortcut
Shortcut key.
Definition: dynui.h:42
const char * text
Text.
Definition: dynui.h:36
unsigned int flags
Flags.
Definition: dynui.h:40
unsigned int index
Index.
Definition: dynui.h:38
struct list_head list
List of dynamic user interface items.
Definition: dynui.h:32
#define NULL
NULL pointer (VOID *)
Definition: Base.h:322

References dynamic_ui::count, flags, dynamic_item::flags, dynamic_item::index, dynamic_ui::items, len, dynamic_item::list, list_add_tail, dynamic_item::name, name, NULL, dynamic_item::shortcut, strcpy(), strlen(), dynamic_item::text, and zalloc().

Referenced by item_exec().

◆ destroy_dynui()

void destroy_dynui ( struct dynamic_ui dynui)

Destroy dynamic user interface.

Parameters
dynuiDynamic user interface

Definition at line 150 of file dynui.c.

150  {
151  struct dynamic_item *item;
152  struct dynamic_item *tmp;
153 
154  /* Remove from list of user interfaces */
155  list_del ( &dynui->list );
156 
157  /* Free items */
158  list_for_each_entry_safe ( item, tmp, &dynui->items, list ) {
159  list_del ( &item->list );
160  free ( item );
161  }
162 
163  /* Free user interface */
164  free ( dynui );
165 }
A dynamic user interface item.
Definition: dynui.h:30
unsigned long tmp
Definition: linux_pci.h:65
#define list_del(list)
Delete an entry from a list.
Definition: list.h:120
struct list_head items
Dynamic user interface items.
Definition: dynui.h:24
#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:459
static void(* free)(struct refcnt *refcnt))
Definition: refcnt.h:55
struct list_head list
List of dynamic user interfaces.
Definition: dynui.h:18
struct list_head list
List of dynamic user interface items.
Definition: dynui.h:32

References free, dynamic_ui::items, dynamic_item::list, dynamic_ui::list, list_del, list_for_each_entry_safe, and tmp.

Referenced by choose_exec(), create_dynui(), dynui_exec(), and present_exec().

◆ find_dynui()

struct dynamic_ui* find_dynui ( const char *  name)

Find dynamic user interface.

Parameters
nameUser interface name, or NULL
Return values
dynuiDynamic user interface, or NULL if not found

Definition at line 173 of file dynui.c.

173  {
174  struct dynamic_ui *dynui;
175 
176  list_for_each_entry ( dynui, &dynamic_uis, list ) {
177  if ( ( dynui->name == name ) ||
178  ( strcmp ( dynui->name, name ) == 0 ) ) {
179  return dynui;
180  }
181  }
182 
183  return NULL;
184 }
const char * name
Definition: ath9k_hw.c:1986
#define list_for_each_entry(pos, head, member)
Iterate over entries in a list.
Definition: list.h:432
A dynamic user interface.
Definition: dynui.h:16
int strcmp(const char *first, const char *second)
Compare strings.
Definition: string.c:174
struct list_head list
List of dynamic user interfaces.
Definition: dynui.h:18
const char * name
Name.
Definition: dynui.h:20
#define NULL
NULL pointer (VOID *)
Definition: Base.h:322

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

Referenced by create_dynui(), and parse_dynui().

◆ dynui_item()

struct dynamic_item* dynui_item ( struct dynamic_ui dynui,
unsigned int  index 
)

Find dynamic user interface item by index.

Parameters
dynuiDynamic user interface
indexIndex
Return values
itemUser interface item, or NULL if not found

Definition at line 193 of file dynui.c.

194  {
195  struct dynamic_item *item;
196 
197  list_for_each_entry ( item, &dynui->items, list ) {
198  if ( index-- == 0 )
199  return item;
200  }
201 
202  return NULL;
203 }
A dynamic user interface item.
Definition: dynui.h:30
long index
Definition: bigint.h:65
struct list_head items
Dynamic user interface items.
Definition: dynui.h:24
#define list_for_each_entry(pos, head, member)
Iterate over entries in a list.
Definition: list.h:432
struct list_head list
List of dynamic user interface items.
Definition: dynui.h:32
#define NULL
NULL pointer (VOID *)
Definition: Base.h:322

References index, dynamic_ui::items, dynamic_item::list, list_for_each_entry, and NULL.

Referenced by draw_menu_item(), and menu_loop().

◆ dynui_shortcut()

struct dynamic_item* dynui_shortcut ( struct dynamic_ui dynui,
int  key 
)

Find dynamic user interface item by shortcut key.

Parameters
dynuiDynamic user interface
keyShortcut key
Return values
itemUser interface item, or NULL if not found

Definition at line 212 of file dynui.c.

212  {
213  struct dynamic_item *item;
214 
215  list_for_each_entry ( item, &dynui->items, list ) {
216  if ( key && ( key == item->shortcut ) )
217  return item;
218  }
219 
220  return NULL;
221 }
A dynamic user interface item.
Definition: dynui.h:30
struct list_head items
Dynamic user interface items.
Definition: dynui.h:24
#define list_for_each_entry(pos, head, member)
Iterate over entries in a list.
Definition: list.h:432
int shortcut
Shortcut key.
Definition: dynui.h:42
struct list_head list
List of dynamic user interface items.
Definition: dynui.h:32
#define NULL
NULL pointer (VOID *)
Definition: Base.h:322
union @391 key
Sense key.
Definition: scsi.h:18

References dynamic_ui::items, key, dynamic_item::list, list_for_each_entry, NULL, and dynamic_item::shortcut.

Referenced by form_loop(), and menu_loop().