2 * (C) Copyright 2009-2010 Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
4 * See file CREDITS for list of people who contributed to this
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; version 2 of
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
29 #include <linux/err.h>
32 #if defined(CONFIG_CMD_MENU_MANAGEMENT)
44 #if defined(CONFIG_CMD_MENU_MANAGEMENT)
54 #if defined(CONFIG_CMD_MENU_MANAGEMENT)
55 #define OPTS "m:earlc:d:RsSn:u:"
56 #define is_entry(x) ((x)->entry)
59 #define is_entry(x) (0)
62 #if defined(CONFIG_CMD_MENU_MANAGEMENT)
64 * menu -e -a -m <menu> -c <command> [-R] -d <description>
65 * menu -e -a -m <menu> -u submenu -d <description>
67 static int do_menu_entry_add(struct cmd_menu
*cm
)
69 struct menu_entry
*me
;
72 if (!cm
->menu
|| (!cm
->command
&& !cm
->submenu
) || !cm
->description
)
75 m
= menu_get_by_name(cm
->menu
);
78 eprintf("Menu '%s' not found\n", cm
->menu
);
83 me
= menu_add_submenu(m
, cm
->submenu
, cm
->description
);
85 me
= menu_add_command_entry(m
, cm
->description
, cm
->command
);
89 me
->non_re_ent
= !cm
->re_entrant
;
95 * menu -e -r -m <name> -n <num>
97 static int do_menu_entry_remove(struct cmd_menu
*cm
)
100 struct menu_entry
*me
;
102 if (!cm
->menu
|| cm
->num
< 0)
105 m
= menu_get_by_name(cm
->menu
);
108 eprintf("Menu '%s' not found\n", cm
->menu
);
112 me
= menu_entry_get_by_num(m
, cm
->num
);
115 eprintf("Entry '%s' not found\n", cm
->num
);
119 menu_remove_entry(m
, me
);
127 * menu -a -m <name> -d <description>
129 static int do_menu_add(struct cmd_menu
*cm
)
134 if (!cm
->menu
|| !cm
->description
)
142 m
->name
= strdup(cm
->menu
);
146 m
->display
= strdup(cm
->description
);
158 eprintf("Menu '%s' add fail", cm
->menu
);
160 eputs(" already exist");
170 static int do_menu_remove(struct cmd_menu
*cm
)
174 m
= menu_get_by_name(cm
->menu
);
177 eprintf("Menu '%s' not found\n", cm
->menu
);
189 * menu -m <menu> -S -n <entry num starting at 1>
191 static int do_menu_select(struct cmd_menu
*cm
)
198 m
= menu_get_by_name(cm
->menu
);
201 eprintf("Menu '%s' not found\n", cm
->menu
);
205 if (!menu_set_selected(m
, cm
->num
)) {
206 eprintf("Entry '%d' not found\n", cm
->num
);
217 static int do_menu_show(struct cmd_menu
*cm
)
222 m
= menu_get_by_name(cm
->menu
);
224 m
= menu_get_by_name("boot");
229 static void print_entries(struct menu
*m
)
231 struct menu_entry
*me
;
233 list_for_each_entry(me
, &m
->entries
, list
)
234 printf("%d: %s\n", me
->num
, me
->display
);
241 static int do_menu_list(struct cmd_menu
*cm
)
243 struct menu
* m
= NULL
;
244 struct menu
* menus
= menu_get_menus();
251 m
= menu_get_by_name(cm
->menu
);
259 list_for_each_entry(m
, &menus
->list
, list
) {
260 printf("%s: %s\n", m
->name
, m
->display
? m
->display
: m
->name
);
268 #if defined(CONFIG_CMD_MENU_MANAGEMENT)
269 static int do_menu_entry(struct cmd_menu
*cm
)
273 return do_menu_list(cm
);
275 return do_menu_entry_remove(cm
);
277 return do_menu_entry_add(cm
);
285 static int do_menu_entry(struct cmd_menu
*cm
)
291 static int do_menu(struct command
*cmdtp
, int argc
, char *argv
[])
298 return COMMAND_ERROR_USAGE
;
300 memset(&cm
, 0, sizeof(struct cmd_menu
));
301 #if defined(CONFIG_CMD_MENU_MANAGEMENT)
305 cm
.action
= action_show
;
307 while((opt
= getopt(argc
, argv
, OPTS
)) > 0) {
313 cm
.action
= action_list
;
316 cm
.action
= action_show
;
318 #if defined(CONFIG_CMD_MENU_MANAGEMENT)
323 cm
.action
= action_add
;
326 cm
.action
= action_remove
;
335 cm
.description
= optarg
;
341 cm
.action
= action_select
;
344 cm
.num
= simple_strtoul(optarg
, NULL
, 10);
353 ret
= do_menu_entry(&cm
);
359 ret
= do_menu_list(&cm
);
361 #if defined(CONFIG_CMD_MENU_MANAGEMENT)
363 ret
= do_menu_remove(&cm
);
366 ret
= do_menu_add(&cm
);
369 ret
= do_menu_select(&cm
);
373 ret
= do_menu_show(&cm
);
384 static const __maybe_unused
char cmd_menu_help
[] =
385 "Usage: menu [OPTION]... \n"
390 #if defined(CONFIG_CMD_MENU_MANAGEMENT)
401 " menu -s -m <menu>\n"
406 #if defined(CONFIG_CMD_MENU_MANAGEMENT)
408 " menu -a -m <name> -d <description>\n"
411 " menu -r -m <name>\n"
414 " (-R for do no exit the menu after executing the command)\n"
415 " menu -e -a -m <menu> -c <command> [-R] -d <description>\n"
417 "Add a submenu entry\n"
418 " (-R is not needed)\n"
419 " menu -e -a -m <menu> -u <menu> -d <description>\n"
422 " menu -e -r -m <name> -n <num>\n"
425 " menu -m <menu> -S -n <entry num starting at 1>\n"
428 " menu -e -l [menu]\n"
431 "menu -a -m boot -d \"Boot Menu\"\n"
432 "menu -e -a -m boot -c boot -d \"Boot\"\n"
433 "menu -e -a -m boot -c reset -d \"Reset\"\n"
441 BAREBOX_CMD_START(menu
)
443 .usage
= "Menu Management",
444 BAREBOX_CMD_HELP(cmd_menu_help
)