Commit 5ea9f64f authored by Cheng Renquan's avatar Cheng Renquan Committed by Michal Marek

scripts/kconfig/nconf: dynamically alloc dialog_input_result

To support unlimited length string config items;

No check for realloc return value keeps code simple, and to be
consistent with other existing unchecked malloc in kconfig.
Signed-off-by: default avatarCheng Renquan <crquan@gmail.com>
Signed-off-by: default avatarArnaud Lacombe <lacombar@gmail.com>
parent cd58a90f
...@@ -278,6 +278,9 @@ static int global_exit; ...@@ -278,6 +278,9 @@ static int global_exit;
/* the currently selected button */ /* the currently selected button */
const char *current_instructions = menu_instructions; const char *current_instructions = menu_instructions;
static char *dialog_input_result;
static int dialog_input_result_len;
static void conf(struct menu *menu); static void conf(struct menu *menu);
static void conf_choice(struct menu *menu); static void conf_choice(struct menu *menu);
static void conf_string(struct menu *menu); static void conf_string(struct menu *menu);
...@@ -693,7 +696,6 @@ static void search_conf(void) ...@@ -693,7 +696,6 @@ static void search_conf(void)
{ {
struct symbol **sym_arr; struct symbol **sym_arr;
struct gstr res; struct gstr res;
char dialog_input_result[100];
char *dialog_input; char *dialog_input;
int dres; int dres;
again: again:
...@@ -701,7 +703,7 @@ static void search_conf(void) ...@@ -701,7 +703,7 @@ static void search_conf(void)
_("Search Configuration Parameter"), _("Search Configuration Parameter"),
_("Enter " CONFIG_ " (sub)string to search for " _("Enter " CONFIG_ " (sub)string to search for "
"(with or without \"" CONFIG_ "\")"), "(with or without \"" CONFIG_ "\")"),
"", dialog_input_result, 99); "", &dialog_input_result, &dialog_input_result_len);
switch (dres) { switch (dres) {
case 0: case 0:
break; break;
...@@ -1346,7 +1348,6 @@ static void conf_choice(struct menu *menu) ...@@ -1346,7 +1348,6 @@ static void conf_choice(struct menu *menu)
static void conf_string(struct menu *menu) static void conf_string(struct menu *menu)
{ {
const char *prompt = menu_get_prompt(menu); const char *prompt = menu_get_prompt(menu);
char dialog_input_result[256];
while (1) { while (1) {
int res; int res;
...@@ -1369,8 +1370,8 @@ static void conf_string(struct menu *menu) ...@@ -1369,8 +1370,8 @@ static void conf_string(struct menu *menu)
prompt ? _(prompt) : _("Main Menu"), prompt ? _(prompt) : _("Main Menu"),
heading, heading,
sym_get_string_value(menu->sym), sym_get_string_value(menu->sym),
dialog_input_result, &dialog_input_result,
sizeof(dialog_input_result)); &dialog_input_result_len);
switch (res) { switch (res) {
case 0: case 0:
if (sym_set_string_value(menu->sym, if (sym_set_string_value(menu->sym,
...@@ -1390,14 +1391,13 @@ static void conf_string(struct menu *menu) ...@@ -1390,14 +1391,13 @@ static void conf_string(struct menu *menu)
static void conf_load(void) static void conf_load(void)
{ {
char dialog_input_result[256];
while (1) { while (1) {
int res; int res;
res = dialog_inputbox(main_window, res = dialog_inputbox(main_window,
NULL, load_config_text, NULL, load_config_text,
filename, filename,
dialog_input_result, &dialog_input_result,
sizeof(dialog_input_result)); &dialog_input_result_len);
switch (res) { switch (res) {
case 0: case 0:
if (!dialog_input_result[0]) if (!dialog_input_result[0])
...@@ -1422,14 +1422,13 @@ static void conf_load(void) ...@@ -1422,14 +1422,13 @@ static void conf_load(void)
static void conf_save(void) static void conf_save(void)
{ {
char dialog_input_result[256];
while (1) { while (1) {
int res; int res;
res = dialog_inputbox(main_window, res = dialog_inputbox(main_window,
NULL, save_config_text, NULL, save_config_text,
filename, filename,
dialog_input_result, &dialog_input_result,
sizeof(dialog_input_result)); &dialog_input_result_len);
switch (res) { switch (res) {
case 0: case 0:
if (!dialog_input_result[0]) if (!dialog_input_result[0])
......
...@@ -356,7 +356,7 @@ int btn_dialog(WINDOW *main_window, const char *msg, int btn_num, ...) ...@@ -356,7 +356,7 @@ int btn_dialog(WINDOW *main_window, const char *msg, int btn_num, ...)
int dialog_inputbox(WINDOW *main_window, int dialog_inputbox(WINDOW *main_window,
const char *title, const char *prompt, const char *title, const char *prompt,
const char *init, char *result, int result_len) const char *init, char **resultp, int *result_len)
{ {
int prompt_lines = 0; int prompt_lines = 0;
int prompt_width = 0; int prompt_width = 0;
...@@ -367,7 +367,12 @@ int dialog_inputbox(WINDOW *main_window, ...@@ -367,7 +367,12 @@ int dialog_inputbox(WINDOW *main_window,
int i, x, y; int i, x, y;
int res = -1; int res = -1;
int cursor_position = strlen(init); int cursor_position = strlen(init);
char *result = *resultp;
if (strlen(init)+1 > *result_len) {
*result_len = strlen(init)+1;
*resultp = result = realloc(result, *result_len);
}
/* find the widest line of msg: */ /* find the widest line of msg: */
prompt_lines = get_line_no(prompt); prompt_lines = get_line_no(prompt);
...@@ -384,7 +389,7 @@ int dialog_inputbox(WINDOW *main_window, ...@@ -384,7 +389,7 @@ int dialog_inputbox(WINDOW *main_window,
y = (LINES-(prompt_lines+4))/2; y = (LINES-(prompt_lines+4))/2;
x = (COLS-(prompt_width+4))/2; x = (COLS-(prompt_width+4))/2;
strncpy(result, init, result_len); strncpy(result, init, *result_len);
/* create the windows */ /* create the windows */
win = newwin(prompt_lines+6, prompt_width+7, y, x); win = newwin(prompt_lines+6, prompt_width+7, y, x);
...@@ -443,7 +448,7 @@ int dialog_inputbox(WINDOW *main_window, ...@@ -443,7 +448,7 @@ int dialog_inputbox(WINDOW *main_window,
case KEY_UP: case KEY_UP:
case KEY_RIGHT: case KEY_RIGHT:
if (cursor_position < len && if (cursor_position < len &&
cursor_position < min(result_len, prompt_width)) cursor_position < min(*result_len, prompt_width))
cursor_position++; cursor_position++;
break; break;
case KEY_DOWN: case KEY_DOWN:
...@@ -452,8 +457,13 @@ int dialog_inputbox(WINDOW *main_window, ...@@ -452,8 +457,13 @@ int dialog_inputbox(WINDOW *main_window,
cursor_position--; cursor_position--;
break; break;
default: default:
if ((isgraph(res) || isspace(res)) && if ((isgraph(res) || isspace(res))) {
len-2 < result_len) { /* one for new char, one for '\0' */
if (len+2 > *result_len) {
*result_len = len+2;
*resultp = result = realloc(result,
*result_len);
}
/* insert the char at the proper position */ /* insert the char at the proper position */
memmove(&result[cursor_position+1], memmove(&result[cursor_position+1],
&result[cursor_position], &result[cursor_position],
......
...@@ -89,7 +89,7 @@ void fill_window(WINDOW *win, const char *text); ...@@ -89,7 +89,7 @@ void fill_window(WINDOW *win, const char *text);
int btn_dialog(WINDOW *main_window, const char *msg, int btn_num, ...); int btn_dialog(WINDOW *main_window, const char *msg, int btn_num, ...);
int dialog_inputbox(WINDOW *main_window, int dialog_inputbox(WINDOW *main_window,
const char *title, const char *prompt, const char *title, const char *prompt,
const char *init, char *result, int result_len); const char *init, char **resultp, int *result_len);
void refresh_all_windows(WINDOW *main_window); void refresh_all_windows(WINDOW *main_window);
void show_scroll_win(WINDOW *main_window, void show_scroll_win(WINDOW *main_window,
const char *title, const char *title,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment