regedit: add search feature.
[Samba.git] / source3 / utils / regedit.c
blob77c254bfd0af2191b99bc232e60450076088b809
1 /*
2 * Samba Unix/Linux SMB client library
3 * Registry Editor
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/>.
20 #include "includes.h"
21 #include "popt_common.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 <ncurses.h>
29 #include <menu.h>
30 #include <panel.h>
32 #define KEY_START_X 0
33 #define KEY_START_Y 1
34 #define KEY_WIDTH (COLS / 4)
35 #define KEY_HEIGHT (LINES - KEY_START_Y - 2)
36 #define VAL_START_X KEY_WIDTH
37 #define VAL_START_Y 1
38 #define VAL_WIDTH (COLS - KEY_WIDTH)
39 #define VAL_HEIGHT (LINES - VAL_START_Y - 2)
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
53 #define PAIR_YELLOW_CYAN 1
54 #define PAIR_BLACK_CYAN 2
56 struct regedit {
57 WINDOW *main_window;
58 WINDOW *path_label;
59 size_t path_len;
60 struct value_list *vl;
61 struct tree_view *keys;
62 bool tree_input;
63 struct regedit_search_opts active_search;
66 static struct regedit *regedit_main = NULL;
68 static void show_path(struct regedit *regedit)
70 int start_pad = 0;
71 int start_win = PATH_START_X;
73 if (PATH_START_X + regedit->path_len > COLS) {
74 start_pad = 3 + PATH_START_X + regedit->path_len - COLS;
75 mvprintw(PATH_START_Y, start_win, "...");
76 start_win += 3;
78 copywin(regedit->path_label, regedit->main_window, 0, start_pad,
79 PATH_START_Y, start_win, PATH_START_Y, PATH_MAX_Y, false);
81 mvchgat(0, 0, COLS, A_BOLD, PAIR_YELLOW_CYAN, NULL);
84 static void print_path(struct regedit *regedit, struct tree_node *node)
86 regedit->path_len = tree_node_print_path(regedit->path_label, node);
87 show_path(regedit);
90 /* load all available hives */
91 static struct tree_node *load_hives(TALLOC_CTX *mem_ctx,
92 struct registry_context *ctx)
94 const char *hives[] = {
95 "HKEY_CLASSES_ROOT",
96 "HKEY_CURRENT_USER",
97 "HKEY_LOCAL_MACHINE",
98 "HKEY_PERFORMANCE_DATA",
99 "HKEY_USERS",
100 "HKEY_CURRENT_CONFIG",
101 "HKEY_DYN_DATA",
102 "HKEY_PERFORMANCE_TEXT",
103 "HKEY_PERFORMANCE_NLSTEXT",
104 NULL
106 struct tree_node *root, *prev, *node;
107 struct registry_key *key;
108 WERROR rv;
109 size_t i;
111 root = NULL;
112 prev = NULL;
114 for (i = 0; hives[i] != NULL; ++i) {
115 rv = reg_get_predefined_key_by_name(ctx, hives[i], &key);
116 if (!W_ERROR_IS_OK(rv)) {
117 continue;
120 node = tree_node_new(mem_ctx, NULL, hives[i], key);
121 if (node == NULL) {
122 return NULL;
125 if (root == NULL) {
126 root = node;
128 if (prev) {
129 tree_node_append(prev, node);
131 prev = node;
134 return root;
137 static void print_help(struct regedit *regedit)
139 const char *khelp = "[n] New Key [s] New Subkey [d] Del Key "
140 "[LEFT] Ascend [RIGHT] Descend";
141 const char *vhelp = "[n] New Value [d] Del Value [ENTER] Edit "
142 "[b] Edit binary";
143 const char *msg = "KEYS";
144 const char *help = khelp;
145 const char *genhelp = "[TAB] Switch sections [q] Quit "
146 "[UP] List up [DOWN] List down "
147 "[/] Search [x] Next";
148 int i, pad;
150 if (!regedit->tree_input) {
151 msg = "VALUES";
152 help = vhelp;
155 move(HELP1_START_Y, HELP1_START_X);
156 clrtoeol();
157 attron(COLOR_PAIR(PAIR_BLACK_CYAN));
158 mvaddstr(HELP1_START_Y, HELP1_START_X, help);
159 pad = COLS - strlen(msg) - strlen(help);
160 for (i = 0; i < pad; ++i) {
161 addch(' ');
163 attroff(COLOR_PAIR(PAIR_BLACK_CYAN));
164 attron(COLOR_PAIR(PAIR_YELLOW_CYAN) | A_BOLD);
165 addstr(msg);
166 attroff(COLOR_PAIR(PAIR_YELLOW_CYAN) | A_BOLD);
168 move(HELP2_START_Y, HELP2_START_X);
169 clrtoeol();
170 mvaddstr(HELP2_START_Y, HELP2_START_X, genhelp);
173 static void print_heading(struct regedit *regedit)
175 if (regedit->tree_input) {
176 tree_view_set_selected(regedit->keys, true);
177 value_list_set_selected(regedit->vl, false);
178 } else {
179 tree_view_set_selected(regedit->keys, false);
180 value_list_set_selected(regedit->vl, true);
183 print_help(regedit);
186 static void load_values(struct regedit *regedit)
188 struct tree_node *node;
190 node = tree_view_get_current_node(regedit->keys);
191 value_list_load(regedit->vl, node->key);
194 static void add_reg_key(struct regedit *regedit, struct tree_node *node,
195 bool subkey)
197 const char *name;
198 const char *msg;
200 if (!subkey && !node->parent) {
201 return;
204 msg = "Enter name of new key";
205 if (subkey) {
206 msg = "Enter name of new subkey";
208 dialog_input(regedit, &name, "New Key", msg);
209 if (name) {
210 WERROR rv;
211 struct registry_key *new_key;
212 struct tree_node *new_node;
213 struct tree_node *list;
214 struct tree_node *parent;
216 if (subkey) {
217 parent = node;
218 list = node->child_head;
219 } else {
220 parent = node->parent;
221 list = tree_node_first(node);
222 SMB_ASSERT(list != NULL);
224 rv = reg_key_add_name(regedit, parent->key, name,
225 NULL, NULL, &new_key);
226 if (W_ERROR_IS_OK(rv)) {
227 /* The list of subkeys may not be present in
228 cache yet, so if not, don't bother allocating
229 a new node for the key. */
230 if (list) {
231 new_node = tree_node_new(parent, parent,
232 name, new_key);
233 SMB_ASSERT(new_node);
234 tree_node_insert_sorted(list, new_node);
237 list = tree_node_first(node);
238 tree_view_clear(regedit->keys);
239 tree_view_update(regedit->keys, list);
240 } else {
241 dialog_notice(regedit, DIA_ALERT, "New Key",
242 "Failed to create key.");
244 talloc_free(discard_const(name));
248 static WERROR next_depth_first(struct tree_node **node)
250 WERROR rv = WERR_OK;
252 SMB_ASSERT(node != NULL && *node != NULL);
254 if (tree_node_has_children(*node)) {
255 /* 1. If the node has children, go to the first one. */
256 rv = tree_node_load_children(*node);
257 if (W_ERROR_IS_OK(rv)) {
258 SMB_ASSERT((*node)->child_head != NULL);
259 *node = (*node)->child_head;
261 } else if ((*node)->next) {
262 /* 2. If there's a node directly after this one, go there */
263 *node = (*node)->next;
264 } else {
265 /* 3. Otherwise, go up the hierarchy to find the next one */
266 do {
267 *node = (*node)->parent;
268 if (*node && (*node)->next) {
269 *node = (*node)->next;
270 break;
272 } while (*node);
275 return rv;
278 static WERROR regedit_search_next(struct regedit *regedit)
280 WERROR rv;
281 struct regedit_search_opts *opts = &regedit->active_search;
283 if (opts->search_recursive) {
284 rv = next_depth_first(&opts->node);
285 if (!W_ERROR_IS_OK(rv)) {
286 return rv;
288 } else {
289 opts->node = opts->node->next;
292 return WERR_OK;
295 static WERROR regedit_search(struct regedit *regedit)
297 struct regedit_search_opts *opts;
298 struct tree_node *found;
299 WERROR rv;
301 opts = &regedit->active_search;
303 if (!opts->query || !opts->match) {
304 return WERR_OK;
307 SMB_ASSERT(opts->search_key || opts->search_value);
309 for (found = NULL; opts->node && !found; ) {
310 if (opts->search_key &&
311 opts->match(opts->node->name, opts->query)) {
312 found = opts->node;
314 if (opts->search_value) {
315 /* TODO
316 rv = regedit_search_value(regedit);
317 if (W_ERROR_IS_OK(rv)) {
318 found = opts->node;
319 } else if (!W_ERROR_EQUAL(rv, WERR_NO_MORE_ITEMS)) {
320 return rv;
324 rv = regedit_search_next(regedit);
325 if (!W_ERROR_IS_OK(rv)) {
326 return rv;
330 if (found) {
331 /* Put the cursor on the node that was found */
332 if (!tree_view_is_node_visible(regedit->keys, found)) {
333 tree_view_update(regedit->keys,
334 tree_node_first(found));
335 print_path(regedit, found);
337 tree_view_set_current_node(regedit->keys, found);
338 load_values(regedit);
339 tree_view_show(regedit->keys);
340 value_list_show(regedit->vl);
341 } else {
342 beep();
345 return WERR_OK;
348 static void handle_tree_input(struct regedit *regedit, int c)
350 struct tree_node *node;
352 switch (c) {
353 case KEY_DOWN:
354 menu_driver(regedit->keys->menu, REQ_DOWN_ITEM);
355 load_values(regedit);
356 break;
357 case KEY_UP:
358 menu_driver(regedit->keys->menu, REQ_UP_ITEM);
359 load_values(regedit);
360 break;
361 case '\n':
362 case KEY_ENTER:
363 case KEY_RIGHT:
364 node = item_userptr(current_item(regedit->keys->menu));
365 if (node && tree_node_has_children(node)) {
366 tree_node_load_children(node);
367 print_path(regedit, node->child_head);
368 tree_view_update(regedit->keys, node->child_head);
369 value_list_load(regedit->vl, node->child_head->key);
371 break;
372 case KEY_LEFT:
373 node = item_userptr(current_item(regedit->keys->menu));
374 if (node && node->parent) {
375 print_path(regedit, node->parent);
376 node = tree_node_first(node->parent);
377 tree_view_update(regedit->keys, node);
378 value_list_load(regedit->vl, node->key);
380 break;
381 case 'n':
382 case 'N':
383 node = item_userptr(current_item(regedit->keys->menu));
384 add_reg_key(regedit, node, false);
385 break;
386 case 's':
387 case 'S':
388 node = item_userptr(current_item(regedit->keys->menu));
389 add_reg_key(regedit, node, true);
390 break;
391 case 'd':
392 case 'D': {
393 int sel;
395 node = item_userptr(current_item(regedit->keys->menu));
396 if (!node->parent) {
397 break;
399 sel = dialog_notice(regedit, DIA_CONFIRM,
400 "Delete Key",
401 "Really delete key \"%s\"?",
402 node->name);
403 if (sel == DIALOG_OK) {
404 WERROR rv;
405 struct tree_node *pop;
406 struct tree_node *parent = node->parent;
408 rv = reg_key_del(node, parent->key, node->name);
409 if (W_ERROR_IS_OK(rv)) {
410 tree_view_clear(regedit->keys);
411 pop = tree_node_pop(&node);
412 tree_node_free_recursive(pop);
413 node = parent->child_head;
414 if (node == NULL) {
415 node = tree_node_first(parent);
416 print_path(regedit, node);
418 tree_view_update(regedit->keys, node);
419 value_list_load(regedit->vl, node->key);
420 } else {
421 dialog_notice(regedit, DIA_ALERT, "Delete Key",
422 "Failed to delete key.");
425 break;
429 tree_view_show(regedit->keys);
430 value_list_show(regedit->vl);
433 static void handle_value_input(struct regedit *regedit, int c)
435 struct value_item *vitem;
436 bool binmode = false;
438 switch (c) {
439 case KEY_DOWN:
440 menu_driver(regedit->vl->menu, REQ_DOWN_ITEM);
441 break;
442 case KEY_UP:
443 menu_driver(regedit->vl->menu, REQ_UP_ITEM);
444 break;
445 case 'b':
446 case 'B':
447 binmode = true;
448 /* Falthrough... */
449 case '\n':
450 case KEY_ENTER:
451 vitem = item_userptr(current_item(regedit->vl->menu));
452 if (vitem) {
453 struct tree_node *node;
454 node = item_userptr(current_item(regedit->keys->menu));
455 dialog_edit_value(regedit, node->key, vitem->type,
456 vitem, binmode);
457 value_list_load(regedit->vl, node->key);
459 break;
460 case 'n':
461 case 'N': {
462 int new_type;
463 int sel;
465 sel = dialog_select_type(regedit, &new_type);
466 if (sel == DIALOG_OK) {
467 struct tree_node *node;
468 node = item_userptr(current_item(regedit->keys->menu));
469 dialog_edit_value(regedit, node->key, new_type, NULL,
470 false);
471 value_list_load(regedit->vl, node->key);
473 break;
475 case 'd':
476 case 'D':
477 vitem = item_userptr(current_item(regedit->vl->menu));
478 if (vitem) {
479 int sel;
481 sel = dialog_notice(regedit, DIA_CONFIRM,
482 "Delete Value",
483 "Really delete value \"%s\"?",
484 vitem->value_name);
485 if (sel == DIALOG_OK) {
486 ITEM *it = current_item(regedit->keys->menu);
487 struct tree_node *node = item_userptr(it);
488 reg_del_value(regedit, node->key,
489 vitem->value_name);
490 value_list_load(regedit->vl, node->key);
493 break;
496 value_list_show(regedit->vl);
499 static bool find_substring(const char *haystack, const char *needle)
501 return strstr(haystack, needle) != NULL;
504 static bool find_substring_nocase(const char *haystack, const char *needle)
506 return strcasestr(haystack, needle) != NULL;
509 static void handle_main_input(struct regedit *regedit, int c)
511 switch (c) {
512 case 'f':
513 case 'F':
514 case '/': {
515 int rv;
516 struct regedit_search_opts *opts;
518 opts = &regedit->active_search;
519 if (opts->query) {
520 talloc_free(discard_const(opts->query));
522 rv = dialog_search_input(regedit, opts);
523 if (rv == DIALOG_OK) {
524 SMB_ASSERT(opts->query != NULL);
525 opts->match = find_substring;
526 opts->node = regedit->keys->root;
527 if (opts->search_nocase) {
528 opts->match = find_substring_nocase;
530 if (opts->search_relative) {
531 opts->node =
532 tree_view_get_current_node(regedit->keys);
534 regedit_search(regedit);
536 break;
538 case 'x':
539 case 'X':
540 regedit_search(regedit);
541 break;
542 case '\t':
543 regedit->tree_input = !regedit->tree_input;
544 print_heading(regedit);
545 break;
546 default:
547 if (regedit->tree_input) {
548 handle_tree_input(regedit, c);
549 } else {
550 handle_value_input(regedit, c);
555 int regedit_getch(void)
557 int c;
559 SMB_ASSERT(regedit_main);
561 c = getch();
562 if (c == KEY_RESIZE) {
563 tree_view_resize(regedit_main->keys, KEY_HEIGHT, KEY_WIDTH,
564 KEY_START_Y, KEY_START_X);
565 value_list_resize(regedit_main->vl, VAL_HEIGHT, VAL_WIDTH,
566 VAL_START_Y, VAL_START_X);
567 print_heading(regedit_main);
568 show_path(regedit_main);
571 return c;
574 static void display_window(TALLOC_CTX *mem_ctx, struct registry_context *ctx)
576 struct regedit *regedit;
577 struct tree_node *root;
578 bool colors;
579 int key;
581 initscr();
583 cbreak();
584 noecho();
586 colors = has_colors();
587 if (colors) {
588 start_color();
589 use_default_colors();
590 assume_default_colors(COLOR_WHITE, COLOR_BLUE);
591 init_pair(PAIR_YELLOW_CYAN, COLOR_YELLOW, COLOR_CYAN);
592 init_pair(PAIR_BLACK_CYAN, COLOR_BLACK, COLOR_CYAN);
595 regedit = talloc_zero(mem_ctx, struct regedit);
596 SMB_ASSERT(regedit != NULL);
597 regedit_main = regedit;
599 regedit->main_window = stdscr;
600 keypad(regedit->main_window, TRUE);
602 mvwprintw(regedit->main_window, 0, 0, "Path: ");
603 regedit->path_label = newpad(1, PATH_WIDTH_MAX);
604 SMB_ASSERT(regedit->path_label);
605 wprintw(regedit->path_label, "/");
606 show_path(regedit_main);
608 root = load_hives(regedit, ctx);
609 SMB_ASSERT(root != NULL);
611 regedit->keys = tree_view_new(regedit, root, KEY_HEIGHT, KEY_WIDTH,
612 KEY_START_Y, KEY_START_X);
613 SMB_ASSERT(regedit->keys != NULL);
615 regedit->vl = value_list_new(regedit, VAL_HEIGHT, VAL_WIDTH,
616 VAL_START_Y, VAL_START_X);
617 SMB_ASSERT(regedit->vl != NULL);
619 regedit->tree_input = true;
620 print_heading(regedit);
622 tree_view_show(regedit->keys);
623 menu_driver(regedit->keys->menu, REQ_FIRST_ITEM);
624 load_values(regedit);
625 value_list_show(regedit->vl);
627 update_panels();
628 doupdate();
630 do {
631 key = regedit_getch();
633 handle_main_input(regedit, key);
634 update_panels();
635 doupdate();
636 } while (key != 'q' || key == 'Q');
638 endwin();
641 int main(int argc, const char **argv)
643 struct poptOption long_options[] = {
644 POPT_AUTOHELP
645 /* ... */
646 POPT_COMMON_SAMBA
647 POPT_COMMON_CONNECTION
648 POPT_COMMON_CREDENTIALS
649 POPT_TABLEEND
651 int opt;
652 poptContext pc;
653 struct user_auth_info *auth_info;
654 TALLOC_CTX *frame;
655 struct registry_context *ctx;
656 WERROR rv;
658 frame = talloc_stackframe();
660 setup_logging("regedit", DEBUG_DEFAULT_STDERR);
661 lp_set_cmdline("log level", "0");
663 /* process options */
664 auth_info = user_auth_info_init(frame);
665 if (auth_info == NULL) {
666 exit(1);
668 popt_common_set_auth_info(auth_info);
669 pc = poptGetContext("regedit", argc, argv, long_options, 0);
671 while ((opt = poptGetNextOpt(pc)) != -1) {
672 /* TODO */
675 if (!lp_load_global(get_dyn_CONFIGFILE())) {
676 DEBUG(0, ("ERROR loading config file...\n"));
677 exit(1);
680 /* some simple tests */
682 rv = reg_open_samba3(frame, &ctx);
683 if (!W_ERROR_IS_OK(rv)) {
684 TALLOC_FREE(frame);
686 return 1;
689 display_window(frame, ctx);
691 TALLOC_FREE(frame);
693 return 0;