lib/util/fault.c: Unify printing of the stack trace with the INTERNAL ERROR string
[bbaumbach/samba-autobuild/.git] / source3 / utils / regedit.c
1 /*
2  * Samba Unix/Linux SMB client library
3  * Registry Editor
4  * Copyright (C) Christopher Davis 2012
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 #include "includes.h"
21 #include "popt_common_cmdline.h"
22 #include "lib/util/data_blob.h"
23 #include "lib/registry/registry.h"
24 #include "regedit.h"
25 #include "regedit_treeview.h"
26 #include "regedit_valuelist.h"
27 #include "regedit_dialog.h"
28 #include "regedit_list.h"
29 #include <ncurses.h>
30 #include <menu.h>
31 #include <panel.h>
32
33 #define KEY_START_X     0
34 #define KEY_START_Y     1
35 #define KEY_WIDTH       (COLS / 4)
36 #define KEY_HEIGHT      (LINES - KEY_START_Y - 2)
37 #define VAL_START_X     KEY_WIDTH
38 #define VAL_START_Y     1
39 #define VAL_WIDTH       (COLS - KEY_WIDTH)
40 #define VAL_HEIGHT      (LINES - VAL_START_Y - 2)
41
42 #define HELP1_START_Y   (LINES - 2)
43 #define HELP1_START_X   0
44 #define HELP1_WIDTH     (LINES)
45 #define HELP2_START_Y   (LINES - 1)
46 #define HELP2_START_X   0
47 #define HELP2_WIDTH     (LINES)
48 #define PATH_START_Y    0
49 #define PATH_START_X    6
50 #define PATH_MAX_Y      (COLS - 1)
51 #define PATH_WIDTH      (COLS - 6)
52 #define PATH_WIDTH_MAX  1024
53
54 struct regedit {
55         struct registry_context *registry_context;
56         WINDOW *main_window;
57         WINDOW *path_label;
58         size_t path_len;
59         struct value_list *vl;
60         struct tree_view *keys;
61         bool tree_input;
62         struct regedit_search_opts active_search;
63 };
64
65 static struct regedit *regedit_main = NULL;
66
67 static void show_path(struct regedit *regedit)
68 {
69         int start_pad = 0;
70         int start_win = PATH_START_X;
71
72         if (PATH_START_X + regedit->path_len > COLS) {
73                 start_pad = 3 + PATH_START_X + regedit->path_len - COLS;
74                 mvprintw(PATH_START_Y, start_win, "...");
75                 start_win += 3;
76         }
77         copywin(regedit->path_label, regedit->main_window, 0, start_pad,
78                 PATH_START_Y, start_win, PATH_START_Y, PATH_MAX_Y, false);
79
80         mvchgat(0, 0, COLS, A_BOLD, PAIR_YELLOW_CYAN, NULL);
81 }
82
83 static void print_path(struct regedit *regedit, struct tree_node *node)
84 {
85         regedit->path_len = tree_node_print_path(regedit->path_label, node);
86         show_path(regedit);
87 }
88
89 static void print_help(struct regedit *regedit)
90 {
91         const char *khelp = "[n] New Key [s] New Subkey [d] Del Key "
92                             "[LEFT] Ascend [RIGHT] Descend";
93         const char *vhelp = "[n] New Value [d] Del Value [ENTER] Edit "
94                             "[b] Edit binary";
95         const char *msg = "KEYS";
96         const char *help = khelp;
97         const char *genhelp = "[TAB] Switch sections [q] Quit "
98                               "[UP] List up [DOWN] List down "
99                               "[/] Search [x] Next";
100         int i, pad;
101
102         if (!regedit->tree_input) {
103                 msg = "VALUES";
104                 help = vhelp;
105         }
106
107         move(HELP1_START_Y, HELP1_START_X);
108         clrtoeol();
109         attron(COLOR_PAIR(PAIR_BLACK_CYAN));
110         mvaddstr(HELP1_START_Y, HELP1_START_X, help);
111         pad = COLS - strlen(msg) - strlen(help);
112         for (i = 0; i < pad; ++i) {
113                 addch(' ');
114         }
115         attroff(COLOR_PAIR(PAIR_BLACK_CYAN));
116         attron(COLOR_PAIR(PAIR_YELLOW_CYAN) | A_BOLD);
117         addstr(msg);
118         attroff(COLOR_PAIR(PAIR_YELLOW_CYAN) | A_BOLD);
119
120         move(HELP2_START_Y, HELP2_START_X);
121         clrtoeol();
122         mvaddstr(HELP2_START_Y, HELP2_START_X, genhelp);
123 }
124
125 static void print_heading(struct regedit *regedit)
126 {
127         if (regedit->tree_input) {
128                 tree_view_set_selected(regedit->keys, true);
129                 value_list_set_selected(regedit->vl, false);
130         } else {
131                 tree_view_set_selected(regedit->keys, false);
132                 value_list_set_selected(regedit->vl, true);
133         }
134
135         print_help(regedit);
136 }
137
138 static void load_values(struct regedit *regedit)
139 {
140         struct tree_node *node;
141
142         node = tree_view_get_current_node(regedit->keys);
143         value_list_load(regedit->vl, node->key);
144 }
145
146 static void add_reg_key(struct regedit *regedit, struct tree_node *node,
147                         bool subkey)
148 {
149         const char *name;
150         const char *msg;
151
152         if (!subkey && tree_node_is_top_level(node)) {
153                 return;
154         }
155
156         msg = "Enter name of new key";
157         if (subkey) {
158                 msg = "Enter name of new subkey";
159         }
160         dialog_input(regedit, &name, "New Key", "%s", msg);
161         if (name) {
162                 WERROR rv;
163                 struct registry_key *new_key;
164                 struct tree_node *new_node = NULL;
165                 struct tree_node *list;
166                 struct tree_node *parent;
167
168                 if (subkey) {
169                         parent = node;
170                         list = node->child_head;
171                 } else {
172                         parent = node->parent;
173                         list = tree_node_first(node);
174                         SMB_ASSERT(list != NULL);
175                 }
176                 rv = reg_key_add_name(regedit, parent->key, name,
177                                       NULL, NULL, &new_key);
178                 if (W_ERROR_IS_OK(rv)) {
179                         /* The list of subkeys may not be present in
180                            cache yet, so if not, don't bother allocating
181                            a new node for the key. */
182                         if (list) {
183                                 new_node = tree_node_new(parent, parent,
184                                                          name, new_key);
185                                 SMB_ASSERT(new_node);
186                                 tree_node_insert_sorted(list, new_node);
187                         } else {
188                                 /* Reopen the parent key to make sure the
189                                    new subkey will be noticed. */
190                                 tree_node_reopen_key(regedit->registry_context,
191                                                      parent);
192                         }
193
194                         list = tree_node_first(node);
195                         tree_view_clear(regedit->keys);
196                         tree_view_update(regedit->keys, list);
197                         if (!subkey) {
198                                 node = new_node;
199                         }
200                         tree_view_set_current_node(regedit->keys, node);
201                         load_values(regedit);
202                 } else {
203                         msg = get_friendly_werror_msg(rv);
204                         dialog_notice(regedit, DIA_ALERT, "New Key",
205                                       "Failed to create key: %s", msg);
206                 }
207                 talloc_free(discard_const(name));
208         }
209 }
210
211 enum search_flags {
212         SEARCH_NEXT = (1<<0),
213         SEARCH_PREV = (1<<1),
214         SEARCH_REPEAT = (1<<2)
215 };
216 static WERROR regedit_search(struct regedit *regedit, struct tree_node *node,
217                              struct value_item *vitem, unsigned flags)
218 {
219         struct regedit_search_opts *opts;
220         struct tree_node *found;
221         struct value_item *found_value;
222         bool search_key, need_sync;
223         char *save_value_name;
224         WERROR rv;
225         bool (*iterate)(struct tree_node **, bool, WERROR *);
226         struct value_item *(*find_item)(struct value_list *,
227                                         struct value_item *,
228                                         const char *,
229                                         regedit_search_match_fn_t);
230
231         opts = &regedit->active_search;
232
233         if (!opts->query || !opts->match) {
234                 return WERR_OK;
235         }
236
237         SMB_ASSERT(opts->search_key || opts->search_value);
238
239         rv = WERR_OK;
240         found = NULL;
241         found_value = NULL;
242         save_value_name = NULL;
243         search_key = opts->search_key;
244         need_sync = false;
245         iterate = tree_node_next;
246         find_item = value_list_find_next_item;
247
248         if (flags & SEARCH_PREV) {
249                 iterate = tree_node_prev;
250                 find_item = value_list_find_prev_item;
251         }
252
253         if (opts->search_value) {
254                 struct value_item *it;
255
256                 it = value_list_get_current_item(regedit->vl);
257                 if (it) {
258                         save_value_name = talloc_strdup(regedit,
259                                                         it->value_name);
260                         if (save_value_name == NULL) {
261                                 return WERR_NOT_ENOUGH_MEMORY;
262                         }
263                 }
264
265                 if (vitem) {
266                         search_key = false;
267                 }
268         }
269
270         if (!vitem && (flags & SEARCH_REPEAT)) {
271                 if (opts->search_value) {
272                         search_key = false;
273                 } else if (!iterate(&node, opts->search_recursive, &rv)) {
274                         beep();
275                         return rv;
276                 }
277         }
278
279         do {
280                 if (search_key) {
281                         SMB_ASSERT(opts->search_key == true);
282                         if (opts->match(node->name, opts->query)) {
283                                 found = node;
284                         } else if (opts->search_value) {
285                                 search_key = false;
286                         }
287                 }
288                 if (!search_key) {
289                         SMB_ASSERT(opts->search_value == true);
290                         if (!vitem) {
291                                 rv = value_list_load_quick(regedit->vl,
292                                                            node->key);
293                                 if (!W_ERROR_IS_OK(rv)) {
294                                         goto out;
295                                 }
296                                 need_sync = true;
297                         }
298                         found_value = find_item(regedit->vl, vitem, opts->query,
299                                                 opts->match);
300                         if (found_value) {
301                                 found = node;
302                         } else {
303                                 vitem = NULL;
304                                 search_key = opts->search_key;
305                         }
306                 }
307         } while (!found && iterate(&node, opts->search_recursive, &rv));
308
309         if (!W_ERROR_IS_OK(rv)) {
310                 goto out;
311         }
312
313         if (found) {
314                 /* Put the cursor on the node that was found */
315                 if (!tree_view_is_node_visible(regedit->keys, found)) {
316                         tree_view_update(regedit->keys,
317                                          tree_node_first(found));
318                         print_path(regedit, found);
319                 }
320                 tree_view_set_current_node(regedit->keys, found);
321                 if (found_value) {
322                         if (need_sync) {
323                                 value_list_sync(regedit->vl);
324                         }
325                         value_list_set_current_item(regedit->vl, found_value);
326                         regedit->tree_input = false;
327                 } else {
328                         load_values(regedit);
329                         regedit->tree_input = true;
330                 }
331                 tree_view_show(regedit->keys);
332                 value_list_show(regedit->vl);
333                 print_heading(regedit);
334         } else {
335                 if (need_sync) {
336                         load_values(regedit);
337                         value_list_set_current_item_by_name(regedit->vl,
338                                                             save_value_name);
339                 }
340                 beep();
341         }
342
343 out:
344         talloc_free(save_value_name);
345
346         return rv;
347 }
348
349 static void regedit_search_repeat(struct regedit *regedit, unsigned flags)
350 {
351         struct tree_node *node;
352         struct value_item *vitem;
353         struct regedit_search_opts *opts;
354
355         opts = &regedit->active_search;
356         if (opts->query == NULL) {
357                 return;
358         }
359
360         node = tree_view_get_current_node(regedit->keys);
361         vitem = NULL;
362         if (opts->search_value && !regedit->tree_input) {
363                 vitem = value_list_get_current_item(regedit->vl);
364         }
365         regedit_search(regedit, node, vitem, flags | SEARCH_REPEAT);
366 }
367
368 static void handle_tree_input(struct regedit *regedit, int c)
369 {
370         struct tree_node *node;
371
372         switch (c) {
373         case KEY_DOWN:
374                 tree_view_driver(regedit->keys, ML_CURSOR_DOWN);
375                 load_values(regedit);
376                 break;
377         case KEY_UP:
378                 tree_view_driver(regedit->keys, ML_CURSOR_UP);
379                 load_values(regedit);
380                 break;
381         case KEY_NPAGE:
382                 tree_view_driver(regedit->keys, ML_CURSOR_PGDN);
383                 load_values(regedit);
384                 break;
385         case KEY_PPAGE:
386                 tree_view_driver(regedit->keys, ML_CURSOR_PGUP);
387                 load_values(regedit);
388                 break;
389         case KEY_HOME:
390                 tree_view_driver(regedit->keys, ML_CURSOR_HOME);
391                 load_values(regedit);
392                 break;
393         case KEY_END:
394                 tree_view_driver(regedit->keys, ML_CURSOR_END);
395                 load_values(regedit);
396                 break;
397         case '\n':
398         case KEY_ENTER:
399         case KEY_RIGHT:
400                 node = tree_view_get_current_node(regedit->keys);
401                 if (node && tree_node_has_children(node)) {
402                         WERROR rv;
403
404                         rv = tree_node_load_children(node);
405                         if (W_ERROR_IS_OK(rv)) {
406                                 print_path(regedit, node->child_head);
407                                 tree_view_update(regedit->keys, node->child_head);
408                                 value_list_load(regedit->vl, node->child_head->key);
409                         } else {
410                                 const char *msg = get_friendly_werror_msg(rv);
411                                 dialog_notice(regedit, DIA_ALERT, "Loading Subkeys",
412                                               "Failed to load subkeys: %s", msg);
413                         }
414                 }
415                 break;
416         case KEY_LEFT:
417                 node = tree_view_get_current_node(regedit->keys);
418                 if (node && !tree_node_is_top_level(node)) {
419                         print_path(regedit, node->parent);
420                         node = node->parent;
421                         tree_view_update(regedit->keys, tree_node_first(node));
422                         tree_view_set_current_node(regedit->keys, node);
423                         value_list_load(regedit->vl, node->key);
424                 }
425                 break;
426         case 'n':
427         case 'N':
428                 node = tree_view_get_current_node(regedit->keys);
429                 add_reg_key(regedit, node, false);
430                 break;
431         case 's':
432         case 'S':
433                 node = tree_view_get_current_node(regedit->keys);
434                 add_reg_key(regedit, node, true);
435                 break;
436         case 'd':
437         case 'D': {
438                 int sel;
439
440                 node = tree_view_get_current_node(regedit->keys);
441                 if (tree_node_is_top_level(node)) {
442                         break;
443                 }
444                 sel = dialog_notice(regedit, DIA_CONFIRM,
445                                     "Delete Key",
446                                      "Really delete key \"%s\"?",
447                                      node->name);
448                 if (sel == DIALOG_OK) {
449                         WERROR rv;
450                         struct tree_node *pop;
451                         struct tree_node *parent = node->parent;
452
453                         rv = reg_key_del(node, parent->key, node->name);
454                         if (W_ERROR_IS_OK(rv)) {
455                                 tree_node_reopen_key(regedit->registry_context,
456                                                      parent);
457                                 tree_view_clear(regedit->keys);
458                                 pop = tree_node_pop(&node);
459                                 talloc_free(pop);
460                                 node = parent->child_head;
461                                 if (node == NULL) {
462                                         node = tree_node_first(parent);
463                                         print_path(regedit, node);
464                                 }
465                                 tree_view_update(regedit->keys, node);
466                                 value_list_load(regedit->vl, node->key);
467                         } else {
468                                 const char *msg = get_friendly_werror_msg(rv);
469                                 dialog_notice(regedit, DIA_ALERT, "Delete Key",
470                                               "Failed to delete key: %s", msg);
471                         }
472                 }
473                 break;
474         }
475         }
476
477         tree_view_show(regedit->keys);
478         value_list_show(regedit->vl);
479 }
480
481 static void handle_value_input(struct regedit *regedit, int c)
482 {
483         struct value_item *vitem;
484         bool binmode = false;
485         WERROR err;
486         int sel;
487
488         switch (c) {
489         case KEY_DOWN:
490                 value_list_driver(regedit->vl, ML_CURSOR_DOWN);
491                 break;
492         case KEY_UP:
493                 value_list_driver(regedit->vl, ML_CURSOR_UP);
494                 break;
495         case KEY_NPAGE:
496                 value_list_driver(regedit->vl, ML_CURSOR_PGDN);
497                 break;
498         case KEY_PPAGE:
499                 value_list_driver(regedit->vl, ML_CURSOR_PGUP);
500                 break;
501         case KEY_HOME:
502                 value_list_driver(regedit->vl, ML_CURSOR_HOME);
503                 break;
504         case KEY_END:
505                 value_list_driver(regedit->vl, ML_CURSOR_END);
506                 break;
507         case 'b':
508         case 'B':
509                 binmode = true;
510
511                 FALL_THROUGH;
512         case '\n':
513         case KEY_ENTER:
514                 vitem = value_list_get_current_item(regedit->vl);
515                 if (vitem) {
516                         struct tree_node *node;
517                         const char *name = NULL;
518                         node = tree_view_get_current_node(regedit->keys);
519                         sel = dialog_edit_value(regedit, node->key, vitem->type,
520                                                 vitem, binmode, &err, &name);
521                         if (!W_ERROR_IS_OK(err)) {
522                                 const char *msg = get_friendly_werror_msg(err);
523                                 dialog_notice(regedit, DIA_ALERT, "Error",
524                                               "Error editing value:\n%s", msg);
525                         } else if (sel == DIALOG_OK) {
526                                 tree_node_reopen_key(regedit->registry_context,
527                                                      node);
528                                 value_list_load(regedit->vl, node->key);
529                                 value_list_set_current_item_by_name(regedit->vl,
530                                                                     name);
531                                 talloc_free(discard_const(name));
532                         }
533                 }
534                 break;
535         case 'n':
536         case 'N': {
537                 int new_type;
538
539                 sel = dialog_select_type(regedit, &new_type);
540                 if (sel == DIALOG_OK) {
541                         struct tree_node *node;
542                         const char *name = NULL;
543                         node = tree_view_get_current_node(regedit->keys);
544                         sel = dialog_edit_value(regedit, node->key, new_type,
545                                                 NULL, false, &err, &name);
546                         if (!W_ERROR_IS_OK(err)) {
547                                 const char *msg = get_friendly_werror_msg(err);
548                                 dialog_notice(regedit, DIA_ALERT, "Error",
549                                               "Error creating value:\n%s", msg);
550                         } else if (sel == DIALOG_OK) {
551                                 tree_node_reopen_key(regedit->registry_context,
552                                                      node);
553                                 value_list_load(regedit->vl, node->key);
554                                 value_list_set_current_item_by_name(regedit->vl,
555                                                                     name);
556                                 talloc_free(discard_const(name));
557                         }
558                 }
559                 break;
560         }
561         case 'd':
562         case 'D':
563                 vitem = value_list_get_current_item(regedit->vl);
564                 if (vitem) {
565                         sel = dialog_notice(regedit, DIA_CONFIRM,
566                                             "Delete Value",
567                                              "Really delete value \"%s\"?",
568                                              vitem->value_name);
569                         if (sel == DIALOG_OK) {
570                                 struct tree_node *node;
571                                 node = tree_view_get_current_node(regedit->keys);
572                                 reg_del_value(regedit, node->key,
573                                               vitem->value_name);
574                                 tree_node_reopen_key(regedit->registry_context,
575                                                      node);
576                                 value_list_load(regedit->vl, node->key);
577                         }
578                 }
579                 break;
580         }
581
582         value_list_show(regedit->vl);
583 }
584
585 static bool find_substring(const char *haystack, const char *needle)
586 {
587         return strstr(haystack, needle) != NULL;
588 }
589
590 static bool find_substring_nocase(const char *haystack, const char *needle)
591 {
592         return strcasestr(haystack, needle) != NULL;
593 }
594
595 static void handle_main_input(struct regedit *regedit, int c)
596 {
597         switch (c) {
598         case 18: { /* CTRL-R */
599                 struct tree_node *root, *node;
600                 const char **path;
601
602                 node = tree_view_get_current_node(regedit->keys);
603                 path = tree_node_get_path(regedit, node);
604                 SMB_ASSERT(path != NULL);
605
606                 root = tree_node_new_root(regedit, regedit->registry_context);
607                 SMB_ASSERT(root != NULL);
608
609                 tree_view_set_root(regedit->keys, root);
610                 tree_view_set_path(regedit->keys, path);
611                 node = tree_view_get_current_node(regedit->keys);
612                 value_list_load(regedit->vl, node->key);
613                 tree_view_show(regedit->keys);
614                 value_list_show(regedit->vl);
615                 print_path(regedit, node);
616                 talloc_free(discard_const(path));
617                 break;
618         }
619         case 'f':
620         case 'F':
621         case '/': {
622                 int rv;
623                 struct regedit_search_opts *opts;
624                 struct tree_node *node;
625
626                 opts = &regedit->active_search;
627                 rv = dialog_search_input(regedit, opts);
628                 if (rv == DIALOG_OK) {
629                         SMB_ASSERT(opts->query != NULL);
630                         opts->match = find_substring_nocase;
631                         node = regedit->keys->root->child_head;
632                         if (opts->search_case) {
633                                 opts->match = find_substring;
634                         }
635                         if (!opts->search_recursive) {
636                                 node = tree_view_get_current_node(regedit->keys);
637                                 node = tree_node_first(node);
638                         }
639                         regedit_search(regedit, node, NULL, SEARCH_NEXT);
640                 }
641                 break;
642         }
643         case 'x':
644                 regedit_search_repeat(regedit, SEARCH_NEXT);
645                 break;
646         case 'X':
647                 regedit_search_repeat(regedit, SEARCH_PREV);
648                 break;
649         case '\t':
650                 regedit->tree_input = !regedit->tree_input;
651                 print_heading(regedit);
652                 break;
653         default:
654                 if (regedit->tree_input) {
655                         handle_tree_input(regedit, c);
656                 } else {
657                         handle_value_input(regedit, c);
658                 }
659         }
660 }
661
662 int regedit_getch(void)
663 {
664         int c;
665
666         SMB_ASSERT(regedit_main);
667
668         c = getch();
669         if (c == KEY_RESIZE) {
670                 tree_view_resize(regedit_main->keys, KEY_HEIGHT, KEY_WIDTH,
671                                  KEY_START_Y, KEY_START_X);
672                 value_list_resize(regedit_main->vl, VAL_HEIGHT, VAL_WIDTH,
673                                   VAL_START_Y, VAL_START_X);
674                 print_heading(regedit_main);
675                 show_path(regedit_main);
676         }
677
678         return c;
679 }
680
681 static void regedit_panic_handler(const char *msg)
682 {
683         endwin();
684         smb_panic_log(msg);
685         smb_panic_s3(msg);
686 }
687
688 static void display_window(TALLOC_CTX *mem_ctx, struct registry_context *ctx)
689 {
690         struct regedit *regedit;
691         struct tree_node *root;
692         bool colors;
693         int key;
694
695         initscr();
696
697         cbreak();
698         noecho();
699
700         fault_configure(regedit_panic_handler);
701
702         colors = has_colors();
703         if (colors) {
704                 start_color();
705                 use_default_colors();
706                 assume_default_colors(COLOR_WHITE, COLOR_BLUE);
707                 init_pair(PAIR_YELLOW_CYAN, COLOR_YELLOW, COLOR_CYAN);
708                 init_pair(PAIR_BLACK_CYAN, COLOR_BLACK, COLOR_CYAN);
709                 init_pair(PAIR_YELLOW_BLUE, COLOR_YELLOW, COLOR_BLUE);
710         }
711
712         regedit = talloc_zero(mem_ctx, struct regedit);
713         SMB_ASSERT(regedit != NULL);
714         regedit_main = regedit;
715
716         regedit->registry_context = ctx;
717         regedit->main_window = stdscr;
718         keypad(regedit->main_window, TRUE);
719
720         mvwprintw(regedit->main_window, 0, 0, "Path: ");
721         regedit->path_label = newpad(1, PATH_WIDTH_MAX);
722         SMB_ASSERT(regedit->path_label);
723         wprintw(regedit->path_label, "/");
724         show_path(regedit_main);
725
726         root = tree_node_new_root(regedit, ctx);
727         SMB_ASSERT(root != NULL);
728
729         regedit->keys = tree_view_new(regedit, root, KEY_HEIGHT, KEY_WIDTH,
730                                       KEY_START_Y, KEY_START_X);
731         SMB_ASSERT(regedit->keys != NULL);
732
733         regedit->vl = value_list_new(regedit, VAL_HEIGHT, VAL_WIDTH,
734                                      VAL_START_Y, VAL_START_X);
735         SMB_ASSERT(regedit->vl != NULL);
736
737         regedit->tree_input = true;
738         print_heading(regedit);
739
740         tree_view_show(regedit->keys);
741         load_values(regedit);
742         value_list_show(regedit->vl);
743
744         update_panels();
745         doupdate();
746
747         do {
748                 key = regedit_getch();
749
750                 handle_main_input(regedit, key);
751                 update_panels();
752                 doupdate();
753         } while (key != 'q' && key != 'Q');
754
755         endwin();
756 }
757
758 int main(int argc, const char **argv)
759 {
760         struct poptOption long_options[] = {
761                 POPT_AUTOHELP
762                 /* ... */
763                 POPT_COMMON_SAMBA
764                 POPT_COMMON_CONNECTION
765                 POPT_COMMON_CREDENTIALS
766                 POPT_TABLEEND
767         };
768         int opt;
769         poptContext pc;
770         TALLOC_CTX *frame;
771         struct registry_context *ctx;
772         WERROR rv;
773
774         frame = talloc_stackframe();
775
776         setup_logging("regedit", DEBUG_DEFAULT_STDERR);
777         lp_set_cmdline("log level", "0");
778
779         /* process options */
780         pc = poptGetContext("regedit", argc, argv, long_options, 0);
781
782         while ((opt = poptGetNextOpt(pc)) != -1) {
783                 /* TODO */
784         }
785
786         rv = reg_open_samba3(frame, &ctx);
787         if (!W_ERROR_IS_OK(rv)) {
788                 fprintf(stderr, "Unable to open registry: %s\n",
789                         win_errstr(rv));
790                 TALLOC_FREE(frame);
791                 poptFreeContext(pc);
792
793                 return 1;
794         }
795
796         display_window(frame, ctx);
797
798         TALLOC_FREE(frame);
799         poptFreeContext(pc);
800
801         return 0;
802 }