r2257: Tab completion support in regshell (complete command names and key names
[sfrench/samba-autobuild/.git] / source4 / lib / registry / tools / regshell.c
1 /* 
2    Unix SMB/CIFS implementation.
3    simple registry frontend
4    
5    Copyright (C) Jelmer Vernooij 2004
6
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.
11    
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.
16    
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.
20 */
21
22 #include "includes.h"
23
24 /* 
25  * ck/cd - change key
26  * ls - list values/keys
27  * rmval/rm - remove value
28  * rmkey/rmdir - remove key
29  * mkkey/mkdir - make key
30  * ch - change hive
31  * info - show key info
32  * help
33  * exit
34  */
35
36 static REG_KEY *cmd_info(REG_KEY *cur, int argc, char **argv)
37 {
38         time_t last_mod;
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 */
45         return cur;
46 }
47
48 static REG_KEY *cmd_pwd(REG_KEY *cur, int argc, char **argv)
49 {
50         printf("%s\n", reg_key_get_path_abs(cur));
51         return cur;
52 }
53
54 static REG_KEY *cmd_set(REG_KEY *cur, int argc, char **argv)
55 {
56         /* FIXME */
57         return NULL;
58 }
59
60 static REG_KEY *cmd_ck(REG_KEY *cur, int argc, char **argv)
61
62         REG_KEY *new = NULL;
63         WERROR error;
64         if(argc < 2) {
65                 new = cur;
66         } else {
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)));
70                         return NULL;
71                 }
72         } 
73
74         printf("Current path is: %s\n", reg_key_get_path_abs(new));
75         
76         return new;
77 }
78
79 static REG_KEY *cmd_ls(REG_KEY *cur, int argc, char **argv)
80 {
81         int i;
82         WERROR error;
83         REG_VAL *value;
84         REG_KEY *sub;
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));
87         }
88
89         if(!W_ERROR_EQUAL(error, WERR_NO_MORE_ITEMS)) {
90                 DEBUG(0, ("Error occured while browsing thru keys: %s\n", win_errstr(error)));
91         }
92
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));
95         }
96         
97         return NULL; 
98 }
99 static REG_KEY *cmd_mkkey(REG_KEY *cur, int argc, char **argv)
100
101         REG_KEY *tmp;
102         if(argc < 2) {
103                 fprintf(stderr, "Usage: mkkey <keyname>\n");
104                 return NULL;
105         }
106         
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]);
109                 return NULL;
110         }
111
112         fprintf(stderr, "Successfully added new subkey '%s' to '%s'\n", argv[1], reg_key_get_path_abs(cur));
113         
114         return NULL; 
115 }
116
117 static REG_KEY *cmd_rmkey(REG_KEY *cur, int argc, char **argv)
118
119         REG_KEY *key;
120         if(argc < 2) {
121                 fprintf(stderr, "Usage: rmkey <name>\n");
122                 return NULL;
123         }
124
125         if(!W_ERROR_IS_OK(reg_open_key(cur, argv[1], &key))) {
126                 fprintf(stderr, "No such subkey '%s'\n", argv[1]);
127                 return NULL;
128         }
129
130         if(!W_ERROR_IS_OK(reg_key_del(key))) {
131                 fprintf(stderr, "Error deleting '%s'\n", argv[1]);
132         } else {
133                 fprintf(stderr, "Successfully deleted '%s'\n", argv[1]);
134         }
135         
136         return NULL; 
137 }
138
139 static REG_KEY *cmd_rmval(REG_KEY *cur, int argc, char **argv)
140
141         REG_VAL *val;
142         if(argc < 2) {
143                 fprintf(stderr, "Usage: rmval <valuename>\n");
144                 return NULL;
145         }
146
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]);
149                 return NULL;
150         }
151
152         if(!W_ERROR_IS_OK(reg_val_del(val))) {
153                 fprintf(stderr, "Error deleting value '%s'\n", argv[1]);
154         } else {
155                 fprintf(stderr, "Successfully deleted value '%s'\n", argv[1]);
156         }
157
158         return NULL; 
159 }
160
161 static REG_KEY *cmd_hive(REG_KEY *cur, int argc, char **argv)
162 {
163         int i;
164         WERROR error = WERR_OK;
165         for(i = 0; W_ERROR_IS_OK(error); i++) {
166                 REG_KEY *hive;
167                 error = reg_get_hive(reg_key_handle(cur), i, &hive);
168                 if(!W_ERROR_IS_OK(error)) break;
169
170                 if(argc == 1) {
171                         printf("%s\n", reg_key_name(hive));
172                 } else if(!strcmp(reg_key_name(hive), argv[1])) {
173                         return hive;
174                 } 
175                 reg_key_free(hive);
176         }
177         return NULL;
178 }
179
180 static REG_KEY *cmd_exit(REG_KEY *cur, int argc, char **argv)
181 {
182         exit(0);
183         return NULL; 
184 }
185
186 static REG_KEY *cmd_help(REG_KEY *, int, char **);
187
188 struct {
189         const char *name;
190         const char *alias;
191         const char *help;
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 },
205         {NULL }
206 };
207
208 static REG_KEY *cmd_help(REG_KEY *cur, int argc, char **argv)
209 {
210         int i;
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);
214         }
215         return NULL;
216
217
218 static REG_KEY *process_cmd(REG_KEY *k, char *line)
219 {
220         int argc;
221         char **argv = NULL;
222         int ret, i;
223
224         if ((ret = poptParseArgvString(line, &argc, (const char ***) &argv)) != 0) {
225                 fprintf(stderr, "regshell: %s\n", poptStrerror(ret));
226                 return k;
227         }
228
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);
233                 }
234         }
235
236         fprintf(stderr, "No such command '%s'\n", argv[0]);
237         
238         return k;
239 }
240
241 #define MAX_COMPLETIONS 100
242
243 static REG_KEY *current_key = NULL;
244
245 static char **reg_complete_command(const char *text, int end)
246 {
247         /* Complete command */
248         char **matches;
249         int i, len, samelen, count=1;
250
251         matches = (char **)malloc(sizeof(matches[0])*MAX_COMPLETIONS);
252         if (!matches) return NULL;
253         matches[0] = NULL;
254
255         len = strlen(text);
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);
259                         if (!matches[count])
260                                 goto cleanup;
261                         if (count == 1)
262                                 samelen = strlen(matches[count]);
263                         else
264                                 while (strncmp(matches[count], matches[count-1], samelen) != 0)
265                                         samelen--;
266                         count++;
267                 }
268         }
269
270         switch (count) {
271         case 0: /* should never happen */
272         case 1:
273                 goto cleanup;
274         case 2:
275                 matches[0] = strdup(matches[1]);
276                 break;
277         default:
278                 matches[0] = malloc(samelen+1);
279                 if (!matches[0])
280                         goto cleanup;
281                 strncpy(matches[0], matches[1], samelen);
282                 matches[0][samelen] = 0;
283         }
284         matches[count] = NULL;
285         return matches;
286
287 cleanup:
288         while (i >= 0) {
289                 free(matches[i]);
290                 i--;
291         }
292         free(matches);
293         return NULL;
294 }
295
296 static char **reg_complete_key(const char *text, int end)
297 {
298         REG_KEY *subkey;
299         int i, j = 0;
300         int len;
301         char **matches;
302         /* Complete argument */
303
304         matches = (char **)malloc(sizeof(matches[0])*MAX_COMPLETIONS);
305         if (!matches) return NULL;
306         matches[0] = NULL;
307
308         len = strlen(text);
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));
314                                 j++;
315                         }
316                         reg_key_free(subkey);
317                 } else if(W_ERROR_EQUAL(status, WERR_NO_MORE_ITEMS)) {
318                         break;
319                 } else {
320                         printf("Error creating completion list: %s\n", win_errstr(status));
321                         return NULL;
322                 }
323         }
324         matches[j] = NULL;
325         return matches;
326 }
327
328 static char **reg_completion(const char *text, int start, int end)
329 {
330         smb_readline_ca_char(' ');
331
332         if (start == 0) {
333                 return reg_complete_command(text, end);
334         } else {
335                 return reg_complete_key(text, end);
336         }
337 }
338
339  int main(int argc, char **argv)
340 {
341         int opt;
342         const char *backend = "dir";
343         const char *credentials = NULL;
344         REG_KEY *curkey = NULL;
345         poptContext pc;
346         WERROR error;
347         REG_HANDLE *h;
348         struct poptOption long_options[] = {
349                 POPT_AUTOHELP
350                 POPT_COMMON_SAMBA
351                 {"backend", 'b', POPT_ARG_STRING, &backend, 0, "backend to use", NULL},
352                 {"credentials", 'c', POPT_ARG_STRING, &credentials, 0, "credentials", NULL},
353                 POPT_TABLEEND
354         };
355         
356         pc = poptGetContext(argv[0], argc, (const char **) argv, long_options,0);
357         
358         while((opt = poptGetNextOpt(pc)) != -1) {
359         }
360
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);
364                 return 1;
365         }
366         poptFreeContext(pc);
367
368     setup_logging("regtree", True);
369
370         error = reg_get_hive(h, 0, &curkey);
371
372         if(!W_ERROR_IS_OK(error)) return 1;
373
374         while(True) {
375                 char *line, *prompt;
376                 
377                 asprintf(&prompt, "%s> ", reg_key_get_path_abs(curkey));
378                 
379                 current_key = curkey;           /* No way to pass a void * pointer 
380                                                                            via readline :-( */
381                 line = smb_readline(prompt, NULL, reg_completion);
382
383                 if(!line)
384                         break;
385
386                 if(line[0] != '\n') {
387                         REG_KEY *new = process_cmd(curkey, line);
388                         if(new)curkey = new;
389                 }
390         }
391
392         return 0;
393 }