2 Unix SMB/CIFS implementation.
3 simple registry frontend
5 Copyright (C) Jelmer Vernooij 2004
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 * ls - list values/keys
27 * rmval/rm - remove value
28 * rmkey/rmdir - remove key
29 * mkkey/mkdir - make key
31 * info - show key info
36 static REG_KEY *cmd_info(REG_KEY *cur, int argc, char **argv)
39 printf("Name: %s\n", reg_key_name(cur));
40 printf("Full path: %s\n", reg_key_get_path(cur));
41 printf("Key Class: %s\n", reg_key_class(cur));
42 last_mod = nt_time_to_unix(reg_key_last_modified(cur));
43 printf("Time Last Modified: %s\n", ctime(&last_mod));
44 /* FIXME: Security info */
48 static REG_KEY *cmd_pwd(REG_KEY *cur, int argc, char **argv)
50 printf("%s\n", reg_key_get_path_abs(cur));
54 static REG_KEY *cmd_set(REG_KEY *cur, int argc, char **argv)
60 static REG_KEY *cmd_ck(REG_KEY *cur, int argc, char **argv)
67 error = reg_open_key(cur, argv[1], &new);
68 if(!W_ERROR_IS_OK(error)) {
69 DEBUG(0, ("Error opening specified key: %s\n", win_errstr(error)));
74 printf("Current path is: %s\n", reg_key_get_path_abs(new));
79 static REG_KEY *cmd_ls(REG_KEY *cur, int argc, char **argv)
85 for(i = 0; W_ERROR_IS_OK(error = reg_key_get_subkey_by_index(cur, i, &sub)); i++) {
86 printf("K %s\n", reg_key_name(sub));
89 if(!W_ERROR_EQUAL(error, WERR_NO_MORE_ITEMS)) {
90 DEBUG(0, ("Error occured while browsing thru keys: %s\n", win_errstr(error)));
93 for(i = 0; W_ERROR_IS_OK(error = reg_key_get_value_by_index(cur, i, &value)); i++) {
94 printf("V \"%s\" %s %s\n", reg_val_name(value), str_regtype(reg_val_type(value)), reg_val_data_string(value));
99 static REG_KEY *cmd_mkkey(REG_KEY *cur, int argc, char **argv)
103 fprintf(stderr, "Usage: mkkey <keyname>\n");
107 if(!W_ERROR_IS_OK(reg_key_add_name(cur, argv[1], 0, NULL, &tmp))) {
108 fprintf(stderr, "Error adding new subkey '%s'\n", argv[1]);
112 fprintf(stderr, "Successfully added new subkey '%s' to '%s'\n", argv[1], reg_key_get_path_abs(cur));
117 static REG_KEY *cmd_rmkey(REG_KEY *cur, int argc, char **argv)
121 fprintf(stderr, "Usage: rmkey <name>\n");
125 if(!W_ERROR_IS_OK(reg_open_key(cur, argv[1], &key))) {
126 fprintf(stderr, "No such subkey '%s'\n", argv[1]);
130 if(!W_ERROR_IS_OK(reg_key_del(key))) {
131 fprintf(stderr, "Error deleting '%s'\n", argv[1]);
133 fprintf(stderr, "Successfully deleted '%s'\n", argv[1]);
139 static REG_KEY *cmd_rmval(REG_KEY *cur, int argc, char **argv)
143 fprintf(stderr, "Usage: rmval <valuename>\n");
147 if(!W_ERROR_IS_OK(reg_key_get_value_by_name(cur, argv[1], &val))) {
148 fprintf(stderr, "No such value '%s'\n", argv[1]);
152 if(!W_ERROR_IS_OK(reg_val_del(val))) {
153 fprintf(stderr, "Error deleting value '%s'\n", argv[1]);
155 fprintf(stderr, "Successfully deleted value '%s'\n", argv[1]);
161 static REG_KEY *cmd_hive(REG_KEY *cur, int argc, char **argv)
164 WERROR error = WERR_OK;
165 for(i = 0; W_ERROR_IS_OK(error); i++) {
167 error = reg_get_hive(reg_key_handle(cur), i, &hive);
168 if(!W_ERROR_IS_OK(error)) break;
171 printf("%s\n", reg_key_name(hive));
172 } else if(!strcmp(reg_key_name(hive), argv[1])) {
180 static REG_KEY *cmd_exit(REG_KEY *cur, int argc, char **argv)
186 static REG_KEY *cmd_help(REG_KEY *, int, char **);
192 REG_KEY *(*handle)(REG_KEY *, int argc, char **argv);
193 } regshell_cmds[] = {
194 {"ck", "cd", "Change current key", cmd_ck },
195 {"ch", "hive", "Change current hive", cmd_hive },
196 {"info", "i", "Show detailed information of a key", cmd_info },
197 {"list", "ls", "List values/keys in current key", cmd_ls },
198 {"mkkey", "mkdir", "Make new key", cmd_mkkey },
199 {"rmval", "rm", "Remove value", cmd_rmval },
200 {"rmkey", "rmdir", "Remove key", cmd_rmkey },
201 {"pwd", "pwk", "Printing current key", cmd_pwd },
202 {"set", "update", "Update value", cmd_set },
203 {"help", "?", "Help", cmd_help },
204 {"exit", "quit", "Exit", cmd_exit },
208 static REG_KEY *cmd_help(REG_KEY *cur, int argc, char **argv)
211 printf("Available commands:\n");
212 for(i = 0; regshell_cmds[i].name; i++) {
213 printf("%s - %s\n", regshell_cmds[i].name, regshell_cmds[i].help);
218 static REG_KEY *process_cmd(REG_KEY *k, char *line)
224 if ((ret = poptParseArgvString(line, &argc, (const char ***) &argv)) != 0) {
225 fprintf(stderr, "regshell: %s\n", poptStrerror(ret));
229 for(i = 0; regshell_cmds[i].name; i++) {
230 if(!strcmp(regshell_cmds[i].name, argv[0]) ||
231 (regshell_cmds[i].alias && !strcmp(regshell_cmds[i].alias, argv[0]))) {
232 return regshell_cmds[i].handle(k, argc, argv);
236 fprintf(stderr, "No such command '%s'\n", argv[0]);
241 #define MAX_COMPLETIONS 100
243 static REG_KEY *current_key = NULL;
245 static char **reg_complete_command(const char *text, int end)
247 /* Complete command */
249 int i, len, samelen, count=1;
251 matches = (char **)malloc(sizeof(matches[0])*MAX_COMPLETIONS);
252 if (!matches) return NULL;
256 for (i=0;regshell_cmds[i].handle && count < MAX_COMPLETIONS-1;i++) {
257 if (strncmp(text, regshell_cmds[i].name, len) == 0) {
258 matches[count] = strdup(regshell_cmds[i].name);
262 samelen = strlen(matches[count]);
264 while (strncmp(matches[count], matches[count-1], samelen) != 0)
271 case 0: /* should never happen */
275 matches[0] = strdup(matches[1]);
278 matches[0] = malloc(samelen+1);
281 strncpy(matches[0], matches[1], samelen);
282 matches[0][samelen] = 0;
284 matches[count] = NULL;
296 static char **reg_complete_key(const char *text, int end)
302 /* Complete argument */
304 matches = (char **)malloc(sizeof(matches[0])*MAX_COMPLETIONS);
305 if (!matches) return NULL;
309 for(i = 0; j < MAX_COMPLETIONS-1; i++) {
310 WERROR status = reg_key_get_subkey_by_index(current_key, i, &subkey);
311 if(W_ERROR_IS_OK(status)) {
312 if(!strncmp(text, reg_key_name(subkey), len)) {
313 matches[j] = strdup(reg_key_name(subkey));
316 reg_key_free(subkey);
317 } else if(W_ERROR_EQUAL(status, WERR_NO_MORE_ITEMS)) {
320 printf("Error creating completion list: %s\n", win_errstr(status));
328 static char **reg_completion(const char *text, int start, int end)
330 smb_readline_ca_char(' ');
333 return reg_complete_command(text, end);
335 return reg_complete_key(text, end);
339 int main(int argc, char **argv)
342 const char *backend = "dir";
343 const char *credentials = NULL;
344 REG_KEY *curkey = NULL;
348 struct poptOption long_options[] = {
351 {"backend", 'b', POPT_ARG_STRING, &backend, 0, "backend to use", NULL},
352 {"credentials", 'c', POPT_ARG_STRING, &credentials, 0, "credentials", NULL},
356 pc = poptGetContext(argv[0], argc, (const char **) argv, long_options,0);
358 while((opt = poptGetNextOpt(pc)) != -1) {
361 error = reg_open(backend, poptPeekArg(pc), credentials, &h);
362 if(!W_ERROR_IS_OK(error)) {
363 fprintf(stderr, "Unable to open '%s' with backend '%s'\n", poptGetArg(pc), backend);
368 setup_logging("regtree", True);
370 error = reg_get_hive(h, 0, &curkey);
372 if(!W_ERROR_IS_OK(error)) return 1;
377 asprintf(&prompt, "%s> ", reg_key_get_path_abs(curkey));
379 current_key = curkey; /* No way to pass a void * pointer
381 line = smb_readline(prompt, NULL, reg_completion);
386 if(line[0] != '\n') {
387 REG_KEY *new = process_cmd(curkey, line);