iPXE
dynui.c
Go to the documentation of this file.
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., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  *
19  * You can also choose to distribute this program under the terms of
20  * the Unmodified Binary Distribution Licence (as given in the file
21  * COPYING.UBDL), provided that you have satisfied its requirements.
22  */
23 
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25 
26 /** @file
27  *
28  * Dynamic user interfaces
29  *
30  */
31 
32 #include <stdlib.h>
33 #include <string.h>
34 #include <assert.h>
35 #include <ipxe/list.h>
36 #include <ipxe/dynui.h>
37 
38 /** List of all dynamic user interfaces */
39 static LIST_HEAD ( dynamic_uis );
40 
41 /**
42  * Create dynamic user interface
43  *
44  * @v name User interface name, or NULL
45  * @v title User interface title, or NULL
46  * @ret dynui Dynamic user interface, or NULL on failure
47  */
48 struct dynamic_ui * create_dynui ( const char *name, const char *title ) {
49  struct dynamic_ui *dynui;
50  size_t name_len;
51  size_t title_len;
52  size_t len;
53  char *name_copy;
54  char *title_copy;
55 
56  /* Destroy any existing user interface of this name */
57  dynui = find_dynui ( name );
58  if ( dynui )
59  destroy_dynui ( dynui );
60 
61  /* Use empty title if none given */
62  if ( ! title )
63  title = "";
64 
65  /* Allocate user interface */
66  name_len = ( name ? ( strlen ( name ) + 1 /* NUL */ ) : 0 );
67  title_len = ( strlen ( title ) + 1 /* NUL */ );
68  len = ( sizeof ( *dynui ) + name_len + title_len );
69  dynui = zalloc ( len );
70  if ( ! dynui )
71  return NULL;
72  name_copy = ( ( void * ) ( dynui + 1 ) );
73  title_copy = ( name_copy + name_len );
74 
75  /* Initialise user interface */
76  if ( name ) {
77  strcpy ( name_copy, name );
78  dynui->name = name_copy;
79  }
80  strcpy ( title_copy, title );
81  dynui->title = title_copy;
82  INIT_LIST_HEAD ( &dynui->items );
83 
84  /* Add to list of user interfaces */
85  list_add_tail ( &dynui->list, &dynamic_uis );
86 
87  DBGC ( dynui, "DYNUI %s created with title \"%s\"\n",
88  dynui->name, dynui->title );
89 
90  return dynui;
91 }
92 
93 /**
94  * Add dynamic user interface item
95  *
96  * @v dynui Dynamic user interface
97  * @v name Name, or NULL
98  * @v text Text, or NULL
99  * @v flags Flags
100  * @v shortcut Shortcut key
101  * @ret item User interface item, or NULL on failure
102  */
103 struct dynamic_item * add_dynui_item ( struct dynamic_ui *dynui,
104  const char *name, const char *text,
105  unsigned int flags, int shortcut ) {
106  struct dynamic_item *item;
107  size_t name_len;
108  size_t text_len;
109  size_t len;
110  char *name_copy;
111  char *text_copy;
112 
113  /* Use empty text if none given */
114  if ( ! text )
115  text = "";
116 
117  /* Allocate item */
118  name_len = ( name ? ( strlen ( name ) + 1 /* NUL */ ) : 0 );
119  text_len = ( strlen ( text ) + 1 /* NUL */ );
120  len = ( sizeof ( *item ) + name_len + text_len );
121  item = zalloc ( len );
122  if ( ! item )
123  return NULL;
124  name_copy = ( ( void * ) ( item + 1 ) );
125  text_copy = ( name_copy + name_len );
126 
127  /* Initialise item */
128  if ( name ) {
129  strcpy ( name_copy, name );
130  item->name = name_copy;
131  }
132  strcpy ( text_copy, text );
133  item->text = text_copy;
134  item->index = dynui->count++;
135  item->flags = flags;
136  item->shortcut = shortcut;
137 
138  /* Add to list of items */
139  list_add_tail ( &item->list, &dynui->items );
140 
141  return item;
142 }
143 
144 /**
145  * Destroy dynamic user interface
146  *
147  * @v dynui Dynamic user interface
148  */
149 void destroy_dynui ( struct dynamic_ui *dynui ) {
150  struct dynamic_item *item;
151  struct dynamic_item *tmp;
152 
153  /* Remove from list of user interfaces */
154  list_del ( &dynui->list );
155 
156  /* Free items */
157  list_for_each_entry_safe ( item, tmp, &dynui->items, list ) {
158  list_del ( &item->list );
159  free ( item );
160  }
161 
162  /* Free user interface */
163  free ( dynui );
164 }
165 
166 /**
167  * Find dynamic user interface
168  *
169  * @v name User interface name, or NULL
170  * @ret dynui Dynamic user interface, or NULL if not found
171  */
172 struct dynamic_ui * find_dynui ( const char *name ) {
173  struct dynamic_ui *dynui;
174 
175  list_for_each_entry ( dynui, &dynamic_uis, list ) {
176  if ( ( dynui->name == name ) ||
177  ( strcmp ( dynui->name, name ) == 0 ) ) {
178  return dynui;
179  }
180  }
181 
182  return NULL;
183 }
184 
185 /**
186  * Find dynamic user interface item by index
187  *
188  * @v dynui Dynamic user interface
189  * @v index Index
190  * @ret item User interface item, or NULL if not found
191  */
192 struct dynamic_item * dynui_item ( struct dynamic_ui *dynui,
193  unsigned int index ) {
194  struct dynamic_item *item;
195 
196  list_for_each_entry ( item, &dynui->items, list ) {
197  if ( index-- == 0 )
198  return item;
199  }
200 
201  return NULL;
202 }
203 
204 /**
205  * Find dynamic user interface item by shortcut key
206  *
207  * @v dynui Dynamic user interface
208  * @v key Shortcut key
209  * @ret item User interface item, or NULL if not found
210  */
211 struct dynamic_item * dynui_shortcut ( struct dynamic_ui *dynui, int key ) {
212  struct dynamic_item *item;
213 
214  list_for_each_entry ( item, &dynui->items, list ) {
215  if ( key && ( key == item->shortcut ) )
216  return item;
217  }
218 
219  return NULL;
220 }
const char * title
Title.
Definition: dynui.h:21
const char * name
Definition: ath9k_hw.c:1984
A dynamic user interface item.
Definition: dynui.h:29
#define DBGC(...)
Definition: compiler.h:505
struct dynamic_ui * find_dynui(const char *name)
Find dynamic user interface.
Definition: dynui.c:172
long index
Definition: bigint.h:61
unsigned int count
Number of user interface items.
Definition: dynui.h:25
unsigned long tmp
Definition: linux_pci.h:63
#define list_del(list)
Delete an entry from a list.
Definition: list.h:119
static LIST_HEAD(dynamic_uis)
List of all dynamic user interfaces.
struct list_head items
Dynamic user interface items.
Definition: dynui.h:23
Assertions.
const char * name
Name.
Definition: dynui.h:33
struct dynamic_item * dynui_shortcut(struct dynamic_ui *dynui, int key)
Find dynamic user interface item by shortcut key.
Definition: dynui.c:211
#define list_for_each_entry(pos, head, member)
Iterate over entries in a list.
Definition: list.h:431
#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:346
struct dynamic_ui * create_dynui(const char *name, const char *title)
Create dynamic user interface.
Definition: dynui.c:48
#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:458
Linked lists.
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.
Definition: dynui.c:103
uint8_t flags
Flags.
Definition: ena.h:18
static void(* free)(struct refcnt *refcnt))
Definition: refcnt.h:54
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:243
A dynamic user interface.
Definition: dynui.h:15
#define INIT_LIST_HEAD(list)
Initialise a list head.
Definition: list.h:45
int shortcut
Shortcut key.
Definition: dynui.h:41
int strcmp(const char *first, const char *second)
Compare strings.
Definition: string.c:173
const char * text
Text.
Definition: dynui.h:35
struct list_head list
List of dynamic user interfaces.
Definition: dynui.h:17
const char * name
Name.
Definition: dynui.h:19
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
Dynamic user interfaces.
unsigned int flags
Flags.
Definition: dynui.h:39
void destroy_dynui(struct dynamic_ui *dynui)
Destroy dynamic user interface.
Definition: dynui.c:149
unsigned int index
Index.
Definition: dynui.h:37
struct dynamic_item * dynui_item(struct dynamic_ui *dynui, unsigned int index)
Find dynamic user interface item by index.
Definition: dynui.c:192
struct list_head list
List of dynamic user interface items.
Definition: dynui.h:31
uint32_t len
Length.
Definition: ena.h:14
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
String functions.
union @383 key
Sense key.
Definition: scsi.h:18