Blame view

common/menu.c 10 KB
b69bf52df   Jason Hobbs   Add generic, reus...
1
2
3
  /*
   * Copyright 2010-2011 Calxeda, Inc.
   *
1a4596601   Wolfgang Denk   Add GPL-2.0+ SPDX...
4
   * SPDX-License-Identifier:	GPL-2.0+
b69bf52df   Jason Hobbs   Add generic, reus...
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
   */
  
  #include <common.h>
  #include <malloc.h>
  #include <errno.h>
  #include <linux/list.h>
  
  #include "menu.h"
  
  /*
   * Internally, each item in a menu is represented by a struct menu_item.
   *
   * These items will be alloc'd and initialized by menu_item_add and destroyed
   * by menu_item_destroy, and the consumer of the interface never sees that
   * this struct is used at all.
   */
  struct menu_item {
  	char *key;
  	void *data;
  	struct list_head list;
  };
  
  /*
   * The menu is composed of a list of items along with settings and callbacks
   * provided by the user. An incomplete definition of this struct is available
   * in menu.h, but the full definition is here to prevent consumers from
   * relying on its contents.
   */
  struct menu {
  	struct menu_item *default_item;
b41bc5a82   Jason Hobbs   common, menu: use...
35
  	int timeout;
b69bf52df   Jason Hobbs   Add generic, reus...
36
37
38
  	char *title;
  	int prompt;
  	void (*item_data_print)(void *);
fc9d64ffc   Pali Rohár   menu: Add support...
39
40
  	char *(*item_choice)(void *);
  	void *item_choice_data;
b69bf52df   Jason Hobbs   Add generic, reus...
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
  	struct list_head items;
  };
  
  /*
   * An iterator function for menu items. callback will be called for each item
   * in m, with m, a pointer to the item, and extra being passed to callback. If
   * callback returns a value other than NULL, iteration stops and the value
   * return by callback is returned from menu_items_iter.  This allows it to be
   * used for search type operations. It is also safe for callback to remove the
   * item from the list of items.
   */
  static inline void *menu_items_iter(struct menu *m,
  		void *(*callback)(struct menu *, struct menu_item *, void *),
  		void *extra)
  {
  	struct list_head *pos, *n;
  	struct menu_item *item;
  	void *ret;
  
  	list_for_each_safe(pos, n, &m->items) {
  		item = list_entry(pos, struct menu_item, list);
  
  		ret = callback(m, item, extra);
  
  		if (ret)
  			return ret;
  	}
  
  	return NULL;
  }
  
  /*
   * Print a menu_item. If the consumer provided an item_data_print function
   * when creating the menu, call it with a pointer to the item's private data.
   * Otherwise, print the key of the item.
   */
  static inline void *menu_item_print(struct menu *m,
  				struct menu_item *item,
  				void *extra)
  {
d887ad54c   Wolfgang Denk   menu.c: use puts(...
81
  	if (!m->item_data_print) {
215749761   Anatolij Gustschin   common/menu.c: Fi...
82
  		puts(item->key);
d887ad54c   Wolfgang Denk   menu.c: use puts(...
83
84
85
  		putc('
  ');
  	} else {
b69bf52df   Jason Hobbs   Add generic, reus...
86
  		m->item_data_print(item->data);
d887ad54c   Wolfgang Denk   menu.c: use puts(...
87
  	}
b69bf52df   Jason Hobbs   Add generic, reus...
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
  
  	return NULL;
  }
  
  /*
   * Free the memory used by a menu item. This includes the memory used by its
   * key.
   */
  static inline void *menu_item_destroy(struct menu *m,
  				struct menu_item *item,
  				void *extra)
  {
  	if (item->key)
  		free(item->key);
  
  	free(item);
  
  	return NULL;
  }
e0611dd97   Heiko Schocher   common, menu: add...
107
108
109
110
111
112
  void __menu_display_statusline(struct menu *m)
  {
  	return;
  }
  void menu_display_statusline(struct menu *m)
  	__attribute__ ((weak, alias("__menu_display_statusline")));
b69bf52df   Jason Hobbs   Add generic, reus...
113
114
115
116
117
118
  /*
   * Display a menu so the user can make a choice of an item. First display its
   * title, if any, and then each item in the menu.
   */
  static inline void menu_display(struct menu *m)
  {
d887ad54c   Wolfgang Denk   menu.c: use puts(...
119
120
121
122
123
  	if (m->title) {
  		puts(m->title);
  		putc('
  ');
  	}
e0611dd97   Heiko Schocher   common, menu: add...
124
  	menu_display_statusline(m);
b69bf52df   Jason Hobbs   Add generic, reus...
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
  
  	menu_items_iter(m, menu_item_print, NULL);
  }
  
  /*
   * Check if an item's key matches a provided string, pointed to by extra. If
   * extra is NULL, an item with a NULL key will match. Otherwise, the item's
   * key has to match according to strcmp.
   *
   * This is called via menu_items_iter, so it returns a pointer to the item if
   * the key matches, and returns NULL otherwise.
   */
  static inline void *menu_item_key_match(struct menu *m,
  			struct menu_item *item, void *extra)
  {
  	char *item_key = extra;
  
  	if (!item_key || !item->key) {
  		if (item_key == item->key)
  			return item;
  
  		return NULL;
  	}
  
  	if (strcmp(item->key, item_key) == 0)
  		return item;
  
  	return NULL;
  }
  
  /*
   * Find the first item with a key matching item_key, if any exists.
   */
  static inline struct menu_item *menu_item_by_key(struct menu *m,
  							char *item_key)
  {
  	return menu_items_iter(m, menu_item_key_match, item_key);
  }
  
  /*
b69bf52df   Jason Hobbs   Add generic, reus...
165
166
167
   * Set *choice to point to the default item's data, if any default item was
   * set, and returns 1. If no default item was set, returns -ENOENT.
   */
6a3439fda   Anatolij Gustschin   menu: export menu...
168
  int menu_default_choice(struct menu *m, void **choice)
b69bf52df   Jason Hobbs   Add generic, reus...
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
  {
  	if (m->default_item) {
  		*choice = m->default_item->data;
  		return 1;
  	}
  
  	return -ENOENT;
  }
  
  /*
   * Displays the menu and asks the user to choose an item. *choice will point
   * to the private data of the item the user chooses. The user makes a choice
   * by inputting a string matching the key of an item. Invalid choices will
   * cause the user to be prompted again, repeatedly, until the user makes a
   * valid choice. The user can exit the menu without making a choice via ^c.
   *
   * Returns 1 if the user made a choice, or -EINTR if they bail via ^c.
   */
  static inline int menu_interactive_choice(struct menu *m, void **choice)
  {
  	char cbuf[CONFIG_SYS_CBSIZE];
  	struct menu_item *choice_item = NULL;
  	int readret;
  
  	while (!choice_item) {
  		cbuf[0] = '\0';
  
  		menu_display(m);
fc9d64ffc   Pali Rohár   menu: Add support...
197
198
199
  		if (!m->item_choice) {
  			readret = readline_into_buffer("Enter choice: ", cbuf,
  					m->timeout / 10);
b69bf52df   Jason Hobbs   Add generic, reus...
200

fc9d64ffc   Pali Rohár   menu: Add support...
201
202
203
204
205
206
207
  			if (readret >= 0) {
  				choice_item = menu_item_by_key(m, cbuf);
  				if (!choice_item)
  					printf("%s not found
  ", cbuf);
  			} else {
  				return menu_default_choice(m, choice);
fc4fa6a16   Heiko Schocher   common, menu: do ...
208
  			}
fc9d64ffc   Pali Rohár   menu: Add support...
209
210
211
212
213
214
215
216
217
  		} else {
  			char *key = m->item_choice(m->item_choice_data);
  
  			if (key)
  				choice_item = menu_item_by_key(m, key);
  		}
  
  		if (!choice_item)
  			m->timeout = 0;
b69bf52df   Jason Hobbs   Add generic, reus...
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
  	}
  
  	*choice = choice_item->data;
  
  	return 1;
  }
  
  /*
   * menu_default_set() - Sets the default choice for the menu. This is safe to
   * call more than once on a menu.
   *
   * m - Points to a menu created by menu_create().
   *
   * item_key - Points to a string that, when compared using strcmp, matches the
   * key for an existing item in the menu.
   *
   * Returns 1 if successful, -EINVAL if m is NULL, or -ENOENT if no item with a
   * key matching item_key is found.
   */
  int menu_default_set(struct menu *m, char *item_key)
  {
  	struct menu_item *item;
  
  	if (!m)
  		return -EINVAL;
  
  	item = menu_item_by_key(m, item_key);
  
  	if (!item)
  		return -ENOENT;
  
  	m->default_item = item;
  
  	return 1;
  }
  
  /*
   * menu_get_choice() - Returns the user's selected menu entry, or the default
b41bc5a82   Jason Hobbs   common, menu: use...
256
257
   * if the menu is set to not prompt or the timeout expires. This is safe to
   * call more than once.
b69bf52df   Jason Hobbs   Add generic, reus...
258
259
260
261
262
263
264
265
   *
   * m - Points to a menu created by menu_create().
   *
   * choice - Points to a location that will store a pointer to the selected
   * menu item. If no item is selected or there is an error, no value will be
   * written at the location it points to.
   *
   * Returns 1 if successful, -EINVAL if m or choice is NULL, -ENOENT if no
b41bc5a82   Jason Hobbs   common, menu: use...
266
267
   * default has been set and the menu is set to not prompt or the timeout
   * expires, or -EINTR if the user exits the menu via ^c.
b69bf52df   Jason Hobbs   Add generic, reus...
268
269
270
271
272
   */
  int menu_get_choice(struct menu *m, void **choice)
  {
  	if (!m || !choice)
  		return -EINVAL;
8594753ba   Rob Herring   menu: only timeou...
273
  	if (!m->prompt)
b69bf52df   Jason Hobbs   Add generic, reus...
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
  		return menu_default_choice(m, choice);
  
  	return menu_interactive_choice(m, choice);
  }
  
  /*
   * menu_item_add() - Adds or replaces a menu item. Note that this replaces the
   * data of an item if it already exists, but doesn't change the order of the
   * item.
   *
   * m - Points to a menu created by menu_create().
   *
   * item_key - Points to a string that will uniquely identify the item.  The
   * string will be copied to internal storage, and is safe to discard after
   * passing to menu_item_add.
   *
   * item_data - An opaque pointer associated with an item. It is never
   * dereferenced internally, but will be passed to the item_data_print, and
   * will be returned from menu_get_choice if the menu item is selected.
   *
   * Returns 1 if successful, -EINVAL if m is NULL, or -ENOMEM if there is
   * insufficient memory to add the menu item.
   */
  int menu_item_add(struct menu *m, char *item_key, void *item_data)
  {
  	struct menu_item *item;
  
  	if (!m)
  		return -EINVAL;
  
  	item = menu_item_by_key(m, item_key);
  
  	if (item) {
  		item->data = item_data;
  		return 1;
  	}
  
  	item = malloc(sizeof *item);
  	if (!item)
  		return -ENOMEM;
  
  	item->key = strdup(item_key);
  
  	if (!item->key) {
  		free(item);
  		return -ENOMEM;
  	}
  
  	item->data = item_data;
  
  	list_add_tail(&item->list, &m->items);
  
  	return 1;
  }
  
  /*
   * menu_create() - Creates a menu handle with default settings
   *
   * title - If not NULL, points to a string that will be displayed before the
   * list of menu items. It will be copied to internal storage, and is safe to
   * discard after passing to menu_create().
   *
b41bc5a82   Jason Hobbs   common, menu: use...
336
337
338
339
340
341
   * timeout - A delay in seconds to wait for user input. If 0, timeout is
   * disabled, and the default choice will be returned unless prompt is 1.
   *
   * prompt - If 0, don't ask for user input unless there is an interrupted
   * timeout. If 1, the user will be prompted for input regardless of the value
   * of timeout.
b69bf52df   Jason Hobbs   Add generic, reus...
342
343
344
345
346
347
348
   *
   * item_data_print - If not NULL, will be called for each item when the menu
   * is displayed, with the pointer to the item's data passed as the argument.
   * If NULL, each item's key will be printed instead.  Since an item's key is
   * what must be entered to select an item, the item_data_print function should
   * make it obvious what the key for each entry is.
   *
fc9d64ffc   Pali Rohár   menu: Add support...
349
350
351
352
353
354
   * item_choice - If not NULL, will be called when asking the user to choose an
   * item. Returns a key string corresponding to the choosen item or NULL if
   * no item has been selected.
   *
   * item_choice_data - Will be passed as the argument to the item_choice function
   *
b69bf52df   Jason Hobbs   Add generic, reus...
355
356
357
   * Returns a pointer to the menu if successful, or NULL if there is
   * insufficient memory available to create the menu.
   */
b41bc5a82   Jason Hobbs   common, menu: use...
358
  struct menu *menu_create(char *title, int timeout, int prompt,
fc9d64ffc   Pali Rohár   menu: Add support...
359
360
361
  				void (*item_data_print)(void *),
  				char *(*item_choice)(void *),
  				void *item_choice_data)
b69bf52df   Jason Hobbs   Add generic, reus...
362
363
364
365
366
367
368
369
370
371
  {
  	struct menu *m;
  
  	m = malloc(sizeof *m);
  
  	if (!m)
  		return NULL;
  
  	m->default_item = NULL;
  	m->prompt = prompt;
b41bc5a82   Jason Hobbs   common, menu: use...
372
  	m->timeout = timeout;
b69bf52df   Jason Hobbs   Add generic, reus...
373
  	m->item_data_print = item_data_print;
fc9d64ffc   Pali Rohár   menu: Add support...
374
375
  	m->item_choice = item_choice;
  	m->item_choice_data = item_choice_data;
b69bf52df   Jason Hobbs   Add generic, reus...
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
  
  	if (title) {
  		m->title = strdup(title);
  		if (!m->title) {
  			free(m);
  			return NULL;
  		}
  	} else
  		m->title = NULL;
  
  
  	INIT_LIST_HEAD(&m->items);
  
  	return m;
  }
  
  /*
   * menu_destroy() - frees the memory used by a menu and its items.
   *
   * m - Points to a menu created by menu_create().
   *
   * Returns 1 if successful, or -EINVAL if m is NULL.
   */
  int menu_destroy(struct menu *m)
  {
  	if (!m)
  		return -EINVAL;
  
  	menu_items_iter(m, menu_item_destroy, NULL);
  
  	if (m->title)
  		free(m->title);
  
  	free(m);
  
  	return 1;
  }