2 * Samba Unix/Linux SMB client library
4 * Copyright (C) Christopher Davis 2012
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "regedit_dialog.h"
22 #include "regedit_valuelist.h"
24 #include "lib/registry/registry.h"
28 static char *string_trim_n(TALLOC_CTX *ctx, const char *buf, size_t n)
32 str = talloc_strndup(ctx, buf, n);
35 trim_string(str, " ", " ");
41 static char *string_trim(TALLOC_CTX *ctx, const char *buf)
45 str = talloc_strdup(ctx, buf);
48 trim_string(str, " ", " ");
54 static int dialog_free(struct dialog *dia)
59 if (dia->sub_window) {
60 delwin(dia->sub_window);
63 del_panel(dia->panel);
66 unpost_menu(dia->choices);
67 free_menu(dia->choices);
69 if (dia->choice_items) {
71 for (it = dia->choice_items; *it != NULL; ++it) {
79 struct dialog *dialog_new(TALLOC_CTX *ctx, const char *title, int nlines,
80 int ncols, int y, int x)
84 dia = talloc_zero(ctx, struct dialog);
89 talloc_set_destructor(dia, dialog_free);
91 dia->window = newwin(nlines, ncols, y, x);
92 if (dia->window == NULL) {
96 box(dia->window, 0, 0);
97 mvwaddstr(dia->window, 0, 1, title);
99 /* body of the dialog within the box outline */
100 dia->sub_window = derwin(dia->window, nlines - 2, ncols - 2, 1, 1);
101 if (dia->sub_window == NULL) {
105 dia->panel = new_panel(dia->window);
106 if (dia->panel == NULL) {
119 static void center_dialog_above_window(WINDOW *below, int *nlines, int *ncols,
123 int centery, centerx;
125 getmaxyx(below, maxy, maxx);
132 if (*nlines > maxy) {
139 if (*nlines < centery) {
140 *y = centery - *nlines;
142 if (*ncols < centerx) {
143 *x = centerx - *ncols;
147 struct dialog *dialog_center_new(TALLOC_CTX *ctx, const char *title, int nlines,
148 int ncols, WINDOW *below)
152 center_dialog_above_window(below, &nlines, &ncols, &y, &x);
154 return dialog_new(ctx, title, nlines, ncols, y, x);
157 struct dialog *dialog_choice_new(TALLOC_CTX *ctx, const char *title,
158 const char **choices, int nlines,
159 int ncols, int y, int x)
164 dia = dialog_new(ctx, title, nlines, ncols, y, x);
169 dia->menu_window = derwin(dia->sub_window, 1, ncols - 3,
171 if (dia->menu_window == NULL) {
175 for (nchoices = 0; choices[nchoices] != NULL; ++nchoices)
177 dia->choice_items = talloc_zero_array(dia, ITEM *, nchoices + 1);
178 if (dia->choice_items == NULL) {
181 for (i = 0; i < nchoices; ++i) {
182 char *desc = talloc_strdup(dia, choices[i]);
186 dia->choice_items[i] = new_item(desc, desc);
187 if (dia->choice_items[i] == NULL) {
190 /* store choice index */
191 set_item_userptr(dia->choice_items[i], (void*)(uintptr_t)i);
194 dia->choices = new_menu(dia->choice_items);
195 if (dia->choices == NULL) {
199 set_menu_format(dia->choices, 1, ncols);
200 set_menu_win(dia->choices, dia->sub_window);
201 set_menu_sub(dia->choices, dia->menu_window);
202 menu_opts_off(dia->choices, O_SHOWDESC);
203 set_menu_mark(dia->choices, "* ");
204 post_menu(dia->choices);
205 wmove(dia->sub_window, 0, 0);
215 struct dialog *dialog_choice_center_new(TALLOC_CTX *ctx, const char *title,
216 const char **choices, int nlines,
217 int ncols, WINDOW *below)
221 center_dialog_above_window(below, &nlines, &ncols, &y, &x);
223 return dialog_choice_new(ctx, title, choices, nlines, ncols, y, x);
226 static int handle_menu_input(MENU *menu, int c)
232 menu_driver(menu, REQ_LEFT_ITEM);
235 menu_driver(menu, REQ_RIGHT_ITEM);
239 item = current_item(menu);
240 return (int)(uintptr_t)item_userptr(item);
246 static void handle_form_input(FORM *frm, int c)
250 form_driver(frm, REQ_NEW_LINE);
253 form_driver(frm, REQ_UP_CHAR);
256 form_driver(frm, REQ_DOWN_CHAR);
260 form_driver(frm, REQ_DEL_PREV);
263 form_driver(frm, REQ_LEFT_CHAR);
266 form_driver(frm, REQ_RIGHT_CHAR);
274 static int modal_loop(struct dialog *dia)
279 keypad(dia->window, true);
283 while (selection == -1) {
284 c = wgetch(dia->window);
285 selection = handle_menu_input(dia->choices, c);
295 static struct dialog *dialog_msg_new(TALLOC_CTX *ctx, const char *title,
296 const char **choices, WINDOW *below,
297 int nlines, const char *msg, va_list ap)
304 str = talloc_vasprintf(ctx, msg, ap);
309 width = strlen(str) + 2;
310 if (width < MIN_WIDTH) {
313 dia = dialog_choice_center_new(ctx, title, choices, nlines, width, below);
318 waddstr(dia->sub_window, str);
324 int dialog_input(TALLOC_CTX *ctx, char **output, const char *title,
325 WINDOW *below, const char *msg, ...)
329 const char *choices[] = {
334 FIELD *field[2] = {0};
339 bool input_section = true;
342 dia = dialog_msg_new(ctx, title, choices, below, 7, msg, ap);
348 getmaxyx(dia->sub_window, y, x);
349 input_win = derwin(dia->sub_window, 1, x - 2, 2, 1);
350 if (input_win == NULL) {
353 field[0] = new_field(1, x - 2, 0, 0, 0, 0);
354 if (field[0] == NULL) {
358 field_opts_off(field[0], O_BLANK | O_AUTOSKIP | O_STATIC);
359 set_field_back(field[0], A_REVERSE);
361 input = new_form(field);
362 form_opts_off(input, O_NL_OVERLOAD | O_BS_OVERLOAD);
363 set_form_win(input, dia->sub_window);
364 set_form_sub(input, input_win);
365 set_current_field(input, field[0]);
369 keypad(dia->window, true);
374 int c = wgetch(dia->window);
376 if (c == '\t' || c == KEY_BTAB) {
378 if (form_driver(input,REQ_VALIDATION) == E_OK) {
379 input_section = false;
380 menu_driver(dia->choices, REQ_FIRST_ITEM);
383 input_section = true;
384 set_current_field(input, field[0]);
390 handle_form_input(input, c);
392 rv = handle_menu_input(dia->choices, c);
393 if (rv == DIALOG_OK) {
394 const char *buf = field_buffer(field[0], 0);
395 *output = string_trim(ctx, buf);
406 free_field(field[0]);
416 int dialog_notice(TALLOC_CTX *ctx, enum dialog_type type,
417 const char *title, WINDOW *below,
418 const char *msg, ...)
422 const char *choices[] = {
428 if (type == DIA_ALERT) {
433 dia = dialog_msg_new(ctx, title, choices, below, 5, msg, ap);
439 return modal_loop(dia);
454 FIELD *field[MAX_FIELDS];
455 enum input_section section;
458 static int edit_dialog_free(struct edit_dialog *edit)
461 unpost_form(edit->input);
463 if (edit->field[0]) {
464 free_field(edit->field[0]);
466 if (edit->field[1]) {
467 free_field(edit->field[1]);
469 delwin(edit->input_win);
474 static WERROR fill_value_buffer(struct edit_dialog *edit,
475 const struct value_item *vitem)
479 switch (vitem->type) {
482 if (vitem->data.length >= 4) {
483 v = IVAL(vitem->data.data, 0);
485 tmp = talloc_asprintf(edit, "0x%x", v);
489 set_field_buffer(edit->field[1], 0, tmp);
494 case REG_EXPAND_SZ: {
497 if (!pull_reg_sz(edit, &vitem->data, &s)) {
500 set_field_buffer(edit->field[1], 0, s);
507 if (!pull_reg_multi_sz(edit, &vitem->data, &a)) {
510 for (p = a; *p != NULL; ++p) {
512 buf = talloc_asprintf(edit, "%s\n", *p);
514 buf = talloc_asprintf_append(buf, "%s\n", *p);
520 set_field_buffer(edit->field[1], 0, buf);
529 static bool value_exists(TALLOC_CTX *ctx, const struct registry_key *key,
536 rv = reg_key_get_value_by_name(ctx, key, name, &type, &blob);
538 return W_ERROR_IS_OK(rv);
541 static WERROR set_value(struct edit_dialog *edit, struct registry_key *key,
542 uint32_t type, bool new_value)
546 const char *buf = field_buffer(edit->field[1], 0);
547 char *name = string_trim(edit, field_buffer(edit->field[0], 0));
552 if (!new_value && !field_status(edit->field[1])) {
555 if (new_value && value_exists(edit, key, name)) {
556 return WERR_FILE_EXISTS;
564 if (buf[0] == '0' && tolower(buf[1]) == 'x') {
568 val = strtoul(buf, NULL, base);
569 blob = data_blob_talloc(edit, NULL, sizeof(val));
570 SIVAL(blob.data, 0, val);
575 case REG_EXPAND_SZ: {
576 char *str = string_trim(edit, buf);
577 if (!str || !push_reg_sz(edit, &blob, str)) {
587 dynamic_field_info(edit->field[1], &rows, &cols, &max);
589 arr = talloc_zero_array(edit, const char *, rows + 1);
593 for (i = 0; *buf; ++i, buf += cols) {
594 SMB_ASSERT(i < rows);
595 arr[i] = string_trim_n(edit, buf, cols);
597 if (!push_reg_multi_sz(edit, &blob, arr)) {
604 rv = reg_val_set(key, name, type, blob);
609 static void section_down(struct edit_dialog *edit)
611 switch (edit->section) {
613 if (form_driver(edit->input, REQ_VALIDATION) == E_OK) {
614 edit->section = IN_DATA;
615 set_current_field(edit->input, edit->field[1]);
619 if (form_driver(edit->input, REQ_VALIDATION) == E_OK) {
620 edit->section = IN_MENU;
621 menu_driver(edit->dia->choices, REQ_FIRST_ITEM);
625 edit->section = IN_NAME;
626 set_current_field(edit->input, edit->field[0]);
631 static void section_up(struct edit_dialog *edit)
633 switch (edit->section) {
635 if (form_driver(edit->input, REQ_VALIDATION) == E_OK) {
636 edit->section = IN_MENU;
637 menu_driver(edit->dia->choices, REQ_FIRST_ITEM);
641 if (form_driver(edit->input, REQ_VALIDATION) == E_OK) {
642 edit->section = IN_NAME;
643 set_current_field(edit->input, edit->field[0]);
647 edit->section = IN_DATA;
648 set_current_field(edit->input, edit->field[1]);
653 WERROR dialog_edit_value(TALLOC_CTX *ctx, struct registry_key *key, uint32_t type,
654 const struct value_item *vitem, WINDOW *below)
656 struct edit_dialog *edit;
657 const char *choices[] = {
663 int nlines, ncols, val_rows;
664 WERROR rv = WERR_NOMEM;
667 edit = talloc_zero(ctx, struct edit_dialog);
671 talloc_set_destructor(edit, edit_dialog_free);
673 title = talloc_asprintf(edit, "Edit %s value", str_regtype(type));
679 if (type == REG_MULTI_SZ) {
683 edit->dia = dialog_choice_center_new(edit, title, choices, nlines,
686 if (edit->dia == NULL) {
691 edit->field[0] = new_field(1, ncols - 4, 1, 1, 0, 0);
692 if (edit->field[0] == NULL) {
698 if (type == REG_MULTI_SZ) {
701 edit->field[1] = new_field(val_rows, ncols - 4, 4, 1, 0, 0);
702 if (edit->field[1] == NULL) {
705 set_field_back(edit->field[0], A_REVERSE);
706 set_field_back(edit->field[1], A_REVERSE);
707 field_opts_off(edit->field[0], O_BLANK | O_AUTOSKIP | O_STATIC);
708 field_opts_off(edit->field[1], O_BLANK | O_AUTOSKIP | O_STATIC | O_WRAP);
709 if (type == REG_DWORD) {
710 set_field_type(edit->field[1], TYPE_REGEXP,
711 "^ *([0-9]+|0[xX][0-9a-fA-F]+) *$");
715 set_field_buffer(edit->field[0], 0, vitem->value_name);
716 field_opts_off(edit->field[0], O_EDIT);
717 fill_value_buffer(edit, vitem);
720 edit->input = new_form(edit->field);
721 if (edit->input == NULL) {
724 form_opts_off(edit->input, O_NL_OVERLOAD | O_BS_OVERLOAD);
726 edit->input_win = derwin(edit->dia->sub_window, nlines - 3, ncols - 3,
728 if (edit->input_win == NULL) {
732 set_form_win(edit->input, edit->dia->sub_window);
733 set_form_sub(edit->input, edit->input_win);
734 post_form(edit->input);
735 mvwprintw(edit->dia->sub_window, 0, 0, "Name");
736 mvwprintw(edit->dia->sub_window, 3, 0, "Data");
738 keypad(edit->dia->window, true);
742 edit->section = IN_NAME;
745 int c = wgetch(edit->dia->window);
749 } else if (c == KEY_BTAB) {
754 if (edit->section == IN_NAME || edit->section == IN_DATA) {
755 handle_form_input(edit->input, c);
757 selection = handle_menu_input(edit->dia->choices, c);
758 if (selection == DIALOG_OK) {
759 rv = set_value(edit, key, type, vitem == NULL);
760 if (W_ERROR_EQUAL(rv, WERR_FILE_EXISTS)) {
761 dialog_notice(edit, DIA_ALERT,
762 "Value exists", below,
763 "Value name already exists.");
768 } else if (selection == DIALOG_CANCEL) {
784 int dialog_select_type(TALLOC_CTX *ctx, int *type, WINDOW *below)
787 const char *choices[] = {
792 const char *reg_types[] = {
798 #define NTYPES (sizeof(reg_types) / sizeof(const char*))
805 dia = dialog_choice_center_new(ctx, "New Value", choices, 10, 20,
811 mvwprintw(dia->sub_window, 0, 0, "Choose type:");
812 type_win = derwin(dia->sub_window, 6, 18, 1, 0);
813 if (type_win == NULL) {
817 item = talloc_zero_array(dia, ITEM *, NTYPES + 1);
822 for (i = 0; i < NTYPES; ++i) {
823 int t = regtype_by_string(reg_types[i]);
825 item[i] = new_item(reg_types[i], reg_types[i]);
826 if (item[i] == NULL) {
829 set_item_userptr(item[i], (void*)(uintptr_t)t);
832 list = new_menu(item);
837 set_menu_format(list, 7, 1);
838 set_menu_win(list, dia->sub_window);
839 set_menu_sub(list, type_win);
840 menu_opts_off(list, O_SHOWDESC);
841 set_menu_mark(list, "* ");
844 keypad(dia->window, true);
850 int c = wgetch(dia->window);
854 menu_driver(list, REQ_UP_ITEM);
857 menu_driver(list, REQ_DOWN_ITEM);
860 menu_driver(dia->choices, REQ_LEFT_ITEM);
863 menu_driver(dia->choices, REQ_RIGHT_ITEM);
867 it = current_item(list);
868 *type = (int)(uintptr_t)item_userptr(it);
869 it = current_item(dia->choices);
870 sel = (int)(uintptr_t)item_userptr(it);
882 for (it = item; *it; ++it) {