regedit: Cut off the front of the path when screen width is too small.
[Samba/id10ts.git] / source3 / utils / regedit.c
blobefea07b89c76f7748fa7521863eeccd3c0af3671
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 INFO_START_Y (LINES - 1)
42 #define INFO_WIDTH (LINES)
43 #define PATH_START_Y 0
44 #define PATH_START_X 6
45 #define PATH_MAX_Y (COLS - 1)
46 #define PATH_WIDTH (COLS - 6)
47 #define PATH_WIDTH_MAX 1024
49 struct regedit {
50 WINDOW *main_window;
51 WINDOW *path_label;
52 size_t path_len;
53 struct value_list *vl;
54 struct tree_view *keys;
55 bool tree_input;
58 static struct regedit *regedit_main = NULL;
60 static void show_path(struct regedit *regedit)
62 int start_pad = 0;
63 int start_win = PATH_START_X;
65 if (PATH_START_X + regedit->path_len > COLS) {
66 start_pad = 3 + PATH_START_X + regedit->path_len - COLS;
67 mvprintw(PATH_START_Y, start_win, "...");
68 start_win += 3;
70 prefresh(regedit->path_label, 0, start_pad, PATH_START_Y, start_win,
71 PATH_START_Y, PATH_MAX_Y);
74 static void print_path(struct regedit *regedit, struct tree_node *node)
76 regedit->path_len = tree_node_print_path(regedit->path_label, node);
79 /* load all available hives */
80 static struct tree_node *load_hives(TALLOC_CTX *mem_ctx,
81 struct registry_context *ctx)
83 const char *hives[] = {
84 "HKEY_CLASSES_ROOT",
85 "HKEY_CURRENT_USER",
86 "HKEY_LOCAL_MACHINE",
87 "HKEY_PERFORMANCE_DATA",
88 "HKEY_USERS",
89 "HKEY_CURRENT_CONFIG",
90 "HKEY_DYN_DATA",
91 "HKEY_PERFORMANCE_TEXT",
92 "HKEY_PERFORMANCE_NLSTEXT",
93 NULL
95 struct tree_node *root, *prev, *node;
96 struct registry_key *key;
97 WERROR rv;
98 size_t i;
100 root = NULL;
101 prev = NULL;
103 for (i = 0; hives[i] != NULL; ++i) {
104 rv = reg_get_predefined_key_by_name(ctx, hives[i], &key);
105 if (!W_ERROR_IS_OK(rv)) {
106 continue;
109 node = tree_node_new(mem_ctx, NULL, hives[i], key);
110 if (node == NULL) {
111 return NULL;
114 if (root == NULL) {
115 root = node;
117 if (prev) {
118 tree_node_append(prev, node);
120 prev = node;
123 return root;
126 static void print_heading(struct regedit *regedit)
128 move(HEADING_START_Y, 0);
129 clrtoeol();
131 if (regedit->tree_input) {
132 attron(A_REVERSE);
133 } else {
134 attroff(A_REVERSE);
136 mvprintw(HEADING_START_Y, KEY_START_X, "Key");
137 attroff(A_REVERSE);
139 if (!regedit->tree_input) {
140 attron(A_REVERSE);
141 } else {
142 attroff(A_REVERSE);
144 mvprintw(HEADING_START_Y, VAL_START_X, "Value");
145 attroff(A_REVERSE);
148 static void add_reg_key(struct regedit *regedit, struct tree_node *node,
149 bool subkey)
151 char *name;
152 const char *msg;
154 if (!subkey && !node->parent) {
155 return;
158 msg = "Enter name of new key";
159 if (subkey) {
160 msg = "Enter name of new subkey";
162 dialog_input(regedit, &name, "New Key", msg);
163 if (name) {
164 WERROR rv;
165 struct registry_key *new_key;
166 struct tree_node *new_node;
167 struct tree_node *list;
168 struct tree_node *parent;
170 if (subkey) {
171 parent = node;
172 list = node->child_head;
173 } else {
174 parent = node->parent;
175 list = tree_node_first(node);
176 SMB_ASSERT(list != NULL);
178 rv = reg_key_add_name(regedit, parent->key, name,
179 NULL, NULL, &new_key);
180 if (W_ERROR_IS_OK(rv)) {
181 /* The list of subkeys may not be present in
182 cache yet, so if not, don't bother allocating
183 a new node for the key. */
184 if (list) {
185 new_node = tree_node_new(parent, parent,
186 name, new_key);
187 SMB_ASSERT(new_node);
188 tree_node_append_last(list, new_node);
191 list = tree_node_first(node);
192 tree_view_clear(regedit->keys);
193 tree_view_update(regedit->keys, list);
194 } else {
195 dialog_notice(regedit, DIA_ALERT, "New Key",
196 "Failed to create key.");
198 talloc_free(name);
202 static void handle_tree_input(struct regedit *regedit, int c)
204 struct tree_node *node;
206 switch (c) {
207 case KEY_DOWN:
208 menu_driver(regedit->keys->menu, REQ_DOWN_ITEM);
209 node = item_userptr(current_item(regedit->keys->menu));
210 value_list_load(regedit->vl, node->key);
211 break;
212 case KEY_UP:
213 menu_driver(regedit->keys->menu, REQ_UP_ITEM);
214 node = item_userptr(current_item(regedit->keys->menu));
215 value_list_load(regedit->vl, node->key);
216 break;
217 case '\n':
218 case KEY_ENTER:
219 case KEY_RIGHT:
220 node = item_userptr(current_item(regedit->keys->menu));
221 if (node && tree_node_has_children(node)) {
222 tree_node_load_children(node);
223 print_path(regedit, node->child_head);
224 tree_view_update(regedit->keys, node->child_head);
225 value_list_load(regedit->vl, node->child_head->key);
227 break;
228 case KEY_LEFT:
229 node = item_userptr(current_item(regedit->keys->menu));
230 if (node && node->parent) {
231 print_path(regedit, node->parent);
232 node = tree_node_first(node->parent);
233 tree_view_update(regedit->keys, node);
234 value_list_load(regedit->vl, node->key);
236 break;
237 case 'n':
238 case 'N':
239 node = item_userptr(current_item(regedit->keys->menu));
240 add_reg_key(regedit, node, false);
241 break;
242 case 's':
243 case 'S':
244 node = item_userptr(current_item(regedit->keys->menu));
245 add_reg_key(regedit, node, true);
246 break;
247 case 'd':
248 case 'D': {
249 int sel;
251 node = item_userptr(current_item(regedit->keys->menu));
252 if (!node->parent) {
253 break;
255 sel = dialog_notice(regedit, DIA_CONFIRM,
256 "Delete Key",
257 "Really delete key \"%s\"?",
258 node->name);
259 if (sel == DIALOG_OK) {
260 WERROR rv;
261 struct tree_node *pop;
262 struct tree_node *parent = node->parent;
264 rv = reg_key_del(node, parent->key, node->name);
265 if (W_ERROR_IS_OK(rv)) {
266 tree_view_clear(regedit->keys);
267 pop = tree_node_pop(&node);
268 tree_node_free_recursive(pop);
269 node = parent->child_head;
270 if (node == NULL) {
271 node = tree_node_first(parent);
272 print_path(regedit, node);
274 tree_view_update(regedit->keys, node);
275 value_list_load(regedit->vl, node->key);
276 } else {
277 dialog_notice(regedit, DIA_ALERT, "Delete Key",
278 "Failed to delete key.");
281 break;
285 tree_view_show(regedit->keys);
286 value_list_show(regedit->vl);
289 static void handle_value_input(struct regedit *regedit, int c)
291 struct value_item *vitem;
293 switch (c) {
294 case KEY_DOWN:
295 menu_driver(regedit->vl->menu, REQ_DOWN_ITEM);
296 break;
297 case KEY_UP:
298 menu_driver(regedit->vl->menu, REQ_UP_ITEM);
299 break;
300 case '\n':
301 case KEY_ENTER:
302 vitem = item_userptr(current_item(regedit->vl->menu));
303 if (vitem) {
304 struct tree_node *node;
305 node = item_userptr(current_item(regedit->keys->menu));
306 dialog_edit_value(regedit, node->key, vitem->type,
307 vitem);
308 value_list_load(regedit->vl, node->key);
310 break;
311 case 'n':
312 case 'N': {
313 int new_type;
314 int sel;
316 sel = dialog_select_type(regedit, &new_type);
317 if (sel == DIALOG_OK) {
318 struct tree_node *node;
319 node = item_userptr(current_item(regedit->keys->menu));
320 dialog_edit_value(regedit, node->key, new_type, NULL);
321 value_list_load(regedit->vl, node->key);
323 break;
325 case 'd':
326 case 'D':
327 vitem = item_userptr(current_item(regedit->vl->menu));
328 if (vitem) {
329 int sel;
331 sel = dialog_notice(regedit, DIA_CONFIRM,
332 "Delete Value",
333 "Really delete value \"%s\"?",
334 vitem->value_name);
335 if (sel == DIALOG_OK) {
336 ITEM *it = current_item(regedit->keys->menu);
337 struct tree_node *node = item_userptr(it);
338 reg_del_value(regedit, node->key,
339 vitem->value_name);
340 value_list_load(regedit->vl, node->key);
343 break;
346 value_list_show(regedit->vl);
349 static void handle_main_input(struct regedit *regedit, int c)
351 switch (c) {
352 case '\t':
353 regedit->tree_input = !regedit->tree_input;
354 print_heading(regedit);
355 break;
356 default:
357 if (regedit->tree_input) {
358 handle_tree_input(regedit, c);
359 } else {
360 handle_value_input(regedit, c);
365 int regedit_getch(void)
367 int c;
369 SMB_ASSERT(regedit_main);
371 c = getch();
372 if (c == KEY_RESIZE) {
373 tree_view_resize(regedit_main->keys, KEY_HEIGHT, KEY_WIDTH,
374 KEY_START_Y, KEY_START_X);
375 value_list_resize(regedit_main->vl, VAL_HEIGHT, VAL_WIDTH,
376 VAL_START_Y, VAL_START_X);
377 print_heading(regedit_main);
380 return c;
383 static void display_window(TALLOC_CTX *mem_ctx, struct registry_context *ctx)
385 struct regedit *regedit;
386 struct tree_node *root;
387 int c;
389 initscr();
390 start_color();
391 cbreak();
392 noecho();
394 regedit = talloc_zero(mem_ctx, struct regedit);
395 SMB_ASSERT(regedit != NULL);
396 regedit_main = regedit;
398 regedit->main_window = stdscr;
399 keypad(regedit->main_window, TRUE);
401 mvwprintw(regedit->main_window, 0, 0, "Path: ");
402 regedit->path_label = newpad(1, PATH_WIDTH_MAX);
403 SMB_ASSERT(regedit->path_label);
404 wprintw(regedit->path_label, "/");
406 root = load_hives(regedit, ctx);
407 SMB_ASSERT(root != NULL);
409 regedit->keys = tree_view_new(regedit, root, KEY_HEIGHT, KEY_WIDTH,
410 KEY_START_Y, KEY_START_X);
411 SMB_ASSERT(regedit->keys != NULL);
413 regedit->vl = value_list_new(regedit, VAL_HEIGHT, VAL_WIDTH,
414 VAL_START_Y, VAL_START_X);
415 SMB_ASSERT(regedit->vl != NULL);
417 regedit->tree_input = true;
418 print_heading(regedit);
420 tree_view_show(regedit->keys);
421 value_list_show(regedit->vl);
423 update_panels();
424 doupdate();
425 show_path(regedit_main);
426 while (1) {
427 c = regedit_getch();
428 if (c == 'q' || c == 'Q') {
429 break;
431 handle_main_input(regedit, c);
432 update_panels();
433 doupdate();
434 show_path(regedit_main);
437 endwin();
440 int main(int argc, char **argv)
442 struct poptOption long_options[] = {
443 POPT_AUTOHELP
444 /* ... */
445 POPT_COMMON_SAMBA
446 POPT_COMMON_CONNECTION
447 POPT_COMMON_CREDENTIALS
448 POPT_TABLEEND
450 int opt;
451 poptContext pc;
452 struct user_auth_info *auth_info;
453 TALLOC_CTX *frame;
454 struct registry_context *ctx;
455 WERROR rv;
457 talloc_enable_leak_report_full();
459 frame = talloc_stackframe();
461 setup_logging("regedit", DEBUG_DEFAULT_STDERR);
462 lp_set_cmdline("log level", "0");
464 /* process options */
465 auth_info = user_auth_info_init(frame);
466 if (auth_info == NULL) {
467 exit(1);
469 popt_common_set_auth_info(auth_info);
470 pc = poptGetContext("regedit", argc, (const char **)argv, long_options, 0);
472 while ((opt = poptGetNextOpt(pc)) != -1) {
473 /* TODO */
476 if (!lp_load_global(get_dyn_CONFIGFILE())) {
477 DEBUG(0, ("ERROR loading config file...\n"));
478 exit(1);
481 /* some simple tests */
483 rv = reg_open_samba3(frame, &ctx);
484 if (!W_ERROR_IS_OK(rv)) {
485 TALLOC_FREE(frame);
487 return 1;
490 display_window(frame, ctx);
492 //talloc_report_full(frame, stdout);
494 TALLOC_FREE(frame);
496 return 0;