regedit: Add help msgs for key and value sections at bottom of screen.
[Samba/id10ts.git] / source3 / utils / regedit.c
blob5ca1edffe7242bee1e1f834799ecf754d47b1c9b
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 3
34 #define KEY_WIDTH (COLS / 4)
35 #define KEY_HEIGHT (LINES - KEY_START_Y - 1)
36 #define VAL_START_X KEY_WIDTH
37 #define VAL_START_Y 3
38 #define VAL_WIDTH (COLS - KEY_WIDTH)
39 #define VAL_HEIGHT (LINES - VAL_START_Y - 1)
40 #define HEADING_START_Y (KEY_START_Y - 1)
41 #define HELP_START_Y (LINES - 1)
42 #define HELP_START_X 0
43 #define HELP_WIDTH (LINES)
44 #define PATH_START_Y 0
45 #define PATH_START_X 6
46 #define PATH_MAX_Y (COLS - 1)
47 #define PATH_WIDTH (COLS - 6)
48 #define PATH_WIDTH_MAX 1024
50 struct regedit {
51 WINDOW *main_window;
52 WINDOW *path_label;
53 size_t path_len;
54 struct value_list *vl;
55 struct tree_view *keys;
56 bool tree_input;
59 static struct regedit *regedit_main = NULL;
61 static void show_path(struct regedit *regedit)
63 int start_pad = 0;
64 int start_win = PATH_START_X;
66 if (PATH_START_X + regedit->path_len > COLS) {
67 start_pad = 3 + PATH_START_X + regedit->path_len - COLS;
68 mvprintw(PATH_START_Y, start_win, "...");
69 start_win += 3;
71 copywin(regedit->path_label, regedit->main_window, 0, start_pad,
72 PATH_START_Y, start_win, PATH_START_Y, PATH_MAX_Y, false);
75 static void print_path(struct regedit *regedit, struct tree_node *node)
77 regedit->path_len = tree_node_print_path(regedit->path_label, node);
78 show_path(regedit);
81 /* load all available hives */
82 static struct tree_node *load_hives(TALLOC_CTX *mem_ctx,
83 struct registry_context *ctx)
85 const char *hives[] = {
86 "HKEY_CLASSES_ROOT",
87 "HKEY_CURRENT_USER",
88 "HKEY_LOCAL_MACHINE",
89 "HKEY_PERFORMANCE_DATA",
90 "HKEY_USERS",
91 "HKEY_CURRENT_CONFIG",
92 "HKEY_DYN_DATA",
93 "HKEY_PERFORMANCE_TEXT",
94 "HKEY_PERFORMANCE_NLSTEXT",
95 NULL
97 struct tree_node *root, *prev, *node;
98 struct registry_key *key;
99 WERROR rv;
100 size_t i;
102 root = NULL;
103 prev = NULL;
105 for (i = 0; hives[i] != NULL; ++i) {
106 rv = reg_get_predefined_key_by_name(ctx, hives[i], &key);
107 if (!W_ERROR_IS_OK(rv)) {
108 continue;
111 node = tree_node_new(mem_ctx, NULL, hives[i], key);
112 if (node == NULL) {
113 return NULL;
116 if (root == NULL) {
117 root = node;
119 if (prev) {
120 tree_node_append(prev, node);
122 prev = node;
125 return root;
128 static void print_help(struct regedit *regedit)
130 const char *khelp = "[n] New Key [s] New Subkey [d] Del Key "
131 "[LEFT] Ascend [RIGHT] Descend";
132 const char *vhelp = "[n] New Value [d] Del Value [ENTER] Edit";
133 const char *msg = "KEYS";
134 const char *help = khelp;
135 int i, pad;
137 if (!regedit->tree_input) {
138 msg = "VALUES";
139 help = vhelp;
142 move(HELP_START_Y, HELP_START_X);
143 clrtoeol();
144 attron(A_REVERSE | A_BOLD);
145 move(HELP_START_Y, HELP_START_X);
146 addstr(msg);
147 attroff(A_BOLD);
148 pad = COLS - strlen(msg) - strlen(help);
149 for (i = 0; i < pad; ++i) {
150 addch(' ');
152 addstr(help);
153 attroff(A_REVERSE);
156 static void print_heading(struct regedit *regedit)
158 move(HEADING_START_Y, 0);
159 clrtoeol();
161 if (regedit->tree_input) {
162 attron(A_REVERSE);
163 } else {
164 attroff(A_REVERSE);
166 mvprintw(HEADING_START_Y, KEY_START_X, "Key");
167 attroff(A_REVERSE);
169 if (!regedit->tree_input) {
170 attron(A_REVERSE);
171 } else {
172 attroff(A_REVERSE);
174 mvprintw(HEADING_START_Y, VAL_START_X, "Value");
175 attroff(A_REVERSE);
177 print_help(regedit);
180 static void add_reg_key(struct regedit *regedit, struct tree_node *node,
181 bool subkey)
183 char *name;
184 const char *msg;
186 if (!subkey && !node->parent) {
187 return;
190 msg = "Enter name of new key";
191 if (subkey) {
192 msg = "Enter name of new subkey";
194 dialog_input(regedit, &name, "New Key", msg);
195 if (name) {
196 WERROR rv;
197 struct registry_key *new_key;
198 struct tree_node *new_node;
199 struct tree_node *list;
200 struct tree_node *parent;
202 if (subkey) {
203 parent = node;
204 list = node->child_head;
205 } else {
206 parent = node->parent;
207 list = tree_node_first(node);
208 SMB_ASSERT(list != NULL);
210 rv = reg_key_add_name(regedit, parent->key, name,
211 NULL, NULL, &new_key);
212 if (W_ERROR_IS_OK(rv)) {
213 /* The list of subkeys may not be present in
214 cache yet, so if not, don't bother allocating
215 a new node for the key. */
216 if (list) {
217 new_node = tree_node_new(parent, parent,
218 name, new_key);
219 SMB_ASSERT(new_node);
220 tree_node_append_last(list, new_node);
223 list = tree_node_first(node);
224 tree_view_clear(regedit->keys);
225 tree_view_update(regedit->keys, list);
226 } else {
227 dialog_notice(regedit, DIA_ALERT, "New Key",
228 "Failed to create key.");
230 talloc_free(name);
234 static void handle_tree_input(struct regedit *regedit, int c)
236 struct tree_node *node;
238 switch (c) {
239 case KEY_DOWN:
240 menu_driver(regedit->keys->menu, REQ_DOWN_ITEM);
241 node = item_userptr(current_item(regedit->keys->menu));
242 value_list_load(regedit->vl, node->key);
243 break;
244 case KEY_UP:
245 menu_driver(regedit->keys->menu, REQ_UP_ITEM);
246 node = item_userptr(current_item(regedit->keys->menu));
247 value_list_load(regedit->vl, node->key);
248 break;
249 case '\n':
250 case KEY_ENTER:
251 case KEY_RIGHT:
252 node = item_userptr(current_item(regedit->keys->menu));
253 if (node && tree_node_has_children(node)) {
254 tree_node_load_children(node);
255 print_path(regedit, node->child_head);
256 tree_view_update(regedit->keys, node->child_head);
257 value_list_load(regedit->vl, node->child_head->key);
259 break;
260 case KEY_LEFT:
261 node = item_userptr(current_item(regedit->keys->menu));
262 if (node && node->parent) {
263 print_path(regedit, node->parent);
264 node = tree_node_first(node->parent);
265 tree_view_update(regedit->keys, node);
266 value_list_load(regedit->vl, node->key);
268 break;
269 case 'n':
270 case 'N':
271 node = item_userptr(current_item(regedit->keys->menu));
272 add_reg_key(regedit, node, false);
273 break;
274 case 's':
275 case 'S':
276 node = item_userptr(current_item(regedit->keys->menu));
277 add_reg_key(regedit, node, true);
278 break;
279 case 'd':
280 case 'D': {
281 int sel;
283 node = item_userptr(current_item(regedit->keys->menu));
284 if (!node->parent) {
285 break;
287 sel = dialog_notice(regedit, DIA_CONFIRM,
288 "Delete Key",
289 "Really delete key \"%s\"?",
290 node->name);
291 if (sel == DIALOG_OK) {
292 WERROR rv;
293 struct tree_node *pop;
294 struct tree_node *parent = node->parent;
296 rv = reg_key_del(node, parent->key, node->name);
297 if (W_ERROR_IS_OK(rv)) {
298 tree_view_clear(regedit->keys);
299 pop = tree_node_pop(&node);
300 tree_node_free_recursive(pop);
301 node = parent->child_head;
302 if (node == NULL) {
303 node = tree_node_first(parent);
304 print_path(regedit, node);
306 tree_view_update(regedit->keys, node);
307 value_list_load(regedit->vl, node->key);
308 } else {
309 dialog_notice(regedit, DIA_ALERT, "Delete Key",
310 "Failed to delete key.");
313 break;
317 tree_view_show(regedit->keys);
318 value_list_show(regedit->vl);
321 static void handle_value_input(struct regedit *regedit, int c)
323 struct value_item *vitem;
325 switch (c) {
326 case KEY_DOWN:
327 menu_driver(regedit->vl->menu, REQ_DOWN_ITEM);
328 break;
329 case KEY_UP:
330 menu_driver(regedit->vl->menu, REQ_UP_ITEM);
331 break;
332 case '\n':
333 case KEY_ENTER:
334 vitem = item_userptr(current_item(regedit->vl->menu));
335 if (vitem) {
336 struct tree_node *node;
337 node = item_userptr(current_item(regedit->keys->menu));
338 dialog_edit_value(regedit, node->key, vitem->type,
339 vitem);
340 value_list_load(regedit->vl, node->key);
342 break;
343 case 'n':
344 case 'N': {
345 int new_type;
346 int sel;
348 sel = dialog_select_type(regedit, &new_type);
349 if (sel == DIALOG_OK) {
350 struct tree_node *node;
351 node = item_userptr(current_item(regedit->keys->menu));
352 dialog_edit_value(regedit, node->key, new_type, NULL);
353 value_list_load(regedit->vl, node->key);
355 break;
357 case 'd':
358 case 'D':
359 vitem = item_userptr(current_item(regedit->vl->menu));
360 if (vitem) {
361 int sel;
363 sel = dialog_notice(regedit, DIA_CONFIRM,
364 "Delete Value",
365 "Really delete value \"%s\"?",
366 vitem->value_name);
367 if (sel == DIALOG_OK) {
368 ITEM *it = current_item(regedit->keys->menu);
369 struct tree_node *node = item_userptr(it);
370 reg_del_value(regedit, node->key,
371 vitem->value_name);
372 value_list_load(regedit->vl, node->key);
375 break;
378 value_list_show(regedit->vl);
381 static void handle_main_input(struct regedit *regedit, int c)
383 switch (c) {
384 case '\t':
385 regedit->tree_input = !regedit->tree_input;
386 print_heading(regedit);
387 break;
388 default:
389 if (regedit->tree_input) {
390 handle_tree_input(regedit, c);
391 } else {
392 handle_value_input(regedit, c);
397 int regedit_getch(void)
399 int c;
401 SMB_ASSERT(regedit_main);
403 c = getch();
404 if (c == KEY_RESIZE) {
405 tree_view_resize(regedit_main->keys, KEY_HEIGHT, KEY_WIDTH,
406 KEY_START_Y, KEY_START_X);
407 value_list_resize(regedit_main->vl, VAL_HEIGHT, VAL_WIDTH,
408 VAL_START_Y, VAL_START_X);
409 print_heading(regedit_main);
410 show_path(regedit_main);
413 return c;
416 static void display_window(TALLOC_CTX *mem_ctx, struct registry_context *ctx)
418 struct regedit *regedit;
419 struct tree_node *root;
420 int c;
422 initscr();
423 start_color();
424 cbreak();
425 noecho();
427 regedit = talloc_zero(mem_ctx, struct regedit);
428 SMB_ASSERT(regedit != NULL);
429 regedit_main = regedit;
431 regedit->main_window = stdscr;
432 keypad(regedit->main_window, TRUE);
434 mvwprintw(regedit->main_window, 0, 0, "Path: ");
435 regedit->path_label = newpad(1, PATH_WIDTH_MAX);
436 SMB_ASSERT(regedit->path_label);
437 wprintw(regedit->path_label, "/");
438 show_path(regedit_main);
440 root = load_hives(regedit, ctx);
441 SMB_ASSERT(root != NULL);
443 regedit->keys = tree_view_new(regedit, root, KEY_HEIGHT, KEY_WIDTH,
444 KEY_START_Y, KEY_START_X);
445 SMB_ASSERT(regedit->keys != NULL);
447 regedit->vl = value_list_new(regedit, VAL_HEIGHT, VAL_WIDTH,
448 VAL_START_Y, VAL_START_X);
449 SMB_ASSERT(regedit->vl != NULL);
451 regedit->tree_input = true;
452 print_heading(regedit);
454 tree_view_show(regedit->keys);
455 value_list_show(regedit->vl);
457 update_panels();
458 doupdate();
459 while (1) {
460 c = regedit_getch();
461 if (c == 'q' || c == 'Q') {
462 break;
464 handle_main_input(regedit, c);
465 update_panels();
466 doupdate();
469 endwin();
472 int main(int argc, char **argv)
474 struct poptOption long_options[] = {
475 POPT_AUTOHELP
476 /* ... */
477 POPT_COMMON_SAMBA
478 POPT_COMMON_CONNECTION
479 POPT_COMMON_CREDENTIALS
480 POPT_TABLEEND
482 int opt;
483 poptContext pc;
484 struct user_auth_info *auth_info;
485 TALLOC_CTX *frame;
486 struct registry_context *ctx;
487 WERROR rv;
489 talloc_enable_leak_report_full();
491 frame = talloc_stackframe();
493 setup_logging("regedit", DEBUG_DEFAULT_STDERR);
494 lp_set_cmdline("log level", "0");
496 /* process options */
497 auth_info = user_auth_info_init(frame);
498 if (auth_info == NULL) {
499 exit(1);
501 popt_common_set_auth_info(auth_info);
502 pc = poptGetContext("regedit", argc, (const char **)argv, long_options, 0);
504 while ((opt = poptGetNextOpt(pc)) != -1) {
505 /* TODO */
508 if (!lp_load_global(get_dyn_CONFIGFILE())) {
509 DEBUG(0, ("ERROR loading config file...\n"));
510 exit(1);
513 /* some simple tests */
515 rv = reg_open_samba3(frame, &ctx);
516 if (!W_ERROR_IS_OK(rv)) {
517 TALLOC_FREE(frame);
519 return 1;
522 display_window(frame, ctx);
524 //talloc_report_full(frame, stdout);
526 TALLOC_FREE(frame);
528 return 0;