7daae5c4845849a303b9028f40c5f120074ae674
[openwrt/openwrt.git] /
1 From 5a15437610e8e8c68dc347845a83d0cbad80ca08 Mon Sep 17 00:00:00 2001
2 From: Weijie Gao <weijie.gao@mediatek.com>
3 Date: Tue, 19 Jan 2021 10:58:48 +0800
4 Subject: [PATCH 51/71] cmd: bootmenu: add ability to select item by shortkey
5
6 Add ability to use shortkey to select item for bootmenu command
7
8 Signed-off-by: Weijie Gao <weijie.gao@mediatek.com>
9 ---
10 cmd/bootmenu.c | 28 +++++++++++++++++++++++---
11 common/menu.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++
12 include/cli.h | 2 ++
13 include/menu.h | 3 +++
14 4 files changed, 84 insertions(+), 3 deletions(-)
15
16 --- a/cmd/bootmenu.c
17 +++ b/cmd/bootmenu.c
18 @@ -114,6 +114,8 @@ static char *bootmenu_choice_entry(void
19 ++menu->active;
20 /* no menu key selected, regenerate menu */
21 return NULL;
22 + case BKEY_CHOICE:
23 + menu->active = cch->choice;
24 case BKEY_SELECT:
25 iter = menu->first;
26 for (i = 0; i < menu->active; ++i)
27 @@ -182,6 +184,9 @@ static int prepare_bootmenu_entry(struct
28 unsigned short int i = *index;
29 struct bootmenu_entry *entry = NULL;
30 struct bootmenu_entry *iter = *current;
31 + char *choice_option;
32 + char choice_char;
33 + int len;
34
35 while ((option = bootmenu_getoption(i))) {
36
37 @@ -196,11 +201,28 @@ static int prepare_bootmenu_entry(struct
38 if (!entry)
39 return -ENOMEM;
40
41 - entry->title = strndup(option, sep - option);
42 + /* Add BKEY_CHOICE support: '%c. %s\0' : len --> len + 4 */
43 + len = sep - option + 4;
44 +
45 + choice_option = malloc(len);
46 + if (!choice_option) {
47 + free(entry->title);
48 + free(entry);
49 + return -ENOMEM;
50 + }
51 +
52 + if (!get_choice_char(i, &choice_char))
53 + len = snprintf(choice_option, len, "%c. %s", choice_char, option);
54 + else
55 + len = snprintf(choice_option, len, " %s", option);
56 +
57 + entry->title = strndup(choice_option, len);
58 +
59 if (!entry->title) {
60 free(entry);
61 return -ENOMEM;
62 }
63 + free(choice_option);
64
65 entry->command = strdup(sep + 1);
66 if (!entry->command) {
67 @@ -382,9 +404,9 @@ static struct bootmenu_data *bootmenu_cr
68
69 /* Add Quit entry if exiting bootmenu is disabled */
70 if (!IS_ENABLED(CONFIG_BOOTMENU_DISABLE_UBOOT_CONSOLE))
71 - entry->title = strdup("Exit");
72 + entry->title = strdup("0. Exit");
73 else
74 - entry->title = strdup("Quit");
75 + entry->title = strdup("0. Quit");
76
77 if (!entry->title) {
78 free(entry);
79 --- a/common/menu.c
80 +++ b/common/menu.c
81 @@ -8,6 +8,7 @@
82 #include <cli.h>
83 #include <malloc.h>
84 #include <errno.h>
85 +#include <linux/ctype.h>
86 #include <linux/delay.h>
87 #include <linux/list.h>
88 #include <watchdog.h>
89 @@ -49,6 +50,33 @@ struct menu {
90 int item_cnt;
91 };
92
93 +const char choice_chars[] = {
94 + '1', '2', '3', '4', '5', '6', '7', '8', '9',
95 + 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
96 + 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
97 + 'u', 'v', 'w', 'x', 'y', 'z'
98 +};
99 +
100 +static int find_choice(char choice)
101 +{
102 + int i;
103 +
104 + for (i = 0; i < ARRAY_SIZE(choice_chars); i++)
105 + if (tolower(choice) == choice_chars[i])
106 + return i;
107 +
108 + return -1;
109 +}
110 +
111 +int get_choice_char(int index, char *result)
112 +{
113 + if (index < ARRAY_SIZE(choice_chars))
114 + *result = choice_chars[index];
115 + else
116 + return -1;
117 + return 0;
118 +}
119 +
120 /*
121 * An iterator function for menu items. callback will be called for each item
122 * in m, with m, a pointer to the item, and extra being passed to callback. If
123 @@ -441,6 +469,7 @@ enum bootmenu_key bootmenu_autoboot_loop
124 {
125 enum bootmenu_key key = BKEY_NONE;
126 int i, c;
127 + int choice;
128
129 while (menu->delay > 0) {
130 if (ansi)
131 @@ -458,6 +487,18 @@ enum bootmenu_key bootmenu_autoboot_loop
132 menu->delay = -1;
133 c = getchar();
134
135 + choice = find_choice(c);
136 + if ((choice >= 0 &&
137 + choice < menu->count - 1)) {
138 + cch->choice = choice;
139 + key = BKEY_CHOICE;
140 + break;
141 + } else if (c == '0') {
142 + cch->choice = menu->count - 1;
143 + key = BKEY_CHOICE;
144 + break;
145 + }
146 +
147 ichar = cli_ch_process(cch, c);
148
149 switch (ichar) {
150 @@ -537,6 +578,7 @@ enum bootmenu_key bootmenu_loop(struct b
151 {
152 enum bootmenu_key key;
153 int c, errchar = 0;
154 + int choice;
155
156 c = cli_ch_process(cch, 0);
157 if (!c) {
158 @@ -548,6 +590,18 @@ enum bootmenu_key bootmenu_loop(struct b
159 }
160 if (!c) {
161 c = getchar();
162 +
163 + choice = find_choice(c);
164 + if ((choice >= 0 &&
165 + choice < menu->count - 1)) {
166 + cch->choice = choice;
167 + return BKEY_CHOICE;
168 +
169 + } else if (c == '0') {
170 + cch->choice = menu->count - 1;
171 + return BKEY_CHOICE;
172 + }
173 +
174 c = cli_ch_process(cch, c);
175 }
176 }
177 --- a/include/cli.h
178 +++ b/include/cli.h
179 @@ -23,6 +23,8 @@ struct cli_ch_state {
180 char esc_save[8];
181 int emit_upto;
182 bool emitting;
183 + /* mediatek bootmenu choice feature */
184 + char choice;
185 };
186
187 /**
188 --- a/include/menu.h
189 +++ b/include/menu.h
190 @@ -37,6 +37,8 @@ int menu_default_choice(struct menu *m,
191 */
192 int menu_show(int bootdelay);
193
194 +int get_choice_char(int index, char *result);
195 +
196 struct bootmenu_data {
197 int delay; /* delay for autoboot */
198 int active; /* active menu entry */
199 @@ -51,6 +53,7 @@ enum bootmenu_key {
200 BKEY_UP,
201 BKEY_DOWN,
202 BKEY_SELECT,
203 + BKEY_CHOICE,
204 BKEY_QUIT,
205 BKEY_SAVE,
206