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