Import tdbtool from SAMBA (e440a2e11e78a562f97971c0dfe0cf3f694996ff)
[ccan] / ccan / tdb / tools / tdbtool.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Samba database functions
4    Copyright (C) Andrew Tridgell              1999-2000
5    Copyright (C) Paul `Rusty' Russell              2000
6    Copyright (C) Jeremy Allison                    2000
7    Copyright (C) Andrew Esh                        2001
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 #include <ccan/tdb/tdb.h>
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <ctype.h>
27 #include <sys/time.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <fcntl.h>
31 #include <errno.h>
32 #include <string.h>
33
34 static int do_command(void);
35 const char *cmdname;
36 char *arg1, *arg2;
37 size_t arg1len, arg2len;
38 int bIterate = 0;
39 char *line;
40 TDB_DATA iterate_kbuf;
41 char cmdline[1024];
42 static int disable_mmap;
43
44 enum commands {
45         CMD_CREATE_TDB,
46         CMD_OPEN_TDB,
47         CMD_TRANSACTION_START,
48         CMD_TRANSACTION_COMMIT,
49         CMD_TRANSACTION_CANCEL,
50         CMD_ERASE,
51         CMD_DUMP,
52         CMD_INSERT,
53         CMD_MOVE,
54         CMD_STORE,
55         CMD_SHOW,
56         CMD_KEYS,
57         CMD_HEXKEYS,
58         CMD_DELETE,
59         CMD_LIST_HASH_FREE,
60         CMD_LIST_FREE,
61         CMD_INFO,
62         CMD_MMAP,
63         CMD_SPEED,
64         CMD_FIRST,
65         CMD_NEXT,
66         CMD_SYSTEM,
67         CMD_CHECK,
68         CMD_QUIT,
69         CMD_HELP
70 };
71
72 typedef struct {
73         const char *name;
74         enum commands cmd;
75 } COMMAND_TABLE;
76
77 COMMAND_TABLE cmd_table[] = {
78         {"create",      CMD_CREATE_TDB},
79         {"open",        CMD_OPEN_TDB},
80         {"transaction_start",   CMD_TRANSACTION_START},
81         {"transaction_commit",  CMD_TRANSACTION_COMMIT},
82         {"transaction_cancel",  CMD_TRANSACTION_CANCEL},
83         {"erase",       CMD_ERASE},
84         {"dump",        CMD_DUMP},
85         {"insert",      CMD_INSERT},
86         {"move",        CMD_MOVE},
87         {"store",       CMD_STORE},
88         {"show",        CMD_SHOW},
89         {"keys",        CMD_KEYS},
90         {"hexkeys",     CMD_HEXKEYS},
91         {"delete",      CMD_DELETE},
92         {"list",        CMD_LIST_HASH_FREE},
93         {"free",        CMD_LIST_FREE},
94         {"info",        CMD_INFO},
95         {"speed",       CMD_SPEED},
96         {"mmap",        CMD_MMAP},
97         {"first",       CMD_FIRST},
98         {"1",           CMD_FIRST},
99         {"next",        CMD_NEXT},
100         {"n",           CMD_NEXT},
101         {"check",       CMD_CHECK},
102         {"quit",        CMD_QUIT},
103         {"q",           CMD_QUIT},
104         {"!",           CMD_SYSTEM},
105         {NULL,          CMD_HELP}
106 };
107
108 struct timeval tp1,tp2;
109
110 static void _start_timer(void)
111 {
112         gettimeofday(&tp1,NULL);
113 }
114
115 static double _end_timer(void)
116 {
117         gettimeofday(&tp2,NULL);
118         return((tp2.tv_sec - tp1.tv_sec) + 
119                (tp2.tv_usec - tp1.tv_usec)*1.0e-6);
120 }
121
122 /* a tdb tool for manipulating a tdb database */
123
124 static TDB_CONTEXT *tdb;
125
126 static int print_rec(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state);
127 static int print_key(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state);
128 static int print_hexkey(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state);
129
130 static void print_asc(const char *buf,int len)
131 {
132         int i;
133
134         /* We're probably printing ASCII strings so don't try to display
135            the trailing NULL character. */
136
137         if (buf[len - 1] == 0)
138                 len--;
139
140         for (i=0;i<len;i++)
141                 printf("%c",isprint(buf[i])?buf[i]:'.');
142 }
143
144 static void print_data(const char *buf,int len)
145 {
146         int i=0;
147         if (len<=0) return;
148         printf("[%03X] ",i);
149         for (i=0;i<len;) {
150                 printf("%02X ",(int)((unsigned char)buf[i]));
151                 i++;
152                 if (i%8 == 0) printf(" ");
153                 if (i%16 == 0) {      
154                         print_asc(&buf[i-16],8); printf(" ");
155                         print_asc(&buf[i-8],8); printf("\n");
156                         if (i<len) printf("[%03X] ",i);
157                 }
158         }
159         if (i%16) {
160                 int n;
161                 
162                 n = 16 - (i%16);
163                 printf(" ");
164                 if (n>8) printf(" ");
165                 while (n--) printf("   ");
166                 
167                 n = i%16;
168                 if (n > 8) n = 8;
169                 print_asc(&buf[i-(i%16)],n); printf(" ");
170                 n = (i%16) - n;
171                 if (n>0) print_asc(&buf[i-n],n); 
172                 printf("\n");    
173         }
174 }
175
176 static void help(void)
177 {
178         printf("\n"
179 "tdbtool: \n"
180 "  create    dbname     : create a database\n"
181 "  open      dbname     : open an existing database\n"
182 "  transaction_start    : start a transaction\n"
183 "  transaction_commit   : commit a transaction\n"
184 "  transaction_cancel   : cancel a transaction\n"
185 "  erase                : erase the database\n"
186 "  dump                 : dump the database as strings\n"
187 "  keys                 : dump the database keys as strings\n"
188 "  hexkeys              : dump the database keys as hex values\n"
189 "  info                 : print summary info about the database\n"
190 "  insert    key  data  : insert a record\n"
191 "  move      key  file  : move a record to a destination tdb\n"
192 "  store     key  data  : store a record (replace)\n"
193 "  show      key        : show a record by key\n"
194 "  delete    key        : delete a record by key\n"
195 "  list                 : print the database hash table and freelist\n"
196 "  free                 : print the database freelist\n"
197 "  check                : check the integrity of an opened database\n"
198 "  speed                : perform speed tests on the database\n"
199 "  ! command            : execute system command\n"
200 "  1 | first            : print the first record\n"
201 "  n | next             : print the next record\n"
202 "  q | quit             : terminate\n"
203 "  \\n                   : repeat 'next' command\n"
204 "\n");
205 }
206
207 static void terror(const char *why)
208 {
209         printf("%s\n", why);
210 }
211
212 static void create_tdb(const char *tdbname)
213 {
214         if (tdb) tdb_close(tdb);
215         tdb = tdb_open(tdbname, 0, TDB_CLEAR_IF_FIRST | (disable_mmap?TDB_NOMMAP:0),
216                        O_RDWR | O_CREAT | O_TRUNC, 0600);
217         if (!tdb) {
218                 printf("Could not create %s: %s\n", tdbname, strerror(errno));
219         }
220 }
221
222 static void open_tdb(const char *tdbname)
223 {
224         if (tdb) tdb_close(tdb);
225         tdb = tdb_open(tdbname, 0, disable_mmap?TDB_NOMMAP:0, O_RDWR, 0600);
226         if (!tdb) {
227                 printf("Could not open %s: %s\n", tdbname, strerror(errno));
228         }
229 }
230
231 static void insert_tdb(char *keyname, size_t keylen, char* data, size_t datalen)
232 {
233         TDB_DATA key, dbuf;
234
235         if ((keyname == NULL) || (keylen == 0)) {
236                 terror("need key");
237                 return;
238         }
239
240         key.dptr = (unsigned char *)keyname;
241         key.dsize = keylen;
242         dbuf.dptr = (unsigned char *)data;
243         dbuf.dsize = datalen;
244
245         if (tdb_store(tdb, key, dbuf, TDB_INSERT) == -1) {
246                 terror("insert failed");
247         }
248 }
249
250 static void store_tdb(char *keyname, size_t keylen, char* data, size_t datalen)
251 {
252         TDB_DATA key, dbuf;
253
254         if ((keyname == NULL) || (keylen == 0)) {
255                 terror("need key");
256                 return;
257         }
258
259         if ((data == NULL) || (datalen == 0)) {
260                 terror("need data");
261                 return;
262         }
263
264         key.dptr = (unsigned char *)keyname;
265         key.dsize = keylen;
266         dbuf.dptr = (unsigned char *)data;
267         dbuf.dsize = datalen;
268
269         printf("Storing key:\n");
270         print_rec(tdb, key, dbuf, NULL);
271
272         if (tdb_store(tdb, key, dbuf, TDB_REPLACE) == -1) {
273                 terror("store failed");
274         }
275 }
276
277 static void show_tdb(char *keyname, size_t keylen)
278 {
279         TDB_DATA key, dbuf;
280
281         if ((keyname == NULL) || (keylen == 0)) {
282                 terror("need key");
283                 return;
284         }
285
286         key.dptr = (unsigned char *)keyname;
287         key.dsize = keylen;
288
289         dbuf = tdb_fetch(tdb, key);
290         if (!dbuf.dptr) {
291             terror("fetch failed");
292             return;
293         }
294         
295         print_rec(tdb, key, dbuf, NULL);
296         
297         free( dbuf.dptr );
298         
299         return;
300 }
301
302 static void delete_tdb(char *keyname, size_t keylen)
303 {
304         TDB_DATA key;
305
306         if ((keyname == NULL) || (keylen == 0)) {
307                 terror("need key");
308                 return;
309         }
310
311         key.dptr = (unsigned char *)keyname;
312         key.dsize = keylen;
313
314         if (tdb_delete(tdb, key) != 0) {
315                 terror("delete failed");
316         }
317 }
318
319 static void move_rec(char *keyname, size_t keylen, char* tdbname)
320 {
321         TDB_DATA key, dbuf;
322         TDB_CONTEXT *dst_tdb;
323
324         if ((keyname == NULL) || (keylen == 0)) {
325                 terror("need key");
326                 return;
327         }
328
329         if ( !tdbname ) {
330                 terror("need destination tdb name");
331                 return;
332         }
333
334         key.dptr = (unsigned char *)keyname;
335         key.dsize = keylen;
336
337         dbuf = tdb_fetch(tdb, key);
338         if (!dbuf.dptr) {
339                 terror("fetch failed");
340                 return;
341         }
342         
343         print_rec(tdb, key, dbuf, NULL);
344         
345         dst_tdb = tdb_open(tdbname, 0, 0, O_RDWR, 0600);
346         if ( !dst_tdb ) {
347                 terror("unable to open destination tdb");
348                 return;
349         }
350         
351         if ( tdb_store( dst_tdb, key, dbuf, TDB_REPLACE ) == -1 ) {
352                 terror("failed to move record");
353         }
354         else
355                 printf("record moved\n");
356         
357         tdb_close( dst_tdb );
358         
359         return;
360 }
361
362 static int print_rec(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
363 {
364         printf("\nkey %d bytes\n", (int)key.dsize);
365         print_asc((const char *)key.dptr, key.dsize);
366         printf("\ndata %d bytes\n", (int)dbuf.dsize);
367         print_data((const char *)dbuf.dptr, dbuf.dsize);
368         return 0;
369 }
370
371 static int print_key(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
372 {
373         printf("key %d bytes: ", (int)key.dsize);
374         print_asc((const char *)key.dptr, key.dsize);
375         printf("\n");
376         return 0;
377 }
378
379 static int print_hexkey(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
380 {
381         printf("key %d bytes\n", (int)key.dsize);
382         print_data((const char *)key.dptr, key.dsize);
383         printf("\n");
384         return 0;
385 }
386
387 static int total_bytes;
388
389 static int traverse_fn(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
390 {
391         total_bytes += dbuf.dsize;
392         return 0;
393 }
394
395 static void info_tdb(void)
396 {
397         int count;
398         total_bytes = 0;
399         if ((count = tdb_traverse(tdb, traverse_fn, NULL)) == -1)
400                 printf("Error = %s\n", tdb_errorstr(tdb));
401         else
402                 printf("%d records totalling %d bytes\n", count, total_bytes);
403 }
404
405 static void speed_tdb(const char *tlimit)
406 {
407         unsigned timelimit = tlimit?atoi(tlimit):0;
408         double t;
409         int ops;
410         if (timelimit == 0) timelimit = 5;
411
412         ops = 0;
413         printf("Testing store speed for %u seconds\n", timelimit);
414         _start_timer();
415         do {
416                 long int r = random();
417                 TDB_DATA key, dbuf;
418                 key.dptr = (unsigned char *)"store test";
419                 key.dsize = strlen((char *)key.dptr);
420                 dbuf.dptr = (unsigned char *)&r;
421                 dbuf.dsize = sizeof(r);
422                 tdb_store(tdb, key, dbuf, TDB_REPLACE);
423                 t = _end_timer();
424                 ops++;
425         } while (t < timelimit);
426         printf("%10.3f ops/sec\n", ops/t);
427
428         ops = 0;
429         printf("Testing fetch speed for %u seconds\n", timelimit);
430         _start_timer();
431         do {
432                 long int r = random();
433                 TDB_DATA key, dbuf;
434                 key.dptr = (unsigned char *)"store test";
435                 key.dsize = strlen((char *)key.dptr);
436                 dbuf.dptr = (unsigned char *)&r;
437                 dbuf.dsize = sizeof(r);
438                 tdb_fetch(tdb, key);
439                 t = _end_timer();
440                 ops++;
441         } while (t < timelimit);
442         printf("%10.3f ops/sec\n", ops/t);
443
444         ops = 0;
445         printf("Testing transaction speed for %u seconds\n", timelimit);
446         _start_timer();
447         do {
448                 long int r = random();
449                 TDB_DATA key, dbuf;
450                 key.dptr = (unsigned char *)"transaction test";
451                 key.dsize = strlen((char *)key.dptr);
452                 dbuf.dptr = (unsigned char *)&r;
453                 dbuf.dsize = sizeof(r);
454                 tdb_transaction_start(tdb);
455                 tdb_store(tdb, key, dbuf, TDB_REPLACE);
456                 tdb_transaction_commit(tdb);
457                 t = _end_timer();
458                 ops++;
459         } while (t < timelimit);
460         printf("%10.3f ops/sec\n", ops/t);
461
462         ops = 0;
463         printf("Testing traverse speed for %u seconds\n", timelimit);
464         _start_timer();
465         do {
466                 tdb_traverse(tdb, traverse_fn, NULL);
467                 t = _end_timer();
468                 ops++;
469         } while (t < timelimit);
470         printf("%10.3f ops/sec\n", ops/t);
471 }
472
473 static void toggle_mmap(void)
474 {
475         disable_mmap = !disable_mmap;
476         if (disable_mmap) {
477                 printf("mmap is disabled\n");
478         } else {
479                 printf("mmap is enabled\n");
480         }
481 }
482
483 static char *tdb_getline(const char *prompt)
484 {
485         static char thisline[1024];
486         char *p;
487         fputs(prompt, stdout);
488         thisline[0] = 0;
489         p = fgets(thisline, sizeof(thisline)-1, stdin);
490         if (p) p = strchr(p, '\n');
491         if (p) *p = 0;
492         return p?thisline:NULL;
493 }
494
495 static int do_delete_fn(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf,
496                      void *state)
497 {
498     return tdb_delete(the_tdb, key);
499 }
500
501 static void first_record(TDB_CONTEXT *the_tdb, TDB_DATA *pkey)
502 {
503         TDB_DATA dbuf;
504         *pkey = tdb_firstkey(the_tdb);
505         
506         dbuf = tdb_fetch(the_tdb, *pkey);
507         if (!dbuf.dptr) terror("fetch failed");
508         else {
509                 print_rec(the_tdb, *pkey, dbuf, NULL);
510         }
511 }
512
513 static void next_record(TDB_CONTEXT *the_tdb, TDB_DATA *pkey)
514 {
515         TDB_DATA dbuf;
516         *pkey = tdb_nextkey(the_tdb, *pkey);
517         
518         dbuf = tdb_fetch(the_tdb, *pkey);
519         if (!dbuf.dptr) 
520                 terror("fetch failed");
521         else
522                 print_rec(the_tdb, *pkey, dbuf, NULL);
523 }
524
525 static int test_fn(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
526 {
527         return 0;
528 }
529
530 static void check_db(TDB_CONTEXT *the_tdb)
531 {
532         int tdbcount=-1;
533         if (the_tdb) {
534                 tdbcount = tdb_traverse(the_tdb, test_fn, NULL);
535         } else {
536                 printf("Error: No database opened!\n");
537         }
538
539         if (tdbcount<0) {
540                 printf("Integrity check for the opened database failed.\n");
541         } else {
542                 printf("Database integrity is OK and has %d records.\n", tdbcount);
543         }
544 }
545
546 static int do_command(void)
547 {
548         COMMAND_TABLE *ctp = cmd_table;
549         enum commands mycmd = CMD_HELP;
550         int cmd_len;
551
552         if (cmdname && strlen(cmdname) == 0) {
553                 mycmd = CMD_NEXT;
554         } else {
555                 while (ctp->name) {
556                         cmd_len = strlen(ctp->name);
557                         if (strncmp(ctp->name,cmdname,cmd_len) == 0) {
558                                 mycmd = ctp->cmd;
559                                 break;
560                         }
561                         ctp++;
562                 }
563         }
564
565         switch (mycmd) {
566         case CMD_CREATE_TDB:
567                 bIterate = 0;
568                 create_tdb(arg1);
569                 return 0;
570         case CMD_OPEN_TDB:
571                 bIterate = 0;
572                 open_tdb(arg1);
573                 return 0;
574         case CMD_SYSTEM:
575                 /* Shell command */
576                 if (system(arg1) == -1) {
577                         terror("system() call failed\n");
578                 }
579                 return 0;
580         case CMD_QUIT:
581                 return 1;
582         default:
583                 /* all the rest require a open database */
584                 if (!tdb) {
585                         bIterate = 0;
586                         terror("database not open");
587                         help();
588                         return 0;
589                 }
590                 switch (mycmd) {
591                 case CMD_TRANSACTION_START:
592                         bIterate = 0;
593                         tdb_transaction_start(tdb);
594                         return 0;
595                 case CMD_TRANSACTION_COMMIT:
596                         bIterate = 0;
597                         tdb_transaction_commit(tdb);
598                         return 0;
599                 case CMD_TRANSACTION_CANCEL:
600                         bIterate = 0;
601                         tdb_transaction_cancel(tdb);
602                         return 0;
603                 case CMD_ERASE:
604                         bIterate = 0;
605                         tdb_traverse(tdb, do_delete_fn, NULL);
606                         return 0;
607                 case CMD_DUMP:
608                         bIterate = 0;
609                         tdb_traverse(tdb, print_rec, NULL);
610                         return 0;
611                 case CMD_INSERT:
612                         bIterate = 0;
613                         insert_tdb(arg1, arg1len,arg2,arg2len);
614                         return 0;
615                 case CMD_MOVE:
616                         bIterate = 0;
617                         move_rec(arg1,arg1len,arg2);
618                         return 0;
619                 case CMD_STORE:
620                         bIterate = 0;
621                         store_tdb(arg1,arg1len,arg2,arg2len);
622                         return 0;
623                 case CMD_SHOW:
624                         bIterate = 0;
625                         show_tdb(arg1, arg1len);
626                         return 0;
627                 case CMD_KEYS:
628                         tdb_traverse(tdb, print_key, NULL);
629                         return 0;
630                 case CMD_HEXKEYS:
631                         tdb_traverse(tdb, print_hexkey, NULL);
632                         return 0;
633                 case CMD_DELETE:
634                         bIterate = 0;
635                         delete_tdb(arg1,arg1len);
636                         return 0;
637                 case CMD_LIST_HASH_FREE:
638                         tdb_dump_all(tdb);
639                         return 0;
640                 case CMD_LIST_FREE:
641                         tdb_printfreelist(tdb);
642                         return 0;
643                 case CMD_INFO:
644                         info_tdb();
645                         return 0;
646                 case CMD_SPEED:
647                         speed_tdb(arg1);
648                         return 0;
649                 case CMD_MMAP:
650                         toggle_mmap();
651                         return 0;
652                 case CMD_FIRST:
653                         bIterate = 1;
654                         first_record(tdb, &iterate_kbuf);
655                         return 0;
656                 case CMD_NEXT:
657                         if (bIterate)
658                                 next_record(tdb, &iterate_kbuf);
659                         return 0;
660                 case CMD_CHECK:
661                         check_db(tdb);
662                         return 0;
663                 case CMD_HELP:
664                         help();
665                         return 0;
666                 case CMD_CREATE_TDB:
667                 case CMD_OPEN_TDB:
668                 case CMD_SYSTEM:
669                 case CMD_QUIT:
670                         /*
671                          * unhandled commands.  cases included here to avoid compiler
672                          * warnings.
673                          */
674                         return 0;
675                 }
676         }
677
678         return 0;
679 }
680
681 static char *convert_string(char *instring, size_t *sizep)
682 {
683         size_t length = 0;
684         char *outp, *inp;
685         char temp[3];
686
687         outp = inp = instring;
688
689         while (*inp) {
690                 if (*inp == '\\') {
691                         inp++;
692                         if (*inp && strchr("0123456789abcdefABCDEF",(int)*inp)) {
693                                 temp[0] = *inp++;
694                                 temp[1] = '\0';
695                                 if (*inp && strchr("0123456789abcdefABCDEF",(int)*inp)) {
696                                         temp[1] = *inp++;
697                                         temp[2] = '\0';
698                                 }
699                                 *outp++ = (char)strtol((const char *)temp,NULL,16);
700                         } else {
701                                 *outp++ = *inp++;
702                         }
703                 } else {
704                         *outp++ = *inp++;
705                 }
706                 length++;
707         }
708         *sizep = length;
709         return instring;
710 }
711
712 int main(int argc, char *argv[])
713 {
714         cmdname = "";
715         arg1 = NULL;
716         arg1len = 0;
717         arg2 = NULL;
718         arg2len = 0;
719
720         if (argv[1]) {
721                 cmdname = "open";
722                 arg1 = argv[1];
723                 do_command();
724                 cmdname =  "";
725                 arg1 = NULL;
726         }
727
728         switch (argc) {
729         case 1:
730         case 2:
731                 /* Interactive mode */
732                 while ((cmdname = tdb_getline("tdb> "))) {
733                         arg2 = arg1 = NULL;
734                         if ((arg1 = strchr((const char *)cmdname,' ')) != NULL) {
735                                 arg1++;
736                                 arg2 = arg1;
737                                 while (*arg2) {
738                                         if (*arg2 == ' ') {
739                                                 *arg2++ = '\0';
740                                                 break;
741                                         }
742                                         if ((*arg2++ == '\\') && (*arg2 == ' ')) {
743                                                 arg2++;
744                                         }
745                                 }
746                         }
747                         if (arg1) arg1 = convert_string(arg1,&arg1len);
748                         if (arg2) arg2 = convert_string(arg2,&arg2len);
749                         if (do_command()) break;
750                 }
751                 break;
752         case 5:
753                 arg2 = convert_string(argv[4],&arg2len);
754         case 4:
755                 arg1 = convert_string(argv[3],&arg1len);
756         case 3:
757                 cmdname = argv[2];
758         default:
759                 do_command();
760                 break;
761         }
762
763         if (tdb) tdb_close(tdb);
764
765         return 0;
766 }