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 "popt_common.h"
22 #include "lib/util/data_blob.h"
23 #include "lib/registry/registry.h"
25 #include "regedit_treeview.h"
26 #include "regedit_valuelist.h"
27 #include "regedit_dialog.h"
34 #define KEY_WIDTH (COLS / 4)
35 #define KEY_HEIGHT (LINES - KEY_START_Y - 2)
36 #define VAL_START_X KEY_WIDTH
38 #define VAL_WIDTH (COLS - KEY_WIDTH)
39 #define VAL_HEIGHT (LINES - VAL_START_Y - 2)
40 #define HEADING_START_Y (KEY_START_Y - 1)
41 #define HELP1_START_Y (LINES - 2)
42 #define HELP1_START_X 0
43 #define HELP1_WIDTH (LINES)
44 #define HELP2_START_Y (LINES - 1)
45 #define HELP2_START_X 0
46 #define HELP2_WIDTH (LINES)
47 #define PATH_START_Y 0
48 #define PATH_START_X 6
49 #define PATH_MAX_Y (COLS - 1)
50 #define PATH_WIDTH (COLS - 6)
51 #define PATH_WIDTH_MAX 1024
57 struct value_list
*vl
;
58 struct tree_view
*keys
;
62 static struct regedit
*regedit_main
= NULL
;
64 static void show_path(struct regedit
*regedit
)
67 int start_win
= PATH_START_X
;
69 if (PATH_START_X
+ regedit
->path_len
> COLS
) {
70 start_pad
= 3 + PATH_START_X
+ regedit
->path_len
- COLS
;
71 mvprintw(PATH_START_Y
, start_win
, "...");
74 copywin(regedit
->path_label
, regedit
->main_window
, 0, start_pad
,
75 PATH_START_Y
, start_win
, PATH_START_Y
, PATH_MAX_Y
, false);
78 static void print_path(struct regedit
*regedit
, struct tree_node
*node
)
80 regedit
->path_len
= tree_node_print_path(regedit
->path_label
, node
);
84 /* load all available hives */
85 static struct tree_node
*load_hives(TALLOC_CTX
*mem_ctx
,
86 struct registry_context
*ctx
)
88 const char *hives
[] = {
92 "HKEY_PERFORMANCE_DATA",
94 "HKEY_CURRENT_CONFIG",
96 "HKEY_PERFORMANCE_TEXT",
97 "HKEY_PERFORMANCE_NLSTEXT",
100 struct tree_node
*root
, *prev
, *node
;
101 struct registry_key
*key
;
108 for (i
= 0; hives
[i
] != NULL
; ++i
) {
109 rv
= reg_get_predefined_key_by_name(ctx
, hives
[i
], &key
);
110 if (!W_ERROR_IS_OK(rv
)) {
114 node
= tree_node_new(mem_ctx
, NULL
, hives
[i
], key
);
123 tree_node_append(prev
, node
);
131 static void print_help(struct regedit
*regedit
)
133 const char *khelp
= "[n] New Key [s] New Subkey [d] Del Key "
134 "[LEFT] Ascend [RIGHT] Descend";
135 const char *vhelp
= "[n] New Value [d] Del Value [ENTER] Edit "
137 const char *msg
= "KEYS";
138 const char *help
= khelp
;
139 const char *genhelp
= "[TAB] Switch sections [q] Quit regedit "
140 "[UP] List up [DOWN] List down";
143 if (!regedit
->tree_input
) {
148 move(HELP1_START_Y
, HELP1_START_X
);
151 mvaddstr(HELP1_START_Y
, HELP1_START_X
, help
);
152 pad
= COLS
- strlen(msg
) - strlen(help
);
153 for (i
= 0; i
< pad
; ++i
) {
158 attroff(A_REVERSE
| A_BOLD
);
160 move(HELP2_START_Y
, HELP2_START_X
);
162 mvaddstr(HELP2_START_Y
, HELP2_START_X
, genhelp
);
165 static void print_heading(struct regedit
*regedit
)
167 move(HEADING_START_Y
, 0);
170 if (regedit
->tree_input
) {
175 mvprintw(HEADING_START_Y
, KEY_START_X
, "Key");
178 if (!regedit
->tree_input
) {
183 mvprintw(HEADING_START_Y
, VAL_START_X
, "Value");
189 static void load_values(struct regedit
*regedit
)
191 struct tree_node
*node
;
193 node
= item_userptr(current_item(regedit
->keys
->menu
));
194 value_list_load(regedit
->vl
, node
->key
);
197 static void add_reg_key(struct regedit
*regedit
, struct tree_node
*node
,
203 if (!subkey
&& !node
->parent
) {
207 msg
= "Enter name of new key";
209 msg
= "Enter name of new subkey";
211 dialog_input(regedit
, &name
, "New Key", msg
);
214 struct registry_key
*new_key
;
215 struct tree_node
*new_node
;
216 struct tree_node
*list
;
217 struct tree_node
*parent
;
221 list
= node
->child_head
;
223 parent
= node
->parent
;
224 list
= tree_node_first(node
);
225 SMB_ASSERT(list
!= NULL
);
227 rv
= reg_key_add_name(regedit
, parent
->key
, name
,
228 NULL
, NULL
, &new_key
);
229 if (W_ERROR_IS_OK(rv
)) {
230 /* The list of subkeys may not be present in
231 cache yet, so if not, don't bother allocating
232 a new node for the key. */
234 new_node
= tree_node_new(parent
, parent
,
236 SMB_ASSERT(new_node
);
237 tree_node_append_last(list
, new_node
);
240 list
= tree_node_first(node
);
241 tree_view_clear(regedit
->keys
);
242 tree_view_update(regedit
->keys
, list
);
244 dialog_notice(regedit
, DIA_ALERT
, "New Key",
245 "Failed to create key.");
251 static void handle_tree_input(struct regedit
*regedit
, int c
)
253 struct tree_node
*node
;
257 menu_driver(regedit
->keys
->menu
, REQ_DOWN_ITEM
);
258 load_values(regedit
);
261 menu_driver(regedit
->keys
->menu
, REQ_UP_ITEM
);
262 load_values(regedit
);
267 node
= item_userptr(current_item(regedit
->keys
->menu
));
268 if (node
&& tree_node_has_children(node
)) {
269 tree_node_load_children(node
);
270 print_path(regedit
, node
->child_head
);
271 tree_view_update(regedit
->keys
, node
->child_head
);
272 value_list_load(regedit
->vl
, node
->child_head
->key
);
276 node
= item_userptr(current_item(regedit
->keys
->menu
));
277 if (node
&& node
->parent
) {
278 print_path(regedit
, node
->parent
);
279 node
= tree_node_first(node
->parent
);
280 tree_view_update(regedit
->keys
, node
);
281 value_list_load(regedit
->vl
, node
->key
);
286 node
= item_userptr(current_item(regedit
->keys
->menu
));
287 add_reg_key(regedit
, node
, false);
291 node
= item_userptr(current_item(regedit
->keys
->menu
));
292 add_reg_key(regedit
, node
, true);
298 node
= item_userptr(current_item(regedit
->keys
->menu
));
302 sel
= dialog_notice(regedit
, DIA_CONFIRM
,
304 "Really delete key \"%s\"?",
306 if (sel
== DIALOG_OK
) {
308 struct tree_node
*pop
;
309 struct tree_node
*parent
= node
->parent
;
311 rv
= reg_key_del(node
, parent
->key
, node
->name
);
312 if (W_ERROR_IS_OK(rv
)) {
313 tree_view_clear(regedit
->keys
);
314 pop
= tree_node_pop(&node
);
315 tree_node_free_recursive(pop
);
316 node
= parent
->child_head
;
318 node
= tree_node_first(parent
);
319 print_path(regedit
, node
);
321 tree_view_update(regedit
->keys
, node
);
322 value_list_load(regedit
->vl
, node
->key
);
324 dialog_notice(regedit
, DIA_ALERT
, "Delete Key",
325 "Failed to delete key.");
332 tree_view_show(regedit
->keys
);
333 value_list_show(regedit
->vl
);
336 static void handle_value_input(struct regedit
*regedit
, int c
)
338 struct value_item
*vitem
;
339 bool binmode
= false;
343 menu_driver(regedit
->vl
->menu
, REQ_DOWN_ITEM
);
346 menu_driver(regedit
->vl
->menu
, REQ_UP_ITEM
);
354 vitem
= item_userptr(current_item(regedit
->vl
->menu
));
356 struct tree_node
*node
;
357 node
= item_userptr(current_item(regedit
->keys
->menu
));
358 dialog_edit_value(regedit
, node
->key
, vitem
->type
,
360 value_list_load(regedit
->vl
, node
->key
);
368 sel
= dialog_select_type(regedit
, &new_type
);
369 if (sel
== DIALOG_OK
) {
370 struct tree_node
*node
;
371 node
= item_userptr(current_item(regedit
->keys
->menu
));
372 dialog_edit_value(regedit
, node
->key
, new_type
, NULL
,
374 value_list_load(regedit
->vl
, node
->key
);
380 vitem
= item_userptr(current_item(regedit
->vl
->menu
));
384 sel
= dialog_notice(regedit
, DIA_CONFIRM
,
386 "Really delete value \"%s\"?",
388 if (sel
== DIALOG_OK
) {
389 ITEM
*it
= current_item(regedit
->keys
->menu
);
390 struct tree_node
*node
= item_userptr(it
);
391 reg_del_value(regedit
, node
->key
,
393 value_list_load(regedit
->vl
, node
->key
);
399 value_list_show(regedit
->vl
);
402 static void handle_main_input(struct regedit
*regedit
, int c
)
406 regedit
->tree_input
= !regedit
->tree_input
;
407 print_heading(regedit
);
410 if (regedit
->tree_input
) {
411 handle_tree_input(regedit
, c
);
413 handle_value_input(regedit
, c
);
418 int regedit_getch(void)
422 SMB_ASSERT(regedit_main
);
425 if (c
== KEY_RESIZE
) {
426 tree_view_resize(regedit_main
->keys
, KEY_HEIGHT
, KEY_WIDTH
,
427 KEY_START_Y
, KEY_START_X
);
428 value_list_resize(regedit_main
->vl
, VAL_HEIGHT
, VAL_WIDTH
,
429 VAL_START_Y
, VAL_START_X
);
430 print_heading(regedit_main
);
431 show_path(regedit_main
);
437 static void display_window(TALLOC_CTX
*mem_ctx
, struct registry_context
*ctx
)
439 struct regedit
*regedit
;
440 struct tree_node
*root
;
448 regedit
= talloc_zero(mem_ctx
, struct regedit
);
449 SMB_ASSERT(regedit
!= NULL
);
450 regedit_main
= regedit
;
452 regedit
->main_window
= stdscr
;
453 keypad(regedit
->main_window
, TRUE
);
455 mvwprintw(regedit
->main_window
, 0, 0, "Path: ");
456 regedit
->path_label
= newpad(1, PATH_WIDTH_MAX
);
457 SMB_ASSERT(regedit
->path_label
);
458 wprintw(regedit
->path_label
, "/");
459 show_path(regedit_main
);
461 root
= load_hives(regedit
, ctx
);
462 SMB_ASSERT(root
!= NULL
);
464 regedit
->keys
= tree_view_new(regedit
, root
, KEY_HEIGHT
, KEY_WIDTH
,
465 KEY_START_Y
, KEY_START_X
);
466 SMB_ASSERT(regedit
->keys
!= NULL
);
468 regedit
->vl
= value_list_new(regedit
, VAL_HEIGHT
, VAL_WIDTH
,
469 VAL_START_Y
, VAL_START_X
);
470 SMB_ASSERT(regedit
->vl
!= NULL
);
472 regedit
->tree_input
= true;
473 print_heading(regedit
);
475 tree_view_show(regedit
->keys
);
476 menu_driver(regedit
->keys
->menu
, REQ_FIRST_ITEM
);
477 load_values(regedit
);
478 value_list_show(regedit
->vl
);
484 if (c
== 'q' || c
== 'Q') {
487 handle_main_input(regedit
, c
);
495 int main(int argc
, char **argv
)
497 struct poptOption long_options
[] = {
501 POPT_COMMON_CONNECTION
502 POPT_COMMON_CREDENTIALS
507 struct user_auth_info
*auth_info
;
509 struct registry_context
*ctx
;
512 talloc_enable_leak_report_full();
514 frame
= talloc_stackframe();
516 setup_logging("regedit", DEBUG_DEFAULT_STDERR
);
517 lp_set_cmdline("log level", "0");
519 /* process options */
520 auth_info
= user_auth_info_init(frame
);
521 if (auth_info
== NULL
) {
524 popt_common_set_auth_info(auth_info
);
525 pc
= poptGetContext("regedit", argc
, (const char **)argv
, long_options
, 0);
527 while ((opt
= poptGetNextOpt(pc
)) != -1) {
531 if (!lp_load_global(get_dyn_CONFIGFILE())) {
532 DEBUG(0, ("ERROR loading config file...\n"));
536 /* some simple tests */
538 rv
= reg_open_samba3(frame
, &ctx
);
539 if (!W_ERROR_IS_OK(rv
)) {
545 display_window(frame
, ctx
);
547 //talloc_report_full(frame, stdout);